Code

* use enums to deal with displacementmap channel selectors
[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);
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     }
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!
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);
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;
70     
71     Matrix trans = units.get_matrix_primitiveunits2pb();
72     double scalex = scale*trans.expansionX();
73     double scaley = scale*trans.expansionY();
74     
75     for (x=0; x < out_w; x++){
76         for (y=0; y < out_h; y++){
77             if (x+out_x0-map->area.x0 >= 0 &&
78                 x+out_x0-map->area.x0 < in_w &&
79                 y+out_y0-map->area.y0 >= 0 &&
80                 y+out_y0-map->area.y0 < in_h){
82                     coordx = out_x0 - map->area.x0 + x + scalex * ( double(map_data[4*((x+out_x0-map->area.x0) + in_w*(y+out_y0-map->area.y0)) + Xchannel])/255 - 0.5);
83                     coordy = out_y0 - map->area.y0 + y + scaley * ( double(map_data[4*((x+out_x0-map->area.x0) + in_w*(y+out_y0-map->area.y0)) + Ychannel])/255 - 0.5);
85                     if (coordx>=0 && coordx<in_w && coordy>=0 && coordy<in_h){
86                             out_data[4*(x + out_w*y)] = texture_data[4*(int(coordx) + int(coordy)*in_w)];
87                             out_data[4*(x + out_w*y) + 1] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 1];
88                             out_data[4*(x + out_w*y) + 2] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 2];
89                             out_data[4*(x + out_w*y) + 3] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 3];
90                     } else {
91                             out_data[4*(x + out_w*y)] = 255;
92                             out_data[4*(x + out_w*y) + 1] = 255;
93                             out_data[4*(x + out_w*y) + 2] = 255;
94                             out_data[4*(x + out_w*y) + 3] = 0;
95                     }
96             }
97         }
98     }
100     out->empty = FALSE;
101     slot.set(_output, out);
102             return 0;
105 void FilterDisplacementMap::set_input(int slot) {
106     _input = slot;
109 void FilterDisplacementMap::set_scale(double s) {
110     scale = s;
113 void FilterDisplacementMap::set_input(int input, int slot) {
114     if (input == 0) _input = slot;
115     if (input == 1) _input2 = slot;
118 void FilterDisplacementMap::set_channel_selector(int s, FilterDisplacementMapChannelSelector channel) {
119     if (channel > DISPLACEMENTMAP_CHANNEL_ALPHA || channel < DISPLACEMENTMAP_CHANNEL_RED) {
120         g_warning("Selected an invalid channel value. (%d)", channel);
121         return;
122     }
124     if (s == 0) Xchannel = channel;
125     if (s == 1) Ychannel = channel;
128 void FilterDisplacementMap::area_enlarge(NRRectL &area, Matrix const &trans)
130     out_x0 = area.x0;
131     out_y0 = area.y0;
132     out_w = area.x1 - area.x0;
133     out_h = area.y1 - area.y0;
134     
135     double scalex = scale*trans.expansionX();
136     double scaley = scale*trans.expansionY();
138     area.x0 -= (int)(scalex/2);
139     area.x1 += (int)(scalex/2);
140     area.y0 -= (int)(scaley/2);
141     area.y1 += (int)(scaley/2);
144 FilterTraits FilterDisplacementMap::get_input_traits() {
145     return TRAIT_PARALLER;
148 } /* namespace NR */
150 /*
151   Local Variables:
152   mode:c++
153   c-file-style:"stroustrup"
154   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
155   indent-tabs-mode:nil
156   fill-column:99
157   End:
158 */
159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :