diff --git a/TP7-image/Makefile b/TP7-image/Makefile index 2ca09986d9426e2e85cd2b696840765e1c06e98f..b212756febb151db9d23c753a800035e91fb3975 100644 --- a/TP7-image/Makefile +++ b/TP7-image/Makefile @@ -1,17 +1,22 @@ +LIBS=-lSDL2 -lm +CC=gcc -Wextra -Wall -g -fsanitize=address -fsanitize=leak -pedantic -lm -main: matrix.o main.o image.o - gcc matrix.o main.o image.o -o image.x -lm -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak +image.x: image.o main.o matrix.o gfx.o + $(CC) $^ -o $@ $(LIBS) image.o: image.c image.h - gcc -Wall -Wextra -c image.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak + $(CC) -c $< + +main.o: main.c + $(CC) -c $< matrix.o: matrix.c matrix.h - gcc -Wall -Wextra -c matrix.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak + $(CC) -c $< -main.o: main.c - gcc -Wall -Wextra -c main.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak +gfx.o: gfx.c gfx.h + $(CC) -lSDL2 -c $< clean: - rm -f *.o main + @rm -f *.o *.x -rebuild: clean main +rebuild: clean image.x \ No newline at end of file diff --git a/TP7-image/image.c b/TP7-image/image.c index 298bf7af2795a875d0d2c802503334bfa5b22d13..9a8bddd2cf0e70113082e0e84ad6229ded939231 100644 --- a/TP7-image/image.c +++ b/TP7-image/image.c @@ -1,9 +1,9 @@ #include "image.h" +#include "gfx.h" #include <math.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> - // typedef struct _pgm { // int max; // matrix pixels; @@ -180,8 +180,19 @@ 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) { +pgm_error pmg_conv(pgm *conv, const pgm *const orig,const matrix *const kernel) { + + 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]; + } + } + + if (divider < 1) { + divider = 1; + } matrix_alloc(&(conv->pixels), orig->pixels.m, orig->pixels.n); conv->max = orig->max; @@ -189,16 +200,13 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig, for (int i = 0; i < orig->pixels.m; i++) { // ligne for (int j = 0; j < orig->pixels.n; j++) { // col - int divider = 0; double sum = 0.0; - for (int y = 0; y < 3; y++) { // ligne - for (int x = 0; x < 3; x++) { // col + for (int y = 0; y < kernel->m; y++) { // ligne + for (int x = 0; x < kernel->n; x++) { // col - int yi = i - 1 + y; - int xj = j - 1 + x; - - divider += kernel->data[y][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) { @@ -206,10 +214,8 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig, } } } + sum /= divider; - if (divider > 1) { - sum /= divider; - } // printf("%f ",sum); if (sum < 0) { sum = 0; @@ -220,5 +226,34 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig, conv->pixels.data[i][j] = (int)sum; } } + + return success; +} + +pgm_error pgm_display(pgm p) { + + 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; + } + + 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 color = MAKE_COLOR(val, val, val); + gfx_putpixel(context, x, y, color); + } + } + + gfx_present(context); + } + + gfx_destroy(context); + return success; } diff --git a/TP7-image/image.h b/TP7-image/image.h index 5cf731a8e7f9861a6a3e4cb916e2ba69a648390a..3e06047f856defad7e7bd43519918805bb89a83b 100644 --- a/TP7-image/image.h +++ b/TP7-image/image.h @@ -36,6 +36,7 @@ pgm_error pmg_crop(pgm *crop, const pgm *const orig, int x0, int x1, int y0, int pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel); +pgm_error pgm_display(pgm p); diff --git a/TP7-image/main.c b/TP7-image/main.c index 6a31b3e29a302dea23d099326124fbc5319547fb..9c5858fe3988ff9525b3263e8199e134ecf2add8 100644 --- a/TP7-image/main.c +++ b/TP7-image/main.c @@ -1,9 +1,9 @@ +#include "gfx.h" #include "image.h" #include "matrix.h" #include <math.h> #include <stdio.h> #include <stdlib.h> - // typedef struct _matrix { // int m, n; // int **data; @@ -14,58 +14,87 @@ // matrix pixels; // } pgm; + int main() { + pgm in; + pgm tmp; + pgm out; + matrix kernel; + + pmg_read_from_file(&in, "mandrill.pgm"); + int blur5[] = {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}; + + int sharp[]={ + 0,-1,0, + -1,5,-1, + 0,-1,0}; + + matrix_init_from_array(&kernel, 5, 5, blur5); + pmg_conv(&out, &in, &kernel); + //pgm_display(out); + pmg_write_to_file(&out, "blur5.pgm"); + // struct gfx_context_t *ctxt = gfx_create("Example", 512, 512); + // if (!ctxt) { + // fprintf(stderr, "Graphics initialization failed!\n"); + // return EXIT_FAILURE; + // } + + // while (gfx_keypressed() != SDLK_ESCAPE) { + // render2d(ctxt, in.pixels); + // gfx_present(ctxt); + // } + + // gfx_destroy(ctxt); + + // pgm tmp; + // pgm out; + // matrix kernel; + // int id[]={ + // 0,0,-1, + // 1,8,1, + // -1,-1,-1}; + + // int blur[]={ + // 1,1,1, + // 1,1,1, + // 1,1,1}; + + // int edge1[]={ + // 1,0,-1, + // 0,0,0, + // -1,0,1}; + + // int edge2[]={ + // 0,-1,0, + // -1,4,-1, + // 0,-1,0}; + + // int edge3[]={ + // -1,-1,-1, + // -1,8,-1, + // -1,-1,-1}; + + // int sharp[]={ + // 0,-1,0, + // 1,5,1, + // 0,-1,0}; + // //pmg_negative(&out, &in); + + // //pmg_symmetry_cent(&out, &in); + + // //pmg_photomaton(&tmp, &in); + // //pmg_photomaton(&out, &tmp); + // matrix_init_from_array(&kernel, 3, 3, edge2); + // //matrix_print(kernel); + // pmg_conv(&out, &in,&kernel); + // //pmg_crop(&out, &in, 100, 500,100, 500); + // pmg_write_to_file(&out, "edge2.pgm"); + + matrix_destroy(&tmp.pixels); + matrix_destroy(&in.pixels); + matrix_destroy(&out.pixels); + matrix_destroy(&kernel); -pgm in; -//pgm tmp; -pgm out; -pmg_read_from_file(&in, "mandrill.pgm"); -matrix kernel; -int id[]={ -0,0,-1, -1,8,1, --1,-1,-1}; - -int blur[]={ -1,1,1, -1,1,1, -1,1,1}; - -int edge1[]={ -1,0,-1, -0,0,0, --1,0,1}; - -int edge2[]={ -0,-1,0, --1,4,-1, -0,-1,0}; - - -int edge3[]={ --1,-1,-1, -1,8,1, --1,-1,-1}; - -int sharp[]={ -0,-1,0, -1,5,1, -0,-1,0}; -//pmg_negative(&out, &in); - -//pmg_symmetry_cent(&out, &in); - -//pmg_photomaton(&tmp, &in); -//pmg_photomaton(&out, &tmp); -matrix_init_from_array(&kernel, 3, 3, edge2); -//matrix_print(kernel); -pmg_conv(&out, &in,&kernel); -//pmg_crop(&out, &in, 100, 500,100, 500); -pmg_write_to_file(&out, "edge2.pgm"); - -//matrix_destroy(&tmp.pixels); -matrix_destroy(&in.pixels); -matrix_destroy(&out.pixels); -matrix_destroy(&kernel); return EXIT_SUCCESS; } diff --git a/TP7-image/matrix.c b/TP7-image/matrix.c index ff5c316bf471f7532fa13c39e620d6e436888f3b..6cddcf51a86ead6aa907b7b91b80d4d90fbc2043 100644 --- a/TP7-image/matrix.c +++ b/TP7-image/matrix.c @@ -15,7 +15,7 @@ error_code matrix_alloc(matrix *mat, int m, int n) { if (m < 0 || n < 0) { return out_of_bounds; } - + mat->data = malloc(m * sizeof(int *)); if(mat->data == NULL){