Code

ab9d2e68b9b800ca18510886fd171fada18a00c2
[inkscape.git] / src / display / pixblock-transform.cpp
1 #define __NR_PIXBLOCK_SCALER_CPP__
3 /*
4  * Functions for blitting pixblocks using matrix transformation
5  *
6  * Author:
7  *   Niko Kiirala <niko@kiirala.com>
8  *
9  * Copyright (C) 2006 Niko Kiirala
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include <glib.h>
15 #include <cmath>
16 #if defined (SOLARIS_2_8)
17 #include "round.h"
18 using Inkscape::round;
19 #endif 
20 using std::floor;
22 #include "display/nr-filter-utils.h"
24 #include "libnr/nr-pixblock.h"
25 #include "libnr/nr-matrix.h"
27 namespace NR {
29 struct RGBA {
30     int r, g, b, a;
31 };
33 /**
34  * Sanity check function for indexing pixblocks.
35  * Catches reading and writing outside the pixblock area.
36  * When enabled, decreases filter rendering speed massively.
37  */
38 inline void _check_index(NRPixBlock const * const pb, int const location, int const line)
39 {
40     if(false) {
41         int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
42         if (location < 0 || (location + 4) > max_loc)
43             g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
44     }
45 }
47 void transform_nearest(NRPixBlock *to, NRPixBlock *from, Matrix &trans)
48 {
49     if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
50         g_warning("A non-32-bpp image passed to transform_nearest: scaling aborted.");
51         return;
52     }
54     // Precalculate sizes of source and destination pixblocks
55     int from_width = from->area.x1 - from->area.x0;
56     int from_height = from->area.y1 - from->area.y0;
57     int to_width = to->area.x1 - to->area.x0;
58     int to_height = to->area.y1 - to->area.y0;
60     Matrix itrans = trans.inverse();
62     // Loop through every pixel of destination image, a line at a time
63     for (int to_y = 0 ; to_y < to_height ; to_y++) {
64         for (int to_x = 0 ; to_x < to_width ; to_x++) {
65             RGBA result = {0,0,0,0};
67             int from_x = (int)round(itrans[0] * (to_x + to->area.x0)
68                                     + itrans[2] * (to_y + to->area.y0)
69                                     + itrans[4]);
70             from_x -= from->area.x0;
71             int from_y = (int)round(itrans[1] * (to_x + to->area.x0)
72                                     + itrans[3] * (to_y + to->area.y0)
73                                     + itrans[5]);
74             from_y -= from->area.y0;
76             if (from_x >= 0 && from_x < from_width
77                 && from_y >= 0 && from_y < from_height) {
78                 _check_index(from, from_y * from->rs + from_x * 4, __LINE__);
79                 result.r = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4];
80                 result.g = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 1];
81                 result.b = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 2];
82                 result.a = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 3];
83             }
85             _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
86             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = result.r;
87             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = result.g;
88             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = result.b;
89             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
90         }
91     }
92 }
94 /** Calculates cubically interpolated value of the four given pixel values.
95  * The pixel values should be from four vertically adjacent pixels.
96  * If we are calculating a pixel, whose y-coordinate in source image is
97  * i, these pixel values a, b, c and d should come from lines
98  * floor(i) - 1, floor(i), floor(i) + 1, floor(i) + 2, respectively.
99  * Parameter len should be set to i.
100  * Returns the interpolated value in fixed point format with 8 bit
101  * decimal part. (24.8 assuming 32-bit int)
102  */
103 __attribute__ ((const))
104 inline static int sampley(unsigned const char a, unsigned const char b,
105                    unsigned const char c, unsigned const char d,
106                    const double len)
108     double lenf = len - floor(len);
109     int sum = 0;
110     sum += (int)((((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0)
111                  * lenf * 256 * a);
112     sum += (int)((((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0)
113                  * 256 * b);
114     sum += (int)(((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0)
115                   * (1 - lenf) + 1.0) * 256 * c);
116     sum += (int)((((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf)
117                   - 7.0 / 15.0) * (1 - lenf) * 256 * d);
118     return sum;
121 /** Calculates cubically interpolated value of the four given pixel values.
122  * The pixel values should be interpolated values from sampley, from four
123  * horizontally adjacent vertical lines. The parameters a, b, c and d
124  * should be in fixed point format with 8-bit decimal part.
125  * If we are calculating a pixel, whose x-coordinate in source image is
126  * i, these vertical  lines from where a, b, c and d are calculated, should be
127  * floor(i) - 1, floor(i), floor(i) + 1, floor(i) + 2, respectively.
128  * Parameter len should be set to i.
129  * Returns the interpolated value in 8-bit format, ready to be written
130  * to output buffer.
131  */
132 inline static int samplex(const int a, const int b, const int c, const int d, const double len) {
133     double lenf = len - floor(len);
134     int sum = 0;
135     sum += (int)(a * (((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0) * lenf);
136     sum += (int)(b * (((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0));
137     sum += (int)(c * ((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0) * (1 - lenf) + 1.0));
138     sum += (int)(d * (((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf) - 7.0 / 15.0) * (1 - lenf));
139     //if (sum < 0) sum = 0;
140     //if (sum > 255 * 256) sum = 255 * 256;
141     return sum / 256;
144 void transform_bicubic(NRPixBlock *to, NRPixBlock *from, Matrix &trans)
146     if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
147         g_warning("A non-32-bpp image passed to transform_bicubic: scaling aborted.");
148         return;
149     }
151     // Precalculate sizes of source and destination pixblocks
152     int from_width = from->area.x1 - from->area.x0;
153     int from_height = from->area.y1 - from->area.y0;
154     int to_width = to->area.x1 - to->area.x0;
155     int to_height = to->area.y1 - to->area.y0;
157     Matrix itrans = trans.inverse();
159     // Loop through every pixel of destination image, a line at a time
160     for (int to_y = 0 ; to_y < to_height ; to_y++) {
161         for (int to_x = 0 ; to_x < to_width ; to_x++) {
162             double from_x = itrans[0] * (to_x + to->area.x0)
163                 + itrans[2] * (to_y + to->area.y0)
164                 + itrans[4] - from->area.x0;
165             double from_y = itrans[1] * (to_x + to->area.x0)
166                 + itrans[3] * (to_y + to->area.y0)
167                 + itrans[5] - from->area.y0;
169             if (from_x < 0 || from_x >= from_width ||
170                 from_y < 0 || from_y >= from_height) {
171                 continue;
172             }
174             RGBA line[4];
176             int from_line[4];
177             for (int i = 0 ; i < 4 ; i++) {
178                 if ((int)floor(from_y) + i - 1 >= 0) {
179                     if ((int)floor(from_y) + i - 1 < from_height) {
180                         from_line[i] = ((int)floor(from_y) + i - 1) * from->rs;
181                     } else {
182                         from_line[i] = (from_height - 1) * from->rs;
183                     }
184                 } else {
185                     from_line[i] = 0;
186                 }                
187             }
189             for (int i = 0 ; i < 4 ; i++) {
190                 int k = (int)floor(from_x) + i - 1;
191                 if (k < 0) k = 0;
192                 if (k >= from_width) k = from_width - 1;
193                 k *= 4;
194                 _check_index(from, from_line[0] + k, __LINE__);
195                 _check_index(from, from_line[1] + k, __LINE__);
196                 _check_index(from, from_line[2] + k, __LINE__);
197                 _check_index(from, from_line[3] + k, __LINE__);
198                 line[i].r = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k],
199                                     NR_PIXBLOCK_PX(from)[from_line[1] + k],
200                                     NR_PIXBLOCK_PX(from)[from_line[2] + k],
201                                     NR_PIXBLOCK_PX(from)[from_line[3] + k],
202                                     from_y);
203                 line[i].g = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1],
204                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 1],
205                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 1],
206                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 1],
207                                     from_y);
208                 line[i].b = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2],
209                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 2],
210                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 2],
211                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 2],
212                                     from_y);
213                 line[i].a = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3],
214                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 3],
215                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 3],
216                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 3],
217                                     from_y);
218             }
219             RGBA result;
220             result.r = samplex(line[0].r, line[1].r, line[2].r, line[3].r,
221                                from_x);
222             result.g = samplex(line[0].g, line[1].g, line[2].g, line[3].g,
223                                from_x);
224             result.b = samplex(line[0].b, line[1].b, line[2].b, line[3].b,
225                                from_x);
226             result.a = samplex(line[0].a, line[1].a, line[2].a, line[3].a,
227                                from_x);
229             _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
230             if (to->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
231                 /* Make sure, none of the RGB channels exceeds 100% intensity
232                  * in premultiplied output */
233                 result.a = clamp(result.a);
234                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = 
235                     clamp_alpha(result.r, result.a);
236                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = 
237                     clamp_alpha(result.g, result.a);
238                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = 
239                     clamp_alpha(result.b, result.a);
240                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
241             } else {
242                 /* Clamp the output to unsigned char range */
243                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = clamp(result.r);
244                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = clamp(result.g);
245                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = clamp(result.b);
246                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = clamp(result.a);
247             }
248         }
249     }
252 } /* namespace NR */
253 /*
254   Local Variables:
255   mode:c++
256   c-file-style:"stroustrup"
257   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
258   indent-tabs-mode:nil
259   fill-column:99
260   End:
261 */
262 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :