| 295 | } |
| 296 | |
| 297 | void DecompressAlphaDxt5( u8* rgba, void const* block ) |
| 298 | { |
| 299 | // get the two alpha values |
| 300 | u8 const* bytes = reinterpret_cast< u8 const* >( block ); |
| 301 | int alpha0 = bytes[0]; |
| 302 | int alpha1 = bytes[1]; |
| 303 | |
| 304 | // compare the values to build the codebook |
| 305 | u8 codes[8]; |
| 306 | codes[0] = ( u8 )alpha0; |
| 307 | codes[1] = ( u8 )alpha1; |
| 308 | if( alpha0 <= alpha1 ) |
| 309 | { |
| 310 | // use 5-alpha codebook |
| 311 | for( int i = 1; i < 5; ++i ) |
| 312 | codes[1 + i] = ( u8 )( ( ( 5 - i )*alpha0 + i*alpha1 )/5 ); |
| 313 | codes[6] = 0; |
| 314 | codes[7] = 255; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | // use 7-alpha codebook |
| 319 | for( int i = 1; i < 7; ++i ) |
| 320 | codes[1 + i] = ( u8 )( ( ( 7 - i )*alpha0 + i*alpha1 )/7 ); |
| 321 | } |
| 322 | |
| 323 | // decode the indices |
| 324 | u8 indices[16]; |
| 325 | u8 const* src = bytes + 2; |
| 326 | u8* dest = indices; |
| 327 | for( int i = 0; i < 2; ++i ) |
| 328 | { |
| 329 | // grab 3 bytes |
| 330 | int value = 0; |
| 331 | for( int j = 0; j < 3; ++j ) |
| 332 | { |
| 333 | int byte = *src++; |
| 334 | value |= ( byte << 8*j ); |
| 335 | } |
| 336 | |
| 337 | // unpack 8 3-bit values from it |
| 338 | for( int j = 0; j < 8; ++j ) |
| 339 | { |
| 340 | int index = ( value >> 3*j ) & 0x7; |
| 341 | *dest++ = ( u8 )index; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // write out the indexed codebook values |
| 346 | for( int i = 0; i < 16; ++i ) |
| 347 | rgba[4*i + 3] = codes[indices[i]]; |
| 348 | } |
| 349 | |
| 350 | } // namespace squish |