flood-context.cpp revision 7a7345e59acafc35c32fdae5f0ceb5cf4b62bdfc
/**
* @file
* 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>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
*
* 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 "trace/potrace/inkscape-potrace.h"
#include <gdk/gdkkeysyms.h>
#include <queue>
#include <deque>
#include "color.h"
#include "context-fns.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "desktop-style.h"
#include "display/cairo-utils.h"
#include "display/drawing-context.h"
#include "display/drawing-image.h"
#include "display/drawing-item.h"
#include "display/sp-canvas.h"
#include "document.h"
#include "document-undo.h"
#include "flood-context.h"
#include "macros.h"
#include "message-context.h"
#include "message-stack.h"
#include "preferences.h"
#include "rubberband.h"
#include "selection.h"
#include "shape-editor.h"
#include "sp-defs.h"
#include "sp-item.h"
#include "splivarot.h"
#include "sp-metrics.h"
#include "sp-namedview.h"
#include "sp-object.h"
#include "sp-path.h"
#include "sp-rect.h"
#include "sp-root.h"
#include "trace/imagemap.h"
#include "xml/node-event-vector.h"
#include "verbs.h"
#include "pixmaps/cursor-paintbucket.xpm"
using Inkscape::DocumentUndo;
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;
}
{
delete ec->shape_editor;
/* fixme: This is necessary because we do not grab */
}
if (rc->_message_context) {
delete rc->_message_context;
}
}
/**
* Callback that processes the "changed" signal on the selection;
* destroys old and creates new knotholder.
*/
{
}
{
}
if (item) {
}
);
rc->enableSelectionCue();
}
}
{
return pxout;
}
/**
* 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 stride The rowstride of the pixel buffer.
*/
}
}
/**
* Generate the list of trace channel selection entries.
*/
return glist;
}
/**
* Generate the list of autogap selection entries.
*/
return glist;
}
/**
* 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(guint32 check, guint32 orig, guint32 merged_orig_pixel, guint32 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:
return abs(static_cast<int>(ac ? unpremul_alpha(rc, ac) : 0) - (ao ? unpremul_alpha(ro, ao) : 0)) <= threshold;
case FLOOD_CHANNELS_G:
return abs(static_cast<int>(ac ? unpremul_alpha(gc, ac) : 0) - (ao ? unpremul_alpha(go, ao) : 0)) <= threshold;
case FLOOD_CHANNELS_B:
return abs(static_cast<int>(ac ? unpremul_alpha(bc, ac) : 0) - (ao ? unpremul_alpha(bo, ao) : 0)) <= threshold;
case FLOOD_CHANNELS_RGB:
diff += abs(static_cast<int>(amc ? unpremul_alpha(rmc, amc) : 0) - (amop ? unpremul_alpha(rmop, amop) : 0));
diff += abs(static_cast<int>(amc ? unpremul_alpha(gmc, amc) : 0) - (amop ? unpremul_alpha(gmop, amop) : 0));
diff += abs(static_cast<int>(amc ? unpremul_alpha(bmc, amc) : 0) - (amop ? unpremul_alpha(bmop, amop) : 0));
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 stride;
unsigned int threshold;
unsigned int radius;
unsigned int max_queue_size;
unsigned int current_step;
};
/**
* 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, guint32 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;
}
}
}
/**
* 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::Affine 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++;
}
//XML Tree being used here directly while it shouldn't be...."
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) {
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.",
} else {
ngettext("Area filled, path with <b>%d</b> node created.","Area filled, path with <b>%d</b> nodes created.",
}
}
}
}
/**
* The possible return states of perform_bitmap_scanline_check().
*/
enum ScanlineCheckResult {
};
/**
* 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
/**
* 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, guint32 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;
}
}
/**
* 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)) {
}
}
}
/**
* 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)) {
}
}
}
/**
* 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, guint32 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;
}
/**
* Sort the rendered pixel buffer check queue vertically.
*/
}
/**
* Sort the rendered pixel buffer check queue horizontally.
*/
}
/**
* 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) {
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;
{ // this block limits the lifetime of Drawing and DrawingContext
/* Create DrawingItems and set transform */
Inkscape::DrawingItem *root = document->getRoot()->invoke_show( drawing, dkey, SP_ITEM_SHOW_DISPLAY);
// cairo_translate not necessary here - surface origin is at 0,0
// bgcolor is 0xrrggbbaa, we need 0xaarrggbb
// 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, document->getHeight() + (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();
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
DocumentUndo::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_KEY_Up:
case GDK_KEY_Down:
case GDK_KEY_KP_Up:
case GDK_KEY_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:fileencoding=utf-8:textwidth=99 :