implemented the forward energy improvement

This commit is contained in:
antifallobst 2023-03-21 12:39:30 +01:00
parent 701fccdf65
commit 5d98c7e88e
2 changed files with 49 additions and 11 deletions

56
main.c
View File

@ -68,6 +68,7 @@ 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* 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;
@ -75,22 +76,57 @@ 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]; // 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;
distance_T shortest_dist; 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 = pixel_distance(pixel, last_row[x + i], image->mask_buffer[(y * image->width) + x + i]) + dist_last_row[x + i].distance;
if (!found_dist || shortest_dist.distance > dist) { if (x > 0) {
shortest_dist = (distance_T){dist, (int8_t)i}; dist = 0;
found_dist = true; dist += dist_last_row[x - 1].distance;
dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], mask_row[x]);
dist += pixel_distance(last_row [x], pixel_row[x - 1], mask_row[x]);
shortest_dist = (distance_T) {dist, -1};
}
dist = -10;
dist += dist_last_row[x].distance;
dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], mask_row[x]);
if (x == 0 || shortest_dist.distance > dist) {
shortest_dist = (distance_T) {dist, 0};
}
if (x < image->new_width-1) {
dist = 0;
dist += dist_last_row[x + 1].distance;
dist += pixel_distance(pixel_row[x - 1], pixel_row[x + 1], mask_row[x]);
dist += pixel_distance(last_row [x], pixel_row[x + 1], mask_row[x]);
if (shortest_dist.distance > dist) {
shortest_dist = (distance_T) {dist, 1};
} }
} }
dist_pixel_row[x] = shortest_dist; dist_pixel_row[x] = shortest_dist;
} }
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;
@ -118,7 +154,7 @@ uint32_t image_find_best_line(image_T* image) {
void image_cut_line(image_T* image, uint32_t line_id) { void image_cut_line(image_T* image, uint32_t line_id) {
uint32_t x = line_id; uint32_t x = line_id;
for (uint32_t y = image->height-1; y > 0; y--) { for (uint32_t y = image->new_height-1; y > 0; y--) {
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));
@ -156,7 +192,7 @@ void image_dump(image_T* image) {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
printf("Line Cutter - Juniorcamp Informatik\n"); printf("Line Cutter - Juniorcamp Informatik\n");
image_T* image = image_load("../bench.png", "../bench_mask.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); image_dump(image);
@ -167,8 +203,10 @@ int main(int argc, char* argv[]) {
image_dump(image); image_dump(image);
switch (c) { switch (c) {
case 's': { case 's': {
if (image->new_width > 0) {
image_calculate_dist_buffer(image); image_calculate_dist_buffer(image);
image_cut_line(image, image_find_best_line(image)); image_cut_line(image, image_find_best_line(image));
}
break; break;
} }
case 'q': { case 'q': {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.6 KiB

After

Width:  |  Height:  |  Size: 7.3 KiB