squish.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 a very slow but very high quality colour compressor.
  37. kColourIterativeClusterFit = ( 1 << 8 ),
  38. //! Use a slow but high quality colour compressor (the default).
  39. kColourClusterFit = ( 1 << 3 ),
  40. //! Use a fast but low quality colour compressor.
  41. kColourRangeFit = ( 1 << 4 ),
  42. //! Use a perceptual metric for colour error (the default).
  43. kColourMetricPerceptual = ( 1 << 5 ),
  44. //! Use a uniform metric for colour error.
  45. kColourMetricUniform = ( 1 << 6 ),
  46. //! Weight the colour by alpha during cluster fit (disabled by default).
  47. kWeightColourByAlpha = ( 1 << 7 )
  48. };
  49. // -----------------------------------------------------------------------------
  50. /*! @brief Compresses a 4x4 block of pixels.
  51. @param rgba The rgba values of the 16 source pixels.
  52. @param block Storage for the compressed DXT block.
  53. @param flags Compression flags.
  54. The source pixels should be presented as a contiguous array of 16 rgba
  55. values, with each component as 1 byte each. In memory this should be:
  56. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  57. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  58. however, DXT1 will be used by default if none is specified. When using DXT1
  59. compression, 8 bytes of storage are required for the compressed DXT block.
  60. DXT3 and DXT5 compression require 16 bytes of storage per block.
  61. The flags parameter can also specify a preferred colour compressor and
  62. colour error metric to use when fitting the RGB components of the data.
  63. Possible colour compressors are: kColourClusterFit (the default),
  64. kColourRangeFit or kColourIterativeClusterFit. Possible colour error metrics
  65. are: kColourMetricPerceptual (the default) or kColourMetricUniform. If no
  66. flags are specified in any particular category then the default will be
  67. used. Unknown flags are ignored.
  68. When using kColourClusterFit, an additional flag can be specified to
  69. weight the colour of each pixel by its alpha value. For images that are
  70. rendered using alpha blending, this can significantly increase the
  71. perceived quality.
  72. */
  73. void Compress( u8 const* rgba, void* block, int flags );
  74. // -----------------------------------------------------------------------------
  75. /*! @brief Compresses a 4x4 block of pixels.
  76. @param rgba The rgba values of the 16 source pixels.
  77. @param mask The valid pixel mask.
  78. @param block Storage for the compressed DXT block.
  79. @param flags Compression flags.
  80. The source pixels should be presented as a contiguous array of 16 rgba
  81. values, with each component as 1 byte each. In memory this should be:
  82. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  83. The mask parameter enables only certain pixels within the block. The lowest
  84. bit enables the first pixel and so on up to the 16th bit. Bits beyond the
  85. 16th bit are ignored. Pixels that are not enabled are allowed to take
  86. arbitrary colours in the output block. An example of how this can be used
  87. is in the CompressImage function to disable pixels outside the bounds of
  88. the image when the width or height is not divisible by 4.
  89. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  90. however, DXT1 will be used by default if none is specified. When using DXT1
  91. compression, 8 bytes of storage are required for the compressed DXT block.
  92. DXT3 and DXT5 compression require 16 bytes of storage per block.
  93. The flags parameter can also specify a preferred colour compressor and
  94. colour error metric to use when fitting the RGB components of the data.
  95. Possible colour compressors are: kColourClusterFit (the default),
  96. kColourRangeFit or kColourIterativeClusterFit. Possible colour error metrics
  97. are: kColourMetricPerceptual (the default) or kColourMetricUniform. If no
  98. flags are specified in any particular category then the default will be
  99. used. Unknown flags are ignored.
  100. When using kColourClusterFit, an additional flag can be specified to
  101. weight the colour of each pixel by its alpha value. For images that are
  102. rendered using alpha blending, this can significantly increase the
  103. perceived quality.
  104. */
  105. void CompressMasked( u8 const* rgba, int mask, void* block, int flags );
  106. // -----------------------------------------------------------------------------
  107. /*! @brief Decompresses a 4x4 block of pixels.
  108. @param rgba Storage for the 16 decompressed pixels.
  109. @param block The compressed DXT block.
  110. @param flags Compression flags.
  111. The decompressed pixels will be written as a contiguous array of 16 rgba
  112. values, with each component as 1 byte each. In memory this is:
  113. { r1, g1, b1, a1, .... , r16, g16, b16, a16 }
  114. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  115. however, DXT1 will be used by default if none is specified. All other flags
  116. are ignored.
  117. */
  118. void Decompress( u8* rgba, void const* block, int flags );
  119. // -----------------------------------------------------------------------------
  120. /*! @brief Computes the amount of compressed storage required.
  121. @param width The width of the image.
  122. @param height The height of the image.
  123. @param flags Compression flags.
  124. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  125. however, DXT1 will be used by default if none is specified. All other flags
  126. are ignored.
  127. Most DXT images will be a multiple of 4 in each dimension, but this
  128. function supports arbitrary size images by allowing the outer blocks to
  129. be only partially used.
  130. */
  131. int GetStorageRequirements( int width, int height, int flags );
  132. // -----------------------------------------------------------------------------
  133. /*! @brief Compresses an image in memory.
  134. @param rgba The pixels of the source.
  135. @param width The width of the source image.
  136. @param height The height of the source image.
  137. @param blocks Storage for the compressed output.
  138. @param flags Compression flags.
  139. The source pixels should be presented as a contiguous array of width*height
  140. rgba values, with each component as 1 byte each. In memory this should be:
  141. { r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height
  142. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  143. however, DXT1 will be used by default if none is specified. When using DXT1
  144. compression, 8 bytes of storage are required for each compressed DXT block.
  145. DXT3 and DXT5 compression require 16 bytes of storage per block.
  146. The flags parameter can also specify a preferred colour compressor and
  147. colour error metric to use when fitting the RGB components of the data.
  148. Possible colour compressors are: kColourClusterFit (the default),
  149. kColourRangeFit or kColourIterativeClusterFit. Possible colour error metrics
  150. are: kColourMetricPerceptual (the default) or kColourMetricUniform. If no
  151. flags are specified in any particular category then the default will be
  152. used. Unknown flags are ignored.
  153. When using kColourClusterFit, an additional flag can be specified to
  154. weight the colour of each pixel by its alpha value. For images that are
  155. rendered using alpha blending, this can significantly increase the
  156. perceived quality.
  157. Internally this function calls squish::Compress for each block. To see how
  158. much memory is required in the compressed image, use
  159. squish::GetStorageRequirements.
  160. */
  161. void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags );
  162. // -----------------------------------------------------------------------------
  163. /*! @brief Decompresses an image in memory.
  164. @param rgba Storage for the decompressed pixels.
  165. @param width The width of the source image.
  166. @param height The height of the source image.
  167. @param blocks The compressed DXT blocks.
  168. @param flags Compression flags.
  169. The decompressed pixels will be written as a contiguous array of width*height
  170. 16 rgba values, with each component as 1 byte each. In memory this is:
  171. { r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height
  172. The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression,
  173. however, DXT1 will be used by default if none is specified. All other flags
  174. are ignored.
  175. Internally this function calls squish::Decompress for each block.
  176. */
  177. void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags );
  178. // -----------------------------------------------------------------------------
  179. } // namespace squish
  180. #endif // ndef SQUISH_H