OpenXcom  1.0
Open-source clone of the original X-Com
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 
50 /* Test if there is difference in color */
51 static inline int yuv_diff(uint32_t yuv1, uint32_t yuv2) {
52  return (( abs((int)((yuv1 & Ymask) - (yuv2 & Ymask))) > trY ) ||
53  ( abs((int)((yuv1 & Umask) - (yuv2 & Umask))) > trU ) ||
54  ( abs((int)((yuv1 & Vmask) - (yuv2 & Vmask))) > trV ) );
55 }
56 
57 static inline int Diff(uint32_t c1, uint32_t c2)
58 {
59  return yuv_diff(rgb_to_yuv(c1), rgb_to_yuv(c2));
60 }
61 
62 /* Interpolate functions */
63 static inline uint32_t Interpolate_2(uint32_t c1, int w1, uint32_t c2, int w2, int s)
64 {
65  if (c1 == c2)
66  {
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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