gfxFormatUtils.h 4.3 KB

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