@@ -92,18 +92,17 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
92
92
93
93
code = * (const uint32_t * )(block + 4 );
94
94
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 ;
100
99
101
- alpha = alphaValues [j * 4 + i ];
100
+ finalColor = 0 ; positionCode = (code >> 2 * (4 * j + i )) & 0x03 ;
101
+ alpha = alphaValues [j * 4 + i ];
102
102
103
- finalColor = 0 ;
104
- positionCode = (code >> 2 * (4 * j + i )) & 0x03 ;
105
-
106
- switch (positionCode ) {
103
+ if (color0 > color1 ) {
104
+ switch (positionCode )
105
+ {
107
106
case 0 :
108
107
finalColor = PackRGBA (r0 , g0 , b0 , alpha );
109
108
break ;
@@ -117,24 +116,7 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
117
116
finalColor = PackRGBA ((r0 + 2 * r1 )/3 , (g0 + 2 * g1 )/3 , (b0 + 2 * b1 )/3 , alpha );
118
117
break ;
119
118
}
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 {
138
120
switch (positionCode ) {
139
121
case 0 :
140
122
finalColor = PackRGBA (r0 , g0 , b0 , alpha );
@@ -150,18 +132,95 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
150
132
finalColor = PackRGBA (0 , 0 , 0 , alpha );
151
133
break ;
152
134
}
135
+ }
136
+
137
+ if (!alpha )
138
+ * simpleAlpha = 1 ;
139
+ else if (alpha < 0xff )
140
+ * complexAlpha = 1 ;
153
141
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
+ }
158
213
159
- output [j * outputStride + i ] = finalColor ;
160
214
}
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 ;
161
219
}
162
220
}
163
221
}
164
222
223
+
165
224
/*
166
225
void DecompressBlockDXT1(): Decompresses one block of a DXT1 texture and stores the resulting pixels at the appropriate offset in 'image'.
167
226
@@ -176,15 +235,20 @@ void DecompressBlockDXT1(uint32_t x, uint32_t y, uint32_t width,
176
235
int transparent0 , int * simpleAlpha , int * complexAlpha ,
177
236
uint32_t * image )
178
237
{
238
+
179
239
static const uint8_t const_alpha [] = {
180
240
255 , 255 , 255 , 255 ,
181
241
255 , 255 , 255 , 255 ,
182
242
255 , 255 , 255 , 255 ,
183
243
255 , 255 , 255 , 255
184
244
};
185
245
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 );
188
252
}
189
253
190
254
/*
@@ -331,11 +395,3 @@ void DecompressBlockDXT3(uint32_t x, uint32_t y, uint32_t width,
331
395
DecompressBlockDXT1Internal (blockStorage ,
332
396
image + x + (y * width ), width , transparent0 , simpleAlpha , complexAlpha , alphaValues );
333
397
}
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