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