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