123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include "singlecolourfit.h"
- #include "colourset.h"
- #include "colourblock.h"
- namespace squish {
- struct SourceBlock
- {
- u8 start;
- u8 end;
- u8 error;
- };
- struct SingleColourLookup
- {
- SourceBlock sources[2];
- };
- #include "singlecolourlookup.inl"
- static int FloatToInt( float a, int limit )
- {
-
- int i = ( int )( a + 0.5f );
-
- if( i < 0 )
- i = 0;
- else if( i > limit )
- i = limit;
-
- return i;
- }
- SingleColourFit::SingleColourFit( ColourSet const* colours, int flags )
- : ColourFit( colours, flags )
- {
-
- Vec3 const* values = m_colours->GetPoints();
- m_colour[0] = ( u8 )FloatToInt( 255.0f*values->X(), 255 );
- m_colour[1] = ( u8 )FloatToInt( 255.0f*values->Y(), 255 );
- m_colour[2] = ( u8 )FloatToInt( 255.0f*values->Z(), 255 );
-
- m_besterror = INT_MAX;
- }
- void SingleColourFit::Compress3( void* block )
- {
-
- SingleColourLookup const* const lookups[] =
- {
- lookup_5_3,
- lookup_6_3,
- lookup_5_3
- };
-
- ComputeEndPoints( lookups );
-
- if( m_error < m_besterror )
- {
-
- u8 indices[16];
- m_colours->RemapIndices( &m_index, indices );
-
- WriteColourBlock3( m_start, m_end, indices, block );
-
- m_besterror = m_error;
- }
- }
- void SingleColourFit::Compress4( void* block )
- {
-
- SingleColourLookup const* const lookups[] =
- {
- lookup_5_4,
- lookup_6_4,
- lookup_5_4
- };
-
- ComputeEndPoints( lookups );
-
- if( m_error < m_besterror )
- {
-
- u8 indices[16];
- m_colours->RemapIndices( &m_index, indices );
-
- WriteColourBlock4( m_start, m_end, indices, block );
-
- m_besterror = m_error;
- }
- }
- void SingleColourFit::ComputeEndPoints( SingleColourLookup const* const* lookups )
- {
-
- m_error = INT_MAX;
- for( int index = 0; index < 2; ++index )
- {
-
- SourceBlock const* sources[3];
- int error = 0;
- for( int channel = 0; channel < 3; ++channel )
- {
-
- SingleColourLookup const* lookup = lookups[channel];
- int target = m_colour[channel];
-
- sources[channel] = lookup[target].sources + index;
-
- int diff = sources[channel]->error;
- error += diff*diff;
- }
-
- if( error < m_error )
- {
- m_start = Vec3(
- ( float )sources[0]->start/31.0f,
- ( float )sources[1]->start/63.0f,
- ( float )sources[2]->start/31.0f
- );
- m_end = Vec3(
- ( float )sources[0]->end/31.0f,
- ( float )sources[1]->end/63.0f,
- ( float )sources[2]->end/31.0f
- );
- m_index = ( u8 )( 2*index );
- m_error = error;
- }
- }
- }
- }
|