squish.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown [email protected]
  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. };
  49. // -----------------------------------------------------------------------------
  50. /*! @brief Compresses a 4x4 block of pixels.
  51. @param rgba The rgba values of the 16 source pixels.
  52. @param mask The valid pixel mask.
  53. @param block Storage for the compressed DXT block.
  54. @param flags Compression flags.
  55. @param metric An optional perceptual metric.
  56. The source pixels should be presented as a contiguous array of 16 rgba
  57. values, with each component as 1 byte each. In memory this should be:
  58. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  59. The mask parameter enables only certain pixels within the block. The lowest
  60. bit enables the first pixel and so on up to the 16th bit. Bits beyond the
  61. 16th bit are ignored. Pixels that are not enabled are allowed to take
  62. arbitrary colours in the output block. An example of how this can be used
  63. is in the CompressImage function to disable pixels outside the bounds of
  64. the image when the width or height is not divisible by 4.
  65. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  66. however, DXT1 will be used by default if none is specified. When using DXT1
  67. compression, 8 bytes of storage are required for the compressed DXT block.
  68. DXT3 and DXT5 compression require 16 bytes of storage per block.
  69. The flags parameter can also specify a preferred colour compressor to use
  70. when fitting the RGB components of the data. Possible colour compressors
  71. are: kColourClusterFit (the default), kColourRangeFit (very fast, low
  72. quality) or kColourIterativeClusterFit (slowest, best quality).
  73. When using kColourClusterFit or kColourIterativeClusterFit, an additional
  74. flag can be specified to weight the importance of each pixel by its alpha
  75. value. For images that are rendered using alpha blending, this can
  76. significantly increase the perceived quality.
  77. The metric parameter can be used to weight the relative importance of each
  78. colour channel, or pass NULL to use the default uniform weight of
  79. { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that
  80. allowed either uniform or "perceptual" weights with the fixed values
  81. { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a
  82. contiguous array of 3 floats.
  83. */
  84. void CompressMasked( u8 const* rgba, int mask, void* block, int flags, float* metric = 0 );
  85. // -----------------------------------------------------------------------------
  86. /*! @brief Compresses a 4x4 block of pixels.
  87. @param rgba The rgba values of the 16 source pixels.
  88. @param block Storage for the compressed DXT block.
  89. @param flags Compression flags.
  90. @param metric An optional perceptual metric.
  91. The source pixels should be presented as a contiguous array of 16 rgba
  92. values, with each component as 1 byte each. In memory this should be:
  93. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  94. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  95. however, DXT1 will be used by default if none is specified. When using DXT1
  96. compression, 8 bytes of storage are required for the compressed DXT block.
  97. DXT3 and DXT5 compression require 16 bytes of storage per block.
  98. The flags parameter can also specify a preferred colour compressor to use
  99. when fitting the RGB components of the data. Possible colour compressors
  100. are: kColourClusterFit (the default), kColourRangeFit (very fast, low
  101. quality) or kColourIterativeClusterFit (slowest, best quality).
  102. When using kColourClusterFit or kColourIterativeClusterFit, an additional
  103. flag can be specified to weight the importance of each pixel by its alpha
  104. value. For images that are rendered using alpha blending, this can
  105. significantly increase the perceived quality.
  106. The metric parameter can be used to weight the relative importance of each
  107. colour channel, or pass NULL to use the default uniform weight of
  108. { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that
  109. allowed either uniform or "perceptual" weights with the fixed values
  110. { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a
  111. contiguous array of 3 floats.
  112. This method is an inline that calls CompressMasked with a mask of 0xffff,
  113. provided for compatibility with older versions of squish.
  114. */
  115. inline void Compress( u8 const* rgba, void* block, int flags, float* metric = 0 )
  116. {
  117. CompressMasked( rgba, 0xffff, block, flags, metric );
  118. }
  119. // -----------------------------------------------------------------------------
  120. /*! @brief Decompresses a 4x4 block of pixels.
  121. @param rgba Storage for the 16 decompressed pixels.
  122. @param block The compressed DXT block.
  123. @param flags Compression flags.
  124. The decompressed pixels will be written as a contiguous array of 16 rgba
  125. values, with each component as 1 byte each. In memory this is:
  126. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  127. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  128. however, DXT1 will be used by default if none is specified. All other flags
  129. are ignored.
  130. */
  131. void Decompress( u8* rgba, void const* block, int flags );
  132. // -----------------------------------------------------------------------------
  133. /*! @brief Computes the amount of compressed storage required.
  134. @param width The width of the image.
  135. @param height The height of the image.
  136. @param flags Compression flags.
  137. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  138. however, DXT1 will be used by default if none is specified. All other flags
  139. are ignored.
  140. Most DXT images will be a multiple of 4 in each dimension, but this
  141. function supports arbitrary size images by allowing the outer blocks to
  142. be only partially used.
  143. */
  144. int GetStorageRequirements( int width, int height, int flags );
  145. // -----------------------------------------------------------------------------
  146. /*! @brief Compresses an image in memory.
  147. @param rgba The pixels of the source.
  148. @param width The width of the source image.
  149. @param height The height of the source image.
  150. @param blocks Storage for the compressed output.
  151. @param flags Compression flags.
  152. @param metric An optional perceptual metric.
  153. The source pixels should be presented as a contiguous array of width*height
  154. rgba values, with each component as 1 byte each. In memory this should be:
  155. { r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height
  156. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  157. however, DXT1 will be used by default if none is specified. When using DXT1
  158. compression, 8 bytes of storage are required for each compressed DXT block.
  159. DXT3 and DXT5 compression require 16 bytes of storage per block.
  160. The flags parameter can also specify a preferred colour compressor to use
  161. when fitting the RGB components of the data. Possible colour compressors
  162. are: kColourClusterFit (the default), kColourRangeFit (very fast, low
  163. quality) or kColourIterativeClusterFit (slowest, best quality).
  164. When using kColourClusterFit or kColourIterativeClusterFit, an additional
  165. flag can be specified to weight the importance of each pixel by its alpha
  166. value. For images that are rendered using alpha blending, this can
  167. significantly increase the perceived quality.
  168. The metric parameter can be used to weight the relative importance of each
  169. colour channel, or pass NULL to use the default uniform weight of
  170. { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that
  171. allowed either uniform or "perceptual" weights with the fixed values
  172. { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a
  173. contiguous array of 3 floats.
  174. Internally this function calls squish::CompressMasked for each block, which
  175. allows for pixels outside the image to take arbitrary values. The function
  176. squish::GetStorageRequirements can be called to compute the amount of memory
  177. to allocate for the compressed output.
  178. */
  179. void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags, float* metric = 0 );
  180. // -----------------------------------------------------------------------------
  181. /*! @brief Decompresses an image in memory.
  182. @param rgba Storage for the decompressed pixels.
  183. @param width The width of the source image.
  184. @param height The height of the source image.
  185. @param blocks The compressed DXT blocks.
  186. @param flags Compression flags.
  187. The decompressed pixels will be written as a contiguous array of width*height
  188. 16 rgba values, with each component as 1 byte each. In memory this is:
  189. { r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height
  190. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  191. however, DXT1 will be used by default if none is specified. All other flags
  192. are ignored.
  193. Internally this function calls squish::Decompress for each block.
  194. */
  195. void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags );
  196. // -----------------------------------------------------------------------------
  197. } // namespace squish
  198. #endif // ndef SQUISH_H