Code

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