int main(int argc, char **argv) { bmpfile_t *bmp; int i, j; char* infilename; FILE* infile; char* outfile; int width; int height; int depth; unsigned char red, green, blue; // 8-bits each //unsigned char pixel[3]; // 24-bits per pixel if (argc < 6) { printf("Usage: %s infile width height depth outfile.\n", argv[0]); exit(EXIT_FAILURE); } infilename = argv[1]; outfile = argv[5]; infile = fopen(infilename, "rb"); if (NULL == infile) { perror("Couldn't read infile"); exit(EXIT_FAILURE); } width = atoi(argv[2]); height = atoi(argv[3]); depth = atoi(argv[4]); // should be depth/8 at 16-bit depth, but 32-bit depth works better char buffer[height * width * 3]; printf("depth: %d\n", depth); if (fread(&buffer, 1, height * width * 3, infile) != height * width * 3) { fputs("infile dimensions don't match the size you supplied\n", stderr); } if ((bmp = bmp_create(width, height, depth)) == NULL) { printf("Invalid depth value: '%d'. Try 1, 4, 8, 16, 24, or 32.\n", depth); exit(EXIT_FAILURE); } for (i = 0; i < width; ++i) { for (j = 0; j < height; ++j) { red = buffer[(width * j + i) * 3 + 0]; green = buffer[(width * j + i) * 3 + 1]; blue = buffer[(width * j + i) * 3 + 2]; rgb_pixel_t bpixel = {blue, green, red, 0}; bmp_set_pixel(bmp, i, j, bpixel); } } bmp_save(bmp, outfile); bmp_destroy(bmp); return 0; }
int run_hsl(int c, char* src, char* dst, float hh, float ss, float ll) { BMP* bmp = bmp_read(src); if(bmp==0) { return -1;} // open error if(ss>1) ss=1; else if(ss<-1) ss=-1; if(ll>1) ll=1; else if(ll<-1) ll=-1; uint8_t* data = bmp_get_data(bmp); uint32_t h = *(bmp_get_h(bmp)); uint32_t w = *(bmp_get_w(bmp)); if(w%4!=0) { return -1;} // do not support padding uint8_t* dataC = 0; if(*(bmp_get_bitcount(bmp)) == 24) { dataC = malloc(sizeof(uint8_t)*4*h*w); to32(w,h,data,dataC); } else { dataC = data; } if(c==0) C_hsl(w,h,dataC,hh,ss,ll); else if(c==1) ASM_hsl1(w,h,dataC,hh,ss,ll); else if(c==2) ASM_hsl2(w,h,dataC,hh,ss,ll); else {return -1;} if(*(bmp_get_bitcount(bmp)) == 24) { to24(w,h,dataC,data); free(dataC); } bmp_save(dst,bmp); bmp_delete(bmp); return 0; }
int run_blur(int c, char* src, char* dst){ BMP* bmp = bmp_read(src); if(bmp==0) { return -1;} // open error uint8_t* data = bmp_get_data(bmp); uint32_t h = *(bmp_get_h(bmp)); uint32_t w = *(bmp_get_w(bmp)); if(w%4!=0) { return -1;} // do not support padding uint8_t* dataC = 0; if(*(bmp_get_bitcount(bmp)) == 24) { dataC = malloc(sizeof(uint8_t)*4*h*w); to32(w,h,data,dataC); } else { dataC = data; } if(c==0) C_blur(w,h,dataC); else if(c==1) ASM_blur1(w,h,dataC); else if(c==2) ASM_blur2(w,h,dataC); else {return -1;} if(*(bmp_get_bitcount(bmp)) == 24) { to24(w,h,dataC,data); free(dataC); } bmp_save(dst,bmp); bmp_delete(bmp); return 0; }
void bmp_save_distance_curve (BinaryData *binary) { char bmp_filename[1024]; sprintf (bmp_filename, "%s/curves/%s_distance_curve.bmp", binary->output_folder, binary->name); bmp_save (binary->distance_curve, bmp_filename); bmp_destroy (binary->distance_curve); }
void binary_save_bmp (BinaryData *binary, unsigned long int start_offset, unsigned long int cur_offset) { char bmp_filename[1024]; static unsigned int frame_id = 0; sprintf (bmp_filename, "%s/%s/%d_%s_0x%lx-0x%lx.bmp", binary->output_folder, binary->path, frame_id++, binary->name, start_offset, cur_offset); // Output to bmp bmp_save (binary->bmp, bmp_filename); // Cleaning bmp_destroy (binary->bmp); binary->bmp = bmp_create (256, 256, 8); }
/* * Used to print out the first 100 multipliers that passed the sieve * Useful to compare with other sieve implementations */ void cSieve_debug(mpz_class& mpzFixedMultiplier, uint32 chainLength, uint32 primeFactors, uint32 sieveSize) { cSieve_prepare(mpzFixedMultiplier, chainLength, primeFactors, sieveSize); uint32 rows = 128; uint32 pixelsPerRow = cSieve->sieveSize / rows; uint8* pixelMap = (uint8*)malloc(cSieve->sieveSize*(cSieve->chainLength*2)*3); memset(pixelMap, 0x00, cSieve->sieveSize*cSieve->chainLength*2*3); uint32 pixelPerRow = cSieve->sieveSize; for(sint32 y=0; y<cSieve->chainLength; y++) { for(sint32 x=0; x<cSieve->sieveSize; x++) { uint32 rowY = y*cSieve->chainLength*2 + x / pixelsPerRow; uint32 maskBase = cSieve->maskBytes*rowY; uint32 maskIdx = x>>3; uint32 maskVal = 1<<(x&7); uint32 mC1 = cSieve->layerMaskC1[maskBase+maskIdx] & maskVal; uint32 mC2 = cSieve->layerMaskC2[maskBase+maskIdx] & maskVal; uint32 pIdx = (x+y*pixelsPerRow)*3; pixelMap[pIdx+0] = 0; pixelMap[pIdx+1] = 0; pixelMap[pIdx+2] = 0; if( mC1 == 0 ) pixelMap[pIdx+2] = 0xFF; if( mC2 == 0 ) pixelMap[pIdx+0] = 0xFF; } } bitmap_t bmp; bmp.bitDepth = 24; bmp.data = pixelMap; bmp.sizeX = pixelsPerRow; bmp.sizeY = cSieve->chainLength*rows; printf("cSieve_debug(): Saving sieve debug image...\n"); bmp_save("C:\\test\\sieve.bmp", &bmp); uint32 multiplier = 0; for(uint32 i=0; i<100; i++) { uint32 sieveFlags = 0; multiplier = cSieve_findNextMultiplier(&sieveFlags); if( multiplier == 0 ) break; printf("cSieve mult[%04d]: %d\n", i, multiplier); } }
void saveimage(){ printf("Good hits: %llu\t Miss hits: %llu\t Bad hits: %llu\t %f\n", goodHits, missHits, badHits, (f32)goodHits/(badHits > 0 ? badHits : 1)); bmpfile_t *bmp; f32 amax = __sec_reduce_max(h[0:hwid * hhei].a); amax = MAX(amax, 1); bmp = bmp_create(hwid, hhei, 24); printf("generating image"); cilk_for(i32 i = 0; i < hwid * hhei; i++){ const f32 a = log(h[i].a) / log(amax); f32 maxColor = MAX3(h[i].r, h[i].g, h[i].b); if(maxColor <= 0) maxColor = 1; const u8 r = (h[i].r / h[i].a) * 0xFF * a; const u8 g = (h[i].g / h[i].a) * 0xFF * a; const u8 b = (h[i].b / h[i].a) * 0xFF * a; const rgb_pixel_t pixel = {b, g, r, 0xFF}; const u32 x = i % hwid; const u32 y = i / hwid; bmp_set_pixel(bmp, x, y, pixel); // progress bar if((i % ((hwid * hhei) / 20)) == 0) printf("."); } printf(" done\n"); printf("saving image... "); bmp_save(bmp, "fractal.bmp"); bmp_destroy(bmp); printf("done\n"); }
int run_merge(int c, char* src1, char* src2, char* dst, float value){ if(dst==0) { return -1;} // non destine if(value>1) value=1; else if(value<0) value=0; BMP* bmp1 = bmp_read(src1); BMP* bmp2 = bmp_read(src2); if(bmp1==0 || bmp2==0) { return -1;} // open error uint8_t* data1 = bmp_get_data(bmp1); uint8_t* data2 = bmp_get_data(bmp2); uint32_t h1 = *(bmp_get_h(bmp1)); uint32_t w1 = *(bmp_get_w(bmp1)); uint32_t h2 = *(bmp_get_h(bmp2)); uint32_t w2 = *(bmp_get_w(bmp2)); if(w1%4!=0 || w2%4!=0) { return -1;} // do not support padding if( w1!=w2 || h1!=h2 ) { return -1;} // different image size uint8_t* data1C = 0; uint8_t* data2C = 0; if(*(bmp_get_bitcount(bmp1)) == 24) { data1C = malloc(sizeof(uint8_t)*4*h1*w1); data2C = malloc(sizeof(uint8_t)*4*h2*w2); to32(w1,h1,data1,data1C); to32(w2,h2,data2,data2C); } else { data1C = data1; data2C = data2; } if(c==0) C_merge(w1,h1,data1C,data2C,value); else if(c==1) ASM_merge1(w1,h1,data1C,data2C,value); else if(c==2) ASM_merge2(w1,h1,data1C,data2C,value); else {return -1;} if(*(bmp_get_bitcount(bmp1)) == 24) { to24(w1,h1,data1C,data1); free(data1C); free(data2C); } bmp_save(dst,bmp1); bmp_delete(bmp1); bmp_delete(bmp2); return 0; }
int doTest(const char *ext, int width, int align, int height, enum BMPPF pf, enum BMPORN orientation) { char filename[80], *md5sum, md5buf[65]; int pitch=BMPPAD(width*bmp_ps[pf], align), loadWidth=0, loadHeight=0, retval=0; unsigned char *buf=NULL; char *md5ref=!stricmp(ext, "ppm")? "c0c9f772b464d1896326883a5c79c545": "b03eec1eaaad38fed9cab5082bf37e52"; if((buf=(unsigned char *)malloc(pitch*height))==NULL) _throw("Could not allocate memory"); initBuf(buf, width, pitch, height, pf, orientation); snprintf(filename, 80, "bmptest_%s_%d_%s.%s", pfStr[pf], align, orientation==BMPORN_TOPDOWN? "td":"bu", ext); if(bmp_save(filename, buf, width, pitch, height, pf, orientation)==-1) _throw(bmp_geterr()); md5sum=MD5File(filename, md5buf); if(stricmp(md5sum, md5ref)) _throwmd5(filename, md5sum, md5ref); free(buf); buf=NULL; if(bmp_load(filename, &buf, &loadWidth, align, &loadHeight, pf, orientation)==-1) _throw(bmp_geterr()); if(width!=loadWidth || height!=loadHeight) { printf("\n Image dimensions of %s are bogus\n", filename); retval=-1; goto bailout; } if(!cmpBuf(buf, width, pitch, height, pf, orientation)) { printf("\n Pixel data in %s is bogus\n", filename); retval=-1; goto bailout; } unlink(filename); bailout: if(buf) free(buf); return retval; }
int main(int argc, char **argv) { bmpfile_t *bmp_resized = NULL; bmpfile_t *result = NULL; int width, height,num_threads, i, j; float scale; // Parse argv. scale = (float)atof(argv[2]); num_threads = atoi(argv[3]); omp_set_num_threads(num_threads); bmp_resized = bmp_scale( argv[1], scale, num_threads); width = bmp_get_dib(bmp_resized).width; height = bmp_get_dib(bmp_resized).height; result = bmp_create(width, height, 8); if( result != NULL){ //printf(" about to make new file!\n"); //#pragma omp parallel for private(j) for (i = 0; i < width; ++i) { for (j = 0; j < height; ++j) { int num = omp_get_thread_num(); //printf("Thread %i has i = %i and j = %i\n", num, i, j); rgb_pixel_t *p = bmp_get_pixel(bmp_resized, i, j); bmp_set_pixel(result, i, j, *p); } } //printf("made new file!\n"); bmp_save(result, "bmp_scale.bmp"); } //printf("finished saving!\n"); bmp_destroy(bmp_resized); return 0; }
void binary_save_frame (BinaryData *binary, unsigned long int start_offset, unsigned long int cur_offset) { char bmp_filename[1024]; static unsigned int frame_id = 0; sprintf (bmp_filename, "%s/%s/%d_%s_FFT_0x%lx-0x%lx.bmp", binary->output_folder, binary->path, frame_id++, binary->name, start_offset, cur_offset); // Convert frame to complex matrix and compute FFT COMPLEX **c = frame_to_complex (binary->frame); FFT2D (c, binary->frame->size, binary->frame->size, 1); // Output to bmp bmpfile_t *bmp = complex_to_bmp (c, binary->frame->size); bmp_save (bmp, bmp_filename); // Cleaning bmp_destroy (bmp); frame_reset (binary->frame); }
void test_bmpCreate(CuTest* tc) { bmpfile_t* bmp; int i,j; int width = 64, height = 64, depth = 1; bw_pixel_t pixel; pixel.uniColour = 128; CuAssertTrue(tc,NULL!=(bmp=bmp_create(64,64,1))); for (i = 10, j = 10; j < 64; ++i, ++j) { bmp_set_pixel(bmp, i, j, pixel); pixel.uniColour++; bmp_set_pixel(bmp, i + 1, j, pixel); bmp_set_pixel(bmp, i, j + 1, pixel); } CuAssertTrue(tc,width==bmp_get_width(bmp)); CuAssertTrue(tc,height==bmp_get_height(bmp)); CuAssertTrue(tc,depth==bmp_get_depth(bmp)); bmp_save(bmp, "testimage.bmp"); bmp_destroy(bmp); }
void naCreateABmp(JNIEnv* env, jclass clazz, jint width, jint height, jint depth) { bmpfile_t *bmp; int i, j; rgb_pixel_t pixel = {128, 64, 0, 0}; if (NULL == (bmp = bmp_create(width, height, depth))) { LOGE(1, "Invalid depth value: %d. (valid values include 1, 4, 8, 16, 24, 32)\n", depth); return; } for (i = 10, j = 10; j < height; ++i, ++j) { bmp_set_pixel(bmp, i, j, pixel); pixel.red++; pixel.green++; pixel.blue++; bmp_set_pixel(bmp, i + 1, j, pixel); bmp_set_pixel(bmp, i, j + 1, pixel); } bmp_save(bmp, "/sdcard/test_bs_static.bmp"); bmp_destroy(bmp); }
void read_pcx(char *uncomp, uint32_t size, char *name) { struct h3pcx_indexed *h3_img = NULL; BMP *b = NULL; char output_name[500]; size = size; h3_img = (struct h3pcx_indexed*)uncomp; printf("Bitmap Size = %d\n", h3_img->bitmap_size); printf("Width = %d\n", h3_img->width); printf("Height = %d\n", h3_img->height); h3_img->bitmap = (uint8_t*)(uncomp + 3 * sizeof(uint32_t)); b = (BMP*)(uncomp + sizeof(uint32_t)); printf("b->Width = %d\n", b->width); printf("b->heigh = %d\n", b->height); b->data = (uint8_t*)(uncomp + 3 * sizeof(uint32_t)); strcpy(output_name, "./extract/"); strcat(output_name, name); strcat(output_name, ".bmp"); bmp_save(b, output_name); }
int draw_reconstruction_bitmap(PSIRT *psirt) { int i=0,j=0; double* pixel_intensity = reconstruction(psirt); // DESENHAR bmpfile_t *bmp = bmp_create(RES_X, RES_Y, 24); for (i = 0; i < RES_X; i++) { for (j = 0; j < RES_Y; j++) { double pixel = pixel_intensity[i + (j * RES_X)]; // Alternativa 1: função linear simples int paint_of_choice = (pixel * 255); // Alternativa 2: função polinomial #ifdef REC_PAINT_POLY paint_of_choice = pow(pixel, 2) * 255; #endif // Alternativa 3: função exponencial #ifdef REC_PAINT_EXP paint_of_choice = pow(pixel, pixel) * 255; #endif // Alternativa 3: função customizada #ifdef REC_PAINT_CUSTOM paint_of_choice = 255; if (pixel < .2) paint_of_choice = 0; else if (pixel < .4) paint_of_choice = 255 / 4; #endif rgb_pixel_t pix = { paint_of_choice, paint_of_choice, paint_of_choice, 0 }; bmp_set_pixel(bmp, i, j, pix); } } bmp_save(bmp, "psirt_output.bmp"); bmp_destroy(bmp); free(pixel_intensity); return i; }
int main() { int numFractals; scanf("%d", &numFractals); int depth; double angle, heading; char axiom[MAX_CHARS]; bmp = bmp_create(300 * numFractals, 300, 32); resetImage(300 * numFractals, 300); int i; for (i = 0; i < numFractals; i++) { scanf("%d", &depth); scanf("%lf", &angle); scanf("%lf", &heading); angle = degreesToRadians(angle); currH = degreesToRadians(heading); scanf("%s", axiom); scanf("%d", &numRules); int j; for (j = 0; j < numRules; j++) { char tmp[2]; scanf("%s -> %s\n", tmp, rules[j]); codes[j] = tmp[0]; } drawFractal(axiom, heading, angle, depth, i); } bmp_save(bmp, "output.bmp"); bmp_destroy(bmp); }
void draw_projection_bitmap(PSIRT* psirt) { bmpfile_t *bmp_proj = bmp_create(RES_X, RES_Y, 24); int i, j; for (i = 0; i < psirt->n_projections; i++) { for (j = 0; j < psirt->n_trajectories; j++) { Trajectory* t = psirt->projections[i]->lista_trajetorias[j]; Vector2D begin, end, d; sum_void(t->source, t->direction, &begin); d.x = t->direction->x; d.y = t->direction->y; mult_constant_void(&d, -1); sum_void(t->source, &d, &end); // Desenhar linha double delta = (begin.y-end.y)/(begin.x-end.x); double x, y; int k = 0; for (k = -1000; k < 1000; k++) { x = k*0.001f; y = ( delta*(x-begin.x) ) + begin.y; rgb_pixel_t pix = {0,0,255,0}; bmp_set_pixel(bmp_proj, x*RES_X+RES_X/2, y*RES_Y+RES_Y/2, pix); } } } bmp_save(bmp_proj, "projections_output.bmp"); bmp_destroy(bmp_proj); }
int main(int argc, char* argv[]){ int i,j; // (0) leer parametros options opt; if (argc == 1) {print_help(argv[0]); return 0;} if(read_options(argc, argv, &opt)) {printf("ERROR reading parameters\n"); return 1;} int len1 = strlen(opt.file1); int len2 = strlen(opt.file2); if( strcmp(&(opt.file1[len1-4]),".bmp") || strcmp(&(opt.file2[len2-4]),".bmp") ) { printf("ERROR: nombre del archivo\n"); return -1;} // (0.1) siempre armo el summary opt.summary = (int*)malloc(sizeof(int)*256); for(i=0;i<256;i++) opt.summary[i]=0; // (1) leer imagenes BMP* bmp1 = bmp_read(opt.file1); BMP* bmp2 = bmp_read(opt.file2); if( bmp1==0 || bmp1==0 ) { printf("ERROR: no se puede abrir el archivo\n"); return -1;} // (2) check tipo de archivo if( ((BMPIH*)(bmp1->ih))->biSize != ((BMPIH*)(bmp1->ih))->biSize ) { printf("ERROR: tipo de archivo diferente\n"); return -1;} // (3) check tamaño del archivo int w1 = ((BMPIH*)(bmp1->ih))->biWidth; int h1 = ((BMPIH*)(bmp1->ih))->biHeight; int c1 = ((BMPIH*)(bmp1->ih))->biBitCount; int w2 = ((BMPIH*)(bmp2->ih))->biWidth; int h2 = ((BMPIH*)(bmp2->ih))->biHeight; int c2 = ((BMPIH*)(bmp2->ih))->biBitCount; if( w1!=w2 || h1!=h2 || c1!=c2 ) { printf("ERROR: tamaño de archivo diferente\n"); return -1;} //printf("%i=%i %i=%i %i=%i\n",w1,w2,h1,h2,c1,c2); if(w1%4!=0) { printf("ERROR: padding no soportado\n"); return -1;} // TODO: soportar padding! // (3) check el bit count TODO: only 24 o 32 if( c1!=24 && c1!=32 ) { printf("ERROR: (%i) bitcount distinto de 24 o 32\n", c1); return -1;} // (4) crear imagenes de diferencias BMP *bmpDiffR, *bmpDiffG, *bmpDiffB, *bmpDiffA; bmpDiffR = bmp_copy(bmp1,0); bmpDiffG = bmp_copy(bmp1,0); bmpDiffB = bmp_copy(bmp1,0); if(c1 == 32) bmpDiffA = bmp_copy(bmp1,0); // (5) extraer data uint8_t *data1,*data2,*dataR,*dataG,*dataB,*dataA; data1 = bmp_get_data(bmp1); data2 = bmp_get_data(bmp2); dataR = bmp_get_data(bmpDiffR); dataG = bmp_get_data(bmpDiffG); dataB = bmp_get_data(bmpDiffB); if(c1 == 32) dataA = bmp_get_data(bmpDiffA); // (6) calcular diferencias if(c1 == 32) for(j=0;j<h1;j++) { for(i=0;i<w1;i++) { int pos = (j*w1+i)*4; uint8_t R1 = data1[pos+3]; uint8_t G1 = data1[pos+2]; uint8_t B1 = data1[pos+1]; uint8_t A1 = data1[pos+0]; uint8_t R2 = data2[pos+3]; uint8_t G2 = data2[pos+2]; uint8_t B2 = data2[pos+1]; uint8_t A2 = data2[pos+0]; dataR[pos+3] = cmp(R1,R2,i,j,"R",&opt); dataR[pos+2] = dataR[pos+3]; dataR[pos+1] = dataR[pos+3]; dataR[pos+0] = 255; dataG[pos+3] = cmp(G1,G2,i,j,"G",&opt); dataG[pos+2] = dataG[pos+3]; dataG[pos+1] = dataG[pos+3]; dataG[pos+0] = 255; dataB[pos+3] = cmp(B1,B2,i,j,"B",&opt); dataB[pos+2] = dataB[pos+3]; dataB[pos+1] = dataB[pos+3]; dataB[pos+0] = 255; dataA[pos+3] = cmp(A1,A2,i,j,"A",&opt); dataA[pos+2] = dataA[pos+3]; dataA[pos+1] = dataA[pos+3]; dataA[pos+0] = 255; } } if(c1 == 24) for(j=0;j<h1;j++) { for(i=0;i<w1;i++) { int pos = (j*w1+i)*3; uint8_t R1 = data1[pos+2]; uint8_t G1 = data1[pos+1]; uint8_t B1 = data1[pos+0]; uint8_t R2 = data2[pos+2]; uint8_t G2 = data2[pos+1]; uint8_t B2 = data2[pos+0]; dataR[pos+2] = cmp(R1,R2,i,j,"R",&opt); dataR[pos+1] = dataR[pos+2]; dataR[pos+0] = dataR[pos+2]; dataG[pos+2] = cmp(G1,G2,i,j,"G",&opt); dataG[pos+1] = dataG[pos+2]; dataG[pos+0] = dataG[pos+2]; dataB[pos+2] = cmp(B1,B2,i,j,"B",&opt); dataB[pos+1] = dataB[pos+2]; dataB[pos+0] = dataB[pos+2]; } } // (7) mostrar summary if(opt.summaryop) { for(i=1;i<256;i++) if(opt.summary[i]!=0) printf("%i\t%i\n",i,opt.summary[i]); } // (8) guardar resultados if(opt.image) { char* strX = "diffX.bmp"; char* fileSto = malloc(strlen(opt.file1)+5+1); strcpy(fileSto,opt.file1); strcpy(fileSto+len1-4,strX); fileSto[len1]='R'; bmp_save(fileSto,bmpDiffR); fileSto[len1]='G'; bmp_save(fileSto,bmpDiffG); fileSto[len1]='B'; bmp_save(fileSto,bmpDiffB); fileSto[len1]='A'; if(c1 == 32) bmp_save(fileSto,bmpDiffA); // (8.1) borrar las imagenes bmp_delete(bmp1); bmp_delete(bmp2); bmp_delete(bmpDiffR); bmp_delete(bmpDiffG); bmp_delete(bmpDiffB); if(c1 == 32) bmp_delete(bmpDiffA); } // (9) retorno error si encontre una diferencia for(i=opt.epsilon;i<256;i++) if(opt.summary[i]>0) return -1; return 0; }
void imagenes_guardar(configuracion_t *config) { bmp_save(config->archivo_salida, dst_img); }
int main(int argc, char **argv) { unsigned char *img1=NULL, *img2=NULL, *errImg=NULL; int usePPM=0; unsigned char err, pixelErr, maxCompErr[4]={ 0, 0, 0, 0 }, minCompErr[4]={ 255, 255, 255, 255 }, maxTotalErr, minTotalErr; double avgCompErr[4]={ 0., 0., 0., 0. }, avgTotalErr=0., compSumSquares[4]={ 0., 0., 0., 0. }, totalSumSquares=0., rms[4]={ 0., 0., 0., 0. }, totalRMS; int width1, height1, ps1=3, width2, height2, ps2=3, width, height, i, j, k, mag=0; char *temp; if(argc<3) { printf("\nUSAGE: %s <image 1> <image 2> [-mag]\n", argv[0]); printf(" (images must be in BMP or PPM format)\n"); printf("\n-mag = show magnitude of differences using an artificial\n"); printf(" color scale\n\n"); exit(1); } if(argc>3 && !strcmp(argv[3], "-mag")) mag=1; if((temp=strrchr(argv[1], '.'))!=NULL && !stricmp(temp, ".ppm")) usePPM=1; if(mag) { for(i=0; i<64; i++) { redMap[i]=0; greenMap[i]=i*4; blueMap[i]=0; } for(i=0; i<64; i++) { redMap[i+64]=i*4; greenMap[i+64]=255; blueMap[i+64]=0; } for(i=0; i<64; i++) { redMap[i+128]=255; greenMap[i+128]=255-i*4; blueMap[i+128]=0; } for(i=0; i<64; i++) { redMap[i+192]=255; greenMap[i+192]=i*4; blueMap[i+192]=i*4; } } if(bmp_load(argv[1], &img1, &width1, 1, &height1, BMPPF_BGR, BMPORN_TOPDOWN)==-1) { puts(bmp_geterr()); exit(1); } if(bmp_load(argv[2], &img2, &width2, 1, &height2, BMPPF_BGR, BMPORN_TOPDOWN)==-1) { puts(bmp_geterr()); exit(1); } width=min(width1, width2); height=min(height1, height2); if((errImg=(unsigned char *)malloc(width*height*ps1))==NULL) { puts("Could not allocate memory"); exit(1); } for(j=0; j<height; j++) for(i=0; i<width; i++) { pixelErr=0; for(k=0; k<ps1; k++) { err=(unsigned char)abs((int)img2[(width2*j+i)*ps2+k] -(int)img1[(width1*j+i)*ps1+k]); if(err>pixelErr) pixelErr=err; if(err>maxCompErr[k]) maxCompErr[k]=err; if(err<minCompErr[k]) minCompErr[k]=err; avgCompErr[k]+=err; compSumSquares[k]+=err*err; if(!mag) errImg[(width*j+i)*ps1+k]=err; } if(mag) { errImg[(width*j+i)*ps1]=blueMap[pixelErr]; errImg[(width*j+i)*ps1+1]=greenMap[pixelErr]; errImg[(width*j+i)*ps1+2]=redMap[pixelErr]; } } maxTotalErr=maxCompErr[0]; minTotalErr=minCompErr[0]; for(k=0; k<ps1; k++) { if(minCompErr[k]<maxTotalErr) minTotalErr=minCompErr[k]; if(maxCompErr[k]>maxTotalErr) maxTotalErr=maxCompErr[k]; avgTotalErr+=avgCompErr[k]; avgCompErr[k]/=((double)height*(double)width); totalSumSquares+=compSumSquares[k]; compSumSquares[k]/=((double)height*(double)width); rms[k]=sqrt(compSumSquares[k]); } avgTotalErr/=((double)height*(double)width*(double)ps1); totalSumSquares/=((double)height*(double)width*(double)ps1); totalRMS=sqrt(totalSumSquares); if(bmp_save(usePPM? "diff.ppm":"diff.bmp", errImg, width, 0, height, BMPPF_BGR, BMPORN_TOPDOWN)==-1) { puts(bmp_geterr()); exit(1); } free(errImg); for(k=0; k<ps1; k++) printf("%s: min err.= %d max err.= %d avg err.= %f rms= %f PSNR= %f\n", k==0? "B":(k==1? "G":(k==2? "R":"A")), minCompErr[k], maxCompErr[k], avgCompErr[k], rms[k], 20.*log10(255./rms[k])); printf("T: min err.= %d max err.= %d avg err.= %f rms= %f PSNR= %f\n", minTotalErr, maxTotalErr, avgTotalErr, totalRMS, 20.*log10(255./totalRMS)); return 0; }
int main(int argc, char** argv) { // declare and initialize variables bmpfile_t *bmp_read = NULL, *bmp_with_corners = NULL, *bmp_gray = NULL; int x = 0, y = 0, u = 0, v = 0, a = 0, b = 0, width = 0, height = 0, window_x = 1, window_y = 1, threshold = 20; float** cornerness_map = NULL; // arg count must be at least 2 to have a filename arg if ( argc < 3 ) { printf("Usage: %s input_file output_file [threshold]\n", argv[0]); return 1; } if ( argc >= 4 ) threshold = atoi(argv[3]); // read in the BMP bmp_read = bmp_create_8bpp_from_file(argv[1]); // if bmp_read is null, the file wasn't found if ( !bmp_read ) { printf("File %s could not be found\n", argv[1]); return 1; } // get the height and width so we can build our map and new image width = bmp_get_dib(bmp_read).width; height = bmp_get_dib(bmp_read).height; // construct the cornerness map cornerness_map = (float**) malloc( sizeof(float*) * width ); for ( x = 0; x < width; x++ ) cornerness_map[x] = (float*) malloc( sizeof(float) * height ); // calculate V_u,v(x,y) for ( x = 0; x < width; x++ ) { for ( y = 0; y < height; y++ ) { if ( x < 1 + HALF_WINDOW_WIDTH || x > width - 1 - (1 + HALF_WINDOW_WIDTH) || y < 1 + HALF_WINDOW_WIDTH || y > height - 1 - (1 + HALF_WINDOW_WIDTH) ) cornerness_map[x][y] = 0; else { // the cornerness map should have the minumum V_u,v(x,y) float minV; float currV; minV = -1; for ( u = -1; u <= 1; u++ ) { for ( v = -1; v <= 1; v++ ) { if ( u != 0 || v != 0 ) { currV = 0; for ( a = -HALF_WINDOW_WIDTH; a <= HALF_WINDOW_WIDTH; a++ ) { for ( b = -HALF_WINDOW_WIDTH; b <= HALF_WINDOW_WIDTH; b++ ) { rgb_pixel_t *pixel_A = bmp_get_pixel(bmp_read, x + u + a, y + v + b); float intensity_A = 0.2989 * (*pixel_A).red + 0.5870 * (*pixel_A).green + 0.1140 * (*pixel_A).blue; rgb_pixel_t *pixel_B = bmp_get_pixel(bmp_read, x + a, y + b); float intensity_B = 0.2989 * (*pixel_B).red + 0.5870 * (*pixel_B).green + 0.1140 * (*pixel_B).blue; currV += (intensity_A - intensity_B) * (intensity_A - intensity_B); } } if ( minV == -1 || currV < minV ) minV = currV; } } } // only keep the values above the threshold if ( minV > threshold ) cornerness_map[x][y] = minV; else cornerness_map[x][y] = 0; } } } // image with corners highlighted /* bmp_with_corners = bmp_create(width, height, DEPTH); rgb_pixel_t highlight = {255,0,0,0}; // modify original bmp to include highlight color printf("ncolors in bmp_read: %u\n", bmp_get_dib(bmp_read).ncolors); rgb_pixel_t* unusedColor; for ( x = 0; x < bmp_get_dib(bmp_read).ncolors; x++ ) unusedColor = &(bmp_read->colors[x]);*/ // draw picture without using nonmaximal supression /* for ( x = 0; x < width; x++ ) { for ( y = 0; y < height; y++ ) { if ( cornerness_map[x][y] > 0 ) { bmp_set_pixel(bmp_with_corners, x, y, highlight); } else { rgb_pixel_t *pixel = bmp_get_pixel(bmp_read, x, y); bmp_set_pixel(bmp_with_corners, x, y, *pixel); } } } */ // draw picture with nonmaximal suppression rgb_pixel_t highlight = {255,0,0,0}; for ( x = HALF_NEIGHBORHOOD_WIDTH; x < width - HALF_NEIGHBORHOOD_WIDTH; x++) { for ( y = HALF_NEIGHBORHOOD_WIDTH; y < height - HALF_NEIGHBORHOOD_WIDTH; y++ ) { int value = cornerness_map[x][y]; int isMax = 1; for ( u = x-HALF_NEIGHBORHOOD_WIDTH; u <= x+HALF_NEIGHBORHOOD_WIDTH; u++ ) { for ( v = y-HALF_NEIGHBORHOOD_WIDTH; v <= y+HALF_NEIGHBORHOOD_WIDTH; v++ ) { if ( u != x || v != y ) { if ( isMax == 1 && cornerness_map[u][v] < value ) isMax = 1; else isMax = 0; } } } if (isMax == 1) bmp_set_pixel(bmp_read, x, y, highlight); /* else bmp_set_pixel(bmp_with_corners, x, y, *bmp_get_pixel(bmp_read, x, y));*/ } } bmp_save(bmp_read, argv[2]); // clean up bmp_destroy(bmp_read); bmp_destroy(bmp_with_corners); for ( x = 0; x < width; x++ ) free(cornerness_map[x]); free(cornerness_map); return 0; }
int main(){ /* ======================================================================= */ /* === EJ 1 : crear un bmp de 24 bits de 100x100 y dibujar un patron bayer */ // creo el header de una imagen de 100x100 de 24 bits BMPIH* imgh1 = get_BMPIH(100,100); // crea una imagen bmp inicializada BMP* bmp1 = bmp_create(imgh1,1); // obtengo la data y dibujo el patron bayer uint8_t* data1 = bmp_get_data(bmp1); int i,j; for(j=0;j<100;j=j+2) { for(i=0;i<100;i=i+2) { data1[j*300+3*i+1] = 0xff; data1[j*300+3*i+3] = 0xff; } for(i=0;i<100;i=i+2) { data1[j*300+300+3*i+2] = 0xff; data1[j*300+300+3*i+4] = 0xff; } } // guardo la imagen bmp_save(FILE1, bmp1); // borrar bmp bmp_delete(bmp1); /* ======================================================================= */ /* === EJ 2 : crear un bmp de 32 bits de 100x100 y fondo blanco en degrade */ // creo el encavezado de una imagen de 640x480 de 32 bits BMPV5H* imgh2 = get_BMPV5H(100,100); // crea una imagen bmp no inicializada BMP* bmp2 = bmp_create(imgh2,0); // obtengo la data y dibujo el degrade uint8_t* data2 = bmp_get_data(bmp2); for(j=0;j<100;j++) { for(i=0;i<100;i++) { data2[j*400+i*4+0] = (uint8_t)(((float)(i+j))*(256.0/200.0)); data2[j*400+i*4+1] = 0xff; data2[j*400+i*4+2] = 0xff; data2[j*400+i*4+3] = 0xff; } } // guardo la imagen bmp_save(FILE2, bmp2); // borrar bmp bmp_delete(bmp2); /* ======================================================================= */ /* === EJ 3 : crear un bmp con el mapa de bits de bmp1 y el alpha de bmp2 */ // Abro los dos archivos BMP* bmp1n = bmp_read(FILE1); BMP* bmp2n = bmp_read(FILE2); // copio la imagen con transparecia sin datos BMP* bmpNEW = bmp_copy(bmp2n, 0); // obtengo datos de new y las combino uint8_t* data1n = bmp_get_data(bmp1n); uint8_t* data2n = bmp_get_data(bmp2n); uint8_t* dataNEW = bmp_get_data(bmpNEW); for(j=0;j<100;j++) { for(i=0;i<100;i++) { dataNEW[j*400+i*4+0] = data2n[j*400+i*4+0]; dataNEW[j*400+i*4+1] = data1n[j*300+i*3+0]; dataNEW[j*400+i*4+2] = data1n[j*300+i*3+1]; dataNEW[j*400+i*4+3] = data1n[j*300+i*3+2]; } } // guardo la imagen bmp_save(FILE3, bmpNEW); // borrar bmp bmp_delete(bmp1n); bmp_delete(bmp2n); bmp_delete(bmpNEW); return 0; }
int vista_fswc_grab() { /* Allocate memory for the average bitmap buffer. */ abitmap = calloc(config->width * config->height * 3, sizeof(avgbmp_t)); if(!abitmap) { ERROR("Out of memory."); return(-1); } /* Grab (and do nothing with) the skipped frames. */ for(frame = 0; frame < config->skipframes; frame++) if(src_grab(&src) == -1) break; /* Grab the requested number of frames. */ for(frame = 0; frame < config->frames; frame++) { if(src_grab(&src) == -1) break; /* Add frame to the average bitmap. */ switch(src.palette) { case SRC_PAL_PNG: fswc_add_image_png(&src, abitmap); break; case SRC_PAL_JPEG: case SRC_PAL_MJPEG: fswc_add_image_jpeg(&src, abitmap); break; case SRC_PAL_S561: fswc_add_image_s561(abitmap, src.img, src.length, src.width, src.height, src.palette); break; case SRC_PAL_RGB32: fswc_add_image_rgb32(&src, abitmap); break; case SRC_PAL_BGR32: fswc_add_image_bgr32(&src, abitmap); break; case SRC_PAL_RGB24: fswc_add_image_rgb24(&src, abitmap); break; case SRC_PAL_BGR24: fswc_add_image_bgr24(&src, abitmap); break; case SRC_PAL_BAYER: case SRC_PAL_SGBRG8: case SRC_PAL_SGRBG8: fswc_add_image_bayer(abitmap, src.img, src.length, src.width, src.height, src.palette); break; case SRC_PAL_YUYV: case SRC_PAL_UYVY: fswc_add_image_yuyv(&src, abitmap); break; case SRC_PAL_YUV420P: fswc_add_image_yuv420p(&src, abitmap); break; case SRC_PAL_NV12MB: fswc_add_image_nv12mb(&src, abitmap); break; case SRC_PAL_RGB565: fswc_add_image_rgb565(&src, abitmap); break; case SRC_PAL_RGB555: fswc_add_image_rgb555(&src, abitmap); break; case SRC_PAL_Y16: fswc_add_image_y16(&src, abitmap); break; case SRC_PAL_GREY: fswc_add_image_grey(&src, abitmap); break; } } /* Copy the average bitmap image to a gdImage. */ original = gdImageCreateTrueColor(config->width, config->height); if(!original) { ERROR("Out of memory."); free(abitmap); return(-1); } pbitmap = abitmap; for(y = 0; y < config->height; y++) for(x = 0; x < config->width; x++) { int px = x; int py = y; int colour; colour = (*(pbitmap++) / config->frames) << 16; colour += (*(pbitmap++) / config->frames) << 8; colour += (*(pbitmap++) / config->frames); gdImageSetPixel(original, px, py, colour); } free(abitmap); // scaling stuff just for Ian to learn the art of coding if((config->width != VISTA_WIDTH) || (config->height != VISTA_HEIGHT)) { gdImage *im; im = gdImageCreateTrueColor(VISTA_WIDTH, VISTA_HEIGHT); if(!im) { WARN("Out of memory."); return(-1); } gdImageCopyResampled(im, original, 0, 0, 0, 0, VISTA_WIDTH, VISTA_HEIGHT, gdImageSX(original), gdImageSY(original)); gdImageDestroy(original); original = im; } // convert the gdimage to a BMP bmp = bmp_create(VISTA_WIDTH, VISTA_HEIGHT, 24); rgb_pixel_t pixel = {128, 64, 0, 0}; int c; for(y = 0; y < VISTA_HEIGHT; y++) { for(x = 0; x < VISTA_WIDTH; x++) { c = gdImageGetPixel(original, x, y); pixel.red = gdImageRed(original, c); pixel.green = gdImageGreen(original, c); pixel.blue = gdImageBlue(original, c); bmp_set_pixel(bmp, x, y, pixel); } } gdImageDestroy(original); bmp_save(bmp, "/tmp/hope.bmp"); bmp_destroy(bmp); return 0; }