X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fflood-context.cpp;h=aa6eab7b36cb7390b76ac8cd697a73a5f333378f;hb=ac63672b8bd0ad9539384d4eac6e27875d4d2693;hp=cd2f1a5ec46f3e9286f236b17309f3846215d0c1;hpb=5efc89d7a5830b387c1362a077c82980c5624174;p=inkscape.git diff --git a/src/flood-context.cpp b/src/flood-context.cpp index cd2f1a5ec..aa6eab7b3 100644 --- a/src/flood-context.cpp +++ b/src/flood-context.cpp @@ -60,11 +60,13 @@ #include "sp-item.h" #include "sp-root.h" #include "sp-defs.h" +#include "sp-path.h" #include "splivarot.h" #include "livarot/Path.h" #include "livarot/Shape.h" #include "libnr/n-art-bpath.h" #include "svg/svg.h" +#include "color.h" #include "trace/trace.h" #include "trace/potrace/inkscape-potrace.h" @@ -237,6 +239,12 @@ inline unsigned char * get_pixel(guchar *px, int x, int y, int width) { enum PaintBucketChannels { FLOOD_CHANNELS_RGB, + FLOOD_CHANNELS_R, + FLOOD_CHANNELS_G, + FLOOD_CHANNELS_B, + FLOOD_CHANNELS_H, + FLOOD_CHANNELS_S, + FLOOD_CHANNELS_L, FLOOD_CHANNELS_ALPHA }; @@ -244,17 +252,37 @@ GList * flood_channels_dropdown_items_list() { GList *glist = NULL; glist = g_list_append (glist, _("Visible Colors")); + glist = g_list_append (glist, _("Red")); + glist = g_list_append (glist, _("Green")); + glist = g_list_append (glist, _("Blue")); + glist = g_list_append (glist, _("Hue")); + glist = g_list_append (glist, _("Saturation")); + glist = g_list_append (glist, _("Lightness")); glist = g_list_append (glist, _("Alpha")); return glist; } -static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *dtc, int tolerance, PaintBucketChannels method) { +static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *dtc, int threshold, PaintBucketChannels method) { int diff = 0; + float hsl_check[3], hsl_orig[3]; + + if ((method == FLOOD_CHANNELS_H) || + (method == FLOOD_CHANNELS_S) || + (method == FLOOD_CHANNELS_L)) { + sp_color_rgb_to_hsl_floatv(hsl_check, check[0] / 255.0, check[1] / 255.0, check[2] / 255.0); + sp_color_rgb_to_hsl_floatv(hsl_orig, orig[0] / 255.0, orig[1] / 255.0, orig[2] / 255.0); + } switch (method) { case FLOOD_CHANNELS_ALPHA: - return ((int)abs(check[3] - orig[3]) <= (tolerance / 4)); + return ((int)abs(check[3] - orig[3]) <= threshold); + case FLOOD_CHANNELS_R: + return ((int)abs(check[0] - orig[0]) <= threshold); + case FLOOD_CHANNELS_G: + return ((int)abs(check[1] - orig[1]) <= threshold); + case FLOOD_CHANNELS_B: + return ((int)abs(check[2] - orig[2]) <= threshold); case FLOOD_CHANNELS_RGB: unsigned char merged_orig[4]; unsigned char merged_check[4]; @@ -265,15 +293,22 @@ static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned c for (int i = 0; i < 3; i++) { diff += (int)abs(merged_check[i] - merged_orig[i]); } - return ((diff / 3) <= ((tolerance * 3) / 4)); + return ((diff / 3) <= ((threshold * 3) / 4)); + + case FLOOD_CHANNELS_H: + return ((int)(fabs(hsl_check[0] - hsl_orig[0]) * 100.0) <= threshold); + case FLOOD_CHANNELS_S: + return ((int)(fabs(hsl_check[1] - hsl_orig[1]) * 100.0) <= threshold); + case FLOOD_CHANNELS_L: + return ((int)(fabs(hsl_check[2] - hsl_orig[2]) * 100.0) <= threshold); } return false; } -static bool try_add_to_queue(std::queue *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig, unsigned char *dtc, int x, int y, int width, int tolerance, PaintBucketChannels method, bool fill_switch) { +static bool try_add_to_queue(std::queue *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig, unsigned char *dtc, int x, int y, int width, int threshold, PaintBucketChannels method, bool fill_switch) { unsigned char *t = get_pixel(px, x, y, width); - if (compare_pixels(t, orig, dtc, tolerance, method)) { + if (compare_pixels(t, orig, dtc, threshold, method)) { unsigned char *trace_t = get_pixel(trace_px, x, y, width); if (trace_t[3] != 255) { if (fill_switch) { @@ -315,33 +350,50 @@ static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) { NArtBpath *bpath = sp_svg_read_path(result.getPathData().c_str()); Path *path = bpath_to_Path(bpath); g_free(bpath); + + if (offset != 0) { - Shape *path_shape = new Shape(); + Shape *path_shape = new Shape(); - path->ConvertWithBackData(0.03); - path->Fill(path_shape, 0); - delete path; + path->ConvertWithBackData(0.03); + path->Fill(path_shape, 0); + delete path; - Shape *expanded_path_shape = new Shape(); + Shape *expanded_path_shape = new Shape(); - expanded_path_shape->ConvertToShape(path_shape, fill_nonZero); - path_shape->MakeOffset(expanded_path_shape, offset, join_round, 4); - expanded_path_shape->ConvertToShape(path_shape, fill_positive); + expanded_path_shape->ConvertToShape(path_shape, fill_nonZero); + path_shape->MakeOffset(expanded_path_shape, offset * desktop->current_zoom(), join_round, 4); + expanded_path_shape->ConvertToShape(path_shape, fill_positive); - Path *expanded_path = new Path(); + Path *expanded_path = new Path(); - expanded_path->Reset(); - expanded_path_shape->ConvertToForme(expanded_path); - expanded_path->ConvertEvenLines(1.0); - expanded_path->Simplify(1.0); + expanded_path->Reset(); + expanded_path_shape->ConvertToForme(expanded_path); + expanded_path->ConvertEvenLines(1.0); + expanded_path->Simplify(1.0); - delete path_shape; - delete expanded_path_shape; + delete path_shape; + delete expanded_path_shape; - gchar *str = expanded_path->svg_dump_path(); - delete expanded_path; - pathRepr->setAttribute("d", str); - g_free(str); + gchar *str = expanded_path->svg_dump_path(); + if (str && *str) { + pathRepr->setAttribute("d", str); + g_free(str); + } else { + desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Too much inset, the result is empty.")); + Inkscape::GC::release(pathRepr); + g_free(str); + return; + } + + delete expanded_path; + + } else { + gchar *str = path->svg_dump_path(); + delete path; + pathRepr->setAttribute("d", str); + g_free(str); + } layer_repr->addChild(pathRepr, NULL); @@ -366,9 +418,12 @@ static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) { Inkscape::Selection *selection = sp_desktop_selection(desktop); selection->set(reprobj); pathRepr->setPosition(-1); + + desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, _("Area filled, path with %d nodes created."), sp_nodes_in_path(SP_PATH(reprobj))); } Inkscape::GC::release(pathRepr); + } } @@ -378,7 +433,7 @@ struct bitmap_coords_info { int y; int y_limit; int width; - int tolerance; + int threshold; PaintBucketChannels method; unsigned char *dtc; bool top_fill; @@ -411,15 +466,15 @@ static ScanlineCheckResult perform_bitmap_scanline_check(std::queue * if (keep_tracing) { t = get_pixel(px, bci.x, bci.y, bci.width); - if (compare_pixels(t, orig_color, bci.dtc, bci.tolerance, bci.method)) { + if (compare_pixels(t, orig_color, bci.dtc, bci.threshold, bci.method)) { for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; } trace_t = get_pixel(trace_px, bci.x, bci.y, bci.width); trace_t[3] = 255; if (bci.y > 0) { - bci.top_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y - 1, bci.width, bci.tolerance, bci.method, bci.top_fill); + bci.top_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y - 1, bci.width, bci.threshold, bci.method, bci.top_fill); } if (bci.y < bci.y_limit) { - bci.bottom_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y + 1, bci.width, bci.tolerance, bci.method, bci.bottom_fill); + bci.bottom_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y + 1, bci.width, bci.threshold, bci.method, bci.bottom_fill); } if (bci.is_left) { bci.x--; @@ -549,8 +604,22 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even merge_pixel_with_background(orig_color, dtc, merged_orig); - int tolerance = (255 * prefs_get_int_attribute_limited("tools.paintbucket", "tolerance", 1, 0, 100)) / 100; PaintBucketChannels method = (PaintBucketChannels)prefs_get_int_attribute("tools.paintbucket", "channels", 0); + int threshold = prefs_get_int_attribute_limited("tools.paintbucket", "threshold", 1, 0, 100); + + switch(method) { + case FLOOD_CHANNELS_ALPHA: + case FLOOD_CHANNELS_RGB: + case FLOOD_CHANNELS_R: + case FLOOD_CHANNELS_G: + case FLOOD_CHANNELS_B: + threshold = (255 * threshold) / 100; + break; + case FLOOD_CHANNELS_H: + case FLOOD_CHANNELS_S: + case FLOOD_CHANNELS_L: + break; + } bool reached_screen_boundary = false; @@ -558,7 +627,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even bci.y_limit = y_limit; bci.width = width; - bci.tolerance = tolerance; + bci.threshold = threshold; bci.method = method; bci.bbox = *bbox; bci.screen = screen; @@ -570,7 +639,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even 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, dtc, tolerance, method)) { + if (compare_pixels(s, orig_color, dtc, threshold, method)) { int x = (int)cp[NR::X]; int y = (int)cp[NR::Y]; @@ -578,7 +647,7 @@ 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, trace_px, orig_color, dtc, x, y - 1, width, tolerance, method, top_fill); + top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y - 1, width, threshold, method, top_fill); } else { if (bbox->min()[NR::Y] > screen.min()[NR::Y]) { aborted = true; break; @@ -587,7 +656,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even } } if (y < y_limit) { - bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y + 1, width, tolerance, method, bottom_fill); + bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y + 1, width, threshold, method, bottom_fill); } else { if (bbox->max()[NR::Y] < screen.max()[NR::Y]) { aborted = true; break; @@ -668,10 +737,22 @@ static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem { gint ret = FALSE; + SPDesktop *desktop = event_context->desktop; + switch (event->type) { case GDK_BUTTON_PRESS: + if (event->button.state & GDK_CONTROL_MASK) { + NR::Point const button_w(event->button.x, + event->button.y); + + SPItem *item = sp_event_context_find_item (desktop, button_w, TRUE, TRUE); + + Inkscape::XML::Node *pathRepr = SP_OBJECT_REPR(item); + /* Set style */ + sp_desktop_apply_style_tool (desktop, pathRepr, "tools.paintbucket", false); + ret = TRUE; + } break; - // motion and release are always on root (why?) default: break; } @@ -686,12 +767,24 @@ static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event) { gint ret = FALSE; + SPDesktop *desktop = event_context->desktop; + switch (event->type) { case GDK_BUTTON_PRESS: if ( event->button.button == 1 ) { - sp_flood_do_flood_fill(event_context, event); - - ret = TRUE; + if (!(event->button.state & GDK_CONTROL_MASK)) { + // set "busy" cursor + GdkCursor *waiting = gdk_cursor_new(GDK_WATCH); + gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, waiting); + + sp_flood_do_flood_fill(event_context, event); + + // restore cursor when done + gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor); + gdk_cursor_unref(waiting); + + ret = TRUE; + } } break; case GDK_KEY_PRESS: