ETC.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. #define ETC_LIB_TEXGENPACK 1 // only decompression, faster than ETC_LIB_RG and ETC_LIB_ETCPACK
  4. #define ETC_LIB_RG 2 // only ETC1
  5. #define ETC_LIB_ETCPACK 3
  6. #define ETC1_DEC ETC_LIB_TEXGENPACK
  7. #define ETC2_DEC ETC_LIB_TEXGENPACK
  8. #include "../../../../ThirdPartyLibs/begin.h"
  9. #if ETC1_DEC==ETC_LIB_RG
  10. #include "../../../../ThirdPartyLibs/RG-ETC1/rg_etc1.cpp"
  11. #endif
  12. #if ETC1_DEC==ETC_LIB_TEXGENPACK || ETC2_DEC==ETC_LIB_TEXGENPACK
  13. namespace TGP
  14. {
  15. #include "../../../../ThirdPartyLibs/TexGenPack/etc2.c"
  16. }
  17. #endif
  18. #include "../../../../ThirdPartyLibs/end.h"
  19. /******************************************************************************/
  20. namespace EE{
  21. /******************************************************************************/
  22. Bool (*CompressETC)(C Image &src, Image &dest, Int quality, Bool perceptual);
  23. /******************************************************************************/
  24. #if ETC1_DEC==ETC_LIB_RG
  25. void DecompressBlockETC1(C Byte *b, Color (&block)[4][4]) {rg_etc1::unpack_etc1_block(b, (UInt*)block);}
  26. #elif ETC1_DEC==ETC_LIB_TEXGENPACK
  27. void DecompressBlockETC1(C Byte *b, Color (&block)[4][4]) { TGP::draw_block4x4_etc1(b, (UInt*)block);}
  28. #elif ETC1_DEC==ETC_LIB_ETCPACK
  29. void DecompressBlockETC1(C Byte *b, Color (&block)[4][4])
  30. {
  31. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  32. ETCPACK::decompressBlockDiffFlipC(b0, b1, block[0][0].c, 4, 4, 0, 0, 4);
  33. FREPD(y, 4)FREPD(x, 4)block[y][x].a=255;
  34. }
  35. #endif
  36. #if ETC2_DEC==ETC_LIB_TEXGENPACK
  37. void DecompressBlockETC2 (C Byte *b, Color (&block)[4][4]) {TGP::draw_block4x4_etc2_rgb8 (b, (UInt*)block);}
  38. void DecompressBlockETC2A1(C Byte *b, Color (&block)[4][4]) {TGP::draw_block4x4_etc2_punchthrough(b, (UInt*)block);}
  39. void DecompressBlockETC2A8(C Byte *b, Color (&block)[4][4]) {TGP::draw_block4x4_etc2_eac (b, (UInt*)block);}
  40. #endif
  41. #if ETC2_DEC==ETC_LIB_ETCPACK
  42. void DecompressBlockETC2(C Byte *b, Color (&block)[4][4])
  43. {
  44. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  45. ETCPACK::decompressBlockETC2c(b0, b1, block[0][0].c, 4, 4, 0, 0, 4);
  46. FREPD(y, 4)FREPD(x, 4)block[y][x].a=255;
  47. }
  48. void DecompressBlockETC2A1(C Byte *b, Color (&block)[4][4])
  49. {
  50. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  51. ETCPACK::decompressBlockETC21BitAlphaC(b0, b1, block[0][0].c, null, 4, 4, 0, 0, 4);
  52. }
  53. void DecompressBlockETC2A8(C Byte *b, Color (&block)[4][4])
  54. {
  55. UInt b0=((UInt*)b)[2], b1=((UInt*)b)[3]; SwapEndian(b0); SwapEndian(b1);
  56. ETCPACK::decompressBlockETC2c ( b0, b1, block[0][0].c, 4, 4, 0, 0, 4);
  57. ETCPACK::decompressBlockAlphaC((Byte*)b, &block[0][0].a, 4, 4, 0, 0, 4);
  58. }
  59. #endif
  60. void DecompressBlockETC1(C Byte *b, Color *dest, Int pitch)
  61. {
  62. #if ETC1_DEC==ETC_LIB_ETCPACK
  63. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  64. ETCPACK::decompressBlockDiffFlipC(b0, b1, dest->c, pitch/4, 4, 0, 0, 4);
  65. FREPD(y, 4) // move in forward order so 'dest' can be increased by pitch
  66. {
  67. FREPD(x, 4)dest[x].a=255;
  68. dest=(Color*)((Byte*)dest+pitch);
  69. }
  70. #else
  71. Color block[4][4];
  72. #if ETC1_DEC==ETC_LIB_RG
  73. rg_etc1::unpack_etc1_block(b, (UInt*)block);
  74. #elif ETC1_DEC==ETC_LIB_TEXGENPACK
  75. TGP::draw_block4x4_etc1(b, (UInt*)block);
  76. #endif
  77. FREPD(y, 4) // move in forward order so 'dest' can be increased by pitch
  78. {
  79. CopyFast(dest, block[y], SIZE(Color)*4);
  80. dest=(Color*)((Byte*)dest+pitch);
  81. }
  82. #endif
  83. }
  84. void DecompressBlockETC2(C Byte *b, Color *dest, Int pitch)
  85. {
  86. #if ETC2_DEC==ETC_LIB_ETCPACK
  87. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  88. ETCPACK::decompressBlockETC2c(b0, b1, dest->c, pitch/4, 4, 0, 0, 4);
  89. FREPD(y, 4) // move in forward order so 'dest' can be increased by pitch
  90. {
  91. FREPD(x, 4)dest[x].a=255;
  92. dest=(Color*)((Byte*)dest+pitch);
  93. }
  94. #else
  95. Color block[4][4];
  96. #if ETC2_DEC==ETC_LIB_TEXGENPACK
  97. TGP::draw_block4x4_etc2_rgb8(b, (UInt*)block);
  98. #endif
  99. FREPD(y, 4) // move in forward order so 'dest' can be increased by pitch
  100. {
  101. CopyFast(dest, block[y], SIZE(Color)*4);
  102. dest=(Color*)((Byte*)dest+pitch);
  103. }
  104. #endif
  105. }
  106. void DecompressBlockETC2A1(C Byte *b, Color *dest, Int pitch)
  107. {
  108. #if ETC2_DEC==ETC_LIB_ETCPACK
  109. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  110. ETCPACK::decompressBlockETC21BitAlphaC(b0, b1, dest->c, null, pitch/4, 4, 0, 0, 4);
  111. #else
  112. Color block[4][4];
  113. #if ETC2_DEC==ETC_LIB_TEXGENPACK
  114. TGP::draw_block4x4_etc2_punchthrough(b, (UInt*)block);
  115. #endif
  116. FREPD(y, 4) // move in forward order so 'dest' can be increased by pitch
  117. {
  118. CopyFast(dest, block[y], SIZE(Color)*4);
  119. dest=(Color*)((Byte*)dest+pitch);
  120. }
  121. #endif
  122. }
  123. void DecompressBlockETC2A8(C Byte *b, Color *dest, Int pitch)
  124. {
  125. #if ETC2_DEC==ETC_LIB_ETCPACK
  126. UInt b0=((UInt*)b)[2], b1=((UInt*)b)[3]; SwapEndian(b0); SwapEndian(b1); pitch/=4;
  127. ETCPACK::decompressBlockETC2c ( b0, b1, dest->c, pitch, 4, 0, 0, 4);
  128. ETCPACK::decompressBlockAlphaC((Byte*)b, &dest->a, pitch, 4, 0, 0, 4);
  129. #else
  130. Color block[4][4];
  131. #if ETC2_DEC==ETC_LIB_TEXGENPACK
  132. TGP::draw_block4x4_etc2_eac(b, (UInt*)block);
  133. #endif
  134. FREPD(y, 4) // move in forward order so 'dest' can be increased by pitch
  135. {
  136. CopyFast(dest, block[y], SIZE(Color)*4);
  137. dest=(Color*)((Byte*)dest+pitch);
  138. }
  139. #endif
  140. }
  141. /******************************************************************************/
  142. Color DecompressPixelETC1(C Byte *b, Int x, Int y)
  143. {
  144. Color rgba[4][4];
  145. #if ETC1_DEC==ETC_LIB_RG
  146. rg_etc1::unpack_etc1_block(b, (UInt*)rgba);
  147. #elif ETC1_DEC==ETC_LIB_TEXGENPACK
  148. TGP::draw_block4x4_etc1(b, (UInt*)rgba);
  149. #elif ETC1_DEC==ETC_LIB_ETCPACK
  150. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  151. ETCPACK::decompressBlockDiffFlipC(b0, b1, rgba[0][0].c, 4, 4, 0, 0, 4);
  152. rgba[y][x].a=255;
  153. #endif
  154. return rgba[y][x];
  155. }
  156. Color DecompressPixelETC2(C Byte *b, Int x, Int y)
  157. {
  158. Color rgba[4][4];
  159. #if ETC2_DEC==ETC_LIB_TEXGENPACK
  160. TGP::draw_block4x4_etc2_rgb8(b, (UInt*)rgba);
  161. #elif ETC2_DEC==ETC_LIB_ETCPACK
  162. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  163. ETCPACK::decompressBlockETC2c(b0, b1, rgba[0][0].c, 4, 4, 0, 0, 4);
  164. rgba[y][x].a=255;
  165. #endif
  166. return rgba[y][x];
  167. }
  168. Color DecompressPixelETC2A1(C Byte *b, Int x, Int y)
  169. {
  170. Color rgba[4][4];
  171. #if ETC2_DEC==ETC_LIB_TEXGENPACK
  172. TGP::draw_block4x4_etc2_punchthrough(b, (UInt*)rgba);
  173. #elif ETC2_DEC==ETC_LIB_ETCPACK
  174. UInt b0=((UInt*)b)[0], b1=((UInt*)b)[1]; SwapEndian(b0); SwapEndian(b1);
  175. ETCPACK::decompressBlockETC21BitAlphaC(b0, b1, rgba[0][0].c, null, 4, 4, 0, 0, 4);
  176. #endif
  177. return rgba[y][x];
  178. }
  179. Color DecompressPixelETC2A8(C Byte *b, Int x, Int y)
  180. {
  181. Color rgba[4][4];
  182. #if ETC2_DEC==ETC_LIB_TEXGENPACK
  183. TGP::draw_block4x4_etc2_eac(b, (UInt*)rgba);
  184. #elif ETC2_DEC==ETC_LIB_ETCPACK
  185. UInt b0=((UInt*)b)[2], b1=((UInt*)b)[3]; SwapEndian(b0); SwapEndian(b1);
  186. ETCPACK::decompressBlockETC2c ( b0, b1, rgba[0][0].c, 4, 4, 0, 0, 4);
  187. ETCPACK::decompressBlockAlphaC((Byte*)b, &rgba[0][0].a, 4, 4, 0, 0, 4);
  188. #endif
  189. return rgba[y][x];
  190. }
  191. /******************************************************************************/
  192. }
  193. /******************************************************************************/