d2b1c2a0bdb3093503a569f8feec815ce00aee4f
1 /*
2 * feDisplacementMap 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-displacement-map.h"
13 #include "display/nr-filter-types.h"
14 #include "display/nr-filter-units.h"
15 #include "libnr/nr-blit.h"
16 #include "libnr/nr-pixops.h"
18 namespace Inkscape {
19 namespace Filters {
21 FilterDisplacementMap::FilterDisplacementMap()
22 {}
24 FilterPrimitive * FilterDisplacementMap::create() {
25 return new FilterDisplacementMap();
26 }
28 FilterDisplacementMap::~FilterDisplacementMap()
29 {}
31 struct pixel_t {
32 unsigned char channels[4];
33 inline unsigned char operator[](int c) const { return channels[c]; }
34 inline unsigned char& operator[](int c) { return channels[c]; }
35 static inline pixel_t blank() {
36 pixel_t p;
37 for(unsigned int i=0; i<4; i++) {
38 p[i] = 0;
39 }
40 return p;
41 }
42 };
44 static inline pixel_t pixelValue(NRPixBlock const* pb, int x, int y) {
45 if ( x < pb->area.x0 || x >= pb->area.x1 || y < pb->area.y0 || y >= pb->area.y1 ) return pixel_t::blank(); // This assumes anything outside the defined range is (0,0,0,0)
46 pixel_t const* data = reinterpret_cast<pixel_t const*>(NR_PIXBLOCK_PX(pb));
47 int offset = (x-pb->area.x0) + (pb->area.x1-pb->area.x0)*(y-pb->area.y0);
48 return data[offset];
49 }
51 template<bool PREMULTIPLIED>
52 static pixel_t interpolatePixels(NRPixBlock const* pb, double x, double y) {
53 unsigned int const sfl = 8u;
54 unsigned int const sf = 1u<<sfl;
55 unsigned int const sf2h = 1u<<(2u*sfl-1);
56 int xi = (int)floor(x), yi = (int)floor(y);
57 unsigned int xf = static_cast<unsigned int>(floor(sf*(x-xi)+.5)), yf = static_cast<unsigned int>(floor(sf*(y-yi)+.5));
58 pixel_t p00 = pixelValue(pb, xi+0, yi+0);
59 pixel_t p01 = pixelValue(pb, xi+1, yi+0);
60 pixel_t p10 = pixelValue(pb, xi+0, yi+1);
61 pixel_t p11 = pixelValue(pb, xi+1, yi+1);
63 /* It's a good idea to interpolate premultiplied colors:
64 *
65 * Consider two pixels, one being rgba(255,0,0,0), which is fully transparent,
66 * and the other being rgba(0,0,255,255), or blue (fully opaque).
67 * If these two colors are interpolated the expected result would be bluish pixels
68 * containing no red.
69 *
70 * However, if our final alpha value is zero, then the RGB values aren't really determinate.
71 * We might as well avoid premultiplication in this case, which still gives us a fully
72 * transparent result, but with interpolated RGB parts. */
74 /* First calculate interpolated alpha value. */
75 unsigned ra = 0;
76 if (!PREMULTIPLIED) {
77 unsigned const y0 = sf*p00[3] + xf*(p01[3]-p00[3]); // range [0,a*sf]
78 unsigned const y1 = sf*p10[3] + xf*(p11[3]-p10[3]);
79 ra = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
80 }
82 pixel_t r;
83 if (ra == 0) {
84 /* Either premultiplied or the interpolated alpha value is zero,
85 * so do simple interpolation. */
86 for (unsigned i = 0; i != 4; ++i) {
87 // y0,y1 have range [0,a*sf]
88 unsigned const y0 = sf*p00[i] + xf*((unsigned int)p01[i]-(unsigned int)p00[i]);
89 unsigned const y1 = sf*p10[i] + xf*((unsigned int)p11[i]-(unsigned int)p10[i]);
91 unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
92 r[i] = (ri + sf2h)>>(2*sfl); // range [0,a]
93 }
94 } else {
95 /* Do premultiplication ourselves. */
96 for (unsigned i = 0; i != 3; ++i) {
97 // Premultiplied versions. Range [0,255*a].
98 unsigned const c00 = p00[i]*p00[3];
99 unsigned const c01 = p01[i]*p01[3];
100 unsigned const c10 = p10[i]*p10[3];
101 unsigned const c11 = p11[i]*p11[3];
103 // Interpolation.
104 unsigned const y0 = sf*c00 + xf*(c01-c00); // range [0,255*a*sf]
105 unsigned const y1 = sf*c10 + xf*(c11-c10); // range [0,255*a*sf]
106 unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,255*a*sf*sf]
107 r[i] = (ri + ra/2) / ra; // range [0,255]
108 }
109 r[3] = (ra + sf2h)>>(2*sfl); // range [0,a]
110 }
112 return r;
113 }
115 template<bool MAP_PREMULTIPLIED, bool DATA_PREMULTIPLIED>
116 static void performDisplacement(NRPixBlock const* texture, NRPixBlock const* map, int Xchannel, int Ychannel, NRPixBlock* out, double scalex, double scaley) {
117 pixel_t *out_data = reinterpret_cast<pixel_t*>(NR_PIXBLOCK_PX(out));
119 bool Xneedsdemul = MAP_PREMULTIPLIED && Xchannel<3;
120 bool Yneedsdemul = MAP_PREMULTIPLIED && Ychannel<3;
121 if (!Xneedsdemul) scalex /= 255.0;
122 if (!Yneedsdemul) scaley /= 255.0;
124 for (int xout=out->area.x0; xout < out->area.x1; xout++){
125 for (int yout=out->area.y0; yout < out->area.y1; yout++){
126 int xmap = xout;
127 int ymap = yout;
129 pixel_t mapValue = pixelValue(map, xmap, ymap);
130 double xtex = xout + (Xneedsdemul ? // Although the value of the pixel corresponds to the MIDDLE of the pixel, no +0.5 is needed because we're interpolating pixels anyway (so to get the actual pixel locations 0.5 would have to be subtracted again).
131 (mapValue[3]==0?0:(scalex * (mapValue[Xchannel] - mapValue[3]*0.5) / mapValue[3])) :
132 (scalex * (mapValue[Xchannel] - 127.5)));
133 double ytex = yout + (Yneedsdemul ?
134 (mapValue[3]==0?0:(scaley * (mapValue[Ychannel] - mapValue[3]*0.5) / mapValue[3])) :
135 (scaley * (mapValue[Ychannel] - 127.5)));
137 out_data[(xout-out->area.x0) + (out->area.x1-out->area.x0)*(yout-out->area.y0)] = interpolatePixels<DATA_PREMULTIPLIED>(texture, xtex, ytex);
138 }
139 }
140 }
142 int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
143 NRPixBlock *texture = slot.get(_input);
144 NRPixBlock *map = slot.get(_input2);
146 // Bail out if either one of source images is missing
147 if (!map || !texture) {
148 g_warning("Missing source image for feDisplacementMap (map=%d texture=%d)", _input, _input2);
149 return 1;
150 }
152 //TODO: check whether do we really need this check:
153 if (map->area.x1 <= map->area.x0 || map->area.y1 <= map->area.y0) return 0; //nothing to do!
155 if (texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
156 g_warning("Source images without an alpha channel are not supported by feDisplacementMap at the moment.");
157 return 1;
158 }
160 NRPixBlock *out = new NRPixBlock;
162 out->area.x0 = map->area.x0;
163 out->area.y0 = map->area.y0;
164 out->area.x1 = map->area.x1;
165 out->area.y1 = map->area.y1;
167 nr_pixblock_setup_fast(out, texture->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
169 // convert to a suitable format
170 bool free_map_on_exit = false;
171 if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && map->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
172 NRPixBlock *original_map = map;
173 map = new NRPixBlock;
174 nr_pixblock_setup_fast(map, NR_PIXBLOCK_MODE_R8G8B8A8N,
175 original_map->area.x0, original_map->area.y0,
176 original_map->area.x1, original_map->area.y1,
177 false);
178 nr_blit_pixblock_pixblock(map, original_map);
179 free_map_on_exit = true;
180 }
181 bool map_premultiplied = (map->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
182 bool data_premultiplied = (out->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
184 Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
185 double scalex = scale * trans.expansionX();
186 double scaley = scale * trans.expansionY();
188 if (map_premultiplied && data_premultiplied) {
189 performDisplacement<true,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
190 } else if (map_premultiplied && !data_premultiplied) {
191 performDisplacement<true,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
192 } else if (data_premultiplied) {
193 performDisplacement<false,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
194 } else {
195 performDisplacement<false,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
196 }
198 if (free_map_on_exit) {
199 nr_pixblock_release(map);
200 delete map;
201 }
203 out->empty = FALSE;
204 slot.set(_output, out);
205 return 0;
206 }
208 void FilterDisplacementMap::set_input(int slot) {
209 _input = slot;
210 }
212 void FilterDisplacementMap::set_scale(double s) {
213 scale = s;
214 }
216 void FilterDisplacementMap::set_input(int input, int slot) {
217 if (input == 0) _input = slot;
218 if (input == 1) _input2 = slot;
219 }
221 void FilterDisplacementMap::set_channel_selector(int s, FilterDisplacementMapChannelSelector channel) {
222 if (channel > DISPLACEMENTMAP_CHANNEL_ALPHA || channel < DISPLACEMENTMAP_CHANNEL_RED) {
223 g_warning("Selected an invalid channel value. (%d)", channel);
224 return;
225 }
227 if (s == 0) Xchannel = channel;
228 if (s == 1) Ychannel = channel;
229 }
231 void FilterDisplacementMap::area_enlarge(NRRectL &area, Geom::Matrix const &trans)
232 {
233 //I assume scale is in user coordinates (?!?)
234 //FIXME: trans should be multiplied by some primitiveunits2user, shouldn't it?
236 double scalex = scale/2.*(std::fabs(trans[0])+std::fabs(trans[1]));
237 double scaley = scale/2.*(std::fabs(trans[2])+std::fabs(trans[3]));
239 //FIXME: no +2 should be there!... (noticable only for big scales at big zoom factor)
240 area.x0 -= (int)(scalex)+2;
241 area.x1 += (int)(scalex)+2;
242 area.y0 -= (int)(scaley)+2;
243 area.y1 += (int)(scaley)+2;
244 }
246 FilterTraits FilterDisplacementMap::get_input_traits() {
247 return TRAIT_PARALLER;
248 }
250 } /* namespace Filters */
251 } /* namespace Inkscape */
253 /*
254 Local Variables:
255 mode:c++
256 c-file-style:"stroustrup"
257 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
258 indent-tabs-mode:nil
259 fill-column:99
260 End:
261 */
262 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :