diff --git a/CMakeLists.txt b/CMakeLists.txt index b510fbe..b43c513 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(Juniorcamp C) set(CMAKE_C_STANDARD 99) -add_executable(Juniorcamp src/main.c src/gfx.c) +add_executable(Juniorcamp src/main.c src/distance.c src/gfx.c) target_link_libraries(Juniorcamp imago m X11) target_include_directories(Juniorcamp PRIVATE inc) diff --git a/images/ratatouille.jpg b/images/ratatouille.jpg new file mode 100644 index 0000000..92f724d Binary files /dev/null and b/images/ratatouille.jpg differ diff --git a/images/ratatouille_mask.jpg b/images/ratatouille_mask.jpg new file mode 100644 index 0000000..4187bd4 Binary files /dev/null and b/images/ratatouille_mask.jpg differ diff --git a/inc/distance.h b/inc/distance.h new file mode 100644 index 0000000..cc19245 --- /dev/null +++ b/inc/distance.h @@ -0,0 +1,15 @@ +#ifndef JUNIORCAMP_DISTANCE_H +#define JUNIORCAMP_DISTANCE_H + +#include + +#define MASK_MULTIPLIER 500 + +typedef struct { + int64_t distance; + int8_t direction; +} distance_T; + +int64_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m); + +#endif //JUNIORCAMP_DISTANCE_H diff --git a/src/distance.c b/src/distance.c new file mode 100644 index 0000000..820a49e --- /dev/null +++ b/src/distance.c @@ -0,0 +1,23 @@ +#include "distance.h" +#include + +int64_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m) { + uint8_t r_a = (pixel_a) & 0xFF; + uint8_t g_a = (pixel_a >> 8) & 0xFF; + uint8_t b_a = (pixel_a >> 16) & 0xFF; + + uint8_t r_b = (pixel_b) & 0xFF; + uint8_t g_b = (pixel_b >> 8) & 0xFF; + uint8_t b_b = (pixel_b >> 16) & 0xFF; + + uint8_t r_m = (pixel_m) & 0xFF; + uint8_t g_m = (pixel_m >> 8) & 0xFF; + + int64_t dist = (int64_t)sqrt(pow(r_a - r_b, 2) + + pow(g_a - g_b, 2) + + pow(b_a - b_b, 2)) + + (r_m * MASK_MULTIPLIER) - + (g_m * MASK_MULTIPLIER); + + return dist; +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 423f73a..1703957 100644 --- a/src/main.c +++ b/src/main.c @@ -2,18 +2,11 @@ #include #include #include -#include #include #include #include #include "gfx.h" - -#define MASK_MULTIPLIER 50 - -typedef struct { - int32_t distance; - int8_t direction; -} distance_T; +#include "distance.h" typedef struct { uint32_t width; @@ -25,38 +18,12 @@ typedef struct { uint32_t* mask_buffer; } image_T; -int32_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m) { - uint8_t r_a = (pixel_a) & 0xFF; - uint8_t g_a = (pixel_a >> 8) & 0xFF; - uint8_t b_a = (pixel_a >> 16) & 0xFF; - - uint8_t r_b = (pixel_b) & 0xFF; - uint8_t g_b = (pixel_b >> 8) & 0xFF; - uint8_t b_b = (pixel_b >> 16) & 0xFF; - - uint8_t r_m = (pixel_m) & 0xFF; - uint8_t g_m = (pixel_m >> 8) & 0xFF; - - int32_t dist = (int32_t)sqrt(pow(r_a - r_b, 2) + - pow(g_a - g_b, 2) + - pow(b_a - b_b, 2)) + - (r_m * MASK_MULTIPLIER) - - (g_m * MASK_MULTIPLIER); - - return dist; -} image_T* image_load(const char* filename, const char* filename_mask) { printf("loading '%s'...\n", filename); image_T* image = malloc(sizeof(image_T)); - - if (filename_mask != NULL) { - image->mask_buffer = img_load_pixels(filename_mask, (int*)&image->width, (int*)&image->height, IMG_FMT_RGBA32); - } else { - image->mask_buffer = NULL; - } - + image->mask_buffer = img_load_pixels(filename_mask, (int*)&image->width, (int*)&image->height, IMG_FMT_RGBA32); image->buffer = img_load_pixels(filename, (int*)&image->width, (int*)&image->height, IMG_FMT_RGBA32); image->dist_buffer = calloc(image->width * image->height, sizeof(distance_T)); image->new_width = image->width; @@ -87,7 +54,7 @@ void image_calculate_dist_buffer(image_T* image) { for (int y = 1; y < image->new_height; y++) { for (int x = 0; x < image->new_width; x++) { - int32_t dist; + int64_t dist; distance_T shortest_dist; if (x > 0) { @@ -157,7 +124,7 @@ void image_cut_line(image_T* image, uint32_t line_id) { memcpy(&image->buffer[(image->width * y) + x], &image->buffer[(image->width * y) + x + 1], (image->new_width - x) * sizeof(uint32_t)); if (image->mask_buffer != NULL) memcpy(&image->mask_buffer[(image->width * y) + x], &image->mask_buffer[(image->width * y) + x + 1], (image->new_width - x) * sizeof(uint32_t)); gfx_color(0, 0xFF, 0); - gfx_point(x, y); + gfx_point((int)x, (int)y); } image->new_width -= 1; @@ -178,7 +145,7 @@ void image_dump(image_T* image) { gfx_color(0, 0, 0); for (int y = 0; y < image->new_height; y++) { - gfx_point(image->new_width, y); + gfx_point((int)image->new_width, y); } gfx_flush(); @@ -188,7 +155,7 @@ void image_export(image_T* image, const char* filename) { printf("exporting image to '%s'\n", filename); struct img_pixmap img; img_init(&img); - img_set_pixels(&img, image->new_width, image->new_height, IMG_FMT_RGBA32, NULL); + img_set_pixels(&img, (int)image->new_width, (int)image->new_height, IMG_FMT_RGBA32, NULL); uint32_t* out_buffer = (uint32_t*)img.pixels; @@ -200,7 +167,7 @@ void image_export(image_T* image, const char* filename) { } -int main(int argc, char* argv[]) { +int main() { printf("Line Cutter - Juniorcamp Informatik\n"); image_T* image = image_load("../images/paris.png", "../images/paris_mask.png"); @@ -235,6 +202,9 @@ int main(int argc, char* argv[]) { running = false; break; } + default: { + break; + } } }