From d579b5e8bf669918908c894304efd66ee79f179e Mon Sep 17 00:00:00 2001 From: jvech Date: Mon, 19 Jun 2023 13:11:29 -0500 Subject: fix: convolution filter fixed edge filters work --- src/common.h | 8 ++++++++ src/main.c | 38 +++++++++++++++++++++++++++----------- src/ppm.c | 5 ++++- src/ppm.h | 5 +---- 4 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 src/common.h (limited to 'src') diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..8c35b6a --- /dev/null +++ b/src/common.h @@ -0,0 +1,8 @@ +#ifndef __COMMON__ +#define __COMMON__ +#include +typedef struct Image { + uint8_t *data; + int32_t width, height, pixel_bits; +} Image; +#endif diff --git a/src/main.c b/src/main.c index 0182328..ec29a6b 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,7 @@ image_convolution(Image input, float kernel[9]) } uint8_t *r, *g, *b; + uint8_t red, green, blue; r = calloc(input.width * input.height, sizeof(uint8_t)); g = calloc(input.width * input.height, sizeof(uint8_t)); b = calloc(input.width * input.height, sizeof(uint8_t)); @@ -36,13 +37,16 @@ image_convolution(Image input, float kernel[9]) for (i = 0; i < input.height; i++) { for (j = 0; j < input.width; j++) { index = i * input.width + j; - r[index] = kernel_op(r, kernel, i, j, input.width, input.height); - g[index] = kernel_op(g, kernel, i, j, input.width, input.height); - b[index] = kernel_op(b, kernel, i, j, input.width, input.height); + red = kernel_op(r, kernel, i, j, input.width, input.height); + green = kernel_op(g, kernel, i, j, input.width, input.height); + blue = kernel_op(b, kernel, i, j, input.width, input.height); + out.data[index * 3] = red; + out.data[index * 3 + 1] = green; + out.data[index * 3 + 2] = blue; } } - merge_rgb(&out, r, g, b); + //merge_rgb(&out, r, g, b); free(r); free(g); free(b); @@ -81,10 +85,10 @@ kernel_op(uint8_t *data, float kernel[9], int i, int j, int w, int h) kernel_value += data[img_index] * kernel[index_k]; } - kernel_value = (kernel_value > 255) ? 255: kernel_value; - kernel_value = (kernel_value < 0) ? 0: kernel_value; } } + kernel_value = (kernel_value > 255) ? 255: kernel_value; + kernel_value = (kernel_value < 0) ? 0: kernel_value; return (uint8_t)kernel_value; } @@ -109,15 +113,27 @@ split_rgb(Image input, uint8_t *r, uint8_t *g, uint8_t *b) int main(int argc, char *argv[]) { Image img; - float kernel[9] = { + float edge[9] = { -1, -1, -1, -1, 8, -1, -1, -1, -1, }; - //for (int i = 0; i < 9; i++) kernel[i] /= 16; - ppm_read("images/synth_1.ppm", &img); - Image out_img = image_convolution(img, kernel); - ppm_write("out.ppm", &out_img); + float eye[9] = { + 0, 0, 0, + 0, 1, 0, + 0, 0, 0, + }; + float gauss[9] = { + 1, 2, 1, + 2, 4, 2, + 1, 2, 1, + }; + for (int i = 0; i < 9; i++) gauss[i] /= 16; + ppm_read("images/house_1.ppm", &img); + Image out_img = image_convolution(img, edge); + ppm_write("out1.ppm", &out_img); + out_img = image_convolution(out_img, gauss); + ppm_write("out2.ppm", &out_img); return 0; } diff --git a/src/ppm.c b/src/ppm.c index aded7cd..a5cf3fe 100644 --- a/src/ppm.c +++ b/src/ppm.c @@ -6,6 +6,9 @@ static void read_p6_data(FILE *fp, Image *img); void ppm_write(const char *file, Image *img) { + /* + * Write a ppm file + */ FILE *fp = fopen(file, "w"); if (fp == NULL) { @@ -23,7 +26,7 @@ ppm_write(const char *file, Image *img) void ppm_read(const char *file, Image *img) /* - * Read a ppm image + * Read a ppm file */ { FILE *fp = fopen(file, "r"); diff --git a/src/ppm.h b/src/ppm.h index 5cf8b02..fa82d7a 100644 --- a/src/ppm.h +++ b/src/ppm.h @@ -5,10 +5,7 @@ #include #include -typedef struct Image { - uint8_t *data; - int32_t width, height, pixel_bits; -} Image; +#include "common.h" void ppm_read(const char *file, Image *img); void ppm_write(const char *file, Image *img); -- cgit v1.2.3-70-g09d2