s3tc.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies
  3. http://www.cocos2d-x.org
  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 deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "base/s3tc.h"
  21. //Decode S3TC encode block to 4x4 RGB32 pixels
  22. static void s3tc_decode_block(uint8_t **blockData,
  23. uint32_t *decodeBlockData,
  24. unsigned int stride,
  25. bool oneBitAlphaFlag,
  26. uint64_t alpha,
  27. S3TCDecodeFlag decodeFlag)
  28. {
  29. unsigned int colorValue0 = 0 , colorValue1 = 0, initAlpha = (!oneBitAlphaFlag * 255u) << 24;
  30. unsigned int rb0 = 0, rb1 = 0, rb2 = 0, rb3 = 0, g0 = 0, g1 = 0, g2 = 0, g3 = 0;
  31. uint32_t colors[4], pixelsIndex = 0;
  32. /* load the two color values*/
  33. memcpy((void *)&colorValue0, *blockData, 2);
  34. (*blockData) += 2;
  35. memcpy((void *)&colorValue1, *blockData, 2);
  36. (*blockData) += 2;
  37. /* the channel is r5g6b5 , 16 bits */
  38. rb0 = (colorValue0 << 19 | colorValue0 >> 8) & 0xf800f8;
  39. rb1 = (colorValue1 << 19 | colorValue1 >> 8) & 0xf800f8;
  40. g0 = (colorValue0 << 5) & 0x00fc00;
  41. g1 = (colorValue1 << 5) & 0x00fc00;
  42. g0 += (g0 >> 6) & 0x000300;
  43. g1 += (g1 >> 6) & 0x000300;
  44. colors[0] = rb0 + g0 + initAlpha;
  45. colors[1] = rb1 + g1 + initAlpha;
  46. /* interpolate the other two color values */
  47. if (colorValue0 > colorValue1 || oneBitAlphaFlag)
  48. {
  49. rb2 = (((2*rb0 + rb1) * 21) >> 6) & 0xff00ff;
  50. rb3 = (((2*rb1 + rb0) * 21) >> 6) & 0xff00ff;
  51. g2 = (((2*g0 + g1 ) * 21) >> 6) & 0x00ff00;
  52. g3 = (((2*g1 + g0 ) * 21) >> 6) & 0x00ff00;
  53. colors[3] = rb3 + g3 + initAlpha;
  54. }
  55. else
  56. {
  57. rb2 = ((rb0+rb1) >> 1) & 0xff00ff;
  58. g2 = ((g0 +g1 ) >> 1) & 0x00ff00;
  59. colors[3] = 0 ;
  60. }
  61. colors[2] = rb2 + g2 + initAlpha;
  62. /*read the pixelsIndex , 2bits per pixel, 4 bytes */
  63. memcpy((void*)&pixelsIndex, *blockData, 4);
  64. (*blockData) += 4;
  65. if (S3TCDecodeFlag::DXT5 == decodeFlag)
  66. {
  67. //dxt5 use interpolate alpha
  68. // 8-Alpha block: derive the other six alphas.
  69. // Bit code 000 = alpha0, 001 = alpha1, other are interpolated.
  70. unsigned int alphaArray[8];
  71. alphaArray[0] = (alpha ) & 0xff ;
  72. alphaArray[1] = (alpha >> 8) & 0xff ;
  73. if (alphaArray[0] >= alphaArray[1])
  74. {
  75. alphaArray[2] = (alphaArray[0]*6 + alphaArray[1]*1) / 7;
  76. alphaArray[3] = (alphaArray[0]*5 + alphaArray[1]*2) / 7;
  77. alphaArray[4] = (alphaArray[0]*4 + alphaArray[1]*3) / 7;
  78. alphaArray[5] = (alphaArray[0]*3 + alphaArray[1]*4) / 7;
  79. alphaArray[6] = (alphaArray[0]*2 + alphaArray[1]*5) / 7;
  80. alphaArray[7] = (alphaArray[0]*1 + alphaArray[1]*6) / 7;
  81. }
  82. else if (alphaArray[0] < alphaArray[1])
  83. {
  84. alphaArray[2] = (alphaArray[0]*4 + alphaArray[1]*1) / 5;
  85. alphaArray[3] = (alphaArray[0]*3 + alphaArray[1]*2) / 5;
  86. alphaArray[4] = (alphaArray[0]*2 + alphaArray[1]*3) / 5;
  87. alphaArray[5] = (alphaArray[0]*1 + alphaArray[1]*4) / 5;
  88. alphaArray[6] = 0;
  89. alphaArray[7] = 255;
  90. }
  91. // read the flowing 48bit indices (16*3)
  92. alpha >>= 16;
  93. for (int y = 0; y < 4; ++y)
  94. {
  95. for (int x = 0; x < 4; ++x)
  96. {
  97. decodeBlockData[x] = (alphaArray[alpha & 5] << 24) + colors[pixelsIndex & 3];
  98. pixelsIndex >>= 2;
  99. alpha >>= 3;
  100. }
  101. decodeBlockData += stride;
  102. }
  103. } //if (dxt5 == comFlag)
  104. else
  105. { //dxt1 dxt3 use explicit alpha
  106. for (int y = 0; y < 4; ++y)
  107. {
  108. for (int x = 0; x < 4; ++x)
  109. {
  110. initAlpha = (static_cast<int>(alpha) & 0x0f) << 28;
  111. initAlpha += initAlpha >> 4;
  112. decodeBlockData[x] = initAlpha + colors[pixelsIndex & 3];
  113. pixelsIndex >>= 2;
  114. alpha >>= 4;
  115. }
  116. decodeBlockData += stride;
  117. }
  118. }
  119. }
  120. //Decode S3TC encode data to RGB32
  121. void s3tc_decode(uint8_t *encodeData, //in_data
  122. uint8_t *decodeData, //out_data
  123. const int pixelsWidth,
  124. const int pixelsHeight,
  125. S3TCDecodeFlag decodeFlag)
  126. {
  127. uint32_t *decodeBlockData = (uint32_t *)decodeData;
  128. for (int block_y = 0; block_y < pixelsHeight / 4; ++block_y, decodeBlockData += 3 * pixelsWidth) //stride = 3*width
  129. {
  130. for(int block_x = 0; block_x < pixelsWidth / 4; ++block_x, decodeBlockData += 4) //skip 4 pixels
  131. {
  132. uint64_t blockAlpha = 0;
  133. switch (decodeFlag)
  134. {
  135. case S3TCDecodeFlag::DXT1:
  136. {
  137. s3tc_decode_block(&encodeData, decodeBlockData, pixelsWidth, 0, 0LL, S3TCDecodeFlag::DXT1);
  138. }
  139. break;
  140. case S3TCDecodeFlag::DXT3:
  141. {
  142. memcpy((void *)&blockAlpha, encodeData, 8);
  143. encodeData += 8;
  144. s3tc_decode_block(&encodeData, decodeBlockData, pixelsWidth, 1, blockAlpha, S3TCDecodeFlag::DXT3);
  145. }
  146. break;
  147. case S3TCDecodeFlag::DXT5:
  148. {
  149. memcpy((void *)&blockAlpha, encodeData, 8);
  150. encodeData += 8;
  151. s3tc_decode_block(&encodeData, decodeBlockData, pixelsWidth, 1, blockAlpha, S3TCDecodeFlag::DXT5);
  152. }
  153. break;
  154. default:
  155. break;
  156. }//switch
  157. }//for block_x
  158. }//for block_y
  159. }