1 /*
2 * feDisplacementMap filter primitive renderer
3 *
4 * Authors:
5 * Felipe CorrĂȘa da Silva Sanches <juca@members.fsf.org>
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* rowData = reinterpret_cast<pixel_t const*>(NR_PIXBLOCK_PX(pb) + (y-pb->area.y0)*pb->rs);
47 return rowData[x-pb->area.x0];
48 }
50 template<bool PREMULTIPLIED>
51 static pixel_t interpolatePixels(NRPixBlock const* pb, double x, double y) {
52 // NOTE: The values of x and y are shifted by -0.5 (the "true" values would be x+0.5 and y+0.5).
53 // This is done because otherwise the pixel values first have to be shifted by +0.5 and then by -0.5 again...
54 unsigned int const sfl = 8u;
55 unsigned int const sf = 1u<<sfl;
56 unsigned int const sf2h = 1u<<(2u*sfl-1);
57 int xi = (int)floor(x), yi = (int)floor(y);
58 unsigned int xf = static_cast<unsigned int>(round(sf * (x - xi))),
59 yf = static_cast<unsigned int>(round(sf * (y - yi)));
60 pixel_t p00 = pixelValue(pb, xi+0, yi+0);
61 pixel_t p01 = pixelValue(pb, xi+1, yi+0);
62 pixel_t p10 = pixelValue(pb, xi+0, yi+1);
63 pixel_t p11 = pixelValue(pb, xi+1, yi+1);
65 /* It's a good idea to interpolate premultiplied colors:
66 *
67 * Consider two pixels, one being rgba(255,0,0,0), which is fully transparent,
68 * and the other being rgba(0,0,255,255), or blue (fully opaque).
69 * If these two colors are interpolated the expected result would be bluish pixels
70 * containing no red.
71 *
72 * However, if our final alpha value is zero, then the RGB values aren't really determinate.
73 * We might as well avoid premultiplication in this case, which still gives us a fully
74 * transparent result, but with interpolated RGB parts. */
76 pixel_t r;
77 if (PREMULTIPLIED) {
78 /* Premultiplied, so do simple interpolation. */
79 for (unsigned i = 0; i != 4; ++i) {
80 // y0,y1 have range [0,a*sf]
81 unsigned const y0 = sf*p00[i] + xf*((unsigned int)p01[i]-(unsigned int)p00[i]);
82 unsigned const y1 = sf*p10[i] + xf*((unsigned int)p11[i]-(unsigned int)p10[i]);
84 unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
85 r[i] = (ri + sf2h)>>(2*sfl); // range [0,a]
86 }
87 } else {
88 /* First calculate interpolated alpha value. */
89 unsigned const y0 = sf*p00[3] + xf*((unsigned int)p01[3]-(unsigned int)p00[3]); // range [0,a*sf]
90 unsigned const y1 = sf*p10[3] + xf*((unsigned int)p11[3]-(unsigned int)p10[3]);
91 unsigned const ra = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
93 if (ra==0) {
94 /* Fully transparent, so do simple interpolation. */
95 for (unsigned i = 0; i != 3; ++i) {
96 // y0,y1 have range [0,255*sf]
97 unsigned const y0 = sf*p00[i] + xf*((unsigned int)p01[i]-(unsigned int)p00[i]);
98 unsigned const y1 = sf*p10[i] + xf*((unsigned int)p11[i]-(unsigned int)p10[i]);
100 unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,255*sf*sf]
101 r[i] = (ri + sf2h)>>(2*sfl); // range [0,255]
102 }
103 r[3] = 0;
104 } else {
105 /* Do premultiplication ourselves. */
106 for (unsigned i = 0; i != 3; ++i) {
107 // Premultiplied versions. Range [0,255*a].
108 unsigned const c00 = p00[i]*p00[3];
109 unsigned const c01 = p01[i]*p01[3];
110 unsigned const c10 = p10[i]*p10[3];
111 unsigned const c11 = p11[i]*p11[3];
113 // Interpolation.
114 unsigned const y0 = sf*c00 + xf*(c01-c00); // range [0,255*a*sf]
115 unsigned const y1 = sf*c10 + xf*(c11-c10); // range [0,255*a*sf]
116 unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,255*a*sf*sf]
117 r[i] = (ri + ra/2) / ra; // range [0,255]
118 }
119 r[3] = (ra + sf2h)>>(2*sfl); // range [0,a]
120 }
121 }
123 return r;
124 }
126 template<bool MAP_PREMULTIPLIED, bool DATA_PREMULTIPLIED>
127 static void performDisplacement(NRPixBlock const* texture, NRPixBlock const* map, int Xchannel, int Ychannel, NRPixBlock* out, double scalex, double scaley) {
128 bool Xneedsdemul = MAP_PREMULTIPLIED && Xchannel<3;
129 bool Yneedsdemul = MAP_PREMULTIPLIED && Ychannel<3;
130 if (!Xneedsdemul) scalex /= 255.0;
131 if (!Yneedsdemul) scaley /= 255.0;
133 for (int yout=out->area.y0; yout < out->area.y1; yout++){
134 pixel_t const* mapRowData = reinterpret_cast<pixel_t const*>(NR_PIXBLOCK_PX(map) + (yout-map->area.y0)*map->rs);
135 pixel_t* outRowData = reinterpret_cast<pixel_t*>(NR_PIXBLOCK_PX(out) + (yout-out->area.y0)*out->rs);
136 for (int xout=out->area.x0; xout < out->area.x1; xout++){
137 pixel_t const mapValue = mapRowData[xout-map->area.x0];
139 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).
140 (mapValue[3]==0?0:(scalex * (mapValue[Xchannel] - mapValue[3]*0.5) / mapValue[3])) :
141 (scalex * (mapValue[Xchannel] - 127.5)));
142 double ytex = yout + (Yneedsdemul ?
143 (mapValue[3]==0?0:(scaley * (mapValue[Ychannel] - mapValue[3]*0.5) / mapValue[3])) :
144 (scaley * (mapValue[Ychannel] - 127.5)));
146 outRowData[xout-out->area.x0] = interpolatePixels<DATA_PREMULTIPLIED>(texture, xtex, ytex);
147 }
148 }
149 }
151 int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
152 NRPixBlock *texture = slot.get(_input);
153 NRPixBlock *map = slot.get(_input2);
155 // Bail out if either one of source images is missing
156 if (!map || !texture) {
157 g_warning("Missing source image for feDisplacementMap (map=%d texture=%d)", _input, _input2);
158 return 1;
159 }
161 NR::IRect area = units.get_pixblock_filterarea_paraller();
162 int x0 = std::max(map->area.x0,area.min()[NR::X]);
163 int y0 = std::max(map->area.y0,area.min()[NR::Y]);
164 int x1 = std::min(map->area.x1,area.max()[NR::X]);
165 int y1 = std::min(map->area.y1,area.max()[NR::Y]);
167 //TODO: check whether we really need this check:
168 if (x1 <= x0 || y1 <= y0) return 0; //nothing to do!
170 if (texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
171 g_warning("Source images without an alpha channel are not supported by feDisplacementMap at the moment.");
172 return 1;
173 }
175 NRPixBlock *out = new NRPixBlock;
176 nr_pixblock_setup_fast(out, texture->mode, x0, y0, x1, y1, true);
178 // convert to a suitable format
179 bool free_map_on_exit = false;
180 if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && map->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
181 NRPixBlock *original_map = map;
182 map = new NRPixBlock;
183 nr_pixblock_setup_fast(map, NR_PIXBLOCK_MODE_R8G8B8A8N,
184 original_map->area.x0, original_map->area.y0,
185 original_map->area.x1, original_map->area.y1,
186 false);
187 nr_blit_pixblock_pixblock(map, original_map);
188 free_map_on_exit = true;
189 }
190 bool map_premultiplied = (map->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
191 bool data_premultiplied = (out->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
193 Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
194 double scalex = scale * trans.expansionX();
195 double scaley = scale * trans.expansionY();
197 if (map_premultiplied && data_premultiplied) {
198 performDisplacement<true,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
199 } else if (map_premultiplied && !data_premultiplied) {
200 performDisplacement<true,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
201 } else if (data_premultiplied) {
202 performDisplacement<false,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
203 } else {
204 performDisplacement<false,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
205 }
207 if (free_map_on_exit) {
208 nr_pixblock_release(map);
209 delete map;
210 }
212 out->empty = FALSE;
213 slot.set(_output, out);
214 return 0;
215 }
217 void FilterDisplacementMap::set_input(int slot) {
218 _input = slot;
219 }
221 void FilterDisplacementMap::set_scale(double s) {
222 scale = s;
223 }
225 void FilterDisplacementMap::set_input(int input, int slot) {
226 if (input == 0) _input = slot;
227 if (input == 1) _input2 = slot;
228 }
230 void FilterDisplacementMap::set_channel_selector(int s, FilterDisplacementMapChannelSelector channel) {
231 if (channel > DISPLACEMENTMAP_CHANNEL_ALPHA || channel < DISPLACEMENTMAP_CHANNEL_RED) {
232 g_warning("Selected an invalid channel value. (%d)", channel);
233 return;
234 }
236 if (s == 0) Xchannel = channel;
237 if (s == 1) Ychannel = channel;
238 }
240 void FilterDisplacementMap::area_enlarge(NRRectL &area, Geom::Matrix const &trans)
241 {
242 //I assume scale is in user coordinates (?!?)
243 //FIXME: trans should be multiplied by some primitiveunits2user, shouldn't it?
245 double scalex = scale/2.*(std::fabs(trans[0])+std::fabs(trans[1]));
246 double scaley = scale/2.*(std::fabs(trans[2])+std::fabs(trans[3]));
248 //FIXME: no +2 should be there!... (noticable only for big scales at big zoom factor)
249 area.x0 -= (int)(scalex)+2;
250 area.x1 += (int)(scalex)+2;
251 area.y0 -= (int)(scaley)+2;
252 area.y1 += (int)(scaley)+2;
253 }
255 FilterTraits FilterDisplacementMap::get_input_traits() {
256 return TRAIT_PARALLER;
257 }
259 } /* namespace Filters */
260 } /* namespace Inkscape */
262 /*
263 Local Variables:
264 mode:c++
265 c-file-style:"stroustrup"
266 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
267 indent-tabs-mode:nil
268 fill-column:99
269 End:
270 */
271 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :