medfall

A super great game engine
Log | Files | Refs

squish.cc (7017B)


      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 #include "squish.h"
     27 #include "colourset.h"
     28 #include "maths.h"
     29 #include "rangefit.h"
     30 #include "clusterfit.h"
     31 #include "colourblock.h"
     32 #include "alpha.h"
     33 #include "singlecolourfit.h"
     34 
     35 namespace squish {
     36 
     37 static int FixFlags( int flags )
     38 {
     39 	// grab the flag bits
     40 	int method = flags & ( kDxt1 | kDxt3 | kDxt5 | kBc4 | kBc5 );
     41 	int fit = flags & ( kColourIterativeClusterFit | kColourClusterFit | kColourRangeFit );
     42 	int extra = flags & kWeightColourByAlpha;
     43 	
     44 	// set defaults
     45 	if ( method != kDxt3
     46 	&&   method != kDxt5
     47 	&&   method != kBc4
     48 	&&   method != kBc5 )
     49 	{
     50 		method = kDxt1;
     51 	}
     52 	if( fit != kColourRangeFit && fit != kColourIterativeClusterFit )
     53 		fit = kColourClusterFit;
     54 		
     55 	// done
     56 	return method | fit | extra;
     57 }
     58 
     59 void CompressMasked( u8 const* rgba, int mask, void* block, int flags, float* metric )
     60 {
     61 	// fix any bad flags
     62 	flags = FixFlags( flags );
     63 
     64 	if ( ( flags & ( kBc4 | kBc5 ) ) != 0 )
     65 	{
     66 		u8 alpha[16*4];
     67 		for( int i = 0; i < 16; ++i )
     68 		{
     69 			alpha[i*4 + 3] = rgba[i*4 + 0]; // copy R to A
     70 		}
     71 
     72 		u8* rBlock = reinterpret_cast< u8* >( block );
     73 		CompressAlphaDxt5( alpha, mask, rBlock );
     74 
     75 		if ( ( flags & ( kBc5 ) ) != 0 )
     76 		{
     77 			for( int i = 0; i < 16; ++i )
     78 			{
     79 				alpha[i*4 + 3] = rgba[i*4 + 1]; // copy G to A
     80 			}
     81 
     82 			u8* gBlock = reinterpret_cast< u8* >( block ) + 8;
     83 			CompressAlphaDxt5( alpha, mask, gBlock );
     84 		}
     85 
     86 		return;
     87 	}
     88 
     89 	// get the block locations
     90 	void* colourBlock = block;
     91 	void* alphaBlock = block;
     92 	if( ( flags & ( kDxt3 | kDxt5 ) ) != 0 )
     93 		colourBlock = reinterpret_cast< u8* >( block ) + 8;
     94 
     95 	// create the minimal point set
     96 	ColourSet colours( rgba, mask, flags );
     97 	
     98 	// check the compression type and compress colour
     99 	if( colours.GetCount() == 1 )
    100 	{
    101 		// always do a single colour fit
    102 		SingleColourFit fit( &colours, flags );
    103 		fit.Compress( colourBlock );
    104 	}
    105 	else if( ( flags & kColourRangeFit ) != 0 || colours.GetCount() == 0 )
    106 	{
    107 		// do a range fit
    108 		RangeFit fit( &colours, flags, metric );
    109 		fit.Compress( colourBlock );
    110 	}
    111 	else
    112 	{
    113 		// default to a cluster fit (could be iterative or not)
    114 		ClusterFit fit( &colours, flags, metric );
    115 		fit.Compress( colourBlock );
    116 	}
    117 	
    118 	// compress alpha separately if necessary
    119 	if( ( flags & kDxt3 ) != 0 )
    120 		CompressAlphaDxt3( rgba, mask, alphaBlock );
    121 	else if( ( flags & kDxt5 ) != 0 )
    122 		CompressAlphaDxt5( rgba, mask, alphaBlock );
    123 }
    124 
    125 void Decompress( u8* rgba, void const* block, int flags )
    126 {
    127 	// fix any bad flags
    128 	flags = FixFlags( flags );
    129 
    130 	// get the block locations
    131 	void const* colourBlock = block;
    132 	void const* alphaBock = block;
    133 	if( ( flags & ( kDxt3 | kDxt5 ) ) != 0 )
    134 		colourBlock = reinterpret_cast< u8 const* >( block ) + 8;
    135 
    136 	// decompress colour
    137 	DecompressColour( rgba, colourBlock, ( flags & kDxt1 ) != 0 );
    138 
    139 	// decompress alpha separately if necessary
    140 	if( ( flags & kDxt3 ) != 0 )
    141 		DecompressAlphaDxt3( rgba, alphaBock );
    142 	else if( ( flags & kDxt5 ) != 0 )
    143 		DecompressAlphaDxt5( rgba, alphaBock );
    144 }
    145 
    146 int GetStorageRequirements( int width, int height, int flags )
    147 {
    148 	// fix any bad flags
    149 	flags = FixFlags( flags );
    150 	
    151 	// compute the storage requirements
    152 	int blockcount = ( ( width + 3 )/4 ) * ( ( height + 3 )/4 );
    153 	int blocksize = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
    154 	return blockcount*blocksize;
    155 }
    156 
    157 void CompressImage( u8 const* rgba, int width, int height, void* blocks, int flags, float* metric )
    158 {
    159 	// fix any bad flags
    160 	flags = FixFlags( flags );
    161 
    162 	// initialise the block output
    163 	u8* targetBlock = reinterpret_cast< u8* >( blocks );
    164 	int bytesPerBlock = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
    165 
    166 	// loop over blocks
    167 	for( int y = 0; y < height; y += 4 )
    168 	{
    169 		for( int x = 0; x < width; x += 4 )
    170 		{
    171 			// build the 4x4 block of pixels
    172 			u8 sourceRgba[16*4];
    173 			u8* targetPixel = sourceRgba;
    174 			int mask = 0;
    175 			for( int py = 0; py < 4; ++py )
    176 			{
    177 				for( int px = 0; px < 4; ++px )
    178 				{
    179 					// get the source pixel in the image
    180 					int sx = x + px;
    181 					int sy = y + py;
    182 					
    183 					// enable if we're in the image
    184 					if( sx < width && sy < height )
    185 					{
    186 						// copy the rgba value
    187 						u8 const* sourcePixel = rgba + 4*( width*sy + sx );
    188 						for( int i = 0; i < 4; ++i )
    189 							*targetPixel++ = *sourcePixel++;
    190 							
    191 						// enable this pixel
    192 						mask |= ( 1 << ( 4*py + px ) );
    193 					}
    194 					else
    195 					{
    196 						// skip this pixel as its outside the image
    197 						targetPixel += 4;
    198 					}
    199 				}
    200 			}
    201 			
    202 			// compress it into the output
    203 			CompressMasked( sourceRgba, mask, targetBlock, flags, metric );
    204 			
    205 			// advance
    206 			targetBlock += bytesPerBlock;
    207 		}
    208 	}
    209 }
    210 
    211 void DecompressImage( u8* rgba, int width, int height, void const* blocks, int flags )
    212 {
    213 	// fix any bad flags
    214 	flags = FixFlags( flags );
    215 
    216 	// initialise the block input
    217 	u8 const* sourceBlock = reinterpret_cast< u8 const* >( blocks );
    218 	int bytesPerBlock = ( ( flags & ( kDxt1 | kBc4 ) ) != 0 ) ? 8 : 16;
    219 
    220 	// loop over blocks
    221 	for( int y = 0; y < height; y += 4 )
    222 	{
    223 		for( int x = 0; x < width; x += 4 )
    224 		{
    225 			// decompress the block
    226 			u8 targetRgba[4*16];
    227 			Decompress( targetRgba, sourceBlock, flags );
    228 			
    229 			// write the decompressed pixels to the correct image locations
    230 			u8 const* sourcePixel = targetRgba;
    231 			for( int py = 0; py < 4; ++py )
    232 			{
    233 				for( int px = 0; px < 4; ++px )
    234 				{
    235 					// get the target location
    236 					int sx = x + px;
    237 					int sy = y + py;
    238 					if( sx < width && sy < height )
    239 					{
    240 						u8* targetPixel = rgba + 4*( width*sy + sx );
    241 						
    242 						// copy the rgba value
    243 						for( int i = 0; i < 4; ++i )
    244 							*targetPixel++ = *sourcePixel++;
    245 					}
    246 					else
    247 					{
    248 						// skip this pixel as its outside the image
    249 						sourcePixel += 4;
    250 					}
    251 				}
    252 			}
    253 			
    254 			// advance
    255 			sourceBlock += bytesPerBlock;
    256 		}
    257 	}
    258 }
    259 
    260 } // namespace squish