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 "colourset.h"
27
28 namespace squish {
29
ColourSet(u8 const * rgba,int mask,int flags)30 ColourSet::ColourSet( u8 const* rgba, int mask, int flags )
31 : m_count( 0 ),
32 m_transparent( false )
33 {
34 bool toLinear = ((flags & kToLinear) != 0);
35 // check the compression mode for dxt1
36 bool isDxt1 = ( ( flags & kDxt1 ) != 0 );
37 bool weightByAlpha = ( ( flags & kWeightColourByAlpha ) != 0 );
38
39 // create the minimal set
40 for( int i = 0; i < 16; ++i )
41 {
42 // check this pixel is enabled
43 int bit = 1 << i;
44 if( ( mask & bit ) == 0 )
45 {
46 m_remap[i] = -1;
47 continue;
48 }
49
50 // check for transparent pixels when using dxt1
51 if( isDxt1 && rgba[4*i + 3] < 128 )
52 {
53 m_remap[i] = -1;
54 m_transparent = true;
55 continue;
56 }
57
58 // loop over previous points for a match
59 for( int j = 0;; ++j )
60 {
61 // allocate a new point
62 if( j == i )
63 {
64 // normalise coordinates to [0,1]
65 float x = toLinear ?
66 SRGBToLinear(( float )rgba[4*i] / 255.0f) :
67 ( float )rgba[4*i] / 255.0f;
68 float y = toLinear ?
69 SRGBToLinear(( float )rgba[4*i + 1] / 255.0f) :
70 ( float )rgba[4*i + 1] / 255.0f;
71 float z = toLinear ?
72 SRGBToLinear(( float )rgba[4*i + 2] / 255.0f) :
73 ( float )rgba[4*i + 2] / 255.0f;
74
75 // ensure there is always non-zero weight even for zero alpha
76 float w = toLinear != 0 ?
77 SRGBToLinear(( float )rgba[4*i + 3] / 256.0f) :
78 ( float )rgba[4*i + 3] / 256.0f;
79
80 // add the point
81 m_points[m_count] = Vec3( x, y, z );
82 m_weights[m_count] = ( weightByAlpha ? w : 1.0f );
83 m_remap[i] = m_count;
84
85 // advance
86 ++m_count;
87 break;
88 }
89
90 // check for a match
91 int oldbit = 1 << j;
92 bool match = ( ( mask & oldbit ) != 0 )
93 && ( rgba[4*i] == rgba[4*j] )
94 && ( rgba[4*i + 1] == rgba[4*j + 1] )
95 && ( rgba[4*i + 2] == rgba[4*j + 2] )
96 && ( rgba[4*j + 3] >= 128 || !isDxt1 );
97 if( match )
98 {
99 // get the index of the match
100 int index = m_remap[j];
101
102 // ensure there is always non-zero weight even for zero alpha
103 float w = toLinear != 0 ?
104 SRGBToLinear(( float )( rgba[4*i + 3] + 1 ) / 256.0f) :
105 ( float )( rgba[4*i + 3] + 1 )/ 256.0f;
106
107 // map to this point and increase the weight
108 m_weights[index] += ( weightByAlpha ? w : 1.0f );
109 m_remap[i] = index;
110 break;
111 }
112 }
113 }
114
115 // square root the weights
116 for( int i = 0; i < m_count; ++i )
117 m_weights[i] = std::sqrt( m_weights[i] );
118 }
119
RemapIndices(u8 const * source,u8 * target) const120 void ColourSet::RemapIndices( u8 const* source, u8* target ) const
121 {
122 for( int i = 0; i < 16; ++i )
123 {
124 int j = m_remap[i];
125 if( j == -1 )
126 target[i] = 3;
127 else
128 target[i] = source[j];
129 }
130 }
131
132 } // namespace squish
133