Exemple #1
0
void Image::Convolve(int *filter, int n, int normalization, int absval) {
  // This is my definition of an auxiliary function for image convolution 
  // with an integer filter of width n and certain normalization.
  // The absval param is to consider absolute values for edge detection.
  
  // It is helpful if you write an auxiliary convolve function.
  // But this form is just for guidance and is completely optional.
  // Your solution NEED NOT fill in this function at all
  // Or it can use an alternate form or definition
  int sumr, sumg, sumb;
  sumr = sumg = sumb = 0;

  for (int h = 0; h < height; h++) {
    for (int w = 0; w < width; w++) {
      for (int p = 0; p < n; p++) {
        for (int q = 0; q < n; q++) {
          int x = 0;
          int y = 0;
          int mp = 0;
          if (absval) {
            mp = 2;
            x = Lim(w - (p - mp), width);
            y = Lim(h - (q - mp), height);
            //x = w - (p - mp);
            //y = h - (q - mp); 
          }
          else {
            mp = n / 2;
            x = LimRef(w - (p - mp), width);
            y = LimRef(h - (q - mp), height);
          }
          if (!ValidCoord(x, y)) continue;
          Pixel curr = GetPixel(x, y);
          int filt = filter[q * n + p];
          sumr += (int)curr.r * filt;
          sumg += (int)curr.g * filt;
          sumb += (int)curr.b * filt;
        }
      }
      if (absval) {
        sumr = abs(sumr);
        sumg = abs(sumg);
        sumb = abs(sumb);
      }
      GetPixel(w, h).SetClamp(sumr / normalization, sumg / normalization, sumb / normalization);
      sumr = sumg = sumb = 0;
    }
  }
}
Exemple #2
0
int main(int argc, char *argv[]) {
	ub4 tots=0;
	ub4 ts, r, bts, div;
	ub4 q1 = 24;
	ub4 q2 = 28;
	double totSigma=0.0;
	double totRange=0.0;
	double sigma,range,mSigma,mRange;
	// timer
	time_t a,z;
	// rounds
	qi = pow(2,q1);
	// check the command line
	if (argc<=1) { usage(); exit(0); }
	if (argc>=2) q1 = atoi(argv[1]);
	if (argc>=3) q2 = atoi(argv[2]);
	if (argc>=4) rng= atoi(argv[3]) % 9;
	if (argc>=5) strcpy(s,argv[4]);
	
	#ifdef TEST
	#ifdef __TINYC__
		puts("Tiny C");
	#endif
	#ifdef __WATCOMC__
		puts("Open Watcom C");
	#endif
	#ifdef _MSC_VER
		puts("Microsoft Visual C");
	#endif
	#ifdef __GNUC__
		puts("GNU C");
	#endif
	#endif
	#if __STDC_VERSION__ >= 199901L
		puts("C99 supported.");
	#endif

	#ifdef MOD
	printf("MOD: ");
	#endif
	#ifdef LIM
	printf("LIM: ");
	#endif
	#ifdef SAM
	printf("SAM: ");
	#endif

	div = q2-q1+1;
	printf("%d %s trial sets in [2**%d..2**%d]\n",div,RNGs[rng],q1,q2);
		
	puts("Trial      Range        Sigma         Time");
	puts("------------------------------------------");
	
	for (j=q1; j<=q2; j++) {
		for (i=0; i<MODU; i++) totals[i]=0;
		probtot = 0.0;
		qi = pow(2,j);
		rSeed(rng,s,rStateSize(rng)*7);
		time(&a);
		
		for (q=0; q<qi; q++) { 
			#ifdef MOD
			r=rRandom(rng) % MODU;
			#else
			#ifdef LIM
			r=Lim(rng);
			#endif
			r=Sam(rng);
			#endif
			totals[r]++;
		}
		
		time(&z);
		ts=(size_t)(z-a);
		tots+=ts;
	
		for (i=0; i<MODU; i++) {
			// expected probabilities
			expect[i] = (double)1/MODU;
			// actual probabilities
			prob[i]=(double)totals[i]/qi;
			// probtot holds total of probabilities - it should converge to 1.0
			probtot=probtot+prob[i];
			// collect value-names & decide output format
			values[i] = i + 'A';
		}
		sigma = Sigma(0,MODM1);
		range = prob[MaxP(0,MODM1)]-prob[MinP(0,MODM1)];
		totSigma+=sigma; totRange+=range;
		printf("2**%d    %1.7f    %1.7f      %3d s\n",j,range,sigma,ts);
	}
	puts("------------------------------------------");
	mSigma = (double)totSigma/div; mRange = (double)totRange/div;
	    printf("Mean:    %1.7f    %1.7f     %4d s\n",mRange,mSigma,tots);
	#ifdef TEST
	puts(""); for (i=0; i<MODU; i++) printf("%2c) %1.7f\n",values[i],prob[i]);
	#endif
	return 0;
}