medfall

A super great game engine
Log | Files | Refs

colourset.cc (3491B)


      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 "colourset.h"
     27 
     28 namespace squish {
     29 
     30 ColourSet::ColourSet( u8 const* rgba, int mask, int flags )
     31   : m_count( 0 ), 
     32 	m_transparent( false )
     33 {
     34 	// check the compression mode for dxt1
     35 	bool isDxt1 = ( ( flags & kDxt1 ) != 0 );
     36 	bool weightByAlpha = ( ( flags & kWeightColourByAlpha ) != 0 );
     37 
     38 	// create the minimal set
     39 	for( int i = 0; i < 16; ++i )
     40 	{
     41 		// check this pixel is enabled
     42 		int bit = 1 << i;
     43 		if( ( mask & bit ) == 0 )
     44 		{
     45 			m_remap[i] = -1;
     46 			continue;
     47 		}
     48 	
     49 		// check for transparent pixels when using dxt1
     50 		if( isDxt1 && rgba[4*i + 3] < 128 )
     51 		{
     52 			m_remap[i] = -1;
     53 			m_transparent = true;
     54 			continue;
     55 		}
     56 
     57 		// loop over previous points for a match
     58 		for( int j = 0;; ++j )
     59 		{
     60 			// allocate a new point
     61 			if( j == i )
     62 			{
     63 				// normalise coordinates to [0,1]
     64 				float x = ( float )rgba[4*i] / 255.0f;
     65 				float y = ( float )rgba[4*i + 1] / 255.0f;
     66 				float z = ( float )rgba[4*i + 2] / 255.0f;
     67 				
     68 				// ensure there is always non-zero weight even for zero alpha
     69 				float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
     70 
     71 				// add the point
     72 				m_points[m_count] = Vec3( x, y, z );
     73 				m_weights[m_count] = ( weightByAlpha ? w : 1.0f );
     74 				m_remap[i] = m_count;
     75 				
     76 				// advance
     77 				++m_count;
     78 				break;
     79 			}
     80 		
     81 			// check for a match
     82 			int oldbit = 1 << j;
     83 			bool match = ( ( mask & oldbit ) != 0 )
     84 				&& ( rgba[4*i] == rgba[4*j] )
     85 				&& ( rgba[4*i + 1] == rgba[4*j + 1] )
     86 				&& ( rgba[4*i + 2] == rgba[4*j + 2] )
     87 				&& ( rgba[4*j + 3] >= 128 || !isDxt1 );
     88 			if( match )
     89 			{
     90 				// get the index of the match
     91 				int index = m_remap[j];
     92 				
     93 				// ensure there is always non-zero weight even for zero alpha
     94 				float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
     95 
     96 				// map to this point and increase the weight
     97 				m_weights[index] += ( weightByAlpha ? w : 1.0f );
     98 				m_remap[i] = index;
     99 				break;
    100 			}
    101 		}
    102 	}
    103 
    104 	// square root the weights
    105 	for( int i = 0; i < m_count; ++i )
    106 		m_weights[i] = std::sqrt( m_weights[i] );
    107 }
    108 
    109 void ColourSet::RemapIndices( u8 const* source, u8* target ) const
    110 {
    111 	for( int i = 0; i < 16; ++i )
    112 	{
    113 		int j = m_remap[i];
    114 		if( j == -1 )
    115 			target[i] = 3;
    116 		else
    117 			target[i] = source[j];
    118 	}
    119 }
    120 
    121 } // namespace squish