Skip to content
Snippets Groups Projects
Commit 22e5a6ae authored by thib's avatar thib
Browse files

pgm display, mat 5x5

parent 2f705df0
Branches
No related tags found
No related merge requests found
LIBS=-lSDL2 -lm
CC=gcc -Wextra -Wall -g -fsanitize=address -fsanitize=leak -pedantic -lm
main: matrix.o main.o image.o image.x: image.o main.o matrix.o gfx.o
gcc matrix.o main.o image.o -o image.x -lm -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak $(CC) $^ -o $@ $(LIBS)
image.o: image.c image.h 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 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 gfx.o: gfx.c gfx.h
gcc -Wall -Wextra -c main.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak $(CC) -lSDL2 -c $<
clean: clean:
rm -f *.o main @rm -f *.o *.x
rebuild: clean main rebuild: clean image.x
\ No newline at end of file
#include "image.h" #include "image.h"
#include "gfx.h"
#include <math.h> #include <math.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// typedef struct _pgm { // typedef struct _pgm {
// int max; // int max;
// matrix pixels; // matrix pixels;
...@@ -180,8 +180,19 @@ pgm_error pmg_crop(pgm *crop, const pgm *const orig, int x0, int x1, int y0, ...@@ -180,8 +180,19 @@ pgm_error pmg_crop(pgm *crop, const pgm *const orig, int x0, int x1, int y0,
return success; return success;
} }
pgm_error pmg_conv(pgm *conv, const pgm *const orig, pgm_error pmg_conv(pgm *conv, const pgm *const orig,const matrix *const kernel) {
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); matrix_alloc(&(conv->pixels), orig->pixels.m, orig->pixels.n);
conv->max = orig->max; conv->max = orig->max;
...@@ -189,16 +200,13 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig, ...@@ -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 i = 0; i < orig->pixels.m; i++) { // ligne
for (int j = 0; j < orig->pixels.n; j++) { // col for (int j = 0; j < orig->pixels.n; j++) { // col
int divider = 0;
double sum = 0.0; double sum = 0.0;
for (int y = 0; y < 3; y++) { // ligne for (int y = 0; y < kernel->m; y++) { // ligne
for (int x = 0; x < 3; x++) { // col 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 && if (xj >= 0 && yi >= 0 && xj < orig->pixels.m &&
yi < orig->pixels.n) { yi < orig->pixels.n) {
...@@ -206,10 +214,8 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig, ...@@ -206,10 +214,8 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig,
} }
} }
} }
if (divider > 1) {
sum /= divider; sum /= divider;
}
// printf("%f ",sum); // printf("%f ",sum);
if (sum < 0) { if (sum < 0) {
sum = 0; sum = 0;
...@@ -220,5 +226,34 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig, ...@@ -220,5 +226,34 @@ pgm_error pmg_conv(pgm *conv, const pgm *const orig,
conv->pixels.data[i][j] = (int)sum; 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; return success;
} }
...@@ -36,6 +36,7 @@ pgm_error pmg_crop(pgm *crop, const pgm *const orig, int x0, int x1, int y0, int ...@@ -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 pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel);
pgm_error pgm_display(pgm p);
......
#include "gfx.h"
#include "image.h" #include "image.h"
#include "matrix.h" #include "matrix.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// typedef struct _matrix { // typedef struct _matrix {
// int m, n; // int m, n;
// int **data; // int **data;
...@@ -14,58 +14,87 @@ ...@@ -14,58 +14,87 @@
// matrix pixels; // matrix pixels;
// } pgm; // } pgm;
int main() {
int main() {
pgm in; pgm in;
//pgm tmp; pgm tmp;
pgm out; pgm out;
pmg_read_from_file(&in, "mandrill.pgm");
matrix kernel; 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[]={ pmg_read_from_file(&in, "mandrill.pgm");
-1,-1,-1, int blur5[] = {1, 4, 6, 4, 1, 4, 16, 24, 16, 4, 6, 24, 36,
1,8,1, 24, 6, 4, 16, 24, 16, 4, 1, 4, 6, 4, 1};
-1,-1,-1};
int sharp[]={ int sharp[]={
0,-1,0, 0,-1,0,
1,5,1, -1,5,-1,
0,-1,0}; 0,-1,0};
//pmg_negative(&out, &in);
//pmg_symmetry_cent(&out, &in); matrix_init_from_array(&kernel, 5, 5, blur5);
//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_conv(&out, &in, &kernel);
//pmg_crop(&out, &in, 100, 500,100, 500); //pgm_display(out);
pmg_write_to_file(&out, "edge2.pgm"); 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);
// }
//matrix_destroy(&tmp.pixels); // 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(&in.pixels);
matrix_destroy(&out.pixels); matrix_destroy(&out.pixels);
matrix_destroy(&kernel); matrix_destroy(&kernel);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment