27
28
FilterDisplacementMap::~FilterDisplacementMap()
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() {
37
for(unsigned int i=0; i<4; i++) {
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);
51
template<bool PREMULTIPLIED>
52
static pixel_t interpolatePixels(NRPixBlock const* pb, double x, double y) {
53
// NOTE: The values of x and y are shifted by -0.5 (the "true" values would be x+0.5 and y+0.5).
54
// This is done because otherwise the pixel values first have to be shifted by +0.5 and then by -0.5 again...
55
unsigned int const sfl = 8u;
56
unsigned int const sf = 1u<<sfl;
57
unsigned int const sf2h = 1u<<(2u*sfl-1);
58
int xi = (int)floor(x), yi = (int)floor(y);
59
unsigned int xf = static_cast<unsigned int>(round(sf * (x - xi))),
60
yf = static_cast<unsigned int>(round(sf * (y - yi)));
61
pixel_t p00 = pixelValue(pb, xi+0, yi+0);
62
pixel_t p01 = pixelValue(pb, xi+1, yi+0);
63
pixel_t p10 = pixelValue(pb, xi+0, yi+1);
64
pixel_t p11 = pixelValue(pb, xi+1, yi+1);
66
/* It's a good idea to interpolate premultiplied colors:
68
* Consider two pixels, one being rgba(255,0,0,0), which is fully transparent,
69
* and the other being rgba(0,0,255,255), or blue (fully opaque).
70
* If these two colors are interpolated the expected result would be bluish pixels
73
* However, if our final alpha value is zero, then the RGB values aren't really determinate.
74
* We might as well avoid premultiplication in this case, which still gives us a fully
75
* transparent result, but with interpolated RGB parts. */
77
/* First calculate interpolated alpha value. */
80
unsigned const y0 = sf*p00[3] + xf*(p01[3]-p00[3]); // range [0,a*sf]
81
unsigned const y1 = sf*p10[3] + xf*(p11[3]-p10[3]);
82
ra = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
87
/* Either premultiplied or the interpolated alpha value is zero,
88
* so do simple interpolation. */
89
for (unsigned i = 0; i != 4; ++i) {
90
// y0,y1 have range [0,a*sf]
91
unsigned const y0 = sf*p00[i] + xf*((unsigned int)p01[i]-(unsigned int)p00[i]);
92
unsigned const y1 = sf*p10[i] + xf*((unsigned int)p11[i]-(unsigned int)p10[i]);
94
unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
95
r[i] = (ri + sf2h)>>(2*sfl); // range [0,a]
98
/* Do premultiplication ourselves. */
99
for (unsigned i = 0; i != 3; ++i) {
100
// Premultiplied versions. Range [0,255*a].
101
unsigned const c00 = p00[i]*p00[3];
102
unsigned const c01 = p01[i]*p01[3];
103
unsigned const c10 = p10[i]*p10[3];
104
unsigned const c11 = p11[i]*p11[3];
107
unsigned const y0 = sf*c00 + xf*(c01-c00); // range [0,255*a*sf]
108
unsigned const y1 = sf*c10 + xf*(c11-c10); // range [0,255*a*sf]
109
unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,255*a*sf*sf]
110
r[i] = (ri + ra/2) / ra; // range [0,255]
112
r[3] = (ra + sf2h)>>(2*sfl); // range [0,a]
118
template<bool MAP_PREMULTIPLIED, bool DATA_PREMULTIPLIED>
119
static void performDisplacement(NRPixBlock const* texture, NRPixBlock const* map, int Xchannel, int Ychannel, NRPixBlock* out, double scalex, double scaley) {
120
pixel_t *out_data = reinterpret_cast<pixel_t*>(NR_PIXBLOCK_PX(out));
122
bool Xneedsdemul = MAP_PREMULTIPLIED && Xchannel<3;
123
bool Yneedsdemul = MAP_PREMULTIPLIED && Ychannel<3;
124
if (!Xneedsdemul) scalex /= 255.0;
125
if (!Yneedsdemul) scaley /= 255.0;
127
for (int yout=out->area.y0; yout < out->area.y1; yout++){
128
for (int xout=out->area.x0; xout < out->area.x1; xout++){
132
pixel_t mapValue = pixelValue(map, xmap, ymap);
133
double xtex = xout + (Xneedsdemul ? // Although the value of the pixel corresponds to the MIDDLE of the pixel, no +0.5 is needed because we're interpolating pixels anyway (so to get the actual pixel locations 0.5 would have to be subtracted again).
134
(mapValue[3]==0?0:(scalex * (mapValue[Xchannel] - mapValue[3]*0.5) / mapValue[3])) :
135
(scalex * (mapValue[Xchannel] - 127.5)));
136
double ytex = yout + (Yneedsdemul ?
137
(mapValue[3]==0?0:(scaley * (mapValue[Ychannel] - mapValue[3]*0.5) / mapValue[3])) :
138
(scaley * (mapValue[Ychannel] - 127.5)));
140
out_data[(xout-out->area.x0) + (out->area.x1-out->area.x0)*(yout-out->area.y0)] = interpolatePixels<DATA_PREMULTIPLIED>(texture, xtex, ytex);
30
145
int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
31
146
NRPixBlock *texture = slot.get(_input);
32
147
NRPixBlock *map = slot.get(_input2);
40
155
//TODO: check whether do we really need this check:
41
156
if (map->area.x1 <= map->area.x0 || map->area.y1 <= map->area.y0) return 0; //nothing to do!
158
if (texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
159
g_warning("Source images without an alpha channel are not supported by feDisplacementMap at the moment.");
43
163
NRPixBlock *out = new NRPixBlock;
45
out_x0 = map->area.x0;
46
out_y0 = map->area.y0;
47
out_w = map->area.x1 - map->area.x0;
48
out_h = map->area.y1 - map->area.y0;
50
out->area.x0 = out_x0;
51
out->area.y0 = out_y0;
52
out->area.x1 = out_x0 + out_w;
53
out->area.y1 = out_y0 + out_h;
165
out->area.x0 = map->area.x0;
166
out->area.y0 = map->area.y0;
167
out->area.x1 = map->area.x1;
168
out->area.y1 = map->area.y1;
55
170
nr_pixblock_setup_fast(out, texture->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
57
// this primitive is defined for non-premultiplied RGBA values,
58
// thus convert them to that format
59
if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N) {
172
// convert to a suitable format
173
bool free_map_on_exit = false;
174
if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && map->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
60
175
NRPixBlock *original_map = map;
61
176
map = new NRPixBlock;
62
177
nr_pixblock_setup_fast(map, NR_PIXBLOCK_MODE_R8G8B8A8N,
64
179
original_map->area.x1, original_map->area.y1,
66
181
nr_blit_pixblock_pixblock(map, original_map);
69
unsigned char *map_data = NR_PIXBLOCK_PX(map);
70
unsigned char *texture_data = NR_PIXBLOCK_PX(texture);
71
unsigned char *out_data = NR_PIXBLOCK_PX(out);
73
int in_w = map->area.x1 - map->area.x0;
74
int in_h = map->area.y1 - map->area.y0;
75
double coordx, coordy;
77
Matrix trans = units.get_matrix_primitiveunits2pb();
78
double scalex = scale*trans.expansionX();
79
double scaley = scale*trans.expansionY();
81
for (x=0; x < out_w; x++){
82
for (y=0; y < out_h; y++){
83
int xmap = x+out_x0-map->area.x0;
84
int ymap = y+out_y0-map->area.y0;
90
coordx = xmap + scalex * ( double(map_data[4*(xmap + in_w*ymap) + Xchannel]-128.)/256);
91
coordy = ymap + scaley * ( double(map_data[4*(xmap + in_w*ymap) + Ychannel]-128.)/256);
93
if (coordx>=0 && coordx<in_w && coordy>=0 && coordy<in_h){
94
out_data[4*(x + out_w*y) ] = texture_data[4*(int(coordx) + int(coordy)*in_w) ];
95
out_data[4*(x + out_w*y) + 1] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 1];
96
out_data[4*(x + out_w*y) + 2] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 2];
97
out_data[4*(x + out_w*y) + 3] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 3];
99
out_data[4*(x + out_w*y) ] = 255;
100
out_data[4*(x + out_w*y) + 1] = 255;
101
out_data[4*(x + out_w*y) + 2] = 255;
102
out_data[4*(x + out_w*y) + 3] = 0;
182
free_map_on_exit = true;
184
bool map_premultiplied = (map->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
185
bool data_premultiplied = (out->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
187
Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
188
double scalex = scale * trans.expansionX();
189
double scaley = scale * trans.expansionY();
191
if (map_premultiplied && data_premultiplied) {
192
performDisplacement<true,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
193
} else if (map_premultiplied && !data_premultiplied) {
194
performDisplacement<true,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
195
} else if (data_premultiplied) {
196
performDisplacement<false,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
198
performDisplacement<false,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
201
if (free_map_on_exit) {
202
nr_pixblock_release(map);
108
206
out->empty = FALSE;