squish.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 <string.h>
  21. #include "squish.h"
  22. #include "colourset.h"
  23. #include "maths.h"
  24. #include "rangefit.h"
  25. #include "clusterfit.h"
  26. #include "colourblock.h"
  27. #include "alpha.h"
  28. #include "singlecolourfit.h"
  29. namespace squish {
  30. static int FixFlags( int flags )
  31. {
  32. // grab the flag bits
  33. int method = flags & ( kDxt1 | kDxt3 | kDxt5 | kBc4 | kBc5 );
  34. int fit = flags & ( kColourIterativeClusterFit | kColourClusterFit | kColourRangeFit );
  35. int extra = flags & kWeightColourByAlpha;
  36. // set defaults
  37. if ( method != kDxt3
  38. && method != kDxt5
  39. && method != kBc4
  40. && method != kBc5 )
  41. {
  42. method = kDxt1;
  43. }
  44. if( fit != kColourRangeFit && fit != kColourIterativeClusterFit )
  45. fit = kColourClusterFit;
  46. // done
  47. return method | fit | extra;
  48. }
  49. void CompressMasked( u8 const* rgba, int mask, void* block, int flags, float* metric )
  50. {
  51. // fix any bad flags
  52. flags = FixFlags( flags );
  53. if ( ( flags & ( kBc4 | kBc5 ) ) != 0 )
  54. {
  55. u8 alpha[16*4];
  56. for( int i = 0; i < 16; ++i )
  57. {
  58. alpha[i*4 + 3] = rgba[i*4 + 0]; // copy R to A
  59. }
  60. u8* rBlock = reinterpret_cast< u8* >( block );
  61. CompressAlphaDxt5( alpha, mask, rBlock );
  62. if ( ( flags & ( kBc5 ) ) != 0 )
  63. {
  64. for( int i = 0; i < 16; ++i )
  65. {
  66. alpha[i*4 + 3] = rgba[i*4 + 1]; // copy G to A
  67. }
  68. u8* gBlock = reinterpret_cast< u8* >( block ) + 8;
  69. CompressAlphaDxt5( alpha, mask, gBlock );
  70. }
  71. return;
  72. }
  73. // get the block locations
  74. void* colourBlock = block;
  75. void* alphaBlock = block;
  76. if( ( flags & ( kDxt3 | kDxt5 ) ) != 0 )
  77. colourBlock = reinterpret_cast< u8* >( block ) + 8;
  78. // create the minimal point set
  79. ColourSet colours( rgba, mask, flags );
  80. // check the compression type and compress colour
  81. if( colours.GetCount() == 1 )
  82. {
  83. // always do a single colour fit
  84. SingleColourFit fit( &colours, flags );
  85. fit.Compress( colourBlock );
  86. }
  87. else if( ( flags & kColourRangeFit ) != 0 || colours.GetCount() == 0 )
  88. {
  89. // do a range fit
  90. RangeFit fit( &colours, flags, metric );
  91. fit.Compress( colourBlock );
  92. }
  93. else
  94. {
  95. // default to a cluster fit (could be iterative or not)
  96. ClusterFit fit( &colours, flags, metric );
  97. fit.Compress( colourBlock );
  98. }
  99. // compress alpha separately if necessary
  100. if( ( flags & kDxt3 ) != 0 )
  101. CompressAlphaDxt3( rgba, mask, alphaBlock );
  102. else if( ( flags & kDxt5 ) != 0 )
  103. CompressAlphaDxt5( rgba, mask, alphaBlock );
  104. }
  105. void Decompress( u8* rgba, void const* block, int flags )
  106. {
  107. // fix any bad flags
  108. flags = FixFlags( flags );
  109. // get the block locations
  110. void const* colourBlock = block;
  111. void const* alphaBlock = block;
  112. if( ( flags & ( kDxt3 | kDxt5 ) ) != 0 )
  113. colourBlock = reinterpret_cast< u8 const* >( block ) + 8;
  114. // decompress colour
  115. // -- GODOT start --
  116. //DecompressColour( rgba, colourBlock, ( flags & kDxt1 ) != 0 );
  117. if(( flags & ( kBc5 ) ) != 0)
  118. DecompressColourBc5( rgba, colourBlock);
  119. else
  120. DecompressColour( rgba, colourBlock, ( flags & kDxt1 ) != 0 );
  121. // -- GODOT end --
  122. // decompress alpha separately if necessary
  123. if( ( flags & kDxt3 ) != 0 )
  124. DecompressAlphaDxt3( rgba, alphaBlock );
  125. else if( ( flags & kDxt5 ) != 0 )
  126. DecompressAlphaDxt5( rgba, alphaBlock );
  127. }
  128. int GetStorageRequirements( int width, int height, int flags )
  129. {
  130. // fix any bad flags
  131. flags = FixFlags( flags );
  132. // compute the storage requirements
  133. int blockcount = ( ( width + 3 )/4 ) * ( ( height + 3 )/4 );
  134. int blocksize = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  135. return blockcount*blocksize;
  136. }
  137. void CopyRGBA( u8 const* source, u8* dest, int flags )
  138. {
  139. if (flags & kSourceBGRA)
  140. {
  141. // convert from bgra to rgba
  142. dest[0] = source[2];
  143. dest[1] = source[1];
  144. dest[2] = source[0];
  145. dest[3] = source[3];
  146. }
  147. else
  148. {
  149. for( int i = 0; i < 4; ++i )
  150. *dest++ = *source++;
  151. }
  152. }
  153. void CompressImage( u8 const* rgba, int width, int height, int pitch, void* blocks, int flags, float* metric )
  154. {
  155. // fix any bad flags
  156. flags = FixFlags( flags );
  157. // loop over blocks
  158. #ifdef SQUISH_USE_OPENMP
  159. # pragma omp parallel for
  160. #endif
  161. for( int y = 0; y < height; y += 4 )
  162. {
  163. // initialise the block output
  164. u8* targetBlock = reinterpret_cast< u8* >( blocks );
  165. int bytesPerBlock = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  166. targetBlock += ( (y / 4) * ( (width + 3) / 4) ) * bytesPerBlock;
  167. for( int x = 0; x < width; x += 4 )
  168. {
  169. // build the 4x4 block of pixels
  170. u8 sourceRgba[16*4];
  171. u8* targetPixel = sourceRgba;
  172. int mask = 0;
  173. for( int py = 0; py < 4; ++py )
  174. {
  175. for( int px = 0; px < 4; ++px )
  176. {
  177. // get the source pixel in the image
  178. int sx = x + px;
  179. int sy = y + py;
  180. // enable if we're in the image
  181. if( sx < width && sy < height )
  182. {
  183. // copy the rgba value
  184. u8 const* sourcePixel = rgba + pitch*sy + 4*sx;
  185. CopyRGBA(sourcePixel, targetPixel, flags);
  186. // enable this pixel
  187. mask |= ( 1 << ( 4*py + px ) );
  188. }
  189. // advance to the next pixel
  190. targetPixel += 4;
  191. }
  192. }
  193. // compress it into the output
  194. CompressMasked( sourceRgba, mask, targetBlock, flags, metric );
  195. // advance
  196. targetBlock += bytesPerBlock;
  197. }
  198. }
  199. }
  200. void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags, float* metric )
  201. {
  202. CompressImage(rgba, width, height, width*4, blocks, flags, metric);
  203. }
  204. void DecompressImage( u8* rgba, int width, int height, int pitch, void const* blocks, int flags )
  205. {
  206. // fix any bad flags
  207. flags = FixFlags( flags );
  208. // loop over blocks
  209. #ifdef SQUISH_USE_OPENMP
  210. # pragma omp parallel for
  211. #endif
  212. for( int y = 0; y < height; y += 4 )
  213. {
  214. // initialise the block input
  215. u8 const* sourceBlock = reinterpret_cast< u8 const* >( blocks );
  216. int bytesPerBlock = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  217. sourceBlock += ( (y / 4) * ( (width + 3) / 4) ) * bytesPerBlock;
  218. for( int x = 0; x < width; x += 4 )
  219. {
  220. // decompress the block
  221. u8 targetRgba[4*16];
  222. Decompress( targetRgba, sourceBlock, flags );
  223. // write the decompressed pixels to the correct image locations
  224. u8 const* sourcePixel = targetRgba;
  225. for( int py = 0; py < 4; ++py )
  226. {
  227. for( int px = 0; px < 4; ++px )
  228. {
  229. // get the target location
  230. int sx = x + px;
  231. int sy = y + py;
  232. // write if we're in the image
  233. if( sx < width && sy < height )
  234. {
  235. // copy the rgba value
  236. u8* targetPixel = rgba + pitch*sy + 4*sx;
  237. CopyRGBA(sourcePixel, targetPixel, flags);
  238. }
  239. // advance to the next pixel
  240. sourcePixel += 4;
  241. }
  242. }
  243. // advance
  244. sourceBlock += bytesPerBlock;
  245. }
  246. }
  247. }
  248. void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags )
  249. {
  250. DecompressImage( rgba, width, height, width*4, blocks, flags );
  251. }
  252. static double ErrorSq(double x, double y)
  253. {
  254. return (x - y) * (x - y);
  255. }
  256. static void ComputeBlockWMSE(u8 const *original, u8 const *compressed, unsigned int w, unsigned int h, double &cmse, double &amse)
  257. {
  258. // Computes the MSE for the block and weights it by the variance of the original block.
  259. // If the variance of the original block is less than 4 (i.e. a standard deviation of 1 per channel)
  260. // then the block is close to being a single colour. Quantisation errors in single colour blocks
  261. // are easier to see than similar errors in blocks that contain more colours, particularly when there
  262. // are many such blocks in a large area (eg a blue sky background) as they cause banding. Given that
  263. // banding is easier to see than small errors in "complex" blocks, we weight the errors by a factor
  264. // of 5. This implies that images with large, single colour areas will have a higher potential WMSE
  265. // than images with lots of detail.
  266. cmse = amse = 0;
  267. unsigned int sum_p[4]; // per channel sum of pixels
  268. unsigned int sum_p2[4]; // per channel sum of pixels squared
  269. memset(sum_p, 0, sizeof(sum_p));
  270. memset(sum_p2, 0, sizeof(sum_p2));
  271. for( unsigned int py = 0; py < 4; ++py )
  272. {
  273. for( unsigned int px = 0; px < 4; ++px )
  274. {
  275. if( px < w && py < h )
  276. {
  277. double pixelCMSE = 0;
  278. for( int i = 0; i < 3; ++i )
  279. {
  280. pixelCMSE += ErrorSq(original[i], compressed[i]);
  281. sum_p[i] += original[i];
  282. sum_p2[i] += (unsigned int)original[i]*original[i];
  283. }
  284. if( original[3] == 0 && compressed[3] == 0 )
  285. pixelCMSE = 0; // transparent in both, so colour is inconsequential
  286. amse += ErrorSq(original[3], compressed[3]);
  287. cmse += pixelCMSE;
  288. sum_p[3] += original[3];
  289. sum_p2[3] += (unsigned int)original[3]*original[3];
  290. }
  291. original += 4;
  292. compressed += 4;
  293. }
  294. }
  295. unsigned int variance = 0;
  296. for( int i = 0; i < 4; ++i )
  297. variance += w*h*sum_p2[i] - sum_p[i]*sum_p[i];
  298. if( variance < 4 * w * w * h * h )
  299. {
  300. amse *= 5;
  301. cmse *= 5;
  302. }
  303. }
  304. void ComputeMSE( u8 const *rgba, int width, int height, int pitch, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE )
  305. {
  306. // fix any bad flags
  307. flags = FixFlags( flags );
  308. colourMSE = alphaMSE = 0;
  309. // initialise the block input
  310. squish::u8 const* sourceBlock = dxt;
  311. int bytesPerBlock = ( ( flags & squish::kDxt1 ) != 0 ) ? 8 : 16;
  312. // loop over blocks
  313. for( int y = 0; y < height; y += 4 )
  314. {
  315. for( int x = 0; x < width; x += 4 )
  316. {
  317. // decompress the block
  318. u8 targetRgba[4*16];
  319. Decompress( targetRgba, sourceBlock, flags );
  320. u8 const* sourcePixel = targetRgba;
  321. // copy across to a similar pixel block
  322. u8 originalRgba[4*16];
  323. u8* originalPixel = originalRgba;
  324. for( int py = 0; py < 4; ++py )
  325. {
  326. for( int px = 0; px < 4; ++px )
  327. {
  328. int sx = x + px;
  329. int sy = y + py;
  330. if( sx < width && sy < height )
  331. {
  332. u8 const* targetPixel = rgba + pitch*sy + 4*sx;
  333. CopyRGBA(targetPixel, originalPixel, flags);
  334. }
  335. sourcePixel += 4;
  336. originalPixel += 4;
  337. }
  338. }
  339. // compute the weighted MSE of the block
  340. double blockCMSE, blockAMSE;
  341. ComputeBlockWMSE(originalRgba, targetRgba, std::min(4, width - x), std::min(4, height - y), blockCMSE, blockAMSE);
  342. colourMSE += blockCMSE;
  343. alphaMSE += blockAMSE;
  344. // advance
  345. sourceBlock += bytesPerBlock;
  346. }
  347. }
  348. colourMSE /= (width * height * 3);
  349. alphaMSE /= (width * height);
  350. }
  351. void ComputeMSE( u8 const *rgba, int width, int height, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE )
  352. {
  353. ComputeMSE(rgba, width, height, width*4, dxt, flags, colourMSE, alphaMSE);
  354. }
  355. } // namespace squish