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 int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
32 NRPixBlock *texture = slot.get(_input);
33 NRPixBlock *map = slot.get(_input2);
35 // Bail out if either one of source images is missing
36 if (!map || !texture) {
37 g_warning("Missing source image for feDisplacementMap (map=%d texture=%d)", _input, _input2);
38 return 1;
39 }
41 //TODO: check whether do we really need this check:
42 if (map->area.x1 <= map->area.x0 || map->area.y1 <= map->area.y0) return 0; //nothing to do!
44 NRPixBlock *out = new NRPixBlock;
46 out_x0 = map->area.x0;
47 out_y0 = map->area.y0;
48 out_w = map->area.x1 - map->area.x0;
49 out_h = map->area.y1 - map->area.y0;
51 out->area.x0 = out_x0;
52 out->area.y0 = out_y0;
53 out->area.x1 = out_x0 + out_w;
54 out->area.y1 = out_y0 + out_h;
56 nr_pixblock_setup_fast(out, texture->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
58 // this primitive is defined for non-premultiplied RGBA values,
59 // thus convert them to that format
60 bool free_map_on_exit = false;
61 if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N) {
62 NRPixBlock *original_map = map;
63 map = new NRPixBlock;
64 nr_pixblock_setup_fast(map, NR_PIXBLOCK_MODE_R8G8B8A8N,
65 original_map->area.x0, original_map->area.y0,
66 original_map->area.x1, original_map->area.y1,
67 false);
68 nr_blit_pixblock_pixblock(map, original_map);
69 free_map_on_exit = true;
70 }
72 unsigned char *map_data = NR_PIXBLOCK_PX(map);
73 unsigned char *texture_data = NR_PIXBLOCK_PX(texture);
74 unsigned char *out_data = NR_PIXBLOCK_PX(out);
75 int x, y;
76 int in_w = map->area.x1 - map->area.x0;
77 int in_h = map->area.y1 - map->area.y0;
78 double coordx, coordy;
80 Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
81 double scalex = scale * trans.expansionX();
82 double scaley = scale * trans.expansionY();
84 for (x=0; x < out_w; x++){
85 for (y=0; y < out_h; y++){
86 int xmap = x+out_x0-map->area.x0;
87 int ymap = y+out_y0-map->area.y0;
88 if (xmap >= 0 &&
89 xmap < in_w &&
90 ymap >= 0 &&
91 ymap < in_h){
93 coordx = xmap + scalex * ( double(map_data[4*(xmap + in_w*ymap) + Xchannel]-128.)/256);
94 coordy = ymap + scaley * ( double(map_data[4*(xmap + in_w*ymap) + Ychannel]-128.)/256);
96 if (coordx>=0 && coordx<in_w && coordy>=0 && coordy<in_h){
97 out_data[4*(x + out_w*y) ] = texture_data[4*(int(coordx) + int(coordy)*in_w) ];
98 out_data[4*(x + out_w*y) + 1] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 1];
99 out_data[4*(x + out_w*y) + 2] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 2];
100 out_data[4*(x + out_w*y) + 3] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 3];
101 } else {
102 out_data[4*(x + out_w*y) ] = 255;
103 out_data[4*(x + out_w*y) + 1] = 255;
104 out_data[4*(x + out_w*y) + 2] = 255;
105 out_data[4*(x + out_w*y) + 3] = 0;
106 }
107 }
108 }
109 }
111 if (free_map_on_exit) {
112 nr_pixblock_release(map);
113 delete map;
114 }
116 out->empty = FALSE;
117 slot.set(_output, out);
118 return 0;
119 }
121 void FilterDisplacementMap::set_input(int slot) {
122 _input = slot;
123 }
125 void FilterDisplacementMap::set_scale(double s) {
126 scale = s;
127 }
129 void FilterDisplacementMap::set_input(int input, int slot) {
130 if (input == 0) _input = slot;
131 if (input == 1) _input2 = slot;
132 }
134 void FilterDisplacementMap::set_channel_selector(int s, FilterDisplacementMapChannelSelector channel) {
135 if (channel > DISPLACEMENTMAP_CHANNEL_ALPHA || channel < DISPLACEMENTMAP_CHANNEL_RED) {
136 g_warning("Selected an invalid channel value. (%d)", channel);
137 return;
138 }
140 if (s == 0) Xchannel = channel;
141 if (s == 1) Ychannel = channel;
142 }
144 void FilterDisplacementMap::area_enlarge(NRRectL &area, Geom::Matrix const &trans)
145 {
146 //I assume scale is in user coordinates (?!?)
147 //FIXME: trans should be multiplied by some primitiveunits2user, shouldn't it?
149 double scalex = scale/2.*(std::fabs(trans[0])+std::fabs(trans[1]));
150 double scaley = scale/2.*(std::fabs(trans[2])+std::fabs(trans[3]));
152 //FIXME: no +2 should be there!... (noticable only for big scales at big zoom factor)
153 area.x0 -= (int)(scalex)+2;
154 area.x1 += (int)(scalex)+2;
155 area.y0 -= (int)(scaley)+2;
156 area.y1 += (int)(scaley)+2;
157 }
159 FilterTraits FilterDisplacementMap::get_input_traits() {
160 return TRAIT_PARALLER;
161 }
163 } /* namespace Filters */
164 } /* namespace Inkscape */
166 /*
167 Local Variables:
168 mode:c++
169 c-file-style:"stroustrup"
170 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
171 indent-tabs-mode:nil
172 fill-column:99
173 End:
174 */
175 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :