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 "colourblock.h"
27// -- Godot start --
28#include "alpha.h"
29// -- Godot end --
30
31namespace squish {
32
33static int FloatToInt( float a, int limit )
34{
35 // use ANSI round-to-zero behaviour to get round-to-nearest
36 int i = ( int )( a + 0.5f );
37
38 // clamp to the limit
39 if( i < 0 )
40 i = 0;
41 else if( i > limit )
42 i = limit;
43
44 // done
45 return i;
46}
47
48static int FloatTo565( Vec3::Arg colour )
49{
50 // get the components in the correct range
51 int r = FloatToInt( 31.0f*colour.X(), 31 );
52 int g = FloatToInt( 63.0f*colour.Y(), 63 );
53 int b = FloatToInt( 31.0f*colour.Z(), 31 );
54
55 // pack into a single value
56 return ( r << 11 ) | ( g << 5 ) | b;
57}
58
59static void WriteColourBlock( int a, int b, u8* indices, void* block )
60{
61 // get the block as bytes
62 u8* bytes = ( u8* )block;
63
64 // write the endpoints
65 bytes[0] = ( u8 )( a & 0xff );
66 bytes[1] = ( u8 )( a >> 8 );
67 bytes[2] = ( u8 )( b & 0xff );
68 bytes[3] = ( u8 )( b >> 8 );
69
70 // write the indices
71 for( int i = 0; i < 4; ++i )
72 {
73 u8 const* ind = indices + 4*i;
74 bytes[4 + i] = ind[0] | ( ind[1] << 2 ) | ( ind[2] << 4 ) | ( ind[3] << 6 );
75 }
76}
77
78void WriteColourBlock3( Vec3::Arg start, Vec3::Arg end, u8 const* indices, void* block )
79{
80 // get the packed values
81 int a = FloatTo565( start );
82 int b = FloatTo565( end );
83
84 // remap the indices
85 u8 remapped[16];
86 if( a <= b )
87 {
88 // use the indices directly
89 for( int i = 0; i < 16; ++i )
90 remapped[i] = indices[i];
91 }
92 else
93 {
94 // swap a and b
95 std::swap( a, b );
96 for( int i = 0; i < 16; ++i )
97 {
98 if( indices[i] == 0 )
99 remapped[i] = 1;
100 else if( indices[i] == 1 )
101 remapped[i] = 0;
102 else
103 remapped[i] = indices[i];
104 }
105 }
106
107 // write the block
108 WriteColourBlock( a, b, remapped, block );
109}
110
111void WriteColourBlock4( Vec3::Arg start, Vec3::Arg end, u8 const* indices, void* block )
112{
113 // get the packed values
114 int a = FloatTo565( start );
115 int b = FloatTo565( end );
116
117 // remap the indices
118 u8 remapped[16];
119 if( a < b )
120 {
121 // swap a and b
122 std::swap( a, b );
123 for( int i = 0; i < 16; ++i )
124 remapped[i] = ( indices[i] ^ 0x1 ) & 0x3;
125 }
126 else if( a == b )
127 {
128 // use index 0
129 for( int i = 0; i < 16; ++i )
130 remapped[i] = 0;
131 }
132 else
133 {
134 // use the indices directly
135 for( int i = 0; i < 16; ++i )
136 remapped[i] = indices[i];
137 }
138
139 // write the block
140 WriteColourBlock( a, b, remapped, block );
141}
142
143static int Unpack565( u8 const* packed, u8* colour )
144{
145 // build the packed value
146 int value = ( int )packed[0] | ( ( int )packed[1] << 8 );
147
148 // get the components in the stored range
149 u8 red = ( u8 )( ( value >> 11 ) & 0x1f );
150 u8 green = ( u8 )( ( value >> 5 ) & 0x3f );
151 u8 blue = ( u8 )( value & 0x1f );
152
153 // scale up to 8 bits
154 colour[0] = ( red << 3 ) | ( red >> 2 );
155 colour[1] = ( green << 2 ) | ( green >> 4 );
156 colour[2] = ( blue << 3 ) | ( blue >> 2 );
157 colour[3] = 255;
158
159 // return the value
160 return value;
161}
162
163void DecompressColour( u8* rgba, void const* block, bool isDxt1 )
164{
165 // get the block bytes
166 u8 const* bytes = reinterpret_cast< u8 const* >( block );
167
168 // unpack the endpoints
169 u8 codes[16];
170 int a = Unpack565( bytes, codes );
171 int b = Unpack565( bytes + 2, codes + 4 );
172
173 // generate the midpoints
174 for( int i = 0; i < 3; ++i )
175 {
176 int c = codes[i];
177 int d = codes[4 + i];
178
179 if( isDxt1 && a <= b )
180 {
181 codes[8 + i] = ( u8 )( ( c + d )/2 );
182 codes[12 + i] = 0;
183 }
184 else
185 {
186 codes[8 + i] = ( u8 )( ( 2*c + d )/3 );
187 codes[12 + i] = ( u8 )( ( c + 2*d )/3 );
188 }
189 }
190
191 // fill in alpha for the intermediate values
192 codes[8 + 3] = 255;
193 codes[12 + 3] = ( isDxt1 && a <= b ) ? 0 : 255;
194
195 // unpack the indices
196 u8 indices[16];
197 for( int i = 0; i < 4; ++i )
198 {
199 u8* ind = indices + 4*i;
200 u8 packed = bytes[4 + i];
201
202 ind[0] = packed & 0x3;
203 ind[1] = ( packed >> 2 ) & 0x3;
204 ind[2] = ( packed >> 4 ) & 0x3;
205 ind[3] = ( packed >> 6 ) & 0x3;
206 }
207
208 // store out the colours
209 for( int i = 0; i < 16; ++i )
210 {
211 u8 offset = 4*indices[i];
212 for( int j = 0; j < 4; ++j )
213 rgba[4*i + j] = codes[offset + j];
214 }
215}
216
217// -- Godot start --
218void DecompressColourBc5( u8* rgba, void const* block)
219{
220 void const* rblock = block;
221 void const* gblock = reinterpret_cast< u8 const* >( block ) + 8;
222 DecompressAlphaDxt5(rgba,rblock);
223 for ( int i = 0; i < 16; ++i ) {
224 rgba[i*4] = rgba[i*4 + 3];
225 }
226 DecompressAlphaDxt5(rgba,gblock);
227 for ( int i = 0; i < 16; ++i ) {
228 rgba[i*4+1] = rgba[i*4 + 3];
229 rgba[i*4 + 2] = 0;
230 rgba[i*4 + 3] = 255;
231 }
232}
233// -- GODOT end --
234
235
236} // namespace squish
237