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 if (to->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
185 /* Clamp the colour channels to range from 0 to result.a to
186 * make sure, we don't exceed 100% per colour channel with
187 * images that have premultiplied alpha */
189 result.a = clamp(result.a);
191 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = clamp_alpha(result.r, result.a);
192 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = clamp_alpha(result.g, result.a);
193 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = clamp_alpha(result.b, result.a);
194 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
195 } else {
196 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = clamp(result.r);
197 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = clamp(result.g);
198 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = clamp(result.b);
199 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = clamp(result.a);
200 }
201 }
202 }
203 if (free_from_on_exit) {
204 nr_pixblock_release(from);
205 delete from;
206 }
208 }
210 void scale_bicubic_alpha(NRPixBlock *to, NRPixBlock *from)
211 {
212 if (NR_PIXBLOCK_BPP(from) != 1 || NR_PIXBLOCK_BPP(to) != 1) {
213 g_warning("A non-8-bpp image passed to scale_bicubic_alpha: scaling aborted.");
214 return;
215 }
217 // Precalculate sizes of source and destination pixblocks
218 int from_width = from->area.x1 - from->area.x0;
219 int from_height = from->area.y1 - from->area.y0;
220 int to_width = to->area.x1 - to->area.x0;
221 int to_height = to->area.y1 - to->area.y0;
223 // from_step: when advancing one pixel in destination image,
224 // how much we should advance in source image
225 double from_stepx = (double)from_width / (double)to_width;
226 double from_stepy = (double)from_height / (double)to_height;
228 // Loop through every pixel of destination image, a line at a time
229 for (int to_y = 0 ; to_y < to_height ; to_y++) {
230 double from_y = to_y * from_stepy + from_stepy / 2;
231 // Pre-calculate beginning of the four horizontal lines, from
232 // which we should read
233 int from_line[4];
234 for (int i = 0 ; i < 4 ; i++) {
235 if ((int)floor(from_y) + i - 1 >= 0) {
236 if ((int)floor(from_y) + i - 1 < from_height) {
237 from_line[i] = ((int)floor(from_y) + i - 1) * from->rs;
238 } else {
239 from_line[i] = (from_height - 1) * from->rs;
240 }
241 } else {
242 from_line[i] = 0;
243 }
244 }
245 // Loop through this horizontal line in destination image
246 // For every pixel, calculate the color of pixel with
247 // bicubic interpolation and set the pixel value in destination image
248 for (int to_x = 0 ; to_x < to_width ; to_x++) {
249 double from_x = to_x * from_stepx + from_stepx / 2;
250 int line[4];
251 for (int i = 0 ; i < 4 ; i++) {
252 int k = (int)floor(from_x) + i - 1;
253 if (k < 0) k = 0;
254 if (k >= from_width) k = from_width - 1;
255 _check_index(from, from_line[0] + k, __LINE__);
256 _check_index(from, from_line[1] + k, __LINE__);
257 _check_index(from, from_line[2] + k, __LINE__);
258 _check_index(from, from_line[3] + k, __LINE__);
259 line[i] = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k],
260 NR_PIXBLOCK_PX(from)[from_line[1] + k],
261 NR_PIXBLOCK_PX(from)[from_line[2] + k],
262 NR_PIXBLOCK_PX(from)[from_line[3] + k],
263 from_y);
264 }
265 int result;
266 result = samplex(line[0], line[1], line[2], line[3],
267 from_x);
269 _check_index(to, to_y * to->rs + to_x, __LINE__);
271 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x] = clamp(result);
272 }
273 }
274 }
276 void scale_bicubic(NRPixBlock *to, NRPixBlock *from)
277 {
278 if (NR_PIXBLOCK_BPP(to) == 4 && NR_PIXBLOCK_BPP(from) == 4) {
279 scale_bicubic_rgba(to, from);
280 } else if (NR_PIXBLOCK_BPP(to) == 1 && NR_PIXBLOCK_BPP(from) == 1) {
281 scale_bicubic_alpha(to, from);
282 } else {
283 g_warning("NR::scale_bicubic: unsupported bitdepths for scaling: to %d, from %d", NR_PIXBLOCK_BPP(to), NR_PIXBLOCK_BPP(from));
284 }
285 }
287 } /* namespace NR */
288 /*
289 Local Variables:
290 mode:c++
291 c-file-style:"stroustrup"
292 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
293 indent-tabs-mode:nil
294 fill-column:99
295 End:
296 */
297 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :