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