Code

API change: render methods now take a cairo_t (not yet used)
[inkscape.git] / src / flood-context.cpp
index a6ef8a266b846f7e0b81c0098cb5a0d33c18bf24..02e398c453c45703b2e02f01e11e3c9fd9da8cb2 100644 (file)
@@ -34,7 +34,7 @@
 #include "desktop-style.h"
 #include "message-stack.h"
 #include "message-context.h"
-#include "pixmaps/cursor-rect.xpm"
+#include "pixmaps/cursor-paintbucket.xpm"
 #include "flood-context.h"
 #include "sp-metrics.h"
 #include <glibmm/i18n.h>
@@ -130,9 +130,9 @@ static void sp_flood_context_init(SPFloodContext *flood_context)
 {
     SPEventContext *event_context = SP_EVENT_CONTEXT(flood_context);
 
-    event_context->cursor_shape = cursor_rect_xpm;
-    event_context->hot_x = 4;
-    event_context->hot_y = 4;
+    event_context->cursor_shape = cursor_paintbucket_xpm;
+    event_context->hot_x = 11;
+    event_context->hot_y = 30;
     event_context->xp = 0;
     event_context->yp = 0;
     event_context->tolerance = 0;
@@ -260,20 +260,26 @@ inline unsigned char * get_pixel(guchar *px, int x, int y, int width) {
   return px + (x + y * width) * 4;
 }
 
-static bool compare_pixels(unsigned char *a, unsigned char *b, int fuzziness) {
+static bool compare_pixels(unsigned char *a, unsigned char *b, int tolerance) {
+  int diff = 0;
   for (int i = 0; i < 4; i++) {
-    if (a[i] != b[i]) { 
-      return false;
-    }
+    diff += (int)abs(a[i] - b[i]);
   }
-  return true;
+  return ((diff / 4) <= tolerance);
 }
 
-static void try_add_to_queue(std::queue<NR::Point> *fill_queue, guchar *px, unsigned char *orig, int x, int y, int width) {
+static bool try_add_to_queue(std::queue<NR::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig, int x, int y, int width, int tolerance, bool fill_switch) {
   unsigned char *t = get_pixel(px, x, y, width);
-  if (compare_pixels(t, orig, 0)) {
-    fill_queue->push(NR::Point(x, y));
+  if (compare_pixels(t, orig, tolerance)) {
+    unsigned char *trace_t = get_pixel(trace_px, x, y, width);
+    if (trace_t[3] != 255) {
+      if (fill_switch) {
+        fill_queue->push(NR::Point(x, y));
+      }
+    }
+    return false;
   }
+  return true;
 }
 
 static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
@@ -331,12 +337,27 @@ static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
         delete expanded_path;
         pathRepr->setAttribute("d", str);
         g_free(str);
-        
+
         layer_repr->addChild(pathRepr, NULL);
 
         SPObject *reprobj = document->getObjectByRepr(pathRepr);
         if (reprobj) {
             sp_item_write_transform(SP_ITEM(reprobj), pathRepr, transform, NULL);
+            
+            // premultiply the item transform by the accumulated parent transform in the paste layer
+            NR::Matrix local = sp_item_i2doc_affine(SP_GROUP(desktop->currentLayer()));
+            if (!local.test_identity()) {
+                gchar const *t_str = pathRepr->attribute("transform");
+                NR::Matrix item_t (NR::identity());
+                if (t_str)
+                    sp_svg_transform_read(t_str, &item_t);
+                item_t *= local.inverse();
+                // (we're dealing with unattached repr, so we write to its attr instead of using sp_item_set_transform)
+                gchar *affinestr=sp_svg_transform_write(item_t);
+                pathRepr->setAttribute("transform", affinestr);
+                g_free(affinestr);
+            }
+
             Inkscape::Selection *selection = sp_desktop_selection(desktop);
             selection->set(reprobj);
             pathRepr->setPosition(-1);
@@ -365,7 +386,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     }
     
     double zoom_scale = desktop->current_zoom();
-    double padding = 1.4;
+    double padding = 1.6;
 
     NR::Rect screen = desktop->get_display_area();
 
@@ -404,7 +425,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
                               final_bbox.x0, final_bbox.y0, final_bbox.x1, final_bbox.y1,
                               px, 4 * width, FALSE, FALSE );
     
-    nr_arena_item_invoke_render( root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
+    nr_arena_item_invoke_render(NULL, root, &final_bbox, &B, NR_ARENA_ITEM_RENDER_NO_CACHE );
     nr_pixblock_release(&B);
     
     // Hide items
@@ -413,7 +434,6 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     nr_arena_item_unref(root);
     nr_object_unref((NRObject *) arena);
     
-
     NR::Point pw = NR::Point(event->button.x / zoom_scale, sp_document_height(document) + (event->button.y / zoom_scale)) * affine;
     
     pw[NR::X] = (int)MIN(width - 1, MAX(0, pw[NR::X]));
@@ -432,29 +452,37 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     unsigned char *orig_px = get_pixel(px, (int)pw[NR::X], (int)pw[NR::Y], width);
     for (int i = 0; i < 4; i++) { orig_color[i] = orig_px[i]; }
 
+    int tolerance = (255 * prefs_get_int_attribute_limited("tools.paintbucket", "tolerance", 1, 0, 100)) / 100;
+
     while (!fill_queue.empty() && !aborted) {
       NR::Point cp = fill_queue.front();
       fill_queue.pop();
       unsigned char *s = get_pixel(px, (int)cp[NR::X], (int)cp[NR::Y], width);
       
       // same color at this point
-      if (compare_pixels(s, orig_color, 0)) {
+      if (compare_pixels(s, orig_color, tolerance)) {
         int left = (int)cp[NR::X];
         int right = (int)cp[NR::X] + 1;
         int x = (int)cp[NR::X];
         int y = (int)cp[NR::Y];
         
+        bool top_fill = true;
+        bool bottom_fill = true;
+        
         if (y > 0) { 
-          try_add_to_queue(&fill_queue, px, orig_color, x, y - 1, width);
+          top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, x, y - 1, width, tolerance, top_fill);
         } else {
           aborted = true; break;
         }
         if (y < y_limit) { 
-          try_add_to_queue(&fill_queue, px, orig_color, x, y + 1, width);
+          bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, x, y + 1, width, tolerance, bottom_fill);
         } else {
           aborted = true; break;
         }
         
+        bool default_top_fill = top_fill;
+        bool default_bottom_fill = bottom_fill;
+        
         unsigned char *t, *trace_t;
         bool ok = false;
         
@@ -463,12 +491,12 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
           // go left
           if (left >= 0) {
             t = get_pixel(px, left, y, width);
-            if (compare_pixels(t, orig_color, 0)) {
+            if (compare_pixels(t, orig_color, tolerance)) {
               for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; }
               trace_t = get_pixel(trace_px, left, y, width);
-              trace_t[3] = 255;
-              if (y > 0) { try_add_to_queue(&fill_queue, px, orig_color, left, y - 1, width); }
-              if (y < y_limit) { try_add_to_queue(&fill_queue, px, orig_color, left, y + 1, width); }
+              trace_t[3] = 255; 
+              if (y > 0) { top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, left, y - 1, width, tolerance, top_fill); }
+              if (y < y_limit) { bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, left, y + 1, width, tolerance, bottom_fill); }
               left--; ok = true;
             }
           } else {
@@ -476,17 +504,20 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
           }
         } while (ok);
       
+        top_fill = default_top_fill;
+        bottom_fill = default_bottom_fill;
+        
         do {
           ok = false;
-          // go left
+          // go right
           if (right < width) {
             t = get_pixel(px, right, y, width);
-            if (compare_pixels(t, orig_color, 0)) {
+            if (compare_pixels(t, orig_color, tolerance)) {
               for (int i = 0; i < 4; i++) { t[i] = 255 - t[i]; }
               trace_t = get_pixel(trace_px, right, y, width);
-              trace_t[3] = 255; 
-              if (y > 0) { try_add_to_queue(&fill_queue, px, orig_color, right, y - 1, width); }
-              if (y < y_limit) { try_add_to_queue(&fill_queue, px, orig_color, right, y + 1, width); }
+              trace_t[3] = 255;
+              if (y > 0) { top_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, right, y - 1, width, tolerance, top_fill); }
+              if (y < y_limit) { bottom_fill = try_add_to_queue(&fill_queue, px, trace_px, orig_color, right, y + 1, width, tolerance, bottom_fill); }
               right++; ok = true;
             }
           } else {