implemented negative masking
This commit is contained in:
parent
ec79d1e09a
commit
9e4b53b74f
80
main.c
80
main.c
|
@ -9,7 +9,7 @@
|
|||
|
||||
#define DEBUG_BUILD 0
|
||||
|
||||
#define MASK_VALUE 50000
|
||||
#define MASK_MULTIPLIER 50
|
||||
|
||||
typedef struct {
|
||||
uint32_t width;
|
||||
|
@ -17,11 +17,11 @@ typedef struct {
|
|||
uint32_t new_width;
|
||||
uint32_t new_height;
|
||||
uint32_t* buffer;
|
||||
uint32_t* dist_buffer;
|
||||
int32_t* dist_buffer;
|
||||
uint32_t* mask_buffer;
|
||||
} image_T;
|
||||
|
||||
uint32_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m) {
|
||||
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;
|
||||
|
@ -31,16 +31,13 @@ uint32_t pixel_distance(uint32_t pixel_a, uint32_t pixel_b, uint32_t pixel_m) {
|
|||
uint8_t b_b = (pixel_b >> 16) & 0xFF;
|
||||
|
||||
uint8_t r_m = (pixel_m) & 0xFF;
|
||||
uint8_t g_m = (pixel_m >> 8) & 0xFF;
|
||||
|
||||
uint32_t dist = (uint32_t)sqrt(pow(r_a - r_b, 2) +
|
||||
int32_t dist = (int32_t)sqrt(pow(r_a - r_b, 2) +
|
||||
pow(g_a - g_b, 2) +
|
||||
pow(b_a - b_b, 2));
|
||||
|
||||
if (r_m > 0) { dist += MASK_VALUE; }
|
||||
|
||||
#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);
|
||||
#endif
|
||||
pow(b_a - b_b, 2)) +
|
||||
(r_m * MASK_MULTIPLIER) -
|
||||
(g_m * MASK_MULTIPLIER);
|
||||
|
||||
return dist;
|
||||
}
|
||||
|
@ -66,39 +63,27 @@ void image_destruct(image_T* image) {
|
|||
}
|
||||
|
||||
void image_calculate_dist_buffer(image_T* image) {
|
||||
memset(image->dist_buffer, 0, image->height * image->width * sizeof(uint32_t));
|
||||
memset(image->dist_buffer, 0, image->height * image->width * sizeof(int32_t));
|
||||
|
||||
uint32_t* last_row = image->buffer;
|
||||
uint32_t* pixel_row = &last_row[image->width];
|
||||
uint32_t* dist_last_row = image->dist_buffer;
|
||||
uint32_t* dist_pixel_row = &dist_last_row[image->width];
|
||||
int32_t* dist_last_row = image->dist_buffer;
|
||||
int32_t* dist_pixel_row = &dist_last_row[image->width];
|
||||
|
||||
for (int y = 1; y < image->new_height; y++) {
|
||||
#if DEBUG_BUILD
|
||||
printf(" Y\n");
|
||||
#endif
|
||||
for (int x = 0; x < image->new_width; x++) {
|
||||
uint32_t pixel = pixel_row[x];
|
||||
|
||||
#if DEBUG_BUILD
|
||||
printf(" X\n");
|
||||
#endif
|
||||
|
||||
int32_t shortest_dist = -1;
|
||||
int32_t shortest_dist;
|
||||
bool found_dist = false;
|
||||
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], image->mask_buffer[(y * image->width) + x + i]) + dist_last_row[x + i];
|
||||
int32_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) {
|
||||
shortest_dist = (int32_t)dist;
|
||||
if (!found_dist || shortest_dist > dist) {
|
||||
shortest_dist = dist;
|
||||
found_dist = true;
|
||||
}
|
||||
|
||||
#if DEBUG_BUILD
|
||||
printf(" 0x%x - %d\n", dist, x+i);
|
||||
#endif
|
||||
}
|
||||
#if DEBUG_BUILD
|
||||
printf(" 0x%x\n", shortest_dist);
|
||||
#endif
|
||||
|
||||
dist_pixel_row[x] = shortest_dist;
|
||||
}
|
||||
|
@ -111,14 +96,16 @@ void image_calculate_dist_buffer(image_T* image) {
|
|||
}
|
||||
|
||||
uint32_t image_find_best_line(image_T* image) {
|
||||
uint32_t* row = &image->dist_buffer[(image->new_height-1) * image->width];
|
||||
int32_t* row = &image->dist_buffer[(image->new_height-1) * image->width];
|
||||
uint32_t line_id = 0;
|
||||
int32_t shortest_dist = -1;
|
||||
int32_t shortest_dist;
|
||||
bool found_dist = false;
|
||||
|
||||
for (uint32_t x = 0; x < image->new_width; x++) {
|
||||
if (shortest_dist == -1 || shortest_dist > row[x]) {
|
||||
shortest_dist = (int32_t)row[x];
|
||||
if (!found_dist || shortest_dist > row[x]) {
|
||||
shortest_dist = row[x];
|
||||
line_id = x;
|
||||
found_dist = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -129,14 +116,16 @@ void image_cut_line(image_T* image, uint32_t line_id) {
|
|||
uint32_t x = line_id;
|
||||
|
||||
for (uint32_t y = image->height-1; y > 0; y--) {
|
||||
int32_t shortest_dist = -1;
|
||||
int32_t shortest_dist;
|
||||
int8_t offset;
|
||||
bool found_dist = false;
|
||||
for (int i = (x > 0 ? -1 : 0); i < (x < image->new_width-1 ? 2 : 1); i++) {
|
||||
uint32_t dist = image->dist_buffer[(image->width * y) + x + i];
|
||||
int32_t dist = image->dist_buffer[(image->width * y) + x + i];
|
||||
|
||||
if (shortest_dist == -1 || shortest_dist > dist) {
|
||||
shortest_dist = (int32_t)dist;
|
||||
offset = i;
|
||||
if (!found_dist || shortest_dist > dist) {
|
||||
shortest_dist = dist;
|
||||
offset = (int8_t)i;
|
||||
found_dist = true;
|
||||
}
|
||||
}
|
||||
x += offset;
|
||||
|
@ -184,23 +173,16 @@ void image_dump(image_T* image) {
|
|||
|
||||
#if DEBUG_BUILD
|
||||
printf("Distances:\n");
|
||||
#endif
|
||||
for (int y = 0; y < image->height; y++) {
|
||||
uint32_t* dist_row = &image->dist_buffer[y * image->width];
|
||||
|
||||
#if DEBUG_BUILD
|
||||
printf(" ");
|
||||
#endif
|
||||
for (int x = 0; x < image->width; x++) {
|
||||
#if DEBUG_BUILD
|
||||
printf("%x ", dist_row[x]);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if DEBUG_BUILD
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
BIN
paris_mask.png
BIN
paris_mask.png
Binary file not shown.
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 7.1 KiB |
Loading…
Reference in New Issue