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