Code

Merging in the DBus Interface from GSoC 2009
[inkscape.git] / src / display / nr-filter-gaussian.cpp
index c1b534b7705addcb019e8ceb45800ffe9f26e9b0..9509eaef7f2e4b02e3aeb19c25c5aac57d6252c5 100644 (file)
@@ -8,7 +8,7 @@
  *   bulia byak
  *   Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
  *
- * Copyright (C) 2006 authors
+ * Copyright (C) 2006-2008 authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 #include "display/nr-filter-units.h"
 #include "libnr/nr-blit.h"
 #include "libnr/nr-pixblock.h"
-#include "libnr/nr-matrix.h"
-#include "libnr/nr-matrix-fns.h"
+#include <2geom/matrix.h>
 #include "util/fixed_point.h"
 #include "preferences.h"
 
+#ifndef INK_UNUSED
+#define INK_UNUSED(x) ((void)(x))
+#endif
+
 // IIR filtering method based on:
 // L.J. van Vliet, I.T. Young, and P.W. Verbeek, Recursive Gaussian Derivative Filters,
 // in: A.K. Jain, S. Venkatesh, B.C. Lovell (eds.),
@@ -56,7 +59,7 @@
 static size_t const N = 3;
 
 template<typename InIt, typename OutIt, typename Size>
-void copy_n(InIt beg_in, Size N, OutIt beg_out) {
+inline void copy_n(InIt beg_in, Size N, OutIt beg_out) {
     std::copy(beg_in, beg_in+N, beg_out);
 }
 
@@ -87,7 +90,8 @@ static inline Tt clip_round_cast(Ts const& v, Tt const minval=std::numeric_limit
     return round_cast<Tt>(v);
 }
 
-namespace NR {
+namespace Inkscape {
+namespace Filters {
 
 FilterGaussian::FilterGaussian()
 {
@@ -282,6 +286,8 @@ filter2D_IIR(PT *const dest, int const dstr1, int const dstr2,
 {
 #if HAVE_OPENMP
 #pragma omp parallel for num_threads(num_threads)
+#else
+    INK_UNUSED(num_threads);
 #endif // HAVE_OPENMP
     for ( int c2 = 0 ; c2 < n2 ; c2++ ) {
 #if HAVE_OPENMP
@@ -339,7 +345,7 @@ filter2D_IIR(PT *const dest, int const dstr1, int const dstr2,
 
 // Filters over 1st dimension
 // Assumes kernel is symmetric
-// scr_len should be size of kernel - 1
+// Kernel should have scr_len+1 elements
 template<typename PT, unsigned int PC>
 static void
 filter2D_FIR(PT *const dst, int const dstr1, int const dstr2,
@@ -351,6 +357,8 @@ filter2D_FIR(PT *const dst, int const dstr1, int const dstr2,
 
 #if HAVE_OPENMP
 #pragma omp parallel for num_threads(num_threads) private(history)
+#else
+    INK_UNUSED(num_threads);
 #endif // HAVE_OPENMP
     for ( int c2 = 0 ; c2 < n2 ; c2++ ) {
 
@@ -369,7 +377,7 @@ filter2D_FIR(PT *const dst, int const dstr1, int const dstr2,
         for ( int c1 = 0 ; c1 < n1 ; c1++ ) {
 
             int const src_disp = src_line + c1 * sstr1;
-            int const dst_disp = dst_line + c1 * sstr1;
+            int const dst_disp = dst_line + c1 * dstr1;
 
             // update history
             for(int i=scr_len; i>0; i--) copy_n(history[i-1], PC, history[i]);
@@ -425,7 +433,7 @@ filter2D_FIR(PT *const dst, int const dstr1, int const dstr2,
                 // optimization: if there was no variation within this point's neighborhood,
                 // skip ahead while we keep seeing the same last_in byte:
                 // blurring flat color would not change it anyway
-                if (different_count <= 1) {
+                if (different_count <= 1) { // note that different_count is at least 1, because last_in is initialized to -1
                     int pos = c1 + 1;
                     int nb_src_disp = src_disp + (1+scr_len)*sstr1 + byte; // src_line + (pos+scr_len) * sstr1 + byte
                     int nb_dst_disp = dst_disp + (1)        *dstr1 + byte; // dst_line + (pos) * sstr1 + byte
@@ -433,7 +441,7 @@ filter2D_FIR(PT *const dst, int const dstr1, int const dstr2,
                         dst[nb_dst_disp] = last_in;
                         pos++;
                         nb_src_disp += sstr1;
-                        nb_dst_disp += sstr1;
+                        nb_dst_disp += dstr1;
                     }
                     skipbuf[byte] = pos;
                 }
@@ -531,48 +539,64 @@ upsample(PT *const dst, int const dstr1, int const dstr2, unsigned int const dn1
 
 int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
 {
-    /* in holds the input pixblock */
-    NRPixBlock *in = slot.get(_input);
-    if (!in) {
-        g_warning("Missing source image for feGaussianBlur (in=%d)", _input);
-        return 1;
-    }
+    // TODO: Meaningful return values? (If they're checked at all.)
 
-    Matrix trans = units.get_matrix_primitiveunits2pb();
+    /* in holds the input pixblock */
+    NRPixBlock *original_in = slot.get(_input);
 
     /* If to either direction, the standard deviation is zero or
      * input image is not defined,
      * a transparent black image should be returned. */
-    if (_deviation_x <= 0 || _deviation_y <= 0 || in == NULL) {
-        NRPixBlock *out = new NRPixBlock;
-        if (in == NULL) {
+    if (_deviation_x <= 0 || _deviation_y <= 0 || original_in == NULL) {
+        NRPixBlock *src = original_in;
+        if (src == NULL) {
+            g_warning("Missing source image for feGaussianBlur (in=%d)", _input);
             // A bit guessing here, but source graphic is likely to be of
             // right size
-            in = slot.get(NR_FILTER_SOURCEGRAPHIC);
+            src = slot.get(NR_FILTER_SOURCEGRAPHIC);
         }
-        nr_pixblock_setup_fast(out, in->mode, in->area.x0, in->area.y0,
-                               in->area.x1, in->area.y1, true);
-        if (out->data.px != NULL) {
+        NRPixBlock *out = new NRPixBlock;
+        nr_pixblock_setup_fast(out, src->mode, src->area.x0, src->area.y0,
+                               src->area.x1, src->area.y1, true);
+        if (out->size != NR_PIXBLOCK_SIZE_TINY && out->data.px != NULL) {
             out->empty = false;
             slot.set(_output, out);
         }
         return 0;
     }
 
+    // Gaussian blur is defined to operate on non-premultiplied color values.
+    //   So, convert the input first it uses non-premultiplied color values.
+    //   And please note that this should not be done AFTER resampling, as resampling a non-premultiplied image
+    //   does not simply yield a non-premultiplied version of the resampled premultiplied image!!!
+    NRPixBlock *in = original_in;
+    if (in->mode == NR_PIXBLOCK_MODE_R8G8B8A8N) {
+        in = nr_pixblock_new_fast(NR_PIXBLOCK_MODE_R8G8B8A8P,
+                                  original_in->area.x0, original_in->area.y0,
+                                  original_in->area.x1, original_in->area.y1,
+                                  false);
+        if (!in) {
+            // ran out of memory
+            return 0;
+        }
+        nr_blit_pixblock_pixblock(in, original_in);
+    }
+
+    Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
+
     // Some common constants
-    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
     int const width_org = in->area.x1-in->area.x0, height_org = in->area.y1-in->area.y0;
-    double const deviation_x_org = _deviation_x * NR::expansionX(trans);
-    double const deviation_y_org = _deviation_y * NR::expansionY(trans);
+    double const deviation_x_org = _deviation_x * trans.expansionX();
+    double const deviation_y_org = _deviation_y * trans.expansionY();
     int const PC = NR_PIXBLOCK_BPP(in);
 #if HAVE_OPENMP
-    int const NTHREADS = std::max(1,std::min(8,prefs->getInt("/options/threading/numthreads",omp_get_num_procs())));
+    int const NTHREADS = std::max(1,std::min(8, Inkscape::Preferences::get()->getInt("/options/threading/numthreads", omp_get_num_procs())));
 #else
     int const NTHREADS = 1;
 #endif // HAVE_OPENMP
 
     // Subsampling constants
-    int const quality = prefs->getInt("/options/blurquality/value");
+    int const quality = slot.get_blurquality();
     int const x_step_l2 = _effect_subsample_step_log2(deviation_x_org, quality);
     int const y_step_l2 = _effect_subsample_step_log2(deviation_y_org, quality);
     int const x_step = 1<<x_step_l2;
@@ -599,6 +623,8 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
                                           in->area.x0/x_step+width, in->area.y0/y_step+height, true);
     if (out->size != NR_PIXBLOCK_SIZE_TINY && out->data.px == NULL) {
         // alas, we've accomplished a lot, but ran out of memory - so abort
+        if (in != original_in) nr_pixblock_free(in);
+        delete out;
         return 0;
     }
     // Temporary storage for IIR filter
@@ -609,6 +635,7 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         for(int i=0; i<NTHREADS; i++) {
             tmpdata[i] = new IIRValue[std::max(width,height)*PC];
             if (tmpdata[i] == NULL) {
+                if (in != original_in) nr_pixblock_free(in);
                 nr_pixblock_release(out);
                 while(i-->0) {
                     delete[] tmpdata[i];
@@ -618,6 +645,8 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
             }
         }
     }
+
+    // Resampling (if necessary), goes from in -> out (setting ssin to out if used)
     NRPixBlock *ssin = in;
     if ( resampling ) {
         ssin = out;
@@ -629,10 +658,10 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         case NR_PIXBLOCK_MODE_R8G8B8:    ///< 8 bit RGB
             downsample<unsigned char,3>(NR_PIXBLOCK_PX(out), 3, out->rs, width, height, NR_PIXBLOCK_PX(in), 3, in->rs, width_org, height_org, x_step_l2, y_step_l2);
             break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
-            downsample<unsigned char,4>(NR_PIXBLOCK_PX(out), 4, out->rs, width, height, NR_PIXBLOCK_PX(in), 4, in->rs, width_org, height_org, x_step_l2, y_step_l2);
-            break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8P:  ///< Premultiplied 8 bit RGBA
+        //case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
+        //    downsample<unsigned char,4>(NR_PIXBLOCK_PX(out), 4, out->rs, width, height, NR_PIXBLOCK_PX(in), 4, in->rs, width_org, height_org, x_step_l2, y_step_l2);
+        //    break;
+        case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
             downsample<unsigned char,4>(NR_PIXBLOCK_PX(out), 4, out->rs, width, height, NR_PIXBLOCK_PX(in), 4, in->rs, width_org, height_org, x_step_l2, y_step_l2);
             break;
         default:
@@ -640,6 +669,7 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         };
     }
 
+    // Horizontal filtering, goes from ssin -> out (ssin might be equal to out, but these algorithms can be used in-place)
     if (use_IIR_x) {
         // Filter variables
         IIRValue b[N+1];  // scaling coefficient + filter coefficients (can be 10.21 fixed point)
@@ -666,18 +696,18 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         case NR_PIXBLOCK_MODE_R8G8B8:    ///< 8 bit RGB
             filter2D_IIR<unsigned char,3,false>(NR_PIXBLOCK_PX(out), 3, out->rs, NR_PIXBLOCK_PX(ssin), 3, ssin->rs, width, height, b, M, tmpdata, NTHREADS);
             break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
-            filter2D_IIR<unsigned char,4,false>(NR_PIXBLOCK_PX(out), 4, out->rs, NR_PIXBLOCK_PX(ssin), 4, ssin->rs, width, height, b, M, tmpdata, NTHREADS);
-            break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8P:  ///< Premultiplied 8 bit RGBA
+        //case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
+        //    filter2D_IIR<unsigned char,4,false>(NR_PIXBLOCK_PX(out), 4, out->rs, NR_PIXBLOCK_PX(ssin), 4, ssin->rs, width, height, b, M, tmpdata, NTHREADS);
+        //    break;
+        case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
             filter2D_IIR<unsigned char,4,true >(NR_PIXBLOCK_PX(out), 4, out->rs, NR_PIXBLOCK_PX(ssin), 4, ssin->rs, width, height, b, M, tmpdata, NTHREADS);
             break;
         default:
             assert(false);
         };
-    } else if ( scr_len_x > 1 ) { // !use_IIR_x
+    } else if ( scr_len_x > 0 ) { // !use_IIR_x
         // Filter kernel for x direction
-        FIRValue kernel[scr_len_x];
+        FIRValue kernel[scr_len_x+1];
         _make_kernel(kernel, deviation_x);
 
         // Filter (x)
@@ -688,10 +718,10 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         case NR_PIXBLOCK_MODE_R8G8B8:    ///< 8 bit RGB
             filter2D_FIR<unsigned char,3>(NR_PIXBLOCK_PX(out), 3, out->rs, NR_PIXBLOCK_PX(ssin), 3, ssin->rs, width, height, kernel, scr_len_x, NTHREADS);
             break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
-            filter2D_FIR<unsigned char,4>(NR_PIXBLOCK_PX(out), 4, out->rs, NR_PIXBLOCK_PX(ssin), 4, ssin->rs, width, height, kernel, scr_len_x, NTHREADS);
-            break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8P:  ///< Premultiplied 8 bit RGBA
+        //case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
+        //    filter2D_FIR<unsigned char,4>(NR_PIXBLOCK_PX(out), 4, out->rs, NR_PIXBLOCK_PX(ssin), 4, ssin->rs, width, height, kernel, scr_len_x, NTHREADS);
+        //    break;
+        case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
             filter2D_FIR<unsigned char,4>(NR_PIXBLOCK_PX(out), 4, out->rs, NR_PIXBLOCK_PX(ssin), 4, ssin->rs, width, height, kernel, scr_len_x, NTHREADS);
             break;
         default:
@@ -701,6 +731,7 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         nr_blit_pixblock_pixblock(out, ssin);
     }
 
+    // Vertical filtering, goes from out -> out
     if (use_IIR_y) {
         // Filter variables
         IIRValue b[N+1];  // scaling coefficient + filter coefficients (can be 10.21 fixed point)
@@ -727,18 +758,18 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         case NR_PIXBLOCK_MODE_R8G8B8:    ///< 8 bit RGB
             filter2D_IIR<unsigned char,3,false>(NR_PIXBLOCK_PX(out), out->rs, 3, NR_PIXBLOCK_PX(out), out->rs, 3, height, width, b, M, tmpdata, NTHREADS);
             break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
-            filter2D_IIR<unsigned char,4,false>(NR_PIXBLOCK_PX(out), out->rs, 4, NR_PIXBLOCK_PX(out), out->rs, 4, height, width, b, M, tmpdata, NTHREADS);
-            break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8P:  ///< Premultiplied 8 bit RGBA
+        //case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
+        //    filter2D_IIR<unsigned char,4,false>(NR_PIXBLOCK_PX(out), out->rs, 4, NR_PIXBLOCK_PX(out), out->rs, 4, height, width, b, M, tmpdata, NTHREADS);
+        //    break;
+        case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
             filter2D_IIR<unsigned char,4,true >(NR_PIXBLOCK_PX(out), out->rs, 4, NR_PIXBLOCK_PX(out), out->rs, 4, height, width, b, M, tmpdata, NTHREADS);
             break;
         default:
             assert(false);
         };
-    } else if ( scr_len_y > 1 ) { // !use_IIR_y
+    } else if ( scr_len_y > 0 ) { // !use_IIR_y
         // Filter kernel for y direction
-        FIRValue kernel[scr_len_y];
+        FIRValue kernel[scr_len_y+1];
         _make_kernel(kernel, deviation_y);
 
         // Filter (y)
@@ -749,10 +780,10 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         case NR_PIXBLOCK_MODE_R8G8B8:    ///< 8 bit RGB
             filter2D_FIR<unsigned char,3>(NR_PIXBLOCK_PX(out), out->rs, 3, NR_PIXBLOCK_PX(out), out->rs, 3, height, width, kernel, scr_len_y, NTHREADS);
             break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
-            filter2D_FIR<unsigned char,4>(NR_PIXBLOCK_PX(out), out->rs, 4, NR_PIXBLOCK_PX(out), out->rs, 4, height, width, kernel, scr_len_y, NTHREADS);
-            break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8P:  ///< Premultiplied 8 bit RGBA
+        //case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
+        //    filter2D_FIR<unsigned char,4>(NR_PIXBLOCK_PX(out), out->rs, 4, NR_PIXBLOCK_PX(out), out->rs, 4, height, width, kernel, scr_len_y, NTHREADS);
+        //    break;
+        case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
             filter2D_FIR<unsigned char,4>(NR_PIXBLOCK_PX(out), out->rs, 4, NR_PIXBLOCK_PX(out), out->rs, 4, height, width, kernel, scr_len_y, NTHREADS);
             break;
         default:
@@ -764,6 +795,7 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         delete[] tmpdata[i]; // deleting a nullptr has no effect, so this is safe
     }
 
+    // Upsampling, stores (the upsampled) out using slot.set(_output, ...)
     if ( !resampling ) {
         // No upsampling needed
         out->empty = FALSE;
@@ -775,6 +807,7 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
                                                    in->area.x1, in->area.y1, true);
         if (finalout->size != NR_PIXBLOCK_SIZE_TINY && finalout->data.px == NULL) {
             // alas, we've accomplished a lot, but ran out of memory - so abort
+            if (in != original_in) nr_pixblock_free(in);
             nr_pixblock_release(out);
             delete out;
             return 0;
@@ -788,10 +821,10 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         case NR_PIXBLOCK_MODE_R8G8B8:    ///< 8 bit RGB
             upsample<unsigned char,3>(NR_PIXBLOCK_PX(finalout), 3, finalout->rs, width_org, height_org, NR_PIXBLOCK_PX(out), 3, out->rs, width, height, x_step_l2, y_step_l2);
             break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
-            upsample<unsigned char,4>(NR_PIXBLOCK_PX(finalout), 4, finalout->rs, width_org, height_org, NR_PIXBLOCK_PX(out), 4, out->rs, width, height, x_step_l2, y_step_l2);
-            break;
-        case NR_PIXBLOCK_MODE_R8G8B8A8P:  ///< Premultiplied 8 bit RGBA
+        //case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
+        //    upsample<unsigned char,4>(NR_PIXBLOCK_PX(finalout), 4, finalout->rs, width_org, height_org, NR_PIXBLOCK_PX(out), 4, out->rs, width, height, x_step_l2, y_step_l2);
+        //    break;
+        case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
             upsample<unsigned char,4>(NR_PIXBLOCK_PX(finalout), 4, finalout->rs, width_org, height_org, NR_PIXBLOCK_PX(out), 4, out->rs, width, height, x_step_l2, y_step_l2);
             break;
         default:
@@ -807,13 +840,16 @@ int FilterGaussian::render(FilterSlot &slot, FilterUnits const &units)
         slot.set(_output, finalout);
     }
 
+    // If we downsampled the input, clean up the downsampled data
+    if (in != original_in) nr_pixblock_free(in);
+
     return 0;
 }
 
-void FilterGaussian::area_enlarge(NRRectL &area, Matrix const &trans)
+void FilterGaussian::area_enlarge(NRRectL &area, Geom::Matrix const &trans)
 {
-    int area_x = _effect_area_scr(_deviation_x * NR::expansionX(trans));
-    int area_y = _effect_area_scr(_deviation_y * NR::expansionY(trans));
+    int area_x = _effect_area_scr(_deviation_x * trans.expansionX());
+    int area_y = _effect_area_scr(_deviation_y * trans.expansionY());
     // maximum is used because rotations can mix up these directions
     // TODO: calculate a more tight-fitting rendering area
     int area_max = std::max(area_x, area_y);
@@ -842,7 +878,8 @@ void FilterGaussian::set_deviation(double x, double y)
     }
 }
 
-} /* namespace NR */
+} /* namespace Filters */
+} /* namespace Inkscape */
 
 /*
   Local Variables: