1 /*
2  * Copyright (C) 2014 Peter Olsson
3  *
4  * Copyright (C) 2003 Maxim Stepin ( maxst@hiend3d.com )
5  *
6  * Copyright (C) 2010 Cameron Zemek ( grom@zeminvaders.net)
7  * Copyright (C) 2011 Francois Gannaz <mytskine@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #ifndef __HQX_COMMON_H_
25 #define __HQX_COMMON_H_
26 
27 #include <stdlib.h>
28 #include <stdint.h>
29 
30 #define MASK_2     0x0000FF00
31 #define MASK_13    0x00FF00FF
32 #define MASK_RGB   0x00FFFFFF
33 #define MASK_ALPHA 0xFF000000
34 
35 #define Ymask 0x00FF0000
36 #define Umask 0x0000FF00
37 #define Vmask 0x000000FF
38 #define trY   0x00300000
39 #define trU   0x00000700
40 #define trV   0x00000006
41 
42 /* RGB to YUV lookup table (now a pointer) */
43 extern uint32_t* RGBtoYUV;
44 
rgb_to_yuv(uint32_t c)45 static inline uint32_t rgb_to_yuv(uint32_t c)
46 {
47     // Mask against MASK_RGB to discard the alpha channel
48     return RGBtoYUV[MASK_RGB & c];
49 }
50 
51 /* Test if there is difference in color */
yuv_diff(uint32_t yuv1,uint32_t yuv2)52 static inline int yuv_diff(uint32_t yuv1, uint32_t yuv2) {
53     return (( abs(static_cast<int>(yuv1 & Ymask) - static_cast<int>(yuv2 & Ymask)) > trY ) ||
54             ( abs(static_cast<int>(yuv1 & Umask) - static_cast<int>(yuv2 & Umask)) > trU ) ||
55             ( abs(static_cast<int>(yuv1 & Vmask) - static_cast<int>(yuv2 & Vmask)) > trV ) );
56 }
57 
Diff(uint32_t c1,uint32_t c2)58 static inline int Diff(uint32_t c1, uint32_t c2)
59 {
60     return yuv_diff(rgb_to_yuv(c1), rgb_to_yuv(c2));
61 }
62 
63 /* Interpolate functions */
Interpolate_2(uint32_t c1,int w1,uint32_t c2,int w2,int s)64 static inline uint32_t Interpolate_2(uint32_t c1, int w1, uint32_t c2, int w2, int s)
65 {
66     if (c1 == c2) {
67         return c1;
68     }
69     return
70         (((((c1 & MASK_ALPHA) >> 24) * w1 + ((c2 & MASK_ALPHA) >> 24) * w2) << (24-s)) & MASK_ALPHA) +
71         ((((c1 & MASK_2) * w1 + (c2 & MASK_2) * w2) >> s) & MASK_2)	+
72         ((((c1 & MASK_13) * w1 + (c2 & MASK_13) * w2) >> s) & MASK_13);
73 }
74 
Interpolate_3(uint32_t c1,int w1,uint32_t c2,int w2,uint32_t c3,int w3,int s)75 static inline uint32_t Interpolate_3(uint32_t c1, int w1, uint32_t c2, int w2, uint32_t c3, int w3, int s)
76 {
77     return
78         (((((c1 & MASK_ALPHA) >> 24) * w1 + ((c2 & MASK_ALPHA) >> 24) * w2 + ((c3 & MASK_ALPHA) >> 24) * w3) << (24-s)) & MASK_ALPHA) +
79         ((((c1 & MASK_2) * w1 + (c2 & MASK_2) * w2 + (c3 & MASK_2) * w3) >> s) & MASK_2) +
80         ((((c1 & MASK_13) * w1 + (c2 & MASK_13) * w2 + (c3 & MASK_13) * w3) >> s) & MASK_13);
81 }
82 
Interp1(uint32_t c1,uint32_t c2)83 static inline uint32_t Interp1(uint32_t c1, uint32_t c2)
84 {
85     //(c1*3+c2) >> 2;
86     return Interpolate_2(c1, 3, c2, 1, 2);
87 }
88 
Interp2(uint32_t c1,uint32_t c2,uint32_t c3)89 static inline uint32_t Interp2(uint32_t c1, uint32_t c2, uint32_t c3)
90 {
91     //(c1*2+c2+c3) >> 2;
92     return Interpolate_3(c1, 2, c2, 1, c3, 1, 2);
93 }
94 
Interp3(uint32_t c1,uint32_t c2)95 static inline uint32_t Interp3(uint32_t c1, uint32_t c2)
96 {
97     //(c1*7+c2)/8;
98     return Interpolate_2(c1, 7, c2, 1, 3);
99 }
100 
Interp4(uint32_t c1,uint32_t c2,uint32_t c3)101 static inline uint32_t Interp4(uint32_t c1, uint32_t c2, uint32_t c3)
102 {
103     //(c1*2+(c2+c3)*7)/16;
104     return Interpolate_3(c1, 2, c2, 7, c3, 7, 4);
105 }
106 
Interp5(uint32_t c1,uint32_t c2)107 static inline uint32_t Interp5(uint32_t c1, uint32_t c2)
108 {
109     //(c1+c2) >> 1;
110     return Interpolate_2(c1, 1, c2, 1, 1);
111 }
112 
Interp6(uint32_t c1,uint32_t c2,uint32_t c3)113 static inline uint32_t Interp6(uint32_t c1, uint32_t c2, uint32_t c3)
114 {
115     //(c1*5+c2*2+c3)/8;
116     return Interpolate_3(c1, 5, c2, 2, c3, 1, 3);
117 }
118 
Interp7(uint32_t c1,uint32_t c2,uint32_t c3)119 static inline uint32_t Interp7(uint32_t c1, uint32_t c2, uint32_t c3)
120 {
121     //(c1*6+c2+c3)/8;
122     return Interpolate_3(c1, 6, c2, 1, c3, 1, 3);
123 }
124 
Interp8(uint32_t c1,uint32_t c2)125 static inline uint32_t Interp8(uint32_t c1, uint32_t c2)
126 {
127     //(c1*5+c2*3)/8;
128     return Interpolate_2(c1, 5, c2, 3, 3);
129 }
130 
Interp9(uint32_t c1,uint32_t c2,uint32_t c3)131 static inline uint32_t Interp9(uint32_t c1, uint32_t c2, uint32_t c3)
132 {
133     //(c1*2+(c2+c3)*3)/8;
134     return Interpolate_3(c1, 2, c2, 3, c3, 3, 3);
135 }
136 
Interp10(uint32_t c1,uint32_t c2,uint32_t c3)137 static inline uint32_t Interp10(uint32_t c1, uint32_t c2, uint32_t c3)
138 {
139     //(c1*14+c2+c3)/16;
140     return Interpolate_3(c1, 14, c2, 1, c3, 1, 4);
141 }
142 
143 #endif
144