implemented basic masking

This commit is contained in:
antifallobst 2023-03-21 00:38:33 +01:00
parent 98d5d58558
commit ec79d1e09a
2 changed files with 19 additions and 8 deletions

27
main.c
View File

@ -9,6 +9,8 @@
#define DEBUG_BUILD 0 #define DEBUG_BUILD 0
#define MASK_VALUE 50000
typedef struct { typedef struct {
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
@ -16,9 +18,10 @@ typedef struct {
uint32_t new_height; uint32_t new_height;
uint32_t* buffer; uint32_t* buffer;
uint32_t* dist_buffer; uint32_t* dist_buffer;
uint32_t* mask_buffer;
} image_T; } image_T;
uint32_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b) { uint32_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m) {
uint8_t r_a = (pixel_a) & 0xFF; uint8_t r_a = (pixel_a) & 0xFF;
uint8_t g_a = (pixel_a >> 8) & 0xFF; uint8_t g_a = (pixel_a >> 8) & 0xFF;
uint8_t b_a = (pixel_a >> 16) & 0xFF; uint8_t b_a = (pixel_a >> 16) & 0xFF;
@ -27,10 +30,14 @@ uint32_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b) {
uint8_t g_b = (pixel_b >> 8) & 0xFF; uint8_t g_b = (pixel_b >> 8) & 0xFF;
uint8_t b_b = (pixel_b >> 16) & 0xFF; uint8_t b_b = (pixel_b >> 16) & 0xFF;
uint8_t r_m = (pixel_m) & 0xFF;
uint32_t dist = (uint32_t)sqrt(pow(r_a - r_b, 2) + uint32_t dist = (uint32_t)sqrt(pow(r_a - r_b, 2) +
pow(g_a - g_b, 2) + pow(g_a - g_b, 2) +
pow(b_a - b_b, 2)); pow(b_a - b_b, 2));
if (r_m > 0) { dist += MASK_VALUE; }
#if DEBUG_BUILD #if DEBUG_BUILD
printf(" %x %x %x --> %x %x %x = %d\n", r_a, g_a, b_a, r_b, g_b, b_b, dist); printf(" %x %x %x --> %x %x %x = %d\n", r_a, g_a, b_a, r_b, g_b, b_b, dist);
#endif #endif
@ -38,10 +45,11 @@ uint32_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b) {
return dist; return dist;
} }
image_T* image_load(const char* filename) { image_T* image_load(const char* filename, const char* filename_mask) {
printf("loading '%s'...\n", filename); printf("loading '%s'...\n", filename);
image_T* image = malloc(sizeof(image_T)); image_T* image = malloc(sizeof(image_T));
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->buffer = img_load_pixels(filename, (int*)&image->width, (int*)&image->height, IMG_FMT_RGBA32);
image->dist_buffer = calloc(image->width * image->height, sizeof(uint32_t)); image->dist_buffer = calloc(image->width * image->height, sizeof(uint32_t));
image->new_width = image->width; image->new_width = image->width;
@ -52,6 +60,7 @@ void image_destruct(image_T* image) {
printf("destructing resources\n"); printf("destructing resources\n");
img_free_pixels(image->buffer); img_free_pixels(image->buffer);
img_free_pixels(image->mask_buffer);
free(image->dist_buffer); free(image->dist_buffer);
free(image); free(image);
} }
@ -77,7 +86,7 @@ void image_calculate_dist_buffer(image_T* image) {
int32_t shortest_dist = -1; int32_t shortest_dist = -1;
for (int i = (x > 0 ? -1 : 0); i < (x < image->new_width-1 ? 2 : 1); i++) { for (int i = (x > 0 ? -1 : 0); i < (x < image->new_width-1 ? 2 : 1); i++) {
uint32_t dist = pixel_distance(pixel, last_row[x + i]) + dist_last_row[x + i]; uint32_t dist = pixel_distance(pixel, last_row[x + i], image->mask_buffer[(y * image->width) + x + i]) + dist_last_row[x + i];
if (shortest_dist == -1 || shortest_dist > dist) { if (shortest_dist == -1 || shortest_dist > dist) {
shortest_dist = (int32_t)dist; shortest_dist = (int32_t)dist;
@ -110,7 +119,6 @@ uint32_t image_find_best_line(image_T* image) {
if (shortest_dist == -1 || shortest_dist > row[x]) { if (shortest_dist == -1 || shortest_dist > row[x]) {
shortest_dist = (int32_t)row[x]; shortest_dist = (int32_t)row[x];
line_id = x; line_id = x;
printf("id: %d dist: %d\n", x, row[x]);
} }
} }
@ -133,11 +141,13 @@ void image_cut_line(image_T* image, uint32_t line_id) {
} }
x += offset; x += offset;
memcpy(&image->buffer[(image->width * y) + x], &image->buffer[(image->width * y) + x + 1], (image->new_width - x) * sizeof(uint32_t)); memcpy(&image->buffer[(image->width * y) + x], &image->buffer[(image->width * y) + x + 1], (image->new_width - x) * sizeof(uint32_t));
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_color(0, 0xFF, 0);
gfx_point(x, y); gfx_point(x, y);
gfx_flush();
} }
image->new_width -= 1; image->new_width -= 1;
gfx_flush();
} }
void image_dump(image_T* image) { void image_dump(image_T* image) {
@ -166,7 +176,7 @@ void image_dump(image_T* image) {
} }
gfx_color(0, 0, 0); gfx_color(0, 0, 0);
for (int y = 0; y < image->new_height; y++) { for (int y = 0; y < image->new_height*2; y++) {
gfx_point(image->new_width, y); gfx_point(image->new_width, y);
} }
@ -196,14 +206,15 @@ void image_dump(image_T* image) {
int main() { int main() {
printf("Line Cutter - Juniorcamp Informatik\n"); printf("Line Cutter - Juniorcamp Informatik\n");
image_T* image = image_load("../paris.png"); image_T* image = image_load("../paris.png", "../paris_mask.png");
gfx_open(800, 600, "Juniorcamp Informatik - Line Cutter"); gfx_open(800, 600, "Juniorcamp Informatik - Line Cutter");
image_dump(image);
bool running = true; bool running = true;
while(running) { while(running) {
image_dump(image);
char c = gfx_wait(); char c = gfx_wait();
image_dump(image);
switch (c) { switch (c) {
case 's': { case 's': {
image_calculate_dist_buffer(image); image_calculate_dist_buffer(image);

BIN
paris_mask.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB