nomlib
Loading...
Searching...
No Matches
hqx.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 hqx Algorithm
291. Copyright (C) 2003 Maxim Stepin ( maxst@hiend3d.com )
302. Copyright (C) 2010 Cameron Zemek ( grom@zeminvaders.net)
31
32******************************************************************************/
33#ifndef NOMLIB_HQX_HPP
34#define NOMLIB_HQX_HPP
35
54
55#include "export.hpp"
56#include "nomlib/config.hpp"
57#include "nomlib/math/math_helpers.hpp"
58
59#define MASK_2 0x0000FF00
60#define MASK_13 0x00FF00FF
61#define MASK_RGB 0x00FFFFFF
62#define MASK_ALPHA 0xFF000000
63
64#define Ymask 0x00FF0000
65#define Umask 0x0000FF00
66#define Vmask 0x000000FF
67#define trY 0x00300000
68#define trU 0x00000700
69#define trV 0x00000006
70
71namespace nom {
72 namespace priv {
73
74/* RGB to YUV lookup table */
75extern uint32 RGBtoYUV[16777216];
76
77NOM_NO_EXPORT
78static inline uint32 rgb_to_yuv(uint32 c)
79{
80 // Mask against MASK_RGB to discard the alpha channel
81 return RGBtoYUV[MASK_RGB & c];
82}
83
91/* Test if there is difference in color */
92NOM_NO_EXPORT
93static inline int yuv_diff(int yuv1, int yuv2) {
94 return (( abs((yuv1 & Ymask) - (yuv2 & Ymask)) > trY ) ||
95 ( abs((yuv1 & Umask) - (yuv2 & Umask)) > trU ) ||
96 ( abs((yuv1 & Vmask) - (yuv2 & Vmask)) > trV ) );
97}
98
99NOM_NO_EXPORT
100static inline int32 Diff(uint32 c1, uint32 c2)
101{
102 return yuv_diff(rgb_to_yuv(c1), rgb_to_yuv(c2));
103}
104
105/* Interpolate functions */
106NOM_NO_EXPORT
107static inline uint32 Interpolate_2(uint32 c1, int w1, uint32 c2, int32 w2, int32 s)
108{
109 if (c1 == c2) {
110 return c1;
111 }
112 return
113 (((((c1 & MASK_ALPHA) >> 24) * w1 + ((c2 & MASK_ALPHA) >> 24) * w2) << (24-s)) & MASK_ALPHA) +
114 ((((c1 & MASK_2) * w1 + (c2 & MASK_2) * w2) >> s) & MASK_2) +
115 ((((c1 & MASK_13) * w1 + (c2 & MASK_13) * w2) >> s) & MASK_13);
116}
117
118NOM_NO_EXPORT
119static inline uint32 Interpolate_3(uint32 c1, int32 w1, uint32 c2, int32 w2, uint32 c3, int32 w3, int32 s)
120{
121 return
122 (((((c1 & MASK_ALPHA) >> 24) * w1 + ((c2 & MASK_ALPHA) >> 24) * w2 + ((c3 & MASK_ALPHA) >> 24) * w3) << (24-s)) & MASK_ALPHA) +
123 ((((c1 & MASK_2) * w1 + (c2 & MASK_2) * w2 + (c3 & MASK_2) * w3) >> s) & MASK_2) +
124 ((((c1 & MASK_13) * w1 + (c2 & MASK_13) * w2 + (c3 & MASK_13) * w3) >> s) & MASK_13);
125}
126
127NOM_NO_EXPORT
128static inline uint32 Interp1(uint32 c1, uint32 c2)
129{
130 //(c1*3+c2) >> 2;
131 return Interpolate_2(c1, 3, c2, 1, 2);
132}
133
134NOM_NO_EXPORT
135static inline uint32 Interp2(uint32 c1, uint32 c2, uint32 c3)
136{
137 //(c1*2+c2+c3) >> 2;
138 return Interpolate_3(c1, 2, c2, 1, c3, 1, 2);
139}
140
141NOM_NO_EXPORT
142static inline uint32 Interp3(uint32 c1, uint32 c2)
143{
144 //(c1*7+c2)/8;
145 return Interpolate_2(c1, 7, c2, 1, 3);
146}
147
148NOM_NO_EXPORT
149static inline uint32 Interp4(uint32 c1, uint32 c2, uint32 c3)
150{
151 //(c1*2+(c2+c3)*7)/16;
152 return Interpolate_3(c1, 2, c2, 7, c3, 7, 4);
153}
154
155NOM_NO_EXPORT
156static inline uint32 Interp5(uint32 c1, uint32 c2)
157{
158 //(c1+c2) >> 1;
159 return Interpolate_2(c1, 1, c2, 1, 1);
160}
161
162NOM_NO_EXPORT
163static inline uint32 Interp6(uint32 c1, uint32 c2, uint32 c3)
164{
165 //(c1*5+c2*2+c3)/8;
166 return Interpolate_3(c1, 5, c2, 2, c3, 1, 3);
167}
168
169NOM_NO_EXPORT
170static inline uint32 Interp7(uint32 c1, uint32 c2, uint32 c3)
171{
172 //(c1*6+c2+c3)/8;
173 return Interpolate_3(c1, 6, c2, 1, c3, 1, 3);
174}
175
176NOM_NO_EXPORT
177static inline uint32 Interp8(uint32 c1, uint32 c2)
178{
179 //(c1*5+c2*3)/8;
180 return Interpolate_2(c1, 5, c2, 3, 3);
181}
182
183NOM_NO_EXPORT
184static inline uint32 Interp9(uint32 c1, uint32 c2, uint32 c3)
185{
186 //(c1*2+(c2+c3)*3)/8;
187 return Interpolate_3(c1, 2, c2, 3, c3, 3, 3);
188}
189
190NOM_NO_EXPORT
191static inline uint32 Interp10(uint32 c1, uint32 c2, uint32 c3)
192{
193 //(c1*14+c2+c3)/16;
194 return Interpolate_3(c1, 14, c2, 1, c3, 1, 4);
195}
196
198NOM_NO_EXPORT
199void hq2x_32_rb( uint32* sp, uint32 srb, uint32* dp, uint32 drb, int32 Xres, int32 Yres );
200
202NOM_NO_EXPORT
203void hq3x_32_rb( uint32* sp, uint32 srb, uint32* dp, uint32 drb, int32 Xres, int32 Yres );
204
206NOM_NO_EXPORT
207void hq4x_32_rb( uint32* sp, uint32 srb, uint32* dp, uint32 drb, int32 Xres, int32 Yres );
208
210NOM_EXPORT void hqxInit ( void );
211
215NOM_EXPORT
216void hq2x_32 ( uint32* src, uint32* dest, int32 width, int32 height );
217
221NOM_EXPORT
222void hq3x_32 ( uint32* src, uint32* dest, int32 width, int32 height );
223
227NOM_EXPORT
228void hq4x_32 ( uint32* src, uint32* dest, int32 width, int32 height );
229
230
231 } // namespace priv
232} // namespace nom
233
234#endif // include guard defined