fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Image / Squish / OSGSquishRangefit.cpp
blobf7949743fccec58bfa27f283e71236ebd78e9fb8
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 -------------------------------------------------------------------------- */
26 #include "rangefit.h"
27 #include "colourset.h"
28 #include "colourblock.h"
29 #include <cfloat>
31 namespace osgsquish {
33 RangeFit::RangeFit( ColourSet const* colours, int flags )
34 : ColourFit( colours, flags ),
35 m_metric(),
36 m_start(),
37 m_end(),
38 m_besterror(0.f)
40 // initialise the metric
41 bool perceptual = ( ( m_flags & kColourMetricPerceptual ) != 0 );
42 if( perceptual )
43 m_metric = Vec3( 0.2126f, 0.7152f, 0.0722f );
44 else
45 m_metric = Vec3( 1.0f );
47 // initialise the best error
48 m_besterror = FLT_MAX;
50 // cache some values
51 int const count = m_colours->GetCount();
52 Vec3 const* values = m_colours->GetPoints();
53 float const* weights = m_colours->GetWeights();
55 // get the covariance matrix
56 Sym3x3 covariance = ComputeWeightedCovariance( count, values, weights );
58 // compute the principle component
59 Vec3 principle = ComputePrincipleComponent( covariance );
61 // get the min and max range as the codebook endpoints
62 Vec3 start( 0.0f );
63 Vec3 end( 0.0f );
64 if( count > 0 )
66 float min, max;
68 // compute the range
69 start = end = values[0];
70 min = max = Dot( values[0], principle );
71 for( int i = 1; i < count; ++i )
73 float val = Dot( values[i], principle );
74 if( val < min )
76 start = values[i];
77 min = val;
79 else if( val > max )
81 end = values[i];
82 max = val;
87 // clamp the output to [0, 1]
88 Vec3 const one( 1.0f );
89 Vec3 const zero( 0.0f );
90 start = Min( one, Max( zero, start ) );
91 end = Min( one, Max( zero, end ) );
93 // clamp to the grid and save
94 Vec3 const grid( 31.0f, 63.0f, 31.0f );
95 Vec3 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f );
96 Vec3 const half( 0.5f );
97 m_start = Truncate( grid*start + half )*gridrcp;
98 m_end = Truncate( grid*end + half )*gridrcp;
101 void RangeFit::Compress3( void* block )
103 // cache some values
104 int const count = m_colours->GetCount();
105 Vec3 const* values = m_colours->GetPoints();
107 // create a codebook
108 Vec3 codes[3];
109 codes[0] = m_start;
110 codes[1] = m_end;
111 codes[2] = 0.5f*m_start + 0.5f*m_end;
113 // match each point to the closest code
114 u8 closest[16];
115 float error = 0.0f;
116 for( int i = 0; i < count; ++i )
118 // find the closest code
119 float dist = FLT_MAX;
120 int idx = 0;
121 for( int j = 0; j < 3; ++j )
123 float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
124 if( d < dist )
126 dist = d;
127 idx = j;
131 // save the index
132 closest[i] = u8(idx);
134 // accumulate the error
135 error += dist;
138 // save this scheme if it wins
139 if( error < m_besterror )
141 // remap the indices
142 u8 indices[16];
143 m_colours->RemapIndices( closest, indices );
145 // save the block
146 WriteColourBlock3( m_start, m_end, indices, block );
148 // save the error
149 m_besterror = error;
153 void RangeFit::Compress4( void* block )
155 // cache some values
156 int const count = m_colours->GetCount();
157 Vec3 const* values = m_colours->GetPoints();
159 // create a codebook
160 Vec3 codes[4];
161 codes[0] = m_start;
162 codes[1] = m_end;
163 codes[2] = ( 2.0f/3.0f )*m_start + ( 1.0f/3.0f )*m_end;
164 codes[3] = ( 1.0f/3.0f )*m_start + ( 2.0f/3.0f )*m_end;
166 // match each point to the closest code
167 u8 closest[16];
168 float error = 0.0f;
169 for( int i = 0; i < count; ++i )
171 // find the closest code
172 float dist = FLT_MAX;
173 int idx = 0;
174 for( int j = 0; j < 4; ++j )
176 float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
177 if( d < dist )
179 dist = d;
180 idx = j;
184 // save the index
185 closest[i] = u8(idx);
187 // accumulate the error
188 error += dist;
191 // save this scheme if it wins
192 if( error < m_besterror )
194 // remap the indices
195 u8 indices[16];
196 m_colours->RemapIndices( closest, indices );
198 // save the block
199 WriteColourBlock4( m_start, m_end, indices, block );
201 // save the error
202 m_besterror = error;
206 } // namespace squish