squish.cpp 6.5 KB

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