Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[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,2009 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) && (SOLARIS == 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 <2geom/matrix.h>
28 namespace NR {
30 struct RGBA {
31     double r, g, b, a;
32 };
33 struct RGBAi {
34     int r, g, b, a;
35 };
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 }
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     }
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     }
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;
73     Geom::Matrix itrans = trans.inverse();
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};
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;
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             }
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     }
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)
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;
133     return sum;
136 void transform_bicubic(NRPixBlock *to, NRPixBlock *from, Geom::Matrix const &trans)
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     }
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     }
152     if (from->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
153         // TODO: Fix this... (The problem is that for interpolation non-premultiplied colors should be premultiplied...)
154         g_warning("transform_bicubic does not properly support non-premultiplied images");
155     }
156     
157     // Precalculate sizes of source and destination pixblocks
158     int from_width = from->area.x1 - from->area.x0;
159     int from_height = from->area.y1 - from->area.y0;
160     int to_width = to->area.x1 - to->area.x0;
161     int to_height = to->area.y1 - to->area.y0;
163     Geom::Matrix itrans = trans.inverse();
165     // Loop through every pixel of destination image, a line at a time
166     for (int to_y = 0 ; to_y < to_height ; to_y++) {
167         for (int to_x = 0 ; to_x < to_width ; to_x++) {
168             double from_x = itrans[0] * (to_x + 0.5 + to->area.x0)
169                 + itrans[2] * (to_y + 0.5 + to->area.y0)
170                 + itrans[4] - from->area.x0;
171             double from_y = itrans[1] * (to_x + 0.5 + to->area.x0)
172                 + itrans[3] * (to_y + 0.5 + to->area.y0)
173                 + itrans[5] - from->area.y0;
175             if (from_x < 0 || from_x >= from_width ||
176                 from_y < 0 || from_y >= from_height) {
177                 continue;
178             }
180             RGBA line[4];
182             int from_line[4];
183             for (int i = 0 ; i < 4 ; i++) {
184                 int fy_line = (int)round(from_y) + i - 2;
185                 if (fy_line >= 0) {
186                     if (fy_line < from_height) {
187                         from_line[i] = fy_line * from->rs;
188                     } else {
189                         from_line[i] = (from_height - 1) * from->rs;
190                     }
191                 } else {
192                     from_line[i] = 0;
193                 }                
194             }
196             for (int i = 0 ; i < 4 ; i++) {
197                 int k = (int)round(from_x) + i - 2;
198                 if (k < 0) k = 0;
199                 if (k >= from_width) k = from_width - 1;
200                 k *= 4;
201                 _check_index(from, from_line[0] + k, __LINE__);
202                 _check_index(from, from_line[1] + k, __LINE__);
203                 _check_index(from, from_line[2] + k, __LINE__);
204                 _check_index(from, from_line[3] + k, __LINE__);
205                 line[i].r = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k],
206                                    NR_PIXBLOCK_PX(from)[from_line[1] + k],
207                                    NR_PIXBLOCK_PX(from)[from_line[2] + k],
208                                    NR_PIXBLOCK_PX(from)[from_line[3] + k],
209                                    from_y);
210                 line[i].g = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1],
211                                    NR_PIXBLOCK_PX(from)[from_line[1] + k + 1],
212                                    NR_PIXBLOCK_PX(from)[from_line[2] + k + 1],
213                                    NR_PIXBLOCK_PX(from)[from_line[3] + k + 1],
214                                    from_y);
215                 line[i].b = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2],
216                                    NR_PIXBLOCK_PX(from)[from_line[1] + k + 2],
217                                    NR_PIXBLOCK_PX(from)[from_line[2] + k + 2],
218                                    NR_PIXBLOCK_PX(from)[from_line[3] + k + 2],
219                                    from_y);
220                 line[i].a = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3],
221                                    NR_PIXBLOCK_PX(from)[from_line[1] + k + 3],
222                                    NR_PIXBLOCK_PX(from)[from_line[2] + k + 3],
223                                    NR_PIXBLOCK_PX(from)[from_line[3] + k + 3],
224                                    from_y);
225             }
226             RGBA result;
227             result.r = round(sample(line[0].r, line[1].r, line[2].r, line[3].r,
228                                     from_x));
229             result.g = round(sample(line[0].g, line[1].g, line[2].g, line[3].g,
230                                     from_x));
231             result.b = round(sample(line[0].b, line[1].b, line[2].b, line[3].b,
232                                     from_x));
233             result.a = round(sample(line[0].a, line[1].a, line[2].a, line[3].a,
234                                     from_x));
236             using Inkscape::Filters::clamp;
237             using Inkscape::Filters::clamp_alpha;
238             _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
239             if (to->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
240                 /* Make sure, none of the RGB channels exceeds 100% intensity
241                  * in premultiplied output */
242                 int const alpha = clamp((int)result.a);
243                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = 
244                     clamp_alpha((int)result.r, alpha);
245                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = 
246                     clamp_alpha((int)result.g, alpha);
247                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = 
248                     clamp_alpha((int)result.b, alpha);
249                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = alpha;
250             } else {
251                 /* Clamp the output to unsigned char range */
252                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4]
253                     = clamp((int)result.r);
254                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1]
255                     = clamp((int)result.g);
256                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2]
257                     = clamp((int)result.b);
258                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3]
259                     = clamp((int)result.a);
260             }
261         }
262     }
263     if (free_from_on_exit) {
264         nr_pixblock_release(from);
265         delete from;
266     }
269 } /* namespace NR */
270 /*
271   Local Variables:
272   mode:c++
273   c-file-style:"stroustrup"
274   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
275   indent-tabs-mode:nil
276   fill-column:99
277   End:
278 */
279 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :