1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef GRAPHICS_SCALER_INTERN_H
24 #define GRAPHICS_SCALER_INTERN_H
25 
26 #include "common/scummsys.h"
27 #include "graphics/colormasks.h"
28 
29 
30 /**
31  * Interpolate two 16 bit pixel *pairs* at once with equal weights 1.
32  * In particular, p1 and p2 can contain two pixels each in the upper
33  * and lower halves.
34  */
35 template<typename ColorMask>
interpolate32_1_1(uint32 p1,uint32 p2)36 static inline uint32 interpolate32_1_1(uint32 p1, uint32 p2) {
37 	return (((p1 & ColorMask::kHighBitsMask) >> 1) +
38 	        ((p2 & ColorMask::kHighBitsMask) >> 1) +
39 	         (p1 & p2 & ColorMask::kLowBitsMask));
40 }
41 
42 /**
43  * Interpolate two 16 bit pixel *pairs* at once with weights 3 resp. 1.
44  * In particular, p1 and p2 can contain two pixels/each in the upper
45  * and lower halves.
46  */
47 template<typename ColorMask>
interpolate32_3_1(uint32 p1,uint32 p2)48 static inline uint32 interpolate32_3_1(uint32 p1, uint32 p2) {
49 	register uint32 x = ((p1 & ColorMask::qhighBits) >> 2) * 3 + ((p2 & ColorMask::qhighBits) >> 2);
50 	register uint32 y = ((p1 & ColorMask::qlowBits) * 3 + (p2 & ColorMask::qlowBits)) >> 2;
51 
52 	y &= ColorMask::qlowBits;
53 	return x + y;
54 }
55 
56 /**
57  * Interpolate two 16 bit pixels with weights 1 and 1, i.e., (p1+p2)/2.
58  * See <http://www.slack.net/~ant/info/rgb_mixing.html> for details on how this works.
59  */
60 template<typename ColorMask>
interpolate16_1_1(unsigned p1,unsigned p2)61 static inline unsigned interpolate16_1_1(unsigned p1, unsigned p2) {
62 	const unsigned lowbits = (p1 ^ p2) & ColorMask::kLowBits;
63 	return ((p1 + p2) - lowbits) >> 1;
64 }
65 
66 /**
67  * Interpolate two 16 bit pixels with weights 3 and 1, i.e., (3*p1+p2)/4.
68  */
69 template<typename ColorMask>
interpolate16_3_1(unsigned p1,unsigned p2)70 static inline unsigned interpolate16_3_1(unsigned p1, unsigned p2) {
71 	const unsigned lowbits = (((p1 & ColorMask::kLowBits) << 1) + (p1 & ColorMask::kLow2Bits)
72 		                   + (p2 & ColorMask::kLow2Bits)) & ColorMask::kLow2Bits;
73 	return ((p1*3 + p2) - lowbits) >> 2;
74 }
75 
76 /**
77  * Interpolate two 16 bit pixels with weights 5 and 3 and 1, i.e., (5*p1+3*p2)/8.
78  */
79 template<typename ColorMask>
interpolate16_5_3(unsigned p1,unsigned p2)80 static inline unsigned interpolate16_5_3(unsigned p1, unsigned p2) {
81 	const unsigned lowbits = (((p1 & ColorMask::kLowBits) << 2) + (p1 & ColorMask::kLow3Bits)
82 		                   + ((p2 & ColorMask::kLow2Bits) << 1) + (p2 & ColorMask::kLow3Bits)) & ColorMask::kLow3Bits;
83 	return ((p1*5 + p2*3) - lowbits) >> 3;
84 }
85 
86 /**
87  * Interpolate two 16 bit pixels with weights 7 and 1, i.e., (7*p1+p2)/8.
88  */
89 template<typename ColorMask>
interpolate16_7_1(unsigned p1,unsigned p2)90 static inline unsigned interpolate16_7_1(unsigned p1, unsigned p2) {
91 	const unsigned lowbits = (((p1 & ColorMask::kLowBits) << 2) + ((p1 & ColorMask::kLow2Bits) << 1) + (p1 & ColorMask::kLow3Bits)
92 		                   +  (p2 & ColorMask::kLow3Bits)) & ColorMask::kLow3Bits;
93 	return ((p1*7+p2) - lowbits) >> 3;
94 }
95 
96 /**
97  * Interpolate three 16 bit pixels with weights 2, 1, and 1, i.e., (2*p1+p2+p3)/4.
98  */
99 template<typename ColorMask>
interpolate16_2_1_1(unsigned p1,unsigned p2,unsigned p3)100 static inline unsigned interpolate16_2_1_1(unsigned p1, unsigned p2, unsigned p3) {
101 	p1<<=1;
102 	const unsigned lowbits = ((p1 & (ColorMask::kLowBits << 1))
103 		                   +  (p2 & ColorMask::kLow2Bits)
104 		                   +  (p3 & ColorMask::kLow2Bits)) & ColorMask::kLow2Bits;
105 	return ((p1+p2+p3) - lowbits) >> 2;
106 }
107 
108 /**
109  * Interpolate three 16 bit pixels with weights 5, 2, and 1, i.e., (5*p1+2*p2+p3)/8.
110  */
111 template<typename ColorMask>
interpolate16_5_2_1(unsigned p1,unsigned p2,unsigned p3)112 static inline unsigned interpolate16_5_2_1(unsigned p1, unsigned p2, unsigned p3) {
113 	p2<<=1;
114 	const unsigned lowbits = (((p1 & ColorMask::kLowBits) << 2) + (p1 & ColorMask::kLow3Bits)
115 		                   +  (p2 & (ColorMask::kLow2Bits << 1))
116 		                   +  (p3 & ColorMask::kLow3Bits)) & ColorMask::kLow3Bits;
117 	return ((p1*5 + p2 + p3) - lowbits) >> 3;
118 }
119 
120 /**
121  * Interpolate three 16 bit pixels with weights 6, 1, and 1, i.e., (6*p1+p2+p3)/8.
122  */
123 template<typename ColorMask>
interpolate16_6_1_1(unsigned p1,unsigned p2,unsigned p3)124 static inline unsigned interpolate16_6_1_1(unsigned p1, unsigned p2, unsigned p3) {
125 	const unsigned lowbits = (((((p1 & ColorMask::kLowBits) << 1) + (p1 & ColorMask::kLow2Bits)) << 1)
126 		                   + (p2 & ColorMask::kLow3Bits)
127 		                   + (p3 & ColorMask::kLow3Bits)) & ColorMask::kLow3Bits;
128 	return ((p1*6 + p2 + p3) - lowbits) >> 3;
129 }
130 
131 /**
132  * Interpolate three 16 bit pixels with weights 2, 3, and 3, i.e., (2*p1+3*(p2+p3))/8.
133  */
134 template<typename ColorMask>
interpolate16_2_3_3(unsigned p1,unsigned p2,unsigned p3)135 static inline unsigned interpolate16_2_3_3(unsigned p1, unsigned p2, unsigned p3) {
136 	p1 <<= 1;
137 	const unsigned rb = (p1 & (ColorMask::kRedBlueMask<<1))
138 		              + ((p2 & ColorMask::kRedBlueMask) + (p3 & ColorMask::kRedBlueMask))*3;
139 	const unsigned  g = (p1 & (ColorMask::kGreenMask<<1))
140 		              + ((p2 & ColorMask::kGreenMask) + (p3 & ColorMask::kGreenMask))*3;
141 	return ((rb & (ColorMask::kRedBlueMask<<3)) | (g & (ColorMask::kGreenMask<<3))) >> 3;
142 }
143 
144 /**
145  * Interpolate three 16 bit pixels with weights 2, 7, and 7, i.e., (2*p1+7*(p2+p3))/16.
146  */
147 template<typename ColorMask>
interpolate16_2_7_7(unsigned p1,unsigned p2,unsigned p3)148 static inline unsigned interpolate16_2_7_7(unsigned p1, unsigned p2, unsigned p3) {
149 	p1 <<= 1;
150 	const unsigned rb = (p1 & (ColorMask::kRedBlueMask<<1))
151 		              + ((p2 & ColorMask::kRedBlueMask) + (p3 & ColorMask::kRedBlueMask))*7;
152 	const unsigned  g = (p1 & (ColorMask::kGreenMask<<1))
153 		              + ((p2 & ColorMask::kGreenMask) + (p3 & ColorMask::kGreenMask))*7;
154 	return ((rb & (ColorMask::kRedBlueMask<<4)) | (g & (ColorMask::kGreenMask<<4))) >> 4;
155 }
156 
157 /**
158  * Interpolate three 16 bit pixels with weights 14, 1, and 1, i.e., (14*p1+p2+p3)/16.
159  */
160 template<typename ColorMask>
interpolate16_14_1_1(unsigned p1,unsigned p2,unsigned p3)161 static inline unsigned interpolate16_14_1_1(unsigned p1, unsigned p2, unsigned p3) {
162 	const unsigned rb = (p1&ColorMask::kRedBlueMask)*14
163 	                  + (p2&ColorMask::kRedBlueMask)
164 	                  + (p3&ColorMask::kRedBlueMask);
165 	const unsigned  g = (p1&ColorMask::kGreenMask)*14
166 	                  + (p2&ColorMask::kGreenMask) + (p3&ColorMask::kGreenMask);
167 	return ((rb&(ColorMask::kRedBlueMask<<4)) | (g&(ColorMask::kGreenMask<<4))) >> 4;
168 }
169 
170 /**
171  * Interpolate four 16 bit pixels with weights 1, 1, 1, and 1, i.e., (p1+p2+p3+p4)/4.
172  */
173 template<typename ColorMask>
interpolate16_1_1_1_1(unsigned p1,unsigned p2,unsigned p3,unsigned p4)174 static inline unsigned interpolate16_1_1_1_1(unsigned p1, unsigned p2, unsigned p3, unsigned p4) {
175 	const unsigned lowbits = ((p1 & ColorMask::kLow2Bits)
176 		                   +  (p2 & ColorMask::kLow2Bits)
177 		                   +  (p3 & ColorMask::kLow2Bits)
178 		                   +  (p4 & ColorMask::kLow2Bits)) & ColorMask::kLow2Bits;
179 	return ((p1+p2+p3+p4) - lowbits) >> 2;
180 }
181 
182 /**
183  * Compare two YUV values (encoded 8-8-8) and check if they differ by more than
184  * a certain hard coded threshold. Used by the hq scaler family.
185  */
diffYUV(int yuv1,int yuv2)186 static inline bool diffYUV(int yuv1, int yuv2) {
187 	static const int Ymask = 0x00FF0000;
188 	static const int Umask = 0x0000FF00;
189 	static const int Vmask = 0x000000FF;
190 	static const int trY   = 0x00300000;
191 	static const int trU   = 0x00000700;
192 	static const int trV   = 0x00000006;
193 
194 	int diff;
195 	int mask;
196 
197 	diff = ((yuv1 & Umask) - (yuv2 & Umask));
198 	mask = diff >> 31; // -1 if value < 0, 0 otherwise
199 	diff = (diff ^ mask) - mask; //-1: ~value + 1; 0: value
200 	if (diff > trU) return true;
201 
202 	diff = ((yuv1 & Vmask) - (yuv2 & Vmask));
203 	mask = diff >> 31; // -1 if value < 0, 0 otherwise
204 	diff = (diff ^ mask) - mask; //-1: ~value + 1; 0: value
205 	if (diff > trV) return true;
206 
207 	diff = ((yuv1 & Ymask) - (yuv2 & Ymask));
208 	mask = diff >> 31; // -1 if value < 0, 0 otherwise
209 	diff = (diff ^ mask) - mask; //-1: ~value + 1; 0: value
210 	if (diff > trY) return true;
211 
212 	return false;
213 /*
214 	return
215 	  ( ( ABS((yuv1 & Ymask) - (yuv2 & Ymask)) > trY ) ||
216 	    ( ABS((yuv1 & Umask) - (yuv2 & Umask)) > trU ) ||
217 	    ( ABS((yuv1 & Vmask) - (yuv2 & Vmask)) > trV ) );
218 */
219 }
220 
221 #endif
222