2d5abc62c11a9910f4461710222ca93872bb4771
1 #define __NR_PIXBLOCK_SCALER_CPP__
3 /*
4 * Functions for blitting pixblocks using matrix transformation
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 #include "round.h"
17 using Inkscape::round;
18 using std::floor;
20 #include "libnr/nr-pixblock.h"
21 #include "libnr/nr-matrix.h"
23 namespace NR {
25 struct RGBA {
26 int r, g, b, a;
27 };
29 /**
30 * Sanity check function for indexing pixblocks.
31 * Catches reading and writing outside the pixblock area.
32 * When enabled, decreases filter rendering speed massively.
33 */
34 inline void _check_index(NRPixBlock const * const pb, int const location, int const line)
35 {
36 if(false) {
37 int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
38 if (location < 0 || (location + 4) > max_loc)
39 g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
40 }
41 }
43 void transform_nearest(NRPixBlock *to, NRPixBlock *from, Matrix &trans)
44 {
45 if (NR_PIXBLOCK_BPP(from) != 4 || NR_PIXBLOCK_BPP(to) != 4) {
46 g_warning("A non-32-bpp image passed to transform_nearest: scaling aborted.");
47 return;
48 }
50 // Precalculate sizes of source and destination pixblocks
51 int from_width = from->area.x1 - from->area.x0;
52 int from_height = from->area.y1 - from->area.y0;
53 int to_width = to->area.x1 - to->area.x0;
54 int to_height = to->area.y1 - to->area.y0;
56 Matrix itrans = trans.inverse();
58 // Loop through every pixel of destination image, a line at a time
59 for (int to_y = 0 ; to_y < to_height ; to_y++) {
60 for (int to_x = 0 ; to_x < to_width ; to_x++) {
61 RGBA result = {0,0,0,0};
63 int from_x = (int)round(itrans[0] * (to_x + to->area.x0)
64 + itrans[2] * (to_y + to->area.y0)
65 + itrans[4]);
66 from_x -= from->area.x0;
67 int from_y = (int)round(itrans[1] * (to_x + to->area.x0)
68 + itrans[3] * (to_y + to->area.y0)
69 + itrans[5]);
70 from_y -= from->area.y0;
72 if (from_x >= 0 && from_x < from_width
73 && from_y >= 0 && from_y < from_height) {
74 _check_index(from, from_y * from->rs + from_x * 4, __LINE__);
75 result.r = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4];
76 result.g = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 1];
77 result.b = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 2];
78 result.a = NR_PIXBLOCK_PX(from)[from_y * from->rs + from_x * 4 + 3];
79 }
81 _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
82 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = result.r;
83 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = result.g;
84 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = result.b;
85 NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
86 }
87 }
88 }
90 } /* namespace NR */
91 /*
92 Local Variables:
93 mode:c++
94 c-file-style:"stroustrup"
95 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
96 indent-tabs-mode:nil
97 fill-column:99
98 End:
99 */
100 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :