bitmapPvr.cc 7.7 KB

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