diff --git a/pgm.c b/pgm.c index af56fe26a99ecca222dd4c36265df60340001fcb..82777cfa32e8701e843c8c87fcbc2d5a0d7be08e 100644 --- a/pgm.c +++ b/pgm.c @@ -1,5 +1,12 @@ #include "pgm.h" + +/** + * pgm_read_from_file reads a pgm file and fills the pgm struct "p" with its data + * @param p is a pointer of pgm struct + * @param filename is a pointer of characters representing the path of the targeted file + * @return pgm_error +**/ pgm_error pgm_read_from_file(pgm *p, char *filename){ if (filename == NULL || p == NULL){ return failure; @@ -23,6 +30,13 @@ pgm_error pgm_read_from_file(pgm *p, char *filename){ return success; } + +/** + * pgm_write_to_file creates a pgm file from a pgm struct with a custom name + * @param p is a pointer of pgm struct + * @param filename is a pointer of characters that defines the custom file name + * @return pgm_error +**/ pgm_error pgm_write_to_file(pgm *p, char *filename){ if (filename == NULL || p == NULL){ return failure; @@ -45,6 +59,13 @@ pgm_error pgm_write_to_file(pgm *p, char *filename){ return success; } + +/** + * pgm_negative modifies an empty pgm struct with a negative version of the original one + * @param neg is a pointer of pgm struct which contains the negative version + * @param orig constant pointer of pgm which is the original pgm + * @return pgm_error +**/ pgm_error pgm_negative(pgm *neg, const pgm *const orig){ if (neg == NULL || orig == NULL){ return failure; @@ -61,6 +82,13 @@ pgm_error pgm_negative(pgm *neg, const pgm *const orig){ return success; } + +/** + * pgm_symmetry_hori modifies an empty pgm struct with a horizontal symmetric version of the original one + * @param sym is a pointer of pgm struct which contains the modified version + * @param orig constant pointer of pgm which is the original pgm + * @return pgm_error +**/ pgm_error pgm_symmetry_hori(pgm *sym, const pgm *const orig){ if (sym == NULL || orig == NULL){ return failure; @@ -77,6 +105,13 @@ pgm_error pgm_symmetry_hori(pgm *sym, const pgm *const orig){ return success; } + +/** + * pgm_symmetry_vert modifies an empty pgm struct with a vertical symmetric version of the original one + * @param sym is a pointer of pgm struct which contains the modified version + * @param orig constant pointer of pgm which is the original pgm + * @return pgm_error +**/ pgm_error pgm_symmetry_vert(pgm *sym, const pgm *const orig){ if (sym == NULL || orig == NULL){ return failure; @@ -93,6 +128,13 @@ pgm_error pgm_symmetry_vert(pgm *sym, const pgm *const orig){ return success; } + +/** + * pgm_symmetry_cent modifies an empty pgm struct with a central symmetric version of the original one + * @param sym is a pointer of pgm struct which contains the modified version + * @param orig constant pointer of pgm which is the original pgm + * @return pgm_error +**/ pgm_error pgm_symmetry_cent(pgm *sym, const pgm *const orig){ if (sym == NULL || orig == NULL){ return failure; @@ -109,6 +151,13 @@ pgm_error pgm_symmetry_cent(pgm *sym, const pgm *const orig){ return success; } + +/** + * pgm_photomaton modifies an empty pgm struct with a x4 photomaton version of the original one + * @param photomaton is a pointer of pgm struct which contains the modified version + * @param orig constant pointer of pgm which is the original pgm + * @return pgm_error +**/ pgm_error pgm_photomaton(pgm *photomaton, const pgm *const orig){ if (photomaton == NULL || orig == NULL){ return failure; @@ -134,6 +183,19 @@ pgm_error pgm_photomaton(pgm *photomaton, const pgm *const orig){ return success; } + +/** + * pgm_crop modifies an empty pgm struct with a cropped version of the original one depending on bounds + * @param crop is a pointer of pgm struct which contains the cropped version + * @param orig constant pointer of pgm which is the original pgm + * @param x0 is the lower x axis bound + * @param x1 is the upper x axis bound + * @param y0 is the lower y axis bound + * @param y1 is the upper y axis bound + * @return pgm_error + * + * WARNING: this function doesn't work properly, only squared bounds make it work +**/ pgm_error pgm_crop(pgm *crop, const pgm *const orig, int32_t x0, int32_t x1, int32_t y0, int32_t y1){ if (crop == NULL || orig == NULL){ return failure; @@ -147,6 +209,14 @@ pgm_error pgm_crop(pgm *crop, const pgm *const orig, int32_t x0, int32_t x1, int return success; } + +/** + * add_zeros_in_pgm modifies an empty pgm struct with a "zero bordered" version of the original one + * Used to avoid edge problems when calculating convolutions + * @param zeroedPgm is a pointer of pgm struct which contains the "zero bordered" version + * @param orig constant pointer of pgm which is the original pgm + * @return pgm_error +**/ pgm_error add_zeros_in_pgm(pgm *zeroedPgm, const pgm *const orig){ if (zeroedPgm == NULL || orig == NULL){ return failure; @@ -169,6 +239,15 @@ pgm_error add_zeros_in_pgm(pgm *zeroedPgm, const pgm *const orig){ return success; } + +/** + * calculate_pixel_conv calculates the convoluted version of a pixel using a sub matrix from + * an image and a kernel + * @param m is a matrix containing pixels from an image + * @param kernel constant pointer of matrix containing values to calcuate a convolution + * @param max represents the maximum value of a pixel + * @return result +**/ int calculate_pixel_conv(const matrix m, const matrix *const kernel, int max){ int result = 0; @@ -184,6 +263,15 @@ int calculate_pixel_conv(const matrix m, const matrix *const kernel, int max){ return result; } + +/** + * pmg_conv modifies an empty pgm struct with a "convoluted" version of the original one depending on a kernel + * @param conv is a pointer of pgm which contains the "convoluted" version + * @param orig constant pointer of pgm which is the original pgm + * @param kernel constant pointer of matrix containing values to calcuate a convolution + * @param norm double number added to modify convolution "behaviour" + * @return pgm_error +**/ pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel, double norm){ if (conv == NULL || orig == NULL || kernel == NULL){ return failure; @@ -203,8 +291,14 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel, return success; } + +/** + * demo uses SDL2 library to display an image on a window + * @param img is a constant pointer of the pgm displayed on screen + * @return pgm_error +**/ pgm_error demo(const pgm *const img){ - int width = img->pixels.l, height = img->pixels.c; + int width = img->pixels.c, height = img->pixels.l; struct gfx_context_t *ctxt = gfx_create("Example", width, height); while (gfx_keypressed() != 27/*ESC btn code*/) { diff --git a/pgm_main.c b/pgm_main.c index cfbd6ecaf0400d2a0c8874caf6e5b4ef53c8b99a..7ed8250fa947a84568d8671383f56d0f6d397fc0 100644 --- a/pgm_main.c +++ b/pgm_main.c @@ -1,6 +1,7 @@ #include "pgm.h" int main(){ + //variables initialization pgm a; pgm neg; pgm sym_hori; @@ -12,50 +13,52 @@ int main(){ pgm pgmWithZeros; pgm convolution; - - int conv[9] = {1,1,1,1,1,1,1,1,1}; matrix convKernel; matrix_init_from_array(&convKernel, 3, 3, conv, 9); + + //Read pgm printf("%d", pgm_read_from_file(&a, "mandrill.pgm")); - //printf("%d", pgm_write_to_file(&a, "newf.pgm")); + //Negative printf("%d", pgm_negative(&neg, &a)); - //printf("%d", pgm_write_to_file(&neg, "neg.pgm")); + //horizontal symmetry printf("%d", pgm_symmetry_hori(&sym_hori, &a)); - //printf("%d", pgm_write_to_file(&sym_hori, "sym_hori.pgm")); + //vertical symmetry printf("%d", pgm_symmetry_vert(&sym_vert, &a)); - //printf("%d", pgm_write_to_file(&sym_vert, "sym_vert.pgm")); + //central symmetry printf("%d", pgm_symmetry_cent(&sym_cent, &a)); - //printf("%d", pgm_write_to_file(&sym_cent, "sym_cent.pgm")); + //Photomaton x4 printf("%d", pgm_photomaton(&photomatonx4, &a)); - //printf("%d", pgm_write_to_file(&photomatonx4, "photomatonx4.pgm")); + + //Photomaton x16 printf("%d", pgm_photomaton(&photomatonx16, &photomatonx4)); - //printf("%d", pgm_write_to_file(&photomatonx16, "photomatonx16.pgm")); + //Crop printf("%d", pgm_crop(&cropped, &a, 100, 200, 200, 500)); - //printf("%d", pgm_write_to_file(&cropped, "cropped.pgm")); + //Convolution printf("%d", add_zeros_in_pgm(&pgmWithZeros, &a)); printf("%d", pmg_conv(&convolution, &pgmWithZeros, &convKernel, 1)); - //printf("%d", pgm_write_to_file(&convolution, "convolution.pgm")); - //demo(&neg); - //demo(&sym_hori); - //demo(&sym_vert); - //demo(&sym_cent); - //demo(&photomatonx4); - //demo(&photomatonx16); - //demo(&cropped); - //demo(&convolution); + demo(&neg); + demo(&sym_hori); + demo(&sym_vert); + demo(&sym_cent); + demo(&photomatonx4); + demo(&photomatonx16); + demo(&cropped); + demo(&convolution); + + //Destroying pgm's matrixes matrix_destroy(&a.pixels); matrix_destroy(&neg.pixels); matrix_destroy(&sym_hori.pixels);