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