gfxFormatUtils.h
Engine/source/gfx/gfxFormatUtils.h
Classes:
class
Some information about a GFXFormat.
class
Public Functions
Detailed Description
Public Functions
GFXPackPixel(GFXFormat format, U8 *& ptr, U8 red, U8 green, U8 blue, U8 alpha, bool leastSignficantFirst)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _GFXFORMATUTILS_H_ 25#define _GFXFORMATUTILS_H_ 26 27#ifndef _PLATFORM_H_ 28 #include "platform/platform.h" 29#endif 30#ifndef _GFXENUMS_H_ 31 #include "gfx/gfxEnums.h" 32#endif 33#ifndef _COLOR_H_ 34 #include "core/color.h" 35#endif 36 37 38//WIP: still in early stages 39 40 41 42/// Some information about a GFXFormat. 43struct GFXFormatInfo 44{ 45 protected: 46 47 struct Data 48 { 49 /// Bytes per single pixel. 50 U32 mBytesPerPixel; 51 52 /// If true, format has alpha channel. 53 bool mHasAlpha; 54 55 /// If true, format uses compression. 56 bool mIsCompressed; 57 58 /// If true, channels are in floating-point. 59 bool mIsFloatingPoint; 60 61 Data() :mBytesPerPixel(0), mHasAlpha(false), mIsCompressed(false), mIsFloatingPoint(false) {} 62 Data( U32 bpp, bool hasAlpha = false, bool isCompressed = false, bool isFP = false ) 63 : mBytesPerPixel( bpp ), 64 mHasAlpha( hasAlpha ), 65 mIsCompressed( isCompressed ), 66 mIsFloatingPoint( isFP ) {} 67 }; 68 69 GFXFormat mFormat; 70 71 static Data smFormatInfos[ GFXFormat_COUNT ]; 72 73 public: 74 75 GFXFormatInfo( GFXFormat format ) 76 : mFormat( format ) {} 77 78 /// @return the number of bytes per pixel in this format. 79 /// @note For compressed formats that can't give a fixed value per pixel, 80 /// this will be zero. 81 U32 getBytesPerPixel() const { return smFormatInfos[ mFormat ].mBytesPerPixel; } 82 83 /// @return true if the format has an alpha channel. 84 bool hasAlpha() const { return smFormatInfos[ mFormat ].mHasAlpha; } 85 86 /// @return true if format uses compression. 87 bool isCompressed() const { return smFormatInfos[ mFormat ].mIsCompressed; } 88 89 /// @return true if channels are stored in floating-point format. 90 bool isFloatingPoint() const { return smFormatInfos[ mFormat ].mIsFloatingPoint; } 91}; 92 93 94#if 0 95/// 96extern void U8* fromData, 97 U8* toData ); 98#endif 99 100 101inline void GFXPackPixel( GFXFormat format, U8*& ptr, U8 red, U8 green, U8 blue, U8 alpha, bool leastSignficantFirst = true ) 102{ 103 switch( format ) 104 { 105 case GFXFormatR8G8B8A8: 106 if( leastSignficantFirst ) 107 { 108 ptr[ 0 ] = blue; 109 ptr[ 1 ] = green; 110 ptr[ 2 ] = red; 111 ptr[ 3 ] = alpha; 112 } 113 else 114 { 115 ptr[ 0 ] = red; 116 ptr[ 1 ] = green; 117 ptr[ 2 ] = blue; 118 ptr[ 3 ] = alpha; 119 } 120 ptr += 4; 121 break; 122 123 case GFXFormatR8G8B8: 124 if( leastSignficantFirst ) 125 { 126 ptr[ 0 ] = blue; 127 ptr[ 1 ] = green; 128 ptr[ 2 ] = red; 129 } 130 else 131 { 132 ptr[ 0 ] = red; 133 ptr[ 1 ] = green; 134 ptr[ 2 ] = blue; 135 } 136 ptr += 3; 137 break; 138 139 default: 140 AssertISV( false, "GFXPackPixel() - pixel format not implemented." ); 141 } 142} 143 144#endif // _GFXFORMATUTILS_H_ 145