This commit is contained in:
Andrew Haynes
2026-03-19 17:46:47 -04:00
parent e3b2e7dd2f
commit 1bcf91e9a2
4 changed files with 34 additions and 66 deletions
Vendored
BIN
View File
Binary file not shown.
+8
View File
@@ -0,0 +1,8 @@
[
{
"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"
}
]
BIN
View File
Binary file not shown.
+26 -66
View File
@@ -53,11 +53,6 @@ typedef struct
char* name; char* name;
} JPEG; } JPEG;
typedef struct
{
unsigned int red, green, blue;
} Pixel;
static inline unsigned int static inline unsigned int
set_alpha(const unsigned int clr, const unsigned char alpha_value) set_alpha(const unsigned int clr, const unsigned char alpha_value)
{ {
@@ -168,7 +163,7 @@ chromatic_circular_shift(const unsigned int clr, const int n)
} }
static inline unsigned int* static inline unsigned int*
horizontal_flip(unsigned int* buf, const PPM img) flip_horizontal(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) return NULL; if (!flip) return NULL;
@@ -182,12 +177,11 @@ horizontal_flip(unsigned int* buf, const PPM img)
} }
} }
free(buf);
return flip; return flip;
} }
static inline unsigned int* static inline unsigned int*
vertical_flip(unsigned int* buf, PPM img) flip_vertical(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) return NULL; if (!flip) return NULL;
@@ -201,7 +195,6 @@ vertical_flip(unsigned int* buf, PPM img)
idx++; idx++;
} }
} }
free(buf);
return flip; return flip;
} }
@@ -220,7 +213,7 @@ constrast_adjustment(unsigned int clr, float factor)
} }
static inline unsigned int* static inline unsigned int*
rotate(unsigned int* buf, PPM* img, int len) 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;
@@ -238,23 +231,11 @@ rotate(unsigned int* buf, PPM* img, int len)
} }
img->width = old_height; img->width = old_height;
img->height = old_width; img->height = old_width;
free(buf);
return mask; return mask;
} }
static inline int static inline int
get_neighbor_idx(int curr_idx, int width, char* direction) is_valid(const int x, const int y, const int width, const int height)
{
if (strncmp(direction, "above", 5) == 0) return curr_idx - width;
if (strncmp(direction, "below", 5) == 0) return curr_idx + width;
if (strncmp(direction, "right", 5) == 0) return curr_idx + 1;
if (strncmp(direction, "left", 4) == 0) return curr_idx + 1;
return curr_idx;
}
static inline int
is_valid(int x, int y, int width, int height)
{ {
if (x < 0 || x >= width) return 0; if (x < 0 || x >= width) return 0;
if (y < 0 || y >= height) return 0; if (y < 0 || y >= height) return 0;
@@ -262,7 +243,7 @@ is_valid(int x, int y, int width, int height)
} }
static inline unsigned int static inline unsigned int
brightness_mirror(unsigned int clr) brightness_mirror(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);
@@ -270,32 +251,8 @@ brightness_mirror(unsigned int clr)
return ENCODE_RGB(red, green, blue); return ENCODE_RGB(red, green, blue);
} }
static inline unsigned int
get_pixel_safe(unsigned int* buf, int x, int y, int width, int height)
{
if (is_valid(x, y, width, height))
{
return buf[(y * width + x)];
}
return 0;
}
static inline int
add_pixel(unsigned int pixel, unsigned int* red, unsigned int* green,
unsigned int* blue)
{
if (pixel != 0)
{
*red += decode_r(pixel);
*green += decode_g(pixel);
*blue += decode_b(pixel);
return 1;
}
return 0;
}
static inline unsigned int* static inline unsigned int*
box_blur(unsigned int* src_buf, PPM* img, int radius) box_blur(const unsigned int* src_buf, const PPM* img, const int radius)
{ {
unsigned int* tmp_buf = malloc(img->len * sizeof(unsigned int)); unsigned int* tmp_buf = malloc(img->len * sizeof(unsigned int));
if (!tmp_buf) return 0; if (!tmp_buf) return 0;
@@ -370,7 +327,6 @@ box_blur(unsigned int* src_buf, PPM* img, int radius)
} }
printf("Pass 2: Successful\n"); printf("Pass 2: Successful\n");
free(tmp_buf); free(tmp_buf);
free(src_buf);
return dst_buf; return dst_buf;
} }
@@ -396,7 +352,7 @@ ppm_decode(const char* file, PPM* img)
fptr = fopen(file, "r"); fptr = fopen(file, "r");
if (!fptr) if (!fptr)
{ {
printf("Invalid file name: %s", file); printf("Invalid file name: %s\n", file);
return NULL; return NULL;
} }
fscanf(fptr, "%s", fmt); fscanf(fptr, "%s", fmt);
@@ -440,7 +396,7 @@ ppm_decode(const char* file, PPM* img)
} }
static inline void static inline void
ppm_encode(unsigned int* buf, const PPM img) ppm_encode(const unsigned int* buf, const PPM img)
{ {
if (!buf) return; if (!buf) return;
unsigned char red, green, blue; unsigned char red, green, blue;
@@ -484,7 +440,6 @@ ppm_encode(unsigned int* buf, const PPM img)
} }
fclose(fptr); fclose(fptr);
free(dst_buf); free(dst_buf);
free(buf);
printf("Encode Done!\n"); printf("Encode Done!\n");
} }
@@ -546,7 +501,6 @@ jpeg_encode(unsigned int* src_buf, JPEG* attr, char* name)
tj3Destroy(tjcompress); tj3Destroy(tjcompress);
tj3Free(dst_buf); tj3Free(dst_buf);
if (src_buf) free(src_buf);
printf("Encode Successful!\n"); printf("Encode Successful!\n");
} }
@@ -554,7 +508,6 @@ 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_decode(in->name, in);
// int jpeg_size = jpeg->width * jpeg->height;
out->width = in->width; out->width = in->width;
out->height = in->height; out->height = in->height;
jpeg_encode(src_buf, out, out->name); jpeg_encode(src_buf, out, out->name);
@@ -573,17 +526,24 @@ jpeg_to_ppm(JPEG* input, PPM* out)
int int
main() main()
{ {
PPM ppm = { .width = 3, JPEG jpeg = {
.height = 2, .width = 3,
.name = "../assets/8ktest.ppm", .height = 2,
.len = 0, .len = 0,
.fmt = "P6" }; .name =
JPEG jpeg = { .width = 3, "/Users/andrew/personal/Projects/C/image_processor/assets/wp12450035.jpg"
.height = 2, };
.len = 0,
.name = "../test.jpg" };
// jpeg_to_ppm(&jpeg, &ppm); unsigned int* src_buf = jpeg_decode(&jpeg);
ppm_to_jpeg(&ppm, &jpeg); if(src_buf) {
for(int i = 0; i < jpeg.width * jpeg.height; i++) {
src_buf[i] = channel_mask(src_buf[i], 'b');
}
}
jpeg_encode(src_buf, &jpeg, "../test.jpg");
if (src_buf)
{
free(src_buf);
}
return 0; return 0;
} }