ScummVM API documentation
colormasks.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef GRAPHICS_COLORMASKS_H
23 #define GRAPHICS_COLORMASKS_H
24 
25 #include "common/scummsys.h"
26 
27 namespace Graphics {
28 
29 template<int bitFormat>
30 struct ColorMasks {
31 };
32 
33 /*
34 The ColorMasks template can be used to map bit format values
35 (like 555, 565, 1555, 4444) to corresponding bit masks and shift values.
36 Currently this is only meant for
37 
38 The meaning of these is masks is the following:
39  kBytesPerPixel
40  -> how many bytes per pixel for that format
41 
42  kRedMask, kGreenMask, kBlueMask
43  -> bitmask, and this with the color to select only the bits of the corresponding color
44 
45  The k*Bits and k*Shift values can be used to extract R,G,B. I.e. to get
46  the red color component of a pixel, as a 8-bit value, you would write
47 
48  R = ((color & kRedMask) >> kRedShift) << (8-kRedBits)
49 
50  Actually, instead of the simple left shift, one might want to use somewhat
51  more sophisticated code (which fills up the least significant bits with
52  appropriate data).
53 
54 
55  The kHighBitsMask / kLowBitsMask / qhighBits / qlowBits are special values that are
56  used in the super-optimized interpolation functions in scaler/intern.h
57  and scaler/aspect.cpp. Currently they are only available in 555 and 565 mode.
58  To be specific: They pack the masks for two 16 bit pixels at once. The pixels
59  are split into "high" and "low" bits, which are then separately interpolated
60  and finally re-composed. That way, 2x2 pixels or even 4x2 pixels can
61  be interpolated in one go. They are also included in 888 and 8888 to make
62  the same functions compatible when interpolating 2 32-bit pixels.
63 */
64 
65 
66 template<>
67 struct ColorMasks<565> {
68  enum : uint {
69  kHighBitsMask = 0xF7DEF7DE,
70  kLowBitsMask = 0x08210821,
71  qhighBits = 0xE79CE79C,
72  qlowBits = 0x18631863,
73 
74 
75  kBytesPerPixel = 2,
76 
77  kAlphaBits = 0,
78  kRedBits = 5,
79  kGreenBits = 6,
80  kBlueBits = 5,
81 
82  kAlphaShift = 0,
83  kRedShift = kGreenBits+kBlueBits,
84  kGreenShift = kBlueBits,
85  kBlueShift = 0,
86 
87  kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
88  kRedMask = ((1 << kRedBits) - 1) << kRedShift,
89  kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
90  kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
91 
92  kRedBlueMask = kRedMask | kBlueMask,
93 
94  kLowBits = (1 << kRedShift) | (1 << kGreenShift) | (1 << kBlueShift),
95  kLow2Bits = (3 << kRedShift) | (3 << kGreenShift) | (3 << kBlueShift),
96  kLow3Bits = (7 << kRedShift) | (7 << kGreenShift) | (7 << kBlueShift)
97  };
98 
99  typedef uint16 PixelType;
100 };
101 
102 template<>
103 struct ColorMasks<555> {
104  enum {
105  kHighBitsMask = 0x7BDE7BDE,
106  kLowBitsMask = 0x04210421,
107  qhighBits = 0x739C739C,
108  qlowBits = 0x0C630C63,
109 
110 
111  kBytesPerPixel = 2,
112 
113  kAlphaBits = 0,
114  kRedBits = 5,
115  kGreenBits = 5,
116  kBlueBits = 5,
117 
118  kAlphaShift = 0,
119  kRedShift = kGreenBits+kBlueBits,
120  kGreenShift = kBlueBits,
121  kBlueShift = 0,
122 
123  kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
124  kRedMask = ((1 << kRedBits) - 1) << kRedShift,
125  kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
126  kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
127 
128  kRedBlueMask = kRedMask | kBlueMask,
129 
130  kLowBits = (1 << kRedShift) | (1 << kGreenShift) | (1 << kBlueShift),
131  kLow2Bits = (3 << kRedShift) | (3 << kGreenShift) | (3 << kBlueShift),
132  kLow3Bits = (7 << kRedShift) | (7 << kGreenShift) | (7 << kBlueShift)
133  };
134 
135  typedef uint16 PixelType;
136 };
137 
138 template<>
139 struct ColorMasks<1555> {
140  enum {
141  kBytesPerPixel = 2,
142 
143  kAlphaBits = 1,
144  kRedBits = 5,
145  kGreenBits = 5,
146  kBlueBits = 5,
147 
148  kAlphaShift = kRedBits+kGreenBits+kBlueBits,
149  kRedShift = 0,
150  kGreenShift = kBlueBits,
151  kBlueShift = kGreenBits+kBlueBits,
152 
153  kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
154  kRedMask = ((1 << kRedBits) - 1) << kRedShift,
155  kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
156  kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
157 
158  kRedBlueMask = kRedMask | kBlueMask
159  };
160 
161  typedef uint16 PixelType;
162 };
163 
164 template<>
165 struct ColorMasks<5551> {
166  enum {
167  kBytesPerPixel = 2,
168 
169  kAlphaBits = 1,
170  kRedBits = 5,
171  kGreenBits = 5,
172  kBlueBits = 5,
173 
174  kAlphaShift = 0,
175  kRedShift = kGreenBits+kBlueBits+kAlphaBits,
176  kGreenShift = kBlueBits+kAlphaBits,
177  kBlueShift = kAlphaBits,
178 
179  kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
180  kRedMask = ((1 << kRedBits) - 1) << kRedShift,
181  kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
182  kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
183 
184  kRedBlueMask = kRedMask | kBlueMask
185  };
186 
187  typedef uint16 PixelType;
188 };
189 
190 template<>
191 struct ColorMasks<4444> {
192  enum {
193  kBytesPerPixel = 2,
194 
195  kAlphaBits = 4,
196  kRedBits = 4,
197  kGreenBits = 4,
198  kBlueBits = 4,
199 
200  kAlphaShift = kRedBits+kGreenBits+kBlueBits,
201  kRedShift = kGreenBits+kBlueBits,
202  kGreenShift = kBlueBits,
203  kBlueShift = 0,
204 
205  kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
206  kRedMask = ((1 << kRedBits) - 1) << kRedShift,
207  kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
208  kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
209 
210  kRedBlueMask = kRedMask | kBlueMask
211  };
212 
213  typedef uint16 PixelType;
214 };
215 
216 template<>
217 struct ColorMasks<888> {
218  enum {
219  kBytesPerPixel = 4,
220 
221  kAlphaBits = 0,
222  kRedBits = 8,
223  kGreenBits = 8,
224  kBlueBits = 8,
225 
226  kAlphaShift = 0,
227  kRedShift = kGreenBits+kBlueBits,
228  kGreenShift = kBlueBits,
229  kBlueShift = 0,
230 
231  kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
232  kRedMask = ((1 << kRedBits) - 1) << kRedShift,
233  kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
234  kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
235 
236  kRedBlueMask = kRedMask | kBlueMask,
237 
238  kLowBits = (1 << kRedShift) | (1 << kGreenShift) | (1 << kBlueShift),
239  kLow2Bits = (3 << kRedShift) | (3 << kGreenShift) | (3 << kBlueShift),
240  kLow3Bits = (7 << kRedShift) | (7 << kGreenShift) | (7 << kBlueShift),
241  kLow4Bits = (15 << kRedShift) | (15 << kGreenShift) | (15 << kBlueShift),
242 
243  kLowBitsMask = kLowBits,
244  // Prevent mask from including padding byte
245  kHighBitsMask = (~kLowBits) & (kRedMask | kBlueMask | kGreenMask),
246  qlowBits = kLow2Bits,
247  qhighBits = (~kLowBits) & (kRedMask | kBlueMask | kGreenMask)
248  };
249 
250  typedef uint32 PixelType;
251 };
252 
253 template<>
254 struct ColorMasks<8888> {
255  enum {
256  kBytesPerPixel = 4,
257 
258  kAlphaBits = 8,
259  kRedBits = 8,
260  kGreenBits = 8,
261  kBlueBits = 8,
262 
263  kAlphaShift = kRedBits+kGreenBits+kBlueBits,
264  kRedShift = kGreenBits+kBlueBits,
265  kGreenShift = kBlueBits,
266  kBlueShift = 0,
267 
268  kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
269  kRedMask = ((1 << kRedBits) - 1) << kRedShift,
270  kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
271  kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
272 
273  kRedBlueMask = kRedMask | kBlueMask,
274 
275  kLowBits = (1 << kRedShift) | (1 << kGreenShift) | (1 << kBlueShift) | (1 << kAlphaShift),
276  kLow2Bits = (3 << kRedShift) | (3 << kGreenShift) | (3 << kBlueShift) | (3 << kAlphaShift),
277  kLow3Bits = (7 << kRedShift) | (7 << kGreenShift) | (7 << kBlueShift) | (7 << kAlphaShift),
278  kLow4Bits = (15 << kRedShift) | (15 << kGreenShift) | (15 << kBlueShift) | (15 << kAlphaShift),
279 
280  kLowBitsMask = kLowBits,
281  kHighBitsMask = ~kLowBits,
282  qlowBits = kLow2Bits,
283  qhighBits = ~kLow2Bits
284  };
285 
286  typedef uint32 PixelType;
287 };
288 
289 template<>
290 struct ColorMasks<-8888> {
291  enum : uint {
292  kBytesPerPixel = 4,
293 
294  kAlphaBits = 8,
295  kRedBits = 8,
296  kGreenBits = 8,
297  kBlueBits = 8,
298 
299  kAlphaShift = 0,
300  kRedShift = kAlphaBits,
301  kGreenShift = kRedBits+kAlphaBits,
302  kBlueShift = kGreenBits+kRedBits+kAlphaBits,
303 
304  kAlphaMask = ((1u << kAlphaBits) - 1) << kAlphaShift,
305  kRedMask = ((1u << kRedBits) - 1) << kRedShift,
306  kGreenMask = ((1u << kGreenBits) - 1) << kGreenShift,
307  kBlueMask = ((1u << kBlueBits) - 1) << kBlueShift,
308 
309  kRedBlueMask = kRedMask | kBlueMask,
310 
311  kLowBits = (1 << kRedShift) | (1 << kGreenShift) | (1 << kBlueShift) | (1 << kAlphaShift),
312  kLow2Bits = (3 << kRedShift) | (3 << kGreenShift) | (3 << kBlueShift) | (3 << kAlphaShift),
313  kLow3Bits = (7 << kRedShift) | (7 << kGreenShift) | (7 << kBlueShift) | (7 << kAlphaShift),
314  kLow4Bits = (15 << kRedShift) | (15 << kGreenShift) | (15 << kBlueShift) | (15 << kAlphaShift),
315 
316  kLowBitsMask = kLowBits,
317  kHighBitsMask = (uint) ~kLowBits,
318  qlowBits = kLow2Bits,
319  qhighBits = (uint) ~kLow2Bits
320  };
321 
322  typedef uint32 PixelType;
323 };
324 
325 } // End of namespace Graphics
326 
327 #endif
Definition: formatinfo.h:28
Definition: colormasks.h:30