squish.h 14 KB

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