squish.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 & ( kBc4 ) ) != 0)
  118. DecompressColourBc4( rgba, colourBlock);
  119. else if(( flags & ( kBc5 ) ) != 0)
  120. DecompressColourBc5( rgba, colourBlock);
  121. else
  122. DecompressColour( rgba, colourBlock, ( flags & kDxt1 ) != 0 );
  123. // -- GODOT end --
  124. // decompress alpha separately if necessary
  125. if( ( flags & kDxt3 ) != 0 )
  126. DecompressAlphaDxt3( rgba, alphaBlock );
  127. else if( ( flags & kDxt5 ) != 0 )
  128. DecompressAlphaDxt5( rgba, alphaBlock );
  129. }
  130. int GetStorageRequirements( int width, int height, int flags )
  131. {
  132. // fix any bad flags
  133. flags = FixFlags( flags );
  134. // compute the storage requirements
  135. int blockcount = ( ( width + 3 )/4 ) * ( ( height + 3 )/4 );
  136. int blocksize = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  137. return blockcount*blocksize;
  138. }
  139. void CopyRGBA( u8 const* source, u8* dest, int flags )
  140. {
  141. if (flags & kSourceBGRA)
  142. {
  143. // convert from bgra to rgba
  144. dest[0] = source[2];
  145. dest[1] = source[1];
  146. dest[2] = source[0];
  147. dest[3] = source[3];
  148. }
  149. else
  150. {
  151. for( int i = 0; i < 4; ++i )
  152. *dest++ = *source++;
  153. }
  154. }
  155. void CompressImage( u8 const* rgba, int width, int height, int pitch, void* blocks, int flags, float* metric )
  156. {
  157. // fix any bad flags
  158. flags = FixFlags( flags );
  159. // loop over blocks
  160. #ifdef SQUISH_USE_OPENMP
  161. # pragma omp parallel for
  162. #endif
  163. for( int y = 0; y < height; y += 4 )
  164. {
  165. // initialise the block output
  166. u8* targetBlock = reinterpret_cast< u8* >( blocks );
  167. int bytesPerBlock = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  168. targetBlock += ( (y / 4) * ( (width + 3) / 4) ) * bytesPerBlock;
  169. for( int x = 0; x < width; x += 4 )
  170. {
  171. // build the 4x4 block of pixels
  172. u8 sourceRgba[16*4];
  173. u8* targetPixel = sourceRgba;
  174. int mask = 0;
  175. for( int py = 0; py < 4; ++py )
  176. {
  177. for( int px = 0; px < 4; ++px )
  178. {
  179. // get the source pixel in the image
  180. int sx = x + px;
  181. int sy = y + py;
  182. // enable if we're in the image
  183. if( sx < width && sy < height )
  184. {
  185. // copy the rgba value
  186. u8 const* sourcePixel = rgba + pitch*sy + 4*sx;
  187. CopyRGBA(sourcePixel, targetPixel, flags);
  188. // enable this pixel
  189. mask |= ( 1 << ( 4*py + px ) );
  190. }
  191. // advance to the next pixel
  192. targetPixel += 4;
  193. }
  194. }
  195. // compress it into the output
  196. CompressMasked( sourceRgba, mask, targetBlock, flags, metric );
  197. // advance
  198. targetBlock += bytesPerBlock;
  199. }
  200. }
  201. }
  202. void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags, float* metric )
  203. {
  204. CompressImage(rgba, width, height, width*4, blocks, flags, metric);
  205. }
  206. void DecompressImage( u8* rgba, int width, int height, int pitch, void const* blocks, int flags )
  207. {
  208. // fix any bad flags
  209. flags = FixFlags( flags );
  210. // loop over blocks
  211. #ifdef SQUISH_USE_OPENMP
  212. # pragma omp parallel for
  213. #endif
  214. for( int y = 0; y < height; y += 4 )
  215. {
  216. // initialise the block input
  217. u8 const* sourceBlock = reinterpret_cast< u8 const* >( blocks );
  218. int bytesPerBlock = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
  219. sourceBlock += ( (y / 4) * ( (width + 3) / 4) ) * bytesPerBlock;
  220. for( int x = 0; x < width; x += 4 )
  221. {
  222. // decompress the block
  223. u8 targetRgba[4*16];
  224. Decompress( targetRgba, sourceBlock, flags );
  225. // write the decompressed pixels to the correct image locations
  226. u8 const* sourcePixel = targetRgba;
  227. for( int py = 0; py < 4; ++py )
  228. {
  229. for( int px = 0; px < 4; ++px )
  230. {
  231. // get the target location
  232. int sx = x + px;
  233. int sy = y + py;
  234. // write if we're in the image
  235. if( sx < width && sy < height )
  236. {
  237. // copy the rgba value
  238. u8* targetPixel = rgba + pitch*sy + 4*sx;
  239. CopyRGBA(sourcePixel, targetPixel, flags);
  240. }
  241. // advance to the next pixel
  242. sourcePixel += 4;
  243. }
  244. }
  245. // advance
  246. sourceBlock += bytesPerBlock;
  247. }
  248. }
  249. }
  250. void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags )
  251. {
  252. DecompressImage( rgba, width, height, width*4, blocks, flags );
  253. }
  254. static double ErrorSq(double x, double y)
  255. {
  256. return (x - y) * (x - y);
  257. }
  258. static void ComputeBlockWMSE(u8 const *original, u8 const *compressed, unsigned int w, unsigned int h, double &cmse, double &amse)
  259. {
  260. // Computes the MSE for the block and weights it by the variance of the original block.
  261. // If the variance of the original block is less than 4 (i.e. a standard deviation of 1 per channel)
  262. // then the block is close to being a single colour. Quantisation errors in single colour blocks
  263. // are easier to see than similar errors in blocks that contain more colours, particularly when there
  264. // are many such blocks in a large area (eg a blue sky background) as they cause banding. Given that
  265. // banding is easier to see than small errors in "complex" blocks, we weight the errors by a factor
  266. // of 5. This implies that images with large, single colour areas will have a higher potential WMSE
  267. // than images with lots of detail.
  268. cmse = amse = 0;
  269. unsigned int sum_p[4]; // per channel sum of pixels
  270. unsigned int sum_p2[4]; // per channel sum of pixels squared
  271. memset(sum_p, 0, sizeof(sum_p));
  272. memset(sum_p2, 0, sizeof(sum_p2));
  273. for( unsigned int py = 0; py < 4; ++py )
  274. {
  275. for( unsigned int px = 0; px < 4; ++px )
  276. {
  277. if( px < w && py < h )
  278. {
  279. double pixelCMSE = 0;
  280. for( int i = 0; i < 3; ++i )
  281. {
  282. pixelCMSE += ErrorSq(original[i], compressed[i]);
  283. sum_p[i] += original[i];
  284. sum_p2[i] += (unsigned int)original[i]*original[i];
  285. }
  286. if( original[3] == 0 && compressed[3] == 0 )
  287. pixelCMSE = 0; // transparent in both, so colour is inconsequential
  288. amse += ErrorSq(original[3], compressed[3]);
  289. cmse += pixelCMSE;
  290. sum_p[3] += original[3];
  291. sum_p2[3] += (unsigned int)original[3]*original[3];
  292. }
  293. original += 4;
  294. compressed += 4;
  295. }
  296. }
  297. unsigned int variance = 0;
  298. for( int i = 0; i < 4; ++i )
  299. variance += w*h*sum_p2[i] - sum_p[i]*sum_p[i];
  300. if( variance < 4 * w * w * h * h )
  301. {
  302. amse *= 5;
  303. cmse *= 5;
  304. }
  305. }
  306. void ComputeMSE( u8 const *rgba, int width, int height, int pitch, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE )
  307. {
  308. // fix any bad flags
  309. flags = FixFlags( flags );
  310. colourMSE = alphaMSE = 0;
  311. // initialise the block input
  312. squish::u8 const* sourceBlock = dxt;
  313. int bytesPerBlock = ( ( flags & squish::kDxt1 ) != 0 ) ? 8 : 16;
  314. // loop over blocks
  315. for( int y = 0; y < height; y += 4 )
  316. {
  317. for( int x = 0; x < width; x += 4 )
  318. {
  319. // decompress the block
  320. u8 targetRgba[4*16];
  321. Decompress( targetRgba, sourceBlock, flags );
  322. u8 const* sourcePixel = targetRgba;
  323. // copy across to a similar pixel block
  324. u8 originalRgba[4*16];
  325. u8* originalPixel = originalRgba;
  326. for( int py = 0; py < 4; ++py )
  327. {
  328. for( int px = 0; px < 4; ++px )
  329. {
  330. int sx = x + px;
  331. int sy = y + py;
  332. if( sx < width && sy < height )
  333. {
  334. u8 const* targetPixel = rgba + pitch*sy + 4*sx;
  335. CopyRGBA(targetPixel, originalPixel, flags);
  336. }
  337. sourcePixel += 4;
  338. originalPixel += 4;
  339. }
  340. }
  341. // compute the weighted MSE of the block
  342. double blockCMSE, blockAMSE;
  343. ComputeBlockWMSE(originalRgba, targetRgba, std::min(4, width - x), std::min(4, height - y), blockCMSE, blockAMSE);
  344. colourMSE += blockCMSE;
  345. alphaMSE += blockAMSE;
  346. // advance
  347. sourceBlock += bytesPerBlock;
  348. }
  349. }
  350. colourMSE /= (width * height * 3);
  351. alphaMSE /= (width * height);
  352. }
  353. void ComputeMSE( u8 const *rgba, int width, int height, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE )
  354. {
  355. ComputeMSE(rgba, width, height, width*4, dxt, flags, colourMSE, alphaMSE);
  356. }
  357. } // namespace squish