1
#define __NR_PIXBLOCK_SCALER_CPP__
4
* Functions for blitting pixblocks using matrix transformation
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"
24
#include "libnr/nr-blit.h"
25
#include "libnr/nr-pixblock.h"
26
#include <2geom/matrix.h>
38
* Sanity check function for indexing pixblocks.
39
* Catches reading and writing outside the pixblock area.
40
* When enabled, decreases filter rendering speed massively.
42
inline void _check_index(NRPixBlock const * const pb, int const location, int const line)
45
int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
46
if (location < 0 || (location + 4) > max_loc)
47
g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
51
void transform_nearest(NRPixBlock *to, NRPixBlock *from, Geom::Matrix const &trans)
53
if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
54
g_warning("A non-32-bpp image passed to transform_nearest: scaling aborted.");
58
bool free_from_on_exit = false;
59
if (from->mode != to->mode){
60
NRPixBlock *o_from = from;
61
from = new NRPixBlock;
62
nr_pixblock_setup_fast(from, to->mode, o_from->area.x0, o_from->area.y0, o_from->area.x1, o_from->area.y1, false);
63
nr_blit_pixblock_pixblock(from, o_from);
64
free_from_on_exit = true;
67
// Precalculate sizes of source and destination pixblocks
68
int from_width = from->area.x1 - from->area.x0;
69
int from_height = from->area.y1 - from->area.y0;
70
int to_width = to->area.x1 - to->area.x0;
71
int to_height = to->area.y1 - to->area.y0;
73
Geom::Matrix itrans = trans.inverse();
75
// Loop through every pixel of destination image, a line at a time
76
for (int to_y = 0 ; to_y < to_height ; to_y++) {
77
for (int to_x = 0 ; to_x < to_width ; to_x++) {
78
RGBAi result = {0,0,0,0};
80
int from_x = (int)floor(itrans[0] * (to_x + 0.5 + to->area.x0)
81
+ itrans[2] * (to_y + 0.5 + to->area.y0)
83
from_x -= from->area.x0;
84
int from_y = (int)floor(itrans[1] * (to_x + 0.5 + to->area.x0)
85
+ itrans[3] * (to_y + 0.5 + to->area.y0)
87
from_y -= from->area.y0;
89
if (from_x >= 0 && from_x < from_width
90
&& from_y >= 0 && from_y < from_height) {
91
_check_index(from, from_y * from->rs + from_x * 4, __LINE__);
92
result.r = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4];
93
result.g = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 1];
94
result.b = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 2];
95
result.a = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 3];
98
_check_index(to, to_y * to->rs + to_x * 4, __LINE__);
99
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = result.r;
100
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = result.g;
101
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = result.b;
102
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
105
if (free_from_on_exit) {
106
nr_pixblock_release(from);
111
/** Calculates cubically interpolated value of the four given pixel values.
112
* The pixel values should be from four adjacent pixels in source image or
113
* four adjacent interpolated values. len should be the x- or y-coordinate
114
* (depending on interpolation direction) of the center of the target pixel
115
* in source image coordinates.
117
__attribute__ ((const))
118
inline static double sample(double const a, double const b,
119
double const c, double const d,
122
double lena = 1.5 + (len - round(len));
123
double lenb = 0.5 + (len - round(len));
124
double lenc = 0.5 - (len - round(len));
125
double lend = 1.5 - (len - round(len));
126
double const f = -0.5; // corresponds to cubic Hermite spline
128
sum += ((((f * lena) - 5.0 * f) * lena + 8.0 * f) * lena - 4 * f) * a;
129
sum += (((f + 2.0) * lenb - (f + 3.0)) * lenb * lenb + 1.0) * b;
130
sum += (((f + 2.0) * lenc - (f + 3.0)) * lenc * lenc + 1.0) * c;
131
sum += ((((f * lend) - 5.0 * f) * lend + 8.0 * f) * lend - 4 * f) * d;
136
void transform_bicubic(NRPixBlock *to, NRPixBlock *from, Geom::Matrix const &trans)
138
if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
139
g_warning("A non-32-bpp image passed to transform_bicubic: scaling aborted.");
143
bool free_from_on_exit = false;
144
if (from->mode != to->mode){
145
NRPixBlock *o_from = from;
146
from = new NRPixBlock;
147
nr_pixblock_setup_fast(from, to->mode, o_from->area.x0, o_from->area.y0, o_from->area.x1, o_from->area.y1, false);
148
nr_blit_pixblock_pixblock(from, o_from);
149
free_from_on_exit = true;
152
// Precalculate sizes of source and destination pixblocks
153
int from_width = from->area.x1 - from->area.x0;
154
int from_height = from->area.y1 - from->area.y0;
155
int to_width = to->area.x1 - to->area.x0;
156
int to_height = to->area.y1 - to->area.y0;
158
Geom::Matrix itrans = trans.inverse();
160
// Loop through every pixel of destination image, a line at a time
161
for (int to_y = 0 ; to_y < to_height ; to_y++) {
162
for (int to_x = 0 ; to_x < to_width ; to_x++) {
163
double from_x = itrans[0] * (to_x + 0.5 + to->area.x0)
164
+ itrans[2] * (to_y + 0.5 + to->area.y0)
165
+ itrans[4] - from->area.x0;
166
double from_y = itrans[1] * (to_x + 0.5 + to->area.x0)
167
+ itrans[3] * (to_y + 0.5 + to->area.y0)
168
+ itrans[5] - from->area.y0;
170
if (from_x < 0 || from_x >= from_width ||
171
from_y < 0 || from_y >= from_height) {
178
for (int i = 0 ; i < 4 ; i++) {
179
int fy_line = (int)round(from_y) + i - 2;
181
if (fy_line < from_height) {
182
from_line[i] = fy_line * from->rs;
184
from_line[i] = (from_height - 1) * from->rs;
191
for (int i = 0 ; i < 4 ; i++) {
192
int k = (int)round(from_x) + i - 2;
194
if (k >= from_width) k = from_width - 1;
196
_check_index(from, from_line[0] + k, __LINE__);
197
_check_index(from, from_line[1] + k, __LINE__);
198
_check_index(from, from_line[2] + k, __LINE__);
199
_check_index(from, from_line[3] + k, __LINE__);
200
line[i].r = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k],
201
NR_PIXBLOCK_PX(from)[from_line[1] + k],
202
NR_PIXBLOCK_PX(from)[from_line[2] + k],
203
NR_PIXBLOCK_PX(from)[from_line[3] + k],
205
line[i].g = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1],
206
NR_PIXBLOCK_PX(from)[from_line[1] + k + 1],
207
NR_PIXBLOCK_PX(from)[from_line[2] + k + 1],
208
NR_PIXBLOCK_PX(from)[from_line[3] + k + 1],
210
line[i].b = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2],
211
NR_PIXBLOCK_PX(from)[from_line[1] + k + 2],
212
NR_PIXBLOCK_PX(from)[from_line[2] + k + 2],
213
NR_PIXBLOCK_PX(from)[from_line[3] + k + 2],
215
line[i].a = sample(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3],
216
NR_PIXBLOCK_PX(from)[from_line[1] + k + 3],
217
NR_PIXBLOCK_PX(from)[from_line[2] + k + 3],
218
NR_PIXBLOCK_PX(from)[from_line[3] + k + 3],
222
result.r = round(sample(line[0].r, line[1].r, line[2].r, line[3].r,
224
result.g = round(sample(line[0].g, line[1].g, line[2].g, line[3].g,
226
result.b = round(sample(line[0].b, line[1].b, line[2].b, line[3].b,
228
result.a = round(sample(line[0].a, line[1].a, line[2].a, line[3].a,
231
using Inkscape::Filters::clamp;
232
using Inkscape::Filters::clamp_alpha;
233
_check_index(to, to_y * to->rs + to_x * 4, __LINE__);
234
if (to->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
235
/* Make sure, none of the RGB channels exceeds 100% intensity
236
* in premultiplied output */
237
int const alpha = clamp((int)result.a);
238
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] =
239
clamp_alpha((int)result.r, alpha);
240
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] =
241
clamp_alpha((int)result.g, alpha);
242
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] =
243
clamp_alpha((int)result.b, alpha);
244
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = alpha;
246
/* Clamp the output to unsigned char range */
247
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4]
248
= clamp((int)result.r);
249
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1]
250
= clamp((int)result.g);
251
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2]
252
= clamp((int)result.b);
253
NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3]
254
= clamp((int)result.a);
258
if (free_from_on_exit) {
259
nr_pixblock_release(from);
268
c-file-style:"stroustrup"
269
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
274
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :