1 #define __NR_FILTER_GAUSSIAN_CPP__
3 /*
4 * Gaussian blur renderer
5 *
6 * Authors:
7 * Niko Kiirala <niko@kiirala.com>
8 * bulia byak
9 * Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
10 *
11 * Copyright (C) 2006 authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include <algorithm>
17 #include <cmath>
18 #include <complex>
19 #include <glib.h>
20 #include <limits>
22 #include "isnan.h"
24 #include "display/nr-filter-primitive.h"
25 #include "display/nr-filter-gaussian.h"
26 #include "display/nr-filter-types.h"
27 #include "libnr/nr-pixblock.h"
28 #include "libnr/nr-matrix.h"
29 #include "util/fixed_point.h"
30 #include "prefs-utils.h"
32 // IIR filtering method based on:
33 // L.J. van Vliet, I.T. Young, and P.W. Verbeek, Recursive Gaussian Derivative Filters,
34 // in: A.K. Jain, S. Venkatesh, B.C. Lovell (eds.),
35 // ICPR'98, Proc. 14th Int. Conference on Pattern Recognition (Brisbane, Aug. 16-20),
36 // IEEE Computer Society Press, Los Alamitos, 1998, 509-514.
37 //
38 // Using the backwards-pass initialization procedure from:
39 // Boundary Conditions for Young - van Vliet Recursive Filtering
40 // Bill Triggs, Michael Sdika
41 // IEEE Transactions on Signal Processing, Volume 54, Number 5 - may 2006
43 // Number of IIR filter coefficients used. Currently only 3 is supported.
44 // "Recursive Gaussian Derivative Filters" says this is enough though (and
45 // some testing indeed shows that the quality doesn't improve much if larger
46 // filters are used).
47 static size_t const N = 3;
49 template<typename InIt, typename OutIt, typename Size>
50 void copy_n(InIt beg_in, Size N, OutIt beg_out) {
51 std::copy(beg_in, beg_in+N, beg_out);
52 }
54 // Type used for IIR filter coefficients (can be 10.21 signed fixed point, see Anisotropic Gaussian Filtering Using Fixed Point Arithmetic, Christoph H. Lampert & Oliver Wirjadi, 2006)
55 typedef double IIRValue;
57 // Type used for FIR filter coefficients (can be 16.16 unsigned fixed point, should have 8 or more bits in the fractional part, the integer part should be capable of storing approximately 20*255)
58 typedef Inkscape::Util::FixedPoint<unsigned int,16> FIRValue;
60 template<typename T> static inline T sqr(T const& v) { return v*v; }
62 template<typename T> static inline T clip(T const& v, T const& a, T const& b) {
63 if ( v < a ) return a;
64 if ( v > b ) return b;
65 return v;
66 }
68 template<typename Tt, typename Ts>
69 static inline Tt round_cast(Ts const& v) {
70 static Ts const rndoffset(.5);
71 return static_cast<Tt>(v+rndoffset);
72 }
74 template<typename Tt, typename Ts>
75 static inline Tt clip_round_cast(Ts const& v, Tt const minval=std::numeric_limits<Tt>::min(), Tt const maxval=std::numeric_limits<Tt>::max()) {
76 if ( v < minval ) return minval;
77 if ( v > maxval ) return maxval;
78 return round_cast<Tt>(v);
79 }
81 namespace NR {
83 FilterGaussian::FilterGaussian()
84 {
85 _deviation_x = _deviation_y = prefs_get_double_attribute("options.filtertest", "value", 1.0);
86 }
88 FilterPrimitive *FilterGaussian::create()
89 {
90 return new FilterGaussian();
91 }
93 FilterGaussian::~FilterGaussian()
94 {
95 // Nothing to do here
96 }
98 static int
99 _effect_area_scr(double const deviation)
100 {
101 return (int)std::ceil(deviation * 3.0);
102 }
104 static void
105 _make_kernel(FIRValue *const kernel, double const deviation)
106 {
107 int const scr_len = _effect_area_scr(deviation);
108 double const d_sq = sqr(deviation) * 2;
109 double k[scr_len+1]; // This is only called for small kernel sizes (above approximately 10 coefficients the IIR filter is used)
111 // Compute kernel and sum of coefficients
112 // Note that actually only half the kernel is computed, as it is symmetric
113 double sum = 0;
114 for ( int i = scr_len; i >= 0 ; i-- ) {
115 k[i] = std::exp(-sqr(i) / d_sq);
116 if ( i > 0 ) sum += k[i];
117 }
118 // the sum of the complete kernel is twice as large (plus the center element which we skipped above to prevent counting it twice)
119 sum = 2*sum + k[0];
121 // Normalize kernel (making sure the sum is exactly 1)
122 double ksum = 0;
123 FIRValue kernelsum = 0;
124 for ( int i = scr_len; i >= 1 ; i-- ) {
125 ksum += k[i]/sum;
126 kernel[i] = ksum-static_cast<double>(kernelsum);
127 kernelsum += kernel[i];
128 }
129 kernel[0] = FIRValue(1)-2*kernelsum;
130 }
132 // Return value (v) should satisfy:
133 // 2^(2*v)*255<2^32
134 // 255<2^(32-2*v)
135 // 2^8<=2^(32-2*v)
136 // 8<=32-2*v
137 // 2*v<=24
138 // v<=12
139 static int
140 _effect_subsample_step_log2(double const deviation, int const quality)
141 {
142 // To make sure FIR will always be used (unless the kernel is VERY big):
143 // deviation/step <= 3
144 // deviation/3 <= step
145 // log(deviation/3) <= log(step)
146 // So when x below is >= 1/3 FIR will almost always be used.
147 // This means IIR is almost only used with the modes BETTER or BEST.
148 int stepsize_l2;
149 switch (quality) {
150 case BLUR_QUALITY_WORST:
151 // 2 == log(x*8/3))
152 // 2^2 == x*2^3/3
153 // x == 3/2
154 stepsize_l2 = clip(static_cast<int>(log(deviation*(3./2.))/log(2.)), 0, 12);
155 break;
156 case BLUR_QUALITY_WORSE:
157 // 2 == log(x*16/3))
158 // 2^2 == x*2^4/3
159 // x == 3/2^2
160 stepsize_l2 = clip(static_cast<int>(log(deviation*(3./4.))/log(2.)), 0, 12);
161 break;
162 case BLUR_QUALITY_BETTER:
163 // 2 == log(x*32/3))
164 // 2 == x*2^5/3
165 // x == 3/2^4
166 stepsize_l2 = clip(static_cast<int>(log(deviation*(3./16.))/log(2.)), 0, 12);
167 break;
168 case BLUR_QUALITY_BEST:
169 stepsize_l2 = 0; // no subsampling at all
170 break;
171 case BLUR_QUALITY_NORMAL:
172 default:
173 // 2 == log(x*16/3))
174 // 2 == x*2^4/3
175 // x == 3/2^3
176 stepsize_l2 = clip(static_cast<int>(log(deviation*(3./8.))/log(2.)), 0, 12);
177 break;
178 }
179 return stepsize_l2;
180 }
182 /**
183 * Sanity check function for indexing pixblocks.
184 * Catches reading and writing outside the pixblock area.
185 * When enabled, decreases filter rendering speed massively.
186 */
187 static inline void
188 _check_index(NRPixBlock const * const pb, int const location, int const line)
189 {
190 if (false) {
191 int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
192 if (location < 0 || location >= max_loc)
193 g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
194 }
195 }
197 static void calcFilter(double const sigma, double b[N]) {
198 assert(N==3);
199 std::complex<double> const d1_org(1.40098, 1.00236);
200 double const d3_org = 1.85132;
201 double qbeg = 1; // Don't go lower than sigma==2 (we'd probably want a normal convolution in that case anyway)
202 double qend = 2*sigma;
203 double const sigmasqr = sqr(sigma);
204 double s;
205 do { // Binary search for right q (a linear interpolation scheme is suggested, but this should work fine as well)
206 double const q = (qbeg+qend)/2;
207 // Compute scaled filter coefficients
208 std::complex<double> const d1 = pow(d1_org, 1.0/q);
209 double const d3 = pow(d3_org, 1.0/q);
210 double const absd1sqr = std::norm(d1);
211 double const re2d1 = 2*d1.real();
212 double const bscale = 1.0/(absd1sqr*d3);
213 b[2] = -bscale;
214 b[1] = bscale*(d3+re2d1);
215 b[0] = -bscale*(absd1sqr+d3*re2d1);
216 // Compute actual sigma^2
217 double const ssqr = 2*(2*(d1/sqr(d1-1.)).real()+d3/sqr(d3-1.));
218 if ( ssqr < sigmasqr ) {
219 qbeg = q;
220 } else {
221 qend = q;
222 }
223 s = sqrt(ssqr);
224 } while(qend-qbeg>(sigma/(1<<30)));
225 }
227 static void calcTriggsSdikaM(double const b[N], double M[N*N]) {
228 assert(N==3);
229 double a1=b[0], a2=b[1], a3=b[2];
230 double const Mscale = 1.0/((1+a1-a2+a3)*(1-a1-a2-a3)*(1+a2+(a1-a3)*a3));
231 M[0] = 1-a2-a1*a3-sqr(a3);
232 M[1] = (a1+a3)*(a2+a1*a3);
233 M[2] = a3*(a1+a2*a3);
234 M[3] = a1+a2*a3;
235 M[4] = (1-a2)*(a2+a1*a3);
236 M[5] = a3*(1-a2-a1*a3-sqr(a3));
237 M[6] = a1*(a1+a3)+a2*(1-a2);
238 M[7] = a1*(a2-sqr(a3))+a3*(1+a2*(a2-1)-sqr(a3));
239 M[8] = a3*(a1+a2*a3);
240 for(unsigned int i=0; i<9; i++) M[i] *= Mscale;
241 }
243 template<unsigned int SIZE>
244 static void calcTriggsSdikaInitialization(double const M[N*N], IIRValue const uold[N][SIZE], IIRValue const uplus[SIZE], IIRValue const vplus[SIZE], IIRValue const alpha, IIRValue vold[N][SIZE]) {
245 for(unsigned int c=0; c<SIZE; c++) {
246 double uminp[N];
247 for(unsigned int i=0; i<N; i++) uminp[i] = uold[i][c] - uplus[c];
248 for(unsigned int i=0; i<N; i++) {
249 double voldf = 0;
250 for(unsigned int j=0; j<N; j++) {
251 voldf += uminp[j]*M[i*N+j];
252 }
253 // Properly takes care of the scaling coefficient alpha and vplus (which is already appropriately scaled)
254 // This was arrived at by starting from a version of the blur filter that ignored the scaling coefficient
255 // (and scaled the final output by alpha^2) and then gradually reintroducing the scaling coefficient.
256 vold[i][c] = voldf*alpha;
257 vold[i][c] += vplus[c];
258 }
259 }
260 }
262 // Filters over 1st dimension
263 template<typename PT, unsigned int PC, bool PREMULTIPLIED_ALPHA>
264 static void
265 filter2D_IIR(PT *const dest, int const dstr1, int const dstr2,
266 PT const *const src, int const sstr1, int const sstr2,
267 int const n1, int const n2, IIRValue const b[N+1], double const M[N*N],
268 IIRValue *const tmpdata)
269 {
270 for ( int c2 = 0 ; c2 < n2 ; c2++ ) {
271 // corresponding line in the source and output buffer
272 PT const * srcimg = src + c2*sstr2;
273 PT * dstimg = dest + c2*dstr2 + n1*dstr1;
274 // Border constants
275 IIRValue imin[PC]; copy_n(srcimg + (0)*sstr1, PC, imin);
276 IIRValue iplus[PC]; copy_n(srcimg + (n1-1)*sstr1, PC, iplus);
277 // Forward pass
278 IIRValue u[N+1][PC];
279 for(unsigned int i=0; i<N; i++) copy_n(imin, PC, u[i]);
280 for ( int c1 = 0 ; c1 < n1 ; c1++ ) {
281 for(unsigned int i=N; i>0; i--) copy_n(u[i-1], PC, u[i]);
282 copy_n(srcimg, PC, u[0]);
283 srcimg += sstr1;
284 for(unsigned int c=0; c<PC; c++) u[0][c] *= b[0];
285 for(unsigned int i=1; i<N+1; i++) {
286 for(unsigned int c=0; c<PC; c++) u[0][c] += u[i][c]*b[i];
287 }
288 copy_n(u[0], PC, tmpdata+c1*PC);
289 }
290 // Backward pass
291 IIRValue v[N+1][PC];
292 calcTriggsSdikaInitialization<PC>(M, u, iplus, iplus, b[0], v);
293 dstimg -= dstr1;
294 if ( PREMULTIPLIED_ALPHA ) {
295 dstimg[PC-1] = clip_round_cast<PT>(v[0][PC-1]);
296 for(unsigned int c=0; c<PC-1; c++) dstimg[c] = clip_round_cast<PT>(v[0][c], std::numeric_limits<PT>::min(), dstimg[PC-1]);
297 } else {
298 for(unsigned int c=0; c<PC; c++) dstimg[c] = clip_round_cast<PT>(v[0][c]);
299 }
300 int c1=n1-1;
301 while(c1-->0) {
302 for(unsigned int i=N; i>0; i--) copy_n(v[i-1], PC, v[i]);
303 copy_n(tmpdata+c1*PC, PC, v[0]);
304 for(unsigned int c=0; c<PC; c++) v[0][c] *= b[0];
305 for(unsigned int i=1; i<N+1; i++) {
306 for(unsigned int c=0; c<PC; c++) v[0][c] += v[i][c]*b[i];
307 }
308 dstimg -= dstr1;
309 if ( PREMULTIPLIED_ALPHA ) {
310 dstimg[PC-1] = clip_round_cast<PT>(v[0][PC-1]);
311 for(unsigned int c=0; c<PC-1; c++) dstimg[c] = clip_round_cast<PT>(v[0][c], std::numeric_limits<PT>::min(), dstimg[PC-1]);
312 } else {
313 for(unsigned int c=0; c<PC; c++) dstimg[c] = clip_round_cast<PT>(v[0][c]);
314 }
315 }
316 }
317 }
319 // Filters over 1st dimension
320 // Assumes kernel is symmetric
321 // scr_len should be size of kernel - 1
322 template<typename PT, unsigned int PC>
323 static void
324 filter2D_FIR(PT *const dst, int const dstr1, int const dstr2,
325 PT const *const src, int const sstr1, int const sstr2,
326 int const n1, int const n2, FIRValue const *const kernel, int const scr_len)
327 {
328 // Past pixels seen (to enable in-place operation)
329 PT history[scr_len+1][PC];
331 for ( int c2 = 0 ; c2 < n2 ; c2++ ) {
333 // corresponding line in the source buffer
334 int const src_line = c2 * sstr2;
336 // current line in the output buffer
337 int const dst_line = c2 * dstr2;
339 int skipbuf[4] = {INT_MIN, INT_MIN, INT_MIN, INT_MIN};
341 // history initialization
342 PT imin[PC]; copy_n(src + src_line, PC, imin);
343 for(int i=0; i<scr_len; i++) copy_n(imin, PC, history[i]);
345 for ( int c1 = 0 ; c1 < n1 ; c1++ ) {
347 int const src_disp = src_line + c1 * sstr1;
348 int const dst_disp = dst_line + c1 * sstr1;
350 // update history
351 for(int i=scr_len; i>0; i--) copy_n(history[i-1], PC, history[i]);
352 copy_n(src + src_disp, PC, history[0]);
354 // for all bytes of the pixel
355 for ( unsigned int byte = 0 ; byte < PC ; byte++) {
357 if(skipbuf[byte] > c1) continue;
359 FIRValue sum = 0;
360 int last_in = -1;
361 int different_count = 0;
363 // go over our point's neighbours in the history
364 for ( int i = 0 ; i <= scr_len ; i++ ) {
365 // value at the pixel
366 PT in_byte = history[i][byte];
368 // is it the same as last one we saw?
369 if(in_byte != last_in) different_count++;
370 last_in = in_byte;
372 // sum pixels weighted by the kernel
373 sum += in_byte * kernel[i];
374 }
376 // go over our point's neighborhood on x axis in the in buffer
377 int nb_src_disp = src_disp + byte;
378 for ( int i = 1 ; i <= scr_len ; i++ ) {
379 // the pixel we're looking at
380 int c1_in = c1 + i;
381 if (c1_in >= n1) {
382 c1_in = n1 - 1;
383 } else {
384 nb_src_disp += sstr1;
385 }
387 // value at the pixel
388 PT in_byte = src[nb_src_disp];
390 // is it the same as last one we saw?
391 if(in_byte != last_in) different_count++;
392 last_in = in_byte;
394 // sum pixels weighted by the kernel
395 sum += in_byte * kernel[i];
396 }
398 // store the result in bufx
399 dst[dst_disp + byte] = round_cast<PT>(sum);
401 // optimization: if there was no variation within this point's neighborhood,
402 // skip ahead while we keep seeing the same last_in byte:
403 // blurring flat color would not change it anyway
404 if (different_count <= 1) {
405 int pos = c1 + 1;
406 int nb_src_disp = src_disp + (1+scr_len)*sstr1 + byte; // src_line + (pos+scr_len) * sstr1 + byte
407 int nb_dst_disp = dst_disp + (1) *dstr1 + byte; // dst_line + (pos) * sstr1 + byte
408 while(pos + scr_len < n1 && src[nb_src_disp] == last_in) {
409 dst[nb_dst_disp] = last_in;
410 pos++;
411 nb_src_disp += sstr1;
412 nb_dst_disp += sstr1;
413 }
414 skipbuf[byte] = pos;
415 }
416 }
417 }
418 }
419 }
421 template<typename PT, unsigned int PC>
422 static void
423 downsample(PT *const dst, int const dstr1, int const dstr2, int const dn1, int const dn2,
424 PT const *const src, int const sstr1, int const sstr2, int const sn1, int const sn2,
425 int const step1_l2, int const step2_l2)
426 {
427 unsigned int const divisor_l2 = step1_l2+step2_l2; // step1*step2=2^(step1_l2+step2_l2)
428 unsigned int const round_offset = (1<<divisor_l2)/2;
429 int const step1 = 1<<step1_l2;
430 int const step2 = 1<<step2_l2;
431 int const step1_2 = step1/2;
432 int const step2_2 = step2/2;
433 for(int dc2 = 0 ; dc2 < dn2 ; dc2++) {
434 int const sc2_begin = (dc2<<step2_l2)-step2_2;
435 int const sc2_end = sc2_begin+step2;
436 for(int dc1 = 0 ; dc1 < dn1 ; dc1++) {
437 int const sc1_begin = (dc1<<step1_l2)-step1_2;
438 int const sc1_end = sc1_begin+step1;
439 unsigned int sum[PC];
440 std::fill_n(sum, PC, 0);
441 for(int sc2 = sc2_begin ; sc2 < sc2_end ; sc2++) {
442 for(int sc1 = sc1_begin ; sc1 < sc1_end ; sc1++) {
443 for(unsigned int ch = 0 ; ch < PC ; ch++) {
444 sum[ch] += src[clip(sc2,0,sn2-1)*sstr2+clip(sc1,0,sn1-1)*sstr1+ch];
445 }
446 }
447 }
448 for(unsigned int ch = 0 ; ch < PC ; ch++) {
449 dst[dc2*dstr2+dc1*dstr1+ch] = static_cast<PT>((sum[ch]+round_offset)>>divisor_l2);
450 }
451 }
452 }
453 }
455 template<typename PT, unsigned int PC>
456 static void
457 upsample(PT *const dst, int const dstr1, int const dstr2, unsigned int const dn1, unsigned int const dn2,
458 PT const *const src, int const sstr1, int const sstr2, unsigned int const sn1, unsigned int const sn2,
459 unsigned int const step1_l2, unsigned int const step2_l2)
460 {
461 assert(((sn1-1)<<step1_l2)>=dn1 && ((sn2-1)<<step2_l2)>=dn2); // The last pixel of the source image should fall outside the destination image
462 unsigned int const divisor_l2 = step1_l2+step2_l2; // step1*step2=2^(step1_l2+step2_l2)
463 unsigned int const round_offset = (1<<divisor_l2)/2;
464 unsigned int const step1 = 1<<step1_l2;
465 unsigned int const step2 = 1<<step2_l2;
466 for ( unsigned int sc2 = 0 ; sc2 < sn2-1 ; sc2++ ) {
467 unsigned int const dc2_begin = (sc2 << step2_l2);
468 unsigned int const dc2_end = std::min(dn2, dc2_begin+step2);
469 for ( unsigned int sc1 = 0 ; sc1 < sn1-1 ; sc1++ ) {
470 unsigned int const dc1_begin = (sc1 << step1_l2);
471 unsigned int const dc1_end = std::min(dn1, dc1_begin+step1);
472 for ( unsigned int byte = 0 ; byte < PC ; byte++) {
474 // get 4 values at the corners of the pixel from src
475 PT a00 = src[sstr2* sc2 + sstr1* sc1 + byte];
476 PT a10 = src[sstr2* sc2 + sstr1*(sc1+1) + byte];
477 PT a01 = src[sstr2*(sc2+1) + sstr1* sc1 + byte];
478 PT a11 = src[sstr2*(sc2+1) + sstr1*(sc1+1) + byte];
480 // initialize values for linear interpolation
481 unsigned int a0 = a00*step2/*+a01*0*/;
482 unsigned int a1 = a10*step2/*+a11*0*/;
484 // iterate over the rectangle to be interpolated
485 for ( unsigned int dc2 = dc2_begin ; dc2 < dc2_end ; dc2++ ) {
487 // prepare linear interpolation for this row
488 unsigned int a = a0*step1/*+a1*0*/+round_offset;
490 for ( unsigned int dc1 = dc1_begin ; dc1 < dc1_end ; dc1++ ) {
492 // simple linear interpolation
493 dst[dstr2*dc2 + dstr1*dc1 + byte] = static_cast<PT>(a>>divisor_l2);
495 // compute a = a0*(ix-1)+a1*(xi+1)+round_offset
496 a = a - a0 + a1;
497 }
499 // compute a0 = a00*(iy-1)+a01*(yi+1) and similar for a1
500 a0 = a0 - a00 + a01;
501 a1 = a1 - a10 + a11;
502 }
503 }
504 }
505 }
506 }
508 int FilterGaussian::render(FilterSlot &slot, Matrix const &trans)
509 {
510 /* in holds the input pixblock */
511 NRPixBlock *in = slot.get(_input);
513 /* If to either direction, the standard deviation is zero or
514 * input image is not defined,
515 * a transparent black image should be returned. */
516 if (_deviation_x <= 0 || _deviation_y <= 0 || in == NULL) {
517 NRPixBlock *out = new NRPixBlock;
518 if (in == NULL) {
519 // A bit guessing here, but source graphic is likely to be of
520 // right size
521 in = slot.get(NR_FILTER_SOURCEGRAPHIC);
522 }
523 nr_pixblock_setup_fast(out, in->mode, in->area.x0, in->area.y0,
524 in->area.x1, in->area.y1, true);
525 if (out->data.px != NULL) {
526 out->empty = false;
527 slot.set(_output, out);
528 }
529 return 0;
530 }
532 // Some common constants
533 int const width_org = in->area.x1-in->area.x0, height_org = in->area.y1-in->area.y0;
534 double const deviation_x_org = _deviation_x * trans.expansionX();
535 double const deviation_y_org = _deviation_y * trans.expansionY();
536 int const PC = NR_PIXBLOCK_BPP(in);
538 // Subsampling constants
539 int const quality = prefs_get_int_attribute("options.blurquality", "value", 0);
540 int const x_step_l2 = _effect_subsample_step_log2(deviation_x_org, quality);
541 int const y_step_l2 = _effect_subsample_step_log2(deviation_y_org, quality);
542 int const x_step = 1<<x_step_l2;
543 int const y_step = 1<<y_step_l2;
544 bool const resampling = x_step > 1 || y_step > 1;
545 int const width = resampling ? static_cast<int>(ceil(static_cast<double>(width_org)/x_step))+1 : width_org;
546 int const height = resampling ? static_cast<int>(ceil(static_cast<double>(height_org)/y_step))+1 : height_org;
547 double const deviation_x = deviation_x_org / x_step;
548 double const deviation_y = deviation_y_org / y_step;
549 int const scr_len_x = _effect_area_scr(deviation_x);
550 int const scr_len_y = _effect_area_scr(deviation_y);
552 // Decide which filter to use for X and Y
553 // This threshold was determined by trial-and-error for one specific machine,
554 // so there's a good chance that it's not optimal.
555 // Whatever you do, don't go below 1 (and preferrably not even below 2), as
556 // the IIR filter gets unstable there.
557 bool const use_IIR_x = deviation_x > 3;
558 bool const use_IIR_y = deviation_y > 3;
560 // new buffer for the subsampled output
561 NRPixBlock *out = new NRPixBlock;
562 nr_pixblock_setup_fast(out, in->mode, in->area.x0/x_step, in->area.y0/y_step,
563 in->area.x0/x_step+width, in->area.y0/y_step+height, true);
564 if (out->size != NR_PIXBLOCK_SIZE_TINY && out->data.px == NULL) {
565 // alas, we've accomplished a lot, but ran out of memory - so abort
566 return 0;
567 }
568 // Temporary storage for IIR filter
569 // NOTE: This can be eliminated, but it reduces the precision a bit
570 IIRValue * tmpdata = 0;
571 if ( use_IIR_x || use_IIR_y ) {
572 tmpdata = new IIRValue[std::max(width,height)*PC];
573 if (tmpdata == NULL) {
574 nr_pixblock_release(out);
575 delete out;
576 return 0;
577 }
578 }
579 NRPixBlock *ssin = in;
580 if ( resampling ) {
581 ssin = out;
582 // Downsample
583 switch(in->mode) {
584 case NR_PIXBLOCK_MODE_A8: ///< Grayscale
585 downsample<unsigned char,1>(NR_PIXBLOCK_PX(out), 1, out->rs, width, height, NR_PIXBLOCK_PX(in), 1, in->rs, width_org, height_org, x_step_l2, y_step_l2);
586 break;
587 case NR_PIXBLOCK_MODE_R8G8B8: ///< 8 bit RGB
588 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);
589 break;
590 case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
591 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);
592 break;
593 case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
594 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);
595 break;
596 default:
597 assert(false);
598 };
599 }
601 if (use_IIR_x) {
602 // Filter variables
603 IIRValue b[N+1]; // scaling coefficient + filter coefficients (can be 10.21 fixed point)
604 double bf[N]; // computed filter coefficients
605 double M[N*N]; // matrix used for initialization procedure (has to be double)
607 // Compute filter (x)
608 calcFilter(deviation_x, bf);
609 for(size_t i=0; i<N; i++) bf[i] = -bf[i];
610 b[0] = 1; // b[0] == alpha (scaling coefficient)
611 for(size_t i=0; i<N; i++) {
612 b[i+1] = bf[i];
613 b[0] -= b[i+1];
614 }
616 // Compute initialization matrix (x)
617 calcTriggsSdikaM(bf, M);
619 // Filter (x)
620 switch(in->mode) {
621 case NR_PIXBLOCK_MODE_A8: ///< Grayscale
622 filter2D_IIR<unsigned char,1,false>(NR_PIXBLOCK_PX(out), 1, out->rs, NR_PIXBLOCK_PX(ssin), 1, ssin->rs, width, height, b, M, tmpdata);
623 break;
624 case NR_PIXBLOCK_MODE_R8G8B8: ///< 8 bit RGB
625 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);
626 break;
627 case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
628 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);
629 break;
630 case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
631 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);
632 break;
633 default:
634 assert(false);
635 };
636 } else { // !use_IIR_x
637 // Filter kernel for x direction
638 FIRValue kernel[scr_len_x];
639 _make_kernel(kernel, deviation_x);
641 // Filter (x)
642 switch(in->mode) {
643 case NR_PIXBLOCK_MODE_A8: ///< Grayscale
644 filter2D_FIR<unsigned char,1>(NR_PIXBLOCK_PX(out), 1, out->rs, NR_PIXBLOCK_PX(ssin), 1, ssin->rs, width, height, kernel, scr_len_x);
645 break;
646 case NR_PIXBLOCK_MODE_R8G8B8: ///< 8 bit RGB
647 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);
648 break;
649 case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
650 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);
651 break;
652 case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
653 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);
654 break;
655 default:
656 assert(false);
657 };
658 }
660 if (use_IIR_y) {
661 // Filter variables
662 IIRValue b[N+1]; // scaling coefficient + filter coefficients (can be 10.21 fixed point)
663 double bf[N]; // computed filter coefficients
664 double M[N*N]; // matrix used for initialization procedure (has to be double)
666 // Compute filter (y)
667 calcFilter(deviation_y, bf);
668 for(size_t i=0; i<N; i++) bf[i] = -bf[i];
669 b[0] = 1; // b[0] == alpha (scaling coefficient)
670 for(size_t i=0; i<N; i++) {
671 b[i+1] = bf[i];
672 b[0] -= b[i+1];
673 }
675 // Compute initialization matrix (y)
676 calcTriggsSdikaM(bf, M);
678 // Filter (y)
679 switch(in->mode) {
680 case NR_PIXBLOCK_MODE_A8: ///< Grayscale
681 filter2D_IIR<unsigned char,1,false>(NR_PIXBLOCK_PX(out), out->rs, 1, NR_PIXBLOCK_PX(out), out->rs, 1, height, width, b, M, tmpdata);
682 break;
683 case NR_PIXBLOCK_MODE_R8G8B8: ///< 8 bit RGB
684 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);
685 break;
686 case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
687 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);
688 break;
689 case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
690 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);
691 break;
692 default:
693 assert(false);
694 };
695 } else { // !use_IIR_y
696 // Filter kernel for y direction
697 FIRValue kernel[scr_len_y];
698 _make_kernel(kernel, deviation_y);
700 // Filter (y)
701 switch(in->mode) {
702 case NR_PIXBLOCK_MODE_A8: ///< Grayscale
703 filter2D_FIR<unsigned char,1>(NR_PIXBLOCK_PX(out), out->rs, 1, NR_PIXBLOCK_PX(out), out->rs, 1, height, width, kernel, scr_len_y);
704 break;
705 case NR_PIXBLOCK_MODE_R8G8B8: ///< 8 bit RGB
706 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);
707 break;
708 case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
709 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);
710 break;
711 case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
712 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);
713 break;
714 default:
715 assert(false);
716 };
717 }
719 delete[] tmpdata; // deleting a nullptr has no effect, so this is save
721 if ( !resampling ) {
722 // No upsampling needed
723 out->empty = FALSE;
724 slot.set(_output, out);
725 } else {
726 // New buffer for the final output, same resolution as the in buffer
727 NRPixBlock *finalout = new NRPixBlock;
728 nr_pixblock_setup_fast(finalout, in->mode, in->area.x0, in->area.y0,
729 in->area.x1, in->area.y1, true);
730 if (finalout->size != NR_PIXBLOCK_SIZE_TINY && finalout->data.px == NULL) {
731 // alas, we've accomplished a lot, but ran out of memory - so abort
732 nr_pixblock_release(out);
733 delete out;
734 return 0;
735 }
737 // Upsample
738 switch(in->mode) {
739 case NR_PIXBLOCK_MODE_A8: ///< Grayscale
740 upsample<unsigned char,1>(NR_PIXBLOCK_PX(finalout), 1, finalout->rs, width_org, height_org, NR_PIXBLOCK_PX(out), 1, out->rs, width, height, x_step_l2, y_step_l2);
741 break;
742 case NR_PIXBLOCK_MODE_R8G8B8: ///< 8 bit RGB
743 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);
744 break;
745 case NR_PIXBLOCK_MODE_R8G8B8A8N: ///< Normal 8 bit RGBA
746 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);
747 break;
748 case NR_PIXBLOCK_MODE_R8G8B8A8P: ///< Premultiplied 8 bit RGBA
749 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);
750 break;
751 default:
752 assert(false);
753 };
755 // We don't need the out buffer anymore
756 nr_pixblock_release(out);
757 delete out;
759 // The final out buffer gets returned
760 finalout->empty = FALSE;
761 slot.set(_output, finalout);
762 }
764 return 0;
765 }
767 void FilterGaussian::area_enlarge(NRRectL &area, Matrix const &trans)
768 {
769 int area_x = _effect_area_scr(_deviation_x * trans.expansionX());
770 int area_y = _effect_area_scr(_deviation_y * trans.expansionY());
771 // maximum is used because rotations can mix up these directions
772 // TODO: calculate a more tight-fitting rendering area
773 int area_max = std::max(area_x, area_y);
774 area.x0 -= area_max;
775 area.x1 += area_max;
776 area.y0 -= area_max;
777 area.y1 += area_max;
778 }
780 FilterTraits FilterGaussian::get_input_traits() {
781 return TRAIT_PARALLER;
782 }
784 void FilterGaussian::set_deviation(double deviation)
785 {
786 if(isFinite(deviation) && deviation >= 0) {
787 _deviation_x = _deviation_y = deviation;
788 }
789 }
791 void FilterGaussian::set_deviation(double x, double y)
792 {
793 if(isFinite(x) && x >= 0 && isFinite(y) && y >= 0) {
794 _deviation_x = x;
795 _deviation_y = y;
796 }
797 }
799 } /* namespace NR */
801 /*
802 Local Variables:
803 mode:c++
804 c-file-style:"stroustrup"
805 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
806 indent-tabs-mode:nil
807 fill-column:99
808 End:
809 */
810 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :