flood-context.cpp revision f5930aca526bbfa975cc5bce6226c59db23aa899
#define __SP_FLOOD_CONTEXT_C__
/*
* Flood fill drawing context
*
* Author:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* John Bintz <jcoswell@coswellproductions.org>
*
* Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
* 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 <gdk/gdkkeysyms.h>
#include <queue>
#include <deque>
#include "macros.h"
#include "display/sp-canvas.h"
#include "document.h"
#include "sp-namedview.h"
#include "sp-object.h"
#include "sp-rect.h"
#include "selection.h"
#include "desktop-handles.h"
#include "desktop.h"
#include "desktop-style.h"
#include "message-stack.h"
#include "message-context.h"
#include "pixmaps/cursor-paintbucket.xpm"
#include "flood-context.h"
#include "sp-metrics.h"
#include "object-edit.h"
#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-translate-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 "sp-item.h"
#include "sp-root.h"
#include "sp-defs.h"
#include "sp-path.h"
#include "splivarot.h"
#include "libnr/n-art-bpath.h"
#include "color.h"
#include "trace/potrace/inkscape-potrace.h"
static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
static SPEventContextClass *parent_class;
{
if (!type) {
sizeof(SPFloodContextClass),
sizeof(SPFloodContext),
4,
NULL, /* value_table */
};
}
return type;
}
{
}
{
event_context->xp = 0;
event_context->yp = 0;
event_context->within_tolerance = false;
}
{
/* fixme: This is necessary because we do not grab */
}
ec->shape_repr = 0;
}
if (rc->_message_context) {
delete rc->_message_context;
}
}
NULL, /* child_added */
NULL, /* child_removed */
NULL, /* content_changed */
NULL /* order_changed */
};
/**
\brief Callback that processes the "changed" signal on the selection;
destroys old and creates new knotholder
*/
{
ec->shape_repr = 0;
}
if (item) {
if (shape_repr) {
}
}
}
{
}
if (item) {
if (shape_repr) {
}
}
);
}
static void
unsigned char *base)
{
for (int i = 0; i < 3; i++) {
}
}
}
return glist;
}
return glist;
}
static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *dtc, int threshold, PaintBucketChannels method) {
int diff = 0;
if ((method == FLOOD_CHANNELS_H) ||
(method == FLOOD_CHANNELS_S) ||
(method == FLOOD_CHANNELS_L)) {
}
switch (method) {
case FLOOD_CHANNELS_ALPHA:
case FLOOD_CHANNELS_R:
case FLOOD_CHANNELS_G:
case FLOOD_CHANNELS_B:
case FLOOD_CHANNELS_RGB:
unsigned char merged_orig[4];
unsigned char merged_check[4];
for (int i = 0; i < 3; i++) {
}
case FLOOD_CHANNELS_H:
case FLOOD_CHANNELS_S:
case FLOOD_CHANNELS_L:
}
return false;
}
static bool try_add_to_queue(std::deque<NR::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig, unsigned char *dtc, int x, int y, int width, int threshold, PaintBucketChannels method, bool fill_switch, unsigned int max_queue_size) {
trace_t[0] = 255;
}
}
return false;
}
return true;
}
static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform, bool union_with_selection) {
long totalNodeCount = 0L;
/* Set style */
if (offset != 0) {
delete path;
expanded_path->Reset();
delete path_shape;
delete expanded_path_shape;
} else {
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Too much inset</b>, the result is empty."));
return;
}
delete expanded_path;
} else {
delete path;
}
if (reprobj) {
// premultiply the item transform by the accumulated parent transform in the paste layer
if (!local.test_identity()) {
if (t_str)
// (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
}
if (union_with_selection) {
desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, _("Area filled, path with <b>%d</b> nodes created and unioned with selection."), sp_nodes_in_path(SP_PATH(reprobj)));
} else {
desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, _("Area filled, path with <b>%d</b> nodes created."), sp_nodes_in_path(SP_PATH(reprobj)));
}
}
}
}
struct bitmap_coords_info {
bool is_left;
int x;
int y;
int y_limit;
int width;
int height;
int threshold;
int radius;
unsigned char *dtc;
bool top_fill;
bool bottom_fill;
};
enum ScanlineCheckResult {
};
static void paint_pixel(guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci) {
unsigned char *t, *trace_t;
if (r == 0) {
for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; }
}
} else {
if ( (tx >= 0)
&& (ty >= 0)
for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; }
}
}
}
}
}
}
}
static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<NR::Point> *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;
do {
ok = false;
keep_tracing = (bci.x >= 0);
} else {
}
if (keep_tracing) {
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.radius), bci.width, bci.threshold, bci.method, bci.top_fill, max_queue_size);
}
bci.bottom_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y + (1 + bci.radius), bci.width, bci.threshold, bci.method, bci.bottom_fill, max_queue_size);
}
} else {
}
ok = true;
}
} else {
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;
}
static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event, bool union_with_selection, bool is_point_fill, bool is_touch_fill) {
/* Create new arena */
if (!bbox) {
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
return;
}
double padding = 1.6;
/* Create ArenaItems and set transform */
NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(document)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
final_bbox.x0 = 0;
nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
NRPixBlock B;
unsigned char dtc[4];
for (int i = 0; i < 4; i++) {
*p++ = dtc[i];
}
}
}
nr_pixblock_release(&B);
// Hide items
if (is_point_fill) {
} else {
fill_points = r->getPoints();
}
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;
if (is_touch_fill) {
if (i == 0) {
} else {
}
} else {
}
}
bool aborted = false;
PaintBucketChannels method = (PaintBucketChannels)prefs_get_int_attribute("tools.paintbucket", "channels", 0);
switch(method) {
case FLOOD_CHANNELS_ALPHA:
case FLOOD_CHANNELS_RGB:
case FLOOD_CHANNELS_R:
case FLOOD_CHANNELS_G:
case FLOOD_CHANNELS_B:
break;
case FLOOD_CHANNELS_H:
case FLOOD_CHANNELS_S:
case FLOOD_CHANNELS_L:
break;
}
bool reached_screen_boundary = false;
color_queue.pop();
unsigned char orig_color[4];
unsigned char merged_orig[4];
unsigned char *trace_t = get_pixel(trace_px, (int)color_point[NR::X], (int)color_point[NR::Y], width);
}
// same color at this point
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 + bci.radius), width, threshold, method, top_fill, max_queue_size);
} else {
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 + bci.radius), width, threshold, method, bottom_fill, max_queue_size);
} else {
aborted = true; break;
} else {
reached_screen_boundary = 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;
}
bci.y = y;
switch (result) {
case SCANLINE_CHECK_ABORTED:
aborted = true;
break;
case SCANLINE_CHECK_BOUNDARY:
reached_screen_boundary = true;
break;
default:
break;
}
}
}
}
if (aborted) {
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
return;
}
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."));
}
TRUE,
NULL);
}
static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
{
case GDK_BUTTON_PRESS:
/* Set style */
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET, _("Set style on object"));
}
break;
default:
break;
}
}
return ret;
}
{
static bool dragging;
case GDK_BUTTON_PRESS:
// save drag origin
event_context->within_tolerance = true;
dragging = true;
}
}
}
case GDK_MOTION_NOTIFY:
if ( dragging
{
break; // do not drag if we're within tolerance from origin
}
event_context->within_tolerance = false;
event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> areas to add to fill, hold <b>Alt</b> for touch fill"));
}
}
break;
case GDK_BUTTON_RELEASE:
if (r->is_started()) {
// set "busy" cursor
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;
sp_flood_do_flood_fill(event_context, event, event->button.state & GDK_SHIFT_MASK, is_point_fill, is_touch_fill);
// 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
}
r->stop();
}
}
break;
case GDK_KEY_PRESS:
case GDK_Up:
case GDK_Down:
case GDK_KP_Up:
case GDK_KP_Down:
// prevent the zoom field from activation
if (!MOD__CTRL_ONLY)
break;
default:
break;
}
break;
default:
break;
}
if (!ret) {
}
}
return ret;
}
{
_("Fill bounded area"));
}
}
{
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :