diff options
author | Johannes Schickel | 2008-11-03 13:44:59 +0000 |
---|---|---|
committer | Johannes Schickel | 2008-11-03 13:44:59 +0000 |
commit | 985c02ee7da43fb66b61d6e3c5530e78bd2d8286 (patch) | |
tree | 44bc31e701a7b020486ab407560d6d9c0e0329b2 /graphics/colormasks.h | |
parent | d0c9b0cb233a00094f18269d94b7d0fb64433099 (diff) | |
download | scummvm-rg350-985c02ee7da43fb66b61d6e3c5530e78bd2d8286.tar.gz scummvm-rg350-985c02ee7da43fb66b61d6e3c5530e78bd2d8286.tar.bz2 scummvm-rg350-985c02ee7da43fb66b61d6e3c5530e78bd2d8286.zip |
Committed my patch #2216641 "GRAPHICS: PixelFormat introduction".
svn-id: r34875
Diffstat (limited to 'graphics/colormasks.h')
-rw-r--r-- | graphics/colormasks.h | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/graphics/colormasks.h b/graphics/colormasks.h index 5b9f0517a9..e8048f6d64 100644 --- a/graphics/colormasks.h +++ b/graphics/colormasks.h @@ -26,6 +26,8 @@ #ifndef GRAPHICS_COLORMASKS_H #define GRAPHICS_COLORMASKS_H +namespace Graphics { + template<int bitFormat> struct ColorMasks { }; @@ -251,4 +253,76 @@ void colorToARGB(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) { b = ((color & T::kBlueMask) >> T::kBlueShift) << (8 - T::kBlueBits); } +/** + * A 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 dependend 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 'kFooBits', while PixelFormat stores + * the loss compared to 8 bits per channel in '#Loss'. + */ +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. */ + + uint32 rMask, gMask, bMask, aMask; /**< Binary mask used to retrieve individual color values. */ +}; + +template<class Mask> +PixelFormat createPixelFormatFromMask() { + PixelFormat format; + + format.bytesPerPixel = Mask::kBytesPerPixel; + + format.rLoss = 8 - Mask::kRedBits; + format.gLoss = 8 - Mask::kGreenBits; + format.bLoss = 8 - Mask::kBlueBits; + format.aLoss = 8 - Mask::kAlphaBits; + + format.rShift = Mask::kRedShift; + format.gShift = Mask::kGreenShift; + format.bShift = Mask::kBlueShift; + format.aShift = Mask::kAlphaShift; + + return format; +} + +inline uint32 RGBToColor(uint8 r, uint8 g, uint8 b, const PixelFormat &fmt) { + return + ((0xFF >> fmt.aLoss) << fmt.aShift) | + (( r >> fmt.rLoss) << fmt.rShift) | + (( g >> fmt.gLoss) << fmt.gShift) | + (( b >> fmt.bLoss) << fmt.bShift); +} + +inline uint32 ARGBToColor(uint8 a, uint8 r, uint8 g, uint8 b, const PixelFormat &fmt) { + return + ((a >> fmt.aLoss) << fmt.aShift) | + ((r >> fmt.rLoss) << fmt.rShift) | + ((g >> fmt.gLoss) << fmt.gShift) | + ((b >> fmt.bLoss) << fmt.bShift); +} + +inline void colorToRGB(uint32 color, uint8 &r, uint8 &g, uint8 &b, const PixelFormat &fmt) { + r = ((color >> fmt.rShift) << fmt.rLoss) & 0xFF; + g = ((color >> fmt.gShift) << fmt.gLoss) & 0xFF; + b = ((color >> fmt.bShift) << fmt.bLoss) & 0xFF; +} + +inline void colorToARGB(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b, const PixelFormat &fmt) { + a = ((color >> fmt.aShift) << fmt.aLoss) & 0xFF; + r = ((color >> fmt.rShift) << fmt.rLoss) & 0xFF; + g = ((color >> fmt.gShift) << fmt.gLoss) & 0xFF; + b = ((color >> fmt.bShift) << fmt.bLoss) & 0xFF; +} + +} // end of namespace Graphics + #endif |