PVRTTexture.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /******************************************************************************
  2. @File PVRTTexture.cpp
  3. @Title PVRTTexture
  4. @Version
  5. @Copyright Copyright (C) Imagination Technologies Limited.
  6. @Platform ANSI compatible
  7. @Description Texture loading.
  8. ******************************************************************************/
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "PVRTTexture.h"
  12. /*****************************************************************************
  13. ** Functions
  14. *****************************************************************************/
  15. /*!***************************************************************************
  16. @Function PVRTTextureLoadTiled
  17. @Modified pDst Texture to place the tiled data
  18. @Input nWidthDst Width of destination texture
  19. @Input nHeightDst Height of destination texture
  20. @Input pSrc Texture to tile
  21. @Input nWidthSrc Width of source texture
  22. @Input nHeightSrc Height of source texture
  23. @Input nElementSize Bytes per pixel
  24. @Input bTwiddled True if the data is twiddled
  25. @Description Needed by PVRTTextureTile() in the various PVRTTextureAPIs
  26. *****************************************************************************/
  27. void PVRTTextureLoadTiled(
  28. PVRTuint8 * const pDst,
  29. const unsigned int nWidthDst,
  30. const unsigned int nHeightDst,
  31. const PVRTuint8 * const pSrc,
  32. const unsigned int nWidthSrc,
  33. const unsigned int nHeightSrc,
  34. const unsigned int nElementSize,
  35. const bool bTwiddled)
  36. {
  37. unsigned int nXs, nYs;
  38. unsigned int nXd, nYd;
  39. unsigned int nIdxSrc, nIdxDst;
  40. for(nIdxDst = 0; nIdxDst < nWidthDst*nHeightDst; ++nIdxDst)
  41. {
  42. if(bTwiddled)
  43. {
  44. PVRTTextureDeTwiddle(nXd, nYd, nIdxDst);
  45. }
  46. else
  47. {
  48. nXd = nIdxDst % nWidthDst;
  49. nYd = nIdxDst / nWidthDst;
  50. }
  51. nXs = nXd % nWidthSrc;
  52. nYs = nYd % nHeightSrc;
  53. if(bTwiddled)
  54. {
  55. PVRTTextureTwiddle(nIdxSrc, nXs, nYs);
  56. }
  57. else
  58. {
  59. nIdxSrc = nYs * nWidthSrc + nXs;
  60. }
  61. memcpy(pDst + nIdxDst*nElementSize, pSrc + nIdxSrc*nElementSize, nElementSize);
  62. }
  63. }
  64. /*!***************************************************************************
  65. @Function PVRTTextureCreate
  66. @Input w Size of the texture
  67. @Input h Size of the texture
  68. @Input wMin Minimum size of a texture level
  69. @Input hMin Minimum size of a texture level
  70. @Input nBPP Bits per pixel of the format
  71. @Input bMIPMap Create memory for MIP-map levels also?
  72. @Return Allocated texture memory (must be free()d)
  73. @Description Creates a PVR_Texture_Header structure, including room for
  74. the specified texture, in memory.
  75. *****************************************************************************/
  76. PVR_Texture_Header *PVRTTextureCreate(
  77. const unsigned int w,
  78. const unsigned int h,
  79. const unsigned int wMin,
  80. const unsigned int hMin,
  81. const unsigned int nBPP,
  82. const bool bMIPMap)
  83. {
  84. size_t len;
  85. unsigned char *p;
  86. {
  87. unsigned int wTmp = w, hTmp = h;
  88. len = 0;
  89. do
  90. {
  91. len += PVRT_MAX(wTmp, wMin) * PVRT_MAX(hTmp, hMin);
  92. wTmp >>= 1;
  93. hTmp >>= 1;
  94. }
  95. while(bMIPMap && (wTmp || hTmp));
  96. }
  97. len = (len * nBPP) / 8;
  98. len += sizeof(PVR_Texture_Header);
  99. p = (unsigned char*)malloc(len);
  100. _ASSERT(p);
  101. if(p)
  102. {
  103. PVR_Texture_Header * const psTexHeader = (PVR_Texture_Header*)p;
  104. psTexHeader->dwHeaderSize = sizeof(PVR_Texture_Header);
  105. psTexHeader->dwWidth = w;
  106. psTexHeader->dwHeight = h;
  107. psTexHeader->dwMipMapCount = 0;
  108. psTexHeader->dwpfFlags = 0;
  109. psTexHeader->dwTextureDataSize = (PVRTuint32)(len - sizeof(PVR_Texture_Header));
  110. psTexHeader->dwBitCount = nBPP;
  111. psTexHeader->dwRBitMask = 0;
  112. psTexHeader->dwGBitMask = 0;
  113. psTexHeader->dwBBitMask = 0;
  114. psTexHeader->dwAlphaBitMask = 0;
  115. psTexHeader->dwPVR = 0;
  116. psTexHeader->dwNumSurfs = 1;
  117. return psTexHeader;
  118. }
  119. else
  120. {
  121. return 0;
  122. }
  123. }
  124. /*!***************************************************************************
  125. @Function PVRTTextureTwiddle
  126. @Output a Twiddled value
  127. @Input u Coordinate axis 0
  128. @Input v Coordinate axis 1
  129. @Description Combine a 2D coordinate into a twiddled value
  130. *****************************************************************************/
  131. void PVRTTextureTwiddle(unsigned int &a, const unsigned int u, const unsigned int v)
  132. {
  133. _ASSERT(!((u|v) & 0xFFFF0000));
  134. a = 0;
  135. for(int i = 0; i < 16; ++i)
  136. {
  137. a |= ((u & (1 << i)) << (i+1));
  138. a |= ((v & (1 << i)) << (i+0));
  139. }
  140. }
  141. /*!***************************************************************************
  142. @Function PVRTTextureDeTwiddle
  143. @Output u Coordinate axis 0
  144. @Output v Coordinate axis 1
  145. @Input a Twiddled value
  146. @Description Extract 2D coordinates from a twiddled value.
  147. *****************************************************************************/
  148. void PVRTTextureDeTwiddle(unsigned int &u, unsigned int &v, const unsigned int a)
  149. {
  150. u = 0;
  151. v = 0;
  152. for(int i = 0; i < 16; ++i)
  153. {
  154. u |= (a & (1 << ((2*i)+1))) >> (i+1);
  155. v |= (a & (1 << ((2*i)+0))) >> (i+0);
  156. }
  157. }
  158. /*****************************************************************************
  159. End of file (PVRTTexture.cpp)
  160. *****************************************************************************/