Code

use all 2geom typed pathv_matrix_point_bbox_wind_distance
[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-blit.h"
25 #include "libnr/nr-pixblock.h"
26 #include "libnr/nr-matrix.h"
28 namespace NR {
30 struct RGBA {
31     int r, g, b, a;
32 };
34 /**
35  * Sanity check function for indexing pixblocks.
36  * Catches reading and writing outside the pixblock area.
37  * When enabled, decreases filter rendering speed massively.
38  */
39 inline void _check_index(NRPixBlock const * const pb, int const location, int const line)
40 {
41     if(false) {
42         int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
43         if (location < 0 || (location + 4) > max_loc)
44             g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
45     }
46 }
48 void transform_nearest(NRPixBlock *to, NRPixBlock *from, Matrix &trans)
49 {
50     if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
51         g_warning("A non-32-bpp image passed to transform_nearest: scaling aborted.");
52         return;
53     }
55     bool free_from_on_exit = false;
56     if (from->mode != to->mode){
57         NRPixBlock *o_from = from;
58         from = new NRPixBlock;
59         nr_pixblock_setup_fast(from, to->mode, o_from->area.x0, o_from->area.y0, o_from->area.x1, o_from->area.y1, false);
60         nr_blit_pixblock_pixblock(from, o_from);
61         free_from_on_exit = true;
62     }
64     // Precalculate sizes of source and destination pixblocks
65     int from_width = from->area.x1 - from->area.x0;
66     int from_height = from->area.y1 - from->area.y0;
67     int to_width = to->area.x1 - to->area.x0;
68     int to_height = to->area.y1 - to->area.y0;
70     Matrix itrans = trans.inverse();
72     // Loop through every pixel of destination image, a line at a time
73     for (int to_y = 0 ; to_y < to_height ; to_y++) {
74         for (int to_x = 0 ; to_x < to_width ; to_x++) {
75             RGBA result = {0,0,0,0};
77             int from_x = (int)round(itrans[0] * (to_x + to->area.x0)
78                                     + itrans[2] * (to_y + to->area.y0)
79                                     + itrans[4]);
80             from_x -= from->area.x0;
81             int from_y = (int)round(itrans[1] * (to_x + to->area.x0)
82                                     + itrans[3] * (to_y + to->area.y0)
83                                     + itrans[5]);
84             from_y -= from->area.y0;
86             if (from_x >= 0 && from_x < from_width
87                 && from_y >= 0 && from_y < from_height) {
88                 _check_index(from, from_y * from->rs + from_x * 4, __LINE__);
89                 result.r = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4];
90                 result.g = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 1];
91                 result.b = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 2];
92                 result.a = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 3];
93             }
95             _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
96             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = result.r;
97             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = result.g;
98             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = result.b;
99             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
100         }
101     }
102     if (free_from_on_exit) {
103         nr_pixblock_release(from);
104         delete from;
105     }
108 /** Calculates cubically interpolated value of the four given pixel values.
109  * The pixel values should be from four vertically adjacent pixels.
110  * If we are calculating a pixel, whose y-coordinate in source image is
111  * i, these pixel values a, b, c and d should come from lines
112  * floor(i) - 1, floor(i), floor(i) + 1, floor(i) + 2, respectively.
113  * Parameter len should be set to i.
114  * Returns the interpolated value in fixed point format with 8 bit
115  * decimal part. (24.8 assuming 32-bit int)
116  */
117 __attribute__ ((const))
118 inline static int sampley(unsigned const char a, unsigned const char b,
119                    unsigned const char c, unsigned const char d,
120                    const double len)
122     double lenf = len - floor(len);
123     int sum = 0;
124     sum += (int)((((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0)
125                  * lenf * 256 * a);
126     sum += (int)((((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0)
127                  * 256 * b);
128     sum += (int)(((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0)
129                   * (1 - lenf) + 1.0) * 256 * c);
130     sum += (int)((((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf)
131                   - 7.0 / 15.0) * (1 - lenf) * 256 * d);
132     return sum;
135 /** Calculates cubically interpolated value of the four given pixel values.
136  * The pixel values should be interpolated values from sampley, from four
137  * horizontally adjacent vertical lines. The parameters a, b, c and d
138  * should be in fixed point format with 8-bit decimal part.
139  * If we are calculating a pixel, whose x-coordinate in source image is
140  * i, these vertical  lines from where a, b, c and d are calculated, should be
141  * floor(i) - 1, floor(i), floor(i) + 1, floor(i) + 2, respectively.
142  * Parameter len should be set to i.
143  * Returns the interpolated value in 8-bit format, ready to be written
144  * to output buffer.
145  */
146 inline static int samplex(const int a, const int b, const int c, const int d, const double len) {
147     double lenf = len - floor(len);
148     int sum = 0;
149     sum += (int)(a * (((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0) * lenf);
150     sum += (int)(b * (((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0));
151     sum += (int)(c * ((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0) * (1 - lenf) + 1.0));
152     sum += (int)(d * (((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf) - 7.0 / 15.0) * (1 - lenf));
153     //if (sum < 0) sum = 0;
154     //if (sum > 255 * 256) sum = 255 * 256;
155     return sum / 256;
158 void transform_bicubic(NRPixBlock *to, NRPixBlock *from, Matrix &trans)
160     if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
161         g_warning("A non-32-bpp image passed to transform_bicubic: scaling aborted.");
162         return;
163     }
165     bool free_from_on_exit = false;
166     if (from->mode != to->mode){
167         NRPixBlock *o_from = from;
168         from = new NRPixBlock;
169         nr_pixblock_setup_fast(from, to->mode, o_from->area.x0, o_from->area.y0, o_from->area.x1, o_from->area.y1, false);
170         nr_blit_pixblock_pixblock(from, o_from);
171         free_from_on_exit = true;
172     }
173     
174     // Precalculate sizes of source and destination pixblocks
175     int from_width = from->area.x1 - from->area.x0;
176     int from_height = from->area.y1 - from->area.y0;
177     int to_width = to->area.x1 - to->area.x0;
178     int to_height = to->area.y1 - to->area.y0;
180     Matrix itrans = trans.inverse();
182     // Loop through every pixel of destination image, a line at a time
183     for (int to_y = 0 ; to_y < to_height ; to_y++) {
184         for (int to_x = 0 ; to_x < to_width ; to_x++) {
185             double from_x = itrans[0] * (to_x + to->area.x0)
186                 + itrans[2] * (to_y + to->area.y0)
187                 + itrans[4] - from->area.x0;
188             double from_y = itrans[1] * (to_x + to->area.x0)
189                 + itrans[3] * (to_y + to->area.y0)
190                 + itrans[5] - from->area.y0;
192             if (from_x < 0 || from_x >= from_width ||
193                 from_y < 0 || from_y >= from_height) {
194                 continue;
195             }
197             RGBA line[4];
199             int from_line[4];
200             for (int i = 0 ; i < 4 ; i++) {
201                 if ((int)floor(from_y) + i - 1 >= 0) {
202                     if ((int)floor(from_y) + i - 1 < from_height) {
203                         from_line[i] = ((int)floor(from_y) + i - 1) * from->rs;
204                     } else {
205                         from_line[i] = (from_height - 1) * from->rs;
206                     }
207                 } else {
208                     from_line[i] = 0;
209                 }                
210             }
212             for (int i = 0 ; i < 4 ; i++) {
213                 int k = (int)floor(from_x) + i - 1;
214                 if (k < 0) k = 0;
215                 if (k >= from_width) k = from_width - 1;
216                 k *= 4;
217                 _check_index(from, from_line[0] + k, __LINE__);
218                 _check_index(from, from_line[1] + k, __LINE__);
219                 _check_index(from, from_line[2] + k, __LINE__);
220                 _check_index(from, from_line[3] + k, __LINE__);
221                 line[i].r = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k],
222                                     NR_PIXBLOCK_PX(from)[from_line[1] + k],
223                                     NR_PIXBLOCK_PX(from)[from_line[2] + k],
224                                     NR_PIXBLOCK_PX(from)[from_line[3] + k],
225                                     from_y);
226                 line[i].g = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1],
227                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 1],
228                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 1],
229                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 1],
230                                     from_y);
231                 line[i].b = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2],
232                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 2],
233                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 2],
234                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 2],
235                                     from_y);
236                 line[i].a = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3],
237                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 3],
238                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 3],
239                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 3],
240                                     from_y);
241             }
242             RGBA result;
243             result.r = samplex(line[0].r, line[1].r, line[2].r, line[3].r,
244                                from_x);
245             result.g = samplex(line[0].g, line[1].g, line[2].g, line[3].g,
246                                from_x);
247             result.b = samplex(line[0].b, line[1].b, line[2].b, line[3].b,
248                                from_x);
249             result.a = samplex(line[0].a, line[1].a, line[2].a, line[3].a,
250                                from_x);
252             _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
253             if (to->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
254                 /* Make sure, none of the RGB channels exceeds 100% intensity
255                  * in premultiplied output */
256                 result.a = clamp(result.a);
257                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = 
258                     clamp_alpha(result.r, result.a);
259                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = 
260                     clamp_alpha(result.g, result.a);
261                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = 
262                     clamp_alpha(result.b, result.a);
263                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
264             } else {
265                 /* Clamp the output to unsigned char range */
266                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = clamp(result.r);
267                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = clamp(result.g);
268                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = clamp(result.b);
269                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = clamp(result.a);
270             }
271         }
272     }
273     if (free_from_on_exit) {
274         nr_pixblock_release(from);
275         delete from;
276     }
279 } /* namespace NR */
280 /*
281   Local Variables:
282   mode:c++
283   c-file-style:"stroustrup"
284   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
285   indent-tabs-mode:nil
286   fill-column:99
287   End:
288 */
289 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :