1 #define __SP_FLOOD_CONTEXT_C__
3 /*
4 * Flood fill drawing context
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 * John Bintz <jcoswell@coswellproductions.org>
10 *
11 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
12 * Copyright (C) 2000-2005 authors
13 * Copyright (C) 2000-2001 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #include "config.h"
20 #include <gdk/gdkkeysyms.h>
21 #include <queue>
23 #include "macros.h"
24 #include "display/sp-canvas.h"
25 #include "document.h"
26 #include "sp-namedview.h"
27 #include "sp-object.h"
28 #include "sp-rect.h"
29 #include "selection.h"
30 #include "selection-chemistry.h"
31 #include "desktop-handles.h"
32 #include "snap.h"
33 #include "desktop.h"
34 #include "desktop-style.h"
35 #include "message-stack.h"
36 #include "message-context.h"
37 #include "pixmaps/cursor-paintbucket.xpm"
38 #include "flood-context.h"
39 #include "sp-metrics.h"
40 #include <glibmm/i18n.h>
41 #include "object-edit.h"
42 #include "xml/repr.h"
43 #include "xml/node-event-vector.h"
44 #include "prefs-utils.h"
45 #include "context-fns.h"
47 #include "display/nr-arena-item.h"
48 #include "display/nr-arena.h"
49 #include "display/nr-arena-image.h"
50 #include "display/canvas-arena.h"
51 #include "libnr/nr-pixops.h"
52 #include "libnr/nr-matrix-rotate-ops.h"
53 #include "libnr/nr-matrix-translate-ops.h"
54 #include "libnr/nr-rotate-fns.h"
55 #include "libnr/nr-scale-ops.h"
56 #include "libnr/nr-scale-translate-ops.h"
57 #include "libnr/nr-translate-matrix-ops.h"
58 #include "libnr/nr-translate-scale-ops.h"
59 #include "libnr/nr-matrix-ops.h"
60 #include "sp-item.h"
61 #include "sp-root.h"
62 #include "sp-defs.h"
63 #include "sp-path.h"
64 #include "splivarot.h"
65 #include "livarot/Path.h"
66 #include "livarot/Shape.h"
67 #include "libnr/n-art-bpath.h"
68 #include "svg/svg.h"
69 #include "color.h"
71 #include "trace/trace.h"
72 #include "trace/potrace/inkscape-potrace.h"
74 static void sp_flood_context_class_init(SPFloodContextClass *klass);
75 static void sp_flood_context_init(SPFloodContext *flood_context);
76 static void sp_flood_context_dispose(GObject *object);
78 static void sp_flood_context_setup(SPEventContext *ec);
80 static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event);
81 static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
83 static void sp_flood_finish(SPFloodContext *rc);
85 static SPEventContextClass *parent_class;
88 GtkType sp_flood_context_get_type()
89 {
90 static GType type = 0;
91 if (!type) {
92 GTypeInfo info = {
93 sizeof(SPFloodContextClass),
94 NULL, NULL,
95 (GClassInitFunc) sp_flood_context_class_init,
96 NULL, NULL,
97 sizeof(SPFloodContext),
98 4,
99 (GInstanceInitFunc) sp_flood_context_init,
100 NULL, /* value_table */
101 };
102 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPFloodContext", &info, (GTypeFlags) 0);
103 }
104 return type;
105 }
107 static void sp_flood_context_class_init(SPFloodContextClass *klass)
108 {
109 GObjectClass *object_class = (GObjectClass *) klass;
110 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
112 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
114 object_class->dispose = sp_flood_context_dispose;
116 event_context_class->setup = sp_flood_context_setup;
117 event_context_class->root_handler = sp_flood_context_root_handler;
118 event_context_class->item_handler = sp_flood_context_item_handler;
119 }
121 static void sp_flood_context_init(SPFloodContext *flood_context)
122 {
123 SPEventContext *event_context = SP_EVENT_CONTEXT(flood_context);
125 event_context->cursor_shape = cursor_paintbucket_xpm;
126 event_context->hot_x = 11;
127 event_context->hot_y = 30;
128 event_context->xp = 0;
129 event_context->yp = 0;
130 event_context->tolerance = 0;
131 event_context->within_tolerance = false;
132 event_context->item_to_select = NULL;
134 event_context->shape_repr = NULL;
135 event_context->shape_knot_holder = NULL;
137 flood_context->item = NULL;
139 new (&flood_context->sel_changed_connection) sigc::connection();
140 }
142 static void sp_flood_context_dispose(GObject *object)
143 {
144 SPFloodContext *rc = SP_FLOOD_CONTEXT(object);
145 SPEventContext *ec = SP_EVENT_CONTEXT(object);
147 rc->sel_changed_connection.disconnect();
148 rc->sel_changed_connection.~connection();
150 /* fixme: This is necessary because we do not grab */
151 if (rc->item) {
152 sp_flood_finish(rc);
153 }
155 if (ec->shape_repr) { // remove old listener
156 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
157 Inkscape::GC::release(ec->shape_repr);
158 ec->shape_repr = 0;
159 }
161 if (rc->_message_context) {
162 delete rc->_message_context;
163 }
165 G_OBJECT_CLASS(parent_class)->dispose(object);
166 }
168 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
169 NULL, /* child_added */
170 NULL, /* child_removed */
171 ec_shape_event_attr_changed,
172 NULL, /* content_changed */
173 NULL /* order_changed */
174 };
176 /**
177 \brief Callback that processes the "changed" signal on the selection;
178 destroys old and creates new knotholder
179 */
180 void sp_flood_context_selection_changed(Inkscape::Selection *selection, gpointer data)
181 {
182 SPFloodContext *rc = SP_FLOOD_CONTEXT(data);
183 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
185 if (ec->shape_repr) { // remove old listener
186 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
187 Inkscape::GC::release(ec->shape_repr);
188 ec->shape_repr = 0;
189 }
191 SPItem *item = selection->singleItem();
192 if (item) {
193 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
194 if (shape_repr) {
195 ec->shape_repr = shape_repr;
196 Inkscape::GC::anchor(shape_repr);
197 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
198 }
199 }
200 }
202 static void sp_flood_context_setup(SPEventContext *ec)
203 {
204 SPFloodContext *rc = SP_FLOOD_CONTEXT(ec);
206 if (((SPEventContextClass *) parent_class)->setup) {
207 ((SPEventContextClass *) parent_class)->setup(ec);
208 }
210 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
211 if (item) {
212 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
213 if (shape_repr) {
214 ec->shape_repr = shape_repr;
215 Inkscape::GC::anchor(shape_repr);
216 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
217 }
218 }
220 rc->sel_changed_connection.disconnect();
221 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
222 sigc::bind(sigc::ptr_fun(&sp_flood_context_selection_changed), (gpointer)rc)
223 );
225 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
226 }
228 static void
229 merge_pixel_with_background (unsigned char *orig, unsigned char *bg,
230 unsigned char *base)
231 {
232 for (int i = 0; i < 3; i++) {
233 base[i] = (255 * (255 - bg[3])) / 255 + (bg[i] * bg[3]) / 255;
234 base[i] = (base[i] * (255 - orig[3])) / 255 + (orig[i] * orig[3]) / 255;
235 }
236 base[3] = 255;
237 }
239 inline unsigned char * get_pixel(guchar *px, int x, int y, int width) {
240 return px + (x + y * width) * 4;
241 }
243 enum PaintBucketChannels {
244 FLOOD_CHANNELS_RGB,
245 FLOOD_CHANNELS_R,
246 FLOOD_CHANNELS_G,
247 FLOOD_CHANNELS_B,
248 FLOOD_CHANNELS_H,
249 FLOOD_CHANNELS_S,
250 FLOOD_CHANNELS_L,
251 FLOOD_CHANNELS_ALPHA
252 };
254 GList * flood_channels_dropdown_items_list() {
255 GList *glist = NULL;
257 glist = g_list_append (glist, _("Visible Colors"));
258 glist = g_list_append (glist, _("Red"));
259 glist = g_list_append (glist, _("Green"));
260 glist = g_list_append (glist, _("Blue"));
261 glist = g_list_append (glist, _("Hue"));
262 glist = g_list_append (glist, _("Saturation"));
263 glist = g_list_append (glist, _("Lightness"));
264 glist = g_list_append (glist, _("Alpha"));
266 return glist;
267 }
269 static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned char *dtc, int threshold, PaintBucketChannels method) {
270 int diff = 0;
271 float hsl_check[3], hsl_orig[3];
273 if ((method == FLOOD_CHANNELS_H) ||
274 (method == FLOOD_CHANNELS_S) ||
275 (method == FLOOD_CHANNELS_L)) {
276 sp_color_rgb_to_hsl_floatv(hsl_check, check[0] / 255.0, check[1] / 255.0, check[2] / 255.0);
277 sp_color_rgb_to_hsl_floatv(hsl_orig, orig[0] / 255.0, orig[1] / 255.0, orig[2] / 255.0);
278 }
280 switch (method) {
281 case FLOOD_CHANNELS_ALPHA:
282 return ((int)abs(check[3] - orig[3]) <= threshold);
283 case FLOOD_CHANNELS_R:
284 return ((int)abs(check[0] - orig[0]) <= threshold);
285 case FLOOD_CHANNELS_G:
286 return ((int)abs(check[1] - orig[1]) <= threshold);
287 case FLOOD_CHANNELS_B:
288 return ((int)abs(check[2] - orig[2]) <= threshold);
289 case FLOOD_CHANNELS_RGB:
290 unsigned char merged_orig[4];
291 unsigned char merged_check[4];
293 merge_pixel_with_background(orig, dtc, merged_orig);
294 merge_pixel_with_background(check, dtc, merged_check);
296 for (int i = 0; i < 3; i++) {
297 diff += (int)abs(merged_check[i] - merged_orig[i]);
298 }
299 return ((diff / 3) <= ((threshold * 3) / 4));
301 case FLOOD_CHANNELS_H:
302 return ((int)(fabs(hsl_check[0] - hsl_orig[0]) * 100.0) <= threshold);
303 case FLOOD_CHANNELS_S:
304 return ((int)(fabs(hsl_check[1] - hsl_orig[1]) * 100.0) <= threshold);
305 case FLOOD_CHANNELS_L:
306 return ((int)(fabs(hsl_check[2] - hsl_orig[2]) * 100.0) <= threshold);
307 }
309 return false;
310 }
312 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 threshold, PaintBucketChannels method, bool fill_switch) {
313 unsigned char *t = get_pixel(px, x, y, width);
314 if (compare_pixels(t, orig, dtc, threshold, method)) {
315 unsigned char *trace_t = get_pixel(trace_px, x, y, width);
316 if (trace_t[3] != 255 && trace_t[0] != 255) {
317 if (fill_switch) {
318 fill_queue->push(NR::Point(x, y));
319 trace_t[0] = 255;
320 }
321 }
322 return false;
323 }
324 return true;
325 }
327 static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform, bool union_with_selection) {
328 SPDocument *document = sp_desktop_document(desktop);
330 Inkscape::Trace::Potrace::PotraceTracingEngine pte;
332 pte.setTraceType(Inkscape::Trace::Potrace::TRACE_BRIGHTNESS);
333 pte.setInvert(false);
335 Glib::RefPtr<Gdk::Pixbuf> pixbuf = Glib::wrap(px, true);
337 std::vector<Inkscape::Trace::TracingEngineResult> results = pte.trace(pixbuf);
339 Inkscape::XML::Node *layer_repr = SP_GROUP(desktop->currentLayer())->repr;
340 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
342 long totalNodeCount = 0L;
344 double offset = prefs_get_double_attribute("tools.paintbucket", "offset", 0.0);
346 for (unsigned int i=0 ; i<results.size() ; i++) {
347 Inkscape::Trace::TracingEngineResult result = results[i];
348 totalNodeCount += result.getNodeCount();
350 Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
351 /* Set style */
352 sp_desktop_apply_style_tool (desktop, pathRepr, "tools.paintbucket", false);
354 NArtBpath *bpath = sp_svg_read_path(result.getPathData().c_str());
355 Path *path = bpath_to_Path(bpath);
356 g_free(bpath);
358 if (offset != 0) {
360 Shape *path_shape = new Shape();
362 path->ConvertWithBackData(0.03);
363 path->Fill(path_shape, 0);
364 delete path;
366 Shape *expanded_path_shape = new Shape();
368 expanded_path_shape->ConvertToShape(path_shape, fill_nonZero);
369 path_shape->MakeOffset(expanded_path_shape, offset * desktop->current_zoom(), join_round, 4);
370 expanded_path_shape->ConvertToShape(path_shape, fill_positive);
372 Path *expanded_path = new Path();
374 expanded_path->Reset();
375 expanded_path_shape->ConvertToForme(expanded_path);
376 expanded_path->ConvertEvenLines(1.0);
377 expanded_path->Simplify(1.0);
379 delete path_shape;
380 delete expanded_path_shape;
382 gchar *str = expanded_path->svg_dump_path();
383 if (str && *str) {
384 pathRepr->setAttribute("d", str);
385 g_free(str);
386 } else {
387 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Too much inset</b>, the result is empty."));
388 Inkscape::GC::release(pathRepr);
389 g_free(str);
390 return;
391 }
393 delete expanded_path;
395 } else {
396 gchar *str = path->svg_dump_path();
397 delete path;
398 pathRepr->setAttribute("d", str);
399 g_free(str);
400 }
402 layer_repr->addChild(pathRepr, NULL);
404 SPObject *reprobj = document->getObjectByRepr(pathRepr);
405 if (reprobj) {
406 sp_item_write_transform(SP_ITEM(reprobj), pathRepr, transform, NULL);
408 // premultiply the item transform by the accumulated parent transform in the paste layer
409 NR::Matrix local = sp_item_i2doc_affine(SP_GROUP(desktop->currentLayer()));
410 if (!local.test_identity()) {
411 gchar const *t_str = pathRepr->attribute("transform");
412 NR::Matrix item_t (NR::identity());
413 if (t_str)
414 sp_svg_transform_read(t_str, &item_t);
415 item_t *= local.inverse();
416 // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
417 gchar *affinestr=sp_svg_transform_write(item_t);
418 pathRepr->setAttribute("transform", affinestr);
419 g_free(affinestr);
420 }
422 Inkscape::Selection *selection = sp_desktop_selection(desktop);
424 pathRepr->setPosition(-1);
426 if (union_with_selection) {
427 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)));
428 selection->add(reprobj);
429 sp_selected_path_union_skip_undo();
430 } else {
431 desktop->messageStack()->flashF(Inkscape::WARNING_MESSAGE, _("Area filled, path with <b>%d</b> nodes created."), sp_nodes_in_path(SP_PATH(reprobj)));
432 selection->set(reprobj);
433 }
435 }
437 Inkscape::GC::release(pathRepr);
439 }
440 }
442 struct bitmap_coords_info {
443 bool is_left;
444 int x;
445 int y;
446 int y_limit;
447 int width;
448 int threshold;
449 PaintBucketChannels method;
450 unsigned char *dtc;
451 bool top_fill;
452 bool bottom_fill;
453 NR::Rect bbox;
454 NR::Rect screen;
455 };
457 enum ScanlineCheckResult {
458 SCANLINE_CHECK_OK,
459 SCANLINE_CHECK_ABORTED,
460 SCANLINE_CHECK_BOUNDARY
461 };
463 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) {
464 bool aborted = false;
465 bool reached_screen_boundary = false;
466 bool ok;
468 bool keep_tracing;
469 unsigned char *t, *trace_t;
471 do {
472 ok = false;
473 if (bci.is_left) {
474 keep_tracing = (bci.x >= 0);
475 } else {
476 keep_tracing = (bci.x < bci.width);
477 }
479 if (keep_tracing) {
480 t = get_pixel(px, bci.x, bci.y, bci.width);
481 if (compare_pixels(t, orig_color, bci.dtc, bci.threshold, bci.method)) {
482 for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; }
483 trace_t = get_pixel(trace_px, bci.x, bci.y, bci.width);
484 trace_t[3] = 255;
485 if (bci.y > 0) {
486 bci.top_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y - 1, bci.width, bci.threshold, bci.method, bci.top_fill);
487 }
488 if (bci.y < bci.y_limit) {
489 bci.bottom_fill = try_add_to_queue(fill_queue, px, trace_px, orig_color, bci.dtc, bci.x, bci.y + 1, bci.width, bci.threshold, bci.method, bci.bottom_fill);
490 }
491 if (bci.is_left) {
492 bci.x--;
493 } else {
494 bci.x++;
495 }
496 ok = true;
497 }
498 } else {
499 if (bci.bbox.min()[NR::X] > bci.screen.min()[NR::X]) {
500 aborted = true; break;
501 } else {
502 reached_screen_boundary = true;
503 }
504 }
505 } while (ok);
507 if (aborted) { return SCANLINE_CHECK_ABORTED; }
508 if (reached_screen_boundary) { return SCANLINE_CHECK_BOUNDARY; }
509 return SCANLINE_CHECK_OK;
510 }
512 static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *event, bool union_with_selection) {
513 SPDesktop *desktop = event_context->desktop;
514 SPDocument *document = sp_desktop_document(desktop);
516 /* Create new arena */
517 NRArena *arena = NRArena::create();
518 unsigned dkey = sp_item_display_key_new(1);
520 sp_document_ensure_up_to_date (document);
522 SPItem *document_root = SP_ITEM(SP_DOCUMENT_ROOT(document));
523 NR::Maybe<NR::Rect> bbox = document_root->getBounds(NR::identity());
525 if (!bbox) {
526 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
527 return;
528 }
530 double zoom_scale = desktop->current_zoom();
531 double padding = 1.6;
533 NR::Rect screen = desktop->get_display_area();
535 int width = (int)ceil(screen.extent(NR::X) * zoom_scale * padding);
536 int height = (int)ceil(screen.extent(NR::Y) * zoom_scale * padding);
538 NR::Point origin(screen.min()[NR::X],
539 sp_document_height(document) - screen.extent(NR::Y) - screen.min()[NR::Y]);
541 origin[NR::X] = origin[NR::X] + (screen.extent(NR::X) * ((1 - padding) / 2));
542 origin[NR::Y] = origin[NR::Y] + (screen.extent(NR::Y) * ((1 - padding) / 2));
544 NR::scale scale(zoom_scale, zoom_scale);
545 NR::Matrix affine = scale * NR::translate(-origin * scale);
547 /* Create ArenaItems and set transform */
548 NRArenaItem *root = sp_item_invoke_show(SP_ITEM(sp_document_root(document)), arena, dkey, SP_ITEM_SHOW_DISPLAY);
549 nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
551 NRGC gc(NULL);
552 nr_matrix_set_identity(&gc.transform);
554 NRRectL final_bbox;
555 final_bbox.x0 = 0;
556 final_bbox.y0 = 0;//row;
557 final_bbox.x1 = width;
558 final_bbox.y1 = height;//row + num_rows;
560 nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
562 guchar *px = g_new(guchar, 4 * width * height);
563 //memset(px, 0x00, 4 * width * height);
565 NRPixBlock B;
566 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
567 final_bbox.x0, final_bbox.y0, final_bbox.x1, final_bbox.y1,
568 px, 4 * width, FALSE, FALSE );
570 SPNamedView *nv = sp_desktop_namedview(desktop);
571 unsigned long bgcolor = nv->pagecolor;
573 unsigned char dtc[4];
574 dtc[0] = NR_RGBA32_R(bgcolor);
575 dtc[1] = NR_RGBA32_G(bgcolor);
576 dtc[2] = NR_RGBA32_B(bgcolor);
577 dtc[3] = NR_RGBA32_A(bgcolor);
579 for (int fy = 0; fy < height; fy++) {
580 guchar *p = NR_PIXBLOCK_PX(&B) + fy * B.rs;
581 for (int fx = 0; fx < width; fx++) {
582 for (int i = 0; i < 4; i++) {
583 *p++ = dtc[i];
584 }
585 }
586 }
588 nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
589 nr_pixblock_release(&B);
591 // Hide items
592 sp_item_invoke_hide(SP_ITEM(sp_document_root(document)), dkey);
594 nr_arena_item_unref(root);
595 nr_object_unref((NRObject *) arena);
597 NR::Point pw = NR::Point(event->button.x / zoom_scale, sp_document_height(document) + (event->button.y / zoom_scale)) * affine;
599 pw[NR::X] = (int)MIN(width - 1, MAX(0, pw[NR::X]));
600 pw[NR::Y] = (int)MIN(height - 1, MAX(0, pw[NR::Y]));
602 guchar *trace_px = g_new(guchar, 4 * width * height);
603 memset(trace_px, 0x00, 4 * width * height);
605 std::queue<NR::Point> fill_queue;
606 fill_queue.push(pw);
608 bool aborted = false;
609 int y_limit = height - 1;
611 unsigned char orig_color[4];
612 unsigned char *orig_px = get_pixel(px, (int)pw[NR::X], (int)pw[NR::Y], width);
613 for (int i = 0; i < 4; i++) { orig_color[i] = orig_px[i]; }
615 unsigned char merged_orig[4];
617 merge_pixel_with_background(orig_color, dtc, merged_orig);
619 PaintBucketChannels method = (PaintBucketChannels)prefs_get_int_attribute("tools.paintbucket", "channels", 0);
620 int threshold = prefs_get_int_attribute_limited("tools.paintbucket", "threshold", 1, 0, 100);
622 switch(method) {
623 case FLOOD_CHANNELS_ALPHA:
624 case FLOOD_CHANNELS_RGB:
625 case FLOOD_CHANNELS_R:
626 case FLOOD_CHANNELS_G:
627 case FLOOD_CHANNELS_B:
628 threshold = (255 * threshold) / 100;
629 break;
630 case FLOOD_CHANNELS_H:
631 case FLOOD_CHANNELS_S:
632 case FLOOD_CHANNELS_L:
633 break;
634 }
636 bool reached_screen_boundary = false;
638 bitmap_coords_info bci;
640 bci.y_limit = y_limit;
641 bci.width = width;
642 bci.threshold = threshold;
643 bci.method = method;
644 bci.bbox = *bbox;
645 bci.screen = screen;
646 bci.dtc = dtc;
648 while (!fill_queue.empty() && !aborted) {
649 NR::Point cp = fill_queue.front();
650 fill_queue.pop();
651 unsigned char *s = get_pixel(px, (int)cp[NR::X], (int)cp[NR::Y], width);
653 // same color at this point
654 if (compare_pixels(s, orig_color, dtc, threshold, method)) {
655 int x = (int)cp[NR::X];
656 int y = (int)cp[NR::Y];
658 bool top_fill = true;
659 bool bottom_fill = true;
661 if (y > 0) {
662 top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y - 1, width, threshold, method, top_fill);
663 } else {
664 if (bbox->min()[NR::Y] > screen.min()[NR::Y]) {
665 aborted = true; break;
666 } else {
667 reached_screen_boundary = true;
668 }
669 }
670 if (y < y_limit) {
671 bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, dtc, x, y + 1, width, threshold, method, bottom_fill);
672 } else {
673 if (bbox->max()[NR::Y] < screen.max()[NR::Y]) {
674 aborted = true; break;
675 } else {
676 reached_screen_boundary = true;
677 }
678 }
680 bci.is_left = true;
681 bci.x = x;
682 bci.y = y;
683 bci.top_fill = top_fill;
684 bci.bottom_fill = bottom_fill;
686 ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
688 switch (result) {
689 case SCANLINE_CHECK_ABORTED:
690 aborted = true;
691 break;
692 case SCANLINE_CHECK_BOUNDARY:
693 reached_screen_boundary = true;
694 break;
695 default:
696 break;
697 }
699 bci.is_left = false;
700 bci.x = x + 1;
701 bci.y = y;
702 bci.top_fill = top_fill;
703 bci.bottom_fill = bottom_fill;
705 result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
707 switch (result) {
708 case SCANLINE_CHECK_ABORTED:
709 aborted = true;
710 break;
711 case SCANLINE_CHECK_BOUNDARY:
712 reached_screen_boundary = true;
713 break;
714 default:
715 break;
716 }
717 }
718 }
720 g_free(px);
722 if (aborted) {
723 g_free(trace_px);
724 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("<b>Area is not bounded</b>, cannot fill."));
725 return;
726 }
728 if (reached_screen_boundary) {
729 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."));
730 }
732 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(trace_px,
733 GDK_COLORSPACE_RGB,
734 TRUE,
735 8, width, height, width * 4,
736 (GdkPixbufDestroyNotify)g_free,
737 NULL);
739 NR::Matrix inverted_affine = NR::Matrix(affine).inverse();
741 do_trace(pixbuf, desktop, inverted_affine, union_with_selection);
743 g_free(trace_px);
745 sp_document_done(document, SP_VERB_CONTEXT_PAINTBUCKET, _("Fill bounded area"));
746 }
748 static gint sp_flood_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
749 {
750 gint ret = FALSE;
752 SPDesktop *desktop = event_context->desktop;
754 switch (event->type) {
755 case GDK_BUTTON_PRESS:
756 if (event->button.state & GDK_CONTROL_MASK) {
757 NR::Point const button_w(event->button.x,
758 event->button.y);
760 SPItem *item = sp_event_context_find_item (desktop, button_w, TRUE, TRUE);
762 Inkscape::XML::Node *pathRepr = SP_OBJECT_REPR(item);
763 /* Set style */
764 sp_desktop_apply_style_tool (desktop, pathRepr, "tools.paintbucket", false);
765 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET, _("Set style on object"));
766 ret = TRUE;
767 }
768 break;
769 default:
770 break;
771 }
773 if (((SPEventContextClass *) parent_class)->item_handler) {
774 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
775 }
777 return ret;
778 }
780 static gint sp_flood_context_root_handler(SPEventContext *event_context, GdkEvent *event)
781 {
782 gint ret = FALSE;
783 SPDesktop *desktop = event_context->desktop;
785 switch (event->type) {
786 case GDK_BUTTON_PRESS:
787 if ( event->button.button == 1 ) {
788 if (!(event->button.state & GDK_CONTROL_MASK)) {
789 // set "busy" cursor
790 desktop->setWaitingCursor();
792 if (SP_IS_EVENT_CONTEXT(event_context)) {
793 // Since setWaitingCursor runs main loop iterations, we may have already left this tool!
794 // So check if the tool is valid before doing anything
796 sp_flood_do_flood_fill(event_context, event, event->button.state & GDK_SHIFT_MASK);
798 // restore cursor when done; note that it may already be different if e.g. user
799 // switched to another tool during interruptible tracing or drawing, in which case do nothing
800 desktop->clearWaitingCursor();
802 ret = TRUE;
803 }
804 }
805 }
806 break;
807 case GDK_KEY_PRESS:
808 switch (get_group0_keyval (&event->key)) {
809 case GDK_Up:
810 case GDK_Down:
811 case GDK_KP_Up:
812 case GDK_KP_Down:
813 // prevent the zoom field from activation
814 if (!MOD__CTRL_ONLY)
815 ret = TRUE;
816 break;
817 default:
818 break;
819 }
820 break;
821 default:
822 break;
823 }
825 if (!ret) {
826 if (((SPEventContextClass *) parent_class)->root_handler) {
827 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
828 }
829 }
831 return ret;
832 }
835 static void sp_flood_finish(SPFloodContext *rc)
836 {
837 rc->_message_context->clear();
839 if ( rc->item != NULL ) {
840 SPDesktop * desktop;
842 desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
844 SP_OBJECT(rc->item)->updateRepr();
846 sp_canvas_end_forced_full_redraws(desktop->canvas);
848 sp_desktop_selection(desktop)->set(rc->item);
849 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PAINTBUCKET,
850 _("Fill bounded area"));
852 rc->item = NULL;
853 }
854 }
856 void flood_channels_changed(GtkComboBox *cbox, GtkWidget *tbl)
857 {
858 prefs_set_int_attribute("tools.paintbucket", "channels", (gint)gtk_combo_box_get_active(cbox));
859 }
861 /*
862 Local Variables:
863 mode:c++
864 c-file-style:"stroustrup"
865 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
866 indent-tabs-mode:nil
867 fill-column:99
868 End:
869 */
870 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :