diff --git a/TP7-image/chien-pasteque.pgm b/TP7-image/chien-pasteque.pgm new file mode 100644 index 0000000000000000000000000000000000000000..bd87794887dbb0b9952467884a4bdb925bf873d7 Binary files /dev/null and b/TP7-image/chien-pasteque.pgm differ diff --git a/TP7-image/image.c b/TP7-image/image.c index 35628418599bb254077602f593dd116b6520fcfd..8cefed8969e298a9c72b614ec17625ae2a050f1c 100644 --- a/TP7-image/image.c +++ b/TP7-image/image.c @@ -10,11 +10,10 @@ // matrix pixels; // } pgm; - pgm_error pmg_read_from_file(pgm *p, char *filename) { -if(NULL== filename || NULL==p){ - return failure; - } + if (NULL == filename || NULL == p) { + return failure; + } FILE *f = fopen(filename, "r"); @@ -49,9 +48,9 @@ if(NULL== filename || NULL==p){ } pgm_error pmg_write_to_file(pgm *p, char *filename) { - if(NULL== filename || NULL==p){ - return failure; - } + if (NULL == filename || NULL == p) { + return failure; + } FILE *f = fopen(filename, "w"); @@ -73,9 +72,9 @@ pgm_error pmg_write_to_file(pgm *p, char *filename) { } pgm_error pmg_negative(pgm *neg, const pgm *const orig) { -if(NULL== orig || NULL==neg){ - return failure; - } + if (NULL == orig || NULL == neg) { + return failure; + } matrix_alloc(&(neg->pixels), orig->pixels.m, orig->pixels.n); neg->max = orig->max; @@ -90,9 +89,9 @@ if(NULL== orig || NULL==neg){ } pgm_error pmg_symmetry_hori(pgm *sym, const pgm *const orig) { -if(NULL== orig || NULL==sym){ - return failure; - } + if (NULL == orig || NULL == sym) { + return failure; + } matrix_alloc(&(sym->pixels), orig->pixels.m, orig->pixels.n); sym->max = orig->max; @@ -108,9 +107,9 @@ if(NULL== orig || NULL==sym){ } pgm_error pmg_symmetry_vert(pgm *sym, const pgm *const orig) { -if(NULL== orig || NULL==sym){ - return failure; - } + if (NULL == orig || NULL == sym) { + return failure; + } matrix_alloc(&(sym->pixels), orig->pixels.m, orig->pixels.n); sym->max = orig->max; @@ -126,9 +125,9 @@ if(NULL== orig || NULL==sym){ } pgm_error pmg_symmetry_cent(pgm *sym, const pgm *const orig) { -if(NULL== orig || NULL==sym){ - return failure; - } + if (NULL == orig || NULL == sym) { + return failure; + } matrix_alloc(&(sym->pixels), orig->pixels.m, orig->pixels.n); sym->max = orig->max; @@ -145,9 +144,9 @@ if(NULL== orig || NULL==sym){ } pgm_error pmg_photomaton(pgm *photomaton, const pgm *const orig) { -if(NULL== orig || NULL==photomaton){ - return failure; - } + if (NULL == orig || NULL == photomaton) { + return failure; + } matrix_alloc(&(photomaton->pixels), orig->pixels.m, orig->pixels.n); photomaton->max = orig->max; @@ -196,7 +195,8 @@ if(NULL== orig || NULL==photomaton){ pgm_error pmg_crop(pgm *crop, const pgm *const orig, int x0, int x1, int y0, int y1) { - if(ok!=matrix_extract_submatrix(&crop->pixels, orig->pixels, y0, y1, x0, x1)){ + if (ok != + matrix_extract_submatrix(&crop->pixels, orig->pixels, y0, y1, x0, x1)) { return failure; } @@ -204,62 +204,72 @@ pgm_error pmg_crop(pgm *crop, const pgm *const orig, int x0, int x1, int y0, return success; } -pgm_error pmg_conv(pgm *conv, const pgm *const orig,const matrix *const kernel) { -if(NULL== orig || NULL==conv || NULL==kernel){ - return failure; - } - +int get_divider(const matrix *const mat) { int divider = 0; - // get divider - for (int y = 0; y < kernel->m; y++) { // ligne - for (int x = 0; x < kernel->n; x++) { - divider += kernel->data[y][x]; + for (int y = 0; y < mat->m; y++) { + for (int x = 0; x < mat->n; x++) { + divider += mat->data[y][x]; } } - if (divider < 1) { divider = 1; } + return divider; +} +pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel) { + if (NULL == orig || NULL == conv || NULL == kernel) { + return failure; + } matrix_alloc(&(conv->pixels), orig->pixels.m, orig->pixels.n); conv->max = orig->max; - for (int i = 0; i < orig->pixels.m; i++) { // ligne - for (int j = 0; j < orig->pixels.n; j++) { // col - double sum = 0.0; + int divider = get_divider(kernel); + + for (int i = 0; i < orig->pixels.m; i++) { + for (int j = 0; j < orig->pixels.n; j++) { + double pixel = 0.0; - for (int y = 0; y < kernel->m; y++) { // ligne - for (int x = 0; x < kernel->n; x++) { // col + for (int y = 0; y < kernel->m; y++) { + for (int x = 0; x < kernel->n; x++) { int yi = i - kernel->m / 2 + y; int xj = j - kernel->n / 2 + x; - - if (xj >= 0 && yi >= 0 && xj < orig->pixels.m && - yi < orig->pixels.n) { - sum += orig->pixels.data[yi][xj] * kernel->data[y][x]; + //if pixel is inside matrix + if (xj >= 0 && yi >= 0 && xj < orig->pixels.m && yi < orig->pixels.n) { + pixel += orig->pixels.data[yi][xj] * kernel->data[y][x]; } } } - sum /= divider; - - // printf("%f ",sum); - if (sum < 0) { - sum = 0; - } else if (sum > conv->max) { - sum = conv->max; + pixel /= divider; + //check pixel's val -> + if (pixel < 0) { + pixel = 0; + } else if (pixel > conv->max) { + pixel = conv->max; } - // printf("%d\n",(int)sum); - conv->pixels.data[i][j] = (int)sum; + //set pixel + conv->pixels.data[i][j] = (int)pixel; } } + return success; } + + + + + + + + pgm_error pgm_display(pgm p) { - struct gfx_context_t *context = gfx_create("SDL Display", p.pixels.m, p.pixels.n); + struct gfx_context_t *context = + gfx_create("SDL Display", p.pixels.m, p.pixels.n); if (!context) { fprintf(stderr, "Graphics initialization failed!\n"); return EXIT_FAILURE; @@ -268,10 +278,10 @@ pgm_error pgm_display(pgm p) { while (gfx_keypressed() != SDLK_ESCAPE) { gfx_clear(context, COLOR_BLACK); - + for (int y = 0; y < p.pixels.m; y++) { // ligne for (int x = 0; x < p.pixels.n; x++) { // col - uint val=p.pixels.data[y][x]; + uint val = p.pixels.data[y][x]; uint color = MAKE_COLOR(val, val, val); gfx_putpixel(context, x, y, color); } diff --git a/TP7-image/main.c b/TP7-image/main.c index 41e12e486f56e7fc39256a00f1cf902693e82871..d22e7496fe9213c431235eea20fcd78e46f00790 100644 --- a/TP7-image/main.c +++ b/TP7-image/main.c @@ -8,12 +8,33 @@ int main() { pgm in; - //pgm tmp; + // pgm tmp; pgm out; matrix kernel; - pmg_read_from_file(&in, "mandrill.pgm"); - //https://en.wikipedia.org/wiki/Kernel_(image_processing) + pmg_read_from_file(&in, "chien-pasteque.pgm"); + // https://en.wikipedia.org/wiki/Kernel_(image_processing) + + pmg_negative(&out, &in); + pmg_write_to_file(&out, "neg.pgm"); + matrix_destroy(&out.pixels); + pmg_symmetry_hori(&out, &in); + pmg_write_to_file(&out, "symh.pgm"); + matrix_destroy(&out.pixels); + pmg_symmetry_vert(&out, &in); + pmg_write_to_file(&out, "symv.pgm"); + matrix_destroy(&out.pixels); + pmg_symmetry_cent(&out, &in); + pmg_write_to_file(&out, "symc.pgm"); + matrix_destroy(&out.pixels); + + pmg_photomaton(&out, &in); + pmg_write_to_file(&out, "photomat.pgm"); + matrix_destroy(&out.pixels); + pmg_crop(&out, &in, 50, 200, 50, 200); + pmg_write_to_file(&out, "crop.pgm"); + matrix_destroy(&out.pixels); + int sharp[] = {0, -1, 0, -1, 5, -1, 0, -1, 0}; int blur[] = {1, 1, 1, 1, 1, 1, 1, 1, 1}; int edge1[] = {1, 0, -1, 0, 0, 0, -1, 0, 1}; @@ -21,20 +42,43 @@ int main() { int edge3[] = {-1, -1, -1, -1, 8, -1, -1, -1, -1}; int blur5x5[] = {1, 4, 6, 4, 1, 4, 16, 24, 16, 4, 6, 24, 36, 24, 6, 4, 16, 24, 16, 4, 1, 4, 6, 4, 1}; - matrix_init_from_array(&kernel, 3, 3, sharp); pmg_conv(&out, &in, &kernel); - // pgm_display(out); - - //pmg_negative(&out, &in); - pmg_write_to_file(&out, "sharp.pgm"); + matrix_destroy(&out.pixels); + matrix_destroy(&kernel); + + matrix_init_from_array(&kernel, 3, 3, blur); + pmg_conv(&out, &in, &kernel); + pmg_write_to_file(&out, "blur.pgm"); + matrix_destroy(&out.pixels); + matrix_destroy(&kernel); + matrix_init_from_array(&kernel, 3, 3, edge1); + pmg_conv(&out, &in, &kernel); + pmg_write_to_file(&out, "edge1.pgm"); + matrix_destroy(&out.pixels); + matrix_destroy(&kernel); - //matrix_destroy(&tmp.pixels); - matrix_destroy(&in.pixels); + matrix_init_from_array(&kernel, 3, 3, edge2); + pmg_conv(&out, &in, &kernel); + pmg_write_to_file(&out, "edge2.pgm"); + matrix_destroy(&out.pixels); + matrix_destroy(&kernel); + + matrix_init_from_array(&kernel, 3, 3, edge3); + pmg_conv(&out, &in, &kernel); + pmg_write_to_file(&out, "edge3.pgm"); + matrix_destroy(&out.pixels); + matrix_destroy(&kernel); + + matrix_init_from_array(&kernel, 5, 5, blur5x5); + pmg_conv(&out, &in, &kernel); + pmg_write_to_file(&out, "blur5x5.pgm"); matrix_destroy(&out.pixels); matrix_destroy(&kernel); + matrix_destroy(&in.pixels); + return EXIT_SUCCESS; }