~valavanisalex/ubuntu/precise/inkscape/fix-943984

« back to all changes in this revision

Viewing changes to inkscape-0.47pre1/src/display/pixblock-transform.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2009-07-02 17:09:45 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702170945-nn6d6zswovbwju1t
Tags: 0.47~pre1-0ubuntu1
* New upstream release.
  - Don't constrain maximization on small resolution devices (pre0)
    (LP: #348842)
  - Fixes segfault on startup (pre0)
    (LP: #391149)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define __NR_PIXBLOCK_SCALER_CPP__
 
2
 
 
3
/*
 
4
 * Functions for blitting pixblocks using matrix transformation
 
5
 *
 
6
 * Author:
 
7
 *   Niko Kiirala <niko@kiirala.com>
 
8
 *
 
9
 * Copyright (C) 2006,2009 Niko Kiirala
 
10
 *
 
11
 * Released under GNU GPL, read the file 'COPYING' for more information
 
12
 */
 
13
 
 
14
#include <glib.h>
 
15
#include <cmath>
 
16
#if defined (SOLARIS) && (SOLARIS == 8)
 
17
#include "round.h"
 
18
using Inkscape::round;
 
19
#endif 
 
20
using std::floor;
 
21
 
 
22
#include "display/nr-filter-utils.h"
 
23
 
 
24
#include "libnr/nr-blit.h"
 
25
#include "libnr/nr-pixblock.h"
 
26
#include <2geom/matrix.h>
 
27
 
 
28
namespace NR {
 
29
 
 
30
struct RGBA {
 
31
    double r, g, b, a;
 
32
};
 
33
struct RGBAi {
 
34
    int r, g, b, a;
 
35
};
 
36
 
 
37
/**
 
38
 * Sanity check function for indexing pixblocks.
 
39
 * Catches reading and writing outside the pixblock area.
 
40
 * When enabled, decreases filter rendering speed massively.
 
41
 */
 
42
inline void _check_index(NRPixBlock const * const pb, int const location, int const line)
 
43
{
 
44
    if(false) {
 
45
        int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
 
46
        if (location < 0 || (location + 4) > max_loc)
 
47
            g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
 
48
    }
 
49
}
 
50
 
 
51
void transform_nearest(NRPixBlock *to, NRPixBlock *from, Geom::Matrix const &trans)
 
52
{
 
53
    if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
 
54
        g_warning("A non-32-bpp image passed to transform_nearest: scaling aborted.");
 
55
        return;
 
56
    }
 
57
 
 
58
    bool free_from_on_exit = false;
 
59
    if (from->mode != to->mode){
 
60
        NRPixBlock *o_from = from;
 
61
        from = new NRPixBlock;
 
62
        nr_pixblock_setup_fast(from, to->mode, o_from->area.x0, o_from->area.y0, o_from->area.x1, o_from->area.y1, false);
 
63
        nr_blit_pixblock_pixblock(from, o_from);
 
64
        free_from_on_exit = true;
 
65
    }
 
66
 
 
67
    // Precalculate sizes of source and destination pixblocks
 
68
    int from_width = from->area.x1 - from->area.x0;
 
69
    int from_height = from->area.y1 - from->area.y0;
 
70
    int to_width = to->area.x1 - to->area.x0;
 
71
    int to_height = to->area.y1 - to->area.y0;
 
72
 
 
73
    Geom::Matrix itrans = trans.inverse();
 
74
 
 
75
    // Loop through every pixel of destination image, a line at a time
 
76
    for (int to_y = 0 ; to_y < to_height ; to_y++) {
 
77
        for (int to_x = 0 ; to_x < to_width ; to_x++) {
 
78
            RGBAi result = {0,0,0,0};
 
79
 
 
80
            int from_x = (int)floor(itrans[0] * (to_x + 0.5 + to->area.x0)
 
81
                                    + itrans[2] * (to_y + 0.5 + to->area.y0)
 
82
                                    + itrans[4]);
 
83
            from_x -= from->area.x0;
 
84
            int from_y = (int)floor(itrans[1] * (to_x + 0.5 + to->area.x0)
 
85
                                    + itrans[3] * (to_y + 0.5 + to->area.y0)
 
86
                                    + itrans[5]);
 
87
            from_y -= from->area.y0;
 
88
 
 
89
            if (from_x >= 0 && from_x < from_width
 
90
                && from_y >= 0 && from_y < from_height) {
 
91
                _check_index(from, from_y * from->rs + from_x * 4, __LINE__);
 
92
                result.r = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4];
 
93
                result.g = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 1];
 
94
                result.b = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 2];
 
95
                result.a = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 3];
 
96
            }
 
97
 
 
98
            _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
 
99
            NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = result.r;
 
100
            NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = result.g;
 
101
            NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = result.b;
 
102
            NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
 
103
        }
 
104
    }
 
105
    if (free_from_on_exit) {
 
106
        nr_pixblock_release(from);
 
107
        delete from;
 
108
    }
 
109
}
 
110
 
 
111
/** Calculates cubically interpolated value of the four given pixel values.
 
112
 * The pixel values should be from four adjacent pixels in source image or
 
113
 * four adjacent interpolated values. len should be the x- or y-coordinate
 
114
 * (depending on interpolation direction) of the center of the target pixel
 
115
 * in source image coordinates.
 
116
 */
 
117
__attribute__ ((const))
 
118
inline static double sample(double const a, double const b,
 
119
                            double const c, double const d,
 
120
                            double const len)
 
121
{
 
122
    double lena = 1.5 + (len - round(len));
 
123
    double lenb = 0.5 + (len - round(len));
 
124
    double lenc = 0.5 - (len - round(len));
 
125
    double lend = 1.5 - (len - round(len));
 
126
    double const f = -0.5; // corresponds to cubic Hermite spline
 
127
    double sum = 0;
 
128
    sum += ((((f * lena) - 5.0 * f) * lena + 8.0 * f) * lena - 4 * f) * a;
 
129
    sum += (((f + 2.0) * lenb - (f + 3.0)) * lenb * lenb + 1.0) * b;
 
130
    sum += (((f + 2.0) * lenc - (f + 3.0)) * lenc * lenc + 1.0) * c;
 
131
    sum += ((((f * lend) - 5.0 * f) * lend + 8.0 * f) * lend - 4 * f) * d;
 
132
 
 
133
    return sum;
 
134
}
 
135
 
 
136
void transform_bicubic(NRPixBlock *to, NRPixBlock *from, Geom::Matrix const &trans)
 
137
{
 
138
    if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
 
139
        g_warning("A non-32-bpp image passed to transform_bicubic: scaling aborted.");
 
140
        return;
 
141
    }
 
142
 
 
143
    bool free_from_on_exit = false;
 
144
    if (from->mode != to->mode){
 
145
        NRPixBlock *o_from = from;
 
146
        from = new NRPixBlock;
 
147
        nr_pixblock_setup_fast(from, to->mode, o_from->area.x0, o_from->area.y0, o_from->area.x1, o_from->area.y1, false);
 
148
        nr_blit_pixblock_pixblock(from, o_from);
 
149
        free_from_on_exit = true;
 
150
    }
 
151
    
 
152
    // Precalculate sizes of source and destination pixblocks
 
153
    int from_width = from->area.x1 - from->area.x0;
 
154
    int from_height = from->area.y1 - from->area.y0;
 
155
    int to_width = to->area.x1 - to->area.x0;
 
156
    int to_height = to->area.y1 - to->area.y0;
 
157
 
 
158
    Geom::Matrix itrans = trans.inverse();
 
159
 
 
160
    // Loop through every pixel of destination image, a line at a time
 
161
    for (int to_y = 0 ; to_y < to_height ; to_y++) {
 
162
        for (int to_x = 0 ; to_x < to_width ; to_x++) {
 
163
            double from_x = itrans[0] * (to_x + 0.5 + to->area.x0)
 
164
                + itrans[2] * (to_y + 0.5 + to->area.y0)
 
165
                + itrans[4] - from->area.x0;
 
166
            double from_y = itrans[1] * (to_x + 0.5 + to->area.x0)
 
167
                + itrans[3] * (to_y + 0.5 + to->area.y0)
 
168
                + itrans[5] - from->area.y0;
 
169
 
 
170
            if (from_x < 0 || from_x >= from_width ||
 
171
                from_y < 0 || from_y >= from_height) {
 
172
                continue;
 
173
            }
 
174
 
 
175
            RGBA line[4];
 
176
 
 
177
            int from_line[4];
 
178
            for (int i = 0 ; i < 4 ; i++) {
 
179
                int fy_line = (int)round(from_y) + i - 2;
 
180
                if (fy_line >= 0) {
 
181
                    if (fy_line < from_height) {
 
182
                        from_line[i] = fy_line * from->rs;
 
183
                    } else {
 
184
                        from_line[i] = (from_height - 1) * from->rs;
 
185
                    }
 
186
                } else {
 
187
                    from_line[i] = 0;
 
188
                }                
 
189
            }
 
190
 
 
191
            for (int i = 0 ; i < 4 ; i++) {
 
192
                int k = (int)round(from_x) + i - 2;
 
193
                if (k < 0) k = 0;
 
194
                if (k >= from_width) k = from_width - 1;
 
195
                k *= 4;
 
196
                _check_index(from, from_line[0] + k, __LINE__);
 
197
                _check_index(from, from_line[1] + k, __LINE__);
 
198
                _check_index(from, from_line[2] + k, __LINE__);
 
199
                _check_index(from, from_line[3] + k, __LINE__);
 
200
                line[i].r = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k],
 
201
                                   NR_PIXBLOCK_PX(from)[from_line[1] + k],
 
202
                                   NR_PIXBLOCK_PX(from)[from_line[2] + k],
 
203
                                   NR_PIXBLOCK_PX(from)[from_line[3] + k],
 
204
                                   from_y);
 
205
                line[i].g = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1],
 
206
                                   NR_PIXBLOCK_PX(from)[from_line[1] + k + 1],
 
207
                                   NR_PIXBLOCK_PX(from)[from_line[2] + k + 1],
 
208
                                   NR_PIXBLOCK_PX(from)[from_line[3] + k + 1],
 
209
                                   from_y);
 
210
                line[i].b = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2],
 
211
                                   NR_PIXBLOCK_PX(from)[from_line[1] + k + 2],
 
212
                                   NR_PIXBLOCK_PX(from)[from_line[2] + k + 2],
 
213
                                   NR_PIXBLOCK_PX(from)[from_line[3] + k + 2],
 
214
                                   from_y);
 
215
                line[i].a = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3],
 
216
                                   NR_PIXBLOCK_PX(from)[from_line[1] + k + 3],
 
217
                                   NR_PIXBLOCK_PX(from)[from_line[2] + k + 3],
 
218
                                   NR_PIXBLOCK_PX(from)[from_line[3] + k + 3],
 
219
                                   from_y);
 
220
            }
 
221
            RGBA result;
 
222
            result.r = round(sample(line[0].r, line[1].r, line[2].r, line[3].r,
 
223
                                    from_x));
 
224
            result.g = round(sample(line[0].g, line[1].g, line[2].g, line[3].g,
 
225
                                    from_x));
 
226
            result.b = round(sample(line[0].b, line[1].b, line[2].b, line[3].b,
 
227
                                    from_x));
 
228
            result.a = round(sample(line[0].a, line[1].a, line[2].a, line[3].a,
 
229
                                    from_x));
 
230
 
 
231
            using Inkscape::Filters::clamp;
 
232
            using Inkscape::Filters::clamp_alpha;
 
233
            _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
 
234
            if (to->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
 
235
                /* Make sure, none of the RGB channels exceeds 100% intensity
 
236
                 * in premultiplied output */
 
237
                int const alpha = clamp((int)result.a);
 
238
                NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = 
 
239
                    clamp_alpha((int)result.r, alpha);
 
240
                NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = 
 
241
                    clamp_alpha((int)result.g, alpha);
 
242
                NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = 
 
243
                    clamp_alpha((int)result.b, alpha);
 
244
                NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = alpha;
 
245
            } else {
 
246
                /* Clamp the output to unsigned char range */
 
247
                NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4]
 
248
                    = clamp((int)result.r);
 
249
                NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1]
 
250
                    = clamp((int)result.g);
 
251
                NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2]
 
252
                    = clamp((int)result.b);
 
253
                NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3]
 
254
                    = clamp((int)result.a);
 
255
            }
 
256
        }
 
257
    }
 
258
    if (free_from_on_exit) {
 
259
        nr_pixblock_release(from);
 
260
        delete from;
 
261
    }
 
262
}
 
263
 
 
264
} /* namespace NR */
 
265
/*
 
266
  Local Variables:
 
267
  mode:c++
 
268
  c-file-style:"stroustrup"
 
269
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
270
  indent-tabs-mode:nil
 
271
  fill-column:99
 
272
  End:
 
273
*/
 
274
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :