Code

API change: render methods now take a cairo_t (not yet used)
[inkscape.git] / src / flood-context.cpp
index 896d4d29f5f349d0706bfdbf3cd7b95fcd4324d2..02e398c453c45703b2e02f01e11e3c9fd9da8cb2 100644 (file)
@@ -261,19 +261,25 @@ inline unsigned char * get_pixel(guchar *px, int x, int y, int width) {
 }
 
 static bool compare_pixels(unsigned char *a, unsigned char *b, int tolerance) {
+  int diff = 0;
   for (int i = 0; i < 4; i++) {
-    if (abs(a[i] - b[i]) > tolerance) { 
-      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, int tolerance) {
+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, tolerance)) {
-    fill_queue->push(NR::Point(x, y));
+    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) {
@@ -347,12 +353,9 @@ static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform) {
                     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[80];
-                if (sp_svg_transform_write(affinestr, 79, item_t)) {
-                    pathRepr->setAttribute("transform", affinestr);
-                } else {
-                    pathRepr->setAttribute("transform", NULL);
-                }
+                gchar *affinestr=sp_svg_transform_write(item_t);
+                pathRepr->setAttribute("transform", affinestr);
+                g_free(affinestr);
             }
 
             Inkscape::Selection *selection = sp_desktop_selection(desktop);
@@ -422,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
@@ -463,17 +466,23 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
         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, tolerance);
+          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, tolerance);
+          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;
         
@@ -485,9 +494,9 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
             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, tolerance); }
-              if (y < y_limit) { try_add_to_queue(&fill_queue, px, orig_color, left, y + 1, width, tolerance); }
+              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 {
@@ -495,6 +504,9 @@ 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 right
@@ -503,9 +515,9 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
             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, tolerance); }
-              if (y < y_limit) { try_add_to_queue(&fill_queue, px, orig_color, right, y + 1, width, tolerance); }
+              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 {