X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fflood-context.cpp;h=2a18b3e3392f5d6874189f5d390447e00835d5b4;hb=f40ff22052300eda2abcd56c08f1db16d8a3b011;hp=07df405c8c8c43b0fbc5e72f0e44477283b0c22a;hpb=660e508a1bd7daa321fb2eeb76be9bf2b9a438ca;p=inkscape.git diff --git a/src/flood-context.cpp b/src/flood-context.cpp index 07df405c8..2a18b3e33 100644 --- a/src/flood-context.cpp +++ b/src/flood-context.cpp @@ -1,24 +1,25 @@ #define __SP_FLOOD_CONTEXT_C__ /* - * Flood fill drawing context - * - * Author: - * Lauris Kaplinski - * bulia byak - * John Bintz - * - * Copyright (C) 2006 Johan Engelen - * Copyright (C) 2000-2005 authors - * Copyright (C) 2000-2001 Ximian, Inc. - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ +* Flood fill drawing context +* +* Author: +* Lauris Kaplinski +* bulia byak +* John Bintz +* +* Copyright (C) 2006 Johan Engelen +* Copyright (C) 2000-2005 authors +* Copyright (C) 2000-2001 Ximian, Inc. +* +* Released under GNU GPL, read the file 'COPYING' for more information +*/ #include "config.h" #include #include +#include #include "macros.h" #include "display/sp-canvas.h" @@ -27,9 +28,7 @@ #include "sp-object.h" #include "sp-rect.h" #include "selection.h" -#include "selection-chemistry.h" #include "desktop-handles.h" -#include "snap.h" #include "desktop.h" #include "desktop-style.h" #include "message-stack.h" @@ -43,15 +42,14 @@ #include "xml/node-event-vector.h" #include "prefs-utils.h" #include "context-fns.h" +#include "rubberband.h" #include "display/nr-arena-item.h" #include "display/nr-arena.h" #include "display/nr-arena-image.h" #include "display/canvas-arena.h" #include "libnr/nr-pixops.h" -#include "libnr/nr-matrix-rotate-ops.h" #include "libnr/nr-matrix-translate-ops.h" -#include "libnr/nr-rotate-fns.h" #include "libnr/nr-scale-ops.h" #include "libnr/nr-scale-translate-ops.h" #include "libnr/nr-translate-matrix-ops.h" @@ -127,7 +125,7 @@ static void sp_flood_context_init(SPFloodContext *flood_context) event_context->hot_y = 30; event_context->xp = 0; event_context->yp = 0; - event_context->tolerance = 0; + event_context->tolerance = 4; event_context->within_tolerance = false; event_context->item_to_select = NULL; @@ -225,29 +223,22 @@ static void sp_flood_context_setup(SPEventContext *ec) rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack()); } -static void merge_pixel_with_background(unsigned char *orig, unsigned char *bg, unsigned char *base) { - for (int i = 0; i < 3; i++) { - base[i] = (255 * (255 - bg[3])) / 255 + (bg[i] * bg[3]) / 255; - base[i] = (base[i] * (255 - orig[3])) / 255 + (orig[i] * orig[3]) / 255; - } - base[3] = 255; +inline static void +merge_pixel_with_background (unsigned char *orig, unsigned char *bg, + unsigned char *base) +{ + int precalc_bg_alpha = (255 * (255 - bg[3])) / 255; + + for (int i = 0; i < 3; i++) { + base[i] = precalc_bg_alpha + (bg[i] * bg[3]) / 255; + base[i] = (base[i] * (255 - orig[3])) / 255 + (orig[i] * orig[3]) / 255; + } } inline unsigned char * get_pixel(guchar *px, int x, int y, int width) { - return px + (x + y * width) * 4; + return px + (x + y * width) * 4; } -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 -}; - GList * flood_channels_dropdown_items_list() { GList *glist = NULL; @@ -263,64 +254,106 @@ GList * flood_channels_dropdown_items_list() { return glist; } -static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *dtc, int tolerance, 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); - case FLOOD_CHANNELS_R: - return ((int)abs(check[0] - orig[0]) <= tolerance); - case FLOOD_CHANNELS_G: - return ((int)abs(check[1] - orig[1]) <= tolerance); - case FLOOD_CHANNELS_B: - return ((int)abs(check[2] - orig[2]) <= tolerance); - case FLOOD_CHANNELS_RGB: - unsigned char merged_orig[4]; - unsigned char merged_check[4]; - - merge_pixel_with_background(orig, dtc, merged_orig); - merge_pixel_with_background(check, dtc, merged_check); - - for (int i = 0; i < 3; i++) { - diff += (int)abs(merged_check[i] - merged_orig[i]); - } - return ((diff / 3) <= ((tolerance * 3) / 4)); - - case FLOOD_CHANNELS_H: - return ((int)(fabs(hsl_check[0] - hsl_orig[0]) * 100.0) <= tolerance); - case FLOOD_CHANNELS_S: - return ((int)(fabs(hsl_check[1] - hsl_orig[1]) * 100.0) <= tolerance); - case FLOOD_CHANNELS_L: - return ((int)(fabs(hsl_check[2] - hsl_orig[2]) * 100.0) <= tolerance); - } - - return false; +GList * flood_autogap_dropdown_items_list() { + GList *glist = NULL; + + glist = g_list_append (glist, _("None")); + glist = g_list_append (glist, _("Small")); + glist = g_list_append (glist, _("Medium")); + glist = g_list_append (glist, _("Large")); + + return glist; } -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) { - unsigned char *t = get_pixel(px, x, y, width); - if (compare_pixels(t, orig, dtc, tolerance, method)) { - 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)); - } +static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *merged_orig_pixel, 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]) <= 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_check[3]; + + merge_pixel_with_background(check, dtc, merged_check); + + for (int i = 0; i < 3; i++) { + diff += (int)abs(merged_check[i] - merged_orig_pixel[i]); + } + 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; - } - return true; } -static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) { +static inline bool is_pixel_checked(unsigned char *t) { return t[0] == 1; } +static inline bool is_pixel_queued(unsigned char *t) { return t[1] == 1; } +static inline bool is_pixel_paintability_checked(unsigned char *t) { return t[2] != 0; } +static inline bool is_pixel_paintable(unsigned char *t) { return t[2] == 1; } +static inline bool is_pixel_colored(unsigned char *t) { return t[3] == 255; } + +static inline void mark_pixel_checked(unsigned char *t) { t[0] = 1; } +static inline void mark_pixel_unchecked(unsigned char *t) { t[0] = 0; } +static inline void mark_pixel_queued(unsigned char *t) { t[1] = 1; } +static inline void mark_pixel_paintable(unsigned char *t) { t[2] = 1; } +static inline void mark_pixel_not_paintable(unsigned char *t) { t[2] = 2; } +static inline void mark_pixel_colored(unsigned char *t) { t[3] = 255; } + +static inline void clear_pixel_paintability(unsigned char *t) { t[2] = 0; } + +struct bitmap_coords_info { + bool is_left; + unsigned int x; + unsigned int y; + int y_limit; + unsigned int width; + unsigned int height; + unsigned int threshold; + unsigned int radius; + PaintBucketChannels method; + unsigned char *dtc; + unsigned char *merged_orig_pixel; + NR::Rect bbox; + NR::Rect screen; + unsigned int max_queue_size; +}; + +inline static bool check_if_pixel_is_paintable(guchar *px, unsigned char *trace_t, int x, int y, unsigned char *orig_color, bitmap_coords_info bci) { + if (is_pixel_paintability_checked(trace_t)) { + return is_pixel_paintable(trace_t); + } else { + unsigned char *t = get_pixel(px, x, y, bci.width); + if (compare_pixels(t, orig_color, bci.merged_orig_pixel, bci.dtc, bci.threshold, bci.method)) { + mark_pixel_paintable(trace_t); + return true; + } else { + mark_pixel_not_paintable(trace_t); + return false; + } + } +} + +static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform, bool union_with_selection) { SPDocument *document = sp_desktop_document(desktop); Inkscape::Trace::Potrace::PotraceTracingEngine pte; @@ -416,72 +449,175 @@ 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))); + if (union_with_selection) { + desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, _("Area filled, path with %d nodes created and unioned with selection."), sp_nodes_in_path(SP_PATH(reprobj))); + selection->add(reprobj); + sp_selected_path_union_skip_undo(); + } else { + desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, _("Area filled, path with %d nodes created."), sp_nodes_in_path(SP_PATH(reprobj))); + selection->set(reprobj); + } + } - + Inkscape::GC::release(pathRepr); } } -struct bitmap_coords_info { - bool is_left; - int x; - int y; - int y_limit; - int width; - int tolerance; - PaintBucketChannels method; - unsigned char *dtc; - bool top_fill; - bool bottom_fill; - NR::Rect bbox; - NR::Rect screen; -}; - enum ScanlineCheckResult { - SCANLINE_CHECK_OK, - SCANLINE_CHECK_ABORTED, - SCANLINE_CHECK_BOUNDARY + SCANLINE_CHECK_OK, + SCANLINE_CHECK_ABORTED, + SCANLINE_CHECK_BOUNDARY }; -static ScanlineCheckResult perform_bitmap_scanline_check(std::queue *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci) { +static bool coords_in_range(unsigned int x, unsigned int y, bitmap_coords_info bci) { + return (x < bci.width) && + (y < bci.height); +} + +#define PAINT_DIRECTION_LEFT 1 +#define PAINT_DIRECTION_RIGHT 2 +#define PAINT_DIRECTION_UP 4 +#define PAINT_DIRECTION_DOWN 8 +#define PAINT_DIRECTION_ALL 15 + +static unsigned int paint_pixel(guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci, unsigned char *original_point_trace_t) { + if (bci.radius == 0) { + mark_pixel_colored(original_point_trace_t); + return PAINT_DIRECTION_ALL; + } else { + unsigned char *trace_t; + + bool can_paint_up = true; + bool can_paint_down = true; + bool can_paint_left = true; + bool can_paint_right = true; + + for (int y = -bci.radius; y <= (int)bci.radius; y++) { + int ty = bci.y + y; + for (int x = -bci.radius; x <= (int)bci.radius; x++) { + int tx = bci.x + x; + + if (coords_in_range(tx, ty, bci)) { + trace_t = get_pixel(trace_px, tx, ty, bci.width); + if (!is_pixel_colored(trace_t)) { + if (check_if_pixel_is_paintable(px, trace_t, tx, ty, orig_color, bci)) { + mark_pixel_colored(trace_t); + } else { + if (x < 0) { can_paint_left = false; } + if (x > 0) { can_paint_right = false; } + if (y < 0) { can_paint_up = false; } + if (y > 0) { can_paint_down = false; } + } + } + } + } + } + + unsigned int paint_directions = 0; + if (can_paint_left) { paint_directions += PAINT_DIRECTION_LEFT; } + if (can_paint_right) { paint_directions += PAINT_DIRECTION_RIGHT; } + if (can_paint_up) { paint_directions += PAINT_DIRECTION_UP; } + if (can_paint_down) { paint_directions += PAINT_DIRECTION_DOWN; } + + return paint_directions; + } +} + +static ScanlineCheckResult perform_bitmap_scanline_check(std::deque *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci) { bool aborted = false; bool reached_screen_boundary = false; bool ok; bool keep_tracing; - unsigned char *t, *trace_t; - + bool initial_paint = true; + + unsigned char *current_trace_t = get_pixel(trace_px, bci.x, bci.y, bci.width); + unsigned int paint_directions; + + bool currently_painting_top = false; + bool currently_painting_bottom = false; + + unsigned int pix_width = bci.width * 4; + do { ok = false; if (bci.is_left) { - keep_tracing = (bci.x >= 0); + keep_tracing = (bci.x != 0); } else { keep_tracing = (bci.x < bci.width); } 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)) { - 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); + if (check_if_pixel_is_paintable(px, current_trace_t, bci.x, bci.y, orig_color, bci)) { + paint_directions = paint_pixel(px, trace_px, orig_color, bci, current_trace_t); + if (bci.radius == 0) { + mark_pixel_checked(current_trace_t); + } + + if (paint_directions & PAINT_DIRECTION_UP) { + unsigned int ty = bci.y - 1; + if (ty > 0) { + unsigned char *trace_t = current_trace_t - pix_width; + if (!is_pixel_queued(trace_t)) { + bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, ty, orig_color, bci); + + if (initial_paint) { currently_painting_top = !ok_to_paint; } + + if (ok_to_paint && (!currently_painting_top)) { + currently_painting_top = true; + if ((fill_queue->size() < bci.max_queue_size)) { + fill_queue->push_back(NR::Point(bci.x, ty)); + mark_pixel_queued(trace_t); + } + } + if ((!ok_to_paint) && currently_painting_top) { + currently_painting_top = false; + } + } + } } - 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); + + if (paint_directions & PAINT_DIRECTION_DOWN) { + unsigned int ty = bci.y + 1; + if (ty < bci.height) { + unsigned char *trace_t = current_trace_t + pix_width; + if (!is_pixel_queued(trace_t)) { + bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, ty, orig_color, bci); + + if (initial_paint) { currently_painting_bottom = !ok_to_paint; } + + if (ok_to_paint && (!currently_painting_bottom)) { + currently_painting_bottom = true; + if ((fill_queue->size() < bci.max_queue_size)) { + fill_queue->push_back(NR::Point(bci.x, ty)); + mark_pixel_queued(trace_t); + } + } + if ((!ok_to_paint) && currently_painting_bottom) { + currently_painting_bottom = false; + } + } + } } + if (bci.is_left) { - bci.x--; + if (paint_directions & PAINT_DIRECTION_LEFT) { + bci.x -= 1; current_trace_t -= 4; + ok = true; + } } else { - bci.x++; + if (paint_directions & PAINT_DIRECTION_RIGHT) { + bci.x += 1; current_trace_t += 4; + ok = true; + } } - ok = true; + + initial_paint = false; } } else { if (bci.bbox.min()[NR::X] > bci.screen.min()[NR::X]) { @@ -497,7 +633,7 @@ static ScanlineCheckResult perform_bitmap_scanline_check(std::queue * return SCANLINE_CHECK_OK; } -static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event) { +static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event, bool union_with_selection, bool is_point_fill, bool is_touch_fill) { SPDesktop *desktop = event_context->desktop; SPDocument *document = sp_desktop_document(desktop); @@ -511,8 +647,8 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even NR::Maybe bbox = document_root->getBounds(NR::identity()); if (!bbox) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Area is not bounded, cannot fill.")); - return; + desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Area is not bounded, cannot fill.")); + return; } double zoom_scale = desktop->current_zoom(); @@ -520,12 +656,12 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even NR::Rect screen = desktop->get_display_area(); - int width = (int)ceil(screen.extent(NR::X) * zoom_scale * padding); - int height = (int)ceil(screen.extent(NR::Y) * zoom_scale * padding); + unsigned int width = (int)ceil(screen.extent(NR::X) * zoom_scale * padding); + unsigned int height = (int)ceil(screen.extent(NR::Y) * zoom_scale * padding); NR::Point origin(screen.min()[NR::X], sp_document_height(document) - screen.extent(NR::Y) - screen.min()[NR::Y]); - + origin[NR::X] = origin[NR::X] + (screen.extent(NR::X) * ((1 - padding) / 2)); origin[NR::Y] = origin[NR::Y] + (screen.extent(NR::Y) * ((1 - padding) / 2)); @@ -548,7 +684,6 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE); guchar *px = g_new(guchar, 4 * width * height); - //memset(px, 0x00, 4 * width * height); NRPixBlock B; nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N, @@ -564,13 +699,13 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even dtc[2] = NR_RGBA32_B(bgcolor); dtc[3] = NR_RGBA32_A(bgcolor); - for (int fy = 0; fy < height; fy++) { - guchar *p = NR_PIXBLOCK_PX(&B) + fy * B.rs; - for (int fx = 0; fx < width; fx++) { - for (int i = 0; i < 4; i++) { - *p++ = dtc[i]; + for (unsigned int fy = 0; fy < height; fy++) { + guchar *p = NR_PIXBLOCK_PX(&B) + fy * B.rs; + for (unsigned int fx = 0; fx < width; fx++) { + for (int i = 0; i < 4; i++) { + *p++ = dtc[i]; + } } - } } nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE ); @@ -582,44 +717,57 @@ 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])); - pw[NR::Y] = (int)MIN(height - 1, MAX(0, pw[NR::Y])); - guchar *trace_px = g_new(guchar, 4 * width * height); memset(trace_px, 0x00, 4 * width * height); - std::queue fill_queue; - fill_queue.push(pw); + std::deque fill_queue; + std::queue color_queue; - bool aborted = false; - int y_limit = height - 1; + std::vector fill_points; - unsigned char orig_color[4]; - 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]; } + if (is_point_fill) { + fill_points.push_back(NR::Point(event->button.x, event->button.y)); + } else { + Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(); + fill_points = r->getPoints(); + } - unsigned char merged_orig[4]; + for (unsigned int i = 0; i < fill_points.size(); i++) { + NR::Point pw = NR::Point(fill_points[i][NR::X] / zoom_scale, sp_document_height(document) + (fill_points[i][NR::Y] / zoom_scale)) * affine; + + pw[NR::X] = (int)MIN(width - 1, MAX(0, pw[NR::X])); + pw[NR::Y] = (int)MIN(height - 1, MAX(0, pw[NR::Y])); + + if (is_touch_fill) { + if (i == 0) { + color_queue.push(pw); + } else { + fill_queue.push_back(pw); + } + } else { + color_queue.push(pw); + } + } + + bool aborted = false; + int y_limit = height - 1; - merge_pixel_with_background(orig_color, dtc, merged_orig); - PaintBucketChannels method = (PaintBucketChannels)prefs_get_int_attribute("tools.paintbucket", "channels", 0); - int tolerance = prefs_get_int_attribute_limited("tools.paintbucket", "tolerance", 1, 0, 100); + 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: - tolerance = (255 * tolerance) / 100; - break; - case FLOOD_CHANNELS_H: - case FLOOD_CHANNELS_S: - case FLOOD_CHANNELS_L: - break; - } + 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; @@ -627,94 +775,127 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even bci.y_limit = y_limit; bci.width = width; - bci.tolerance = tolerance; + bci.height = height; + bci.threshold = threshold; bci.method = method; bci.bbox = *bbox; bci.screen = screen; bci.dtc = dtc; + bci.radius = prefs_get_int_attribute_limited("tools.paintbucket", "autogap", 0, 0, 3); + bci.max_queue_size = width * height; - 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, dtc, tolerance, method)) { - int x = (int)cp[NR::X]; - int y = (int)cp[NR::Y]; - - bool top_fill = true; - bool bottom_fill = true; + bool first_run = true; + + while (!color_queue.empty() && !aborted) { + NR::Point color_point = color_queue.front(); + color_queue.pop(); - 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); - } else { - if (bbox->min()[NR::Y] > screen.min()[NR::Y]) { - aborted = true; break; - } else { - reached_screen_boundary = true; - } - } - 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); - } else { - if (bbox->max()[NR::Y] < screen.max()[NR::Y]) { - aborted = true; break; - } else { - reached_screen_boundary = true; - } - } + unsigned char *orig_px = get_pixel(px, (int)color_point[NR::X], (int)color_point[NR::Y], width); + unsigned char orig_color[4]; + for (int i = 0; i < 4; i++) { orig_color[i] = orig_px[i]; } - bci.is_left = true; - bci.x = x; - bci.y = y; - bci.top_fill = top_fill; - bci.bottom_fill = bottom_fill; + unsigned char merged_orig[3]; + + merge_pixel_with_background(orig_color, dtc, merged_orig); - ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci); + bci.merged_orig_pixel = merged_orig; - switch (result) { - case SCANLINE_CHECK_ABORTED: - aborted = true; - break; - case SCANLINE_CHECK_BOUNDARY: - reached_screen_boundary = true; - break; - default: - break; + unsigned char *trace_t = get_pixel(trace_px, (int)color_point[NR::X], (int)color_point[NR::Y], width); + if (!is_pixel_checked(trace_t) && !is_pixel_colored(trace_t)) { + if (check_if_pixel_is_paintable(px, trace_px, (int)color_point[NR::X], (int)color_point[NR::Y], orig_color, bci)) { + fill_queue.push_front(color_point); + + if (!first_run) { + for (unsigned int y = 0; y < height; y++) { + trace_t = get_pixel(trace_px, 0, y, width); + for (unsigned int x = 0; x < width; x++) { + clear_pixel_paintability(trace_t); + trace_t += 4; + } + } + } + first_run = false; + } } - bci.is_left = false; - bci.x = x + 1; - bci.y = y; - bci.top_fill = top_fill; - bci.bottom_fill = bottom_fill; + while (!fill_queue.empty() && !aborted) { + NR::Point cp = fill_queue.front(); + fill_queue.pop_front(); + + unsigned char *trace_t = get_pixel(trace_px, (int)cp[NR::X], (int)cp[NR::Y], width); + if (!is_pixel_checked(trace_t)) { + mark_pixel_checked(trace_t); + int x = (int)cp[NR::X]; + int y = (int)cp[NR::Y]; + + if (y == 0) { + if (bbox->min()[NR::Y] > screen.min()[NR::Y]) { + aborted = true; break; + } else { + reached_screen_boundary = true; + } + } + + if (y == y_limit) { + if (bbox->max()[NR::Y] < screen.max()[NR::Y]) { + aborted = true; break; + } else { + reached_screen_boundary = true; + } + } + + bci.is_left = true; + bci.x = x; + bci.y = y; + + ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci); + + switch (result) { + case SCANLINE_CHECK_ABORTED: + aborted = true; + break; + case SCANLINE_CHECK_BOUNDARY: + reached_screen_boundary = true; + break; + default: + break; + } + + if (bci.x < width) { + trace_t += 4; + if (!is_pixel_checked(trace_t) && !is_pixel_queued(trace_t)) { + mark_pixel_checked(trace_t); + bci.is_left = false; + bci.x = x + 1; - result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci); + result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci); - switch (result) { - case SCANLINE_CHECK_ABORTED: - aborted = true; - break; - case SCANLINE_CHECK_BOUNDARY: - reached_screen_boundary = true; - break; - default: - break; + switch (result) { + case SCANLINE_CHECK_ABORTED: + aborted = true; + break; + case SCANLINE_CHECK_BOUNDARY: + reached_screen_boundary = true; + break; + default: + break; + } + } + } + } } - } } g_free(px); if (aborted) { - g_free(trace_px); - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Area is not bounded, cannot fill.")); - return; + g_free(trace_px); + desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Area is not bounded, cannot fill.")); + return; } if (reached_screen_boundary) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Only the visible part of the bounded area was filled. If you want to fill all of the area, undo, zoom out, and fill again.")); + desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Only the visible part of the bounded area was filled. If you want to fill all of the area, undo, zoom out, and fill again.")); } GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(trace_px, @@ -726,7 +907,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even NR::Matrix inverted_affine = NR::Matrix(affine).inverse(); - do_trace(pixbuf, desktop, inverted_affine); + do_trace(pixbuf, desktop, inverted_affine, union_with_selection); g_free(trace_px); @@ -737,10 +918,23 @@ 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); + sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET, _("Set style on object")); + ret = TRUE; + } break; - // motion and release are always on root (why?) default: break; } @@ -754,13 +948,81 @@ static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event) { + static bool dragging; + 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); + if (!(event->button.state & GDK_CONTROL_MASK)) { + NR::Point const button_w(event->button.x, + event->button.y); + + if (Inkscape::have_viable_layer(desktop, event_context->defaultMessageContext())) { + // save drag origin + event_context->xp = (gint) button_w[NR::X]; + event_context->yp = (gint) button_w[NR::Y]; + event_context->within_tolerance = true; + + dragging = true; + + NR::Point const p(desktop->w2d(button_w)); + Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH); + Inkscape::Rubberband::get()->start(desktop, p); + } + } + } + case GDK_MOTION_NOTIFY: + if ( dragging + && ( event->motion.state & GDK_BUTTON1_MASK ) ) + { + if ( event_context->within_tolerance + && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance ) + && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) { + break; // do not drag if we're within tolerance from origin + } + + event_context->within_tolerance = false; + + NR::Point const motion_pt(event->motion.x, event->motion.y); + NR::Point const p(desktop->w2d(motion_pt)); + if (Inkscape::Rubberband::get()->is_started()) { + Inkscape::Rubberband::get()->move(p); + event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("Draw over areas to add to fill, hold Alt for touch fill")); + gobble_motion_events(GDK_BUTTON1_MASK); + } + } + break; + + case GDK_BUTTON_RELEASE: + if ( event->button.button == 1 ) { + Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(); + if (r->is_started()) { + // set "busy" cursor + desktop->setWaitingCursor(); + + if (SP_IS_EVENT_CONTEXT(event_context)) { + // Since setWaitingCursor runs main loop iterations, we may have already left this tool! + // So check if the tool is valid before doing anything + dragging = false; + + bool is_point_fill = event_context->within_tolerance; + bool is_touch_fill = event->button.state & GDK_MOD1_MASK; + + sp_flood_do_flood_fill(event_context, event, event->button.state & GDK_SHIFT_MASK, is_point_fill, is_touch_fill); + + desktop->clearWaitingCursor(); + // restore cursor when done; note that it may already be different if e.g. user + // switched to another tool during interruptible tracing or drawing, in which case do nothing + + ret = TRUE; + } - ret = TRUE; + r->stop(); + event_context->defaultMessageContext()->clear(); + } } break; case GDK_KEY_PRESS: @@ -806,15 +1068,15 @@ static void sp_flood_finish(SPFloodContext *rc) sp_desktop_selection(desktop)->set(rc->item); sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET, - _("Fill bounded area")); + _("Fill bounded area")); rc->item = NULL; } } -void flood_channels_changed(GtkComboBox *cbox, GtkWidget *tbl) +void flood_channels_set_channels( gint channels ) { - prefs_set_int_attribute("tools.paintbucket", "channels", (gint)gtk_combo_box_get_active(cbox)); + prefs_set_int_attribute("tools.paintbucket", "channels", channels); } /*