Code

29815abbb22a102123a8b80b174b80b4865d46e5
[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 <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     pixel_t r;
64     if (PREMULTIPLIED) {
65         unsigned int p0[4]; // range [0,a*sf]
66         unsigned int p1[4];
67         for(size_t i=0; i<4; i++) {
68             p0[i] = sf*p00[i] + xf*((unsigned int)p01[i]-(unsigned int)p00[i]);
69             p1[i] = sf*p10[i] + xf*((unsigned int)p11[i]-(unsigned int)p10[i]);
70         }
71         for(size_t i=0; i<4; i++) {
72             unsigned int ru = sf*p0[i] + yf*(p1[i]-p0[i]); // range [0,a*sf^2]
73             r[i] = (ru + sf2h)>>(2u*sfl); // range [0,a]
74         }
75     } else {
76         // It's a good idea to interpolate premultiplied colors
77         //   Consider two pixels, one being rgb(255,0,0,0), which is fully transparent,
78         //   and the other being rgb(0,255,0,255), or green (fully opaque).
79         //   If these two colors are interpolated the expected result would be greenish pixels containing no red.
80         unsigned int p0[4];
81         unsigned int p1[4];
82         for(size_t i=0; i<3; i++) {
83             unsigned int c00 = p00[i]*p00[3], c01 = p01[i]*p01[3], c10 = p10[i]*p10[3], c11 = p11[i]*p11[3]; // range [0,255*a]
84             p0[i] = sf*c00 + xf*(c01-c00); // range [0,255*a*sf]
85             p1[i] = sf*c10 + xf*(c11-c10); // range [0,255*a*sf]
86         }
87         p0[3] = sf*p00[3] + xf*(p01[3]-p00[3]); // range [0,a*sf]
88         p1[3] = sf*p10[3] + xf*(p11[3]-p10[3]);
89         unsigned int ru = sf*p0[3] + yf*(p1[3]-p0[3]); // range [0,a*sf^2]
90         r[3] = (ru + sf2h)>>(2u*sfl); // range [0,a]
91         for(size_t i=0; i<3; i++) {
92             unsigned int ru = sf*p0[i] + yf*(p1[i]-p0[i]); // range [0,255*a*sf^2]
93             r[i] = (ru + ru/2u)/ru; // range [0,255]
94         }
95     }
97     return r;
98 }
100 template<bool MAP_PREMULTIPLIED, bool DATA_PREMULTIPLIED>
101 static void performDisplacement(NRPixBlock const* texture, NRPixBlock const* map, int Xchannel, int Ychannel, NRPixBlock* out, double scalex, double scaley) {
102     pixel_t *out_data = reinterpret_cast<pixel_t*>(NR_PIXBLOCK_PX(out));
104     bool Xneedsdemul = MAP_PREMULTIPLIED && Xchannel<3;
105     bool Yneedsdemul = MAP_PREMULTIPLIED && Ychannel<3;
106     if (!Xneedsdemul) scalex /= 255.0;
107     if (!Yneedsdemul) scaley /= 255.0;
109     for (int xout=out->area.x0; xout < out->area.x1; xout++){
110         for (int yout=out->area.y0; yout < out->area.y1; yout++){
111             int xmap = xout;
112             int ymap = yout;
114             pixel_t mapValue = pixelValue(map, xmap, ymap);
115             double xtex = xout + (Xneedsdemul ?
116                 (mapValue[3]==0?0:(scalex * (mapValue[Xchannel] - mapValue[3]*0.5) / mapValue[3])) :
117                 (scalex * (mapValue[Xchannel] - 127.5)));
118             double ytex = yout + (Yneedsdemul ?
119                 (mapValue[3]==0?0:(scaley * (mapValue[Ychannel] - mapValue[3]*0.5) / mapValue[3])) :
120                 (scaley * (mapValue[Ychannel] - 127.5)));
122             out_data[(xout-out->area.x0) + (out->area.x1-out->area.x0)*(yout-out->area.y0)] = interpolatePixels<DATA_PREMULTIPLIED>(texture, xtex, ytex);
123         }
124     }
127 int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
128     NRPixBlock *texture = slot.get(_input);
129     NRPixBlock *map = slot.get(_input2);
131     // Bail out if either one of source images is missing
132     if (!map || !texture) {
133         g_warning("Missing source image for feDisplacementMap (map=%d texture=%d)", _input, _input2);
134         return 1;
135     }
137     //TODO: check whether do we really need this check:
138     if (map->area.x1 <= map->area.x0 || map->area.y1 <=  map->area.y0) return 0; //nothing to do!
140     if (texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
141         g_warning("Source images without an alpha channel are not supported by feDisplacementMap at the moment.");
142         return 1;
143     }
145     NRPixBlock *out = new NRPixBlock;
146     
147     out->area.x0 = map->area.x0;
148     out->area.y0 = map->area.y0;
149     out->area.x1 = map->area.x1;
150     out->area.y1 = map->area.y1;
152     nr_pixblock_setup_fast(out, texture->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
154     // convert to a suitable format
155     bool free_map_on_exit = false;
156     if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && map->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
157         NRPixBlock *original_map = map;
158         map = new NRPixBlock;
159         nr_pixblock_setup_fast(map, NR_PIXBLOCK_MODE_R8G8B8A8N,
160                                original_map->area.x0, original_map->area.y0,
161                                original_map->area.x1, original_map->area.y1,
162                                false);
163         nr_blit_pixblock_pixblock(map, original_map);
164         free_map_on_exit = true;
165     }
166     bool map_premultiplied = (map->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
167     bool data_premultiplied = (out->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
169     Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
170     double scalex = scale * trans.expansionX();
171     double scaley = scale * trans.expansionY();
173     if (map_premultiplied && data_premultiplied) {
174         performDisplacement<true,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
175     } else if (map_premultiplied && !data_premultiplied) {
176         performDisplacement<true,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
177     } else if (data_premultiplied) {
178         performDisplacement<false,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
179     } else {
180         performDisplacement<false,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
181     }
183     if (free_map_on_exit) {
184         nr_pixblock_release(map);
185         delete map;
186     }
188     out->empty = FALSE;
189     slot.set(_output, out);
190             return 0;
193 void FilterDisplacementMap::set_input(int slot) {
194     _input = slot;
197 void FilterDisplacementMap::set_scale(double s) {
198     scale = s;
201 void FilterDisplacementMap::set_input(int input, int slot) {
202     if (input == 0) _input = slot;
203     if (input == 1) _input2 = slot;
206 void FilterDisplacementMap::set_channel_selector(int s, FilterDisplacementMapChannelSelector channel) {
207     if (channel > DISPLACEMENTMAP_CHANNEL_ALPHA || channel < DISPLACEMENTMAP_CHANNEL_RED) {
208         g_warning("Selected an invalid channel value. (%d)", channel);
209         return;
210     }
212     if (s == 0) Xchannel = channel;
213     if (s == 1) Ychannel = channel;
216 void FilterDisplacementMap::area_enlarge(NRRectL &area, Geom::Matrix const &trans)
218     //I assume scale is in user coordinates (?!?)
219     //FIXME: trans should be multiplied by some primitiveunits2user, shouldn't it?
220     
221     double scalex = scale/2.*(std::fabs(trans[0])+std::fabs(trans[1]));
222     double scaley = scale/2.*(std::fabs(trans[2])+std::fabs(trans[3]));
224     //FIXME: no +2 should be there!... (noticable only for big scales at big zoom factor)
225     area.x0 -= (int)(scalex)+2;
226     area.x1 += (int)(scalex)+2;
227     area.y0 -= (int)(scaley)+2;
228     area.y1 += (int)(scaley)+2;
231 FilterTraits FilterDisplacementMap::get_input_traits() {
232     return TRAIT_PARALLER;
235 } /* namespace Filters */
236 } /* namespace Inkscape */
238 /*
239   Local Variables:
240   mode:c++
241   c-file-style:"stroustrup"
242   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
243   indent-tabs-mode:nil
244   fill-column:99
245   End:
246 */
247 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :