Code

filterUnits and primitiveUnits are now actually read/set, percentages for primitiveUn...
[inkscape.git] / src / display / nr-filter-displacement-map.cpp
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* 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     // NOTE: The values of x and y are shifted by -0.5 (the "true" values would be x+0.5 and y+0.5).
54     //       This is done because otherwise the pixel values first have to be shifted by +0.5 and then by -0.5 again...
55     unsigned int const sfl = 8u;
56     unsigned int const sf = 1u<<sfl;
57     unsigned int const sf2h = 1u<<(2u*sfl-1);
58     int xi = (int)floor(x), yi = (int)floor(y);
59     unsigned int xf = static_cast<unsigned int>(round(sf * (x - xi))),
60         yf = static_cast<unsigned int>(round(sf * (y - yi)));
61     pixel_t p00 = pixelValue(pb, xi+0, yi+0);
62     pixel_t p01 = pixelValue(pb, xi+1, yi+0);
63     pixel_t p10 = pixelValue(pb, xi+0, yi+1);
64     pixel_t p11 = pixelValue(pb, xi+1, yi+1);
66     /* It's a good idea to interpolate premultiplied colors:
67      *
68      *   Consider two pixels, one being rgba(255,0,0,0), which is fully transparent,
69      *   and the other being rgba(0,0,255,255), or blue (fully opaque).
70      *   If these two colors are interpolated the expected result would be bluish pixels
71      *   containing no red.
72      *
73      * However, if our final alpha value is zero, then the RGB values aren't really determinate.
74      * We might as well avoid premultiplication in this case, which still gives us a fully
75      * transparent result, but with interpolated RGB parts. */
77     /* First calculate interpolated alpha value. */
78     unsigned ra = 0;
79     if (!PREMULTIPLIED) {
80         unsigned const y0 = sf*p00[3] + xf*(p01[3]-p00[3]); // range [0,a*sf]
81         unsigned const y1 = sf*p10[3] + xf*(p11[3]-p10[3]);
82         ra = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
83     }
85     pixel_t r;
86     if (ra == 0) {
87         /* Either premultiplied or the interpolated alpha value is zero,
88          * so do simple interpolation. */
89         for (unsigned i = 0; i != 4; ++i) {
90             // y0,y1 have range [0,a*sf]
91             unsigned const y0 = sf*p00[i] + xf*((unsigned int)p01[i]-(unsigned int)p00[i]);
92             unsigned const y1 = sf*p10[i] + xf*((unsigned int)p11[i]-(unsigned int)p10[i]);
94             unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
95             r[i] = (ri + sf2h)>>(2*sfl); // range [0,a]
96         }
97     } else {
98         /* Do premultiplication ourselves. */
99         for (unsigned i = 0; i != 3; ++i) {
100             // Premultiplied versions.  Range [0,255*a].
101             unsigned const c00 = p00[i]*p00[3];
102             unsigned const c01 = p01[i]*p01[3];
103             unsigned const c10 = p10[i]*p10[3];
104             unsigned const c11 = p11[i]*p11[3];
106             // Interpolation.
107             unsigned const y0 = sf*c00 + xf*(c01-c00); // range [0,255*a*sf]
108             unsigned const y1 = sf*c10 + xf*(c11-c10); // range [0,255*a*sf]
109             unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,255*a*sf*sf]
110             r[i] = (ri + ra/2) / ra;  // range [0,255]
111         }
112         r[3] = (ra + sf2h)>>(2*sfl); // range [0,a]
113     }
115     return r;
118 template<bool MAP_PREMULTIPLIED, bool DATA_PREMULTIPLIED>
119 static void performDisplacement(NRPixBlock const* texture, NRPixBlock const* map, int Xchannel, int Ychannel, NRPixBlock* out, double scalex, double scaley) {
120     pixel_t *out_data = reinterpret_cast<pixel_t*>(NR_PIXBLOCK_PX(out));
122     bool Xneedsdemul = MAP_PREMULTIPLIED && Xchannel<3;
123     bool Yneedsdemul = MAP_PREMULTIPLIED && Ychannel<3;
124     if (!Xneedsdemul) scalex /= 255.0;
125     if (!Yneedsdemul) scaley /= 255.0;
127     for (int yout=out->area.y0; yout < out->area.y1; yout++){
128         for (int xout=out->area.x0; xout < out->area.x1; xout++){
129             int xmap = xout;
130             int ymap = yout;
132             pixel_t mapValue = pixelValue(map, xmap, ymap);
133             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).
134                 (mapValue[3]==0?0:(scalex * (mapValue[Xchannel] - mapValue[3]*0.5) / mapValue[3])) :
135                 (scalex * (mapValue[Xchannel] - 127.5)));
136             double ytex = yout + (Yneedsdemul ?
137                 (mapValue[3]==0?0:(scaley * (mapValue[Ychannel] - mapValue[3]*0.5) / mapValue[3])) :
138                 (scaley * (mapValue[Ychannel] - 127.5)));
140             out_data[(xout-out->area.x0) + (out->area.x1-out->area.x0)*(yout-out->area.y0)] = interpolatePixels<DATA_PREMULTIPLIED>(texture, xtex, ytex);
141         }
142     }
145 int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
146     NRPixBlock *texture = slot.get(_input);
147     NRPixBlock *map = slot.get(_input2);
149     // Bail out if either one of source images is missing
150     if (!map || !texture) {
151         g_warning("Missing source image for feDisplacementMap (map=%d texture=%d)", _input, _input2);
152         return 1;
153     }
155     //TODO: check whether do we really need this check:
156     if (map->area.x1 <= map->area.x0 || map->area.y1 <=  map->area.y0) return 0; //nothing to do!
158     if (texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
159         g_warning("Source images without an alpha channel are not supported by feDisplacementMap at the moment.");
160         return 1;
161     }
163     NRPixBlock *out = new NRPixBlock;
164     
165     out->area.x0 = map->area.x0;
166     out->area.y0 = map->area.y0;
167     out->area.x1 = map->area.x1;
168     out->area.y1 = map->area.y1;
170     nr_pixblock_setup_fast(out, texture->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
172     // convert to a suitable format
173     bool free_map_on_exit = false;
174     if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && map->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
175         NRPixBlock *original_map = map;
176         map = new NRPixBlock;
177         nr_pixblock_setup_fast(map, NR_PIXBLOCK_MODE_R8G8B8A8N,
178                                original_map->area.x0, original_map->area.y0,
179                                original_map->area.x1, original_map->area.y1,
180                                false);
181         nr_blit_pixblock_pixblock(map, original_map);
182         free_map_on_exit = true;
183     }
184     bool map_premultiplied = (map->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
185     bool data_premultiplied = (out->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
187     Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
188     double scalex = scale * trans.expansionX();
189     double scaley = scale * trans.expansionY();
191     if (map_premultiplied && data_premultiplied) {
192         performDisplacement<true,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
193     } else if (map_premultiplied && !data_premultiplied) {
194         performDisplacement<true,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
195     } else if (data_premultiplied) {
196         performDisplacement<false,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
197     } else {
198         performDisplacement<false,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
199     }
201     if (free_map_on_exit) {
202         nr_pixblock_release(map);
203         delete map;
204     }
206     out->empty = FALSE;
207     slot.set(_output, out);
208             return 0;
211 void FilterDisplacementMap::set_input(int slot) {
212     _input = slot;
215 void FilterDisplacementMap::set_scale(double s) {
216     scale = s;
219 void FilterDisplacementMap::set_input(int input, int slot) {
220     if (input == 0) _input = slot;
221     if (input == 1) _input2 = slot;
224 void FilterDisplacementMap::set_channel_selector(int s, FilterDisplacementMapChannelSelector channel) {
225     if (channel > DISPLACEMENTMAP_CHANNEL_ALPHA || channel < DISPLACEMENTMAP_CHANNEL_RED) {
226         g_warning("Selected an invalid channel value. (%d)", channel);
227         return;
228     }
230     if (s == 0) Xchannel = channel;
231     if (s == 1) Ychannel = channel;
234 void FilterDisplacementMap::area_enlarge(NRRectL &area, Geom::Matrix const &trans)
236     //I assume scale is in user coordinates (?!?)
237     //FIXME: trans should be multiplied by some primitiveunits2user, shouldn't it?
238     
239     double scalex = scale/2.*(std::fabs(trans[0])+std::fabs(trans[1]));
240     double scaley = scale/2.*(std::fabs(trans[2])+std::fabs(trans[3]));
242     //FIXME: no +2 should be there!... (noticable only for big scales at big zoom factor)
243     area.x0 -= (int)(scalex)+2;
244     area.x1 += (int)(scalex)+2;
245     area.y0 -= (int)(scaley)+2;
246     area.y1 += (int)(scaley)+2;
249 FilterTraits FilterDisplacementMap::get_input_traits() {
250     return TRAIT_PARALLER;
253 } /* namespace Filters */
254 } /* namespace Inkscape */
256 /*
257   Local Variables:
258   mode:c++
259   c-file-style:"stroustrup"
260   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
261   indent-tabs-mode:nil
262   fill-column:99
263   End:
264 */
265 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :