diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/common.h | 8 | ||||
-rw-r--r-- | src/main.c | 38 | ||||
-rw-r--r-- | src/ppm.c | 5 | ||||
-rw-r--r-- | src/ppm.h | 5 |
5 files changed, 44 insertions, 16 deletions
@@ -23,6 +23,10 @@ build: $(OBJS) run: build ./${BIN} +test: build + ./${BIN} + imv images/house_1.ppm out1.ppm out2.ppm + debug: $(BIN) lldb $< --tui 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 <stdint.h> +typedef struct Image { + uint8_t *data; + int32_t width, height, pixel_bits; +} Image; +#endif @@ -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; } @@ -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"); @@ -5,10 +5,7 @@ #include <stdlib.h> #include <string.h> -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); |