Code

Modified filter primitives' render method to use FilterUnits instead of Matrix
[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-pixops.h"
17 namespace NR {
19 FilterDisplacementMap::FilterDisplacementMap()
20 {}
22 FilterPrimitive * FilterDisplacementMap::create() {
23     return new FilterDisplacementMap();
24 }
26 FilterDisplacementMap::~FilterDisplacementMap()
27 {}
29 int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
30     NRPixBlock *texture = slot.get(_input);
31     NRPixBlock *map = slot.get(_input2);
32    
33     // Bail out if either one of source images is missing
34     if (!map || !texture) {
35         g_warning("Missing source image for feDisplacementMap (map=%d texture=%d)", _input, _input2);
36         return 1;
37     }
38     
39     //TODO: check whether do we really need this check:
40     if (map->area.x1 <= map->area.x0 || map->area.y1 <=  map->area.y0) return 0; //nothing to do!
41     
42     NRPixBlock *out = new NRPixBlock;    
44     //are these checks really necessary?
45     if (out_w > map->area.x1 - out_x0) out_w = map->area.x1 - out_x0;
46     if (out_h > map->area.y1 - out_y0) out_h = map->area.y1 - out_y0;    
47     if (out_x0 < map->area.x0){
48         out_x0 = map->area.x0;
49         out_w -= (map->area.x0 - out_x0);
50     }
51     if (out_y0 < map->area.y0){
52         out_y0 = map->area.y0;
53         out_h -= (map->area.y0 - out_y0);
54     }
56     out->area.x0 = out_x0;
57     out->area.y0 = out_y0;
58     out->area.x1 = out_x0 + out_w;
59     out->area.y1 = out_y0 + out_h;
61     nr_pixblock_setup_fast(out, map->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
62     
63     unsigned char *map_data = NR_PIXBLOCK_PX(map);
64     unsigned char *texture_data = NR_PIXBLOCK_PX(texture);
65     unsigned char *out_data = NR_PIXBLOCK_PX(out);
66     int x, y;
67     int in_w = map->area.x1 - map->area.x0;
68     int in_h = map->area.y1 - map->area.y0;
69     double coordx, coordy;
71     for (x=0; x < out_w; x++){
72         for (y=0; y < out_h; y++){
73             if (x+out_x0-map->area.x0 >= 0 &&
74                 x+out_x0-map->area.x0 < in_w &&
75                 y+out_y0-map->area.y0 >= 0 &&
76                 y+out_y0-map->area.y0 < in_h){
77                 
78                     coordx = out_x0 - map->area.x0 + x + scale * ( double(map_data[4*((x+out_x0-map->area.x0) + in_w*(y+out_y0-map->area.y0)) + Xchannel])/255 - 0.5);
79                     coordy = out_y0 - map->area.y0 + y + scale * ( double(map_data[4*((x+out_x0-map->area.x0) + in_w*(y+out_y0-map->area.y0)) + Ychannel])/255 - 0.5);
81                     if (coordx>=0 && coordx<in_w && coordy>=0 && coordy<in_h){
82                             out_data[4*(x + out_w*y)] = texture_data[4*(int(coordx) + int(coordy)*in_w)];
83                             out_data[4*(x + out_w*y) + 1] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 1];
84                             out_data[4*(x + out_w*y) + 2] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 2];
85                             out_data[4*(x + out_w*y) + 3] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 3];
86                     } else {
87                             out_data[4*(x + out_w*y)] = 255;
88                             out_data[4*(x + out_w*y) + 1] = 255;
89                             out_data[4*(x + out_w*y) + 2] = 255;
90                             out_data[4*(x + out_w*y) + 3] = 0;
91                     }
92             }
93         }
94     }
96     out->empty = FALSE;
97     slot.set(_output, out);
98             return 0;
99 }
101 void FilterDisplacementMap::set_input(int slot) {
102     _input = slot;
105 void FilterDisplacementMap::set_scale(double s) {
106     scale = s;
109 void FilterDisplacementMap::set_input(int input, int slot) {
110     if (input == 0) _input = slot;
111     if (input == 1) _input2 = slot;
114 void FilterDisplacementMap::set_channel_selector(int s, int channel) {
115     if (channel>3 || channel <0) {
116         g_warning("Selected an invalid channel value. (%d)", channel);
117         return;
118     }
120     if (s == 0) Xchannel = channel;
121     if (s == 1) Ychannel = channel;
124 void FilterDisplacementMap::area_enlarge(NRRectL &area, Matrix const &trans)
126     out_x0 = area.x0;
127     out_y0 = area.y0;
128     out_w = area.x1 - area.x0;
129     out_h = area.y1 - area.y0;
130     area.x0 -= (int)(scale/2);
131     area.x1 += (int)(scale/2);
132     area.y0 -= (int)(scale/2);
133     area.y1 += (int)(scale/2);
136 FilterTraits FilterDisplacementMap::get_input_traits() {
137     return TRAIT_PARALLER;
140 } /* namespace NR */
142 /*
143   Local Variables:
144   mode:c++
145   c-file-style:"stroustrup"
146   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
147   indent-tabs-mode:nil
148   fill-column:99
149   End:
150 */
151 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :