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