1
#define __NR_PIXBLOCK_SCALER_CPP__
4
* Functions for blitting pixblocks using scaling
7
* Niko Kiirala <niko@kiirala.com>
9
* Copyright (C) 2006,2009 Niko Kiirala
11
* Released under GNU GPL, read the file 'COPYING' for more information
16
#if defined (SOLARIS) && (SOLARIS == 8)
18
using Inkscape::round;
22
#include "display/nr-filter-utils.h"
23
#include "libnr/nr-pixblock.h"
24
#include "libnr/nr-blit.h"
25
#include <2geom/forward.h>
33
/** Calculates cubically interpolated value of the four given pixel values.
34
* The pixel values should be from four adjacent pixels in source image or
35
* four adjacent interpolated values. len should be the x- or y-coordinate
36
* (depending on interpolation direction) of the center of the target pixel
37
* in source image coordinates.
39
__attribute__ ((const))
40
inline static double sample(double const a, double const b,
41
double const c, double const d,
44
double lena = 1.5 + (len - round(len));
45
double lenb = 0.5 + (len - round(len));
46
double lenc = 0.5 - (len - round(len));
47
double lend = 1.5 - (len - round(len));
48
double const f = -0.5; // corresponds to cubic Hermite spline
50
sum += ((((f * lena) - 5.0 * f) * lena + 8.0 * f) * lena - 4 * f) * a;
51
sum += (((f + 2.0) * lenb - (f + 3.0)) * lenb * lenb + 1.0) * b;
52
sum += (((f + 2.0) * lenc - (f + 3.0)) * lenc * lenc + 1.0) * c;
53
sum += ((((f * lend) - 5.0 * f) * lend + 8.0 * f) * lend - 4 * f) * d;
59
* Sanity check function for indexing pixblocks.
60
* Catches reading and writing outside the pixblock area.
61
* When enabled, decreases filter rendering speed massively.
63
inline static void _check_index(NRPixBlock const * const pb, int const location, int const line)
66
int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
67
if (location < 0 || (location + 4) > max_loc)
68
g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
72
static void scale_bicubic_rgba(NRPixBlock *to, NRPixBlock *from,
73
Geom::Matrix const &trans)
75
if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
76
g_warning("A non-32-bpp image passed to scale_bicubic_rgba: scaling aborted.");
80
bool free_from_on_exit = false;
81
if (from->mode != to->mode){
82
NRPixBlock *o_from = from;
83
from = new NRPixBlock;
84
nr_pixblock_setup_fast(from, to->mode, o_from->area.x0, o_from->area.y0, o_from->area.x1, o_from->area.y1, false);
85
nr_blit_pixblock_pixblock(from, o_from);
86
free_from_on_exit = true;
89
// Precalculate sizes of source and destination pixblocks
90
int from_width = from->area.x1 - from->area.x0;
91
int from_height = from->area.y1 - from->area.y0;
92
int to_width = to->area.x1 - to->area.x0;
93
int to_height = to->area.y1 - to->area.y0;
95
// from_step: when advancing one pixel in destination image,
96
// how much we should advance in source image
97
double from_stepx = 1.0 / trans[0];
98
double from_stepy = 1.0 / trans[3];
99
double from_diffx = from_stepx * (-trans[4]);
100
double from_diffy = from_stepy * (-trans[5]);
101
from_diffx = (to->area.x0 * from_stepx + from_diffx) - from->area.x0;
102
from_diffy = (to->area.y0 * from_stepy + from_diffy) - from->area.y0;
104
// Loop through every pixel of destination image, a line at a time
105
for (int to_y = 0 ; to_y < to_height ; to_y++) {
106
double from_y = (to_y + 0.5) * from_stepy + from_diffy;
107
// Pre-calculate beginning of the four horizontal lines, from
108
// which we should read
110
for (int i = 0 ; i < 4 ; i++) {
111
int fy_line = (int)round(from_y) + i - 2;
113
if (fy_line < from_height) {
114
from_line[i] = fy_line * from->rs;
116
from_line[i] = (from_height - 1) * from->rs;
122
// Loop through this horizontal line in destination image
123
// For every pixel, calculate the color of pixel with
124
// bicubic interpolation and set the pixel value in destination image
125
for (int to_x = 0 ; to_x < to_width ; to_x++) {
126
double from_x = (to_x + 0.5) * from_stepx + from_diffx;
128
for (int i = 0 ; i < 4 ; i++) {
129
int k = (int)round(from_x) + i - 2;
131
if (k >= from_width) k = from_width - 1;
133
_check_index(from, from_line[0] + k, __LINE__);
134
_check_index(from, from_line[1] + k, __LINE__);
135
_check_index(from, from_line[2] + k, __LINE__);
136
_check_index(from, from_line[3] + k, __LINE__);
137
line[i].r = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k],
138
NR_PIXBLOCK_PX(from)[from_line[1] + k],
139
NR_PIXBLOCK_PX(from)[from_line[2] + k],
140
NR_PIXBLOCK_PX(from)[from_line[3] + k],
142
line[i].g = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1],
143
NR_PIXBLOCK_PX(from)[from_line[1] + k + 1],
144
NR_PIXBLOCK_PX(from)[from_line[2] + k + 1],
145
NR_PIXBLOCK_PX(from)[from_line[3] + k + 1],
147
line[i].b = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2],
148
NR_PIXBLOCK_PX(from)[from_line[1] + k + 2],
149
NR_PIXBLOCK_PX(from)[from_line[2] + k + 2],
150
NR_PIXBLOCK_PX(from)[from_line[3] + k + 2],
152
line[i].a = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3],
153
NR_PIXBLOCK_PX(from)[from_line[1] + k + 3],
154
NR_PIXBLOCK_PX(from)[from_line[2] + k + 3],
155
NR_PIXBLOCK_PX(from)[from_line[3] + k + 3],
159
result.r = round(sample(line[0].r, line[1].r, line[2].r, line[3].r,
161
result.g = round(sample(line[0].g, line[1].g, line[2].g, line[3].g,
163
result.b = round(sample(line[0].b, line[1].b, line[2].b, line[3].b,
165
result.a = round(sample(line[0].a, line[1].a, line[2].a, line[3].a,
168
_check_index(to, to_y * to->rs + to_x * 4, __LINE__);
170
using Inkscape::Filters::clamp;
171
using Inkscape::Filters::clamp_alpha;
172
if (to->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
173
/* Clamp the colour channels to range from 0 to result.a to
174
* make sure, we don't exceed 100% per colour channel with
175
* images that have premultiplied alpha */
177
int const alpha = clamp((int)result.a);
179
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4]
180
= clamp_alpha((int)result.r, alpha);
181
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1]
182
= clamp_alpha((int)result.g, alpha);
183
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2]
184
= clamp_alpha((int)result.b, alpha);
185
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = alpha;
187
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4]
188
= clamp((int)result.r);
189
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1]
190
= clamp((int)result.g);
191
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2]
192
= clamp((int)result.b);
193
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3]
194
= clamp((int)result.a);
198
if (free_from_on_exit) {
199
nr_pixblock_release(from);
205
void scale_bicubic_alpha(NRPixBlock *to, NRPixBlock *from,
206
Geom::Matrix const &trans)
208
if (NR_PIXBLOCK_BPP(from) != 1 || NR_PIXBLOCK_BPP(to) != 1) {
209
g_warning("A non-8-bpp image passed to scale_bicubic_alpha: scaling aborted.");
213
// Precalculate sizes of source and destination pixblocks
214
int from_width = from->area.x1 - from->area.x0;
215
int from_height = from->area.y1 - from->area.y0;
216
int to_width = to->area.x1 - to->area.x0;
217
int to_height = to->area.y1 - to->area.y0;
219
// from_step: when advancing one pixel in destination image,
220
// how much we should advance in source image
221
double from_stepx = 1.0 / trans[0];
222
double from_stepy = 1.0 / trans[3];
223
double from_diffx = from_stepx * (-trans[4]);
224
double from_diffy = from_stepy * (-trans[5]);
225
from_diffx = (to->area.x0 * from_stepx + from_diffx) - from->area.x0;
226
from_diffy = (to->area.y0 * from_stepy + from_diffy) - from->area.y0;
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 + 0.5) * from_stepy - from_diffy;
231
// Pre-calculate beginning of the four horizontal lines, from
232
// which we should read
234
for (int i = 0 ; i < 4 ; i++) {
235
int fy_line = (int)round(from_y) + i - 2;
237
if (fy_line < from_height) {
238
from_line[i] = fy_line * from->rs;
240
from_line[i] = (from_height - 1) * from->rs;
246
// Loop through this horizontal line in destination image
247
// For every pixel, calculate the color of pixel with
248
// bicubic interpolation and set the pixel value in destination image
249
for (int to_x = 0 ; to_x < to_width ; to_x++) {
250
double from_x = (to_x + 0.5) * from_stepx - from_diffx;
252
for (int i = 0 ; i < 4 ; i++) {
253
int k = (int)round(from_x) + i - 2;
255
if (k >= from_width) k = from_width - 1;
256
_check_index(from, from_line[0] + k, __LINE__);
257
_check_index(from, from_line[1] + k, __LINE__);
258
_check_index(from, from_line[2] + k, __LINE__);
259
_check_index(from, from_line[3] + k, __LINE__);
260
line[i] = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k],
261
NR_PIXBLOCK_PX(from)[from_line[1] + k],
262
NR_PIXBLOCK_PX(from)[from_line[2] + k],
263
NR_PIXBLOCK_PX(from)[from_line[3] + k],
267
result = (int)round(sample(line[0], line[1], line[2], line[3],
270
_check_index(to, to_y * to->rs + to_x, __LINE__);
272
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x]
273
= Inkscape::Filters::clamp(result);
278
void scale_bicubic(NRPixBlock *to, NRPixBlock *from, Geom::Matrix const &trans)
280
if (NR_PIXBLOCK_BPP(to) == 4 && NR_PIXBLOCK_BPP(from) == 4) {
281
scale_bicubic_rgba(to, from, trans);
282
} else if (NR_PIXBLOCK_BPP(to) == 1 && NR_PIXBLOCK_BPP(from) == 1) {
283
scale_bicubic_alpha(to, from, trans);
285
g_warning("NR::scale_bicubic: unsupported bitdepths for scaling: to %d, from %d", NR_PIXBLOCK_BPP(to), NR_PIXBLOCK_BPP(from));
293
c-file-style:"stroustrup"
294
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
299
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :