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
)
34 : ColourFit( colours
, flags
),
40 // initialise the metric
41 bool perceptual
= ( ( m_flags
& kColourMetricPerceptual
) != 0 );
43 m_metric
= Vec3( 0.2126f
, 0.7152f
, 0.0722f
);
45 m_metric
= Vec3( 1.0f
);
47 // initialise the best error
48 m_besterror
= FLT_MAX
;
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
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
);
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
)
104 int const count
= m_colours
->GetCount();
105 Vec3
const* values
= m_colours
->GetPoints();
111 codes
[2] = 0.5f
*m_start
+ 0.5f
*m_end
;
113 // match each point to the closest code
116 for( int i
= 0; i
< count
; ++i
)
118 // find the closest code
119 float dist
= FLT_MAX
;
121 for( int j
= 0; j
< 3; ++j
)
123 float d
= LengthSquared( m_metric
*( values
[i
] - codes
[j
] ) );
132 closest
[i
] = u8(idx
);
134 // accumulate the error
138 // save this scheme if it wins
139 if( error
< m_besterror
)
143 m_colours
->RemapIndices( closest
, indices
);
146 WriteColourBlock3( m_start
, m_end
, indices
, block
);
153 void RangeFit::Compress4( void* block
)
156 int const count
= m_colours
->GetCount();
157 Vec3
const* values
= m_colours
->GetPoints();
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
169 for( int i
= 0; i
< count
; ++i
)
171 // find the closest code
172 float dist
= FLT_MAX
;
174 for( int j
= 0; j
< 4; ++j
)
176 float d
= LengthSquared( m_metric
*( values
[i
] - codes
[j
] ) );
185 closest
[i
] = u8(idx
);
187 // accumulate the error
191 // save this scheme if it wins
192 if( error
< m_besterror
)
196 m_colours
->RemapIndices( closest
, indices
);
199 WriteColourBlock4( m_start
, m_end
, indices
, block
);
206 } // namespace squish