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