Changed function names to match <output_func>

This commit is contained in:
Andrew Haynes
2026-03-22 13:15:06 -04:00
parent 36fb0ebf32
commit 319a6e3b2c
+56 -74
View File
@@ -3,7 +3,6 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <turbojpeg.h> #include <turbojpeg.h>
#include <unistd.h>
#define BYTE_A 0xFF000000 #define BYTE_A 0xFF000000
#define BYTE_R 0x00FF0000 #define BYTE_R 0x00FF0000
@@ -42,19 +41,19 @@
typedef struct typedef struct
{ {
int width, height, len; int width, height;
const char* name; int len;
char* name;
char fmt; char fmt;
} PPM; } PPM;
typedef struct typedef struct
{ {
int width, height; int width, height;
size_t len; int len;
char* name; char* name;
} JPEG; } JPEG;
// Decodes int to a long
static inline unsigned long static inline unsigned long
u32to_u64(const unsigned int clr) 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 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; return (clr & BYTE_RGB) | alpha << SHIFT_24;
} }
static inline unsigned int static inline unsigned int
invert_rgb(const unsigned int clr) u32_invert(const unsigned int clr)
{ {
return ((~clr) & BYTE_RGB) | (clr & BYTE_A); return ((~clr) & BYTE_RGB) | (clr & BYTE_A);
} }
static inline unsigned int 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) | return (clr & 0xFF00FF00) | ((clr & BYTE_R) >> SHIFT_16) |
((clr & BYTE_B) << SHIFT_16); ((clr & BYTE_B) << SHIFT_16);
} }
static inline unsigned int static inline unsigned int
to_grayscale(const unsigned int clr) u32_grayscale(const unsigned int clr)
{ {
unsigned int red = DECODE_R(clr); unsigned int red = DECODE_R(clr);
unsigned int green = DECODE_G(clr); unsigned int green = DECODE_G(clr);
@@ -109,9 +108,9 @@ to_grayscale(const unsigned int clr)
} }
static inline unsigned int 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); 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 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 alpha = (clr & BYTE_A);
unsigned int red = unsigned int red =
@@ -132,7 +131,7 @@ adjust_brightness(const unsigned int clr, const float factor)
} }
static inline unsigned int 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 blue = (DECODE_B(clr_a) + DECODE_B(clr_b)) / 2;
unsigned char green = (DECODE_G(clr_a) + DECODE_G(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 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)) + unsigned char blue = (unsigned char)((DECODE_B(clr_a) * (1 - ratio)) +
((DECODE_B(clr_b) * 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 static inline unsigned int
channel_mask(const unsigned int clr, const char keep) u32_mask(const unsigned int clr, const char keep)
{ {
switch (keep) switch (keep)
{ {
@@ -175,12 +174,12 @@ channel_mask(const unsigned int clr, const char keep)
} }
static inline unsigned int 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) if (n > 8)
{ {
return 0; return 0;
} }
unsigned int red = DECODE_R(clr); unsigned int red = DECODE_R(clr);
unsigned int green = DECODE_G(clr); unsigned int green = DECODE_G(clr);
unsigned int blue = DECODE_B(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* 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)); unsigned int* flip = malloc(img.len * sizeof(unsigned int));
if (!flip) if (!flip)
@@ -212,7 +211,7 @@ flip_horizontal(const unsigned int* buf, const PPM img)
} }
static inline unsigned int* 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)); unsigned int* flip = malloc(img.len * sizeof(unsigned int));
if (!flip) if (!flip)
@@ -234,7 +233,7 @@ flip_vertical(const unsigned int* buf, const PPM img)
} }
static inline unsigned int static inline unsigned int
constrast_adjustment(unsigned int clr, float factor) u32_contrast(unsigned int clr, float factor)
{ {
unsigned int mask = 0; unsigned int mask = 0;
int red = CLAMP_MIN(CLAMP_MAX( int red = CLAMP_MIN(CLAMP_MAX(
@@ -247,7 +246,7 @@ constrast_adjustment(unsigned int clr, float factor)
} }
static inline unsigned int* 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)); unsigned int* mask = malloc(len * sizeof(unsigned int));
int old_width = img->width; int old_width = img->width;
@@ -268,18 +267,8 @@ rotate(const unsigned int* buf, PPM* img, const int len)
return mask; 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 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 red = DECODE_R(clr);
unsigned char blue = DECODE_B(clr); unsigned char blue = DECODE_B(clr);
@@ -287,10 +276,9 @@ brightness_mirror(const unsigned int clr)
return ENCODE_RGB(red, green, blue); return ENCODE_RGB(red, green, blue);
} }
// Used strictly for box_blur
static inline unsigned int static inline unsigned int
get_pixel_x(const unsigned int* src, const int x, const int y, const PPM* img, blur_col(const unsigned int* src, const int x, const int y, const PPM* img,
const int radius) const int radius)
{ {
unsigned long pt = 0, cnt = 0; 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]; return src[y * img->width + x];
} }
// Used strictly for box_blur
static inline unsigned int static inline unsigned int
get_pixel_y(const unsigned int* src, const int x, const int y, const PPM* img, blur_row(const unsigned int* src, const int x, const int y, const PPM* img,
const int radius) const int radius)
{ {
unsigned long pt = 0, cnt = 0; 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]; 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) box_blur(const unsigned int* src_buf, const PPM* img, const int radius)
{ {
unsigned int* first_pass = malloc(img->len * sizeof(unsigned int)); 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++) for (int x = 0; x < img->width; x++)
{ {
first_pass[y * 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++) 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++) for (int x = 0; x < img->width; x++)
{ {
dst_buf[y * 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); free(first_pass);
return dst_buf; 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* static inline unsigned int*
ppm_decode(const char* file, PPM* img) ppm_load(const char* file, PPM* img)
{ {
printf("Starting decode...\n"); printf("Starting decode...\n");
unsigned char red; unsigned char red;
@@ -401,7 +379,10 @@ ppm_decode(const char* file, PPM* img)
fscanf(fptr, "%d", &max_val); fscanf(fptr, "%d", &max_val);
fgetc(fptr); fgetc(fptr);
unsigned char* img_buf = malloc(img->len * 3); 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)); unsigned int* clr_buf = malloc(img->len * sizeof(unsigned int));
if (!clr_buf) if (!clr_buf)
{ {
@@ -436,12 +417,18 @@ ppm_decode(const char* file, PPM* img)
} }
static inline void 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 red, green, blue;
unsigned char* dst_buf = malloc(img.len * 3); unsigned char* dst_buf = malloc(img.len * 3);
if (!dst_buf) return; if (!dst_buf)
{
return;
}
printf("Starting encode...\n"); printf("Starting encode...\n");
FILE* fptr; FILE* fptr;
@@ -490,7 +477,7 @@ ppm_encode(const unsigned int* buf, const PPM img)
} }
unsigned int* unsigned int*
jpeg_decode(JPEG* attr) jpeg_load(JPEG* attr)
{ {
printf("Starting Decode...\n"); printf("Starting Decode...\n");
tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS); tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS);
@@ -525,7 +512,7 @@ jpeg_decode(JPEG* attr)
} }
void 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"); printf("Starting Decode...\n");
tjhandle tjcompress = tj3Init(TJINIT_COMPRESS); tjhandle tjcompress = tj3Init(TJINIT_COMPRESS);
@@ -556,20 +543,20 @@ jpeg_encode(unsigned int* src_buf, JPEG* attr, char* name)
void void
ppm_to_jpeg(PPM* in, JPEG* out) 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->width = in->width;
out->height = in->height; out->height = in->height;
jpeg_encode(src_buf, out, out->name); jpeg_save(src_buf, out, out->name);
} }
void void
jpeg_to_ppm(JPEG* input, PPM* out) 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->width = input->width;
out->height = input->height; out->height = input->height;
out->len = out->width * out->height; out->len = out->width * out->height;
ppm_encode(src_buf, *out); ppm_save(src_buf, *out);
} }
int int
@@ -579,15 +566,10 @@ main()
.name = "../test.ppm", .name = "../test.ppm",
}; };
unsigned int* src_buf = ppm_decode("../assets/8ktest.ppm", &out); unsigned int* src_buf = ppm_load("../assets/8ktest.ppm", &out);
if (src_buf) unsigned int* blurred = box_blur(src_buf, &out, 10);
{ ppm_save(blurred, out);
src_buf = box_blur(src_buf, &out, 10); free(src_buf);
} free(blurred);
ppm_encode(src_buf, out);
if (src_buf)
{
free(src_buf);
}
return 0; return 0;
} }