Code

Fixed rendering glitch in bicubic scaler
[inkscape.git] / src / display / pixblock-scaler.cpp
1 #define __NR_PIXBLOCK_SCALER_CPP__
3 /*
4  * Functions for blitting pixblocks using scaling
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 using std::floor;
18 #include "display/nr-filter-utils.h"
19 #include "libnr/nr-pixblock.h"
21 namespace NR {
23 struct RGBA {
24     int r, g, b, a;
25 };
27 /** Calculates cubically interpolated value of the four given pixel values.
28  * The pixel values should be from four vertically adjacent pixels.
29  * If we are calculating a pixel, whose y-coordinate in source image is
30  * i, these pixel values a, b, c and d should come from lines
31  * floor(i) - 1, floor(i), floor(i) + 1, floor(i) + 2, respectively.
32  * Parameter len should be set to i.
33  * Returns the interpolated value in fixed point format with 8 bit
34  * decimal part. (24.8 assuming 32-bit int)
35  */
36 __attribute__ ((const))
37 inline int sampley(unsigned const char a, unsigned const char b,
38                    unsigned const char c, unsigned const char d,
39                    const double len)
40 {
41     double lenf = len - floor(len);
42     int sum = 0;
43     sum += (int)((((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0)
44                  * lenf * 256 * a);
45     sum += (int)((((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0)
46                  * 256 * b);
47     sum += (int)(((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0)
48                   * (1 - lenf) + 1.0) * 256 * c);
49     sum += (int)((((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf)
50                   - 7.0 / 15.0) * (1 - lenf) * 256 * d);
51     return sum;
52 }
54 /** Calculates cubically interpolated value of the four given pixel values.
55  * The pixel values should be interpolated values from sampley, from four
56  * horizontally adjacent vertical lines. The parameters a, b, c and d
57  * should be in fixed point format with 8-bit decimal part.
58  * If we are calculating a pixel, whose x-coordinate in source image is
59  * i, these vertical  lines from where a, b, c and d are calculated, should be
60  * floor(i) - 1, floor(i), floor(i) + 1, floor(i) + 2, respectively.
61  * Parameter len should be set to i.
62  * Returns the interpolated value in 8-bit format, ready to be written
63  * to output buffer.
64  */
65 inline int samplex(const int a, const int b, const int c, const int d, const double len) {
66     double lenf = len - floor(len);
67     int sum = 0;
68     sum += (int)(a * (((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0) * lenf);
69     sum += (int)(b * (((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0));
70     sum += (int)(c * ((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0) * (1 - lenf) + 1.0));
71     sum += (int)(d * (((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf) - 7.0 / 15.0) * (1 - lenf));
72     //if (sum < 0) sum = 0;
73     //if (sum > 255 * 256) sum = 255 * 256;
74     return sum / 256;
75 }
77 /**
78  * Sanity check function for indexing pixblocks.
79  * Catches reading and writing outside the pixblock area.
80  * When enabled, decreases filter rendering speed massively.
81  */
82 inline void _check_index(NRPixBlock const * const pb, int const location, int const line)
83 {
84     if(false) {
85         int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
86         if (location < 0 || (location + 4) > max_loc)
87             g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
88     }
89 }
91 void scale_bicubic(NRPixBlock *to, NRPixBlock *from)
92 {
93     if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
94         g_warning("A non-32-bpp image passed to scale_bicubic: scaling aborted.");
95         return;
96     }
98     // Precalculate sizes of source and destination pixblocks
99     int from_width = from->area.x1 - from->area.x0;
100     int from_height = from->area.y1 - from->area.y0;
101     int to_width = to->area.x1 - to->area.x0;
102     int to_height = to->area.y1 - to->area.y0;
104     // from_step: when advancing one pixel in destination image,
105     // how much we should advance in source image
106     double from_stepx = (double)from_width / (double)to_width;
107     double from_stepy = (double)from_height / (double)to_height;
109     // Loop through every pixel of destination image, a line at a time
110     for (int to_y = 0 ; to_y < to_height ; to_y++) {
111         double from_y = to_y * from_stepy + from_stepy / 2;
112         // Pre-calculate beginning of the four horizontal lines, from
113         // which we should read
114         int from_line[4];
115         for (int i = 0 ; i < 4 ; i++) {
116             if ((int)floor(from_y) + i - 1 >= 0) {
117                 if ((int)floor(from_y) + i - 1 < from_height) {
118                     from_line[i] = ((int)floor(from_y) + i - 1) * from->rs;
119                 } else {
120                     from_line[i] = (from_height - 1) * from->rs;
121                 }
122             } else {
123                 from_line[i] = 0;
124             }
125         }
126         // Loop through this horizontal line in destination image
127         // For every pixel, calculate the color of pixel with
128         // bicubic interpolation and set the pixel value in destination image
129         for (int to_x = 0 ; to_x < to_width ; to_x++) {
130             double from_x = to_x * from_stepx + from_stepx / 2;
131             RGBA line[4];
132             for (int i = 0 ; i < 4 ; i++) {
133                 int k = (int)floor(from_x) + i - 1;
134                 if (k < 0) k = 0;
135                 if (k >= from_width) k = from_width - 1;
136                 k *= 4;
137                 _check_index(from, from_line[0] + k, __LINE__);
138                 _check_index(from, from_line[1] + k, __LINE__);
139                 _check_index(from, from_line[2] + k, __LINE__);
140                 _check_index(from, from_line[3] + k, __LINE__);
141                 line[i].r = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k],
142                                     NR_PIXBLOCK_PX(from)[from_line[1] + k],
143                                     NR_PIXBLOCK_PX(from)[from_line[2] + k],
144                                     NR_PIXBLOCK_PX(from)[from_line[3] + k],
145                                     from_y);
146                 line[i].g = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1],
147                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 1],
148                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 1],
149                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 1],
150                                     from_y);
151                 line[i].b = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2],
152                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 2],
153                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 2],
154                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 2],
155                                     from_y);
156                 line[i].a = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3],
157                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 3],
158                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 3],
159                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 3],
160                                     from_y);
161             }
162             RGBA result;
163             result.r = samplex(line[0].r, line[1].r, line[2].r, line[3].r,
164                                from_x);
165             result.g = samplex(line[0].g, line[1].g, line[2].g, line[3].g,
166                                from_x);
167             result.b = samplex(line[0].b, line[1].b, line[2].b, line[3].b,
168                                from_x);
169             result.a = samplex(line[0].a, line[1].a, line[2].a, line[3].a,
170                                from_x);
172             _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
174             if (to->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
175                 /* Clamp the colour channels to range from 0 to result.a to
176                  * make sure, we don't exceed 100% per colour channel with
177                  * images that have premultiplied alpha */
179                 result.a = clamp(result.a);
181                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = clamp_alpha(result.r, result.a);
182                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = clamp_alpha(result.g, result.a);
183                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = clamp_alpha(result.b, result.a);
184                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
185             } else {
186                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = clamp(result.r);
187                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = clamp(result.g);
188                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = clamp(result.b);
189                 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = clamp(result.a);
190             }
191         }
192     }
195 } /* namespace NR */
196 /*
197   Local Variables:
198   mode:c++
199   c-file-style:"stroustrup"
200   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
201   indent-tabs-mode:nil
202   fill-column:99
203   End:
204 */
205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :