1 /*
2 * feConvolveMatrix filter primitive renderer
3 *
4 * Authors:
5 * Felipe CorrĂȘa da Silva Sanches <felipe.sanches@gmail.com>
6 *
7 * Copyright (C) 2007 authors
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #include "display/nr-filter-convolve-matrix.h"
13 #include "display/nr-filter-units.h"
14 #include "display/nr-filter-utils.h"
15 #include <vector>
17 namespace NR {
19 FilterConvolveMatrix::FilterConvolveMatrix()
20 {}
22 FilterPrimitive * FilterConvolveMatrix::create() {
23 return new FilterConvolveMatrix();
24 }
26 FilterConvolveMatrix::~FilterConvolveMatrix()
27 {}
29 static bool inside_area(int px, int py, int w, int h){
30 if (px<0) return false;
31 if (py<0) return false;
32 if (px>w) return false;
33 if (py>h) return false;
34 return true;
35 }
37 int FilterConvolveMatrix::render(FilterSlot &slot, FilterUnits const &/*units*/) {
38 NRPixBlock *in = slot.get(_input);
39 if (!in) {
40 g_warning("Missing source image for feConvolveMatrix (in=%d)", _input);
41 return 1;
42 }
44 NRPixBlock *out = new NRPixBlock;
46 nr_pixblock_setup_fast(out, in->mode,
47 in->area.x0, in->area.y0, in->area.x1, in->area.y1,
48 true);
50 unsigned char *in_data = NR_PIXBLOCK_PX(in);
51 unsigned char *out_data = NR_PIXBLOCK_PX(out);
53 double result_R, result_G, result_B, result_A;
54 int i, j, x, y;
55 int width = in->area.x1 - in->area.x0;
56 int height = in->area.y1 - in->area.y0;
58 unsigned int index;
59 unsigned int kernel_index;
61 for (x=targetX; x < width - (orderX - targetX); x++){
62 for (y=targetY; y < height - (orderY - targetY); y++){
63 result_R = 0;
64 result_G = 0;
65 result_B = 0;
66 result_A = 0;
67 for (i=0; i < orderY; i++){
68 for (j=0; j < orderX; j++){
69 if (inside_area(x - targetX + j, y - targetY + i, width, height)){
70 index = 4*( x - targetX + j + width*(y - targetY + i) );
71 kernel_index = orderX-j-1 + orderX*(orderY-i-1);
72 result_R += ( (double) in_data[index++] * kernelMatrix[kernel_index] );
73 result_G += ( (double) in_data[index++] * kernelMatrix[kernel_index] );
74 result_B += ( (double) in_data[index++] * kernelMatrix[kernel_index] );
75 result_A += ( (double) in_data[index] * kernelMatrix[kernel_index] );
76 }
77 }
78 }
79 unsigned int out_index = 4*( x + width*y );
80 out_data[out_index++] = CLAMP_D_TO_U8(result_R / divisor + bias);
81 out_data[out_index++] = CLAMP_D_TO_U8(result_G / divisor + bias);
82 out_data[out_index++] = CLAMP_D_TO_U8(result_B / divisor + bias);
84 if( preserveAlpha ) {
85 out_data[out_index] = in_data[out_index];
86 } else {
87 out_data[out_index] = CLAMP_D_TO_U8(result_A / divisor + bias);
88 }
89 }
90 }
92 out->empty = FALSE;
93 slot.set(_output, out);
94 return 0;
95 }
97 void FilterConvolveMatrix::set_targetX(int coord) {
98 targetX = coord;
99 }
101 void FilterConvolveMatrix::set_targetY(int coord) {
102 targetY = coord;
103 }
105 void FilterConvolveMatrix::set_orderX(int coord) {
106 orderX = coord;
107 }
109 void FilterConvolveMatrix::set_orderY(int coord) {
110 orderY = coord;
111 }
113 void FilterConvolveMatrix::set_divisor(double d) {
114 divisor = d;
115 }
117 void FilterConvolveMatrix::set_bias(double b) {
118 bias = b;
119 }
121 void FilterConvolveMatrix::set_kernelMatrix(std::vector<gdouble> &km) {
122 kernelMatrix = km;
123 }
125 void FilterConvolveMatrix::set_edgeMode(FilterConvolveMatrixEdgeMode mode){
126 edgeMode = mode;
127 }
129 void FilterConvolveMatrix::set_preserveAlpha(bool pa){
130 preserveAlpha = pa;
131 }
133 void FilterConvolveMatrix::area_enlarge(NRRectL &area, Matrix const &/*trans*/)
134 {
135 //Seems to me that since this filter's operation is resolution dependent,
136 // some spurious pixels may still appear at the borders when low zooming or rotating. Needs a better fix.
137 area.x0 -= targetX;
138 area.y0 -= targetY;
139 area.x1 += orderX - targetX;
140 area.y1 += orderY - targetY;
141 }
143 FilterTraits FilterConvolveMatrix::get_input_traits() {
144 return TRAIT_PARALLER;
145 }
147 } /* namespace NR */
149 /*
150 Local Variables:
151 mode:c++
152 c-file-style:"stroustrup"
153 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
154 indent-tabs-mode:nil
155 fill-column:99
156 End:
157 */
158 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :