1 /* -----------------------------------------------------------------------------
2 
3 	Copyright (c) 2006 Simon Brown                          si@sjbrown.co.uk
4 
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:
12 
13 	The above copyright notice and this permission notice shall be included
14 	in all copies or substantial portions of the Software.
15 
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.
23 
24    -------------------------------------------------------------------------- */
25 
26 #include "rangefit.h"
27 #include "colourset.h"
28 #include "colourblock.h"
29 #include <cfloat>
30 
31 namespace squish {
32 
RangeFit(ColourSet const * colours,int flags)33 RangeFit::RangeFit( ColourSet const* colours, int flags )
34   : ColourFit( colours, flags )
35 {
36 	// initialise the metric
37 	bool perceptual = ( ( m_flags & kColourMetricPerceptual ) != 0 );
38 	if( perceptual )
39 		m_metric = Vec3( 0.2126f, 0.7152f, 0.0722f );
40 	else
41 		m_metric = Vec3( 1.0f );
42 
43 	// initialise the best error
44 	m_besterror = FLT_MAX;
45 
46 	// cache some values
47 	int const count = m_colours->GetCount();
48 	Vec3 const* values = m_colours->GetPoints();
49 	float const* weights = m_colours->GetWeights();
50 
51 	// get the covariance matrix
52 	Sym3x3 covariance = ComputeWeightedCovariance( count, values, weights );
53 
54 	// compute the principle component
55 	Vec3 principle = ComputePrincipleComponent( covariance );
56 
57 	// get the min and max range as the codebook endpoints
58 	Vec3 start( 0.0f );
59 	Vec3 end( 0.0f );
60 	if( count > 0 )
61 	{
62 		float min, max;
63 
64 		// compute the range
65 		start = end = values[0];
66 		min = max = Dot( values[0], principle );
67 		for( int i = 1; i < count; ++i )
68 		{
69 			float val = Dot( values[i], principle );
70 			if( val < min )
71 			{
72 				start = values[i];
73 				min = val;
74 			}
75 			else if( val > max )
76 			{
77 				end = values[i];
78 				max = val;
79 			}
80 		}
81 	}
82 
83 	// clamp the output to [0, 1]
84 	Vec3 const one( 1.0f );
85 	Vec3 const zero( 0.0f );
86 	start = Min( one, Max( zero, start ) );
87 	end = Min( one, Max( zero, end ) );
88 
89 	// clamp to the grid and save
90 	Vec3 const grid( 31.0f, 63.0f, 31.0f );
91 	Vec3 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f );
92 	Vec3 const half( 0.5f );
93 	m_start = Truncate( grid*start + half )*gridrcp;
94 	m_end = Truncate( grid*end + half )*gridrcp;
95 }
96 
Compress3(void * block)97 void RangeFit::Compress3( void* block )
98 {
99 	// cache some values
100 	int const count = m_colours->GetCount();
101 	Vec3 const* values = m_colours->GetPoints();
102 
103 	// create a codebook
104 	Vec3 codes[3];
105 	codes[0] = m_start;
106 	codes[1] = m_end;
107 	codes[2] = 0.5f*m_start + 0.5f*m_end;
108 
109 	// match each point to the closest code
110 	u8 closest[16];
111 	float error = 0.0f;
112 	for( int i = 0; i < count; ++i )
113 	{
114 		// find the closest code
115 		float dist = FLT_MAX;
116 		int idx = 0;
117 		for( int j = 0; j < 3; ++j )
118 		{
119 			float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
120 			if( d < dist )
121 			{
122 				dist = d;
123 				idx = j;
124 			}
125 		}
126 
127 		// save the index
128 		closest[i] = ( u8 )idx;
129 
130 		// accumulate the error
131 		error += dist;
132 	}
133 
134 	// save this scheme if it wins
135 	if( error < m_besterror )
136 	{
137 		// remap the indices
138 		u8 indices[16];
139 		m_colours->RemapIndices( closest, indices );
140 
141 		// save the block
142 		WriteColourBlock3( m_start, m_end, indices, block );
143 
144 		// save the error
145 		m_besterror = error;
146 	}
147 }
148 
Compress4(void * block)149 void RangeFit::Compress4( void* block )
150 {
151 	// cache some values
152 	int const count = m_colours->GetCount();
153 	Vec3 const* values = m_colours->GetPoints();
154 
155 	// create a codebook
156 	Vec3 codes[4];
157 	codes[0] = m_start;
158 	codes[1] = m_end;
159 	codes[2] = ( 2.0f/3.0f )*m_start + ( 1.0f/3.0f )*m_end;
160 	codes[3] = ( 1.0f/3.0f )*m_start + ( 2.0f/3.0f )*m_end;
161 
162 	// match each point to the closest code
163 	u8 closest[16];
164 	float error = 0.0f;
165 	for( int i = 0; i < count; ++i )
166 	{
167 		// find the closest code
168 		float dist = FLT_MAX;
169 		int idx = 0;
170 		for( int j = 0; j < 4; ++j )
171 		{
172 			float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
173 			if( d < dist )
174 			{
175 				dist = d;
176 				idx = j;
177 			}
178 		}
179 
180 		// save the index
181 		closest[i] = ( u8 )idx;
182 
183 		// accumulate the error
184 		error += dist;
185 	}
186 
187 	// save this scheme if it wins
188 	if( error < m_besterror )
189 	{
190 		// remap the indices
191 		u8 indices[16];
192 		m_colours->RemapIndices( closest, indices );
193 
194 		// save the block
195 		WriteColourBlock4( m_start, m_end, indices, block );
196 
197 		// save the error
198 		m_besterror = error;
199 	}
200 }
201 
202 } // namespace squish
203