Code

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