From 7745967382d6812244803404241e3c2b721c8cc9 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Thu, 3 Sep 2026 14:36:46 -0400 Subject: [PATCH] DOCS: Added basic explaination to example code. README: Added readme with copy of example code and first push of readme. --- README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 66 +++++++++++++++++++++++------------------------------- 2 files changed, 91 insertions(+), 38 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..d2a46ae --- /dev/null +++ b/README.md @@ -0,0 +1,63 @@ + +```c +/* + * Keep in mind any function that creates a new buffer and returns it can + * overwrite the old one, however it is lost. SO create a new variable and free + * the old one after use. + */ +int +main() +{ + // Init base image containers + PPM img; + JPEG jimg; + + // Add a file location of image to modify + img.name = "../assets/test_2.ppm"; + + // Load the PPM image + unsigned int* buf = ppm_load(img.name, &img); + + // Check for null image load. In case image doesnt exist or fails to + // load + if (!buf) + { + LOGERROR(ERR_BUF_NULL); + return ERR_BUF_NULL; + } + // Blur image and save to new pointer + unsigned int* blur = box_blur(buf, &img, 20); + + // Free old buffer. MUST DO THIS. Maybe not immediately but before + // ending or possibly leaking. Also it stays in heap and a large image + // can take a lot of memory so better to clear anyways. + free(buf); + // Check for null image load. In case image doesnt exist or fails to + // load + if (!blur) + { + LOGERROR(ERR_BUF_NULL); + return ERR_BUF_NULL; + } + + // Loop image and apply filter to each pixel. + for (int i = 0; i < img.len; i++) + { + blur[i] = u32_rotl(blur[i], 4); + } + + // Take the attributes from ppm image so jpeg file can be saved + jimg = inherit_from_ppm(&img); + + // Saves jpeg to location specified + jpeg_save(blur, &jimg, "../assets/test_2_new.jpg"); + + // Make sure to free any buffer still active + free(blur); + + return 0; +} + + + +``` diff --git a/src/main.c b/src/main.c index 232d25a..fe741fa 100644 --- a/src/main.c +++ b/src/main.c @@ -16,62 +16,52 @@ int main() { - // JPEG out_1 = { - // .name = "../MF8jno.jpg", - // - // }; - // JPEG img_j = { - // .name = "../assets/test_2.jpg", - // }; - // + // Init base image containers + PPM img; + JPEG jimg; - // unsigned int* buffer = jpeg_load(&img_j); - // PPM out = inherit_from_jpeg(&img_j, '3'); - // print_attr(&out); - // - // ppm_save(buffer, out, "../assets/test_2.ppm"); - // free(buffer); - PPM img = { - .name = "../assets/test_2.ppm", - }; + // Add a file location of image to modify + img.name = "../assets/test_2.ppm"; - // unsigned int* buf = ppm_load(img.name, &img); - // unsigned int* box = box_blur(buf, &img, 20); - // free(buf); - img.fmt = '3'; + // Load the PPM image unsigned int* buf = ppm_load(img.name, &img); + + // Check for null image load. In case image doesnt exist or fails to + // load if (!buf) { + LOGERROR(ERR_BUF_NULL); return ERR_BUF_NULL; } + // Blur image and save to new pointer unsigned int* blur = box_blur(buf, &img, 20); + + // Free old buffer. MUST DO THIS. Maybe not immediately but before + // ending or possibly leaking. Also it stays in heap and a large image + // can take a lot of memory so better to clear anyways. free(buf); + // Check for null image load. In case image doesnt exist or fails to + // load if (!blur) { + LOGERROR(ERR_BUF_NULL); return ERR_BUF_NULL; } + + // Loop image and apply filter to each pixel. for (int i = 0; i < img.len; i++) { blur[i] = u32_rotl(blur[i], 4); } - ppm_save(blur, img, "../assets/test_2_new.ppm"); + + // Take the attributes from ppm image so jpeg file can be saved + jimg = inherit_from_ppm(&img); + + // Saves jpeg to location specified + jpeg_save(blur, &jimg, "../assets/test_2_new.jpg"); + + // Make sure to free any buffer still active free(blur); - // // buf = u32_rotate(buf, &img); - // ppm_save(box, img, "../assets/test_new.ppm"); - // free(box); - - // PPM out_2 = inherit_from_jpeg(&out_1, '6'); - // - // - - // PPM out = inherit_from_jpeg(&out_1, '6'); - // print_attr(&out); - // unsigned int* src_buf = ppm_load("../assets/8ktest.ppm", &out_2); - // unsigned int* blurred = box_blur(src_buf, &out_2, 10); - // unsigned int* flip = u32_rotate(src_buf_jpeg, ); - // ppm_save(blurred, out_2); - // free(src_buf); - // free(blurred); return 0; }