bitmapPvr.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #include "graphics/gBitmap.h"
  23. #include "graphics/gPalette.h"
  24. #include "io/stream.h"
  25. #include "platform/platform.h"
  26. // PVR definitions stolen from CPVRTextureHeader.h, which is distributed as part of the PowerVR SDK.
  27. // ---------------------------------------------------------------------------------------------
  28. struct PVRTextureHeaderV2
  29. {
  30. unsigned int dwHeaderSize; /* size of the structure */
  31. unsigned int dwHeight; /* height of surface to be created */
  32. unsigned int dwWidth; /* width of input surface */
  33. unsigned int dwMipMapCount; /* number of mip-map levels requested */
  34. unsigned int dwpfFlags; /* pixel format flags */
  35. unsigned int dwDataSize; /* Size of the compress data */
  36. unsigned int dwBitCount; /* number of bits per pixel */
  37. unsigned int dwRBitMask; /* mask for red bit */
  38. unsigned int dwGBitMask; /* mask for green bits */
  39. unsigned int dwBBitMask; /* mask for blue bits */
  40. unsigned int dwAlphaBitMask; /* mask for alpha channel */
  41. unsigned int dwPVR; /* should be 'P' 'V' 'R' '!' */
  42. unsigned int dwNumSurfs; /* number of slices for volume textures or skyboxes */
  43. };
  44. enum PixelType
  45. {
  46. MGLPT_ARGB_4444 = 0x00,
  47. MGLPT_ARGB_1555,
  48. MGLPT_RGB_565,
  49. MGLPT_RGB_555,
  50. MGLPT_RGB_888,
  51. MGLPT_ARGB_8888,
  52. MGLPT_ARGB_8332,
  53. MGLPT_I_8,
  54. MGLPT_AI_88,
  55. MGLPT_1_BPP,
  56. MGLPT_VY1UY0,
  57. MGLPT_Y1VY0U,
  58. MGLPT_PVRTC2,
  59. MGLPT_PVRTC4,
  60. MGLPT_PVRTC2_2,
  61. MGLPT_PVRTC2_4,
  62. OGL_RGBA_4444= 0x10,
  63. OGL_RGBA_5551,
  64. OGL_RGBA_8888,
  65. OGL_RGB_565,
  66. OGL_RGB_555,
  67. OGL_RGB_888,
  68. OGL_I_8,
  69. OGL_AI_88,
  70. OGL_PVRTC2,
  71. OGL_PVRTC4,
  72. // OGL_BGRA_8888 extension
  73. OGL_BGRA_8888,
  74. D3D_DXT1 = 0x20,
  75. D3D_DXT2,
  76. D3D_DXT3,
  77. D3D_DXT4,
  78. D3D_DXT5,
  79. D3D_RGB_332,
  80. D3D_AI_44,
  81. D3D_LVU_655,
  82. D3D_XLVU_8888,
  83. D3D_QWVU_8888,
  84. //10 bits per channel
  85. D3D_ABGR_2101010,
  86. D3D_ARGB_2101010,
  87. D3D_AWVU_2101010,
  88. //16 bits per channel
  89. D3D_GR_1616,
  90. D3D_VU_1616,
  91. D3D_ABGR_16161616,
  92. //HDR formats
  93. D3D_R16F,
  94. D3D_GR_1616F,
  95. D3D_ABGR_16161616F,
  96. //32 bits per channel
  97. D3D_R32F,
  98. D3D_GR_3232F,
  99. D3D_ABGR_32323232F,
  100. // Ericsson
  101. ETC_RGB_4BPP,
  102. ETC_RGBA_EXPLICIT,
  103. ETC_RGBA_INTERPOLATED,
  104. // DX10
  105. ePT_DX10_R32G32B32A32_FLOAT= 0x50,
  106. ePT_DX10_R32G32B32A32_UINT ,
  107. ePT_DX10_R32G32B32A32_SINT,
  108. ePT_DX10_R32G32B32_FLOAT,
  109. ePT_DX10_R32G32B32_UINT,
  110. ePT_DX10_R32G32B32_SINT,
  111. ePT_DX10_R16G16B16A16_FLOAT ,
  112. ePT_DX10_R16G16B16A16_UNORM,
  113. ePT_DX10_R16G16B16A16_UINT ,
  114. ePT_DX10_R16G16B16A16_SNORM ,
  115. ePT_DX10_R16G16B16A16_SINT ,
  116. ePT_DX10_R32G32_FLOAT ,
  117. ePT_DX10_R32G32_UINT ,
  118. ePT_DX10_R32G32_SINT ,
  119. ePT_DX10_R10G10B10A2_UNORM ,
  120. ePT_DX10_R10G10B10A2_UINT ,
  121. ePT_DX10_R11G11B10_FLOAT ,
  122. ePT_DX10_R8G8B8A8_UNORM ,
  123. ePT_DX10_R8G8B8A8_UNORM_SRGB ,
  124. ePT_DX10_R8G8B8A8_UINT ,
  125. ePT_DX10_R8G8B8A8_SNORM ,
  126. ePT_DX10_R8G8B8A8_SINT ,
  127. ePT_DX10_R16G16_FLOAT ,
  128. ePT_DX10_R16G16_UNORM ,
  129. ePT_DX10_R16G16_UINT ,
  130. ePT_DX10_R16G16_SNORM ,
  131. ePT_DX10_R16G16_SINT ,
  132. ePT_DX10_R32_FLOAT ,
  133. ePT_DX10_R32_UINT ,
  134. ePT_DX10_R32_SINT ,
  135. ePT_DX10_R8G8_UNORM ,
  136. ePT_DX10_R8G8_UINT ,
  137. ePT_DX10_R8G8_SNORM ,
  138. ePT_DX10_R8G8_SINT ,
  139. ePT_DX10_R16_FLOAT ,
  140. ePT_DX10_R16_UNORM ,
  141. ePT_DX10_R16_UINT ,
  142. ePT_DX10_R16_SNORM ,
  143. ePT_DX10_R16_SINT ,
  144. ePT_DX10_R8_UNORM,
  145. ePT_DX10_R8_UINT,
  146. ePT_DX10_R8_SNORM,
  147. ePT_DX10_R8_SINT,
  148. ePT_DX10_A8_UNORM,
  149. ePT_DX10_R1_UNORM,
  150. ePT_DX10_R9G9B9E5_SHAREDEXP,
  151. ePT_DX10_R8G8_B8G8_UNORM,
  152. ePT_DX10_G8R8_G8B8_UNORM,
  153. ePT_DX10_BC1_UNORM,
  154. ePT_DX10_BC1_UNORM_SRGB,
  155. ePT_DX10_BC2_UNORM,
  156. ePT_DX10_BC2_UNORM_SRGB,
  157. ePT_DX10_BC3_UNORM,
  158. ePT_DX10_BC3_UNORM_SRGB,
  159. ePT_DX10_BC4_UNORM,
  160. ePT_DX10_BC4_SNORM,
  161. ePT_DX10_BC5_UNORM,
  162. ePT_DX10_BC5_SNORM,
  163. //ePT_DX10_B5G6R5_UNORM, // defined but obsolete - won't actually load in DX10
  164. //ePT_DX10_B5G5R5A1_UNORM,
  165. //ePT_DX10_B8G8R8A8_UNORM,
  166. //ePT_DX10_B8G8R8X8_UNORM,
  167. // OpenVG
  168. /* RGB{A,X} channel ordering */
  169. ePT_VG_sRGBX_8888 = 0x90,
  170. ePT_VG_sRGBA_8888,
  171. ePT_VG_sRGBA_8888_PRE,
  172. ePT_VG_sRGB_565,
  173. ePT_VG_sRGBA_5551,
  174. ePT_VG_sRGBA_4444,
  175. ePT_VG_sL_8,
  176. ePT_VG_lRGBX_8888,
  177. ePT_VG_lRGBA_8888,
  178. ePT_VG_lRGBA_8888_PRE,
  179. ePT_VG_lL_8,
  180. ePT_VG_A_8,
  181. ePT_VG_BW_1,
  182. /* {A,X}RGB channel ordering */
  183. ePT_VG_sXRGB_8888,
  184. ePT_VG_sARGB_8888,
  185. ePT_VG_sARGB_8888_PRE,
  186. ePT_VG_sARGB_1555,
  187. ePT_VG_sARGB_4444,
  188. ePT_VG_lXRGB_8888,
  189. ePT_VG_lARGB_8888,
  190. ePT_VG_lARGB_8888_PRE,
  191. /* BGR{A,X} channel ordering */
  192. ePT_VG_sBGRX_8888,
  193. ePT_VG_sBGRA_8888,
  194. ePT_VG_sBGRA_8888_PRE,
  195. ePT_VG_sBGR_565,
  196. ePT_VG_sBGRA_5551,
  197. ePT_VG_sBGRA_4444,
  198. ePT_VG_lBGRX_8888,
  199. ePT_VG_lBGRA_8888,
  200. ePT_VG_lBGRA_8888_PRE,
  201. /* {A,X}BGR channel ordering */
  202. ePT_VG_sXBGR_8888,
  203. ePT_VG_sABGR_8888 ,
  204. ePT_VG_sABGR_8888_PRE,
  205. ePT_VG_sABGR_1555,
  206. ePT_VG_sABGR_4444,
  207. ePT_VG_lXBGR_8888,
  208. ePT_VG_lABGR_8888,
  209. ePT_VG_lABGR_8888_PRE,
  210. // max cap for iterating
  211. END_OF_PIXEL_TYPES,
  212. MGLPT_NOTYPE = 0xff
  213. };
  214. const static unsigned int PVRTEX_MIPMAP = (1<<8);
  215. const static unsigned int PVRTEX_TWIDDLE = (1<<9);
  216. const static unsigned int PVRTEX_BUMPMAP = (1<<10);
  217. const static unsigned int PVRTEX_TILING = (1<<11);
  218. const static unsigned int PVRTEX_CUBEMAP = (1<<12);
  219. const static unsigned int PVRTEX_FALSEMIPCOL = (1<<13);
  220. const static unsigned int PVRTEX_VOLUME = (1<<14);
  221. const static unsigned int PVRTEX_ALPHA = (1<<15);
  222. // legacy
  223. const static unsigned int PVRTEX_IDENTIFIER = 0x21525650; // 'P''V''R''!'
  224. const static unsigned int PVRTEX_PIXELTYPE = 0xff; // pixel type is always in the last 16bits of the flags
  225. // ---------------------------------------------------------------------------------------------
  226. bool GBitmap::readPvr(Stream& stream)
  227. {
  228. PVRTextureHeaderV2 bi;
  229. stream.read( sizeof(PVRTextureHeaderV2), &bi );
  230. byteSize = bi.dwDataSize;
  231. pBits = new U8[byteSize];
  232. stream.read( byteSize, pBits );
  233. width = bi.dwHeight;
  234. height = bi.dwWidth;
  235. if (bi.dwBitCount == 2) {
  236. if ((bi.dwpfFlags & PVRTEX_ALPHA) == 0) {
  237. internalFormat = PVR2;
  238. } else {
  239. internalFormat = PVR2A;
  240. }
  241. } else { // if (bi.dwBitCount == 4) {
  242. if ((bi.dwpfFlags & PVRTEX_ALPHA) == 0) {
  243. internalFormat = PVR4;
  244. } else {
  245. internalFormat = PVR4A;
  246. }
  247. }
  248. bytesPerPixel = 0; // no provision for sub 1 bytesPerPixel, so set it to 0
  249. numMipLevels = 1; //bi.dwMipMapCount;
  250. mipLevelOffsets[0] = 0;
  251. pPalette = NULL;
  252. return true;
  253. }
  254. bool GBitmap::writePvr(Stream& io_rStream) const
  255. {
  256. return false; // can't write PVR files
  257. }