diff --git a/src/flood-context.cpp b/src/flood-context.cpp
index 561328c54c66319239a5260f9eeb58399adbffb0..dd26782a37897239b14882be3fad5febee95c517 100644 (file)
--- a/src/flood-context.cpp
+++ b/src/flood-context.cpp
-#define __SP_FLOOD_CONTEXT_C__
-
-/*
- * Flood fill drawing context
- *
- * Author:
+/** @file
+ * @brief Bucket fill drawing context, works by bitmap filling an area on a rendered version
+ * of the current display and then tracing the result using potrace.
+ */
+/* Author:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* John Bintz <jcoswell@coswellproductions.org>
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* John Bintz <jcoswell@coswellproductions.org>
+ * Jon A. Cruz <jon@joncruz.org>
+ * Abhishek Sharma
*
* Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
* Copyright (C) 2000-2005 authors
*
* Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
* Copyright (C) 2000-2005 authors
* Released under GNU GPL, read the file 'COPYING' for more information
*/
* Released under GNU GPL, read the file 'COPYING' for more information
*/
+#ifdef HAVE_CONFIG_H
#include "config.h"
#include "config.h"
+#endif
#include <gdk/gdkkeysyms.h>
#include <queue>
#include <gdk/gdkkeysyms.h>
#include <queue>
+#include <deque>
#include "macros.h"
#include "display/sp-canvas.h"
#include "macros.h"
#include "display/sp-canvas.h"
#include "sp-object.h"
#include "sp-rect.h"
#include "selection.h"
#include "sp-object.h"
#include "sp-rect.h"
#include "selection.h"
-#include "selection-chemistry.h"
#include "desktop-handles.h"
#include "desktop-handles.h"
-#include "snap.h"
#include "desktop.h"
#include "desktop-style.h"
#include "message-stack.h"
#include "desktop.h"
#include "desktop-style.h"
#include "message-stack.h"
#include "object-edit.h"
#include "xml/repr.h"
#include "xml/node-event-vector.h"
#include "object-edit.h"
#include "xml/repr.h"
#include "xml/node-event-vector.h"
-#include "prefs-utils.h"
+#include "preferences.h"
#include "context-fns.h"
#include "context-fns.h"
+#include "rubberband.h"
+#include "shape-editor.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 "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-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"
#include "libnr/nr-translate-scale-ops.h"
#include "libnr/nr-matrix-ops.h"
#include "libnr/nr-scale-ops.h"
#include "libnr/nr-scale-translate-ops.h"
#include "libnr/nr-translate-matrix-ops.h"
#include "libnr/nr-translate-scale-ops.h"
#include "libnr/nr-matrix-ops.h"
+#include <2geom/pathvector.h>
#include "sp-item.h"
#include "sp-root.h"
#include "sp-defs.h"
#include "sp-item.h"
#include "sp-root.h"
#include "sp-defs.h"
#include "splivarot.h"
#include "livarot/Path.h"
#include "livarot/Shape.h"
#include "splivarot.h"
#include "livarot/Path.h"
#include "livarot/Shape.h"
-#include "libnr/n-art-bpath.h"
#include "svg/svg.h"
#include "svg/svg.h"
+#include "color.h"
#include "trace/trace.h"
#include "trace/trace.h"
+#include "trace/imagemap.h"
#include "trace/potrace/inkscape-potrace.h"
#include "trace/potrace/inkscape-potrace.h"
+using Inkscape::DocumentUndo;
+
static void sp_flood_context_class_init(SPFloodContextClass *klass);
static void sp_flood_context_init(SPFloodContext *flood_context);
static void sp_flood_context_dispose(GObject *object);
static void sp_flood_context_class_init(SPFloodContextClass *klass);
static void sp_flood_context_init(SPFloodContext *flood_context);
static void sp_flood_context_dispose(GObject *object);
event_context->hot_y = 30;
event_context->xp = 0;
event_context->yp = 0;
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;
event_context->within_tolerance = false;
event_context->item_to_select = NULL;
- event_context->shape_repr = NULL;
- event_context->shape_knot_holder = NULL;
-
flood_context->item = NULL;
new (&flood_context->sel_changed_connection) sigc::connection();
flood_context->item = NULL;
new (&flood_context->sel_changed_connection) sigc::connection();
rc->sel_changed_connection.disconnect();
rc->sel_changed_connection.~connection();
rc->sel_changed_connection.disconnect();
rc->sel_changed_connection.~connection();
+ delete ec->shape_editor;
+ ec->shape_editor = NULL;
+
/* fixme: This is necessary because we do not grab */
if (rc->item) {
sp_flood_finish(rc);
}
/* fixme: This is necessary because we do not grab */
if (rc->item) {
sp_flood_finish(rc);
}
- if (ec->shape_repr) { // remove old listener
- sp_repr_remove_listener_by_data(ec->shape_repr, ec);
- Inkscape::GC::release(ec->shape_repr);
- ec->shape_repr = 0;
- }
-
if (rc->_message_context) {
delete rc->_message_context;
}
if (rc->_message_context) {
delete rc->_message_context;
}
G_OBJECT_CLASS(parent_class)->dispose(object);
}
G_OBJECT_CLASS(parent_class)->dispose(object);
}
-static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
- NULL, /* child_added */
- NULL, /* child_removed */
- ec_shape_event_attr_changed,
- NULL, /* content_changed */
- NULL /* order_changed */
-};
-
/**
\brief Callback that processes the "changed" signal on the selection;
destroys old and creates new knotholder
/**
\brief Callback that processes the "changed" signal on the selection;
destroys old and creates new knotholder
@@ -181,21 +173,9 @@ void sp_flood_context_selection_changed(Inkscape::Selection *selection, gpointer
SPFloodContext *rc = SP_FLOOD_CONTEXT(data);
SPEventContext *ec = SP_EVENT_CONTEXT(rc);
SPFloodContext *rc = SP_FLOOD_CONTEXT(data);
SPEventContext *ec = SP_EVENT_CONTEXT(rc);
- if (ec->shape_repr) { // remove old listener
- sp_repr_remove_listener_by_data(ec->shape_repr, ec);
- Inkscape::GC::release(ec->shape_repr);
- ec->shape_repr = 0;
- }
-
- SPItem *item = selection->singleItem();
- if (item) {
- Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
- if (shape_repr) {
- ec->shape_repr = shape_repr;
- Inkscape::GC::anchor(shape_repr);
- sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
- }
- }
+ ec->shape_editor->unset_item(SH_KNOTHOLDER);
+ SPItem *item = selection->singleItem();
+ ec->shape_editor->set_item(item, SH_KNOTHOLDER);
}
static void sp_flood_context_setup(SPEventContext *ec)
}
static void sp_flood_context_setup(SPEventContext *ec)
((SPEventContextClass *) parent_class)->setup(ec);
}
((SPEventContextClass *) parent_class)->setup(ec);
}
+ ec->shape_editor = new ShapeEditor(ec->desktop);
+
SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
if (item) {
SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
if (item) {
- Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
- if (shape_repr) {
- ec->shape_repr = shape_repr;
- Inkscape::GC::anchor(shape_repr);
- sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
- }
+ ec->shape_editor->set_item(item, SH_KNOTHOLDER);
}
rc->sel_changed_connection.disconnect();
}
rc->sel_changed_connection.disconnect();
);
rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
);
rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
+
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ if (prefs->getBool("/tools/paintbucket/selcue")) {
+ rc->enableSelectionCue();
+ }
}
}
-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;
+/**
+ * \brief Merge a pixel with the background color.
+ * \param orig The pixel to merge with the background.
+ * \param bg The background color.
+ * \param base The pixel to merge the original and background into.
+ */
+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;
+ }
}
}
+/**
+ * \brief Get the pointer to a pixel in a pixel buffer.
+ * \param px The pixel buffer.
+ * \param x The X coordinate.
+ * \param y The Y coordinate.
+ * \param width The width of the pixel buffer.
+ */
inline unsigned char * get_pixel(guchar *px, int x, int y, int width) {
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_ALPHA
-};
+inline unsigned char * get_trace_pixel(guchar *trace_px, int x, int y, int width) {
+ return trace_px + (x + y * width);
+}
+/**
+ * \brief Generate the list of trace channel selection entries.
+ */
GList * flood_channels_dropdown_items_list() {
GList *glist = NULL;
glist = g_list_append (glist, _("Visible Colors"));
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;
}
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) {
- int diff = 0;
-
- switch (method) {
- case FLOOD_CHANNELS_ALPHA:
- return ((int)abs(check[3] - orig[3]) <= (tolerance / 4));
- 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));
- }
-
- return false;
+/**
+ * \brief Generate the list of autogap selection entries.
+ */
+GList * flood_autogap_dropdown_items_list() {
+ GList *glist = NULL;
+
+ glist = g_list_append (glist, (void*) C_("Flood autogap", "None"));
+ glist = g_list_append (glist, (void*) C_("Flood autogap", "Small"));
+ glist = g_list_append (glist, (void*) C_("Flood autogap", "Medium"));
+ glist = g_list_append (glist, (void*) C_("Flood autogap", "Large"));
+
+ return glist;
}
}
-static bool try_add_to_queue(std::queue<NR::Point> *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));
- }
+/**
+ * \brief Compare a pixel in a pixel buffer with another pixel to determine if a point should be included in the fill operation.
+ * \param check The pixel in the pixel buffer to check.
+ * \param orig The original selected pixel to use as the fill target color.
+ * \param merged_orig_pixel The original pixel merged with the background.
+ * \param dtc The desktop background color.
+ * \param threshold The fill threshold.
+ * \param method The fill method to use as defined in PaintBucketChannels.
+ */
+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 false;
- }
- return true;
}
}
-static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
+enum {
+ PIXEL_CHECKED = 1,
+ PIXEL_QUEUED = 2,
+ PIXEL_PAINTABLE = 4,
+ PIXEL_NOT_PAINTABLE = 8,
+ PIXEL_COLORED = 16
+};
+
+static inline bool is_pixel_checked(unsigned char *t) { return (*t & PIXEL_CHECKED) == PIXEL_CHECKED; }
+static inline bool is_pixel_queued(unsigned char *t) { return (*t & PIXEL_QUEUED) == PIXEL_QUEUED; }
+static inline bool is_pixel_paintability_checked(unsigned char *t) {
+ return !((*t & PIXEL_PAINTABLE) == 0) && ((*t & PIXEL_NOT_PAINTABLE) == 0);
+}
+static inline bool is_pixel_paintable(unsigned char *t) { return (*t & PIXEL_PAINTABLE) == PIXEL_PAINTABLE; }
+static inline bool is_pixel_colored(unsigned char *t) { return (*t & PIXEL_COLORED) == PIXEL_COLORED; }
+
+static inline void mark_pixel_checked(unsigned char *t) { *t |= PIXEL_CHECKED; }
+static inline void mark_pixel_unchecked(unsigned char *t) { *t ^= PIXEL_CHECKED; }
+static inline void mark_pixel_queued(unsigned char *t) { *t |= PIXEL_QUEUED; }
+static inline void mark_pixel_paintable(unsigned char *t) { *t |= PIXEL_PAINTABLE; *t ^= PIXEL_NOT_PAINTABLE; }
+static inline void mark_pixel_not_paintable(unsigned char *t) { *t |= PIXEL_NOT_PAINTABLE; *t ^= PIXEL_PAINTABLE; }
+static inline void mark_pixel_colored(unsigned char *t) { *t |= PIXEL_COLORED; }
+
+static inline void clear_pixel_paintability(unsigned char *t) { *t ^= PIXEL_PAINTABLE; *t ^= PIXEL_NOT_PAINTABLE; }
+
+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;
+ Geom::Rect bbox;
+ Geom::Rect screen;
+ unsigned int max_queue_size;
+ unsigned int current_step;
+};
+
+/**
+ * \brief Check if a pixel can be included in the fill.
+ * \param px The rendered pixel buffer to check.
+ * \param trace_t The pixel in the trace pixel buffer to check or mark.
+ * \param x The X coordinate.
+ * \param y The y coordinate.
+ * \param orig_color The original selected pixel to use as the fill target color.
+ * \param bci The bitmap_coords_info structure.
+ */
+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;
+ }
+ }
+}
+
+/**
+ * \brief Perform the bitmap-to-vector tracing and place the traced path onto the document.
+ * \param px The trace pixel buffer to trace to SVG.
+ * \param desktop The desktop on which to place the final SVG path.
+ * \param transform The transform to apply to the final SVG path.
+ * \param union_with_selection If true, merge the final SVG path with the current selection.
+ */
+static void do_trace(bitmap_coords_info bci, guchar *trace_px, SPDesktop *desktop, Geom::Matrix transform, unsigned int min_x, unsigned int max_x, unsigned int min_y, unsigned int max_y, bool union_with_selection) {
SPDocument *document = sp_desktop_document(desktop);
SPDocument *document = sp_desktop_document(desktop);
-
+
+ unsigned char *trace_t;
+
+ GrayMap *gray_map = GrayMapCreate((max_x - min_x + 1), (max_y - min_y + 1));
+ unsigned int gray_map_y = 0;
+ for (unsigned int y = min_y; y <= max_y; y++) {
+ unsigned long *gray_map_t = gray_map->rows[gray_map_y];
+
+ trace_t = get_trace_pixel(trace_px, min_x, y, bci.width);
+ for (unsigned int x = min_x; x <= max_x; x++) {
+ *gray_map_t = is_pixel_colored(trace_t) ? GRAYMAP_BLACK : GRAYMAP_WHITE;
+ gray_map_t++;
+ trace_t++;
+ }
+ gray_map_y++;
+ }
+
Inkscape::Trace::Potrace::PotraceTracingEngine pte;
Inkscape::Trace::Potrace::PotraceTracingEngine pte;
-
- pte.setTraceType(Inkscape::Trace::Potrace::TRACE_BRIGHTNESS);
- pte.setInvert(false);
+ pte.keepGoing = 1;
+ std::vector<Inkscape::Trace::TracingEngineResult> results = pte.traceGrayMap(gray_map);
+ gray_map->destroy(gray_map);
- Glib::RefPtr<Gdk::Pixbuf> pixbuf = Glib::wrap(px, true);
-
- std::vector<Inkscape::Trace::TracingEngineResult> results = pte.trace(pixbuf);
-
- Inkscape::XML::Node *layer_repr = SP_GROUP(desktop->currentLayer())->repr;
- Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+ //XML Tree being used here directly while it shouldn't be...."
+ Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
long totalNodeCount = 0L;
long totalNodeCount = 0L;
- double offset = prefs_get_double_attribute("tools.paintbucket", "offset", 0.0);
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ double offset = prefs->getDouble("/tools/paintbucket/offset", 0.0);
for (unsigned int i=0 ; i<results.size() ; i++) {
Inkscape::Trace::TracingEngineResult result = results[i];
for (unsigned int i=0 ; i<results.size() ; i++) {
Inkscape::Trace::TracingEngineResult result = results[i];
@@ -311,11 +433,11 @@ static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
/* Set style */
Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
/* Set style */
- sp_desktop_apply_style_tool (desktop, pathRepr, "tools.paintbucket", false);
+ sp_desktop_apply_style_tool (desktop, pathRepr, "/tools/paintbucket", false);
- NArtBpath *bpath = sp_svg_read_path(result.getPathData().c_str());
- Path *path = bpath_to_Path(bpath);
- g_free(bpath);
+ Geom::PathVector pathv = sp_svg_read_pathv(result.getPathData().c_str());
+ Path *path = new Path;
+ path->LoadPathVector(pathv);
if (offset != 0) {
if (offset != 0) {
@@ -361,17 +483,17 @@ static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
g_free(str);
}
g_free(str);
}
- layer_repr->addChild(pathRepr, NULL);
+ desktop->currentLayer()->addChild(pathRepr,NULL);
SPObject *reprobj = document->getObjectByRepr(pathRepr);
if (reprobj) {
SPObject *reprobj = document->getObjectByRepr(pathRepr);
if (reprobj) {
- sp_item_write_transform(SP_ITEM(reprobj), pathRepr, transform, NULL);
+ SP_ITEM(reprobj)->doWriteTransform(pathRepr, transform, NULL);
// premultiply the item transform by the accumulated parent transform in the paste layer
// premultiply the item transform by the accumulated parent transform in the paste layer
- NR::Matrix local = sp_item_i2doc_affine(SP_GROUP(desktop->currentLayer()));
- if (!local.test_identity()) {
+ Geom::Matrix local (SP_GROUP(desktop->currentLayer())->i2doc_affine());
+ if (!local.isIdentity()) {
gchar const *t_str = pathRepr->attribute("transform");
gchar const *t_str = pathRepr->attribute("transform");
- NR::Matrix item_t (NR::identity());
+ Geom::Matrix item_t (Geom::identity());
if (t_str)
sp_svg_transform_read(t_str, &item_t);
item_t *= local.inverse();
if (t_str)
sp_svg_transform_read(t_str, &item_t);
item_t *= local.inverse();
@@ -382,139 +504,326 @@ static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
}
Inkscape::Selection *selection = sp_desktop_selection(desktop);
}
Inkscape::Selection *selection = sp_desktop_selection(desktop);
- selection->set(reprobj);
+
pathRepr->setPosition(-1);
pathRepr->setPosition(-1);
- desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, _("Area filled, path with <b>%d</b> nodes created."), sp_nodes_in_path(SP_PATH(reprobj)));
+ if (union_with_selection) {
+ desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, ngettext("Area filled, path with <b>%d</b> node created and unioned with selection.","Area filled, path with <b>%d</b> nodes created and unioned with selection.",sp_nodes_in_path(SP_PATH(reprobj))), sp_nodes_in_path(SP_PATH(reprobj)));
+ selection->add(reprobj);
+ sp_selected_path_union_skip_undo(desktop);
+ } else {
+ desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, ngettext("Area filled, path with <b>%d</b> node created.","Area filled, path with <b>%d</b> nodes created.",sp_nodes_in_path(SP_PATH(reprobj))), sp_nodes_in_path(SP_PATH(reprobj)));
+ selection->set(reprobj);
+ }
+
}
}
-
+
Inkscape::GC::release(pathRepr);
}
}
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;
-};
-
+/**
+ * \brief The possible return states of perform_bitmap_scanline_check()
+ */
enum ScanlineCheckResult {
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<NR::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci) {
+/**
+ * \brief Determine if the provided coordinates are within the pixel buffer limits.
+ * \param x The X coordinate.
+ * \param y The Y coordinate.
+ * \param bci The bitmap_coords_info structure.
+ */
+inline 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
+
+/**
+ * \brief Paint a pixel or a square (if autogap is enabled) on the trace pixel buffer
+ * \param px The rendered pixel buffer to check.
+ * \param trace_px The trace pixel buffer.
+ * \param orig_color The original selected pixel to use as the fill target color.
+ * \param bci The bitmap_coords_info structure.
+ * \param original_point_trace_t The original pixel in the trace pixel buffer to check.
+ */
+inline 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 (unsigned int ty = bci.y - bci.radius; ty <= bci.y + bci.radius; ty++) {
+ for (unsigned int tx = bci.x - bci.radius; tx <= bci.x + bci.radius; tx++) {
+ if (coords_in_range(tx, ty, bci)) {
+ trace_t = get_trace_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 (tx < bci.x) { can_paint_left = false; }
+ if (tx > bci.x) { can_paint_right = false; }
+ if (ty < bci.y) { can_paint_up = false; }
+ if (ty > bci.y) { 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;
+ }
+}
+
+/**
+ * \brief Push a point to be checked onto the bottom of the rendered pixel buffer check queue.
+ * \param fill_queue The fill queue to add the point to.
+ * \param max_queue_size The maximum size of the fill queue.
+ * \param trace_t The trace pixel buffer pixel.
+ * \param x The X coordinate.
+ * \param y The Y coordinate.
+ */
+static void push_point_onto_queue(std::deque<Geom::Point> *fill_queue, unsigned int max_queue_size, unsigned char *trace_t, unsigned int x, unsigned int y) {
+ if (!is_pixel_queued(trace_t)) {
+ if ((fill_queue->size() < max_queue_size)) {
+ fill_queue->push_back(Geom::Point(x, y));
+ mark_pixel_queued(trace_t);
+ }
+ }
+}
+
+/**
+ * \brief Shift a point to be checked onto the top of the rendered pixel buffer check queue.
+ * \param fill_queue The fill queue to add the point to.
+ * \param max_queue_size The maximum size of the fill queue.
+ * \param trace_t The trace pixel buffer pixel.
+ * \param x The X coordinate.
+ * \param y The Y coordinate.
+ */
+static void shift_point_onto_queue(std::deque<Geom::Point> *fill_queue, unsigned int max_queue_size, unsigned char *trace_t, unsigned int x, unsigned int y) {
+ if (!is_pixel_queued(trace_t)) {
+ if ((fill_queue->size() < max_queue_size)) {
+ fill_queue->push_front(Geom::Point(x, y));
+ mark_pixel_queued(trace_t);
+ }
+ }
+}
+
+/**
+ * \brief Scan a row in the rendered pixel buffer and add points to the fill queue as necessary.
+ * \param fill_queue The fill queue to add the point to.
+ * \param px The rendered pixel buffer.
+ * \param trace_px The trace pixel buffer.
+ * \param orig_color The original selected pixel to use as the fill target color.
+ * \param bci The bitmap_coords_info structure.
+ */
+static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<Geom::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci, unsigned int *min_x, unsigned int *max_x) {
bool aborted = false;
bool reached_screen_boundary = false;
bool ok;
bool aborted = false;
bool reached_screen_boundary = false;
bool ok;
-
+
bool keep_tracing;
bool keep_tracing;
- unsigned char *t, *trace_t;
-
+ bool initial_paint = true;
+
+ unsigned char *current_trace_t = get_trace_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 top_ty = bci.y - 1;
+ unsigned int bottom_ty = bci.y + 1;
+
+ bool can_paint_top = (top_ty > 0);
+ bool can_paint_bottom = (bottom_ty < bci.height);
+
+ Geom::Point t = fill_queue->front();
+
do {
ok = false;
if (bci.is_left) {
do {
ok = false;
if (bci.is_left) {
- keep_tracing = (bci.x >= 0);
+ keep_tracing = (bci.x != 0);
} else {
keep_tracing = (bci.x < bci.width);
}
} else {
keep_tracing = (bci.x < bci.width);
}
-
+
+ *min_x = MIN(*min_x, bci.x);
+ *max_x = MAX(*max_x, bci.x);
+
if (keep_tracing) {
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 ((t[Geom::X] == bci.x) && (t[Geom::Y] == bci.y)) {
+ fill_queue->pop_front(); t = fill_queue->front();
+ }
+ }
+
+ if (can_paint_top) {
+ if (paint_directions & PAINT_DIRECTION_UP) {
+ unsigned char *trace_t = current_trace_t - bci.width;
+ if (!is_pixel_queued(trace_t)) {
+ bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, top_ty, orig_color, bci);
+
+ if (initial_paint) { currently_painting_top = !ok_to_paint; }
+
+ if (ok_to_paint && (!currently_painting_top)) {
+ currently_painting_top = true;
+ push_point_onto_queue(fill_queue, bci.max_queue_size, trace_t, bci.x, top_ty);
+ }
+ 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 (can_paint_bottom) {
+ if (paint_directions & PAINT_DIRECTION_DOWN) {
+ unsigned char *trace_t = current_trace_t + bci.width;
+ if (!is_pixel_queued(trace_t)) {
+ bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, bottom_ty, orig_color, bci);
+
+ if (initial_paint) { currently_painting_bottom = !ok_to_paint; }
+
+ if (ok_to_paint && (!currently_painting_bottom)) {
+ currently_painting_bottom = true;
+ push_point_onto_queue(fill_queue, bci.max_queue_size, trace_t, bci.x, bottom_ty);
+ }
+ if ((!ok_to_paint) && currently_painting_bottom) {
+ currently_painting_bottom = false;
+ }
+ }
+ }
}
}
+
if (bci.is_left) {
if (bci.is_left) {
- bci.x--;
+ if (paint_directions & PAINT_DIRECTION_LEFT) {
+ bci.x--; current_trace_t--;
+ ok = true;
+ }
} else {
} else {
- bci.x++;
+ if (paint_directions & PAINT_DIRECTION_RIGHT) {
+ bci.x++; current_trace_t++;
+ ok = true;
+ }
}
}
- ok = true;
+
+ initial_paint = false;
}
} else {
}
} else {
- if (bci.bbox.min()[NR::X] > bci.screen.min()[NR::X]) {
+ if (bci.bbox.min()[Geom::X] > bci.screen.min()[Geom::X]) {
aborted = true; break;
} else {
reached_screen_boundary = true;
}
}
} while (ok);
aborted = true; break;
} else {
reached_screen_boundary = true;
}
}
} while (ok);
-
+
if (aborted) { return SCANLINE_CHECK_ABORTED; }
if (reached_screen_boundary) { return SCANLINE_CHECK_BOUNDARY; }
return SCANLINE_CHECK_OK;
}
if (aborted) { return SCANLINE_CHECK_ABORTED; }
if (reached_screen_boundary) { return SCANLINE_CHECK_BOUNDARY; }
return SCANLINE_CHECK_OK;
}
-static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event) {
+/**
+ * \brief Sort the rendered pixel buffer check queue vertically.
+ */
+static bool sort_fill_queue_vertical(Geom::Point a, Geom::Point b) {
+ return a[Geom::Y] > b[Geom::Y];
+}
+
+/**
+ * \brief Sort the rendered pixel buffer check queue horizontally.
+ */
+static bool sort_fill_queue_horizontal(Geom::Point a, Geom::Point b) {
+ return a[Geom::X] > b[Geom::X];
+}
+
+/**
+ * \brief Perform a flood fill operation.
+ * \param event_context The event context for this tool.
+ * \param event The details of this event.
+ * \param union_with_selection If true, union the new fill with the current selection.
+ * \param is_point_fill If false, use the Rubberband "touch selection" to get the initial points for the fill.
+ * \param is_touch_fill If true, use only the initial contact point in the Rubberband "touch selection" as the fill target color.
+ */
+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);
/* Create new arena */
NRArena *arena = NRArena::create();
SPDesktop *desktop = event_context->desktop;
SPDocument *document = sp_desktop_document(desktop);
/* Create new arena */
NRArena *arena = NRArena::create();
- unsigned dkey = sp_item_display_key_new(1);
+ unsigned dkey = SPItem::display_key_new(1);
- sp_document_ensure_up_to_date (document);
+ document->ensureUpToDate();
- SPItem *document_root = SP_ITEM(SP_DOCUMENT_ROOT(document));
- NR::Maybe<NR::Rect> bbox = document_root->getBounds(NR::identity());
+ SPItem *document_root = SP_ITEM(document->getRoot());
+ Geom::OptRect bbox = document_root->getBounds(Geom::identity());
if (!bbox) {
if (!bbox) {
- desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
- return;
+ desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
+ return;
}
double zoom_scale = desktop->current_zoom();
}
double zoom_scale = desktop->current_zoom();
+
+ // Render 160% of the physical display to the render pixel buffer, so that available
+ // fill areas off the screen can be included in the fill.
double padding = 1.6;
double padding = 1.6;
- NR::Rect screen = desktop->get_display_area();
+ Geom::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.width() * zoom_scale * padding);
+ unsigned int height = (int)ceil(screen.height() * 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));
+ Geom::Point origin(screen.min()[Geom::X],
+ document->getHeight() - screen.height() - screen.min()[Geom::Y]);
+
+ origin[Geom::X] = origin[Geom::X] + (screen.width() * ((1 - padding) / 2));
+ origin[Geom::Y] = origin[Geom::Y] + (screen.height() * ((1 - padding) / 2));
- NR::scale scale(zoom_scale, zoom_scale);
- NR::Matrix affine = scale * NR::translate(-origin * scale);
+ Geom::Scale scale(zoom_scale, zoom_scale);
+ Geom::Matrix affine = scale * Geom::Translate(-origin * scale);
/* Create ArenaItems and set transform */
/* Create ArenaItems and set transform */
- NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(document)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
+ NRArenaItem *root = SP_ITEM(document->getRoot())->invoke_show( arena, dkey, SP_ITEM_SHOW_DISPLAY);
nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
NRGC gc(NULL);
nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
NRGC gc(NULL);
- nr_matrix_set_identity(&gc.transform);
+ gc.transform.setIdentity();
NRRectL final_bbox;
final_bbox.x0 = 0;
NRRectL final_bbox;
final_bbox.x0 = 0;
- final_bbox.y0 = 0;//row;
+ final_bbox.y0 = 0; //row;
final_bbox.x1 = width;
final_bbox.x1 = width;
- final_bbox.y1 = height;//row + num_rows;
+ final_bbox.y1 = height; //row + num_rows;
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);
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,
NRPixBlock B;
nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
@@ -530,169 +839,311 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
dtc[2] = NR_RGBA32_B(bgcolor);
dtc[3] = NR_RGBA32_A(bgcolor);
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 );
nr_pixblock_release(&B);
// Hide items
}
nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
nr_pixblock_release(&B);
// Hide items
- sp_item_invoke_hide(SP_ITEM(sp_document_root(document)), dkey);
+ SP_ITEM(document->getRoot())->invoke_hide(dkey);
- nr_arena_item_unref(root);
nr_object_unref((NRObject *) arena);
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;
+ guchar *trace_px = g_new(guchar, width * height);
+ memset(trace_px, 0x00, width * height);
- 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::deque<Geom::Point> fill_queue;
+ std::queue<Geom::Point> color_queue;
- std::queue<NR::Point> fill_queue;
- fill_queue.push(pw);
+ std::vector<Geom::Point> fill_points;
bool aborted = false;
int y_limit = height - 1;
bool aborted = false;
int y_limit = height - 1;
-
- 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]; }
-
- unsigned char merged_orig[4];
-
- 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);
- bool reached_screen_boundary = false;
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ PaintBucketChannels method = (PaintBucketChannels) prefs->getInt("/tools/paintbucket/channels", 0);
+ int threshold = prefs->getIntLimited("/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;
+ }
bitmap_coords_info bci;
bci.y_limit = y_limit;
bci.width = width;
bitmap_coords_info bci;
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.method = method;
bci.bbox = *bbox;
bci.screen = screen;
bci.dtc = dtc;
+ bci.radius = prefs->getIntLimited("/tools/paintbucket/autogap", 0, 0, 3);
+ bci.max_queue_size = (width * height) / 4;
+ bci.current_step = 0;
+
+ if (is_point_fill) {
+ fill_points.push_back(Geom::Point(event->button.x, event->button.y));
+ } else {
+ Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
+ fill_points = r->getPoints();
+ }
- 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;
-
- 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);
+ for (unsigned int i = 0; i < fill_points.size(); i++) {
+ Geom::Point pw = Geom::Point(fill_points[i][Geom::X] / zoom_scale, document->getHeight() + (fill_points[i][Geom::Y] / zoom_scale)) * affine;
+
+ pw[Geom::X] = (int)MIN(width - 1, MAX(0, pw[Geom::X]));
+ pw[Geom::Y] = (int)MIN(height - 1, MAX(0, pw[Geom::Y]));
+
+ if (is_touch_fill) {
+ if (i == 0) {
+ color_queue.push(pw);
+ } else {
+ unsigned char *trace_t = get_trace_pixel(trace_px, (int)pw[Geom::X], (int)pw[Geom::Y], width);
+ push_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, (int)pw[Geom::X], (int)pw[Geom::Y]);
+ }
} else {
} else {
- if (bbox->max()[NR::Y] < screen.max()[NR::Y]) {
- aborted = true; break;
- } else {
- reached_screen_boundary = true;
- }
+ color_queue.push(pw);
}
}
-
- bci.is_left = true;
- bci.x = x;
- bci.y = y;
- bci.top_fill = top_fill;
- bci.bottom_fill = bottom_fill;
-
- 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;
+ }
+
+ bool reached_screen_boundary = false;
+
+ bool first_run = true;
+
+ unsigned long sort_size_threshold = 5;
+
+ unsigned int min_y = height;
+ unsigned int max_y = 0;
+ unsigned int min_x = width;
+ unsigned int max_x = 0;
+
+ while (!color_queue.empty() && !aborted) {
+ Geom::Point color_point = color_queue.front();
+ color_queue.pop();
+
+ int cx = (int)color_point[Geom::X];
+ int cy = (int)color_point[Geom::Y];
+
+ unsigned char *orig_px = get_pixel(px, cx, cy, width);
+ unsigned char orig_color[4];
+ for (int i = 0; i < 4; i++) { orig_color[i] = orig_px[i]; }
+
+ unsigned char merged_orig[3];
+
+ merge_pixel_with_background(orig_color, dtc, merged_orig);
+
+ bci.merged_orig_pixel = merged_orig;
+
+ unsigned char *trace_t = get_trace_pixel(trace_px, cx, cy, width);
+ if (!is_pixel_checked(trace_t) && !is_pixel_colored(trace_t)) {
+ if (check_if_pixel_is_paintable(px, trace_px, cx, cy, orig_color, bci)) {
+ shift_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, cx, cy);
+
+ if (!first_run) {
+ for (unsigned int y = 0; y < height; y++) {
+ trace_t = get_trace_pixel(trace_px, 0, y, width);
+ for (unsigned int x = 0; x < width; x++) {
+ clear_pixel_paintability(trace_t);
+ trace_t++;
+ }
+ }
+ }
+ first_run = false;
+ }
}
}
-
- bci.is_left = false;
- bci.x = x + 1;
- bci.y = y;
- bci.top_fill = top_fill;
- bci.bottom_fill = bottom_fill;
-
- result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
-
- switch (result) {
- case SCANLINE_CHECK_ABORTED:
+
+ unsigned long old_fill_queue_size = fill_queue.size();
+
+ while (!fill_queue.empty() && !aborted) {
+ Geom::Point cp = fill_queue.front();
+
+ if (bci.radius == 0) {
+ unsigned long new_fill_queue_size = fill_queue.size();
+
+ /*
+ * To reduce the number of points in the fill queue, periodically
+ * resort all of the points in the queue so that scanline checks
+ * can complete more quickly. A point cannot be checked twice
+ * in a normal scanline checks, so forcing scanline checks to start
+ * from one corner of the rendered area as often as possible
+ * will reduce the number of points that need to be checked and queued.
+ */
+ if (new_fill_queue_size > sort_size_threshold) {
+ if (new_fill_queue_size > old_fill_queue_size) {
+ std::sort(fill_queue.begin(), fill_queue.end(), sort_fill_queue_vertical);
+
+ std::deque<Geom::Point>::iterator start_sort = fill_queue.begin();
+ std::deque<Geom::Point>::iterator end_sort = fill_queue.begin();
+ unsigned int sort_y = (unsigned int)cp[Geom::Y];
+ unsigned int current_y = sort_y;
+
+ for (std::deque<Geom::Point>::iterator i = fill_queue.begin(); i != fill_queue.end(); i++) {
+ Geom::Point current = *i;
+ current_y = (unsigned int)current[Geom::Y];
+ if (current_y != sort_y) {
+ if (start_sort != end_sort) {
+ std::sort(start_sort, end_sort, sort_fill_queue_horizontal);
+ }
+ sort_y = current_y;
+ start_sort = i;
+ }
+ end_sort = i;
+ }
+ if (start_sort != end_sort) {
+ std::sort(start_sort, end_sort, sort_fill_queue_horizontal);
+ }
+
+ cp = fill_queue.front();
+ }
+ }
+
+ old_fill_queue_size = new_fill_queue_size;
+ }
+
+ fill_queue.pop_front();
+
+ int x = (int)cp[Geom::X];
+ int y = (int)cp[Geom::Y];
+
+ min_y = MIN((unsigned int)y, min_y);
+ max_y = MAX((unsigned int)y, max_y);
+
+ unsigned char *trace_t = get_trace_pixel(trace_px, x, y, width);
+ if (!is_pixel_checked(trace_t)) {
+ mark_pixel_checked(trace_t);
+
+ if (y == 0) {
+ if (bbox->min()[Geom::Y] > screen.min()[Geom::Y]) {
+ aborted = true; break;
+ } else {
+ reached_screen_boundary = true;
+ }
+ }
+
+ if (y == y_limit) {
+ if (bbox->max()[Geom::Y] < screen.max()[Geom::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, &min_x, &max_x);
+
+ 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++;
+ 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, &min_x, &max_x);
+
+ switch (result) {
+ case SCANLINE_CHECK_ABORTED:
+ aborted = true;
+ break;
+ case SCANLINE_CHECK_BOUNDARY:
+ reached_screen_boundary = true;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ }
+
+ bci.current_step++;
+
+ if (bci.current_step > bci.max_queue_size) {
aborted = true;
aborted = true;
- break;
- case SCANLINE_CHECK_BOUNDARY:
- reached_screen_boundary = true;
- break;
- default:
- break;
+ }
}
}
- }
}
g_free(px);
if (aborted) {
}
g_free(px);
if (aborted) {
- g_free(trace_px);
- desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
- return;
+ g_free(trace_px);
+ desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
+ return;
}
if (reached_screen_boundary) {
}
if (reached_screen_boundary) {
- desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Only the visible part of the bounded area was filled.</b> If you want to fill all of the area, undo, zoom out, and fill again."));
+ desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Only the visible part of the bounded area was filled.</b> If you want to fill all of the area, undo, zoom out, and fill again."));
}
}
+
+ unsigned int trace_padding = bci.radius + 1;
+ if (min_y > trace_padding) { min_y -= trace_padding; }
+ if (max_y < (y_limit - trace_padding)) { max_y += trace_padding; }
+ if (min_x > trace_padding) { min_x -= trace_padding; }
+ if (max_x < (width - 1 - trace_padding)) { max_x += trace_padding; }
+
+ Geom::Point min_start = Geom::Point(min_x, min_y);
- GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(trace_px,
- GDK_COLORSPACE_RGB,
- TRUE,
- 8, width, height, width * 4,
- (GdkPixbufDestroyNotify)g_free,
- NULL);
-
- NR::Matrix inverted_affine = NR::Matrix(affine).inverse();
+ affine = scale * Geom::Translate(-origin * scale - min_start);
+ Geom::Matrix inverted_affine = Geom::Matrix(affine).inverse();
- do_trace(pixbuf, desktop, inverted_affine);
+ do_trace(bci, trace_px, desktop, inverted_affine, min_x, max_x, min_y, max_y, union_with_selection);
g_free(trace_px);
g_free(trace_px);
- sp_document_done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
+ DocumentUndo::done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
}
static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
{
gint ret = FALSE;
}
static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
{
gint ret = FALSE;
+ SPDesktop *desktop = event_context->desktop;
+
switch (event->type) {
case GDK_BUTTON_PRESS:
switch (event->type) {
case GDK_BUTTON_PRESS:
+ if ((event->button.state & GDK_CONTROL_MASK) && event->button.button == 1 && !event_context->space_panning) {
+ Geom::Point const button_w(event->button.x,
+ event->button.y);
+
+ SPItem *item = sp_event_context_find_item (desktop, button_w, TRUE, TRUE);
+
+ // Set style
+ desktop->applyCurrentOrToolStyle(item, "/tools/paintbucket", false);
+ DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET, _("Set style on object"));
+ ret = TRUE;
+ }
break;
break;
- // motion and release are always on root (why?)
default:
break;
}
default:
break;
}
@@ -706,13 +1157,84 @@ static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem
static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event)
{
static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event)
{
+ static bool dragging;
+
gint ret = FALSE;
gint ret = FALSE;
+ SPDesktop *desktop = event_context->desktop;
+
switch (event->type) {
case GDK_BUTTON_PRESS:
switch (event->type) {
case GDK_BUTTON_PRESS:
- if ( event->button.button == 1 ) {
- sp_flood_do_flood_fill(event_context, event);
+ if (event->button.button == 1 && !event_context->space_panning) {
+ if (!(event->button.state & GDK_CONTROL_MASK)) {
+ Geom::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[Geom::X];
+ event_context->yp = (gint) button_w[Geom::Y];
+ event_context->within_tolerance = true;
+
+ dragging = true;
+
+ Geom::Point const p(desktop->w2d(button_w));
+ Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH);
+ Inkscape::Rubberband::get(desktop)->start(desktop, p);
+ }
+ }
+ }
+ case GDK_MOTION_NOTIFY:
+ if ( dragging
+ && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning)
+ {
+ 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;
+
+ Geom::Point const motion_pt(event->motion.x, event->motion.y);
+ Geom::Point const p(desktop->w2d(motion_pt));
+ if (Inkscape::Rubberband::get(desktop)->is_started()) {
+ Inkscape::Rubberband::get(desktop)->move(p);
+ event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> areas to add to fill, hold <b>Alt</b> for touch fill"));
+ gobble_motion_events(GDK_BUTTON1_MASK);
+ }
+ }
+ break;
- ret = TRUE;
+ case GDK_BUTTON_RELEASE:
+ if (event->button.button == 1 && !event_context->space_panning) {
+ Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
+ 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;
+ }
+
+ r->stop();
+
+ if (SP_IS_EVENT_CONTEXT(event_context)) {
+ event_context->defaultMessageContext()->clear();
+ }
+ }
}
break;
case GDK_KEY_PRESS:
}
break;
case GDK_KEY_PRESS:
sp_canvas_end_forced_full_redraws(desktop->canvas);
sp_desktop_selection(desktop)->set(rc->item);
sp_canvas_end_forced_full_redraws(desktop->canvas);
sp_desktop_selection(desktop)->set(rc->item);
- sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
- _("Fill bounded area"));
+ DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
+ _("Fill bounded area"));
rc->item = NULL;
}
}
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));
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ prefs->setInt("/tools/paintbucket/channels", channels);
}
/*
}
/*
fill-column:99
End:
*/
fill-column:99
End:
*/
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :