squish.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown [email protected]
  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 "squish.h"
  21. #include "colourset.h"
  22. #include "maths.h"
  23. #include "rangefit.h"
  24. #include "clusterfit.h"
  25. #include "colourblock.h"
  26. #include "alpha.h"
  27. #include "singlecolourfit.h"
  28. namespace squish {
  29. static int FixFlags( int flags )
  30. {
  31. // grab the flag bits
  32. int method = flags & ( kDxt1 | kDxt3 | kDxt5 | kBc4 | kBc5 );
  33. int fit = flags & ( kColourIterativeClusterFit | kColourClusterFit | kColourRangeFit );
  34. int extra = flags & kWeightColourByAlpha;
  35. // set defaults
  36. if ( method != kDxt3
  37. && method != kDxt5
  38. && method != kBc4
  39. && method != kBc5 )
  40. {
  41. method = kDxt1;
  42. }
  43. if( fit != kColourRangeFit && fit != kColourIterativeClusterFit )
  44. fit = kColourClusterFit;
  45. // done
  46. return method | fit | extra;
  47. }
  48. void CompressMasked( u8 const* rgba, int mask, void* block, int flags, float* metric )
  49. {
  50. // fix any bad flags
  51. flags = FixFlags( flags );
  52. if ( ( flags & ( kBc4 | kBc5 ) ) != 0 )
  53. {
  54. u8 alpha[16*4];
  55. for( int i = 0; i < 16; ++i )
  56. {
  57. alpha[i*4 + 3] = rgba[i*4 + 0]; // copy R to A
  58. }
  59. u8* rBlock = reinterpret_cast< u8* >( block );
  60. CompressAlphaDxt5( alpha, mask, rBlock );
  61. if ( ( flags & ( kBc5 ) ) != 0 )
  62. {
  63. for( int i = 0; i < 16; ++i )
  64. {
  65. alpha[i*4 + 3] = rgba[i*4 + 1]; // copy G to A
  66. }
  67. u8* gBlock = reinterpret_cast< u8* >( block ) + 8;
  68. CompressAlphaDxt5( alpha, mask, gBlock );
  69. }
  70. return;
  71. }
  72. // get the block locations
  73. void* colourBlock = block;
  74. void* alphaBlock = block;
  75. if( ( flags & ( kDxt3 | kDxt5 ) ) != 0 )
  76. colourBlock = reinterpret_cast< u8* >( block ) + 8;
  77. // create the minimal point set
  78. ColourSet colours( rgba, mask, flags );
  79. // check the compression type and compress colour
  80. if( colours.GetCount() == 1 )
  81. {
  82. // always do a single colour fit
  83. SingleColourFit fit( &colours, flags );
  84. fit.Compress( colourBlock );
  85. }
  86. else if( ( flags & kColourRangeFit ) != 0 || colours.GetCount() == 0 )
  87. {
  88. // do a range fit
  89. RangeFit fit( &colours, flags, metric );
  90. fit.Compress( colourBlock );
  91. }
  92. else
  93. {
  94. // default to a cluster fit (could be iterative or not)
  95. ClusterFit fit( &colours, flags, metric );
  96. fit.Compress( colourBlock );
  97. }
  98. // compress alpha separately if necessary
  99. if( ( flags & kDxt3 ) != 0 )
  100. CompressAlphaDxt3( rgba, mask, alphaBlock );
  101. else if( ( flags & kDxt5 ) != 0 )
  102. CompressAlphaDxt5( rgba, mask, alphaBlock );
  103. }
  104. void Decompress( u8* rgba, void const* block, int flags )
  105. {
  106. // fix any bad flags
  107. flags = FixFlags( flags );
  108. // get the block locations
  109. void const* colourBlock = block;
  110. void const* alphaBock = block;
  111. if( ( flags & ( kDxt3 | kDxt5 ) ) != 0 )
  112. colourBlock = reinterpret_cast< u8 const* >( block ) + 8;
  113. // decompress colour
  114. DecompressColour( rgba, colourBlock, ( flags & kDxt1 ) != 0 );
  115. // decompress alpha separately if necessary
  116. if( ( flags & kDxt3 ) != 0 )
  117. DecompressAlphaDxt3( rgba, alphaBock );
  118. else if( ( flags & kDxt5 ) != 0 )
  119. DecompressAlphaDxt5( rgba, alphaBock );
  120. }
  121. int GetStorageRequirements( int width, int height, int flags )
  122. {
  123. // fix any bad flags
  124. flags = FixFlags( flags );
  125. // compute the storage requirements
  126. int blockcount = ( ( width + 3 )/4 ) * ( ( height + 3 )/4 );
  127. int blocksize = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  128. return blockcount*blocksize;
  129. }
  130. void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags, float* metric )
  131. {
  132. // fix any bad flags
  133. flags = FixFlags( flags );
  134. // initialise the block output
  135. u8* targetBlock = reinterpret_cast< u8* >( blocks );
  136. int bytesPerBlock = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  137. // loop over blocks
  138. for( int y = 0; y < height; y += 4 )
  139. {
  140. for( int x = 0; x < width; x += 4 )
  141. {
  142. // build the 4x4 block of pixels
  143. u8 sourceRgba[16*4];
  144. u8* targetPixel = sourceRgba;
  145. int mask = 0;
  146. for( int py = 0; py < 4; ++py )
  147. {
  148. for( int px = 0; px < 4; ++px )
  149. {
  150. // get the source pixel in the image
  151. int sx = x + px;
  152. int sy = y + py;
  153. // enable if we're in the image
  154. if( sx < width && sy < height )
  155. {
  156. // copy the rgba value
  157. u8 const* sourcePixel = rgba + 4*( width*sy + sx );
  158. for( int i = 0; i < 4; ++i )
  159. *targetPixel++ = *sourcePixel++;
  160. // enable this pixel
  161. mask |= ( 1 << ( 4*py + px ) );
  162. }
  163. else
  164. {
  165. // skip this pixel as its outside the image
  166. targetPixel += 4;
  167. }
  168. }
  169. }
  170. // compress it into the output
  171. CompressMasked( sourceRgba, mask, targetBlock, flags, metric );
  172. // advance
  173. targetBlock += bytesPerBlock;
  174. }
  175. }
  176. }
  177. void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags )
  178. {
  179. // fix any bad flags
  180. flags = FixFlags( flags );
  181. // initialise the block input
  182. u8 const* sourceBlock = reinterpret_cast< u8 const* >( blocks );
  183. int bytesPerBlock = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  184. // loop over blocks
  185. for( int y = 0; y < height; y += 4 )
  186. {
  187. for( int x = 0; x < width; x += 4 )
  188. {
  189. // decompress the block
  190. u8 targetRgba[4*16];
  191. Decompress( targetRgba, sourceBlock, flags );
  192. // write the decompressed pixels to the correct image locations
  193. u8 const* sourcePixel = targetRgba;
  194. for( int py = 0; py < 4; ++py )
  195. {
  196. for( int px = 0; px < 4; ++px )
  197. {
  198. // get the target location
  199. int sx = x + px;
  200. int sy = y + py;
  201. if( sx < width && sy < height )
  202. {
  203. u8* targetPixel = rgba + 4*( width*sy + sx );
  204. // copy the rgba value
  205. for( int i = 0; i < 4; ++i )
  206. *targetPixel++ = *sourcePixel++;
  207. }
  208. else
  209. {
  210. // skip this pixel as its outside the image
  211. sourcePixel += 4;
  212. }
  213. }
  214. }
  215. // advance
  216. sourceBlock += bytesPerBlock;
  217. }
  218. }
  219. }
  220. } // namespace squish