From 854f5c2ba6c1572d1f7d60e9a8559cb85c292ab0 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Mon, 31 Aug 2026 22:18:42 -0400 Subject: [PATCH] Many changes just look at the new documentation and such --- .DS_Store | Bin 8196 -> 10244 bytes CMakeLists.txt | 6 +- compile_commands.json | 8 --- include/image/imgio.h | 2 +- include/image/utils.h | 137 ++++++++++++++++++++++++++++++++++-------- src/imgio.c | 9 +-- src/main.c | 24 ++++++-- 7 files changed, 139 insertions(+), 47 deletions(-) delete mode 100644 compile_commands.json diff --git a/.DS_Store b/.DS_Store index 1ab732477a337bebe162b2b5d76801d5bea05942..a4574cddf7185868b95b1063b90f3a7d1394587d 100644 GIT binary patch delta 914 zcmdUtO=}ZT6o%hp3T(?~aV zk*y!`?gY1j)J@Dr?8=4UZx953fS?OeT{#iQ&*9^M zbnZewS&R}H=_p}pBCjf!%@4i(RnPaVcs!je9UPBg33=vnjk)ZgjVE}9eZ0kce8P7c zrDw7j=Cyp2s4qy)Rq6I$be@%Bb z8K)B=15Nu2gX?|2A%N_e%|S*3NDdr|Wp(kl`||Cc`?Xzu0*}c3 zdFZI3iTikrr+A4sc!!VpN+YEG*?o6Uy%p>1+9QcrRA+vi*R`-SJDcBPLP}jxk3{|2 a`wvEu4TYkXx+k6|>i-8d=~vH0)P4fS`Qt+X delta 260 zcmZn(XmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD84aZH$S8JW*z}P#>p;%rIWu3 z@=yLO(ztntC@UkIkhp}Tko06bVR4~&0si8Q{BqCa{Jemo)UwRf^2v$9yEr21_+CTL7TymA($bCp^%}0VLHQN zhV={w8O||0X86kRpOKeQj8TbEn^A|+l+m2gfzgpMh%uTmfiZFNei7BpnG)vAg3J(e iB)EaJE66h&3%@f@=2r>iV1#&)VRAgr)Xmc**qH&IOF32m diff --git a/CMakeLists.txt b/CMakeLists.txt index ada90a5..7c29650 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.16) -project(image_processor LANGUAGES C) +project(libimg LANGUAGES C) set(CMAKE_C_STANDARD 23) set(CMAKE_C_STANDARD_REQUIRED ON) @@ -31,6 +31,6 @@ target_link_libraries(PROCESSOR PUBLIC PkgConfig::TURBOJPEG) set_target_properties(PROCESSOR PROPERTIES EXPORT_NAME PROCESSOR) if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) - add_executable(image_processor src/main.c) # Assuming you have a main.c - target_link_libraries(image_processor PRIVATE PROCESSOR) + add_executable(libimg src/main.c) # Assuming you have a main.c + target_link_libraries(libimg PRIVATE PROCESSOR) endif() diff --git a/compile_commands.json b/compile_commands.json deleted file mode 100644 index 687696b..0000000 --- a/compile_commands.json +++ /dev/null @@ -1,8 +0,0 @@ -[ -{ - "directory": "/Users/andrew/personal/Projects/C/image_processor", - "command": "/usr/bin/cc -I/Users/andrew/personal/Projects/C/image_processor/include -I/opt/homebrew/include -g -std=gnu2x -arch arm64 -o CMakeFiles/image_processor.dir/src/main.c.o -c /Users/andrew/personal/Projects/C/image_processor/src/main.c", - "file": "/Users/andrew/personal/Projects/C/image_processor/src/main.c", - "output": "CMakeFiles/image_processor.dir/src/main.c.o" -} -] \ No newline at end of file diff --git a/include/image/imgio.h b/include/image/imgio.h index f4950a4..59916b5 100644 --- a/include/image/imgio.h +++ b/include/image/imgio.h @@ -7,7 +7,7 @@ unsigned int* ppm_load(const char* file, PPM* m_attr); int -ppm_save(const unsigned int* buf, const PPM img); +ppm_save(const unsigned int* buf, const PPM img, char* name); unsigned int* jpeg_load(JPEG* m_attr); diff --git a/include/image/utils.h b/include/image/utils.h index eb3a1f1..03d2aaa 100644 --- a/include/image/utils.h +++ b/include/image/utils.h @@ -1,69 +1,154 @@ #ifndef UTILS_H #define UTILS_H -#define BYTE_A 0xFF000000u -#define BYTE_R 0x00FF0000u -#define BYTE_G 0x0000FF00u -#define BYTE_B 0x000000FFu -#define BYTE_RGB 0x00FFFFFFu -#define BYTE 0xFFu -#define LONG_BYTE 0xFFFFu +/* + * 0 0 0 0 0 0 0 0 + * ^ - Starting Bit for all calculations + * ^ - Starting bit for when I talk about sets of bits + */ -#define SHIFT_8 8 -#define SHIFT_16 16 -#define SHIFT_24 24 +// The u in the BYTE Macros are for unsigned calls so colors are not changed to +// signed and the values cut in half +#define BYTE_A 0xFF000000u /**< Isolates the First Byte for alpha */ +#define BYTE_R 0x00FF0000u /**< Isolates the Second Byte for red */ +#define BYTE_G 0x0000FF00u /**< Isolates the Third Byte for green */ +#define BYTE_B 0x000000FFu /**< Isolates the Fourth Byte for blue */ +#define BYTE_RGB 0x00FFFFFFu /**< Isolates 24-Bits from end (RGB) */ +#define BYTE 0xFFu /**< Pulls out 8 shifted Bits */ +#define LONG_BYTE 0xFFFFu /**< Pulls out 16 shifted Bits */ +#define SHIFT_8 8 /**< Constant shift of 8-Bits */ +#define SHIFT_16 16 /**< Constant shift of 16-Bits */ +#define SHIFT_24 24 /**< Constant shift of 24-Bits */ + +/** + * + * @brief Max value for any channel + * @details For alpha channel this is the max opacity setting and 0 is fully + * transparent, 255 is white for any color and 0 is black. + * */ #define MAX 255 -#define HALF_MAX ((int)(MAX / 2) + 1) +#define HALF_MAX \ + ((int)(MAX / 2) + 1) /**< Half Max Color Value for any channel */ +/**< Func to isolate alpha channel into variable */ #define DECODE_A(clr) (((clr) >> SHIFT_24) & BYTE) +/**< Func to isolate red channel into variable */ #define DECODE_R(clr) (((clr) >> SHIFT_16) & BYTE) +/**< Func to isolate green channel into variable */ #define DECODE_G(clr) (((clr) >> SHIFT_8) & BYTE) +/**< Func to isolate blue channel into variable */ #define DECODE_B(clr) ((clr) & BYTE) +/** + * @brief Packs RGB channels into int + * @details Takes R, G, B char channels and packs them into a singular integer + * with the first 8 Bits reserved for alpha channel (set to MAX by default). See + * diagram at top for my thinking of starting bits + * @param[in] r The red channel stored as char and packed into int + * @param[in] g The green channel stored as char and packed into int + * @param[in] b The blue channel stored as char and packed into int + * + * */ #define ENCODE_RGB(r, g, b) \ (((unsigned int)(MAX) << SHIFT_24) | ((unsigned int)(r) << SHIFT_16) | \ ((unsigned int)(g) << SHIFT_8) | (unsigned int)(b)) +/** + * @brief Packs ARGB channels into int + * @details Takes A, R, G, B char channels and packs them into a singular + * integer with the first 8 Bits reserved for alpha channel. See diagram at top + * for my thinking of starting bits + * @param[in] a The alpha channel stored as char and packed into int + * @param[in] r The red channel stored as char and packed into int + * @param[in] g The green channel stored as char and packed into int + * @param[in] b The blue channel stored as char and packed into int + * + * */ #define ENCODE_ARGB(a, r, g, b) \ (((unsigned int)(a) << SHIFT_24) | ((unsigned int)(r) << SHIFT_16) | \ ((unsigned int)(g) << SHIFT_8) | (unsigned int)(b)) -// #define ENCODE_BGRA(b, g, r, a) \ -// (((unsigned int)(b) << SHIFT_24) | ((unsigned int)(g) << SHIFT_16) | \ -// ((unsigned int)(r) << SHIFT_8) | (unsigned int)(a)) - +/** + * @brief Stops values exiting 0 - 255 range + * @details Takes in a value of X and checks if it is Above the Max of 255. If + * so, then set to Max. If not, then simply set back to whatever X was before. + * @param[in] x Color channel: char + * */ #define CLAMP_MAX(x) (((x) > MAX) ? MAX : (x)) + +/** + * @brief Stops values exiting 0 - 255 range + * @details Takes in a value of X and checks if it is Below 0. If so, then set + * to 0. If not, then simply set back to whatever X was before. + * @param[in] x Color channel: char + * */ #define CLAMP_MIN(x) (((x) < 0) ? 0 : (x)) +/** + * @brief Contains values for PPM Image + * @details Can be defined with just a name and filled later with ppm_load + * as it stores information for header of file not the pixels of the image + * */ typedef struct { - int width, height; - int len; - char* name; - char fmt; + int width; /**< Image width in pixels */ + int height; /**< Image height in pixels */ + int len; /**< Image length in pixels, width * height */ + char* name; /**< File name and location, relative */ + char fmt; /**< Raw or Binary values, P3 or P6 respectively*/ } PPM; +/** + * @brief Contains values for JPEG Image + * @details Can be defined with just a name and filled later with jpeg_load as + * it stores information for header of file not the pixels of the image + * */ typedef struct { - int width, height; - int len; - char* name; + int width; /**< Image width in pixels */ + int height; /**< Image height in pixels */ + int len; /**< Image length in pixels, width * height */ + char* name; /**< File name and location, relative */ } JPEG; +/** + * @brief Converts an int to long + * @details Takes in 32-Bit packed color and separates the 4 color values into a + * 64-Bit packed color for JPEG translations. + * @param[in] clr Takes a packed color, unsigned int + * @return unsigned long Packed color in 4 ints instead of one + * */ unsigned long u32to_u64(const unsigned int clr); +/** + * @brief Converts a long to int + * @details Takes in a full packed color and compresses it back into 1 ints for + * ppm translations. + * @param[in] clr Takes a packed color, unsigned long + * @param[in] u64_div Takes a divisor to break down to raw color + * @return unsigned int Packed color in 1 int instead of 4 + * */ unsigned int u64to_u32(const unsigned long clr, const unsigned long u64_div); /** - * Pretty Self Explanitory but prints the attributes of PPM images + * @brief Prints quick debug info of container + * @details Prints metadata like Name, Format, Width, Height and Length + * important for debugging errors + * @param[in] src Pointer to src metadata of a PPM image * */ -void __attribute__((overloadable)) print_attr(const PPM *src); +void __attribute__((overloadable)) +print_attr(const PPM* src); + /** - * Pretty Self Explanitory but prints the attributes of JPEG images + * @brief Prints quick debug info of container + * @details Prints metadata like Name, Width, Height and Length + * important for debugging errors + * @param[in] src Pointer to src metadata of a JPEG image * */ -void __attribute__((overloadable)) print_attr(const JPEG *src); +void __attribute__((overloadable)) +print_attr(const JPEG* src); #endif diff --git a/src/imgio.c b/src/imgio.c index ebe4222..248fe9b 100644 --- a/src/imgio.c +++ b/src/imgio.c @@ -65,7 +65,7 @@ ppm_load(const char* file, PPM* m_attr) } int -ppm_save(const unsigned int* buf, const PPM img) +ppm_save(const unsigned int* buf, const PPM img, char* name) { if (!buf) { @@ -82,12 +82,12 @@ ppm_save(const unsigned int* buf, const PPM img) if (img.fmt == '3') { - fptr = fopen(img.name, "w"); + fptr = fopen(name, "w"); } if (img.fmt == '6') { - fptr = fopen(img.name, "wb"); + fptr = fopen(name, "wb"); } if (!fptr) @@ -125,6 +125,7 @@ ppm_save(const unsigned int* buf, const PPM img) fclose(fptr); free(dst_buf); printf("Encode Done!\n"); + printf("Image: %s is saved.\n", name); return 1; } @@ -209,7 +210,7 @@ transcode_to_ppm(JPEG* input, PPM* out) out->width = input->width; out->height = input->height; out->len = out->width * out->height; - ppm_save(dst, *out); + ppm_save(dst, *out, out->name); free(dst); } diff --git a/src/main.c b/src/main.c index 96beafb..48ffc1f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,6 @@ #include + +#include "image/filters.h" #include "image/utils.h" // #include "image/filters.h" // #include "image/transformations.h" @@ -7,16 +9,28 @@ int main() { - JPEG out_1 = { - .name = "../MF8jno.jpg", + // JPEG out_1 = { + // .name = "../MF8jno.jpg", + // + // }; + PPM img = { + .name = "../IMG_1649.ppm", + }; - }; + unsigned int* buf = ppm_load(img.name, &img); + for (int i = 0; i < img.len; i++) + { + // you need to replace the old value with the new updated value + buf[i] = u32_grayscale(buf[i]); + } + ppm_save(buf, img, "../IMG_1649_new.ppm"); // PPM out_2 = inherit_from_jpeg(&out_1, '6'); // // - PPM out = inherit_from_jpeg(&out_1, '6'); - // print_attr(&out); + + // 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, );