Code

3b8461383d4eacf1ff4957e3ca2e59b44028175a
[inkscape.git] / src / display / pixblock-scaler.cpp
1 #define __NR_PIXBLOCK_SCALER_CPP__
3 #include <glib.h>
4 #include <cmath>
5 using std::floor;
7 #include "libnr/nr-pixblock.h"
9 namespace NR {
11 struct RGBA {
12     int r, g, b, a;
13 };
15 inline int sampley(unsigned const char a, unsigned const char b, unsigned const char c, unsigned const char d, const double len) {
16     double lenf = len - floor(len);
17     int sum = 0;
18     sum += (int)((((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0)
19                  * lenf * 256 * a);
20     sum += (int)((((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0)
21                  * 256 * b);
22     sum += (int)(((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0)
23                   * (1 - lenf) + 1.0) * 256 * c);
24     sum += (int)((((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf)
25                   - 7.0 / 15.0) * (1 - lenf) * 256 * d);
26     return sum;
27 }
29 inline unsigned char samplex(const int a, const int b, const int c, const int d, const double len) {
30     double lenf = len - floor(len);
31     int sum = 0;
32     sum += (int)(a * (((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0) * lenf);
33     sum += (int)(b * (((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0));
34     sum += (int)(c * ((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0) * (1 - lenf) + 1.0));
35     sum += (int)(d * (((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf) - 7.0 / 15.0) * (1 - lenf));
36     if (sum < 0) sum = 0;
37     if (sum > 255*256) sum = 255 * 256;
38     return (unsigned char)(sum / 256);
39 }
41 /**
42  * Sanity check function for indexing pixblocks.
43  * Catches reading and writing outside the pixblock area.
44  * When enabled, decreases filter rendering speed massively.
45  */
46 inline void _check_index(NRPixBlock const * const pb, int const location, int const line)
47 {
48     if(true) {
49         int max_loc = pb->rs * (pb->area.y1 - pb->area.y0);
50         if (location < 0 || (location + 4) >= max_loc)
51             g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line);
52     }
53 }
55 void scale_bicubic(NRPixBlock *to, NRPixBlock *from)
56 {
57     int from_width = from->area.x1 - from->area.x0;
58     int from_height = from->area.y1 - from->area.y0;
59     int to_width = to->area.x1 - to->area.x0;
60     int to_height = to->area.y1 - to->area.y0;
62     double from_stepx = (double)from_width / (double)to_width;
63     double from_stepy = (double)from_height / (double)to_height;
65     for (int to_y = 0 ; to_y < to_height ; to_y++) {
66         double from_y = to_y * from_stepy + from_stepy / 2;
67         int from_line[4];
68         for (int i = 0 ; i < 4 ; i++) {
69             if ((int)floor(from_y) + i - 1 >= 0) {
70                 if ((int)floor(from_y) + i - 1 < from_height) {
71                     from_line[i] = ((int)floor(from_y) + i - 1) * from->rs;
72                 } else {
73                     from_line[i] = (from_height - 1) * from->rs;
74                 }
75             } else {
76                 from_line[i] = 0;
77             }
78         }
79         for (int to_x = 0 ; to_x < to_width ; to_x++) {
80             double from_x = to_x * from_stepx + from_stepx / 2;
81             RGBA line[4];
82             for (int i = 0 ; i < 4 ; i++) {
83                 int k = (int)floor(from_x) + i - 1;
84                 if (k < 0) k = 0;
85                 if (k >= from_width) k = from_width - 1;
86                 k *= 4;
87                 _check_index(from, from_line[0] + k, __LINE__);
88                 _check_index(from, from_line[1] + k, __LINE__);
89                 _check_index(from, from_line[2] + k, __LINE__);
90                 _check_index(from, from_line[3] + k, __LINE__);
91                 line[i].r = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k],
92                                     NR_PIXBLOCK_PX(from)[from_line[1] + k],
93                                     NR_PIXBLOCK_PX(from)[from_line[2] + k],
94                                     NR_PIXBLOCK_PX(from)[from_line[3] + k],
95                                     from_y);
96                 line[i].g = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1],
97                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 1],
98                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 1],
99                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 1],
100                                     from_y);
101                 line[i].b = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2],
102                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 2],
103                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 2],
104                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 2],
105                                     from_y);
106                 line[i].a = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3],
107                                     NR_PIXBLOCK_PX(from)[from_line[1] + k + 3],
108                                     NR_PIXBLOCK_PX(from)[from_line[2] + k + 3],
109                                     NR_PIXBLOCK_PX(from)[from_line[3] + k + 3],
110                                     from_y);
111             }
112             RGBA result;
113             result.r = samplex(line[0].r, line[1].r, line[2].r, line[3].r,
114                                from_x);
115             result.g = samplex(line[0].g, line[1].g, line[2].g, line[3].g,
116                                from_x);
117             result.b = samplex(line[0].b, line[1].b, line[2].b, line[3].b,
118                                from_x);
119             result.a = samplex(line[0].a, line[1].a, line[2].a, line[3].a,
120                                from_x);
122             _check_index(to, to_y * to->rs + to_x * 4, __LINE__);
123             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = result.r;
124             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = result.g;
125             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = result.b;
126             NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a;
127         }
128     }
131 } /* namespace NR */
132 /*
133   Local Variables:
134   mode:c++
135   c-file-style:"stroustrup"
136   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
137   indent-tabs-mode:nil
138   fill-column:99
139   End:
140 */
141 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :