flood-context.cpp revision 70a4afb4cd8683415fa1c7fcda611b4dd81c4a56
469N/A#define __SP_FLOOD_CONTEXT_C__
469N/A
469N/A/*
469N/A * Flood fill drawing context
469N/A *
469N/A * Author:
469N/A * Lauris Kaplinski <lauris@kaplinski.com>
469N/A * bulia byak <buliabyak@users.sf.net>
469N/A * John Bintz <jcoswell@coswellproductions.org>
469N/A *
469N/A * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
469N/A * Copyright (C) 2000-2005 authors
469N/A * Copyright (C) 2000-2001 Ximian, Inc.
469N/A *
469N/A * Released under GNU GPL, read the file 'COPYING' for more information
469N/A */
469N/A
469N/A#include "config.h"
469N/A
469N/A#include <gdk/gdkkeysyms.h>
469N/A#include <queue>
469N/A
469N/A#include "macros.h"
469N/A#include "display/sp-canvas.h"
469N/A#include "document.h"
1068N/A#include "sp-namedview.h"
1068N/A#include "sp-object.h"
1068N/A#include "sp-rect.h"
1068N/A#include "selection.h"
1068N/A#include "selection-chemistry.h"
1068N/A#include "desktop-handles.h"
1068N/A#include "snap.h"
1068N/A#include "desktop.h"
1068N/A#include "desktop-style.h"
1068N/A#include "message-stack.h"
1068N/A#include "message-context.h"
1068N/A#include "pixmaps/cursor-paintbucket.xpm"
1068N/A#include "flood-context.h"
1068N/A#include "sp-metrics.h"
1068N/A#include <glibmm/i18n.h>
1068N/A#include "object-edit.h"
1068N/A#include "xml/repr.h"
1068N/A#include "xml/node-event-vector.h"
1068N/A#include "prefs-utils.h"
1068N/A#include "context-fns.h"
1068N/A
1068N/A#include "display/nr-arena-item.h"
1068N/A#include "display/nr-arena.h"
1068N/A#include "display/nr-arena-image.h"
469N/A#include "display/canvas-arena.h"
469N/A#include "libnr/nr-pixops.h"
469N/A#include "libnr/nr-matrix-rotate-ops.h"
469N/A#include "libnr/nr-matrix-translate-ops.h"
469N/A#include "libnr/nr-rotate-fns.h"
469N/A#include "libnr/nr-scale-ops.h"
1068N/A#include "libnr/nr-scale-translate-ops.h"
1068N/A#include "libnr/nr-translate-matrix-ops.h"
1068N/A#include "libnr/nr-translate-scale-ops.h"
1068N/A#include "libnr/nr-matrix-ops.h"
469N/A#include "sp-item.h"
469N/A#include "sp-root.h"
469N/A#include "sp-defs.h"
469N/A#include "splivarot.h"
469N/A#include "livarot/Path.h"
469N/A#include "livarot/Shape.h"
469N/A#include "libnr/n-art-bpath.h"
469N/A#include "svg/svg.h"
469N/A
469N/A#include "trace/trace.h"
469N/A#include "trace/potrace/inkscape-potrace.h"
469N/A
469N/Astatic void sp_flood_context_class_init(SPFloodContextClass *klass);
469N/Astatic void sp_flood_context_init(SPFloodContext *flood_context);
469N/Astatic void sp_flood_context_dispose(GObject *object);
469N/A
469N/Astatic void sp_flood_context_setup(SPEventContext *ec);
469N/A
469N/Astatic gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event);
469N/Astatic gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
469N/A
469N/Astatic void sp_flood_finish(SPFloodContext *rc);
469N/A
469N/Astatic SPEventContextClass *parent_class;
469N/A
469N/A
469N/AGtkType sp_flood_context_get_type()
469N/A{
469N/A static GType type = 0;
469N/A if (!type) {
469N/A GTypeInfo info = {
469N/A sizeof(SPFloodContextClass),
469N/A NULL, NULL,
469N/A (GClassInitFunc) sp_flood_context_class_init,
469N/A NULL, NULL,
469N/A sizeof(SPFloodContext),
469N/A 4,
469N/A (GInstanceInitFunc) sp_flood_context_init,
469N/A NULL, /* value_table */
469N/A };
469N/A type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPFloodContext", &info, (GTypeFlags) 0);
469N/A }
469N/A return type;
469N/A}
469N/A
469N/Astatic void sp_flood_context_class_init(SPFloodContextClass *klass)
469N/A{
469N/A GObjectClass *object_class = (GObjectClass *) klass;
469N/A SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
469N/A
469N/A parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
469N/A
469N/A object_class->dispose = sp_flood_context_dispose;
469N/A
469N/A event_context_class->setup = sp_flood_context_setup;
469N/A event_context_class->root_handler = sp_flood_context_root_handler;
469N/A event_context_class->item_handler = sp_flood_context_item_handler;
469N/A}
469N/A
469N/Astatic void sp_flood_context_init(SPFloodContext *flood_context)
469N/A{
469N/A SPEventContext *event_context = SP_EVENT_CONTEXT(flood_context);
469N/A
469N/A event_context->cursor_shape = cursor_paintbucket_xpm;
469N/A event_context->hot_x = 11;
469N/A event_context->hot_y = 30;
469N/A event_context->xp = 0;
469N/A event_context->yp = 0;
469N/A event_context->tolerance = 0;
469N/A event_context->within_tolerance = false;
469N/A event_context->item_to_select = NULL;
469N/A
469N/A event_context->shape_repr = NULL;
469N/A event_context->shape_knot_holder = NULL;
469N/A
469N/A flood_context->item = NULL;
469N/A
469N/A new (&flood_context->sel_changed_connection) sigc::connection();
469N/A}
469N/A
469N/Astatic void sp_flood_context_dispose(GObject *object)
469N/A{
469N/A SPFloodContext *rc = SP_FLOOD_CONTEXT(object);
469N/A SPEventContext *ec = SP_EVENT_CONTEXT(object);
469N/A
469N/A rc->sel_changed_connection.disconnect();
469N/A rc->sel_changed_connection.~connection();
469N/A
469N/A /* fixme: This is necessary because we do not grab */
469N/A if (rc->item) {
469N/A sp_flood_finish(rc);
469N/A }
469N/A
469N/A if (ec->shape_repr) { // remove old listener
469N/A sp_repr_remove_listener_by_data(ec->shape_repr, ec);
469N/A Inkscape::GC::release(ec->shape_repr);
469N/A ec->shape_repr = 0;
469N/A }
469N/A
469N/A if (rc->_message_context) {
469N/A delete rc->_message_context;
469N/A }
469N/A
469N/A G_OBJECT_CLASS(parent_class)->dispose(object);
469N/A}
469N/A
469N/Astatic Inkscape::XML::NodeEventVector ec_shape_repr_events = {
469N/A NULL, /* child_added */
469N/A NULL, /* child_removed */
469N/A ec_shape_event_attr_changed,
469N/A NULL, /* content_changed */
469N/A NULL /* order_changed */
469N/A};
469N/A
469N/A/**
469N/A\brief Callback that processes the "changed" signal on the selection;
469N/Adestroys old and creates new knotholder
469N/A*/
469N/Avoid sp_flood_context_selection_changed(Inkscape::Selection *selection, gpointer data)
469N/A{
469N/A SPFloodContext *rc = SP_FLOOD_CONTEXT(data);
469N/A SPEventContext *ec = SP_EVENT_CONTEXT(rc);
469N/A
469N/A if (ec->shape_repr) { // remove old listener
469N/A sp_repr_remove_listener_by_data(ec->shape_repr, ec);
469N/A Inkscape::GC::release(ec->shape_repr);
469N/A ec->shape_repr = 0;
469N/A }
469N/A
469N/A SPItem *item = selection->singleItem();
469N/A if (item) {
469N/A Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
469N/A if (shape_repr) {
469N/A ec->shape_repr = shape_repr;
469N/A Inkscape::GC::anchor(shape_repr);
469N/A sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
469N/A }
469N/A }
469N/A}
469N/A
469N/Astatic void sp_flood_context_setup(SPEventContext *ec)
469N/A{
469N/A SPFloodContext *rc = SP_FLOOD_CONTEXT(ec);
469N/A
469N/A if (((SPEventContextClass *) parent_class)->setup) {
469N/A ((SPEventContextClass *) parent_class)->setup(ec);
469N/A }
469N/A
469N/A SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
469N/A if (item) {
469N/A Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
469N/A if (shape_repr) {
469N/A ec->shape_repr = shape_repr;
469N/A Inkscape::GC::anchor(shape_repr);
469N/A sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
469N/A }
469N/A }
469N/A
469N/A rc->sel_changed_connection.disconnect();
469N/A rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
469N/A sigc::bind(sigc::ptr_fun(&sp_flood_context_selection_changed), (gpointer)rc)
469N/A );
469N/A
469N/A rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
469N/A}
469N/A
469N/Astatic void merge_pixel_with_background(unsigned char *orig, unsigned char *bg, unsigned char *base) {
469N/A for (int i = 0; i < 3; i++) {
469N/A base[i] = (255 * (255 - bg[3])) / 255 + (bg[i] * bg[3]) / 255;
469N/A base[i] = (base[i] * (255 - orig[3])) / 255 + (orig[i] * orig[3]) / 255;
469N/A }
469N/A base[3] = 255;
469N/A}
469N/A
469N/Ainline unsigned char * get_pixel(guchar *px, int x, int y, int width) {
469N/A return px + (x + y * width) * 4;
469N/A}
469N/A
469N/Aenum PaintBucketChannels {
469N/A FLOOD_CHANNELS_RGB,
469N/A FLOOD_CHANNELS_ALPHA
469N/A};
469N/A
469N/AGList * flood_channels_dropdown_items_list() {
469N/A GList *glist = NULL;
469N/A
469N/A glist = g_list_append (glist, _("Visible Colors"));
469N/A glist = g_list_append (glist, _("Alpha"));
469N/A
469N/A return glist;
469N/A}
469N/A
469N/Astatic bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *dtc, int tolerance, PaintBucketChannels method) {
469N/A int diff = 0;
469N/A
469N/A switch (method) {
469N/A case FLOOD_CHANNELS_ALPHA:
469N/A return ((int)abs(check[3] - orig[3]) <= (tolerance / 4));
469N/A case FLOOD_CHANNELS_RGB:
469N/A unsigned char merged_orig[4];
469N/A unsigned char merged_check[4];
469N/A
469N/A merge_pixel_with_background(orig, dtc, merged_orig);
469N/A merge_pixel_with_background(check, dtc, merged_check);
469N/A
469N/A for (int i = 0; i < 3; i++) {
469N/A diff += (int)abs(merged_check[i] - merged_orig[i]);
469N/A }
469N/A return ((diff / 3) <= ((tolerance * 3) / 4));
469N/A }
469N/A
469N/A return false;
469N/A}
469N/A
469N/Astatic 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) {
469N/A unsigned char *t = get_pixel(px, x, y, width);
469N/A if (compare_pixels(t, orig, dtc, tolerance, method)) {
469N/A unsigned char *trace_t = get_pixel(trace_px, x, y, width);
469N/A if (trace_t[3] != 255) {
469N/A if (fill_switch) {
469N/A fill_queue->push(NR::Point(x, y));
469N/A }
469N/A }
469N/A return false;
469N/A }
469N/A return true;
469N/A}
469N/A
469N/Astatic void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
469N/A SPDocument *document = sp_desktop_document(desktop);
469N/A
469N/A Inkscape::Trace::Potrace::PotraceTracingEngine pte;
469N/A
469N/A pte.setTraceType(Inkscape::Trace::Potrace::TRACE_BRIGHTNESS);
469N/A pte.setInvert(false);
469N/A
469N/A Glib::RefPtr<Gdk::Pixbuf> pixbuf = Glib::wrap(px, true);
469N/A
469N/A std::vector<Inkscape::Trace::TracingEngineResult> results = pte.trace(pixbuf);
469N/A
469N/A Inkscape::XML::Node *layer_repr = SP_GROUP(desktop->currentLayer())->repr;
469N/A Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
469N/A
469N/A long totalNodeCount = 0L;
469N/A
469N/A double offset = prefs_get_double_attribute_limited("tools.paintbucket", "offset", 1.5, 0.0, 2.0);
469N/A
469N/A for (unsigned int i=0 ; i<results.size() ; i++) {
469N/A Inkscape::Trace::TracingEngineResult result = results[i];
469N/A totalNodeCount += result.getNodeCount();
469N/A
469N/A Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
469N/A /* Set style */
469N/A sp_desktop_apply_style_tool (desktop, pathRepr, "tools.paintbucket", false);
469N/A
469N/A NArtBpath *bpath = sp_svg_read_path(result.getPathData().c_str());
469N/A Path *path = bpath_to_Path(bpath);
469N/A g_free(bpath);
469N/A
469N/A Shape *path_shape = new Shape();
469N/A
469N/A path->ConvertWithBackData(0.03);
469N/A path->Fill(path_shape, 0);
469N/A delete path;
469N/A
469N/A Shape *expanded_path_shape = new Shape();
469N/A
469N/A expanded_path_shape->ConvertToShape(path_shape, fill_nonZero);
469N/A path_shape->MakeOffset(expanded_path_shape, offset, join_round, 4);
469N/A expanded_path_shape->ConvertToShape(path_shape, fill_positive);
469N/A
469N/A Path *expanded_path = new Path();
469N/A
469N/A expanded_path->Reset();
469N/A expanded_path_shape->ConvertToForme(expanded_path);
469N/A expanded_path->ConvertEvenLines(1.0);
469N/A expanded_path->Simplify(1.0);
469N/A
469N/A delete path_shape;
469N/A delete expanded_path_shape;
469N/A
469N/A gchar *str = expanded_path->svg_dump_path();
469N/A delete expanded_path;
469N/A pathRepr->setAttribute("d", str);
469N/A g_free(str);
469N/A
469N/A layer_repr->addChild(pathRepr, NULL);
469N/A
469N/A SPObject *reprobj = document->getObjectByRepr(pathRepr);
469N/A if (reprobj) {
469N/A sp_item_write_transform(SP_ITEM(reprobj), pathRepr, transform, NULL);
469N/A
469N/A // premultiply the item transform by the accumulated parent transform in the paste layer
469N/A NR::Matrix local = sp_item_i2doc_affine(SP_GROUP(desktop->currentLayer()));
469N/A if (!local.test_identity()) {
469N/A gchar const *t_str = pathRepr->attribute("transform");
469N/A NR::Matrix item_t (NR::identity());
469N/A if (t_str)
469N/A sp_svg_transform_read(t_str, &item_t);
469N/A item_t *= local.inverse();
469N/A // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
469N/A gchar *affinestr=sp_svg_transform_write(item_t);
469N/A pathRepr->setAttribute("transform", affinestr);
469N/A g_free(affinestr);
469N/A }
469N/A
469N/A Inkscape::Selection *selection = sp_desktop_selection(desktop);
469N/A selection->set(reprobj);
469N/A pathRepr->setPosition(-1);
469N/A }
469N/A
469N/A Inkscape::GC::release(pathRepr);
469N/A }
469N/A}
469N/A
469N/Astruct bitmap_coords_info {
469N/A bool is_left;
469N/A int x;
469N/A int y;
469N/A int y_limit;
469N/A int width;
469N/A int tolerance;
469N/A PaintBucketChannels method;
469N/A unsigned char *dtc;
469N/A bool top_fill;
469N/A bool bottom_fill;
469N/A NR::Rect bbox;
469N/A NR::Rect screen;
469N/A};
469N/A
469N/Aenum ScanlineCheckResult {
469N/A SCANLINE_CHECK_OK,
469N/A SCANLINE_CHECK_ABORTED,
469N/A SCANLINE_CHECK_BOUNDARY
469N/A};
469N/A
469N/Astatic ScanlineCheckResult perform_bitmap_scanline_check(std::queue<NR::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci) {
469N/A bool aborted = false;
469N/A bool reached_screen_boundary = false;
469N/A bool ok;
469N/A
469N/A bool keep_tracing;
469N/A unsigned char *t, *trace_t;
469N/A
469N/A do {
469N/A ok = false;
469N/A if (bci.is_left) {
469N/A keep_tracing = (bci.x >= 0);
469N/A } else {
469N/A keep_tracing = (bci.x < bci.width);
469N/A }
469N/A
469N/A if (keep_tracing) {
469N/A t = get_pixel(px, bci.x, bci.y, bci.width);
469N/A if (compare_pixels(t, orig_color, bci.dtc, bci.tolerance, bci.method)) {
469N/A for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; }
469N/A trace_t = get_pixel(trace_px, bci.x, bci.y, bci.width);
469N/A trace_t[3] = 255;
469N/A if (bci.y > 0) {
469N/A 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);
469N/A }
469N/A if (bci.y < bci.y_limit) {
469N/A 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);
469N/A }
469N/A if (bci.is_left) {
469N/A bci.x--;
469N/A } else {
469N/A bci.x++;
469N/A }
469N/A ok = true;
469N/A }
469N/A } else {
469N/A if (bci.bbox.min()[NR::X] > bci.screen.min()[NR::X]) {
469N/A aborted = true; break;
469N/A } else {
469N/A reached_screen_boundary = true;
469N/A }
469N/A }
469N/A } while (ok);
469N/A
469N/A if (aborted) { return SCANLINE_CHECK_ABORTED; }
469N/A if (reached_screen_boundary) { return SCANLINE_CHECK_BOUNDARY; }
469N/A return SCANLINE_CHECK_OK;
469N/A}
469N/A
469N/Astatic void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event) {
469N/A SPDesktop *desktop = event_context->desktop;
469N/A SPDocument *document = sp_desktop_document(desktop);
469N/A
469N/A /* Create new arena */
469N/A NRArena *arena = NRArena::create();
469N/A unsigned dkey = sp_item_display_key_new(1);
469N/A
469N/A sp_document_ensure_up_to_date (document);
469N/A
469N/A SPItem *document_root = SP_ITEM(SP_DOCUMENT_ROOT(document));
469N/A NR::Maybe<NR::Rect> bbox = document_root->getBounds(NR::identity());
469N/A
469N/A if (!bbox) {
469N/A desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
469N/A return;
469N/A }
469N/A
469N/A double zoom_scale = desktop->current_zoom();
469N/A double padding = 1.6;
469N/A
469N/A NR::Rect screen = desktop->get_display_area();
469N/A
469N/A int width = (int)ceil(screen.extent(NR::X) * zoom_scale * padding);
469N/A int height = (int)ceil(screen.extent(NR::Y) * zoom_scale * padding);
469N/A
469N/A NR::Point origin(screen.min()[NR::X],
469N/A sp_document_height(document) - screen.extent(NR::Y) - screen.min()[NR::Y]);
469N/A
469N/A origin[NR::X] = origin[NR::X] + (screen.extent(NR::X) * ((1 - padding) / 2));
469N/A origin[NR::Y] = origin[NR::Y] + (screen.extent(NR::Y) * ((1 - padding) / 2));
469N/A
469N/A NR::scale scale(zoom_scale, zoom_scale);
469N/A NR::Matrix affine = scale * NR::translate(-origin * scale);
469N/A
469N/A /* Create ArenaItems and set transform */
469N/A NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(document)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
469N/A nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
469N/A
469N/A NRGC gc(NULL);
469N/A nr_matrix_set_identity(&gc.transform);
469N/A
469N/A NRRectL final_bbox;
469N/A final_bbox.x0 = 0;
469N/A final_bbox.y0 = 0;//row;
469N/A final_bbox.x1 = width;
469N/A final_bbox.y1 = height;//row + num_rows;
469N/A
469N/A nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
469N/A
469N/A guchar *px = g_new(guchar, 4 * width * height);
469N/A //memset(px, 0x00, 4 * width * height);
469N/A
469N/A NRPixBlock B;
469N/A nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
469N/A final_bbox.x0, final_bbox.y0, final_bbox.x1, final_bbox.y1,
469N/A px, 4 * width, FALSE, FALSE );
469N/A
469N/A SPNamedView *nv = sp_desktop_namedview(desktop);
469N/A unsigned long bgcolor = nv->pagecolor;
469N/A
469N/A unsigned char dtc[4];
469N/A dtc[0] = NR_RGBA32_R(bgcolor);
469N/A dtc[1] = NR_RGBA32_G(bgcolor);
469N/A dtc[2] = NR_RGBA32_B(bgcolor);
469N/A dtc[3] = NR_RGBA32_A(bgcolor);
469N/A
469N/A for (int fy = 0; fy < height; fy++) {
469N/A guchar *p = NR_PIXBLOCK_PX(&B) + fy * B.rs;
469N/A for (int fx = 0; fx < width; fx++) {
469N/A for (int i = 0; i < 4; i++) {
469N/A *p++ = dtc[i];
469N/A }
469N/A }
469N/A }
469N/A
469N/A nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
469N/A nr_pixblock_release(&B);
469N/A
469N/A // Hide items
469N/A sp_item_invoke_hide(SP_ITEM(sp_document_root(document)), dkey);
469N/A
469N/A nr_arena_item_unref(root);
469N/A nr_object_unref((NRObject *) arena);
469N/A
469N/A NR::Point pw = NR::Point(event->button.x / zoom_scale, sp_document_height(document) + (event->button.y / zoom_scale)) * affine;
469N/A
469N/A pw[NR::X] = (int)MIN(width - 1, MAX(0, pw[NR::X]));
469N/A pw[NR::Y] = (int)MIN(height - 1, MAX(0, pw[NR::Y]));
469N/A
469N/A guchar *trace_px = g_new(guchar, 4 * width * height);
469N/A memset(trace_px, 0x00, 4 * width * height);
469N/A
469N/A std::queue<NR::Point> fill_queue;
469N/A fill_queue.push(pw);
469N/A
469N/A bool aborted = false;
469N/A int y_limit = height - 1;
469N/A
469N/A unsigned char orig_color[4];
469N/A unsigned char *orig_px = get_pixel(px, (int)pw[NR::X], (int)pw[NR::Y], width);
469N/A for (int i = 0; i < 4; i++) { orig_color[i] = orig_px[i]; }
469N/A
469N/A unsigned char merged_orig[4];
469N/A
469N/A merge_pixel_with_background(orig_color, dtc, merged_orig);
469N/A
469N/A int tolerance = (255 * prefs_get_int_attribute_limited("tools.paintbucket", "tolerance", 1, 0, 100)) / 100;
469N/A PaintBucketChannels method = (PaintBucketChannels)prefs_get_int_attribute("tools.paintbucket", "channels", 0);
469N/A
469N/A bool reached_screen_boundary = false;
469N/A
469N/A bitmap_coords_info bci;
469N/A
469N/A bci.y_limit = y_limit;
469N/A bci.width = width;
469N/A bci.tolerance = tolerance;
469N/A bci.method = method;
469N/A bci.bbox = *bbox;
469N/A bci.screen = screen;
469N/A bci.dtc = dtc;
469N/A
469N/A while (!fill_queue.empty() && !aborted) {
469N/A NR::Point cp = fill_queue.front();
469N/A fill_queue.pop();
469N/A unsigned char *s = get_pixel(px, (int)cp[NR::X], (int)cp[NR::Y], width);
469N/A
469N/A // same color at this point
469N/A if (compare_pixels(s, orig_color, dtc, tolerance, method)) {
469N/A int x = (int)cp[NR::X];
469N/A int y = (int)cp[NR::Y];
469N/A
469N/A bool top_fill = true;
469N/A bool bottom_fill = true;
469N/A
469N/A if (y > 0) {
469N/A top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y - 1, width, tolerance, method, top_fill);
469N/A } else {
469N/A if (bbox->min()[NR::Y] > screen.min()[NR::Y]) {
469N/A aborted = true; break;
469N/A } else {
469N/A reached_screen_boundary = true;
469N/A }
469N/A }
469N/A if (y < y_limit) {
469N/A bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y + 1, width, tolerance, method, bottom_fill);
469N/A } else {
469N/A if (bbox->max()[NR::Y] < screen.max()[NR::Y]) {
469N/A aborted = true; break;
469N/A } else {
469N/A reached_screen_boundary = true;
469N/A }
469N/A }
469N/A
469N/A bci.is_left = true;
469N/A bci.x = x;
469N/A bci.y = y;
469N/A bci.top_fill = top_fill;
469N/A bci.bottom_fill = bottom_fill;
469N/A
469N/A ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
469N/A
469N/A switch (result) {
469N/A case SCANLINE_CHECK_ABORTED:
469N/A aborted = true;
469N/A break;
469N/A case SCANLINE_CHECK_BOUNDARY:
469N/A reached_screen_boundary = true;
469N/A break;
469N/A default:
469N/A break;
469N/A }
469N/A
469N/A bci.is_left = false;
469N/A bci.x = x + 1;
469N/A bci.y = y;
469N/A bci.top_fill = top_fill;
469N/A bci.bottom_fill = bottom_fill;
469N/A
469N/A result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
469N/A
469N/A switch (result) {
469N/A case SCANLINE_CHECK_ABORTED:
469N/A aborted = true;
469N/A break;
469N/A case SCANLINE_CHECK_BOUNDARY:
469N/A reached_screen_boundary = true;
469N/A break;
469N/A default:
469N/A break;
469N/A }
469N/A }
469N/A }
469N/A
469N/A g_free(px);
469N/A
469N/A if (aborted) {
469N/A g_free(trace_px);
469N/A desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
469N/A return;
469N/A }
469N/A
469N/A if (reached_screen_boundary) {
469N/A 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."));
469N/A }
469N/A
469N/A GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(trace_px,
469N/A GDK_COLORSPACE_RGB,
469N/A TRUE,
469N/A 8, width, height, width * 4,
469N/A (GdkPixbufDestroyNotify)g_free,
469N/A NULL);
469N/A
469N/A NR::Matrix inverted_affine = NR::Matrix(affine).inverse();
469N/A
469N/A do_trace(pixbuf, desktop, inverted_affine);
469N/A
469N/A g_free(trace_px);
469N/A
469N/A sp_document_done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
469N/A}
469N/A
469N/Astatic gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
469N/A{
469N/A gint ret = FALSE;
469N/A
469N/A switch (event->type) {
469N/A case GDK_BUTTON_PRESS:
469N/A break;
469N/A // motion and release are always on root (why?)
469N/A default:
469N/A break;
469N/A }
469N/A
469N/A if (((SPEventContextClass *) parent_class)->item_handler) {
469N/A ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
469N/A }
469N/A
469N/A return ret;
469N/A}
469N/A
469N/Astatic gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event)
469N/A{
469N/A gint ret = FALSE;
469N/A switch (event->type) {
469N/A case GDK_BUTTON_PRESS:
469N/A if ( event->button.button == 1 ) {
469N/A sp_flood_do_flood_fill(event_context, event);
469N/A
469N/A ret = TRUE;
469N/A }
469N/A break;
469N/A case GDK_KEY_PRESS:
469N/A switch (get_group0_keyval (&event->key)) {
469N/A case GDK_Up:
469N/A case GDK_Down:
469N/A case GDK_KP_Up:
469N/A case GDK_KP_Down:
469N/A // prevent the zoom field from activation
469N/A if (!MOD__CTRL_ONLY)
469N/A ret = TRUE;
469N/A break;
469N/A default:
469N/A break;
469N/A }
469N/A break;
469N/A default:
469N/A break;
469N/A }
469N/A
469N/A if (!ret) {
469N/A if (((SPEventContextClass *) parent_class)->root_handler) {
469N/A ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
469N/A }
469N/A }
469N/A
469N/A return ret;
469N/A}
469N/A
469N/A
469N/Astatic void sp_flood_finish(SPFloodContext *rc)
469N/A{
469N/A rc->_message_context->clear();
469N/A
469N/A if ( rc->item != NULL ) {
469N/A SPDesktop * desktop;
469N/A
469N/A desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
469N/A
469N/A SP_OBJECT(rc->item)->updateRepr();
469N/A
469N/A sp_canvas_end_forced_full_redraws(desktop->canvas);
469N/A
469N/A sp_desktop_selection(desktop)->set(rc->item);
469N/A sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
469N/A _("Fill bounded area"));
469N/A
469N/A rc->item = NULL;
469N/A }
469N/A}
469N/A
469N/Avoid flood_channels_changed(GtkComboBox *cbox, GtkWidget *tbl)
469N/A{
469N/A prefs_set_int_attribute("tools.paintbucket", "channels", (gint)gtk_combo_box_get_active(cbox));
469N/A}
469N/A/*
469N/A Local Variables:
469N/A mode:c++
469N/A c-file-style:"stroustrup"
469N/A c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
469N/A indent-tabs-mode:nil
469N/A fill-column:99
469N/A End:
469N/A*/
469N/A// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
469N/A