atitc.cpp 7.3 KB

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