123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include "rangefit.h"
- #include "colourset.h"
- #include "colourblock.h"
- #include <cfloat>
- namespace squish {
- RangeFit::RangeFit( ColourSet const* colours, int flags, float* metric )
- : ColourFit( colours, flags )
- {
-
- if( metric )
- m_metric = Vec3( metric[0], metric[1], metric[2] );
- else
- m_metric = Vec3( 1.0f );
-
- m_besterror = FLT_MAX;
-
- int const count = m_colours->GetCount();
- Vec3 const* values = m_colours->GetPoints();
- float const* weights = m_colours->GetWeights();
-
- Sym3x3 covariance = ComputeWeightedCovariance( count, values, weights );
-
- Vec3 principle = ComputePrincipleComponent( covariance );
-
- Vec3 start( 0.0f );
- Vec3 end( 0.0f );
- if( count > 0 )
- {
- float min, max;
-
- start = end = values[0];
- min = max = Dot( values[0], principle );
- for( int i = 1; i < count; ++i )
- {
- float val = Dot( values[i], principle );
- if( val < min )
- {
- start = values[i];
- min = val;
- }
- else if( val > max )
- {
- end = values[i];
- max = val;
- }
- }
- }
-
- Vec3 const one( 1.0f );
- Vec3 const zero( 0.0f );
- start = Min( one, Max( zero, start ) );
- end = Min( one, Max( zero, end ) );
-
- Vec3 const grid( 31.0f, 63.0f, 31.0f );
- Vec3 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f );
- Vec3 const half( 0.5f );
- m_start = Truncate( grid*start + half )*gridrcp;
- m_end = Truncate( grid*end + half )*gridrcp;
- }
- void RangeFit::Compress3( void* block )
- {
-
- int const count = m_colours->GetCount();
- Vec3 const* values = m_colours->GetPoints();
-
- Vec3 codes[3];
- codes[0] = m_start;
- codes[1] = m_end;
- codes[2] = 0.5f*m_start + 0.5f*m_end;
-
- u8 closest[16];
- float error = 0.0f;
- for( int i = 0; i < count; ++i )
- {
-
- float dist = FLT_MAX;
- int idx = 0;
- for( int j = 0; j < 3; ++j )
- {
- float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
- if( d < dist )
- {
- dist = d;
- idx = j;
- }
- }
-
- closest[i] = ( u8 )idx;
-
- error += dist;
- }
-
- if( error < m_besterror )
- {
-
- u8 indices[16];
- m_colours->RemapIndices( closest, indices );
-
- WriteColourBlock3( m_start, m_end, indices, block );
-
- m_besterror = error;
- }
- }
- void RangeFit::Compress4( void* block )
- {
-
- int const count = m_colours->GetCount();
- Vec3 const* values = m_colours->GetPoints();
-
- Vec3 codes[4];
- codes[0] = m_start;
- codes[1] = m_end;
- codes[2] = ( 2.0f/3.0f )*m_start + ( 1.0f/3.0f )*m_end;
- codes[3] = ( 1.0f/3.0f )*m_start + ( 2.0f/3.0f )*m_end;
-
- u8 closest[16];
- float error = 0.0f;
- for( int i = 0; i < count; ++i )
- {
-
- float dist = FLT_MAX;
- int idx = 0;
- for( int j = 0; j < 4; ++j )
- {
- float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
- if( d < dist )
- {
- dist = d;
- idx = j;
- }
- }
-
- closest[i] = ( u8 )idx;
-
- error += dist;
- }
-
- if( error < m_besterror )
- {
-
- u8 indices[16];
- m_colours->RemapIndices( closest, indices );
-
- WriteColourBlock4( m_start, m_end, indices, block );
-
- m_besterror = error;
- }
- }
- }
|