1 /* -----------------------------------------------------------------------------
3 Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
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:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
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.
24 -------------------------------------------------------------------------- */
27 #include "colourset.h"
28 #include "colourblock.h"
33 RangeFit::RangeFit( ColourSet const* colours, int flags, float* metric )
34 : ColourFit( colours, flags )
36 // initialise the metric (old perceptual = 0.2126f, 0.7152f, 0.0722f)
38 m_metric = Vec3( metric[0], metric[1], metric[2] );
40 m_metric = Vec3( 1.0f );
42 // initialise the best error
43 m_besterror = FLT_MAX;
46 int const count = m_colours->GetCount();
47 Vec3 const* values = m_colours->GetPoints();
48 float const* weights = m_colours->GetWeights();
50 // get the covariance matrix
51 Sym3x3 covariance = ComputeWeightedCovariance( count, values, weights );
53 // compute the principle component
54 Vec3 principle = ComputePrincipleComponent( covariance );
56 // get the min and max range as the codebook endpoints
64 start = end = values[0];
65 min = max = Dot( values[0], principle );
66 for( int i = 1; i < count; ++i )
68 float val = Dot( values[i], principle );
82 // clamp the output to [0, 1]
83 Vec3 const one( 1.0f );
84 Vec3 const zero( 0.0f );
85 start = Min( one, Max( zero, start ) );
86 end = Min( one, Max( zero, end ) );
88 // clamp to the grid and save
89 Vec3 const grid( 31.0f, 63.0f, 31.0f );
90 Vec3 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f );
91 Vec3 const half( 0.5f );
92 m_start = Truncate( grid*start + half )*gridrcp;
93 m_end = Truncate( grid*end + half )*gridrcp;
96 void RangeFit::Compress3( void* block )
99 int const count = m_colours->GetCount();
100 Vec3 const* values = m_colours->GetPoints();
106 codes[2] = 0.5f*m_start + 0.5f*m_end;
108 // match each point to the closest code
111 for( int i = 0; i < count; ++i )
113 // find the closest code
114 float dist = FLT_MAX;
116 for( int j = 0; j < 3; ++j )
118 float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
127 closest[i] = ( u8 )idx;
129 // accumulate the error
133 // save this scheme if it wins
134 if( error < m_besterror )
138 m_colours->RemapIndices( closest, indices );
141 WriteColourBlock3( m_start, m_end, indices, block );
148 void RangeFit::Compress4( void* block )
151 int const count = m_colours->GetCount();
152 Vec3 const* values = m_colours->GetPoints();
158 codes[2] = ( 2.0f/3.0f )*m_start + ( 1.0f/3.0f )*m_end;
159 codes[3] = ( 1.0f/3.0f )*m_start + ( 2.0f/3.0f )*m_end;
161 // match each point to the closest code
164 for( int i = 0; i < count; ++i )
166 // find the closest code
167 float dist = FLT_MAX;
169 for( int j = 0; j < 4; ++j )
171 float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
180 closest[i] = ( u8 )idx;
182 // accumulate the error
186 // save this scheme if it wins
187 if( error < m_besterror )
191 m_colours->RemapIndices( closest, indices );
194 WriteColourBlock4( m_start, m_end, indices, block );
201 } // namespace squish