Code

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