1 /* ----------------------------------------------------------------------------- 2 3 Copyright (c) 2006 Simon Brown si@sjbrown.co.uk 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be included 14 in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 -------------------------------------------------------------------------- */ 25 26 #ifndef SQUISH_H 27 #define SQUISH_H 28 29 //! All squish API functions live in this namespace. 30 namespace squish { 31 32 // ----------------------------------------------------------------------------- 33 34 //! Typedef a quantity that is a single unsigned byte. 35 typedef unsigned char u8; 36 37 // ----------------------------------------------------------------------------- 38 39 enum 40 { 41 //! Use DXT1 compression. 42 kDxt1 = ( 1 << 0 ), 43 44 //! Use DXT3 compression. 45 kDxt3 = ( 1 << 1 ), 46 47 //! Use DXT5 compression. 48 kDxt5 = ( 1 << 2 ), 49 50 //! Use a slow but high quality colour compressor (the default). 51 kColourClusterFit = ( 1 << 3 ), 52 53 //! Use a fast but low quality colour compressor. 54 kColourRangeFit = ( 1 << 4 ), 55 56 //! Use a perceptual metric for colour error (the default). 57 kColourMetricPerceptual = ( 1 << 5 ), 58 59 //! Use a uniform metric for colour error. 60 kColourMetricUniform = ( 1 << 6 ), 61 62 //! Weight the colour by alpha during cluster fit (disabled by default). 63 kWeightColourByAlpha = ( 1 << 7 ) 64 }; 65 66 // ----------------------------------------------------------------------------- 67 68 /*! @brief Compresses a 4x4 block of pixels. 69 70 @param rgba The rgba values of the 16 source pixels. 71 @param block Storage for the compressed DXT block. 72 @param flags Compression flags. 73 74 The source pixels should be presented as a contiguous array of 16 rgba 75 values, with each component as 1 byte each. In memory this should be: 76 77 { r1, g1, b1, a1, .... , r16, g16, b16, a16 } 78 79 The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression, 80 however, DXT1 will be used by default if none is specified. When using DXT1 81 compression, 8 bytes of storage are required for the compressed DXT block. 82 DXT3 and DXT5 compression require 16 bytes of storage per block. 83 84 The flags parameter can also specify a preferred colour compressor and 85 colour error metric to use when fitting the RGB components of the data. 86 Possible colour compressors are: kColourClusterFit (the default) or 87 kColourRangeFit. Possible colour error metrics are: kColourMetricPerceptual 88 (the default) or kColourMetricUniform. If no flags are specified in any 89 particular category then the default will be used. Unknown flags are 90 ignored. 91 92 When using kColourClusterFit, an additional flag can be specified to 93 weight the colour of each pixel by its alpha value. For images that are 94 rendered using alpha blending, this can significantly increase the 95 perceived quality. 96 */ 97 void Compress( u8 const* rgba, void* block, int flags ); 98 99 // ----------------------------------------------------------------------------- 100 101 /*! @brief Compresses a 4x4 block of pixels. 102 103 @param rgba The rgba values of the 16 source pixels. 104 @param mask The valid pixel mask. 105 @param block Storage for the compressed DXT block. 106 @param flags Compression flags. 107 108 The source pixels should be presented as a contiguous array of 16 rgba 109 values, with each component as 1 byte each. In memory this should be: 110 111 { r1, g1, b1, a1, .... , r16, g16, b16, a16 } 112 113 The mask parameter enables only certain pixels within the block. The lowest 114 bit enables the first pixel and so on up to the 16th bit. Bits beyond the 115 16th bit are ignored. Pixels that are not enabled are allowed to take 116 arbitrary colours in the output block. An example of how this can be used 117 is in the CompressImage function to disable pixels outside the bounds of 118 the image when the width or height is not divisible by 4. 119 120 The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression, 121 however, DXT1 will be used by default if none is specified. When using DXT1 122 compression, 8 bytes of storage are required for the compressed DXT block. 123 DXT3 and DXT5 compression require 16 bytes of storage per block. 124 125 The flags parameter can also specify a preferred colour compressor and 126 colour error metric to use when fitting the RGB components of the data. 127 Possible colour compressors are: kColourClusterFit (the default) or 128 kColourRangeFit. Possible colour error metrics are: kColourMetricPerceptual 129 (the default) or kColourMetricUniform. If no flags are specified in any 130 particular category then the default will be used. Unknown flags are 131 ignored. 132 133 When using kColourClusterFit, an additional flag can be specified to 134 weight the colour of each pixel by its alpha value. For images that are 135 rendered using alpha blending, this can significantly increase the 136 perceived quality. 137 */ 138 void CompressMasked( u8 const* rgba, int mask, void* block, int flags ); 139 140 // ----------------------------------------------------------------------------- 141 142 /*! @brief Decompresses a 4x4 block of pixels. 143 144 @param rgba Storage for the 16 decompressed pixels. 145 @param block The compressed DXT block. 146 @param flags Compression flags. 147 148 The decompressed pixels will be written as a contiguous array of 16 rgba 149 values, with each component as 1 byte each. In memory this is: 150 151 { r1, g1, b1, a1, .... , r16, g16, b16, a16 } 152 153 The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression, 154 however, DXT1 will be used by default if none is specified. All other flags 155 are ignored. 156 */ 157 void Decompress( u8* rgba, void const* block, int flags ); 158 159 // ----------------------------------------------------------------------------- 160 161 /*! @brief Computes the amount of compressed storage required. 162 163 @param width The width of the image. 164 @param height The height of the image. 165 @param flags Compression flags. 166 167 The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression, 168 however, DXT1 will be used by default if none is specified. All other flags 169 are ignored. 170 171 Most DXT images will be a multiple of 4 in each dimension, but this 172 function supports arbitrary size images by allowing the outer blocks to 173 be only partially used. 174 */ 175 int GetStorageRequirements( int width, int height, int flags ); 176 177 // ----------------------------------------------------------------------------- 178 179 /*! @brief Compresses an image in memory. 180 181 @param rgba The pixels of the source. 182 @param width The width of the source image. 183 @param height The height of the source image. 184 @param blocks Storage for the compressed output. 185 @param flags Compression flags. 186 187 The source pixels should be presented as a contiguous array of width*height 188 rgba values, with each component as 1 byte each. In memory this should be: 189 190 { r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height 191 192 The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression, 193 however, DXT1 will be used by default if none is specified. When using DXT1 194 compression, 8 bytes of storage are required for each compressed DXT block. 195 DXT3 and DXT5 compression require 16 bytes of storage per block. 196 197 The flags parameter can also specify a preferred colour compressor and 198 colour error metric to use when fitting the RGB components of the data. 199 Possible colour compressors are: kColourClusterFit (the default) or 200 kColourRangeFit. Possible colour error metrics are: kColourMetricPerceptual 201 (the default) or kColourMetricUniform. If no flags are specified in any 202 particular category then the default will be used. Unknown flags are 203 ignored. 204 205 When using kColourClusterFit, an additional flag can be specified to 206 weight the colour of each pixel by its alpha value. For images that are 207 rendered using alpha blending, this can significantly increase the 208 perceived quality. 209 210 Internally this function calls squish::Compress for each block. To see how 211 much memory is required in the compressed image, use 212 squish::GetStorageRequirements. 213 */ 214 void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags ); 215 216 // ----------------------------------------------------------------------------- 217 218 /*! @brief Decompresses an image in memory. 219 220 @param rgba Storage for the decompressed pixels. 221 @param width The width of the source image. 222 @param height The height of the source image. 223 @param blocks The compressed DXT blocks. 224 @param flags Compression flags. 225 226 The decompressed pixels will be written as a contiguous array of width*height 227 16 rgba values, with each component as 1 byte each. In memory this is: 228 229 { r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height 230 231 The flags parameter should specify either kDxt1, kDxt3 or kDxt5 compression, 232 however, DXT1 will be used by default if none is specified. All other flags 233 are ignored. 234 235 Internally this function calls squish::Decompress for each block. 236 */ 237 void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags ); 238 239 // ----------------------------------------------------------------------------- 240 241 } // namespace squish 242 243 #endif // ndef SQUISH_H 244 245