2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2006-04-17 10:22:05 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GRAPHICS_COLORMASKS_H
|
|
|
|
#define GRAPHICS_COLORMASKS_H
|
|
|
|
|
2009-01-22 04:35:10 +00:00
|
|
|
#include "graphics/pixelformat.h"
|
|
|
|
|
2008-11-03 13:44:59 +00:00
|
|
|
namespace Graphics {
|
|
|
|
|
2006-04-17 10:22:05 +00:00
|
|
|
template<int bitFormat>
|
|
|
|
struct ColorMasks {
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
The ColorMasks template can be used to map bit format values
|
|
|
|
(like 555, 565, 1555, 4444) to corresponding bit masks and shift values.
|
2008-01-27 19:47:41 +00:00
|
|
|
Currently this is only meant for
|
2006-04-17 10:22:05 +00:00
|
|
|
|
|
|
|
The meaning of these is masks is the following:
|
|
|
|
kBytesPerPixel
|
|
|
|
-> how many bytes per pixel for that format
|
|
|
|
|
|
|
|
kRedMask, kGreenMask, kBlueMask
|
|
|
|
-> bitmask, and this with the color to select only the bits of the corresponding color
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2006-04-17 10:22:05 +00:00
|
|
|
The k*Bits and k*Shift values can be used to extract R,G,B. I.e. to get
|
|
|
|
the red color component of a pixel, as a 8-bit value, you would write
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2006-04-17 10:22:05 +00:00
|
|
|
R = ((color & kRedMask) >> kRedShift) << (8-kRedBits)
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2006-04-17 10:22:05 +00:00
|
|
|
Actually, instead of the simple left shift, one might want to use somewhat
|
2008-01-27 19:47:41 +00:00
|
|
|
more sophisticated code (which fills up the least significant bits with
|
2007-08-11 08:02:19 +00:00
|
|
|
appropriate data).
|
2008-01-27 19:47:41 +00:00
|
|
|
|
|
|
|
|
2009-01-25 04:29:25 +00:00
|
|
|
The kHighBitsMask / kLowBitsMask / qhighBits / qlowBits are special values that are
|
2006-04-17 10:22:05 +00:00
|
|
|
used in the super-optimized interpolation functions in scaler/intern.h
|
|
|
|
and scaler/aspect.cpp. Currently they are only available in 555 and 565 mode.
|
|
|
|
To be specific: They pack the masks for two 16 bit pixels at once. The pixels
|
|
|
|
are split into "high" and "low" bits, which are then separately interpolated
|
|
|
|
and finally re-composed. That way, 2x2 pixels or even 4x2 pixels can
|
|
|
|
be interpolated in one go.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ColorMasks<565> {
|
|
|
|
enum {
|
2009-01-25 04:29:25 +00:00
|
|
|
kHighBitsMask = 0xF7DEF7DE,
|
|
|
|
kLowBitsMask = 0x08210821,
|
2006-04-17 10:22:05 +00:00
|
|
|
qhighBits = 0xE79CE79C,
|
|
|
|
qlowBits = 0x18631863,
|
|
|
|
|
|
|
|
|
|
|
|
kBytesPerPixel = 2,
|
|
|
|
|
|
|
|
kAlphaBits = 0,
|
|
|
|
kRedBits = 5,
|
|
|
|
kGreenBits = 6,
|
|
|
|
kBlueBits = 5,
|
|
|
|
|
2009-01-22 04:35:10 +00:00
|
|
|
kAlphaShift = 0,
|
2006-04-17 10:22:05 +00:00
|
|
|
kRedShift = kGreenBits+kBlueBits,
|
|
|
|
kGreenShift = kBlueBits,
|
|
|
|
kBlueShift = 0,
|
|
|
|
|
2009-01-25 04:29:25 +00:00
|
|
|
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
|
|
|
|
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
|
|
|
|
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
|
|
|
|
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
|
2006-04-17 10:22:05 +00:00
|
|
|
|
2009-01-25 04:29:25 +00:00
|
|
|
kRedBlueMask = kRedMask | kBlueMask,
|
2009-01-27 01:23:04 +00:00
|
|
|
|
|
|
|
kLowBits = (1 << kRedShift) | (1 << kGreenShift) | (1 << kBlueShift),
|
|
|
|
kLow2Bits = (3 << kRedShift) | (3 << kGreenShift) | (3 << kBlueShift),
|
|
|
|
kLow3Bits = (7 << kRedShift) | (7 << kGreenShift) | (7 << kBlueShift)
|
2006-04-17 10:22:05 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ColorMasks<555> {
|
|
|
|
enum {
|
2009-01-25 04:29:25 +00:00
|
|
|
kHighBitsMask = 0x7BDE7BDE,
|
|
|
|
kLowBitsMask = 0x04210421,
|
2006-04-17 10:22:05 +00:00
|
|
|
qhighBits = 0x739C739C,
|
|
|
|
qlowBits = 0x0C630C63,
|
|
|
|
|
|
|
|
|
|
|
|
kBytesPerPixel = 2,
|
|
|
|
|
|
|
|
kAlphaBits = 0,
|
|
|
|
kRedBits = 5,
|
|
|
|
kGreenBits = 5,
|
|
|
|
kBlueBits = 5,
|
|
|
|
|
2009-12-30 21:11:38 +00:00
|
|
|
#ifdef __N64__
|
2009-12-30 22:56:19 +00:00
|
|
|
/* Nintendo 64 uses a BGR555 color format for 16bit display */
|
2009-12-30 21:11:38 +00:00
|
|
|
kAlphaShift = 0,
|
|
|
|
kRedShift = kBlueBits+kGreenBits+1,
|
|
|
|
kGreenShift = kBlueBits + 1,
|
|
|
|
kBlueShift = 1,
|
2009-12-30 22:56:19 +00:00
|
|
|
#else /* RGB555 */
|
2009-01-22 04:35:10 +00:00
|
|
|
kAlphaShift = 0,
|
2006-04-17 10:22:05 +00:00
|
|
|
kRedShift = kGreenBits+kBlueBits,
|
|
|
|
kGreenShift = kBlueBits,
|
|
|
|
kBlueShift = 0,
|
2009-12-30 21:11:38 +00:00
|
|
|
#endif
|
2006-04-17 10:22:05 +00:00
|
|
|
|
|
|
|
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
|
|
|
|
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
|
|
|
|
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
|
|
|
|
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
|
|
|
|
|
2009-01-25 04:29:25 +00:00
|
|
|
kRedBlueMask = kRedMask | kBlueMask,
|
2009-01-27 01:23:04 +00:00
|
|
|
|
|
|
|
kLowBits = (1 << kRedShift) | (1 << kGreenShift) | (1 << kBlueShift),
|
|
|
|
kLow2Bits = (3 << kRedShift) | (3 << kGreenShift) | (3 << kBlueShift),
|
|
|
|
kLow3Bits = (7 << kRedShift) | (7 << kGreenShift) | (7 << kBlueShift)
|
2006-04-17 10:22:05 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ColorMasks<1555> {
|
|
|
|
enum {
|
|
|
|
kBytesPerPixel = 2,
|
|
|
|
|
|
|
|
kAlphaBits = 1,
|
|
|
|
kRedBits = 5,
|
|
|
|
kGreenBits = 5,
|
|
|
|
kBlueBits = 5,
|
|
|
|
|
|
|
|
kAlphaShift = kRedBits+kGreenBits+kBlueBits,
|
2009-02-10 23:38:07 +00:00
|
|
|
kRedShift = 0,
|
2006-04-17 10:22:05 +00:00
|
|
|
kGreenShift = kBlueBits,
|
2009-02-10 23:38:07 +00:00
|
|
|
kBlueShift = kGreenBits+kBlueBits,
|
2006-04-17 10:22:05 +00:00
|
|
|
|
|
|
|
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
|
|
|
|
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
|
|
|
|
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
|
|
|
|
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
|
|
|
|
|
|
|
|
kRedBlueMask = kRedMask | kBlueMask
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2010-09-29 00:19:13 +00:00
|
|
|
template<>
|
|
|
|
struct ColorMasks<5551> {
|
|
|
|
enum {
|
|
|
|
kBytesPerPixel = 2,
|
|
|
|
|
|
|
|
kAlphaBits = 1,
|
|
|
|
kRedBits = 5,
|
|
|
|
kGreenBits = 5,
|
|
|
|
kBlueBits = 5,
|
|
|
|
|
|
|
|
kAlphaShift = 0,
|
|
|
|
kRedShift = kGreenBits+kBlueBits+kAlphaBits,
|
|
|
|
kGreenShift = kBlueBits+kAlphaBits,
|
|
|
|
kBlueShift = kAlphaBits,
|
|
|
|
|
|
|
|
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
|
|
|
|
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
|
|
|
|
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
|
|
|
|
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
|
|
|
|
|
|
|
|
kRedBlueMask = kRedMask | kBlueMask
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2006-04-17 10:22:05 +00:00
|
|
|
template<>
|
|
|
|
struct ColorMasks<4444> {
|
|
|
|
enum {
|
|
|
|
kBytesPerPixel = 2,
|
|
|
|
|
|
|
|
kAlphaBits = 4,
|
|
|
|
kRedBits = 4,
|
|
|
|
kGreenBits = 4,
|
|
|
|
kBlueBits = 4,
|
|
|
|
|
2009-02-18 13:28:52 +00:00
|
|
|
#ifdef __PSP__ //PSP uses ABGR
|
|
|
|
kAlphaShift = kRedBits+kGreenBits+kBlueBits,
|
|
|
|
kRedShift = 0,
|
|
|
|
kGreenShift = kRedBits,
|
|
|
|
kBlueShift = kRedBits+kGreenBits,
|
|
|
|
#else //ARGB
|
2006-04-17 10:22:05 +00:00
|
|
|
kAlphaShift = kRedBits+kGreenBits+kBlueBits,
|
|
|
|
kRedShift = kGreenBits+kBlueBits,
|
|
|
|
kGreenShift = kBlueBits,
|
|
|
|
kBlueShift = 0,
|
2009-02-18 13:28:52 +00:00
|
|
|
#endif
|
2006-04-17 10:22:05 +00:00
|
|
|
|
|
|
|
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
|
|
|
|
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
|
|
|
|
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
|
|
|
|
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
|
|
|
|
|
|
|
|
kRedBlueMask = kRedMask | kBlueMask
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ColorMasks<888> {
|
|
|
|
enum {
|
|
|
|
kBytesPerPixel = 4,
|
|
|
|
|
|
|
|
kAlphaBits = 0,
|
|
|
|
kRedBits = 8,
|
|
|
|
kGreenBits = 8,
|
|
|
|
kBlueBits = 8,
|
|
|
|
|
2009-01-22 04:35:10 +00:00
|
|
|
kAlphaShift = 0,
|
2006-04-17 10:22:05 +00:00
|
|
|
kRedShift = kGreenBits+kBlueBits,
|
|
|
|
kGreenShift = kBlueBits,
|
|
|
|
kBlueShift = 0,
|
|
|
|
|
|
|
|
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
|
|
|
|
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
|
|
|
|
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
|
|
|
|
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
|
|
|
|
|
|
|
|
kRedBlueMask = kRedMask | kBlueMask
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ColorMasks<8888> {
|
|
|
|
enum {
|
|
|
|
kBytesPerPixel = 4,
|
|
|
|
|
|
|
|
kAlphaBits = 8,
|
|
|
|
kRedBits = 8,
|
|
|
|
kGreenBits = 8,
|
|
|
|
kBlueBits = 8,
|
|
|
|
|
|
|
|
kAlphaShift = kRedBits+kGreenBits+kBlueBits,
|
|
|
|
kRedShift = kGreenBits+kBlueBits,
|
|
|
|
kGreenShift = kBlueBits,
|
|
|
|
kBlueShift = 0,
|
|
|
|
|
|
|
|
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
|
|
|
|
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
|
|
|
|
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
|
|
|
|
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
|
|
|
|
|
|
|
|
kRedBlueMask = kRedMask | kBlueMask
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-08-30 16:57:21 +00:00
|
|
|
#ifdef __WII__
|
|
|
|
/* Gamecube/Wii specific ColorMask ARGB3444 */
|
|
|
|
template<>
|
|
|
|
struct ColorMasks<3444> {
|
|
|
|
enum {
|
|
|
|
kBytesPerPixel = 2,
|
|
|
|
|
|
|
|
kAlphaBits = 3,
|
|
|
|
kRedBits = 4,
|
|
|
|
kGreenBits = 4,
|
|
|
|
kBlueBits = 4,
|
|
|
|
|
|
|
|
kBlueShift = 0,
|
|
|
|
kGreenShift = kBlueBits,
|
|
|
|
kRedShift = kGreenBits+kBlueBits,
|
|
|
|
kAlphaShift = kGreenBits+kBlueBits+kRedBits,
|
|
|
|
|
|
|
|
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
|
|
|
|
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
|
|
|
|
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
|
|
|
|
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
|
|
|
|
|
|
|
|
kRedBlueMask = kRedMask | kBlueMask
|
|
|
|
};
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2006-04-17 10:41:18 +00:00
|
|
|
template<class T>
|
|
|
|
uint32 RGBToColor(uint8 r, uint8 g, uint8 b) {
|
2006-04-17 10:44:02 +00:00
|
|
|
return T::kAlphaMask |
|
2006-04-17 11:09:21 +00:00
|
|
|
(((r << T::kRedShift) >> (8 - T::kRedBits)) & T::kRedMask) |
|
|
|
|
(((g << T::kGreenShift) >> (8 - T::kGreenBits)) & T::kGreenMask) |
|
|
|
|
(((b << T::kBlueShift) >> (8 - T::kBlueBits)) & T::kBlueMask);
|
2006-04-17 10:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
uint32 ARGBToColor(uint8 a, uint8 r, uint8 g, uint8 b) {
|
2006-04-17 11:09:21 +00:00
|
|
|
return (((a << T::kAlphaShift) >> (8 - T::kAlphaBits)) & T::kAlphaMask) |
|
|
|
|
(((r << T::kRedShift) >> (8 - T::kRedBits)) & T::kRedMask) |
|
|
|
|
(((g << T::kGreenShift) >> (8 - T::kGreenBits)) & T::kGreenMask) |
|
|
|
|
(((b << T::kBlueShift) >> (8 - T::kBlueBits)) & T::kBlueMask);
|
2006-04-17 10:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void colorToRGB(uint32 color, uint8 &r, uint8 &g, uint8 &b) {
|
|
|
|
r = ((color & T::kRedMask) >> T::kRedShift) << (8 - T::kRedBits);
|
|
|
|
g = ((color & T::kGreenMask) >> T::kGreenShift) << (8 - T::kGreenBits);
|
|
|
|
b = ((color & T::kBlueMask) >> T::kBlueShift) << (8 - T::kBlueBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void colorToARGB(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) {
|
|
|
|
a = ((color & T::kAlphaMask) >> T::kAlphaShift) << (8 - T::kAlphaBits);
|
|
|
|
r = ((color & T::kRedMask) >> T::kRedShift) << (8 - T::kRedBits);
|
|
|
|
g = ((color & T::kGreenMask) >> T::kGreenShift) << (8 - T::kGreenBits);
|
|
|
|
b = ((color & T::kBlueMask) >> T::kBlueShift) << (8 - T::kBlueBits);
|
|
|
|
}
|
2006-04-17 10:22:05 +00:00
|
|
|
|
2008-11-03 13:44:59 +00:00
|
|
|
|
|
|
|
|
2009-01-22 04:35:10 +00:00
|
|
|
/**
|
|
|
|
* Convert a 'bitFormat' as defined by one of the ColorMasks
|
|
|
|
* into a PixelFormat.
|
|
|
|
*/
|
2008-11-03 14:02:29 +00:00
|
|
|
template<int bitFormat>
|
|
|
|
PixelFormat createPixelFormat() {
|
2008-11-03 13:44:59 +00:00
|
|
|
PixelFormat format;
|
|
|
|
|
2008-11-03 14:02:29 +00:00
|
|
|
format.bytesPerPixel = ColorMasks<bitFormat>::kBytesPerPixel;
|
2008-11-03 13:44:59 +00:00
|
|
|
|
2008-11-03 14:02:29 +00:00
|
|
|
format.rLoss = 8 - ColorMasks<bitFormat>::kRedBits;
|
|
|
|
format.gLoss = 8 - ColorMasks<bitFormat>::kGreenBits;
|
|
|
|
format.bLoss = 8 - ColorMasks<bitFormat>::kBlueBits;
|
|
|
|
format.aLoss = 8 - ColorMasks<bitFormat>::kAlphaBits;
|
2008-11-03 13:44:59 +00:00
|
|
|
|
2008-11-03 14:02:29 +00:00
|
|
|
format.rShift = ColorMasks<bitFormat>::kRedShift;
|
|
|
|
format.gShift = ColorMasks<bitFormat>::kGreenShift;
|
|
|
|
format.bShift = ColorMasks<bitFormat>::kBlueShift;
|
|
|
|
format.aShift = ColorMasks<bitFormat>::kAlphaShift;
|
2008-11-03 13:44:59 +00:00
|
|
|
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Graphics
|
2008-11-03 13:44:59 +00:00
|
|
|
|
2006-04-17 10:22:05 +00:00
|
|
|
#endif
|