PVRData.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "PVRData.h"
  2. USING_NS_BF;
  3. struct PVRTextureHeader
  4. {
  5. unsigned int dwHeaderSize;
  6. unsigned int dwHeight;
  7. unsigned int dwWidth;
  8. unsigned int dwMipMapCount;
  9. unsigned int dwpfFlags;
  10. unsigned int dwTextureDataSize;
  11. unsigned int dwBitCount;
  12. unsigned int dwRBitMask;
  13. unsigned int dwGBitMask;
  14. unsigned int dwBBitMask;
  15. unsigned int dwAlphaBitMask;
  16. unsigned int dwPVR;
  17. unsigned int dwNumSurfs;
  18. };
  19. typedef struct TextureMIPLevelData_TAG
  20. {
  21. unsigned char* pData;
  22. unsigned int dwDataSize;
  23. unsigned int dwMIPLevel;
  24. unsigned int dwSizeX;
  25. unsigned int dwSizeY;
  26. } TextureMIPLevelData;
  27. typedef struct TextureImageData_TAG
  28. {
  29. TextureMIPLevelData* pLevels;
  30. int textureFormat;
  31. int textureType;
  32. bool isCompressed;
  33. unsigned int dwMipLevels;
  34. unsigned int dwWidth;
  35. unsigned int dwHeight;
  36. } TextureImageData;
  37. static int DisableTwiddlingRoutine = 0;
  38. static unsigned long TwiddleUV(unsigned long YSize, unsigned long XSize, unsigned long YPos, unsigned long XPos)
  39. {
  40. unsigned long Twiddled;
  41. unsigned long MinDimension;
  42. unsigned long MaxValue;
  43. unsigned long SrcBitPos;
  44. unsigned long DstBitPos;
  45. int ShiftCount;
  46. if(YSize < XSize)
  47. {
  48. MinDimension = YSize;
  49. MaxValue = XPos;
  50. }
  51. else
  52. {
  53. MinDimension = XSize;
  54. MaxValue = YPos;
  55. }
  56. /*
  57. // Nasty hack to disable twiddling
  58. */
  59. if(DisableTwiddlingRoutine)
  60. {
  61. return (YPos* XSize + XPos);
  62. }
  63. /*
  64. // Step through all the bits in the "minimum" dimension
  65. */
  66. SrcBitPos = 1;
  67. DstBitPos = 1;
  68. Twiddled = 0;
  69. ShiftCount = 0;
  70. while(SrcBitPos < MinDimension)
  71. {
  72. if(YPos & SrcBitPos)
  73. {
  74. Twiddled |= DstBitPos;
  75. }
  76. if(XPos & SrcBitPos)
  77. {
  78. Twiddled |= (DstBitPos << 1);
  79. }
  80. SrcBitPos <<= 1;
  81. DstBitPos <<= 2;
  82. ShiftCount += 1;
  83. }/*end while*/
  84. /*
  85. // prepend any unused bits
  86. */
  87. MaxValue >>= ShiftCount;
  88. Twiddled |= (MaxValue << (2*ShiftCount));
  89. return Twiddled;
  90. }
  91. #define PVR_TEXTURE_FLAG_TYPE_MASK 0xFF
  92. #define PVR_TEXTURE_FLAG_TYPE_PVRTC_2 24
  93. #define PVR_TEXTURE_FLAG_TYPE_PVRTC_4 25
  94. bool PVRData::ReadData()
  95. {
  96. PVRTextureHeader* aHeader = (PVRTextureHeader*) (mSrcData);
  97. if (aHeader->dwHeaderSize != 52)
  98. return false;
  99. if ((aHeader->dwpfFlags & PVR_TEXTURE_FLAG_TYPE_MASK) == PVR_TEXTURE_FLAG_TYPE_PVRTC_2)
  100. mHWBitsType = HWBITS_PVRTC_2BPPV1;
  101. else if ((aHeader->dwpfFlags & PVR_TEXTURE_FLAG_TYPE_MASK) == PVR_TEXTURE_FLAG_TYPE_PVRTC_4)
  102. {
  103. if (aHeader->dwNumSurfs > 1)
  104. mHWBitsType = HWBITS_PVRTC_2X4BPPV1;
  105. else
  106. mHWBitsType = HWBITS_PVRTC_4BPPV1;
  107. }
  108. mHWBits = (uint8*)mSrcData + aHeader->dwHeaderSize;
  109. mHWBitsLength = mSrcDataLen - aHeader->dwHeaderSize;
  110. mWidth = aHeader->dwWidth;
  111. mHeight = aHeader->dwHeight;
  112. mKeepSrcDataValid = true;
  113. /*mWidth = aHeader->dwWidth / 4;
  114. mHeight = aHeader->dwHeight / 4;
  115. mBits = new uint32[mWidth*mHeight];
  116. int blockW = aHeader->dwWidth / 4;
  117. int blockH = aHeader->dwHeight / 4;
  118. int numBlocks = blockW * blockH;
  119. for (int blockNum = 0; blockNum < numBlocks; blockNum++)
  120. {
  121. int blockX = (blockNum % blockW);
  122. int blockY = blockH - (blockNum / blockW) - 1;
  123. uint8* srcData = mSrcData + aHeader->dwHeaderSize + TwiddleUV(blockH, blockW, blockY, blockX)*8 + 4;
  124. uint16 baseColorB = *((uint16*) (srcData));
  125. uint32 color = 0xFF000000 |
  126. (((uint32) baseColorB & 0x7C00) >> 7) |
  127. (((uint32) baseColorB & 0x03E0) << 6) |
  128. (((uint32) baseColorB & 0x001F) << 19);
  129. mBits[blockNum] = color;
  130. }*/
  131. return true;
  132. }