@@ -92,18 +92,17 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
9292
9393 code = * (const uint32_t * )(block + 4 );
9494
95- if (color0 > color1 ) {
96- for (j = 0 ; j < 4 ; ++ j ) {
97- for (i = 0 ; i < 4 ; ++ i ) {
98- uint32_t finalColor , positionCode ;
99- uint8_t alpha ;
95+ for (j = 0 ; j < 4 ; ++ j ) {
96+ for (i = 0 ; i < 4 ; ++ i ) {
97+ uint32_t finalColor , positionCode ;
98+ uint8_t alpha ;
10099
101- alpha = alphaValues [j * 4 + i ];
100+ finalColor = 0 ; positionCode = (code >> 2 * (4 * j + i )) & 0x03 ;
101+ alpha = alphaValues [j * 4 + i ];
102102
103- finalColor = 0 ;
104- positionCode = (code >> 2 * (4 * j + i )) & 0x03 ;
105-
106- switch (positionCode ) {
103+ if (color0 > color1 ) {
104+ switch (positionCode )
105+ {
107106 case 0 :
108107 finalColor = PackRGBA (r0 , g0 , b0 , alpha );
109108 break ;
@@ -117,24 +116,7 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
117116 finalColor = PackRGBA ((r0 + 2 * r1 )/3 , (g0 + 2 * g1 )/3 , (b0 + 2 * b1 )/3 , alpha );
118117 break ;
119118 }
120- if (!alpha )
121- * simpleAlpha = 1 ;
122- else if (alpha < 0xff )
123- * complexAlpha = 1 ;
124- output [j * outputStride + i ] = finalColor ;
125- }
126- }
127- } else {
128- for (j = 0 ; j < 4 ; ++ j ) {
129- for (i = 0 ; i < 4 ; ++ i ) {
130- uint32_t finalColor , positionCode ;
131- uint8_t alpha ;
132-
133- alpha = alphaValues [j * 4 + i ];
134-
135- finalColor = 0 ;
136- positionCode = (code >> 2 * (4 * j + i )) & 0x03 ;
137-
119+ } else {
138120 switch (positionCode ) {
139121 case 0 :
140122 finalColor = PackRGBA (r0 , g0 , b0 , alpha );
@@ -150,18 +132,95 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
150132 finalColor = PackRGBA (0 , 0 , 0 , alpha );
151133 break ;
152134 }
135+ }
136+
137+ if (!alpha )
138+ * simpleAlpha = 1 ;
139+ else if (alpha < 0xff )
140+ * complexAlpha = 1 ;
153141
154- if (!alpha )
155- * simpleAlpha = 1 ;
156- else if (alpha < 0xff )
157- * complexAlpha = 1 ;
142+ output [j * outputStride + i ] = finalColor ;
143+ }
144+ }
145+ }
146+
147+ static void DecompressBlockDXT1InternalRGB (const uint8_t * block , uint8_t * output , uint32_t outputStride )
148+ {
149+ uint32_t temp , code ;
150+
151+ uint16_t color0 , color1 ;
152+ uint8_t r0 , g0 , b0 , r1 , g1 , b1 ;
153+
154+ int i , j ;
155+
156+ color0 = * (const uint16_t * )(block );
157+ color1 = * (const uint16_t * )(block + 2 );
158+
159+ temp = (color0 >> 11 ) * 255 + 16 ;
160+ r0 = (uint8_t )((temp /32 + temp )/32 );
161+ temp = ((color0 & 0x07E0 ) >> 5 ) * 255 + 32 ;
162+ g0 = (uint8_t )((temp /64 + temp )/64 );
163+ temp = (color0 & 0x001F ) * 255 + 16 ;
164+ b0 = (uint8_t )((temp /32 + temp )/32 );
165+
166+ temp = (color1 >> 11 ) * 255 + 16 ;
167+ r1 = (uint8_t )((temp /32 + temp )/32 );
168+ temp = ((color1 & 0x07E0 ) >> 5 ) * 255 + 32 ;
169+ g1 = (uint8_t )((temp /64 + temp )/64 );
170+ temp = (color1 & 0x001F ) * 255 + 16 ;
171+ b1 = (uint8_t )((temp /32 + temp )/32 );
172+
173+ code = * (const uint32_t * )(block + 4 );
174+
175+ for (j = 0 ; j < 4 ; ++ j ) {
176+ for (i = 0 ; i < 4 ; ++ i ) {
177+ uint8_t positionCode , finalR , finalG , finalB ;
178+
179+ positionCode = (code >> 2 * (4 * j + i )) & 0x03 ;
180+
181+ if (color0 > color1 ) {
182+
183+ switch (positionCode ) {
184+ case 0 :
185+ finalR = r0 ; finalG = g0 ; finalB = b0 ;
186+ break ;
187+ case 1 :
188+ finalR = r1 ; finalG = g1 ; finalB = b1 ;
189+ break ;
190+ case 2 :
191+
192+ finalR = (2 * r0 + r1 )/3 ; finalG = (2 * g0 + g1 )/3 ; finalB = (2 * b0 + b1 )/3 ;
193+ break ;
194+ case 3 :
195+ finalR = (r0 + 2 * r1 )/3 ; finalG = (g0 + 2 * g1 )/3 ; finalB = (b0 + 2 * b1 )/3 ;
196+ break ;
197+ }
198+ } else {
199+ switch (positionCode ) {
200+ case 0 :
201+ finalR = r0 ; finalG = g0 ; finalB = b0 ;
202+ break ;
203+ case 1 :
204+ finalR = r1 ; finalG = g1 ; finalB = b1 ;
205+ break ;
206+ case 2 :
207+ finalR = (r0 + r1 )/2 ; finalG = (g0 + g1 )/2 ; finalB = (b0 + b1 )/2 ;
208+ break ;
209+ case 3 :
210+ finalR = finalG = finalB = 0 ;
211+ break ;
212+ }
158213
159- output [j * outputStride + i ] = finalColor ;
160214 }
215+
216+ output [j * outputStride * 3 + i * 3 ] = finalR ;
217+ output [j * outputStride * 3 + i * 3 + 1 ] = finalG ;
218+ output [j * outputStride * 3 + i * 3 + 2 ] = finalB ;
161219 }
162220 }
163221}
164222
223+
165224/*
166225void DecompressBlockDXT1(): Decompresses one block of a DXT1 texture and stores the resulting pixels at the appropriate offset in 'image'.
167226
@@ -176,15 +235,20 @@ void DecompressBlockDXT1(uint32_t x, uint32_t y, uint32_t width,
176235 int transparent0 , int * simpleAlpha , int * complexAlpha ,
177236 uint32_t * image )
178237{
238+
179239 static const uint8_t const_alpha [] = {
180240 255 , 255 , 255 , 255 ,
181241 255 , 255 , 255 , 255 ,
182242 255 , 255 , 255 , 255 ,
183243 255 , 255 , 255 , 255
184244 };
185245
186- DecompressBlockDXT1Internal (blockStorage ,
187- image + x + (y * width ), width , transparent0 , simpleAlpha , complexAlpha , const_alpha );
246+
247+ if ( transparent0 )
248+ DecompressBlockDXT1Internal (blockStorage ,
249+ image + x + (y * width ), width , transparent0 , simpleAlpha , complexAlpha , const_alpha );
250+ else
251+ DecompressBlockDXT1InternalRGB (blockStorage , ((uint8_t * )image ) + x * 3 + (y * 3 * width ), width );
188252}
189253
190254/*
@@ -331,11 +395,3 @@ void DecompressBlockDXT3(uint32_t x, uint32_t y, uint32_t width,
331395 DecompressBlockDXT1Internal (blockStorage ,
332396 image + x + (y * width ), width , transparent0 , simpleAlpha , complexAlpha , alphaValues );
333397}
334-
335- // Texture DXT1 / DXT5 compression
336- // Using STB "on file" library
337- // go there https://github.com/nothings/stb
338- // for more details and other libs
339-
340- #define STB_DXT_IMPLEMENTATION
341- #include "stb_dxt_104.h"
0 commit comments