diff --git a/src/main.c b/src/main.c index 01e3014..894c237 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,6 @@ #include #include #include -#include #define BYTE_A 0xFF000000 #define BYTE_R 0x00FF0000 @@ -42,19 +41,19 @@ typedef struct { - int width, height, len; - const char* name; + int width, height; + int len; + char* name; char fmt; } PPM; typedef struct { int width, height; - size_t len; + int len; char* name; } JPEG; -// Decodes int to a long static inline unsigned long u32to_u64(const unsigned int clr) { @@ -78,26 +77,26 @@ u64to_u32(const unsigned long clr, const unsigned long u64_div) } static inline unsigned int -set_alpha(const unsigned int clr, const unsigned char alpha) +u32_put_alpha(const unsigned int clr, const unsigned char alpha) { return (clr & BYTE_RGB) | alpha << SHIFT_24; } static inline unsigned int -invert_rgb(const unsigned int clr) +u32_invert(const unsigned int clr) { return ((~clr) & BYTE_RGB) | (clr & BYTE_A); } static inline unsigned int -swap_rb(const unsigned int clr) +u32_swap_rb(const unsigned int clr) { return (clr & 0xFF00FF00) | ((clr & BYTE_R) >> SHIFT_16) | ((clr & BYTE_B) << SHIFT_16); } static inline unsigned int -to_grayscale(const unsigned int clr) +u32_grayscale(const unsigned int clr) { unsigned int red = DECODE_R(clr); unsigned int green = DECODE_G(clr); @@ -109,9 +108,9 @@ to_grayscale(const unsigned int clr) } static inline unsigned int -apply_threshold(const unsigned int clr, const unsigned char max) +u32_step(const unsigned int clr, const unsigned char max) { - if (DECODE_B(to_grayscale(clr)) < max) + if (DECODE_B(u32_grayscale(clr)) < max) { return ENCODE_ARGB(0, 0, 0, 0); } @@ -119,7 +118,7 @@ apply_threshold(const unsigned int clr, const unsigned char max) } static inline unsigned int -adjust_brightness(const unsigned int clr, const float factor) +u32_brighten(const unsigned int clr, const float factor) { unsigned int alpha = (clr & BYTE_A); unsigned int red = @@ -132,7 +131,7 @@ adjust_brightness(const unsigned int clr, const float factor) } static inline unsigned int -apply_tint(const unsigned int clr_a, const unsigned int clr_b) +u32_mix(const unsigned int clr_a, const unsigned int clr_b) { unsigned char blue = (DECODE_B(clr_a) + DECODE_B(clr_b)) / 2; unsigned char green = (DECODE_G(clr_a) + DECODE_G(clr_b)) / 2; @@ -142,7 +141,7 @@ apply_tint(const unsigned int clr_a, const unsigned int clr_b) } static inline unsigned int -apply_opacity(const unsigned clr_a, const unsigned int clr_b, const float ratio) +u32_lerp(const unsigned clr_a, const unsigned int clr_b, const float ratio) { unsigned char blue = (unsigned char)((DECODE_B(clr_a) * (1 - ratio)) + ((DECODE_B(clr_b) * ratio))); @@ -154,7 +153,7 @@ apply_opacity(const unsigned clr_a, const unsigned int clr_b, const float ratio) } static inline unsigned int -channel_mask(const unsigned int clr, const char keep) +u32_mask(const unsigned int clr, const char keep) { switch (keep) { @@ -175,12 +174,12 @@ channel_mask(const unsigned int clr, const char keep) } static inline unsigned int -chromatic_circular_shift(const unsigned int clr, const int n) +u32_rotl(const unsigned int clr, const int n) { - if (n > 8) - { - return 0; - } + if (n > 8) + { + return 0; + } unsigned int red = DECODE_R(clr); unsigned int green = DECODE_G(clr); unsigned int blue = DECODE_B(clr); @@ -191,7 +190,7 @@ chromatic_circular_shift(const unsigned int clr, const int n) } static inline unsigned int* -flip_horizontal(const unsigned int* buf, const PPM img) +ppm_flip_h(const unsigned int* buf, const PPM img) { unsigned int* flip = malloc(img.len * sizeof(unsigned int)); if (!flip) @@ -212,7 +211,7 @@ flip_horizontal(const unsigned int* buf, const PPM img) } static inline unsigned int* -flip_vertical(const unsigned int* buf, const PPM img) +ppm_flip_v(const unsigned int* buf, const PPM img) { unsigned int* flip = malloc(img.len * sizeof(unsigned int)); if (!flip) @@ -234,7 +233,7 @@ flip_vertical(const unsigned int* buf, const PPM img) } static inline unsigned int -constrast_adjustment(unsigned int clr, float factor) +u32_contrast(unsigned int clr, float factor) { unsigned int mask = 0; int red = CLAMP_MIN(CLAMP_MAX( @@ -247,7 +246,7 @@ constrast_adjustment(unsigned int clr, float factor) } static inline unsigned int* -rotate(const unsigned int* buf, PPM* img, const int len) +u32_rotate(const unsigned int* buf, PPM* img, const int len) { unsigned int* mask = malloc(len * sizeof(unsigned int)); int old_width = img->width; @@ -268,18 +267,8 @@ rotate(const unsigned int* buf, PPM* img, const int len) return mask; } -static inline int -is_valid(const int x, const int y, const int width, const int height) -{ - if (x < 0 || x >= width || y < 0 || y >= height) - { - return 0; - } - return 1; -} - static inline unsigned int -brightness_mirror(const unsigned int clr) +u32_avg_gb(const unsigned int clr) { unsigned char red = DECODE_R(clr); unsigned char blue = DECODE_B(clr); @@ -287,10 +276,9 @@ brightness_mirror(const unsigned int clr) return ENCODE_RGB(red, green, blue); } -// Used strictly for box_blur static inline unsigned int -get_pixel_x(const unsigned int* src, const int x, const int y, const PPM* img, - const int radius) +blur_col(const unsigned int* src, const int x, const int y, const PPM* img, + const int radius) { unsigned long pt = 0, cnt = 0; @@ -311,10 +299,9 @@ get_pixel_x(const unsigned int* src, const int x, const int y, const PPM* img, return src[y * img->width + x]; } -// Used strictly for box_blur static inline unsigned int -get_pixel_y(const unsigned int* src, const int x, const int y, const PPM* img, - const int radius) +blur_row(const unsigned int* src, const int x, const int y, const PPM* img, + const int radius) { unsigned long pt = 0, cnt = 0; @@ -335,7 +322,7 @@ get_pixel_y(const unsigned int* src, const int x, const int y, const PPM* img, return src[y * img->width + x]; } -static inline unsigned int* +unsigned int* box_blur(const unsigned int* src_buf, const PPM* img, const int radius) { unsigned int* first_pass = malloc(img->len * sizeof(unsigned int)); @@ -354,7 +341,7 @@ box_blur(const unsigned int* src_buf, const PPM* img, const int radius) for (int x = 0; x < img->width; x++) { first_pass[y * img->width + x] = - get_pixel_x(src_buf, x, y, img, radius); + blur_col(src_buf, x, y, img, radius); } } for (int y = 0; y < img->height; y++) @@ -362,24 +349,15 @@ box_blur(const unsigned int* src_buf, const PPM* img, const int radius) for (int x = 0; x < img->width; x++) { dst_buf[y * img->width + x] = - get_pixel_y(first_pass, x, y, img, radius); + blur_row(first_pass, x, y, img, radius); } } free(first_pass); return dst_buf; } -static inline void -ppm_apply_brightness(unsigned int* buf, const PPM img, const float factor) -{ - for (int i = 0; i < img.len; i++) - { - buf[i] = adjust_brightness(buf[i], factor); - } -} - static inline unsigned int* -ppm_decode(const char* file, PPM* img) +ppm_load(const char* file, PPM* img) { printf("Starting decode...\n"); unsigned char red; @@ -401,7 +379,10 @@ ppm_decode(const char* file, PPM* img) fscanf(fptr, "%d", &max_val); fgetc(fptr); unsigned char* img_buf = malloc(img->len * 3); - if (!img_buf) return NULL; + if (!img_buf) + { + return NULL; + } unsigned int* clr_buf = malloc(img->len * sizeof(unsigned int)); if (!clr_buf) { @@ -436,12 +417,18 @@ ppm_decode(const char* file, PPM* img) } static inline void -ppm_encode(const unsigned int* buf, const PPM img) +ppm_save(const unsigned int* buf, const PPM img) { - if (!buf) return; + if (!buf) + { + return; + } unsigned char red, green, blue; unsigned char* dst_buf = malloc(img.len * 3); - if (!dst_buf) return; + if (!dst_buf) + { + return; + } printf("Starting encode...\n"); FILE* fptr; @@ -490,7 +477,7 @@ ppm_encode(const unsigned int* buf, const PPM img) } unsigned int* -jpeg_decode(JPEG* attr) +jpeg_load(JPEG* attr) { printf("Starting Decode...\n"); tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS); @@ -525,7 +512,7 @@ jpeg_decode(JPEG* attr) } void -jpeg_encode(unsigned int* src_buf, JPEG* attr, char* name) +jpeg_save(unsigned int* src_buf, JPEG* attr, char* name) { printf("Starting Decode...\n"); tjhandle tjcompress = tj3Init(TJINIT_COMPRESS); @@ -556,20 +543,20 @@ jpeg_encode(unsigned int* src_buf, JPEG* attr, char* name) void ppm_to_jpeg(PPM* in, JPEG* out) { - unsigned int* src_buf = ppm_decode(in->name, in); + unsigned int* src_buf = ppm_load(in->name, in); out->width = in->width; out->height = in->height; - jpeg_encode(src_buf, out, out->name); + jpeg_save(src_buf, out, out->name); } void jpeg_to_ppm(JPEG* input, PPM* out) { - unsigned int* src_buf = jpeg_decode(input); + unsigned int* src_buf = jpeg_load(input); out->width = input->width; out->height = input->height; out->len = out->width * out->height; - ppm_encode(src_buf, *out); + ppm_save(src_buf, *out); } int @@ -579,15 +566,10 @@ main() .name = "../test.ppm", }; - unsigned int* src_buf = ppm_decode("../assets/8ktest.ppm", &out); - if (src_buf) - { - src_buf = box_blur(src_buf, &out, 10); - } - ppm_encode(src_buf, out); - if (src_buf) - { - free(src_buf); - } + unsigned int* src_buf = ppm_load("../assets/8ktest.ppm", &out); + unsigned int* blurred = box_blur(src_buf, &out, 10); + ppm_save(blurred, out); + free(src_buf); + free(blurred); return 0; }