nr-filter-gaussian.cpp revision 208e5a33acc4a8ad9d8c0488f047c260346f1258
#define __NR_FILTER_GAUSSIAN_CPP__
/*
* Gaussian blur renderer
*
* Authors:
* Niko Kiirala <niko@kiirala.com>
* bulia byak
* Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
*
* Copyright (C) 2006 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <algorithm>
#include <cmath>
#include <complex>
#include <glib.h>
#include <limits>
#include "isnan.h"
#include "display/nr-filter-primitive.h"
#include "display/nr-filter-gaussian.h"
#include "display/nr-filter-types.h"
#include "libnr/nr-pixblock.h"
#include "libnr/nr-matrix.h"
#include "util/fixed_point.h"
#include "prefs-utils.h"
// 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.),
// ICPR'98, Proc. 14th Int. Conference on Pattern Recognition (Brisbane, Aug. 16-20),
// IEEE Computer Society Press, Los Alamitos, 1998, 509-514.
//
// Using the backwards-pass initialization procedure from:
// Boundary Conditions for Young - van Vliet Recursive Filtering
// Bill Triggs, Michael Sdika
// IEEE Transactions on Signal Processing, Volume 54, Number 5 - may 2006
// Number of IIR filter coefficients used. Currently only 3 is supported.
// "Recursive Gaussian Derivative Filters" says this is enough though (and
// some testing indeed shows that the quality doesn't improve much if larger
// filters are used).
static size_t const N = 3;
}
// 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)
typedef double IIRValue;
// 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)
template<typename T> static inline T sqr(T const& v) { return v*v; }
template<typename T> static inline T clip(T const& v, T const& a, T const& b) {
if ( v < a ) return a;
if ( v > b ) return b;
return v;
}
}
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()) {
return round_cast<Tt>(v);
}
namespace NR {
{
}
{
return new FilterGaussian();
}
{
// Nothing to do here
}
static int
_effect_area_scr(double const deviation)
{
}
static void
{
double k[scr_len+1]; // This is only called for small kernel sizes (above approximately 10 coefficients the IIR filter is used)
// Compute kernel and sum of coefficients
// Note that actually only half the kernel is computed, as it is symmetric
double sum = 0;
for ( int i = scr_len; i >= 0 ; i-- ) {
if ( i > 0 ) sum += k[i];
}
// the sum of the complete kernel is twice as large (plus the center element which we skipped above to prevent counting it twice)
// Normalize kernel (making sure the sum is exactly 1)
double ksum = 0;
for ( int i = scr_len; i >= 1 ; i-- ) {
}
}
// Return value (v) should satisfy:
// 2^(2*v)*255<2^32
// 255<2^(32-2*v)
// 2^8<=2^(32-2*v)
// 8<=32-2*v
// 2*v<=24
// v<=12
static int
{
// To make sure FIR will always be used (unless the kernel is VERY big):
// deviation/3 <= step
// log(deviation/3) <= log(step)
// So when x below is >= 1/3 FIR will almost always be used.
// This means IIR is almost only used with the modes BETTER or BEST.
int stepsize_l2;
switch (quality) {
case BLUR_QUALITY_WORST:
// 2 == log(x*8/3))
// 2^2 == x*2^3/3
// x == 3/2
break;
case BLUR_QUALITY_WORSE:
// 2 == log(x*16/3))
// 2^2 == x*2^4/3
// x == 3/2^2
break;
case BLUR_QUALITY_BETTER:
// 2 == log(x*32/3))
// 2 == x*2^5/3
// x == 3/2^4
break;
case BLUR_QUALITY_BEST:
stepsize_l2 = 0; // no subsampling at all
break;
case BLUR_QUALITY_NORMAL:
default:
// 2 == log(x*16/3))
// 2 == x*2^4/3
// x == 3/2^3
break;
}
return stepsize_l2;
}
/**
* Sanity check function for indexing pixblocks.
* Catches reading and writing outside the pixblock area.
* When enabled, decreases filter rendering speed massively.
*/
static inline void
{
if (false) {
}
}
static void calcFilter(double const sigma, double b[N]) {
assert(N==3);
double const d3_org = 1.85132;
double qbeg = 1; // Don't go lower than sigma==2 (we'd probably want a normal convolution in that case anyway)
double s;
do { // Binary search for right q (a linear interpolation scheme is suggested, but this should work fine as well)
// Compute scaled filter coefficients
b[2] = -bscale;
// Compute actual sigma^2
qbeg = q;
} else {
qend = q;
}
}
static void calcTriggsSdikaM(double const b[N], double M[N*N]) {
assert(N==3);
for(unsigned int i=0; i<9; i++) M[i] *= Mscale;
}
template<unsigned int SIZE>
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]) {
for(unsigned int c=0; c<SIZE; c++) {
double uminp[N];
for(unsigned int i=0; i<N; i++) {
double voldf = 0;
for(unsigned int j=0; j<N; j++) {
}
// Properly takes care of the scaling coefficient alpha and vplus (which is already appropriately scaled)
// This was arrived at by starting from a version of the blur filter that ignored the scaling coefficient
// (and scaled the final output by alpha^2) and then gradually reintroducing the scaling coefficient.
}
}
}
// Filters over 1st dimension
static void
{
// corresponding line in the source and output buffer
// Border constants
// Forward pass
for(unsigned int c=0; c<PC; c++) u[0][c] *= b[0];
for(unsigned int i=1; i<N+1; i++) {
for(unsigned int c=0; c<PC; c++) u[0][c] += u[i][c]*b[i];
}
}
// Backward pass
if ( PREMULTIPLIED_ALPHA ) {
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]);
} else {
}
while(c1-->0) {
for(unsigned int c=0; c<PC; c++) v[0][c] *= b[0];
for(unsigned int i=1; i<N+1; i++) {
for(unsigned int c=0; c<PC; c++) v[0][c] += v[i][c]*b[i];
}
if ( PREMULTIPLIED_ALPHA ) {
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]);
} else {
}
}
}
}
// Filters over 1st dimension
// Assumes kernel is symmetric
// scr_len should be size of kernel - 1
static void
{
// Past pixels seen (to enable in-place operation)
// corresponding line in the source buffer
// current line in the output buffer
// history initialization
// update history
// for all bytes of the pixel
int last_in = -1;
int different_count = 0;
// go over our point's neighbours in the history
for ( int i = 0 ; i <= scr_len ; i++ ) {
// value at the pixel
// is it the same as last one we saw?
// sum pixels weighted by the kernel
}
// go over our point's neighborhood on x axis in the in buffer
for ( int i = 1 ; i <= scr_len ; i++ ) {
// the pixel we're looking at
} else {
nb_src_disp += sstr1;
}
// value at the pixel
// is it the same as last one we saw?
// sum pixels weighted by the kernel
}
// store the result in bufx
// 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) {
pos++;
nb_src_disp += sstr1;
nb_dst_disp += sstr1;
}
}
}
}
}
}
static void
{
}
}
}
}
}
}
}
static void
upsample(PT *const dst, int const dstr1, int const dstr2, unsigned int const dn1, unsigned int const dn2,
PT const *const src, int const sstr1, int const sstr2, unsigned int const sn1, unsigned int const sn2,
{
assert(((sn1-1)<<step1_l2)>=dn1 && ((sn2-1)<<step2_l2)>=dn2); // The last pixel of the source image should fall outside the destination image
// get 4 values at the corners of the pixel from src
// initialize values for linear interpolation
// iterate over the rectangle to be interpolated
// prepare linear interpolation for this row
// simple linear interpolation
// compute a = a0*(ix-1)+a1*(xi+1)+round_offset
}
// compute a0 = a00*(iy-1)+a01*(yi+1) and similar for a1
}
}
}
}
}
{
/* in holds the input pixblock */
/* If to either direction, the standard deviation is zero or
* input image is not defined,
* a transparent black image should be returned. */
// A bit guessing here, but source graphic is likely to be of
// right size
}
}
return 0;
}
// Some common constants
// Subsampling constants
int const width = resampling ? static_cast<int>(ceil(static_cast<double>(width_org)/x_step))+1 : width_org;
int const height = resampling ? static_cast<int>(ceil(static_cast<double>(height_org)/y_step))+1 : height_org;
// Decide which filter to use for X and Y
// This threshold was determined by trial-and-error for one specific machine,
// so there's a good chance that it's not optimal.
// Whatever you do, don't go below 1 (and preferrably not even below 2), as
// the IIR filter gets unstable there.
// new buffer for the subsampled output
// alas, we've accomplished a lot, but ran out of memory - so abort
return 0;
}
// Temporary storage for IIR filter
// NOTE: This can be eliminated, but it reduces the precision a bit
delete out;
return 0;
}
}
if ( resampling ) {
// Downsample
case NR_PIXBLOCK_MODE_A8: ///< Grayscale
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);
break;
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
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:
assert(false);
};
}
if (use_IIR_x) {
// Filter variables
double bf[N]; // computed filter coefficients
double M[N*N]; // matrix used for initialization procedure (has to be double)
// Compute filter (x)
b[0] = 1; // b[0] == alpha (scaling coefficient)
for(size_t i=0; i<N; i++) {
b[i+1] = bf[i];
b[0] -= b[i+1];
}
// Compute initialization matrix (x)
calcTriggsSdikaM(bf, M);
// Filter (x)
case NR_PIXBLOCK_MODE_A8: ///< Grayscale
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);
break;
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);
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);
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);
break;
default:
assert(false);
};
} else { // !use_IIR_x
// Filter kernel for x direction
// Filter (x)
case NR_PIXBLOCK_MODE_A8: ///< Grayscale
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);
break;
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);
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);
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);
break;
default:
assert(false);
};
}
if (use_IIR_y) {
// Filter variables
double bf[N]; // computed filter coefficients
double M[N*N]; // matrix used for initialization procedure (has to be double)
// Compute filter (y)
b[0] = 1; // b[0] == alpha (scaling coefficient)
for(size_t i=0; i<N; i++) {
b[i+1] = bf[i];
b[0] -= b[i+1];
}
// Compute initialization matrix (y)
calcTriggsSdikaM(bf, M);
// Filter (y)
case NR_PIXBLOCK_MODE_A8: ///< Grayscale
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);
break;
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);
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);
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);
break;
default:
assert(false);
};
} else { // !use_IIR_y
// Filter kernel for y direction
// Filter (y)
case NR_PIXBLOCK_MODE_A8: ///< Grayscale
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);
break;
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);
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);
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);
break;
default:
assert(false);
};
}
delete[] tmpdata; // deleting a nullptr has no effect, so this is save
if ( !resampling ) {
// No upsampling needed
} else {
// New buffer for the final output, same resolution as the in buffer
// alas, we've accomplished a lot, but ran out of memory - so abort
delete out;
return 0;
}
// Upsample
case NR_PIXBLOCK_MODE_A8: ///< Grayscale
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);
break;
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
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:
assert(false);
};
// We don't need the out buffer anymore
delete out;
// The final out buffer gets returned
}
return 0;
}
{
// maximum is used because rotations can mix up these directions
// TODO: calculate a more tight-fitting rendering area
}
return TRAIT_PARALLER;
}
{
}
}
void FilterGaussian::set_deviation(double x, double y)
{
_deviation_x = x;
_deviation_y = y;
}
}
} /* namespace NR */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :