made masks optional

This commit is contained in:
antifallobst 2023-03-21 13:41:34 +01:00
parent 027c88dd7c
commit 72785e2ac7
1 changed files with 21 additions and 25 deletions

View File

@ -50,7 +50,13 @@ 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);
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->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(distance_T)); image->dist_buffer = calloc(image->width * image->height, sizeof(distance_T));
image->new_width = image->width; image->new_width = image->width;
@ -61,7 +67,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); if (image->mask_buffer != NULL) img_free_pixels(image->mask_buffer);
free(image->dist_buffer); free(image->dist_buffer);
free(image); free(image);
} }
@ -69,7 +75,10 @@ void image_destruct(image_T* image) {
void image_calculate_dist_buffer(image_T* image) { void image_calculate_dist_buffer(image_T* image) {
memset(image->dist_buffer, 0, image->width * sizeof(distance_T)); memset(image->dist_buffer, 0, image->width * sizeof(distance_T));
uint32_t* mask_row = &image->mask_buffer[image->width]; uint32_t* mask_row = NULL;
if (image->mask_buffer != NULL) {
mask_row = &image->mask_buffer[image->width];
}
uint32_t* last_row = image->buffer; uint32_t* last_row = image->buffer;
uint32_t* pixel_row = &last_row[image->width]; uint32_t* pixel_row = &last_row[image->width];
distance_T* dist_last_row = image->dist_buffer; distance_T* dist_last_row = image->dist_buffer;
@ -77,21 +86,6 @@ void image_calculate_dist_buffer(image_T* image) {
for (int y = 1; y < image->new_height; y++) { for (int y = 1; y < image->new_height; y++) {
for (int x = 0; x < image->new_width; x++) { for (int x = 0; x < image->new_width; x++) {
// uint32_t pixel = pixel_row[x];
//
// distance_T shortest_dist;
// bool found_dist = false;
// for (int i = (x > 0 ? -1 : 0); i < (x < image->new_width-1 ? 2 : 1); i++) {
// int32_t dist = 0;
// dist += pixel_distance(pixel, last_row[x + i], image->mask_buffer[(y * image->width) + x + i]);
// dist += dist_last_row[x + i].distance;
//
// if (!found_dist || shortest_dist.distance > dist) {
// shortest_dist = (distance_T){dist, (int8_t)i};
// found_dist = true;
// }
// }
int32_t dist; int32_t dist;
distance_T shortest_dist; distance_T shortest_dist;
@ -99,15 +93,15 @@ void image_calculate_dist_buffer(image_T* image) {
if (x > 0) { if (x > 0) {
dist = 0; dist = 0;
dist += dist_last_row[x - 1].distance; dist += dist_last_row[x - 1].distance;
dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], mask_row[x]); dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], (image->mask_buffer != NULL ? mask_row[x] : 0));
dist += pixel_distance(last_row [x], pixel_row[x - 1], mask_row[x]); dist += pixel_distance(last_row [x], pixel_row[x - 1], (image->mask_buffer != NULL ? mask_row[x] : 0));
shortest_dist = (distance_T) {dist, -1}; shortest_dist = (distance_T) {dist, -1};
} }
dist = 0; dist = 0;
dist += dist_last_row[x].distance; dist += dist_last_row[x].distance;
dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], mask_row[x]) * 2; dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], (image->mask_buffer != NULL ? mask_row[x] : 0)) * 2;
if (x == 0 || shortest_dist.distance > dist) { if (x == 0 || shortest_dist.distance > dist) {
shortest_dist = (distance_T) {dist, 0}; shortest_dist = (distance_T) {dist, 0};
@ -116,8 +110,8 @@ void image_calculate_dist_buffer(image_T* image) {
if (x < image->new_width-1) { if (x < image->new_width-1) {
dist = 0; dist = 0;
dist += dist_last_row[x + 1].distance; dist += dist_last_row[x + 1].distance;
dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], mask_row[x]); dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], (image->mask_buffer != NULL ? mask_row[x] : 0));
dist += pixel_distance(last_row [x], pixel_row[x + 1], mask_row[x]); dist += pixel_distance(last_row [x], pixel_row[x + 1], (image->mask_buffer != NULL ? mask_row[x] : 0));
if (shortest_dist.distance > dist) { if (shortest_dist.distance > dist) {
shortest_dist = (distance_T) {dist, 1}; shortest_dist = (distance_T) {dist, 1};
@ -127,7 +121,9 @@ void image_calculate_dist_buffer(image_T* image) {
dist_pixel_row[x] = shortest_dist; dist_pixel_row[x] = shortest_dist;
} }
mask_row = &mask_row[image->width]; if (image->mask_buffer != NULL) {
mask_row = &mask_row[image->width];
}
last_row = pixel_row; last_row = pixel_row;
pixel_row = &pixel_row[image->width]; pixel_row = &pixel_row[image->width];
dist_last_row = dist_pixel_row; dist_last_row = dist_pixel_row;
@ -159,7 +155,7 @@ void image_cut_line(image_T* image, uint32_t line_id) {
distance_T dist = image->dist_buffer[(image->width * y) + x]; distance_T dist = image->dist_buffer[(image->width * y) + x];
x += dist.direction; x += dist.direction;
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)); 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_color(0, 0xFF, 0);
gfx_point(x, y); gfx_point(x, y);
} }