nr-filter-convolve-matrix.cpp revision 2c9bfdad54133659294133396867ccd6318342a5
/*
* feConvolveMatrix filter primitive renderer
*
* Authors:
* Felipe CorrĂȘa da Silva Sanches <juca@members.fsf.org>
* Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
*
* Copyright (C) 2007,2009 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <vector>
#include "display/cairo-templates.h"
#include "display/cairo-utils.h"
#include "display/nr-filter-convolve-matrix.h"
#include "display/nr-filter-slot.h"
#include "display/nr-filter-units.h"
#include "display/nr-filter-utils.h"
namespace Inkscape {
namespace Filters {
{}
return new FilterConvolveMatrix();
}
{}
static inline void convolve2D_XY(unsigned int const x, unsigned int const y, guint32 *const out_data, guint32 const *const in_data, unsigned int const width, unsigned int const height, double const *const kernel, unsigned int const orderX, unsigned int const orderY, unsigned int const targetX, unsigned int const targetY, double const bias) {
double result_R = 0;
double result_G = 0;
double result_B = 0;
double result_A = 0;
unsigned int iBegin = Y_LOWER ? targetY-y : 0; // Note that to prevent signed/unsigned problems this requires that y<=targetY (which is true)
unsigned int iEnd = Y_UPPER ? height+targetY-y : orderY; // And this requires that y<=height+targetY (which is trivially true), in addition it should be true that height+targetY-y<=orderY (or equivalently y>=height+targetY-orderY, which is true)
EXTRACT_ARGB32(px, a,r,g,b)
double k = kernel[kernel_index];
result_R += r * k;
result_G += g * k;
result_B += b * k;
result_A += a * k;
}
}
if (PRESERVE_ALPHA) {
} else {
}
}
static inline void convolve2D_Y(unsigned int const y, guint32 *const out_data, guint32 const *const in_data, unsigned int const width, unsigned int const height, double const *const kernel, unsigned int const orderX, unsigned int const orderY, unsigned int const targetX, unsigned int const targetY, double const bias) {
// See convolve2D below for rationale.
for (unsigned int x=0; x<midXBegin; x++) {
convolve2D_XY<PRESERVE_ALPHA,true,false,Y_LOWER,Y_UPPER>(x, y, out_data, in_data, width, height, kernel, orderX, orderY, targetX, targetY, bias);
}
if (lowerEnd==upperBegin) {
// Do nothing, empty mid section
} else if (lowerEnd<upperBegin) {
// In the middle no bounds have to be adjusted
convolve2D_XY<PRESERVE_ALPHA,false,false,Y_LOWER,Y_UPPER>(x, y, out_data, in_data, width, height, kernel, orderX, orderY, targetX, targetY, bias);
}
} else {
// In the middle both bounds have to be adjusted
convolve2D_XY<PRESERVE_ALPHA,true,true,Y_LOWER,Y_UPPER>(x, y, out_data, in_data, width, height, kernel, orderX, orderY, targetX, targetY, bias);
}
}
convolve2D_XY<PRESERVE_ALPHA,false,true,Y_LOWER,Y_UPPER>(x, y, out_data, in_data, width, height, kernel, orderX, orderY, targetX, targetY, bias);
}
}
template<bool PRESERVE_ALPHA>
static void convolve2D(guint32 *const out_data, guint32 const *const in_data, unsigned int const width, unsigned int const height, double const *const kernel, unsigned int const orderX, unsigned int const orderY, unsigned int const targetX, unsigned int const targetY, double const _bias) {
// For the middle section it should hold that (for all i such that 0<=i<orderY):
// 0 <= y - targetY + i < height
// targetY <= y && y < height + targetY - orderY + 1
// In other words, for y<targetY i's lower bound needs to be adjusted and for y>=height+targetY-orderY+1 i's upper bound needs to be adjusted.
for (unsigned int y=0; y<midYBegin; y++) {
convolve2D_Y<PRESERVE_ALPHA,true,false>(y, out_data, in_data, width, height, kernel, orderX, orderY, targetX, targetY, bias);
}
if (lowerEnd==upperBegin) {
// Do nothing, empty mid section
} else if (lowerEnd<upperBegin) {
// In the middle no bounds have to be adjusted
convolve2D_Y<PRESERVE_ALPHA,false,false>(y, out_data, in_data, width, height, kernel, orderX, orderY, targetX, targetY, bias);
}
} else {
// In the middle both bounds have to be adjusted
convolve2D_Y<PRESERVE_ALPHA,true,true>(y, out_data, in_data, width, height, kernel, orderX, orderY, targetX, targetY, bias);
}
}
convolve2D_Y<PRESERVE_ALPHA,false,true>(y, out_data, in_data, width, height, kernel, orderX, orderY, targetX, targetY, bias);
}
}
/*
struct ConvolveMatrix {
ConvolveMatrix(guint32 *px, int yskip, int targetX, int targetY, int orderX, int orderY,
double divisor, double bias, PixelAccessor::EdgeMode emode,
std::vector<double> const &kernel)
: _kernel(kernel.size())
// , _in(in, emode)
, _tx(targetX), _ty(targetY)
, _oX(orderX), _oY(orderY)
, _yskip(yskip)
, _bias(bias)
{
for (unsigned i = 0; i < kernel.size(); ++i) {
_kernel[i] = kernel[i] / divisor;
}
}
guint32 operator()(int x, int y) {
int start_x = x - _tX;
int start_y = y - _tY;
double ro = 0, go = 0, bo = 0, ao = 0;
for (int i = 0; i < _oY; ++i) {
for (int j = 0; j < _oX; ++j) {
guint32 in = pixelAt(start_x + j, start_y + i);
EXTRACT_ARGB(in, a,r,g,b)
unsigned kidx = i*_oY + j;
double k = kernel[]
ro += r *
}
}
}
private:
inline guint32 pixelAt(int x, int y) {
return *(_px + y * _yskip + x);
}
std::vector<double> _kernel;
guint32 *_px;
// PixelAccessor _in;
double _bias;
int _tX, _tY, _oX, _oY, _yskip;
}; */
{
g_warning("Empty kernel!");
return;
}
g_warning("Invalid target!");
return;
}
g_warning("kernelMatrix does not have orderX*orderY elements!");
return;
}
if (bias!=0) {
g_warning("It is unknown whether Inkscape's implementation of bias in feConvolveMatrix "
"is correct!");
// The SVG specification implies that feConvolveMatrix is defined for premultiplied
// colors (which makes sense). It also says that bias should simply be added to the result
// for each color (without taking the alpha into account). However, it also says that one
// purpose of bias is "to have .5 gray value be the zero response of the filter".
// It seems sensible to indeed support the latter behaviour instead of the former,
// but this does appear to go against the standard.
// Note that Batik simply does not support bias!=0
}
if (edgeMode!=CONVOLVEMATRIX_EDGEMODE_NONE) {
g_warning("Inkscape only supports edgeMode=\"none\" (and a filter uses a different one)!");
}
// Set up predivided kernel matrix
}
if (preserveAlpha) {
} else {
}
}
}
}
}
}
void FilterConvolveMatrix::set_divisor(double d) {
divisor = d;
}
void FilterConvolveMatrix::set_bias(double b) {
bias = b;
}
kernelMatrix = km;
}
}
preserveAlpha = pa;
}
{
//Seems to me that since this filter's operation is resolution dependent,
// some spurious pixels may still appear at the borders when low zooming or rotating. Needs a better fix.
area.x1 += orderX - targetX - 1; // This makes sure the last row/column in the original image corresponds to the last row/column in the new image that can be convolved without adjusting the boundary conditions).
}
} /* namespace Filters */
} /* namespace Inkscape */
/*
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 :