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
30 // @@ Add flags:
31 // - MatchTransparent
32 // - WeightColorByAlpha
33
34
ColourSet(u8 const * rgba,int flags,bool createMinimalSet)35 ColourSet::ColourSet( u8 const* rgba, int flags, bool createMinimalSet/*=false*/ )
36 : m_count( 0 ),
37 m_transparent( false )
38 {
39 // check the compression mode for dxt1
40 bool isDxt1 = ( ( flags & kDxt1 ) != 0 );
41 bool weightByAlpha = ( ( flags & kWeightColourByAlpha ) != 0 );
42
43 // create the minimal set
44 for( int i = 0; i < 16; ++i )
45 {
46 if (createMinimalSet)
47 {
48 // check for transparent pixels when using dxt1
49 if( isDxt1 && rgba[4*i + 3] == 0 )
50 {
51 m_remap[i] = -1;
52 m_transparent = true;
53 continue;
54 }
55
56 // loop over previous points for a match
57 for( int j = 0;; ++j )
58 {
59 // allocate a new point
60 if( j == i )
61 {
62 // normalise coordinates to [0,1]
63 float x = ( float )rgba[4*i + 2] / 255.0f;
64 float y = ( float )rgba[4*i + 1] / 255.0f;
65 float z = ( float )rgba[4*i + 0] / 255.0f;
66
67 // ensure there is always non-zero weight even for zero alpha
68 float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
69
70 // add the point
71 m_points[m_count] = Vec3( x, y, z );
72 m_weights[m_count] = ( weightByAlpha ? w : 1.0f );
73 m_remap[i] = m_count;
74
75 // advance
76 ++m_count;
77 break;
78 }
79
80 // check for a match
81 bool match = ( rgba[4*i] == rgba[4*j] )
82 && ( rgba[4*i + 1] == rgba[4*j + 1] )
83 && ( rgba[4*i + 2] == rgba[4*j + 2] )
84 && ( rgba[4*j + 3] != 0 || !isDxt1 ); // @@ I think this check is not necessary.
85
86 if( match )
87 {
88 // get the index of the match
89 int index = m_remap[j];
90
91 // ensure there is always non-zero weight even for zero alpha
92 float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
93
94 // map to this point and increase the weight
95 m_weights[index] += ( weightByAlpha ? w : 1.0f );
96 m_remap[i] = index;
97 break;
98 }
99 }
100 }
101 else
102 {
103 // check for transparent pixels when using dxt1
104 if( isDxt1 && rgba[4*i + 3] == 0 )
105 {
106 m_remap[i] = -1;
107 m_transparent = true;
108 }
109 else
110 {
111 m_remap[i] = m_count;
112 }
113
114 // normalise coordinates to [0,1]
115 float x = ( float )rgba[4*i + 2] / 255.0f;
116 float y = ( float )rgba[4*i + 1] / 255.0f;
117 float z = ( float )rgba[4*i + 0] / 255.0f;
118
119 // ensure there is always non-zero weight even for zero alpha
120 float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
121
122 // add the point
123 m_points[m_count] = Vec3( x, y, z );
124 m_weights[m_count] = ( weightByAlpha ? w : 1.0f );
125
126 // advance
127 ++m_count;
128 }
129 }
130
131 #if SQUISH_USE_SIMD
132 // generate vector values
133 for( int i = 0; i < m_count; ++i )
134 {
135 m_points_simd[i] = Vec4(m_points[i].X(), m_points[i].Y(), m_points[i].Z(), 1);
136 m_weights_simd[i] = VEC4_CONST(m_weights[i]);
137 }
138 #endif
139 }
140
RemapIndices(u8 const * source,u8 * target) const141 void ColourSet::RemapIndices( u8 const* source, u8* target ) const
142 {
143 for( int i = 0; i < 16; ++i )
144 {
145 int j = m_remap[i];
146 if( j == -1 )
147 target[i] = 3;
148 else
149 target[i] = source[j];
150 }
151 }
152
153 } // namespace squish
154