1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/* 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.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef GRAPHICS_PIXELFORMAT_H
#define GRAPHICS_PIXELFORMAT_H
#include "common/scummsys.h"
#include "common/str.h"
#include "graphics/colormasks.h"
namespace Graphics {
/**
* @defgroup graphics_pixelformat Pixel formats
* @ingroup graphics
*
* @brief Structures for managing pixel formats.
*
* @{
*/
/** Template to expand from an n-bit component to an 8-bit component. */
template<int depth>
struct ColorComponent {
};
/** Return 0 for an empty color component. */
template<>
struct ColorComponent<0> {
static inline uint expand(uint value) {
return 0;
}
};
/** Template to expand a 1-bit component into an 8-bit component. */
template<>
struct ColorComponent<1> {
static inline uint expand(uint value) {
return value ? 0xff : 0;
}
};
/** Template to expand a 2-bit component into an 8-bit component. */
template<>
struct ColorComponent<2> {
static inline uint expand(uint value) {
value &= 3;
return value |
(value << 2) |
(value << 4) |
(value << 6);
}
};
/** Template to expand a 3-bit component into an 8-bit component. */
template<>
struct ColorComponent<3> {
static inline uint expand(uint value) {
value &= 7;
return (value << 5) |
(value << 2) |
(value >> 1);
}
};
/** Template to expand a 4-bit component into an 8-bit component. */
template<>
struct ColorComponent<4> {
static inline uint expand(uint value) {
value &= 15;
return value |
(value << 4);
}
};
/** Template to expand a 5-bit component into an 8-bit component. */
template<>
struct ColorComponent<5> {
static inline uint expand(uint value) {
value &= 31;
return (value << 3) |
(value >> 2);
}
};
/** Template to expand a 6-bit component into an 8-bit component. */
template<>
struct ColorComponent<6> {
static inline uint expand(uint value) {
value &= 63;
return (value << 2) |
(value >> 4);
}
};
/** Template to expand a 7-bit component into an 8-bit component. */
template<>
struct ColorComponent<7> {
static inline uint expand(uint value) {
value &= 127;
return (value << 1) |
(value >> 6);
}
};
/** Return the given value. */
template<>
struct ColorComponent<8> {
static inline uint expand(uint value) {
return value & 255;
}
};
/**
* Pixel format description.
*
* Like ColorMasks, it includes the given values to create colors from RGB
* values and to retrieve RGB values from colors.
*
* Unlike ColorMasks, it is not dependent on knowing the exact pixel format
* on compile time.
*
* A minor difference between ColorMasks and PixelFormat is that ColorMasks
* stores the bit count per channel in @c kFooBits, while PixelFormat stores
* the loss compared to 8 bits per channel in @c \#Loss. It also does not
* contain mask values.
*/
struct PixelFormat {
byte bytesPerPixel; /**< Number of bytes used in the pixel format. */
byte rLoss, gLoss, bLoss, aLoss; /**< Precision loss of each color component. */
byte rShift, gShift, bShift, aShift; /**< Binary left shift of each color component in the pixel value. */
/** Default constructor that creates a null pixel format. */
constexpr PixelFormat() :
bytesPerPixel(0),
rLoss(0), gLoss(0), bLoss(0), aLoss(0),
rShift(0), gShift(0), bShift(0), aShift(0) {}
/** Construct a pixel format based on the provided arguments.
*
* Examples:
*
* - RGBA8888:
* @code
* BytesPerPixel = 4, RBits = GBits = BBits = ABits = 8, RShift = 24, GShift = 16, BShift = 8, AShift = 0
* @endcode
* - ABGR8888:
* @code
* BytesPerPixel = 4, RBits = GBits = BBits = ABits = 8, RShift = 0, GShift = 8, BShift = 16, AShift = 24
* @endcode
* - RGB565:
* @code
* BytesPerPixel = 2, RBits = 5, GBits = 6, BBits = 5, ABits = 0, RShift = 11, GShift = 5, BShift = 0, AShift = 0
* @endcode
*/
constexpr PixelFormat(byte BytesPerPixel,
byte RBits, byte GBits, byte BBits, byte ABits,
byte RShift, byte GShift, byte BShift, byte AShift) :
bytesPerPixel(BytesPerPixel),
rLoss(8 - RBits),
gLoss(8 - GBits),
bLoss(8 - BBits),
aLoss(8 - ABits),
rShift((RBits == 0) ? 0 : RShift),
gShift((GBits == 0) ? 0 : GShift),
bShift((BBits == 0) ? 0 : BShift),
aShift((ABits == 0) ? 0 : AShift)
{
}
/** Define a CLUT8 pixel format. */
static inline PixelFormat createFormatCLUT8() {
return PixelFormat(1, 0, 0, 0, 0, 0, 0, 0, 0);
}
/** Check if two pixel formats are the same */
inline bool operator==(const PixelFormat &fmt) const {
return bytesPerPixel == fmt.bytesPerPixel &&
rLoss == fmt.rLoss &&
gLoss == fmt.gLoss &&
bLoss == fmt.bLoss &&
aLoss == fmt.aLoss &&
rShift == fmt.rShift &&
gShift == fmt.gShift &&
bShift == fmt.bShift &&
aShift == fmt.aShift;
}
/** Check if two pixel formats are different. */
inline bool operator!=(const PixelFormat &fmt) const {
return !(*this == fmt);
}
/** Return an RGB color value from red, green, and blue values. */
inline uint32 RGBToColor(uint8 r, uint8 g, uint8 b) const {
return
((0xFF >> aLoss) << aShift) |
(( r >> rLoss) << rShift) |
(( g >> gLoss) << gShift) |
(( b >> bLoss) << bShift);
}
template<class T>
inline uint32 RGBToColorT(uint8 r, uint8 g, uint8 b) const {
return 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);
}
/** Return an ARGB color value from alpha, red, green, and blue values. */
inline uint32 ARGBToColor(uint8 a, uint8 r, uint8 g, uint8 b) const {
return
((a >> aLoss) << aShift) |
((r >> rLoss) << rShift) |
((g >> gLoss) << gShift) |
((b >> bLoss) << bShift);
}
template<class T>
inline uint32 ARGBToColorT(uint8 a, uint8 r, uint8 g, uint8 b) const {
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);
}
/** Retrieve red, green, and blue values from an RGB color value. */
inline void colorToRGB(uint32 color, uint8 &r, uint8 &g, uint8 &b) const {
r = expand(rBits(), color >> rShift);
g = expand(gBits(), color >> gShift);
b = expand(bBits(), color >> bShift);
}
template<class T>
inline void colorToRGBT(uint32 color, uint8 &r, uint8 &g, uint8 &b) const {
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);
}
/** Retrieve alpha, red, green, and blue values from an ARGB color value. */
inline void colorToARGB(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const {
a = (aBits() == 0) ? 0xFF : expand(aBits(), color >> aShift);
r = expand(rBits(), color >> rShift);
g = expand(gBits(), color >> gShift);
b = expand(bBits(), color >> bShift);
}
template<class T>
inline void colorToARGBT(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const {
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);
}
/**
* @name Convenience functions for getting the number of color component bits
* @{
*/
/**
* Return the number of red component bits.
*/
inline byte rBits() const {
return (8 - rLoss);
}
/**
* Return the number of green component bits.
*/
inline byte gBits() const {
return (8 - gLoss);
}
/**
* Return the number of blue component bits.
*/
inline byte bBits() const {
return (8 - bLoss);
}
/**
* Return the number of alpha component bits.
*/
inline byte aBits() const {
return (8 - aLoss);
}
/**
* Return the total number of bits for the pixel format.
*/
inline byte bpp() const {
return rBits() + gBits() + bBits() + aBits();
}
/** @} */
/**
* @name Convenience functions for getting color components' maximum values
* @{
*/
/**
* Return the maximum value of red.
*/
inline uint rMax() const {
return (1 << rBits()) - 1;
}
/**
* Return the maximum value of green.
*/
inline uint gMax() const {
return (1 << gBits()) - 1;
}
/**
* Return the maximum value of blue.
*/
inline uint bMax() const {
return (1 << bBits()) - 1;
}
/**
* Return the maximum value of alpha.
*/
inline uint aMax() const {
return (1 << aBits()) - 1;
}
/** @} */
/** Expand a given bit-depth component to a full 8-bit component. @todo is that different from the templates at the beginning? */
static inline uint expand(uint bits, uint color) {
switch (bits) {
case 0:
return ColorComponent<0>::expand(color);
case 1:
return ColorComponent<1>::expand(color);
case 2:
return ColorComponent<2>::expand(color);
case 3:
return ColorComponent<3>::expand(color);
case 4:
return ColorComponent<4>::expand(color);
case 5:
return ColorComponent<5>::expand(color);
case 6:
return ColorComponent<6>::expand(color);
case 7:
return ColorComponent<7>::expand(color);
case 8:
return ColorComponent<8>::expand(color);
default:
break;
}
// Unsupported
return 0;
}
/** Return string representation. */
Common::String toString() const;
/** Shortcut method for checking if the current format is CLUT8 or not */
bool isCLUT8() const {
// We do not really need to check masks when all shifts equal zeroes
return bytesPerPixel == 1 && rShift == 0 && gShift == 0 && bShift == 0 && aShift == 0;
}
};
template<>
inline uint32 PixelFormat::RGBToColorT<ColorMasks<0> >(uint8 r, uint8 g, uint8 b) const {
return RGBToColor(r, g, b);
}
template<>
inline uint32 PixelFormat::ARGBToColorT<ColorMasks<0> >(uint8 a, uint8 r, uint8 g, uint8 b) const {
return ARGBToColor(a, r, g, b);
}
template<>
inline void PixelFormat::colorToRGBT<ColorMasks<0> >(uint32 color, uint8 &r, uint8 &g, uint8 &b) const {
colorToRGB(color, r, g, b);
}
template<>
inline void PixelFormat::colorToARGBT<ColorMasks<0> >(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const {
colorToARGB(color, a, r, g, b);
}
/**
* Convert a 'bitFormat' as defined by one of the ColorMasks
* into a PixelFormat.
*/
template<int bitFormat>
PixelFormat createPixelFormat() {
PixelFormat format;
format.bytesPerPixel = ColorMasks<bitFormat>::kBytesPerPixel;
format.rLoss = 8 - ColorMasks<bitFormat>::kRedBits;
format.gLoss = 8 - ColorMasks<bitFormat>::kGreenBits;
format.bLoss = 8 - ColorMasks<bitFormat>::kBlueBits;
format.aLoss = 8 - ColorMasks<bitFormat>::kAlphaBits;
format.rShift = ColorMasks<bitFormat>::kRedShift;
format.gShift = ColorMasks<bitFormat>::kGreenShift;
format.bShift = ColorMasks<bitFormat>::kBlueShift;
format.aShift = ColorMasks<bitFormat>::kAlphaShift;
return format;
}
/** @} */
} // End of namespace Graphics
#endif
|