flood-context.cpp revision e9b6af083e34e2397a8ddbe9781920733d09d151
#define __SP_FLOOD_CONTEXT_C__
/** @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>
*
* 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#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 "preferences.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 "color.h"
#include "trace/imagemap.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) {
}
}
);
rc->enableSelectionCue();
}
}
/**
* \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
unsigned char *base)
{
for (int i = 0; i < 3; i++) {
}
}
/**
* \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.
*/
}
}
/**
* \brief Generate the list of trace channel selection entries.
*/
return glist;
}
/**
* \brief Generate the list of autogap selection entries.
*/
return glist;
}
/**
* \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;
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_check[3];
for (int i = 0; i < 3; i++) {
}
case FLOOD_CHANNELS_H:
case FLOOD_CHANNELS_S:
case FLOOD_CHANNELS_L:
}
return false;
}
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;
unsigned char *dtc;
unsigned char *merged_orig_pixel;
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 {
return true;
} else {
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) {
unsigned char *trace_t;
unsigned int gray_map_y = 0;
gray_map_t++;
trace_t++;
}
gray_map_y++;
}
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.isIdentity()) {
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, 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)));
} 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)));
}
}
}
}
/**
* \brief The possible return states of perform_bitmap_scanline_check()
*/
enum ScanlineCheckResult {
};
/**
* \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.
*/
}
#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) {
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;
if (!is_pixel_colored(trace_t)) {
} else {
}
}
}
}
}
unsigned int paint_directions = 0;
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)) {
}
}
}
/**
* \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)) {
}
}
}
/**
* \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 keep_tracing;
bool initial_paint = true;
unsigned int paint_directions;
bool currently_painting_top = false;
bool currently_painting_bottom = false;
bool can_paint_top = (top_ty > 0);
do {
ok = false;
keep_tracing = (bci.x != 0);
} else {
}
if (keep_tracing) {
}
}
if (can_paint_top) {
if (paint_directions & PAINT_DIRECTION_UP) {
if (!is_pixel_queued(trace_t)) {
if (ok_to_paint && (!currently_painting_top)) {
currently_painting_top = true;
}
if ((!ok_to_paint) && currently_painting_top) {
currently_painting_top = false;
}
}
}
}
if (can_paint_bottom) {
if (paint_directions & PAINT_DIRECTION_DOWN) {
if (!is_pixel_queued(trace_t)) {
if (ok_to_paint && (!currently_painting_bottom)) {
currently_painting_bottom = true;
}
if ((!ok_to_paint) && currently_painting_bottom) {
currently_painting_bottom = false;
}
}
}
}
if (paint_directions & PAINT_DIRECTION_LEFT) {
bci.x--; current_trace_t--;
ok = true;
}
} else {
if (paint_directions & PAINT_DIRECTION_RIGHT) {
bci.x++; current_trace_t++;
ok = true;
}
}
initial_paint = false;
}
} 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;
}
/**
* \brief Sort the rendered pixel buffer check queue vertically.
*/
}
/**
* \brief Sort the rendered pixel buffer check queue horizontally.
*/
}
/**
* \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) {
/* Create new arena */
if (!bbox) {
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
return;
}
// 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;
/* 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
bool aborted = false;
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;
}
bci.current_step = 0;
if (is_point_fill) {
} else {
fill_points = r->getPoints();
}
for (unsigned int i = 0; i < fill_points.size(); i++) {
Geom::Point pw = Geom::Point(fill_points[i][Geom::X] / zoom_scale, sp_document_height(document) + (fill_points[i][Geom::Y] / zoom_scale)) * affine;
if (is_touch_fill) {
if (i == 0) {
} else {
push_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, (int)pw[Geom::X], (int)pw[Geom::Y]);
}
} else {
}
}
bool reached_screen_boundary = false;
bool first_run = true;
unsigned long sort_size_threshold = 5;
unsigned int max_y = 0;
unsigned int max_x = 0;
color_queue.pop();
unsigned char orig_color[4];
unsigned char merged_orig[3];
if (!first_run) {
for (unsigned int y = 0; y < height; y++) {
for (unsigned int x = 0; x < width; x++) {
trace_t++;
}
}
}
first_run = false;
}
}
/*
* 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) {
if (start_sort != end_sort) {
}
start_sort = i;
}
end_sort = i;
}
if (start_sort != end_sort) {
}
}
}
}
if (!is_pixel_checked(trace_t)) {
if (y == 0) {
aborted = true; break;
} else {
reached_screen_boundary = true;
}
}
if (y == y_limit) {
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, &min_x, &max_x);
switch (result) {
case SCANLINE_CHECK_ABORTED:
aborted = true;
break;
case SCANLINE_CHECK_BOUNDARY:
reached_screen_boundary = true;
break;
default:
break;
}
trace_t++;
bci.x = x + 1;
switch (result) {
case SCANLINE_CHECK_ABORTED:
aborted = true;
break;
case SCANLINE_CHECK_BOUNDARY:
reached_screen_boundary = true;
break;
default:
break;
}
}
}
}
bci.current_step++;
aborted = true;
}
}
}
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."));
}
do_trace(bci, trace_px, desktop, inverted_affine, min_x, max_x, min_y, max_y, union_with_selection);
}
static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
{
case GDK_BUTTON_PRESS:
if ((event->button.state & GDK_CONTROL_MASK) && event->button.button == 1 && !event_context->space_panning) {
/* 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();
if (SP_IS_EVENT_CONTEXT(event_context)) {
}
}
}
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 :