1- /* bcdec.h - v0.96
1+ /* bcdec.h - v0.97
22 provides functions to decompress blocks of BC compressed images
33 written by Sergii "iOrange" Kudlai in 2022
44
3030 - Split BC6H decompression function into 'half' and
3131 'float' variants
3232
33+ Michael Schmidt (@RunDevelopment) - Found better "magic" coefficients for integer interpolation
34+ of reference colors in BC1 color block, that match with
35+ the floating point interpolation. This also made it faster
36+ than integer division by 3!
37+
3338 bugfixes:
3439 @linkmauve
3540
3944#ifndef BCDEC_HEADER_INCLUDED
4045#define BCDEC_HEADER_INCLUDED
4146
47+ #define BCDEC_VERSION_MAJOR 0
48+ #define BCDEC_VERSION_MINOR 97
49+
4250/* if BCDEC_STATIC causes problems, try defining BCDECDEF to 'inline' or 'static inline' */
4351#ifndef BCDECDEF
4452#ifdef BCDEC_STATIC
@@ -96,6 +104,7 @@ BCDECDEF void bcdec_bc6h_float(const void* compressedBlock, void* decompressedBl
96104BCDECDEF void bcdec_bc6h_half (const void * compressedBlock , void * decompressedBlock , int destinationPitch , int isSigned );
97105BCDECDEF void bcdec_bc7 (const void * compressedBlock , void * decompressedBlock , int destinationPitch );
98106
107+ #endif /* BCDEC_HEADER_INCLUDED */
99108
100109#ifdef BCDEC_IMPLEMENTATION
101110
@@ -110,35 +119,44 @@ static void bcdec__color_block(const void* compressedBlock, void* decompressedBl
110119 c0 = ((unsigned short * )compressedBlock )[0 ];
111120 c1 = ((unsigned short * )compressedBlock )[1 ];
112121
122+ /* Unpack 565 ref colors */
123+ r0 = (c0 >> 11 ) & 0x1F ;
124+ g0 = (c0 >> 5 ) & 0x3F ;
125+ b0 = c0 & 0x1F ;
126+
127+ r1 = (c1 >> 11 ) & 0x1F ;
128+ g1 = (c1 >> 5 ) & 0x3F ;
129+ b1 = c1 & 0x1F ;
130+
113131 /* Expand 565 ref colors to 888 */
114- r0 = ((( c0 >> 11 ) & 0x1F ) * 527 + 23 ) >> 6 ;
115- g0 = ((( c0 >> 5 ) & 0x3F ) * 259 + 33 ) >> 6 ;
116- b0 = (( c0 & 0x1F ) * 527 + 23 ) >> 6 ;
117- refColors [0 ] = 0xFF000000 | (b0 << 16 ) | (g0 << 8 ) | r0 ;
132+ r = (r0 * 527 + 23 ) >> 6 ;
133+ g = (g0 * 259 + 33 ) >> 6 ;
134+ b = ( b0 * 527 + 23 ) >> 6 ;
135+ refColors [0 ] = 0xFF000000 | (b << 16 ) | (g << 8 ) | r ;
118136
119- r1 = ((( c1 >> 11 ) & 0x1F ) * 527 + 23 ) >> 6 ;
120- g1 = ((( c1 >> 5 ) & 0x3F ) * 259 + 33 ) >> 6 ;
121- b1 = (( c1 & 0x1F ) * 527 + 23 ) >> 6 ;
122- refColors [1 ] = 0xFF000000 | (b1 << 16 ) | (g1 << 8 ) | r1 ;
137+ r = (r1 * 527 + 23 ) >> 6 ;
138+ g = (g1 * 259 + 33 ) >> 6 ;
139+ b = ( b1 * 527 + 23 ) >> 6 ;
140+ refColors [1 ] = 0xFF000000 | (b << 16 ) | (g << 8 ) | r ;
123141
124142 if (c0 > c1 || onlyOpaqueMode ) { /* Standard BC1 mode (also BC3 color block uses ONLY this mode) */
125143 /* color_2 = 2/3*color_0 + 1/3*color_1
126144 color_3 = 1/3*color_0 + 2/3*color_1 */
127- r = (2 * r0 + r1 + 1 ) / 3 ;
128- g = (2 * g0 + g1 + 1 ) / 3 ;
129- b = (2 * b0 + b1 + 1 ) / 3 ;
145+ r = (( 2 * r0 + r1 ) * 351 + 61 ) >> 7 ;
146+ g = (( 2 * g0 + g1 ) * 2763 + 1039 ) >> 11 ;
147+ b = (( 2 * b0 + b1 ) * 351 + 61 ) >> 7 ;
130148 refColors [2 ] = 0xFF000000 | (b << 16 ) | (g << 8 ) | r ;
131149
132- r = (r0 + 2 * r1 + 1 ) / 3 ;
133- g = (g0 + 2 * g1 + 1 ) / 3 ;
134- b = (b0 + 2 * b1 + 1 ) / 3 ;
150+ r = (( r0 + r1 * 2 ) * 351 + 61 ) >> 7 ;
151+ g = (( g0 + g1 * 2 ) * 2763 + 1039 ) >> 11 ;
152+ b = (( b0 + b1 * 2 ) * 351 + 61 ) >> 7 ;
135153 refColors [3 ] = 0xFF000000 | (b << 16 ) | (g << 8 ) | r ;
136154 } else { /* Quite rare BC1A mode */
137155 /* color_2 = 1/2*color_0 + 1/2*color_1;
138156 color_3 = 0; */
139- r = (r0 + r1 + 1 ) >> 1 ;
140- g = (g0 + g1 + 1 ) >> 1 ;
141- b = (b0 + b1 + 1 ) >> 1 ;
157+ r = (( r0 + r1 ) * 1053 + 125 ) >> 8 ;
158+ g = (( g0 + g1 ) * 4145 + 1019 ) >> 11 ;
159+ b = (( b0 + b1 ) * 1053 + 125 ) >> 8 ;
142160 refColors [2 ] = 0xFF000000 | (b << 16 ) | (g << 8 ) | r ;
143161
144162 refColors [3 ] = 0x00000000 ;
@@ -1269,8 +1287,6 @@ BCDECDEF void bcdec_bc7(const void* compressedBlock, void* decompressedBlock, in
12691287
12701288#endif /* BCDEC_IMPLEMENTATION */
12711289
1272- #endif /* BCDEC_HEADER_INCLUDED */
1273-
12741290/* LICENSE:
12751291
12761292This software is available under 2 licenses -- choose whichever you prefer.
@@ -1326,4 +1342,4 @@ OTHER DEALINGS IN THE SOFTWARE.
13261342
13271343For more information, please refer to <https://unlicense.org>
13281344
1329- */
1345+ */
0 commit comments