squish.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #ifndef SQUISH_H
  21. #define SQUISH_H
  22. //! All squish API functions live in this namespace.
  23. namespace squish {
  24. // -----------------------------------------------------------------------------
  25. //! Typedef a quantity that is a single unsigned byte.
  26. typedef unsigned char u8;
  27. // -----------------------------------------------------------------------------
  28. enum
  29. {
  30. //! Use DXT1 compression.
  31. kDxt1 = ( 1 << 0 ),
  32. //! Use DXT3 compression.
  33. kDxt3 = ( 1 << 1 ),
  34. //! Use DXT5 compression.
  35. kDxt5 = ( 1 << 2 ),
  36. //! Use BC4 compression.
  37. kBc4 = ( 1 << 3 ),
  38. //! Use BC5 compression.
  39. kBc5 = ( 1 << 4 ),
  40. //! Use a slow but high quality colour compressor (the default).
  41. kColourClusterFit = ( 1 << 5 ),
  42. //! Use a fast but low quality colour compressor.
  43. kColourRangeFit = ( 1 << 6 ),
  44. //! Weight the colour by alpha during cluster fit (disabled by default).
  45. kWeightColourByAlpha = ( 1 << 7 ),
  46. //! Use a very slow but very high quality colour compressor.
  47. kColourIterativeClusterFit = ( 1 << 8 ),
  48. //! Source is BGRA rather than RGBA
  49. kSourceBGRA = ( 1 << 9 )
  50. };
  51. // -----------------------------------------------------------------------------
  52. /*! @brief Compresses a 4x4 block of pixels.
  53. @param rgba The rgba values of the 16 source pixels.
  54. @param mask The valid pixel mask.
  55. @param block Storage for the compressed DXT block.
  56. @param flags Compression flags.
  57. @param metric An optional perceptual metric.
  58. The source pixels should be presented as a contiguous array of 16 rgba
  59. values, with each component as 1 byte each. In memory this should be:
  60. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  61. The mask parameter enables only certain pixels within the block. The lowest
  62. bit enables the first pixel and so on up to the 16th bit. Bits beyond the
  63. 16th bit are ignored. Pixels that are not enabled are allowed to take
  64. arbitrary colours in the output block. An example of how this can be used
  65. is in the CompressImage function to disable pixels outside the bounds of
  66. the image when the width or height is not divisible by 4.
  67. The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression,
  68. however, DXT1 will be used by default if none is specified. When using DXT1
  69. compression, 8 bytes of storage are required for the compressed DXT block.
  70. DXT3 and DXT5 compression require 16 bytes of storage per block.
  71. The flags parameter can also specify a preferred colour compressor to use
  72. when fitting the RGB components of the data. Possible colour compressors
  73. are: kColourClusterFit (the default), kColourRangeFit (very fast, low
  74. quality) or kColourIterativeClusterFit (slowest, best quality).
  75. When using kColourClusterFit or kColourIterativeClusterFit, an additional
  76. flag can be specified to weight the importance of each pixel by its alpha
  77. value. For images that are rendered using alpha blending, this can
  78. significantly increase the perceived quality.
  79. The metric parameter can be used to weight the relative importance of each
  80. colour channel, or pass NULL to use the default uniform weight of
  81. { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that
  82. allowed either uniform or "perceptual" weights with the fixed values
  83. { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a
  84. contiguous array of 3 floats.
  85. */
  86. void CompressMasked( u8 const* rgba, int mask, void* block, int flags, float* metric = 0 );
  87. // -----------------------------------------------------------------------------
  88. /*! @brief Compresses a 4x4 block of pixels.
  89. @param rgba The rgba values of the 16 source pixels.
  90. @param block Storage for the compressed DXT block.
  91. @param flags Compression flags.
  92. @param metric An optional perceptual metric.
  93. The source pixels should be presented as a contiguous array of 16 rgba
  94. values, with each component as 1 byte each. In memory this should be:
  95. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  96. The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression,
  97. however, DXT1 will be used by default if none is specified. When using DXT1
  98. compression, 8 bytes of storage are required for the compressed DXT block.
  99. DXT3 and DXT5 compression require 16 bytes of storage per block.
  100. The flags parameter can also specify a preferred colour compressor to use
  101. when fitting the RGB components of the data. Possible colour compressors
  102. are: kColourClusterFit (the default), kColourRangeFit (very fast, low
  103. quality) or kColourIterativeClusterFit (slowest, best quality).
  104. When using kColourClusterFit or kColourIterativeClusterFit, an additional
  105. flag can be specified to weight the importance of each pixel by its alpha
  106. value. For images that are rendered using alpha blending, this can
  107. significantly increase the perceived quality.
  108. The metric parameter can be used to weight the relative importance of each
  109. colour channel, or pass NULL to use the default uniform weight of
  110. { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that
  111. allowed either uniform or "perceptual" weights with the fixed values
  112. { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a
  113. contiguous array of 3 floats.
  114. This method is an inline that calls CompressMasked with a mask of 0xffff,
  115. provided for compatibility with older versions of squish.
  116. */
  117. inline void Compress( u8 const* rgba, void* block, int flags, float* metric = 0 )
  118. {
  119. CompressMasked( rgba, 0xffff, block, flags, metric );
  120. }
  121. // -----------------------------------------------------------------------------
  122. /*! @brief Decompresses a 4x4 block of pixels.
  123. @param rgba Storage for the 16 decompressed pixels.
  124. @param block The compressed DXT block.
  125. @param flags Compression flags.
  126. The decompressed pixels will be written as a contiguous array of 16 rgba
  127. values, with each component as 1 byte each. In memory this is:
  128. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  129. The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression,
  130. however, DXT1 will be used by default if none is specified. All other flags
  131. are ignored.
  132. */
  133. void Decompress( u8* rgba, void const* block, int flags );
  134. // -----------------------------------------------------------------------------
  135. /*! @brief Computes the amount of compressed storage required.
  136. @param width The width of the image.
  137. @param height The height of the image.
  138. @param flags Compression flags.
  139. The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression,
  140. however, DXT1 will be used by default if none is specified. All other flags
  141. are ignored.
  142. Most DXT images will be a multiple of 4 in each dimension, but this
  143. function supports arbitrary size images by allowing the outer blocks to
  144. be only partially used.
  145. */
  146. int GetStorageRequirements( int width, int height, int flags );
  147. // -----------------------------------------------------------------------------
  148. /*! @brief Compresses an image in memory.
  149. @param rgba The pixels of the source.
  150. @param width The width of the source image.
  151. @param height The height of the source image.
  152. @param pitch The pitch of the source image.
  153. @param blocks Storage for the compressed output.
  154. @param flags Compression flags.
  155. @param metric An optional perceptual metric.
  156. The source pixels should be presented as a contiguous array of width*height
  157. rgba values, with each component as 1 byte each. In memory this should be:
  158. { r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height
  159. The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression,
  160. however, DXT1 will be used by default if none is specified. When using DXT1
  161. compression, 8 bytes of storage are required for each compressed DXT block.
  162. DXT3 and DXT5 compression require 16 bytes of storage per block.
  163. The flags parameter can also specify a preferred colour compressor to use
  164. when fitting the RGB components of the data. Possible colour compressors
  165. are: kColourClusterFit (the default), kColourRangeFit (very fast, low
  166. quality) or kColourIterativeClusterFit (slowest, best quality).
  167. When using kColourClusterFit or kColourIterativeClusterFit, an additional
  168. flag can be specified to weight the importance of each pixel by its alpha
  169. value. For images that are rendered using alpha blending, this can
  170. significantly increase the perceived quality.
  171. The metric parameter can be used to weight the relative importance of each
  172. colour channel, or pass NULL to use the default uniform weight of
  173. { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that
  174. allowed either uniform or "perceptual" weights with the fixed values
  175. { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a
  176. contiguous array of 3 floats.
  177. Internally this function calls squish::CompressMasked for each block, which
  178. allows for pixels outside the image to take arbitrary values. The function
  179. squish::GetStorageRequirements can be called to compute the amount of memory
  180. to allocate for the compressed output.
  181. Note on compression quality: When compressing textures with
  182. libsquish it is recommended to apply a gamma-correction
  183. beforehand. This will reduce the blockiness in dark areas. The
  184. level of necessary gamma-correction is platform dependent. For
  185. example, a gamma correction with gamma = 0.5 before compression
  186. and gamma = 2.0 after decompression yields good results on the
  187. Windows platform but for other platforms like MacOS X a different
  188. gamma value may be more suitable.
  189. */
  190. void CompressImage( u8 const* rgba, int width, int height, int pitch, void* blocks, int flags, float* metric = 0 );
  191. void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags, float* metric = 0 );
  192. // -----------------------------------------------------------------------------
  193. /*! @brief Decompresses an image in memory.
  194. @param rgba Storage for the decompressed pixels.
  195. @param width The width of the source image.
  196. @param height The height of the source image.
  197. @param pitch The pitch of the decompressed pixels.
  198. @param blocks The compressed DXT blocks.
  199. @param flags Compression flags.
  200. The decompressed pixels will be written as a contiguous array of width*height
  201. 16 rgba values, with each component as 1 byte each. In memory this is:
  202. { r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height
  203. The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression,
  204. however, DXT1 will be used by default if none is specified. All other flags
  205. are ignored.
  206. Internally this function calls squish::Decompress for each block.
  207. */
  208. void DecompressImage( u8* rgba, int width, int height, int pitch, void const* blocks, int flags );
  209. void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags );
  210. // -----------------------------------------------------------------------------
  211. /*! @brief Computes MSE of an compressed image in memory.
  212. @param rgba The original image pixels.
  213. @param width The width of the source image.
  214. @param height The height of the source image.
  215. @param pitch The pitch of the source image.
  216. @param dxt The compressed dxt blocks
  217. @param flags Compression flags.
  218. @param colourMSE The MSE of the colour values.
  219. @param alphaMSE The MSE of the alpha values.
  220. The colour MSE and alpha MSE are computed across all pixels. The colour MSE is
  221. averaged across all rgb values (i.e. colourMSE = sum sum_k ||dxt.k - rgba.k||/3)
  222. The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression,
  223. however, DXT1 will be used by default if none is specified. All other flags
  224. are ignored.
  225. Internally this function calls squish::Decompress for each block.
  226. */
  227. void ComputeMSE(u8 const *rgba, int width, int height, int pitch, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE);
  228. void ComputeMSE(u8 const *rgba, int width, int height, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE);
  229. // -----------------------------------------------------------------------------
  230. } // namespace squish
  231. #endif // ndef SQUISH_H