diff options
| author | John Bintz <me@johnbintz.com> | 2007-03-01 03:58:45 +0000 |
|---|---|---|
| committer | johncoswell <johncoswell@users.sourceforge.net> | 2007-03-01 03:58:45 +0000 |
| commit | 7d4019afc7a09e7347200f3d013b4aa590c973c3 (patch) | |
| tree | fb91d0a15115040a17812b98c4d0f36a3c30769e /src/flood-context.cpp | |
| parent | Optimize flood algorithm to only scan adjacent runs once (diff) | |
| download | inkscape-7d4019afc7a09e7347200f3d013b4aa590c973c3.tar.gz inkscape-7d4019afc7a09e7347200f3d013b4aa590c973c3.zip | |
Fix massive point queueing and tolerance issue
(bzr r2469)
Diffstat (limited to 'src/flood-context.cpp')
| -rw-r--r-- | src/flood-context.cpp | 39 |
1 files changed, 20 insertions, 19 deletions
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<NR::Point> *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<NR::Point> *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 { |
