~valavanisalex/ubuntu/oneiric/inkscape/inkscape_0.48.1-2ubuntu4

« back to all changes in this revision

Viewing changes to src/display/nr-filter-displacement-map.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook, Ted Gould, Kees Cook
  • Date: 2009-06-24 14:00:43 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20090624140043-07stp20mry48hqup
Tags: 0.47~pre0-0ubuntu1
* New upstream release

[ Ted Gould ]
* debian/control: Adding libgsl0 and removing version specifics on boost

[ Kees Cook ]
* debian/watch: updated to run uupdate and mangle pre-release versions.
* Dropped patches that have been taken upstream:
  - 01_mips
  - 02-poppler-0.8.3
  - 03-chinese-inkscape
  - 05_fix_latex_patch
  - 06_gcc-4.4
  - 07_cdr2svg
  - 08_skip-bad-utf-on-pdf-import
  - 09_gtk-clist
  - 10_belarussian
  - 11_libpng
  - 12_desktop
  - 13_slider
  - 100_svg_import_improvements
  - 102_sp_pattern_painter_free
  - 103_bitmap_type_print

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#include "libnr/nr-blit.h"
16
16
#include "libnr/nr-pixops.h"
17
17
 
18
 
namespace NR {
 
18
namespace Inkscape {
 
19
namespace Filters {
19
20
 
20
21
FilterDisplacementMap::FilterDisplacementMap()
21
22
{}
27
28
FilterDisplacementMap::~FilterDisplacementMap()
28
29
{}
29
30
 
 
31
struct pixel_t {
 
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() {
 
36
        pixel_t p;
 
37
        for(unsigned int i=0; i<4; i++) {
 
38
            p[i] = 0;
 
39
        }
 
40
        return p;
 
41
    }
 
42
};
 
43
 
 
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);
 
48
    return data[offset];
 
49
}
 
50
 
 
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);
 
65
 
 
66
    /* It's a good idea to interpolate premultiplied colors:
 
67
     *
 
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
 
71
     *   containing no red.
 
72
     *
 
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. */
 
76
 
 
77
    /* First calculate interpolated alpha value. */
 
78
    unsigned ra = 0;
 
79
    if (!PREMULTIPLIED) {
 
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]
 
83
    }
 
84
 
 
85
    pixel_t r;
 
86
    if (ra == 0) {
 
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]);
 
93
 
 
94
            unsigned const ri = sf*y0 + yf*(y1-y0); // range [0,a*sf*sf]
 
95
            r[i] = (ri + sf2h)>>(2*sfl); // range [0,a]
 
96
        }
 
97
    } else {
 
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];
 
105
 
 
106
            // Interpolation.
 
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]
 
111
        }
 
112
        r[3] = (ra + sf2h)>>(2*sfl); // range [0,a]
 
113
    }
 
114
 
 
115
    return r;
 
116
}
 
117
 
 
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));
 
121
 
 
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;
 
126
 
 
127
    for (int yout=out->area.y0; yout < out->area.y1; yout++){
 
128
        for (int xout=out->area.x0; xout < out->area.x1; xout++){
 
129
            int xmap = xout;
 
130
            int ymap = yout;
 
131
 
 
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)));
 
139
 
 
140
            out_data[(xout-out->area.x0) + (out->area.x1-out->area.x0)*(yout-out->area.y0)] = interpolatePixels<DATA_PREMULTIPLIED>(texture, xtex, ytex);
 
141
        }
 
142
    }
 
143
}
 
144
 
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!
42
157
 
 
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.");
 
160
        return 1;
 
161
    }
 
162
 
43
163
    NRPixBlock *out = new NRPixBlock;
44
164
    
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;
49
 
 
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;
54
169
 
55
170
    nr_pixblock_setup_fast(out, texture->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
56
171
 
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,
65
180
                               false);
66
181
        nr_blit_pixblock_pixblock(map, original_map);
67
 
    }
68
 
 
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);
72
 
    int x, y;
73
 
    int in_w = map->area.x1 - map->area.x0;
74
 
    int in_h = map->area.y1 - map->area.y0;
75
 
    double coordx, coordy;
76
 
    
77
 
    Matrix trans = units.get_matrix_primitiveunits2pb();
78
 
    double scalex = scale*trans.expansionX();
79
 
    double scaley = scale*trans.expansionY();
80
 
    
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;
85
 
            if (xmap >= 0 &&
86
 
                xmap < in_w &&
87
 
                ymap >= 0 &&
88
 
                ymap < in_h){
89
 
 
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);
92
 
 
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];
98
 
                } else {
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;
103
 
                }
104
 
            }
105
 
        }
 
182
        free_map_on_exit = true;
 
183
    }
 
184
    bool map_premultiplied = (map->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
 
185
    bool data_premultiplied = (out->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
 
186
 
 
187
    Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
 
188
    double scalex = scale * trans.expansionX();
 
189
    double scaley = scale * trans.expansionY();
 
190
 
 
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);
 
197
    } else {
 
198
        performDisplacement<false,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
 
199
    }
 
200
 
 
201
    if (free_map_on_exit) {
 
202
        nr_pixblock_release(map);
 
203
        delete map;
106
204
    }
107
205
 
108
206
    out->empty = FALSE;
133
231
    if (s == 1) Ychannel = channel;
134
232
}
135
233
 
136
 
void FilterDisplacementMap::area_enlarge(NRRectL &area, Matrix const &trans)
 
234
void FilterDisplacementMap::area_enlarge(NRRectL &area, Geom::Matrix const &trans)
137
235
{
138
236
    //I assume scale is in user coordinates (?!?)
139
237
    //FIXME: trans should be multiplied by some primitiveunits2user, shouldn't it?
152
250
    return TRAIT_PARALLER;
153
251
}
154
252
 
155
 
} /* namespace NR */
 
253
} /* namespace Filters */
 
254
} /* namespace Inkscape */
156
255
 
157
256
/*
158
257
  Local Variables: