From: johncoswell Date: Tue, 27 Feb 2007 23:28:03 +0000 (+0000) Subject: Add paint bucket tolerance setting and toolbar widget to control tolerance X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=2fa60d63644e485374b2f2a1230ab6054e69cce3;p=inkscape.git Add paint bucket tolerance setting and toolbar widget to control tolerance --- diff --git a/src/flood-context.cpp b/src/flood-context.cpp index fa547a6b9..e737b90f9 100644 --- a/src/flood-context.cpp +++ b/src/flood-context.cpp @@ -260,18 +260,18 @@ inline unsigned char * get_pixel(guchar *px, int x, int y, int width) { return px + (x + y * width) * 4; } -static bool compare_pixels(unsigned char *a, unsigned char *b, int fuzziness) { +static bool compare_pixels(unsigned char *a, unsigned char *b, int tolerance) { for (int i = 0; i < 4; i++) { - if (a[i] != b[i]) { + if (abs(a[i] - b[i]) > tolerance) { return false; } } return true; } -static void try_add_to_queue(std::queue *fill_queue, guchar *px, unsigned char *orig, int x, int y, int width) { +static void try_add_to_queue(std::queue *fill_queue, guchar *px, unsigned char *orig, int x, int y, int width, int tolerance) { unsigned char *t = get_pixel(px, x, y, width); - if (compare_pixels(t, orig, 0)) { + if (compare_pixels(t, orig, tolerance)) { fill_queue->push(NR::Point(x, y)); } } @@ -413,7 +413,6 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even nr_arena_item_unref(root); nr_object_unref((NRObject *) arena); - NR::Point pw = NR::Point(event->button.x / zoom_scale, sp_document_height(document) + (event->button.y / zoom_scale)) * affine; pw[NR::X] = (int)MIN(width - 1, MAX(0, pw[NR::X])); @@ -432,25 +431,27 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even unsigned char *orig_px = get_pixel(px, (int)pw[NR::X], (int)pw[NR::Y], width); for (int i = 0; i < 4; i++) { orig_color[i] = orig_px[i]; } + int tolerance = prefs_get_int_attribute_limited("tools.paintbucket", "tolerance", 1, 0, 255); + while (!fill_queue.empty() && !aborted) { NR::Point cp = fill_queue.front(); fill_queue.pop(); unsigned char *s = get_pixel(px, (int)cp[NR::X], (int)cp[NR::Y], width); // same color at this point - if (compare_pixels(s, orig_color, 0)) { + if (compare_pixels(s, orig_color, tolerance)) { int left = (int)cp[NR::X]; int right = (int)cp[NR::X] + 1; int x = (int)cp[NR::X]; int y = (int)cp[NR::Y]; if (y > 0) { - try_add_to_queue(&fill_queue, px, orig_color, x, y - 1, width); + try_add_to_queue(&fill_queue, px, orig_color, x, y - 1, width, tolerance); } else { aborted = true; break; } if (y < y_limit) { - try_add_to_queue(&fill_queue, px, orig_color, x, y + 1, width); + try_add_to_queue(&fill_queue, px, orig_color, x, y + 1, width, tolerance); } else { aborted = true; break; } @@ -463,12 +464,12 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even // go left if (left >= 0) { t = get_pixel(px, left, y, width); - if (compare_pixels(t, orig_color, 0)) { + if (compare_pixels(t, orig_color, tolerance)) { for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; } trace_t = get_pixel(trace_px, left, y, width); trace_t[3] = 255; - if (y > 0) { try_add_to_queue(&fill_queue, px, orig_color, left, y - 1, width); } - if (y < y_limit) { try_add_to_queue(&fill_queue, px, orig_color, left, y + 1, width); } + if (y > 0) { try_add_to_queue(&fill_queue, px, orig_color, left, y - 1, width, tolerance); } + if (y < y_limit) { try_add_to_queue(&fill_queue, px, orig_color, left, y + 1, width, tolerance); } left--; ok = true; } } else { @@ -478,15 +479,15 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even do { ok = false; - // go left + // go right if (right < width) { t = get_pixel(px, right, y, width); - if (compare_pixels(t, orig_color, 0)) { + if (compare_pixels(t, orig_color, tolerance)) { for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; } trace_t = get_pixel(trace_px, right, y, width); trace_t[3] = 255; - if (y > 0) { try_add_to_queue(&fill_queue, px, orig_color, right, y - 1, width); } - if (y < y_limit) { try_add_to_queue(&fill_queue, px, orig_color, right, y + 1, width); } + if (y > 0) { try_add_to_queue(&fill_queue, px, orig_color, right, y - 1, width, tolerance); } + if (y < y_limit) { try_add_to_queue(&fill_queue, px, orig_color, right, y + 1, width, tolerance); } right++; ok = true; } } else { diff --git a/src/widgets/toolbox.cpp b/src/widgets/toolbox.cpp index 43ebb6b5d..788176d9c 100644 --- a/src/widgets/toolbox.cpp +++ b/src/widgets/toolbox.cpp @@ -4432,11 +4432,29 @@ sp_connector_toolbox_new(SPDesktop *desktop) } // end of sp_connector_toolbox_new() +static void paintbucket_tolerance_changed(GtkAdjustment *adj, GtkWidget *tbl) +{ + prefs_set_int_attribute("tools.paintbucket", "tolerance", (gint)adj->value); + spinbutton_defocus(GTK_OBJECT(tbl)); +} + static GtkWidget * sp_paintbucket_toolbox_new(SPDesktop *desktop) { GtkWidget *tbl = gtk_hbox_new(FALSE, 0); + // Spacing spinbox + { + GtkWidget *tolerance = sp_tb_spinbutton(_("Tolerance:"), + _("The maximum allowed difference between the clicked pixel and the neighboring pixels to be counted in the fill"), + "tools.paintbucket", "tolerance", 8, NULL, tbl, TRUE, + "inkscape:paintbucket-tolerance", 0, 255, 1.0, 10.0, + paintbucket_tolerance_changed, 1, 0); + + gtk_box_pack_start(GTK_BOX(tbl), tolerance, FALSE, FALSE, + AUX_SPACING); + } + Inkscape::UI::Widget::StyleSwatch *swatch = new Inkscape::UI::Widget::StyleSwatch(NULL, _("Style of Paint Bucket fill objects")); swatch->setDesktop (desktop); swatch->setClickVerb (SP_VERB_CONTEXT_PAINTBUCKET_PREFS);