diff --git a/pgm.c b/pgm.c index 020e5d2d5cfcb906be4cf603df923e243b74713a..3c27a34b51e0530b1375d42459bd158707273dfc 100644 --- a/pgm.c +++ b/pgm.c @@ -141,21 +141,52 @@ pgm_error pgm_crop(pgm *crop, const pgm *const orig, int32_t x0, int32_t x1, int if (x0 < 0 || y0 < 0 || x0 > x1 || y0 > y1 || x1 > orig->pixels.l || y1 > orig->pixels.l){ return failure; } - - + //Still buggy for rectangles int l = x1 - x0; int c = y1 - y0; crop->max = 255; matrix_alloc(&crop->pixels, l, c); - for (int i = x0, k = 0; i < x1; i++, k++){ - for (int j = y0, m = 0; j < y1; j++, m++){ + for (int j = y0, m = 0; j < y1; j++, m++){ crop->pixels.data[k][m] = orig->pixels.data[i][j]; } } return success; } -/* -pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel); -*/ \ No newline at end of file +pgm_error add_zeros_in_pgm(pgm *zeroedPgm, const pgm *const orig){ + if (zeroedPgm == NULL || orig == NULL){ + return failure; + } + zeroedPgm->max = orig->max; + matrix_alloc(&zeroedPgm->pixels, (orig->pixels.l + 2), (orig->pixels.c + 2)); + + for(int i = 0; i < zeroedPgm->pixels.l; i++){ + for (int j = 0; j < zeroedPgm->pixels.c; j++){ + zeroedPgm->pixels.data[i][j] = 0; + } + } + + for(int i = 1, k = 0; i < zeroedPgm->pixels.l - 1; i++, k++){ + for(int j = 1, m = 0; i < zeroedPgm->pixels.c - 1; j++, m++){ + zeroedPgm->pixels.data[i][j] = orig->pixels.data[k][m]; + } + } + + return success; +} + +pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel){ + if (conv == NULL || orig == NULL || kernel){ + return failure; + } + + conv->max = orig->max; + matrix_alloc(&conv->pixels, orig->pixels.l, orig->pixels.c); + for (int i = 1; i <= orig->pixels.l; i++){ + for (int j = 1; j <= orig->pixels.c; j++){ + //attention au valeurs < 0 ou > 255 + } + } + return success; +} diff --git a/pgm.h b/pgm.h index 5ae333a017f9980b0df965d2f34d28186209f65d..bd4561353427097747e672ae17fa9635d4452fd9 100644 --- a/pgm.h +++ b/pgm.h @@ -23,6 +23,7 @@ pgm_error pgm_symmetry_vert(pgm *sym, const pgm *const orig); pgm_error pgm_symmetry_cent(pgm *sym, const pgm *const orig); pgm_error pgm_photomaton(pgm *photomaton, const pgm *const orig); pgm_error pgm_crop(pgm *crop, const pgm *const orig, int32_t x0, int32_t x1, int32_t y0, int32_t y1); +pgm_error add_zeros_in_pgm(pgm *zeroedPgm, const pgm *const orig); pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel); #endif \ No newline at end of file diff --git a/pgm_main.c b/pgm_main.c index 2a4e000124a07ee0558e0b3f9fc09fb8d4d5025e..83f9d379bc2ae2b99f43fa023a1fc4e617cec04a 100644 --- a/pgm_main.c +++ b/pgm_main.c @@ -1,6 +1,5 @@ #include "pgm.h" - int main(){ pgm a; pgm neg; @@ -10,6 +9,12 @@ int main(){ pgm photomatonx4; pgm photomatonx16; pgm cropped; + pgm zeroPgm; + //pgm convolution; + int conv[9] = {1,1,1,1,0,1,1,1,1}; + matrix convKernel; + matrix_init_from_array(&convKernel, 3, 3, conv, 9); + printf("%d", pgm_read_from_file(&a, "mandrill.pgm")); @@ -30,8 +35,14 @@ int main(){ printf("%d", pgm_photomaton(&photomatonx16, &photomatonx4)); //printf("%d", pgm_write_to_file(&photomatonx16, "photomatonx16.pgm")); - printf("%d", pgm_crop(&cropped, &a, 100, 300, 100, 200)); - printf("%d", pgm_write_to_file(&cropped, "cropped.pgm")); + printf("%d", pgm_crop(&cropped, &a, 100, 300, 100, 300)); + //printf("%d", pgm_write_to_file(&cropped, "cropped.pgm")); + + printf("%d", add_zeros_in_pgm(&zeroPgm, &a)); + printf("%d", pgm_write_to_file(&zeroPgm, "zero.pgm")); + + //printf("%d", pmg_conv(&convolution, &a, &convKernel)); + //printf("%d", pgm_write_to_file(&convolution, "convolution.pgm")); matrix_destroy(&a.pixels); matrix_destroy(&neg.pixels); @@ -41,6 +52,7 @@ int main(){ matrix_destroy(&photomatonx4.pixels); matrix_destroy(&photomatonx16.pixels); matrix_destroy(&cropped.pixels); + matrix_destroy(&convKernel); return EXIT_SUCCESS; } \ No newline at end of file