static void process(char *ims, char *imt, char* imd){ pnm pims = pnm_load(ims); pnm pimt = pnm_load(imt); int colst = pnm_get_width(pimt); int rowst = pnm_get_height(pimt); int colss = pnm_get_width(pims); int rowss = pnm_get_height(pims); pnm pimd = pnm_new(colst, rowst, PnmRawPpm); float * LABs = malloc(colss * rowss * 3 * sizeof(float)); float * LABt = malloc(colst * rowst * 3 * sizeof(float)); float * LABd = malloc(colst * rowst * 3 * sizeof(float)); // Conversion des images sources (pims) et target (pimt) en format lab (LABs et LABt) RGB2LAB(pims, LABs, colss, rowss); RGB2LAB(pimt, LABt, colst, rowst); // Colorisation de l'image de destination LABd (en format lab) colorization(LABs, LABt, LABd, colst, rowst, colss, rowss); // Conversion de l'image de destination (LABd) en format RGB (pimd) LAB2RGB(pimd, LABd, colst, rowst); free(LABs); free(LABt); free(LABd); pnm_save(pimd, PnmRawPpm, imd); pnm_free(pimt); pnm_free(pimd); pnm_free(pims); }
void process(char* ims_name,char* imd_name){ pnm ims = pnm_load(ims_name); int cols = pnm_get_width(ims); int rows = pnm_get_height(ims); pnm imd = pnm_new(cols, rows, PnmRawPpm); unsigned short *ps = pnm_get_image(ims); unsigned short *pd = pnm_get_image(imd); int i,j; unsigned short *image = pnm_get_image(ims); for(i=0;i<rows;i++){ for(j=0;j<cols;j++){ ps = image + pnm_offset(ims,i,j); for(int c=0; c<3; c++){ *pd = (*ps + *(ps+1) + *(ps+2)) / 3; pd++; } } } pnm_save(imd, PnmRawPpm, imd_name); pnm_free(imd); pnm_free(ims); }
void process(int x0, int y0, int width, int height, char* ims_name, char* imd_name){ pnm imd = pnm_new(width, height, PnmRawPpm); pnm ims = pnm_load(ims_name); unsigned short r,g,b; for(int y=0; y<height; y++){ for(int x=0; x<width; x++){ for(int z=0; z<3; z++){ r = pnm_get_component(ims, y+y0, x+x0,PnmRed); g = pnm_get_component(ims, y+y0, x+x0,PnmGreen); b = pnm_get_component(ims, y+y0, x+x0,PnmBlue); pnm_set_component(imd,y,x,PnmRed, r); pnm_set_component(imd,y,x,PnmGreen, g); pnm_set_component(imd,y,x,PnmBlue, b); } } } pnm_save(imd, PnmRawPpm, imd_name); pnm_free(imd); pnm_free(ims); }
void process(int num, char* ims_name, char* imd_name){ pnm ims = pnm_load(ims_name); int height = pnm_get_height(ims); int width = pnm_get_width(ims); pnm imd = pnm_new(width, height, PnmRawPpm); unsigned short r,g,b; for(int y=0; y<height; y++){ for(int x=0; x<width; x++){ for(int z=0; z<3; z++){ switch (num){ case 1: r = pnm_get_component(ims, y, x,PnmRed); pnm_set_component(imd,y,x,PnmRed, r); pnm_set_component(imd,y,x,PnmGreen, 0); pnm_set_component(imd,y,x,PnmBlue, 0); break; case 2: g = pnm_get_component(ims, y, x,PnmGreen); pnm_set_component(imd,y,x,PnmRed, 0); pnm_set_component(imd,y,x,PnmGreen, g); pnm_set_component(imd,y,x,PnmBlue, 0); break; case 3: b = pnm_get_component(ims, y, x,PnmBlue); pnm_set_component(imd,y,x,PnmRed, 0); pnm_set_component(imd,y,x,PnmGreen, 0); pnm_set_component(imd,y,x,PnmBlue, b); break; } } } } pnm_save(imd, PnmRawPpm, imd_name); pnm_free(imd); pnm_free(ims); }
void process(int factor, char* filter, char* ims_name, char* imd_name){ /* Selection du filtre */ float (*function_pointer) (float); if(strcmp(filter,"box") == 0){ printf("box filter\n"); function_pointer = box; } else if(strcmp(filter,"tent") == 0){ printf("tent filter\n"); function_pointer = tent; } else if(strcmp(filter,"gauss") == 0){ printf("gaussian filter\n"); function_pointer = gaussian; } else if(strcmp(filter,"bell") == 0){ printf("bell filter\n"); function_pointer = bell; } else if(strcmp(filter,"mitch") == 0){ printf("Mitchell-Netravali filter\n"); function_pointer = mitchell_netravali; } else { printf("unknown filter,\n filters avalaible: box, tent, gauss, bell, mitch"); assert(false); } pnm ims = pnm_load(ims_name); int height = pnm_get_height(ims); int width = pnm_get_width(ims); int height2 = height * factor; int width2 = width * factor; float wf = factor / 2; pnm imd = pnm_new(width2, height2, PnmRawPpm); /* image contiendra les valeur des pixels de l'image source, image2 contiendra l'image en cours de modification et image3 contiendra les images pivotées dont l'image finale*/ unsigned short * image = (unsigned short *) malloc(width * height * sizeof(unsigned short)); unsigned short * image2 = (unsigned short *) malloc(width2 * height2 * sizeof(unsigned short)); unsigned short * image3 = (unsigned short *) malloc(width2 * height2 * sizeof(unsigned short)); image = pnm_get_channel(ims, image, PnmRed); float left, right, s, l; int i_origin; /* On applique le filtre sur les lignes */ for(int i=0; i < height2; i++){ i_origin = i / factor; for(int l2=0; l2 < width2; l2++){ l = l2 / factor; left = (l - wf) >= 0 ? (l - wf) : 0; right = (l + wf) <= width ? (l + wf) : width; s = 0; for(int k=left; k <= right; k++) s += image[k + i_origin * width] * function_pointer(k-l); if(s > 255) s = 255; else if(s < 0) s = 0; image2[i_origin * width2 + l2] = s; } } /* retourne l'image pour que l'on puisse travailler sur les colonnes */ flip(width2, height2, image3, image2); /* On applique le filtre sur les colonnes */ for(int i=0; i < height2; i++){ for(int l2=0; l2 < width2; l2++){ l = l2 / factor; left = (l - wf) >= 0 ? (l - wf) : 0; right = (l + wf) <= width ? (l + wf) : width; s = 0; for(int k=left; k <= right; k++) s += image3[k + i * width2] * function_pointer(k-l); if(s > 255) s = 255; else if(s < 0) s = 0; image2[i * width2 + l2] = s; } } flip(width2, height2, image3, image2); pnm_set_channel(imd, image3, PnmRed); pnm_set_channel(imd, image3, PnmGreen); pnm_set_channel(imd, image3, PnmBlue); pnm_save(imd, PnmRawPpm, imd_name); free(image); free(image2); free(image3); pnm_free(imd); pnm_free(ims); }
void process(int factor, char* filter_name,char* ims_name, char* imd_name) { pnm ims = pnm_load(ims_name); int cols = pnm_get_width(ims); int rows = pnm_get_height(ims); int new_cols = factor*cols; int new_rows = factor*rows; pnm imw = pnm_new(new_cols, rows, PnmRawPpm); pnm imd = pnm_new(new_cols, new_rows, PnmRawPpm); // ===== Columns interpolation ===== for(int i=0; i<rows; i++) { for(int j=0; j<new_cols; j++) { float new_j = (float)j / factor; float WF = 0; if (strcmp(filter_name, "box")== 0) WF = 0.5; else if (strcmp(filter_name, "tent")== 0) WF = 1.0; else if (strcmp(filter_name, "bell")== 0) WF = 1.5; else if (strcmp(filter_name, "mitch")== 0) WF = 2.0; float left = new_j - WF; float right = new_j + WF; int l_int = floor(left); int r_int = floor(right); float S = 0.0; for (int k=l_int; k <= r_int; k++) { float h = 0.0; if (strcmp(filter_name, "box")== 0) h = box((float)k-new_j); else if (strcmp(filter_name, "tent")== 0) h = tent((float)k-new_j); else if (strcmp(filter_name, "bell")== 0) h = bell((float)k-new_j); else if (strcmp(filter_name, "mitch")== 0) h = mitch((float)k-new_j); if (k<0) { S += pnm_get_component(ims, i, 0, 0)*h; } else if (k>=cols) { S += pnm_get_component(ims, i, cols-1, 0)*h; } else { S += pnm_get_component(ims, i, k, 0)*h; } } for (int c=0; c<3; c++) { pnm_set_component(imw, i, j, c, (unsigned short) S); } } } // ===== Rows interpolation ===== for(int i=0; i<new_rows; i++) { for(int j=0; j<new_cols; j++) { float new_i = (float) i / factor; float WF = 0; if (strcmp(filter_name, "box")== 0) WF = 0.5; else if (strcmp(filter_name, "tent")== 0) WF = 1.0; else if (strcmp(filter_name, "bell")== 0) WF = 1.5; else if (strcmp(filter_name, "mitch")== 0) WF = 2.0; float above = new_i - WF; float below = new_i + WF; int a_int = floor(above); int b_int = floor(below); float S = 0.0; for (int k=a_int; k <= b_int; k++) { float h = 0.0; if (strcmp(filter_name, "box")== 0) h = box((float)k-new_i); else if (strcmp(filter_name, "tent")== 0) h = tent((float)k-new_i); else if (strcmp(filter_name, "bell")== 0) h = bell((float)k-new_i); else if (strcmp(filter_name, "mitch")== 0) h = mitch((float)k-new_i); if (k<0) { S += pnm_get_component(imw, 0, j, 0)*h; } else if (k>=rows) { S += pnm_get_component(imw, rows-1, j, 0)*h; } else { S += pnm_get_component(imw, k, j, 0)*h; } } for (int c=0; c<3; c++) { pnm_set_component(imd, i, j, c, (unsigned short) S); } } } pnm_save(imd, PnmRawPpm, imd_name); pnm_free(ims); pnm_free(imw); pnm_free(imd); }
void process(int min, int max, char* ims_name, char* imd_name){ pnm ims = pnm_load(ims_name); int height = pnm_get_height(ims); int width = pnm_get_width(ims); pnm imd = pnm_new(width, height, PnmRawPpm); unsigned short r,g,b,min_r,min_g,min_b,max_r,max_g,max_b; min_r = 255; min_g = 255; min_b = 255; max_r = 0; max_g = 0; max_b = 0; for(int y=0; y<height; y++){ for(int x=0; x<width; x++){ for(int z=0; z<3; z++){ r = pnm_get_component(ims, y, x, PnmRed); g = pnm_get_component(ims, y, x, PnmGreen); b = pnm_get_component(ims, y, x, PnmBlue); if(r < min_r){ min_r = r; } if(g < min_g){ min_g = g; } if(b < min_b){ min_b = b; } if(r > max_r){ max_r = r; } if(g > max_g){ max_g = g; } if(b > max_b){ max_b = b; } } } } unsigned short normalize_r, normalize_g, normalize_b; for(int y=0; y<height; y++){ for(int x=0; x<width; x++){ for(int z=0; z<3; z++){ normalize_r = (max - min) / (max_r - min_r) * pnm_get_component(ims, y, x, PnmRed) + (min * max_r - max * min_r) / (max_r - min_r); normalize_g = (max - min) / (max_g - min_g) * pnm_get_component(ims, y, x, PnmGreen) + (min * max_g - max * min_g) / (max_g - min_g); normalize_b = (max - min) / (max_b - min_b) * pnm_get_component(ims, y, x, PnmBlue) + (min * max_b - max * min_b) / (max_b - min_b); pnm_set_component(imd, y, x, PnmRed, normalize_r); pnm_set_component(imd, y, x, PnmGreen, normalize_g); pnm_set_component(imd, y, x,PnmBlue, normalize_b); } } } printf("****** valeurs min r g b ****** \n"); printf("%u \n", min_r); printf("%u \n", min_g); printf("%u \n", min_b); printf("****** valeurs max r g b ****** \n"); printf("%u \n", max_r); printf("%u \n", max_g); printf("%u \n", max_b); printf("****** valeurs normalize r g b ****** \n"); printf("%u \n", normalize_r); printf("%u \n", normalize_g); printf("%u \n", normalize_b); pnm_save(imd, PnmRawPpm, imd_name); pnm_free(imd); pnm_free(ims); }
void process(char* ims_name, char* imd_name, char* filter, int d0, int n, int w, int u0, int v0) { /* Selection du filtre */ /*float (*function_pointer) (double, double, double, double, int, int, int); if(strcmp(filter,"lp") == 0){ printf("low pass filter\n"); function_pointer = lp; } else if(strcmp(filter,"hp") == 0){ printf("high pass filter\n"); function_pointer = hp; } else if(strcmp(filter,"br") == 0){ printf("band reject filter\n"); function_pointer = br; } else if(strcmp(filter,"bp") == 0){ printf("band pass filter\n"); function_pointer = br; } else if(strcmp(filter,"no") == 0){ printf("rejet d'encoche \n"); function_pointer = no; } else { printf("unknown filter,\n filters avalaible: lp, hp, br, bp, no"); assert(false); }*/ pnm ims = pnm_load(ims_name); int width = pnm_get_width(ims); int height = pnm_get_height(ims); pnm imd = pnm_new(width, height, PnmRawPpm); unsigned short * image = (unsigned short *) malloc(height * width * sizeof(unsigned short)); fftw_complex * freq_repr = (fftw_complex *) fftw_malloc(height* width * sizeof(fftw_complex)); image = pnm_get_channel(ims, image, PnmRed); freq_repr = fft_forward(height, width, image); float * as = (float *) malloc(sizeof(float) * height * width); float * ps = (float *) malloc(sizeof(float) * height * width); fft_fr_to_spectra(width, height, freq_repr, as, ps); pnm imd2 = pnm_new(width, height, PnmRawPpm); for(int y = 0; y < height; y++) for(int x = 0; x < width; x++) for(int z = 0; z < 3; z++) pnm_set_component(imd2,y,x,z,as[x+y*height]); pnm_save(imd2, PnmRawPpm, "toto.ppm"); //float d_u_v; for(int j=0; j<height; j++){ for(int i=0; i<width; i++){ //d_u_v = sqrt((float)(j-height/2)*(j-height/2)+(float)(i-width/2)*(i-width/2)); //as[i+j*width] = function_pointer(i, j, u0, v0, n, w, d0) * as[i+j*width]; as[i+j*width] = low_pass(d0, n, d((float) i-width/2,(float) j-height/2)) * as[i+j*width]; //printf("%f \n", hp(i, j, u0, v0, n, w, d0)); //printf("%d \n", function_pointer(i, j, u0, v0, n, w, d0)); } } pnm imd3 = pnm_new(width, height, PnmRawPpm); for(int y = 0; y < height; y++) for(int x = 0; x < width; x++) for(int z = 0; z < 3; z++) pnm_set_component(imd3,y,x,z,as[x+y*height]); pnm_save(imd3, PnmRawPpm, "toto2.ppm"); fft_spectra_to_fr(height,width,as,ps, freq_repr); image = fft_backward(height, width, freq_repr); for(int y = 0; y < height; y++) for(int x = 0; x < width; x++) for(int z = 0; z < 3; z++) pnm_set_component(imd,y,x,z,image[x+y*height]); pnm_save(imd, PnmRawPpm, imd_name); free(image); }