alpha.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 <climits>
  22. #include <algorithm>
  23. namespace squish {
  24. static int FloatToInt( float a, int limit )
  25. {
  26. // use ANSI round-to-zero behaviour to get round-to-nearest
  27. int i = ( int )( a + 0.5f );
  28. // clamp to the limit
  29. if( i < 0 )
  30. i = 0;
  31. else if( i > limit )
  32. i = limit;
  33. // done
  34. return i;
  35. }
  36. void CompressAlphaDxt3( u8 const* rgba, int mask, void* block )
  37. {
  38. u8* bytes = reinterpret_cast< u8* >( block );
  39. // quantise and pack the alpha values pairwise
  40. for( int i = 0; i < 8; ++i )
  41. {
  42. // quantise down to 4 bits
  43. float alpha1 = ( float )rgba[8*i + 3] * ( 15.0f/255.0f );
  44. float alpha2 = ( float )rgba[8*i + 7] * ( 15.0f/255.0f );
  45. int quant1 = FloatToInt( alpha1, 15 );
  46. int quant2 = FloatToInt( alpha2, 15 );
  47. // set alpha to zero where masked
  48. int bit1 = 1 << ( 2*i );
  49. int bit2 = 1 << ( 2*i + 1 );
  50. if( ( mask & bit1 ) == 0 )
  51. quant1 = 0;
  52. if( ( mask & bit2 ) == 0 )
  53. quant2 = 0;
  54. // pack into the byte
  55. bytes[i] = ( u8 )( quant1 | ( quant2 << 4 ) );
  56. }
  57. }
  58. void DecompressAlphaDxt3( u8* rgba, void const* block )
  59. {
  60. u8 const* bytes = reinterpret_cast< u8 const* >( block );
  61. // unpack the alpha values pairwise
  62. for( int i = 0; i < 8; ++i )
  63. {
  64. // quantise down to 4 bits
  65. u8 quant = bytes[i];
  66. // unpack the values
  67. u8 lo = quant & 0x0f;
  68. u8 hi = quant & 0xf0;
  69. // convert back up to bytes
  70. rgba[8*i + 3] = lo | ( lo << 4 );
  71. rgba[8*i + 7] = hi | ( hi >> 4 );
  72. }
  73. }
  74. static void FixRange( int& min, int& max, int steps )
  75. {
  76. if( max - min < steps )
  77. max = std::min( min + steps, 255 );
  78. if( max - min < steps )
  79. min = std::max( 0, max - steps );
  80. }
  81. static int FitCodes( u8 const* rgba, int mask, u8 const* codes, u8* indices )
  82. {
  83. // fit each alpha value to the codebook
  84. int err = 0;
  85. for( int i = 0; i < 16; ++i )
  86. {
  87. // check this pixel is valid
  88. int bit = 1 << i;
  89. if( ( mask & bit ) == 0 )
  90. {
  91. // use the first code
  92. indices[i] = 0;
  93. continue;
  94. }
  95. // find the least error and corresponding index
  96. int value = rgba[4*i + 3];
  97. int least = INT_MAX;
  98. int index = 0;
  99. for( int j = 0; j < 8; ++j )
  100. {
  101. // get the squared error from this code
  102. int dist = ( int )value - ( int )codes[j];
  103. dist *= dist;
  104. // compare with the best so far
  105. if( dist < least )
  106. {
  107. least = dist;
  108. index = j;
  109. }
  110. }
  111. // save this index and accumulate the error
  112. indices[i] = ( u8 )index;
  113. err += least;
  114. }
  115. // return the total error
  116. return err;
  117. }
  118. static void WriteAlphaBlock( int alpha0, int alpha1, u8 const* indices, void* block )
  119. {
  120. u8* bytes = reinterpret_cast< u8* >( block );
  121. // write the first two bytes
  122. bytes[0] = ( u8 )alpha0;
  123. bytes[1] = ( u8 )alpha1;
  124. // pack the indices with 3 bits each
  125. u8* dest = bytes + 2;
  126. u8 const* src = indices;
  127. for( int i = 0; i < 2; ++i )
  128. {
  129. // pack 8 3-bit values
  130. int value = 0;
  131. for( int j = 0; j < 8; ++j )
  132. {
  133. int index = *src++;
  134. value |= ( index << 3*j );
  135. }
  136. // store in 3 bytes
  137. for( int j = 0; j < 3; ++j )
  138. {
  139. int byte = ( value >> 8*j ) & 0xff;
  140. *dest++ = ( u8 )byte;
  141. }
  142. }
  143. }
  144. static void WriteAlphaBlock5( int alpha0, int alpha1, u8 const* indices, void* block )
  145. {
  146. // check the relative values of the endpoints
  147. if( alpha0 > alpha1 )
  148. {
  149. // swap the indices
  150. u8 swapped[16];
  151. for( int i = 0; i < 16; ++i )
  152. {
  153. u8 index = indices[i];
  154. if( index == 0 )
  155. swapped[i] = 1;
  156. else if( index == 1 )
  157. swapped[i] = 0;
  158. else if( index <= 5 )
  159. swapped[i] = 7 - index;
  160. else
  161. swapped[i] = index;
  162. }
  163. // write the block
  164. WriteAlphaBlock( alpha1, alpha0, swapped, block );
  165. }
  166. else
  167. {
  168. // write the block
  169. WriteAlphaBlock( alpha0, alpha1, indices, block );
  170. }
  171. }
  172. static void WriteAlphaBlock7( int alpha0, int alpha1, u8 const* indices, void* block )
  173. {
  174. // check the relative values of the endpoints
  175. if( alpha0 < alpha1 )
  176. {
  177. // swap the indices
  178. u8 swapped[16];
  179. for( int i = 0; i < 16; ++i )
  180. {
  181. u8 index = indices[i];
  182. if( index == 0 )
  183. swapped[i] = 1;
  184. else if( index == 1 )
  185. swapped[i] = 0;
  186. else
  187. swapped[i] = 9 - index;
  188. }
  189. // write the block
  190. WriteAlphaBlock( alpha1, alpha0, swapped, block );
  191. }
  192. else
  193. {
  194. // write the block
  195. WriteAlphaBlock( alpha0, alpha1, indices, block );
  196. }
  197. }
  198. void CompressAlphaDxt5( u8 const* rgba, int mask, void* block )
  199. {
  200. // get the range for 5-alpha and 7-alpha interpolation
  201. int min5 = 255;
  202. int max5 = 0;
  203. int min7 = 255;
  204. int max7 = 0;
  205. for( int i = 0; i < 16; ++i )
  206. {
  207. // check this pixel is valid
  208. int bit = 1 << i;
  209. if( ( mask & bit ) == 0 )
  210. continue;
  211. // incorporate into the min/max
  212. int value = rgba[4*i + 3];
  213. if( value < min7 )
  214. min7 = value;
  215. if( value > max7 )
  216. max7 = value;
  217. if( value != 0 && value < min5 )
  218. min5 = value;
  219. if( value != 255 && value > max5 )
  220. max5 = value;
  221. }
  222. // handle the case that no valid range was found
  223. if( min5 > max5 )
  224. min5 = max5;
  225. if( min7 > max7 )
  226. min7 = max7;
  227. // fix the range to be the minimum in each case
  228. FixRange( min5, max5, 5 );
  229. FixRange( min7, max7, 7 );
  230. // set up the 5-alpha code book
  231. u8 codes5[8];
  232. codes5[0] = ( u8 )min5;
  233. codes5[1] = ( u8 )max5;
  234. for( int i = 1; i < 5; ++i )
  235. codes5[1 + i] = ( u8 )( ( ( 5 - i )*min5 + i*max5 )/5 );
  236. codes5[6] = 0;
  237. codes5[7] = 255;
  238. // set up the 7-alpha code book
  239. u8 codes7[8];
  240. codes7[0] = ( u8 )min7;
  241. codes7[1] = ( u8 )max7;
  242. for( int i = 1; i < 7; ++i )
  243. codes7[1 + i] = ( u8 )( ( ( 7 - i )*min7 + i*max7 )/7 );
  244. // fit the data to both code books
  245. u8 indices5[16];
  246. u8 indices7[16];
  247. int err5 = FitCodes( rgba, mask, codes5, indices5 );
  248. int err7 = FitCodes( rgba, mask, codes7, indices7 );
  249. // save the block with least error
  250. if( err5 <= err7 )
  251. WriteAlphaBlock5( min5, max5, indices5, block );
  252. else
  253. WriteAlphaBlock7( min7, max7, indices7, block );
  254. }
  255. void DecompressAlphaDxt5( u8* rgba, void const* block )
  256. {
  257. // get the two alpha values
  258. u8 const* bytes = reinterpret_cast< u8 const* >( block );
  259. int alpha0 = bytes[0];
  260. int alpha1 = bytes[1];
  261. // compare the values to build the codebook
  262. u8 codes[8];
  263. codes[0] = ( u8 )alpha0;
  264. codes[1] = ( u8 )alpha1;
  265. if( alpha0 <= alpha1 )
  266. {
  267. // use 5-alpha codebook
  268. for( int i = 1; i < 5; ++i )
  269. codes[1 + i] = ( u8 )( ( ( 5 - i )*alpha0 + i*alpha1 )/5 );
  270. codes[6] = 0;
  271. codes[7] = 255;
  272. }
  273. else
  274. {
  275. // use 7-alpha codebook
  276. for( int i = 1; i < 7; ++i )
  277. codes[1 + i] = ( u8 )( ( ( 7 - i )*alpha0 + i*alpha1 )/7 );
  278. }
  279. // decode the indices
  280. u8 indices[16];
  281. u8 const* src = bytes + 2;
  282. u8* dest = indices;
  283. for( int i = 0; i < 2; ++i )
  284. {
  285. // grab 3 bytes
  286. int value = 0;
  287. for( int j = 0; j < 3; ++j )
  288. {
  289. int byte = *src++;
  290. value |= ( byte << 8*j );
  291. }
  292. // unpack 8 3-bit values from it
  293. for( int j = 0; j < 8; ++j )
  294. {
  295. int index = ( value >> 3*j ) & 0x7;
  296. *dest++ = ( u8 )index;
  297. }
  298. }
  299. // write out the indexed codebook values
  300. for( int i = 0; i < 16; ++i )
  301. rgba[4*i + 3] = codes[indices[i]];
  302. }
  303. } // namespace squish