1*2f083884Ss.makeev_local /* -----------------------------------------------------------------------------
2*2f083884Ss.makeev_local
3*2f083884Ss.makeev_local Copyright (c) 2006 Simon Brown [email protected]
4*2f083884Ss.makeev_local
5*2f083884Ss.makeev_local Permission is hereby granted, free of charge, to any person obtaining
6*2f083884Ss.makeev_local a copy of this software and associated documentation files (the
7*2f083884Ss.makeev_local "Software"), to deal in the Software without restriction, including
8*2f083884Ss.makeev_local without limitation the rights to use, copy, modify, merge, publish,
9*2f083884Ss.makeev_local distribute, sublicense, and/or sell copies of the Software, and to
10*2f083884Ss.makeev_local permit persons to whom the Software is furnished to do so, subject to
11*2f083884Ss.makeev_local the following conditions:
12*2f083884Ss.makeev_local
13*2f083884Ss.makeev_local The above copyright notice and this permission notice shall be included
14*2f083884Ss.makeev_local in all copies or substantial portions of the Software.
15*2f083884Ss.makeev_local
16*2f083884Ss.makeev_local THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17*2f083884Ss.makeev_local OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*2f083884Ss.makeev_local MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19*2f083884Ss.makeev_local IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20*2f083884Ss.makeev_local CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21*2f083884Ss.makeev_local TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22*2f083884Ss.makeev_local SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*2f083884Ss.makeev_local
24*2f083884Ss.makeev_local -------------------------------------------------------------------------- */
25*2f083884Ss.makeev_local
26*2f083884Ss.makeev_local #include <squish.h>
27*2f083884Ss.makeev_local #include "colourset.h"
28*2f083884Ss.makeev_local #include "maths.h"
29*2f083884Ss.makeev_local #include "rangefit.h"
30*2f083884Ss.makeev_local #include "clusterfit.h"
31*2f083884Ss.makeev_local #include "colourblock.h"
32*2f083884Ss.makeev_local #include "alpha.h"
33*2f083884Ss.makeev_local #include "singlecolourfit.h"
34*2f083884Ss.makeev_local
35*2f083884Ss.makeev_local namespace squish {
36*2f083884Ss.makeev_local
FixFlags(int flags)37*2f083884Ss.makeev_local static int FixFlags( int flags )
38*2f083884Ss.makeev_local {
39*2f083884Ss.makeev_local // grab the flag bits
40*2f083884Ss.makeev_local int method = flags & ( kDxt1 | kDxt3 | kDxt5 );
41*2f083884Ss.makeev_local int fit = flags & ( kColourIterativeClusterFit | kColourClusterFit | kColourRangeFit );
42*2f083884Ss.makeev_local int metric = flags & ( kColourMetricPerceptual | kColourMetricUniform );
43*2f083884Ss.makeev_local int extra = flags & kWeightColourByAlpha;
44*2f083884Ss.makeev_local
45*2f083884Ss.makeev_local // set defaults
46*2f083884Ss.makeev_local if( method != kDxt3 && method != kDxt5 )
47*2f083884Ss.makeev_local method = kDxt1;
48*2f083884Ss.makeev_local if( fit != kColourRangeFit )
49*2f083884Ss.makeev_local fit = kColourClusterFit;
50*2f083884Ss.makeev_local if( metric != kColourMetricUniform )
51*2f083884Ss.makeev_local metric = kColourMetricPerceptual;
52*2f083884Ss.makeev_local
53*2f083884Ss.makeev_local // done
54*2f083884Ss.makeev_local return method | fit | metric | extra;
55*2f083884Ss.makeev_local }
56*2f083884Ss.makeev_local
Compress(u8 const * rgba,void * block,int flags)57*2f083884Ss.makeev_local void Compress( u8 const* rgba, void* block, int flags )
58*2f083884Ss.makeev_local {
59*2f083884Ss.makeev_local // compress with full mask
60*2f083884Ss.makeev_local CompressMasked( rgba, 0xffff, block, flags );
61*2f083884Ss.makeev_local }
62*2f083884Ss.makeev_local
CompressMasked(u8 const * rgba,int mask,void * block,int flags)63*2f083884Ss.makeev_local void CompressMasked( u8 const* rgba, int mask, void* block, int flags )
64*2f083884Ss.makeev_local {
65*2f083884Ss.makeev_local // fix any bad flags
66*2f083884Ss.makeev_local flags = FixFlags( flags );
67*2f083884Ss.makeev_local
68*2f083884Ss.makeev_local // get the block locations
69*2f083884Ss.makeev_local void* colourBlock = block;
70*2f083884Ss.makeev_local void* alphaBock = block;
71*2f083884Ss.makeev_local if( ( flags & ( kDxt3 | kDxt5 ) ) != 0 )
72*2f083884Ss.makeev_local colourBlock = reinterpret_cast< u8* >( block ) + 8;
73*2f083884Ss.makeev_local
74*2f083884Ss.makeev_local // create the minimal point set
75*2f083884Ss.makeev_local ColourSet colours( rgba, mask, flags );
76*2f083884Ss.makeev_local
77*2f083884Ss.makeev_local // check the compression type and compress colour
78*2f083884Ss.makeev_local if( colours.GetCount() == 1 )
79*2f083884Ss.makeev_local {
80*2f083884Ss.makeev_local // always do a single colour fit
81*2f083884Ss.makeev_local SingleColourFit fit( &colours, flags );
82*2f083884Ss.makeev_local fit.Compress( colourBlock );
83*2f083884Ss.makeev_local }
84*2f083884Ss.makeev_local else if( ( flags & kColourRangeFit ) != 0 || colours.GetCount() == 0 )
85*2f083884Ss.makeev_local {
86*2f083884Ss.makeev_local // do a range fit
87*2f083884Ss.makeev_local RangeFit fit( &colours, flags );
88*2f083884Ss.makeev_local fit.Compress( colourBlock );
89*2f083884Ss.makeev_local }
90*2f083884Ss.makeev_local else
91*2f083884Ss.makeev_local {
92*2f083884Ss.makeev_local // default to a cluster fit (could be iterative or not)
93*2f083884Ss.makeev_local ClusterFit fit( &colours, flags );
94*2f083884Ss.makeev_local fit.Compress( colourBlock );
95*2f083884Ss.makeev_local }
96*2f083884Ss.makeev_local
97*2f083884Ss.makeev_local // compress alpha separately if necessary
98*2f083884Ss.makeev_local if( ( flags & kDxt3 ) != 0 )
99*2f083884Ss.makeev_local CompressAlphaDxt3( rgba, mask, alphaBock );
100*2f083884Ss.makeev_local else if( ( flags & kDxt5 ) != 0 )
101*2f083884Ss.makeev_local CompressAlphaDxt5( rgba, mask, alphaBock );
102*2f083884Ss.makeev_local }
103*2f083884Ss.makeev_local
Decompress(u8 * rgba,void const * block,int flags)104*2f083884Ss.makeev_local void Decompress( u8* rgba, void const* block, int flags )
105*2f083884Ss.makeev_local {
106*2f083884Ss.makeev_local // fix any bad flags
107*2f083884Ss.makeev_local flags = FixFlags( flags );
108*2f083884Ss.makeev_local
109*2f083884Ss.makeev_local // get the block locations
110*2f083884Ss.makeev_local void const* colourBlock = block;
111*2f083884Ss.makeev_local void const* alphaBock = block;
112*2f083884Ss.makeev_local if( ( flags & ( kDxt3 | kDxt5 ) ) != 0 )
113*2f083884Ss.makeev_local colourBlock = reinterpret_cast< u8 const* >( block ) + 8;
114*2f083884Ss.makeev_local
115*2f083884Ss.makeev_local // decompress colour
116*2f083884Ss.makeev_local DecompressColour( rgba, colourBlock, ( flags & kDxt1 ) != 0 );
117*2f083884Ss.makeev_local
118*2f083884Ss.makeev_local // decompress alpha separately if necessary
119*2f083884Ss.makeev_local if( ( flags & kDxt3 ) != 0 )
120*2f083884Ss.makeev_local DecompressAlphaDxt3( rgba, alphaBock );
121*2f083884Ss.makeev_local else if( ( flags & kDxt5 ) != 0 )
122*2f083884Ss.makeev_local DecompressAlphaDxt5( rgba, alphaBock );
123*2f083884Ss.makeev_local }
124*2f083884Ss.makeev_local
GetStorageRequirements(int width,int height,int flags)125*2f083884Ss.makeev_local int GetStorageRequirements( int width, int height, int flags )
126*2f083884Ss.makeev_local {
127*2f083884Ss.makeev_local // fix any bad flags
128*2f083884Ss.makeev_local flags = FixFlags( flags );
129*2f083884Ss.makeev_local
130*2f083884Ss.makeev_local // compute the storage requirements
131*2f083884Ss.makeev_local int blockcount = ( ( width + 3 )/4 ) * ( ( height + 3 )/4 );
132*2f083884Ss.makeev_local int blocksize = ( ( flags & kDxt1 ) != 0 ) ? 8 : 16;
133*2f083884Ss.makeev_local return blockcount*blocksize;
134*2f083884Ss.makeev_local }
135*2f083884Ss.makeev_local
CompressImage(u8 const * rgba,int width,int height,void * blocks,int flags)136*2f083884Ss.makeev_local void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags )
137*2f083884Ss.makeev_local {
138*2f083884Ss.makeev_local // fix any bad flags
139*2f083884Ss.makeev_local flags = FixFlags( flags );
140*2f083884Ss.makeev_local
141*2f083884Ss.makeev_local // initialise the block output
142*2f083884Ss.makeev_local u8* targetBlock = reinterpret_cast< u8* >( blocks );
143*2f083884Ss.makeev_local int bytesPerBlock = ( ( flags & kDxt1 ) != 0 ) ? 8 : 16;
144*2f083884Ss.makeev_local
145*2f083884Ss.makeev_local // loop over blocks
146*2f083884Ss.makeev_local for( int y = 0; y < height; y += 4 )
147*2f083884Ss.makeev_local {
148*2f083884Ss.makeev_local for( int x = 0; x < width; x += 4 )
149*2f083884Ss.makeev_local {
150*2f083884Ss.makeev_local // build the 4x4 block of pixels
151*2f083884Ss.makeev_local u8 sourceRgba[16*4];
152*2f083884Ss.makeev_local u8* targetPixel = sourceRgba;
153*2f083884Ss.makeev_local int mask = 0;
154*2f083884Ss.makeev_local for( int py = 0; py < 4; ++py )
155*2f083884Ss.makeev_local {
156*2f083884Ss.makeev_local for( int px = 0; px < 4; ++px )
157*2f083884Ss.makeev_local {
158*2f083884Ss.makeev_local // get the source pixel in the image
159*2f083884Ss.makeev_local int sx = x + px;
160*2f083884Ss.makeev_local int sy = y + py;
161*2f083884Ss.makeev_local
162*2f083884Ss.makeev_local // enable if we're in the image
163*2f083884Ss.makeev_local if( sx < width && sy < height )
164*2f083884Ss.makeev_local {
165*2f083884Ss.makeev_local // copy the rgba value
166*2f083884Ss.makeev_local u8 const* sourcePixel = rgba + 4*( width*sy + sx );
167*2f083884Ss.makeev_local for( int i = 0; i < 4; ++i )
168*2f083884Ss.makeev_local *targetPixel++ = *sourcePixel++;
169*2f083884Ss.makeev_local
170*2f083884Ss.makeev_local // enable this pixel
171*2f083884Ss.makeev_local mask |= ( 1 << ( 4*py + px ) );
172*2f083884Ss.makeev_local }
173*2f083884Ss.makeev_local else
174*2f083884Ss.makeev_local {
175*2f083884Ss.makeev_local // skip this pixel as its outside the image
176*2f083884Ss.makeev_local targetPixel += 4;
177*2f083884Ss.makeev_local }
178*2f083884Ss.makeev_local }
179*2f083884Ss.makeev_local }
180*2f083884Ss.makeev_local
181*2f083884Ss.makeev_local // compress it into the output
182*2f083884Ss.makeev_local CompressMasked( sourceRgba, mask, targetBlock, flags );
183*2f083884Ss.makeev_local
184*2f083884Ss.makeev_local // advance
185*2f083884Ss.makeev_local targetBlock += bytesPerBlock;
186*2f083884Ss.makeev_local }
187*2f083884Ss.makeev_local }
188*2f083884Ss.makeev_local }
189*2f083884Ss.makeev_local
DecompressImage(u8 * rgba,int width,int height,void const * blocks,int flags)190*2f083884Ss.makeev_local void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags )
191*2f083884Ss.makeev_local {
192*2f083884Ss.makeev_local // fix any bad flags
193*2f083884Ss.makeev_local flags = FixFlags( flags );
194*2f083884Ss.makeev_local
195*2f083884Ss.makeev_local // initialise the block input
196*2f083884Ss.makeev_local u8 const* sourceBlock = reinterpret_cast< u8 const* >( blocks );
197*2f083884Ss.makeev_local int bytesPerBlock = ( ( flags & kDxt1 ) != 0 ) ? 8 : 16;
198*2f083884Ss.makeev_local
199*2f083884Ss.makeev_local // loop over blocks
200*2f083884Ss.makeev_local for( int y = 0; y < height; y += 4 )
201*2f083884Ss.makeev_local {
202*2f083884Ss.makeev_local for( int x = 0; x < width; x += 4 )
203*2f083884Ss.makeev_local {
204*2f083884Ss.makeev_local // decompress the block
205*2f083884Ss.makeev_local u8 targetRgba[4*16];
206*2f083884Ss.makeev_local Decompress( targetRgba, sourceBlock, flags );
207*2f083884Ss.makeev_local
208*2f083884Ss.makeev_local // write the decompressed pixels to the correct image locations
209*2f083884Ss.makeev_local u8 const* sourcePixel = targetRgba;
210*2f083884Ss.makeev_local for( int py = 0; py < 4; ++py )
211*2f083884Ss.makeev_local {
212*2f083884Ss.makeev_local for( int px = 0; px < 4; ++px )
213*2f083884Ss.makeev_local {
214*2f083884Ss.makeev_local // get the target location
215*2f083884Ss.makeev_local int sx = x + px;
216*2f083884Ss.makeev_local int sy = y + py;
217*2f083884Ss.makeev_local if( sx < width && sy < height )
218*2f083884Ss.makeev_local {
219*2f083884Ss.makeev_local u8* targetPixel = rgba + 4*( width*sy + sx );
220*2f083884Ss.makeev_local
221*2f083884Ss.makeev_local // copy the rgba value
222*2f083884Ss.makeev_local for( int i = 0; i < 4; ++i )
223*2f083884Ss.makeev_local *targetPixel++ = *sourcePixel++;
224*2f083884Ss.makeev_local }
225*2f083884Ss.makeev_local else
226*2f083884Ss.makeev_local {
227*2f083884Ss.makeev_local // skip this pixel as its outside the image
228*2f083884Ss.makeev_local sourcePixel += 4;
229*2f083884Ss.makeev_local }
230*2f083884Ss.makeev_local }
231*2f083884Ss.makeev_local }
232*2f083884Ss.makeev_local
233*2f083884Ss.makeev_local // advance
234*2f083884Ss.makeev_local sourceBlock += bytesPerBlock;
235*2f083884Ss.makeev_local }
236*2f083884Ss.makeev_local }
237*2f083884Ss.makeev_local }
238*2f083884Ss.makeev_local
239*2f083884Ss.makeev_local } // namespace squish
240