diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 7c52a34..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..687696b --- /dev/null +++ b/compile_commands.json @@ -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" +} +] \ No newline at end of file diff --git a/program b/program deleted file mode 100755 index 4ec36b3..0000000 Binary files a/program and /dev/null differ diff --git a/src/main.c b/src/main.c index 73a9309..8bfbf3f 100644 --- a/src/main.c +++ b/src/main.c @@ -53,11 +53,6 @@ typedef struct char* name; } JPEG; -typedef struct -{ - unsigned int red, green, blue; -} Pixel; - static inline unsigned int 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* -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)); if (!flip) return NULL; @@ -182,12 +177,11 @@ horizontal_flip(unsigned int* buf, const PPM img) } } - free(buf); return flip; } 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)); if (!flip) return NULL; @@ -201,7 +195,6 @@ vertical_flip(unsigned int* buf, PPM img) idx++; } } - free(buf); return flip; } @@ -220,7 +213,7 @@ constrast_adjustment(unsigned int clr, float factor) } 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)); int old_width = img->width; @@ -238,23 +231,11 @@ rotate(unsigned int* buf, PPM* img, int len) } img->width = old_height; img->height = old_width; - free(buf); return mask; } static inline int -get_neighbor_idx(int curr_idx, int width, char* direction) -{ - 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) +is_valid(const int x, const int y, const int width, const int height) { if (x < 0 || x >= width) 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 -brightness_mirror(unsigned int clr) +brightness_mirror(const unsigned int clr) { unsigned char red = decode_r(clr); unsigned char blue = decode_b(clr); @@ -270,32 +251,8 @@ brightness_mirror(unsigned int clr) 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* -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)); if (!tmp_buf) return 0; @@ -370,7 +327,6 @@ box_blur(unsigned int* src_buf, PPM* img, int radius) } printf("Pass 2: Successful\n"); free(tmp_buf); - free(src_buf); return dst_buf; } @@ -396,7 +352,7 @@ ppm_decode(const char* file, PPM* img) fptr = fopen(file, "r"); if (!fptr) { - printf("Invalid file name: %s", file); + printf("Invalid file name: %s\n", file); return NULL; } fscanf(fptr, "%s", fmt); @@ -440,7 +396,7 @@ ppm_decode(const char* file, PPM* img) } static inline void -ppm_encode(unsigned int* buf, const PPM img) +ppm_encode(const unsigned int* buf, const PPM img) { if (!buf) return; unsigned char red, green, blue; @@ -484,7 +440,6 @@ ppm_encode(unsigned int* buf, const PPM img) } fclose(fptr); free(dst_buf); - free(buf); printf("Encode Done!\n"); } @@ -546,7 +501,6 @@ jpeg_encode(unsigned int* src_buf, JPEG* attr, char* name) tj3Destroy(tjcompress); tj3Free(dst_buf); - if (src_buf) free(src_buf); printf("Encode Successful!\n"); } @@ -554,7 +508,6 @@ void ppm_to_jpeg(PPM* in, JPEG* out) { unsigned int* src_buf = ppm_decode(in->name, in); - // int jpeg_size = jpeg->width * jpeg->height; out->width = in->width; out->height = in->height; jpeg_encode(src_buf, out, out->name); @@ -573,17 +526,24 @@ jpeg_to_ppm(JPEG* input, PPM* out) int main() { - PPM ppm = { .width = 3, - .height = 2, - .name = "../assets/8ktest.ppm", - .len = 0, - .fmt = "P6" }; - JPEG jpeg = { .width = 3, - .height = 2, - .len = 0, - .name = "../test.jpg" }; + JPEG jpeg = { + .width = 3, + .height = 2, + .len = 0, + .name = + "/Users/andrew/personal/Projects/C/image_processor/assets/wp12450035.jpg" + }; - // jpeg_to_ppm(&jpeg, &ppm); - ppm_to_jpeg(&ppm, &jpeg); + unsigned int* src_buf = jpeg_decode(&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; }