Code

Only trace the painted area of the screen with each fill, reducing memory usage.
[inkscape.git] / src / flood-context.cpp
index 9f0b041f3a09cacf53294d3bdc726c9eb81b38ea..083b87fa20ba7dc7c5880e70e62276551b70d272 100644 (file)
@@ -1,7 +1,7 @@
 #define __SP_FLOOD_CONTEXT_C__
 
-/*
-* Flood fill drawing context
+/** \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>
@@ -67,6 +67,7 @@
 #include "color.h"
 
 #include "trace/trace.h"
+#include "trace/imagemap.h"
 #include "trace/potrace/inkscape-potrace.h"
 
 static void sp_flood_context_class_init(SPFloodContextClass *klass);
@@ -227,6 +228,12 @@ static void sp_flood_context_setup(SPEventContext *ec)
     }
 }
 
+/**
+ * \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
 merge_pixel_with_background (unsigned char *orig, unsigned char *bg,
            unsigned char *base)
@@ -239,10 +246,24 @@ merge_pixel_with_background (unsigned char *orig, unsigned char *bg,
     }
 }
 
+/**
+ * \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.
+ */
 inline unsigned char * get_pixel(guchar *px, int x, int y, int width) {
     return px + (x + y * width) * 4;
 }
 
+inline unsigned char * get_trace_pixel(guchar *trace_px, int x, int y, int width) {
+    return trace_px + (x + y * width);
+}
+
+/**
+ * \brief Generate the list of trace channel selection entries.
+ */
 GList * flood_channels_dropdown_items_list() {
     GList *glist = NULL;
 
@@ -258,6 +279,9 @@ GList * flood_channels_dropdown_items_list() {
     return glist;
 }
 
+/**
+ * \brief Generate the list of autogap selection entries.
+ */
 GList * flood_autogap_dropdown_items_list() {
     GList *glist = NULL;
 
@@ -269,6 +293,15 @@ GList * flood_autogap_dropdown_items_list() {
     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;
     float hsl_check[3], hsl_orig[3];
@@ -310,20 +343,30 @@ static bool compare_pixels(unsigned char *check, unsigned char *orig, unsigned c
     return false;
 }
 
-static inline bool is_pixel_checked(unsigned char *t) { return t[0] == 1; }
-static inline bool is_pixel_queued(unsigned char *t) { return t[1] == 1; }
-static inline bool is_pixel_paintability_checked(unsigned char *t) { return t[2] != 0; }
-static inline bool is_pixel_paintable(unsigned char *t) { return t[2] == 1; }
-static inline bool is_pixel_colored(unsigned char *t) { return t[3] == 255; }
+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[0] = 1; }
-static inline void mark_pixel_unchecked(unsigned char *t) { t[0] = 0; }
-static inline void mark_pixel_queued(unsigned char *t) { t[1] = 1; }
-static inline void mark_pixel_paintable(unsigned char *t) { t[2] = 1; }
-static inline void mark_pixel_not_paintable(unsigned char *t) { t[2] = 2; }
-static inline void mark_pixel_colored(unsigned char *t) { t[3] = 255; }
+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[2] = 0; }
+static inline void clear_pixel_paintability(unsigned char *t) { *t ^= PIXEL_PAINTABLE; *t ^= PIXEL_NOT_PAINTABLE; }
 
 struct bitmap_coords_info {
     bool is_left;
@@ -343,6 +386,15 @@ struct bitmap_coords_info {
     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);
@@ -358,18 +410,36 @@ inline static bool check_if_pixel_is_paintable(guchar *px, unsigned char *trace_
     }
 }
 
-static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform, bool union_with_selection) {
+/**
+ * \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, NR::Matrix transform, unsigned int min_x, unsigned int max_x, unsigned int min_y, unsigned int max_y, bool union_with_selection) {
     SPDocument *document = sp_desktop_document(desktop);
-    
+
+    unsigned char *trace_t;
+
+    GrayMap *gray_map = GrayMapCreate((max_x - min_x + 1), (max_y - min_y + 1));
+    unsigned int gray_map_y = 0;
+    for (unsigned int y = min_y; y <= max_y; y++) {
+        unsigned long *gray_map_t = gray_map->rows[gray_map_y];
+
+        trace_t = get_trace_pixel(trace_px, min_x, y, bci.width);
+        for (unsigned int x = min_x; x <= max_x; x++) {
+            *gray_map_t = is_pixel_colored(trace_t) ? GRAYMAP_BLACK : GRAYMAP_WHITE;
+            gray_map_t++;
+            trace_t++;
+        }
+        gray_map_y++;
+    }
+
     Inkscape::Trace::Potrace::PotraceTracingEngine pte;
-        
-    pte.setTraceType(Inkscape::Trace::Potrace::TRACE_BRIGHTNESS);
-    pte.setInvert(false);
+    std::vector<Inkscape::Trace::TracingEngineResult> results = pte.traceGrayMap(gray_map);
+    gray_map->destroy(gray_map);
 
-    Glib::RefPtr<Gdk::Pixbuf> pixbuf = Glib::wrap(px, true);
-    
-    std::vector<Inkscape::Trace::TracingEngineResult> results = pte.trace(pixbuf);
-    
     Inkscape::XML::Node *layer_repr = SP_GROUP(desktop->currentLayer())->repr;
     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
 
@@ -473,12 +543,21 @@ static void do_trace(GdkPixbuf *px, SPDesktop *desktop, NR::Matrix transform, bo
     }
 }
 
+/**
+ * \brief The possible return states of perform_bitmap_scanline_check()
+ */
 enum ScanlineCheckResult {
     SCANLINE_CHECK_OK,
     SCANLINE_CHECK_ABORTED,
     SCANLINE_CHECK_BOUNDARY
 };
 
+/**
+ * \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.
+ */
 inline static bool coords_in_range(unsigned int x, unsigned int y, bitmap_coords_info bci) {
     return (x < bci.width) &&
            (y < bci.height);
@@ -490,6 +569,14 @@ inline static bool coords_in_range(unsigned int x, unsigned int y, bitmap_coords
 #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) {
     if (bci.radius == 0) {
         mark_pixel_colored(original_point_trace_t); 
@@ -505,7 +592,7 @@ inline static unsigned int paint_pixel(guchar *px, guchar *trace_px, unsigned ch
         for (unsigned int ty = bci.y - bci.radius; ty <= bci.y + bci.radius; ty++) {
             for (unsigned int tx = bci.x - bci.radius; tx <= bci.x + bci.radius; tx++) {
                 if (coords_in_range(tx, ty, bci)) {
-                    trace_t = get_pixel(trace_px, tx, ty, bci.width);
+                    trace_t = get_trace_pixel(trace_px, tx, ty, bci.width);
                     if (!is_pixel_colored(trace_t)) {
                         if (check_if_pixel_is_paintable(px, trace_t, tx, ty, orig_color, bci)) {
                             mark_pixel_colored(trace_t); 
@@ -530,6 +617,14 @@ inline static unsigned int paint_pixel(guchar *px, guchar *trace_px, unsigned ch
     }
 }
 
+/**
+ * \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<NR::Point> *fill_queue, unsigned int max_queue_size, unsigned char *trace_t, unsigned int x, unsigned int y) {
     if (!is_pixel_queued(trace_t)) {
         if ((fill_queue->size() < max_queue_size)) {
@@ -539,6 +634,14 @@ static void push_point_onto_queue(std::deque<NR::Point> *fill_queue, unsigned in
     }
 }
 
+/**
+ * \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<NR::Point> *fill_queue, unsigned int max_queue_size, unsigned char *trace_t, unsigned int x, unsigned int y) {
     if (!is_pixel_queued(trace_t)) {
         if ((fill_queue->size() < max_queue_size)) {
@@ -548,7 +651,15 @@ static void shift_point_onto_queue(std::deque<NR::Point> *fill_queue, unsigned i
     }
 }
 
-static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<NR::Point> *fill_queue, guchar *px, guchar *trace_px, unsigned char *orig_color, bitmap_coords_info bci) {
+/**
+ * \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<NR::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;
@@ -556,14 +667,12 @@ static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<NR::Point> *
     bool keep_tracing;
     bool initial_paint = true;
 
-    unsigned char *current_trace_t = get_pixel(trace_px, bci.x, bci.y, bci.width);
+    unsigned char *current_trace_t = get_trace_pixel(trace_px, bci.x, bci.y, bci.width);
     unsigned int paint_directions;
 
     bool currently_painting_top = false;
     bool currently_painting_bottom = false;
 
-    unsigned int pix_width = bci.width * 4;
-
     unsigned int top_ty = bci.y - 1;
     unsigned int bottom_ty = bci.y + 1;
 
@@ -580,6 +689,9 @@ static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<NR::Point> *
             keep_tracing = (bci.x < bci.width);
         }
 
+        *min_x = MIN(*min_x, bci.x);
+        *max_x = MAX(*max_x, bci.x);
+
         if (keep_tracing) {
             if (check_if_pixel_is_paintable(px, current_trace_t, bci.x, bci.y, orig_color, bci)) {
                 paint_directions = paint_pixel(px, trace_px, orig_color, bci, current_trace_t);
@@ -592,7 +704,7 @@ static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<NR::Point> *
 
                 if (can_paint_top) {
                     if (paint_directions & PAINT_DIRECTION_UP) { 
-                        unsigned char *trace_t = current_trace_t - pix_width;
+                        unsigned char *trace_t = current_trace_t - bci.width;
                         if (!is_pixel_queued(trace_t)) {
                             bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, top_ty, orig_color, bci);
 
@@ -611,7 +723,7 @@ static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<NR::Point> *
 
                 if (can_paint_bottom) {
                     if (paint_directions & PAINT_DIRECTION_DOWN) { 
-                        unsigned char *trace_t = current_trace_t + pix_width;
+                        unsigned char *trace_t = current_trace_t + bci.width;
                         if (!is_pixel_queued(trace_t)) {
                             bool ok_to_paint = check_if_pixel_is_paintable(px, trace_t, bci.x, bottom_ty, orig_color, bci);
 
@@ -630,12 +742,12 @@ static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<NR::Point> *
 
                 if (bci.is_left) {
                     if (paint_directions & PAINT_DIRECTION_LEFT) {
-                        bci.x -= 1; current_trace_t -= 4;
+                        bci.x--; current_trace_t--;
                         ok = true;
                     }
                 } else {
                     if (paint_directions & PAINT_DIRECTION_RIGHT) {
-                        bci.x += 1; current_trace_t += 4;
+                        bci.x++; current_trace_t++;
                         ok = true;
                     }
                 }
@@ -656,14 +768,28 @@ static ScanlineCheckResult perform_bitmap_scanline_check(std::deque<NR::Point> *
     return SCANLINE_CHECK_OK;
 }
 
+/**
+ * \brief Sort the rendered pixel buffer check queue vertically.
+ */
 static bool sort_fill_queue_vertical(NR::Point a, NR::Point b) {
     return a[NR::Y] > b[NR::Y];
 }
 
+/**
+ * \brief Sort the rendered pixel buffer check queue horizontally.
+ */
 static bool sort_fill_queue_horizontal(NR::Point a, NR::Point b) {
     return a[NR::X] > b[NR::X];
 }
 
+/**
+ * \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) {
     SPDesktop *desktop = event_context->desktop;
     SPDocument *document = sp_desktop_document(desktop);
@@ -683,6 +809,9 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     }
     
     double zoom_scale = desktop->current_zoom();
+    
+    // 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;
 
     NR::Rect screen = desktop->get_display_area();
@@ -704,13 +833,13 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     nr_arena_item_set_transform(NR_ARENA_ITEM(root), affine);
 
     NRGC gc(NULL);
-    nr_matrix_set_identity(&gc.transform);
+    gc.transform.set_identity();
     
     NRRectL final_bbox;
     final_bbox.x0 = 0;
-    final_bbox.y0 = 0;//row;
+    final_bbox.y0 = 0; //row;
     final_bbox.x1 = width;
-    final_bbox.y1 = height;//row + num_rows;
+    final_bbox.y1 = height; //row + num_rows;
     
     nr_arena_item_invoke_update(root, &final_bbox, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
 
@@ -748,8 +877,8 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     nr_arena_item_unref(root);
     nr_object_unref((NRObject *) arena);
     
-    guchar *trace_px = g_new(guchar, 4 * width * height);
-    memset(trace_px, 0x00, 4 * width * height);
+    guchar *trace_px = g_new(guchar, width * height);
+    memset(trace_px, 0x00, width * height);
     
     std::deque<NR::Point> fill_queue;
     std::queue<NR::Point> color_queue;
@@ -807,7 +936,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
             if (i == 0) {
                 color_queue.push(pw);
             } else {
-                unsigned char *trace_t = get_pixel(trace_px, (int)pw[NR::X], (int)pw[NR::Y], width);
+                unsigned char *trace_t = get_trace_pixel(trace_px, (int)pw[NR::X], (int)pw[NR::Y], width);
                 push_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, (int)pw[NR::X], (int)pw[NR::Y]);
             }
         } else {
@@ -821,6 +950,11 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
 
     unsigned long sort_size_threshold = 5;
 
+    unsigned int min_y = height;
+    unsigned int max_y = 0;
+    unsigned int min_x = width;
+    unsigned int max_x = 0;
+
     while (!color_queue.empty() && !aborted) {
         NR::Point color_point = color_queue.front();
         color_queue.pop();
@@ -838,17 +972,17 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
 
         bci.merged_orig_pixel = merged_orig;
 
-        unsigned char *trace_t = get_pixel(trace_px, cx, cy, width);
+        unsigned char *trace_t = get_trace_pixel(trace_px, cx, cy, width);
         if (!is_pixel_checked(trace_t) && !is_pixel_colored(trace_t)) {
             if (check_if_pixel_is_paintable(px, trace_px, cx, cy, orig_color, bci)) {
                 shift_point_onto_queue(&fill_queue, bci.max_queue_size, trace_t, cx, cy);
 
                 if (!first_run) {
                     for (unsigned int y = 0; y < height; y++) {
-                        trace_t = get_pixel(trace_px, 0, y, width);
+                        trace_t = get_trace_pixel(trace_px, 0, y, width);
                         for (unsigned int x = 0; x < width; x++) {
                             clear_pixel_paintability(trace_t);
-                            trace_t += 4;
+                            trace_t++;
                         }
                     }
                 }
@@ -864,6 +998,14 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
             if (bci.radius == 0) {
                 unsigned long new_fill_queue_size = fill_queue.size();
 
+                /*
+                 * 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) {
                         std::sort(fill_queue.begin(), fill_queue.end(), sort_fill_queue_vertical);
@@ -901,7 +1043,10 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
             int x = (int)cp[NR::X];
             int y = (int)cp[NR::Y];
 
-            unsigned char *trace_t = get_pixel(trace_px, x, y, width);
+            min_y = MIN((unsigned int)y, min_y);
+            max_y = MAX((unsigned int)y, max_y);
+
+            unsigned char *trace_t = get_trace_pixel(trace_px, x, y, width);
             if (!is_pixel_checked(trace_t)) {
                 mark_pixel_checked(trace_t);
 
@@ -925,7 +1070,7 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
                 bci.x = x;
                 bci.y = y;
 
-                ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
+                ScanlineCheckResult result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci, &min_x, &max_x);
 
                 switch (result) {
                     case SCANLINE_CHECK_ABORTED:
@@ -939,13 +1084,13 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
                 }
 
                 if (bci.x < width) {
-                    trace_t += 4;
+                    trace_t++;
                     if (!is_pixel_checked(trace_t) && !is_pixel_queued(trace_t)) {
                         mark_pixel_checked(trace_t);
                         bci.is_left = false;
                         bci.x = x + 1;
 
-                        result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci);
+                        result = perform_bitmap_scanline_check(&fill_queue, px, trace_px, orig_color, bci, &min_x, &max_x);
 
                         switch (result) {
                             case SCANLINE_CHECK_ABORTED:
@@ -980,17 +1125,19 @@ static void sp_flood_do_flood_fill(SPEventContext *event_context, GdkEvent *even
     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.")); 
     }
-    
-    GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(trace_px,
-                                      GDK_COLORSPACE_RGB,
-                                      TRUE,
-                                      8, width, height, width * 4,
-                                      (GdkPixbufDestroyNotify)g_free,
-                                      NULL);
 
+    unsigned int trace_padding = bci.radius + 1;
+    if (min_y > trace_padding) { min_y -= trace_padding; }
+    if (max_y < (y_limit - trace_padding)) { max_y += trace_padding; }
+    if (min_x > trace_padding) { min_x -= trace_padding; }
+    if (max_x < (width - 1 - trace_padding)) { max_x += trace_padding; }
+
+    NR::Point min_start = NR::Point(min_x, min_y);
+    
+    affine = scale * NR::translate(-origin * scale - min_start);
     NR::Matrix inverted_affine = NR::Matrix(affine).inverse();
     
-    do_trace(pixbuf, desktop, inverted_affine, union_with_selection);
+    do_trace(bci, trace_px, desktop, inverted_affine, min_x, max_x, min_y, max_y, union_with_selection);
 
     g_free(trace_px);