From: johncoswell Date: Thu, 1 Mar 2007 03:58:45 +0000 (+0000) Subject: Fix massive point queueing and tolerance issue X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=dd1787f88bfff2218bd14957449b992a5846ba17;p=inkscape.git Fix massive point queueing and tolerance issue --- diff --git a/src/flood-context.cpp b/src/flood-context.cpp index 611c46646..da8f22513 100644 --- a/src/flood-context.cpp +++ b/src/flood-context.cpp @@ -261,21 +261,23 @@ inline unsigned char * get_pixel(guchar *px, int x, int y, int width) { } static bool compare_pixels(unsigned char *a, unsigned char *b, int tolerance) { + int diff = 0; for (int i = 0; i < 4; i++) { - if (abs(a[i] - b[i]) > tolerance) { - return false; - } + diff += (int)abs(a[i] - b[i]); } - return true; + return ((diff / 4) <= tolerance); } -static bool try_add_to_queue(std::queue *fill_queue, guchar *px, unsigned char *orig, int x, int y, int width, int tolerance, bool fill_switch) { +static bool try_add_to_queue(std::queue *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig, int x, int y, int width, int tolerance, bool fill_switch) { unsigned char *t = get_pixel(px, x, y, width); if (compare_pixels(t, orig, tolerance)) { - if (fill_switch) { - fill_queue->push(NR::Point(x, y)); - return false; + unsigned char *trace_t = get_pixel(trace_px, x, y, width); + if (trace_t[3] != 255) { + if (fill_switch) { + fill_queue->push(NR::Point(x, y)); + } } + return false; } return true; } @@ -468,12 +470,12 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even bool bottom_fill = true; if (y > 0) { - top_fill = try_add_to_queue(&fill_queue, px, orig_color, x, y - 1, width, tolerance, top_fill); + top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, x, y - 1, width, tolerance, top_fill); } else { aborted = true; break; } if (y < y_limit) { - bottom_fill = try_add_to_queue(&fill_queue, px, orig_color, x, y + 1, width, tolerance, bottom_fill); + bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, x, y + 1, width, tolerance, bottom_fill); } else { aborted = true; break; } @@ -484,8 +486,6 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even unsigned char *t, *trace_t; bool ok = false; - trace_t = get_pixel(trace_px, left, y, width); - do { ok = false; // go left @@ -493,9 +493,10 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even t = get_pixel(px, left, y, width); if (compare_pixels(t, orig_color, tolerance)) { for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; } - trace_t[3] = 255; trace_t -= 4; - if (y > 0) { top_fill = try_add_to_queue(&fill_queue, px, orig_color, left, y - 1, width, tolerance, top_fill); } - if (y < y_limit) { bottom_fill = try_add_to_queue(&fill_queue, px, orig_color, left, y + 1, width, tolerance, bottom_fill); } + trace_t = get_pixel(trace_px, left, y, width); + trace_t[3] = 255; + if (y > 0) { top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, left, y - 1, width, tolerance, top_fill); } + if (y < y_limit) { bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, left, y + 1, width, tolerance, bottom_fill); } left--; ok = true; } } else { @@ -505,7 +506,6 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even top_fill = default_top_fill; bottom_fill = default_bottom_fill; - trace_t = get_pixel(trace_px, right, y, width); do { ok = false; @@ -514,9 +514,10 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even t = get_pixel(px, right, y, width); if (compare_pixels(t, orig_color, tolerance)) { for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; } - trace_t[3] = 255; trace_t += 4; - if (y > 0) { top_fill = try_add_to_queue(&fill_queue, px, orig_color, right, y - 1, width, tolerance, top_fill); } - if (y < y_limit) { bottom_fill = try_add_to_queue(&fill_queue, px, orig_color, right, y + 1, width, tolerance, bottom_fill); } + trace_t = get_pixel(trace_px, right, y, width); + trace_t[3] = 255; + if (y > 0) { top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, right, y - 1, width, tolerance, top_fill); } + if (y < y_limit) { bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, right, y + 1, width, tolerance, bottom_fill); } right++; ok = true; } } else {