libsquish v1.15.1.2
S3TC/DXT compliant image compression
Loading...
Searching...
No Matches
squish Namespace Reference

All squish API functions live in this namespace.

Typedefs

typedef unsigned char u8
 Typedef a quantity that is a single unsigned byte.
 

Enumerations

enum  {
  kDxt1 = ( 1 << 0 ) ,
  kDxt3 = ( 1 << 1 ) ,
  kDxt5 = ( 1 << 2 ) ,
  kBc4 = ( 1 << 3 ) ,
  kBc5 = ( 1 << 4 ) ,
  kColourClusterFit = ( 1 << 5 ) ,
  kColourRangeFit = ( 1 << 6 ) ,
  kWeightColourByAlpha = ( 1 << 7 ) ,
  kColourIterativeClusterFit = ( 1 << 8 ) ,
  kSourceBGRA = ( 1 << 9 )
}
 

Functions

void Compress (u8 const *rgba, void *block, int flags, float *metric=0)
 Compresses a 4x4 block of pixels.
 
void CompressImage (u8 const *rgba, int width, int height, int pitch, void *blocks, int flags, float *metric=0)
 Compresses an image in memory.
 
void CompressImage (u8 const *rgba, int width, int height, void *blocks, int flags, float *metric=0)
 
void CompressMasked (u8 const *rgba, int mask, void *block, int flags, float *metric=0)
 Compresses a 4x4 block of pixels.
 
void ComputeMSE (u8 const *rgba, int width, int height, int pitch, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE)
 Computes MSE of an compressed image in memory.
 
void ComputeMSE (u8 const *rgba, int width, int height, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE)
 
void Decompress (u8 *rgba, void const *block, int flags)
 Decompresses a 4x4 block of pixels.
 
void DecompressImage (u8 *rgba, int width, int height, int pitch, void const *blocks, int flags)
 Decompresses an image in memory.
 
void DecompressImage (u8 *rgba, int width, int height, void const *blocks, int flags)
 
int GetStorageRequirements (int width, int height, int flags)
 Computes the amount of compressed storage required.
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kDxt1 

Use DXT1 compression.

kDxt3 

Use DXT3 compression.

kDxt5 

Use DXT5 compression.

kBc4 

Use BC4 compression.

kBc5 

Use BC5 compression.

kColourClusterFit 

Use a slow but high quality colour compressor (the default).

kColourRangeFit 

Use a fast but low quality colour compressor.

kWeightColourByAlpha 

Weight the colour by alpha during cluster fit (disabled by default).

kColourIterativeClusterFit 

Use a very slow but very high quality colour compressor.

kSourceBGRA 

Source is BGRA rather than RGBA.

Function Documentation

◆ Compress()

void squish::Compress ( u8 const * rgba,
void * block,
int flags,
float * metric = 0 )
inline
Parameters
rgbaThe rgba values of the 16 source pixels.
blockStorage for the compressed DXT block.
flagsCompression flags.
metricAn optional perceptual metric.

The source pixels should be presented as a contiguous array of 16 rgba values, with each component as 1 byte each. In memory this should be:

{ r1, g1, b1, a1, .... , r16, g16, b16, a16 }

The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression, however, DXT1 will be used by default if none is specified. When using DXT1 compression, 8 bytes of storage are required for the compressed DXT block. DXT3 and DXT5 compression require 16 bytes of storage per block.

The flags parameter can also specify a preferred colour compressor to use when fitting the RGB components of the data. Possible colour compressors are: kColourClusterFit (the default), kColourRangeFit (very fast, low quality) or kColourIterativeClusterFit (slowest, best quality).

When using kColourClusterFit or kColourIterativeClusterFit, an additional flag can be specified to weight the importance of each pixel by its alpha value. For images that are rendered using alpha blending, this can significantly increase the perceived quality.

The metric parameter can be used to weight the relative importance of each colour channel, or pass NULL to use the default uniform weight of { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that allowed either uniform or "perceptual" weights with the fixed values { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a contiguous array of 3 floats.

This method is an inline that calls CompressMasked with a mask of 0xffff, provided for compatibility with older versions of squish.

◆ CompressImage() [1/2]

void squish::CompressImage ( u8 const * rgba,
int width,
int height,
int pitch,
void * blocks,
int flags,
float * metric = 0 )
Parameters
rgbaThe pixels of the source.
widthThe width of the source image.
heightThe height of the source image.
pitchThe pitch of the source image.
blocksStorage for the compressed output.
flagsCompression flags.
metricAn optional perceptual metric.

The source pixels should be presented as a contiguous array of width*height rgba values, with each component as 1 byte each. In memory this should be:

{ r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height

The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression, however, DXT1 will be used by default if none is specified. When using DXT1 compression, 8 bytes of storage are required for each compressed DXT block. DXT3 and DXT5 compression require 16 bytes of storage per block.

The flags parameter can also specify a preferred colour compressor to use when fitting the RGB components of the data. Possible colour compressors are: kColourClusterFit (the default), kColourRangeFit (very fast, low quality) or kColourIterativeClusterFit (slowest, best quality).

When using kColourClusterFit or kColourIterativeClusterFit, an additional flag can be specified to weight the importance of each pixel by its alpha value. For images that are rendered using alpha blending, this can significantly increase the perceived quality.

The metric parameter can be used to weight the relative importance of each colour channel, or pass NULL to use the default uniform weight of { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that allowed either uniform or "perceptual" weights with the fixed values { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a contiguous array of 3 floats.

Internally this function calls squish::CompressMasked for each block, which allows for pixels outside the image to take arbitrary values. The function squish::GetStorageRequirements can be called to compute the amount of memory to allocate for the compressed output.

Note on compression quality: When compressing textures with libsquish it is recommended to apply a gamma-correction beforehand. This will reduce the blockiness in dark areas. The level of necessary gamma-correction is platform dependent. For example, a gamma correction with gamma = 0.5 before compression and gamma = 2.0 after decompression yields good results on the Windows platform but for other platforms like MacOS X a different gamma value may be more suitable.

◆ CompressImage() [2/2]

void squish::CompressImage ( u8 const * rgba,
int width,
int height,
void * blocks,
int flags,
float * metric = 0 )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ CompressMasked()

void squish::CompressMasked ( u8 const * rgba,
int mask,
void * block,
int flags,
float * metric = 0 )
Parameters
rgbaThe rgba values of the 16 source pixels.
maskThe valid pixel mask.
blockStorage for the compressed DXT block.
flagsCompression flags.
metricAn optional perceptual metric.

The source pixels should be presented as a contiguous array of 16 rgba values, with each component as 1 byte each. In memory this should be:

{ r1, g1, b1, a1, .... , r16, g16, b16, a16 }

The mask parameter enables only certain pixels within the block. The lowest bit enables the first pixel and so on up to the 16th bit. Bits beyond the 16th bit are ignored. Pixels that are not enabled are allowed to take arbitrary colours in the output block. An example of how this can be used is in the CompressImage function to disable pixels outside the bounds of the image when the width or height is not divisible by 4.

The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression, however, DXT1 will be used by default if none is specified. When using DXT1 compression, 8 bytes of storage are required for the compressed DXT block. DXT3 and DXT5 compression require 16 bytes of storage per block.

The flags parameter can also specify a preferred colour compressor to use when fitting the RGB components of the data. Possible colour compressors are: kColourClusterFit (the default), kColourRangeFit (very fast, low quality) or kColourIterativeClusterFit (slowest, best quality).

When using kColourClusterFit or kColourIterativeClusterFit, an additional flag can be specified to weight the importance of each pixel by its alpha value. For images that are rendered using alpha blending, this can significantly increase the perceived quality.

The metric parameter can be used to weight the relative importance of each colour channel, or pass NULL to use the default uniform weight of { 1.0f, 1.0f, 1.0f }. This replaces the previous flag-based control that allowed either uniform or "perceptual" weights with the fixed values { 0.2126f, 0.7152f, 0.0722f }. If non-NULL, the metric should point to a contiguous array of 3 floats.

◆ ComputeMSE() [1/2]

void squish::ComputeMSE ( u8 const * rgba,
int width,
int height,
int pitch,
u8 const * dxt,
int flags,
double & colourMSE,
double & alphaMSE )
Parameters
rgbaThe original image pixels.
widthThe width of the source image.
heightThe height of the source image.
pitchThe pitch of the source image.
dxtThe compressed dxt blocks
flagsCompression flags.
colourMSEThe MSE of the colour values.
alphaMSEThe MSE of the alpha values.

The colour MSE and alpha MSE are computed across all pixels. The colour MSE is averaged across all rgb values (i.e. colourMSE = sum sum_k ||dxt.k - rgba.k||/3)

The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression, however, DXT1 will be used by default if none is specified. All other flags are ignored.

Internally this function calls squish::Decompress for each block.

◆ ComputeMSE() [2/2]

void squish::ComputeMSE ( u8 const * rgba,
int width,
int height,
u8 const * dxt,
int flags,
double & colourMSE,
double & alphaMSE )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ Decompress()

void squish::Decompress ( u8 * rgba,
void const * block,
int flags )
Parameters
rgbaStorage for the 16 decompressed pixels.
blockThe compressed DXT block.
flagsCompression flags.

The decompressed pixels will be written as a contiguous array of 16 rgba values, with each component as 1 byte each. In memory this is:

{ r1, g1, b1, a1, .... , r16, g16, b16, a16 }

The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression, however, DXT1 will be used by default if none is specified. All other flags are ignored.

◆ DecompressImage() [1/2]

void squish::DecompressImage ( u8 * rgba,
int width,
int height,
int pitch,
void const * blocks,
int flags )
Parameters
rgbaStorage for the decompressed pixels.
widthThe width of the source image.
heightThe height of the source image.
pitchThe pitch of the decompressed pixels.
blocksThe compressed DXT blocks.
flagsCompression flags.

The decompressed pixels will be written as a contiguous array of width*height 16 rgba values, with each component as 1 byte each. In memory this is:

{ r1, g1, b1, a1, .... , rn, gn, bn, an } for n = width*height

The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression, however, DXT1 will be used by default if none is specified. All other flags are ignored.

Internally this function calls squish::Decompress for each block.

◆ DecompressImage() [2/2]

void squish::DecompressImage ( u8 * rgba,
int width,
int height,
void const * blocks,
int flags )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ GetStorageRequirements()

int squish::GetStorageRequirements ( int width,
int height,
int flags )
Parameters
widthThe width of the image.
heightThe height of the image.
flagsCompression flags.

The flags parameter should specify kDxt1, kDxt3, kDxt5, kBc4, or kBc5 compression, however, DXT1 will be used by default if none is specified. All other flags are ignored.

Most DXT images will be a multiple of 4 in each dimension, but this function supports arbitrary size images by allowing the outer blocks to be only partially used.