fixed: gcc8 compile issues
[opensg.git] / Source / System / Image / Squish / OSGSquishSinglecolourfit.cpp
blob150ab94bfe8b0f7b37690262f8bf5e8ce9c653f8
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 "singlecolourfit.h"
27 #include "colourset.h"
28 #include "colourblock.h"
29 #include <climits>
31 namespace osgsquish {
33 struct SourceBlock
35 u8 start;
36 u8 end;
37 u8 error;
40 struct SingleColourLookup
42 SourceBlock sources[2];
45 #include "singlecolourlookup.inl"
47 static int FloatToInt( float a, int limit )
49 // use ANSI round-to-zero behaviour to get round-to-nearest
50 int i = int( a + 0.5f );
52 // clamp to the limit
53 if( i < 0 )
54 i = 0;
55 else if( i > limit )
56 i = limit;
58 // done
59 return i;
62 SingleColourFit::SingleColourFit( ColourSet const* colours, int flags )
63 : ColourFit( colours, flags ),
64 m_start(),
65 m_end(),
66 m_index(0),
67 m_error(0),
68 m_besterror(0)
70 // grab the single colour
71 Vec3 const* values = m_colours->GetPoints();
72 m_colour[0] = u8(FloatToInt( 255.0f*values->X(), 255 ));
73 m_colour[1] = u8(FloatToInt( 255.0f*values->Y(), 255 ));
74 m_colour[2] = u8(FloatToInt( 255.0f*values->Z(), 255 ));
76 // initialise the best error
77 m_besterror = INT_MAX;
80 void SingleColourFit::Compress3( void* block )
82 // build the table of lookups
83 SingleColourLookup const* const lookups[] =
85 lookup_5_3,
86 lookup_6_3,
87 lookup_5_3
90 // find the best end-points and index
91 ComputeEndPoints( lookups );
93 // build the block if we win
94 if( m_error < m_besterror )
96 // remap the indices
97 u8 indices[16];
98 m_colours->RemapIndices( &m_index, indices );
100 // save the block
101 WriteColourBlock3( m_start, m_end, indices, block );
103 // save the error
104 m_besterror = m_error;
108 void SingleColourFit::Compress4( void* block )
110 // build the table of lookups
111 SingleColourLookup const* const lookups[] =
113 lookup_5_4,
114 lookup_6_4,
115 lookup_5_4
118 // find the best end-points and index
119 ComputeEndPoints( lookups );
121 // build the block if we win
122 if( m_error < m_besterror )
124 // remap the indices
125 u8 indices[16];
126 m_colours->RemapIndices( &m_index, indices );
128 // save the block
129 WriteColourBlock4( m_start, m_end, indices, block );
131 // save the error
132 m_besterror = m_error;
136 void SingleColourFit::ComputeEndPoints( SingleColourLookup const* const* lookups )
138 // check each index combination (endpoint or intermediate)
139 m_error = INT_MAX;
140 for( int index = 0; index < 2; ++index )
142 // check the error for this codebook index
143 SourceBlock const* sources[3];
144 int error = 0;
145 for( int channel = 0; channel < 3; ++channel )
147 // grab the lookup table and index for this channel
148 SingleColourLookup const* lookup = lookups[channel];
149 int target = m_colour[channel];
151 // store a pointer to the source for this channel
152 sources[channel] = lookup[target].sources + index;
154 // accumulate the error
155 int diff = sources[channel]->error;
156 error += diff*diff;
159 // keep it if the error is lower
160 if( error < m_error )
162 m_start = Vec3(
163 float(sources[0]->start)/31.0f,
164 float(sources[1]->start)/63.0f,
165 float(sources[2]->start)/31.0f
167 m_end = Vec3(
168 float(sources[0]->end)/31.0f,
169 float(sources[1]->end)/63.0f,
170 float(sources[2]->end)/31.0f
172 m_index = u8( 2*index );
173 m_error = error;
178 } // namespace squish