alpha.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. -------------------------------------------------------------------------- */
  20. #include "alpha.h"
  21. #include <algorithm>
  22. namespace squish {
  23. static int FloatToInt( float a, int limit )
  24. {
  25. // use ANSI round-to-zero behaviour to get round-to-nearest
  26. int i = ( int )( a + 0.5f );
  27. // clamp to the limit
  28. if( i < 0 )
  29. i = 0;
  30. else if( i > limit )
  31. i = limit;
  32. // done
  33. return i;
  34. }
  35. void CompressAlphaDxt3( u8 const* rgba, int mask, void* block )
  36. {
  37. u8* bytes = reinterpret_cast< u8* >( block );
  38. // quantise and pack the alpha values pairwise
  39. for( int i = 0; i < 8; ++i )
  40. {
  41. // quantise down to 4 bits
  42. float alpha1 = ( float )rgba[8*i + 3] * ( 15.0f/255.0f );
  43. float alpha2 = ( float )rgba[8*i + 7] * ( 15.0f/255.0f );
  44. int quant1 = FloatToInt( alpha1, 15 );
  45. int quant2 = FloatToInt( alpha2, 15 );
  46. // set alpha to zero where masked
  47. int bit1 = 1 << ( 2*i );
  48. int bit2 = 1 << ( 2*i + 1 );
  49. if( ( mask & bit1 ) == 0 )
  50. quant1 = 0;
  51. if( ( mask & bit2 ) == 0 )
  52. quant2 = 0;
  53. // pack into the byte
  54. bytes[i] = ( u8 )( quant1 | ( quant2 << 4 ) );
  55. }
  56. }
  57. void DecompressAlphaDxt3( u8* rgba, void const* block )
  58. {
  59. u8 const* bytes = reinterpret_cast< u8 const* >( block );
  60. // unpack the alpha values pairwise
  61. for( int i = 0; i < 8; ++i )
  62. {
  63. // quantise down to 4 bits
  64. u8 quant = bytes[i];
  65. // unpack the values
  66. u8 lo = quant & 0x0f;
  67. u8 hi = quant & 0xf0;
  68. // convert back up to bytes
  69. rgba[8*i + 3] = lo | ( lo << 4 );
  70. rgba[8*i + 7] = hi | ( hi >> 4 );
  71. }
  72. }
  73. static void FixRange( int& min, int& max, int steps )
  74. {
  75. if( max - min < steps )
  76. max = std::min( min + steps, 255 );
  77. if( max - min < steps )
  78. min = std::max( 0, max - steps );
  79. }
  80. static int FitCodes( u8 const* rgba, int mask, u8 const* codes, u8* indices )
  81. {
  82. // fit each alpha value to the codebook
  83. int err = 0;
  84. for( int i = 0; i < 16; ++i )
  85. {
  86. // check this pixel is valid
  87. int bit = 1 << i;
  88. if( ( mask & bit ) == 0 )
  89. {
  90. // use the first code
  91. indices[i] = 0;
  92. continue;
  93. }
  94. // find the least error and corresponding index
  95. int value = rgba[4*i + 3];
  96. int least = 2147483647; //INT_MAX
  97. int index = 0;
  98. for( int j = 0; j < 8; ++j )
  99. {
  100. // get the squared error from this code
  101. int dist = ( int )value - ( int )codes[j];
  102. dist *= dist;
  103. // compare with the best so far
  104. if( dist < least )
  105. {
  106. least = dist;
  107. index = j;
  108. }
  109. }
  110. // save this index and accumulate the error
  111. indices[i] = ( u8 )index;
  112. err += least;
  113. }
  114. // return the total error
  115. return err;
  116. }
  117. static void WriteAlphaBlock( int alpha0, int alpha1, u8 const* indices, void* block )
  118. {
  119. u8* bytes = reinterpret_cast< u8* >( block );
  120. // write the first two bytes
  121. bytes[0] = ( u8 )alpha0;
  122. bytes[1] = ( u8 )alpha1;
  123. // pack the indices with 3 bits each
  124. u8* dest = bytes + 2;
  125. u8 const* src = indices;
  126. for( int i = 0; i < 2; ++i )
  127. {
  128. // pack 8 3-bit values
  129. int value = 0;
  130. for( int j = 0; j < 8; ++j )
  131. {
  132. int index = *src++;
  133. value |= ( index << 3*j );
  134. }
  135. // store in 3 bytes
  136. for( int j = 0; j < 3; ++j )
  137. {
  138. int byte = ( value >> 8*j ) & 0xff;
  139. *dest++ = ( u8 )byte;
  140. }
  141. }
  142. }
  143. static void WriteAlphaBlock5( int alpha0, int alpha1, u8 const* indices, void* block )
  144. {
  145. // check the relative values of the endpoints
  146. if( alpha0 > alpha1 )
  147. {
  148. // swap the indices
  149. u8 swapped[16];
  150. for( int i = 0; i < 16; ++i )
  151. {
  152. u8 index = indices[i];
  153. if( index == 0 )
  154. swapped[i] = 1;
  155. else if( index == 1 )
  156. swapped[i] = 0;
  157. else if( index <= 5 )
  158. swapped[i] = 7 - index;
  159. else
  160. swapped[i] = index;
  161. }
  162. // write the block
  163. WriteAlphaBlock( alpha1, alpha0, swapped, block );
  164. }
  165. else
  166. {
  167. // write the block
  168. WriteAlphaBlock( alpha0, alpha1, indices, block );
  169. }
  170. }
  171. static void WriteAlphaBlock7( int alpha0, int alpha1, u8 const* indices, void* block )
  172. {
  173. // check the relative values of the endpoints
  174. if( alpha0 < alpha1 )
  175. {
  176. // swap the indices
  177. u8 swapped[16];
  178. for( int i = 0; i < 16; ++i )
  179. {
  180. u8 index = indices[i];
  181. if( index == 0 )
  182. swapped[i] = 1;
  183. else if( index == 1 )
  184. swapped[i] = 0;
  185. else
  186. swapped[i] = 9 - index;
  187. }
  188. // write the block
  189. WriteAlphaBlock( alpha1, alpha0, swapped, block );
  190. }
  191. else
  192. {
  193. // write the block
  194. WriteAlphaBlock( alpha0, alpha1, indices, block );
  195. }
  196. }
  197. void CompressAlphaDxt5( u8 const* rgba, int mask, void* block )
  198. {
  199. // get the range for 5-alpha and 7-alpha interpolation
  200. int min5 = 255;
  201. int max5 = 0;
  202. int min7 = 255;
  203. int max7 = 0;
  204. for( int i = 0; i < 16; ++i )
  205. {
  206. // check this pixel is valid
  207. int bit = 1 << i;
  208. if( ( mask & bit ) == 0 )
  209. continue;
  210. // incorporate into the min/max
  211. int value = rgba[4*i + 3];
  212. if( value < min7 )
  213. min7 = value;
  214. if( value > max7 )
  215. max7 = value;
  216. if( value != 0 && value < min5 )
  217. min5 = value;
  218. if( value != 255 && value > max5 )
  219. max5 = value;
  220. }
  221. // handle the case that no valid range was found
  222. if( min5 > max5 )
  223. min5 = max5;
  224. if( min7 > max7 )
  225. min7 = max7;
  226. // fix the range to be the minimum in each case
  227. FixRange( min5, max5, 5 );
  228. FixRange( min7, max7, 7 );
  229. // set up the 5-alpha code book
  230. u8 codes5[8];
  231. codes5[0] = ( u8 )min5;
  232. codes5[1] = ( u8 )max5;
  233. for( int i = 1; i < 5; ++i )
  234. codes5[1 + i] = ( u8 )( ( ( 5 - i )*min5 + i*max5 )/5 );
  235. codes5[6] = 0;
  236. codes5[7] = 255;
  237. // set up the 7-alpha code book
  238. u8 codes7[8];
  239. codes7[0] = ( u8 )min7;
  240. codes7[1] = ( u8 )max7;
  241. for( int i = 1; i < 7; ++i )
  242. codes7[1 + i] = ( u8 )( ( ( 7 - i )*min7 + i*max7 )/7 );
  243. // fit the data to both code books
  244. u8 indices5[16];
  245. u8 indices7[16];
  246. int err5 = FitCodes( rgba, mask, codes5, indices5 );
  247. int err7 = FitCodes( rgba, mask, codes7, indices7 );
  248. // save the block with least error
  249. if( err5 <= err7 )
  250. WriteAlphaBlock5( min5, max5, indices5, block );
  251. else
  252. WriteAlphaBlock7( min7, max7, indices7, block );
  253. }
  254. void DecompressAlphaDxt5( u8* rgba, void const* block )
  255. {
  256. // get the two alpha values
  257. u8 const* bytes = reinterpret_cast< u8 const* >( block );
  258. int alpha0 = bytes[0];
  259. int alpha1 = bytes[1];
  260. // compare the values to build the codebook
  261. u8 codes[8];
  262. codes[0] = ( u8 )alpha0;
  263. codes[1] = ( u8 )alpha1;
  264. if( alpha0 <= alpha1 )
  265. {
  266. // use 5-alpha codebook
  267. for( int i = 1; i < 5; ++i )
  268. codes[1 + i] = ( u8 )( ( ( 5 - i )*alpha0 + i*alpha1 )/5 );
  269. codes[6] = 0;
  270. codes[7] = 255;
  271. }
  272. else
  273. {
  274. // use 7-alpha codebook
  275. for( int i = 1; i < 7; ++i )
  276. codes[1 + i] = ( u8 )( ( ( 7 - i )*alpha0 + i*alpha1 )/7 );
  277. }
  278. // decode the indices
  279. u8 indices[16];
  280. u8 const* src = bytes + 2;
  281. u8* dest = indices;
  282. for( int i = 0; i < 2; ++i )
  283. {
  284. // grab 3 bytes
  285. int value = 0;
  286. for( int j = 0; j < 3; ++j )
  287. {
  288. int byte = *src++;
  289. value |= ( byte << 8*j );
  290. }
  291. // unpack 8 3-bit values from it
  292. for( int j = 0; j < 8; ++j )
  293. {
  294. int index = ( value >> 3*j ) & 0x7;
  295. *dest++ = ( u8 )index;
  296. }
  297. }
  298. // write out the indexed codebook values
  299. for( int i = 0; i < 16; ++i )
  300. rgba[4*i + 3] = codes[indices[i]];
  301. }
  302. } // namespace squish