Bug Summary

File:Externals/SOIL/stbi_DDS_aug_c.h
Location:line 410, column 3
Description:Value stored to 'DXT_family' is never read

Annotated Source Code

1
2/// DDS file support, does decoding, _not_ direct uploading
3/// (use SOIL for that ;-)
4
5/// A bunch of DirectDraw Surface structures and flags
6typedef struct {
7 unsigned int dwMagic;
8 unsigned int dwSize;
9 unsigned int dwFlags;
10 unsigned int dwHeight;
11 unsigned int dwWidth;
12 unsigned int dwPitchOrLinearSize;
13 unsigned int dwDepth;
14 unsigned int dwMipMapCount;
15 unsigned int dwReserved1[ 11 ];
16
17 // DDPIXELFORMAT
18 struct {
19 unsigned int dwSize;
20 unsigned int dwFlags;
21 unsigned int dwFourCC;
22 unsigned int dwRGBBitCount;
23 unsigned int dwRBitMask;
24 unsigned int dwGBitMask;
25 unsigned int dwBBitMask;
26 unsigned int dwAlphaBitMask;
27 } sPixelFormat;
28
29 // DDCAPS2
30 struct {
31 unsigned int dwCaps1;
32 unsigned int dwCaps2;
33 unsigned int dwDDSX;
34 unsigned int dwReserved;
35 } sCaps;
36 unsigned int dwReserved2;
37} DDS_header ;
38
39// the following constants were copied directly off the MSDN website
40
41// The dwFlags member of the original DDSURFACEDESC2 structure
42// can be set to one or more of the following values.
43#define DDSD_CAPS0x00000001 0x00000001
44#define DDSD_HEIGHT0x00000002 0x00000002
45#define DDSD_WIDTH0x00000004 0x00000004
46#define DDSD_PITCH0x00000008 0x00000008
47#define DDSD_PIXELFORMAT0x00001000 0x00001000
48#define DDSD_MIPMAPCOUNT0x00020000 0x00020000
49#define DDSD_LINEARSIZE0x00080000 0x00080000
50#define DDSD_DEPTH0x00800000 0x00800000
51
52// DirectDraw Pixel Format
53#define DDPF_ALPHAPIXELS0x00000001 0x00000001
54#define DDPF_FOURCC0x00000004 0x00000004
55#define DDPF_RGB0x00000040 0x00000040
56
57// The dwCaps1 member of the DDSCAPS2 structure can be
58// set to one or more of the following values.
59#define DDSCAPS_COMPLEX0x00000008 0x00000008
60#define DDSCAPS_TEXTURE0x00001000 0x00001000
61#define DDSCAPS_MIPMAP0x00400000 0x00400000
62
63// The dwCaps2 member of the DDSCAPS2 structure can be
64// set to one or more of the following values.
65#define DDSCAPS2_CUBEMAP0x00000200 0x00000200
66#define DDSCAPS2_CUBEMAP_POSITIVEX0x00000400 0x00000400
67#define DDSCAPS2_CUBEMAP_NEGATIVEX0x00000800 0x00000800
68#define DDSCAPS2_CUBEMAP_POSITIVEY0x00001000 0x00001000
69#define DDSCAPS2_CUBEMAP_NEGATIVEY0x00002000 0x00002000
70#define DDSCAPS2_CUBEMAP_POSITIVEZ0x00004000 0x00004000
71#define DDSCAPS2_CUBEMAP_NEGATIVEZ0x00008000 0x00008000
72#define DDSCAPS2_VOLUME0x00200000 0x00200000
73
74static int dds_test(stbi *s)
75{
76 // check the magic number
77 if (get8(s) != 'D') return 0;
78 if (get8(s) != 'D') return 0;
79 if (get8(s) != 'S') return 0;
80 if (get8(s) != ' ') return 0;
81 // check header size
82 if (get32le(s) != 124) return 0;
83 return 1;
84}
85#ifndef STBI_NO_STDIO
86int stbi_dds_test_file (FILE *f)
87{
88 stbi s;
89 int r,n = ftell(f);
90 start_file(&s,f);
91 r = dds_test(&s);
92 fseek(f,n,SEEK_SET0);
93 return r;
94}
95#endif
96
97int stbi_dds_test_memory (stbi_uc const *buffer, int len)
98{
99 stbi s;
100 start_mem(&s,buffer, len);
101 return dds_test(&s);
102}
103
104// helper functions
105int stbi_convert_bit_range( int c, int from_bits, int to_bits )
106{
107 int b = (1 << (from_bits - 1)) + c * ((1 << to_bits) - 1);
108 return (b + (b >> from_bits)) >> from_bits;
109}
110void stbi_rgb_888_from_565( unsigned int c, int *r, int *g, int *b )
111{
112 *r = stbi_convert_bit_range( (c >> 11) & 31, 5, 8 );
113 *g = stbi_convert_bit_range( (c >> 05) & 63, 6, 8 );
114 *b = stbi_convert_bit_range( (c >> 00) & 31, 5, 8 );
115}
116void stbi_decode_DXT1_block(
117 unsigned char uncompressed[16*4],
118 unsigned char compressed[8] )
119{
120 int next_bit = 4*8;
121 int i, r, g, b;
122 int c0, c1;
123 unsigned char decode_colors[4*4];
124 // find the 2 primary colors
125 c0 = compressed[0] + (compressed[1] << 8);
126 c1 = compressed[2] + (compressed[3] << 8);
127 stbi_rgb_888_from_565( c0, &r, &g, &b );
128 decode_colors[0] = r;
129 decode_colors[1] = g;
130 decode_colors[2] = b;
131 decode_colors[3] = 255;
132 stbi_rgb_888_from_565( c1, &r, &g, &b );
133 decode_colors[4] = r;
134 decode_colors[5] = g;
135 decode_colors[6] = b;
136 decode_colors[7] = 255;
137 if( c0 > c1 )
138 {
139 // no alpha, 2 interpolated colors
140 decode_colors[8] = (2*decode_colors[0] + decode_colors[4]) / 3;
141 decode_colors[9] = (2*decode_colors[1] + decode_colors[5]) / 3;
142 decode_colors[10] = (2*decode_colors[2] + decode_colors[6]) / 3;
143 decode_colors[11] = 255;
144 decode_colors[12] = (decode_colors[0] + 2*decode_colors[4]) / 3;
145 decode_colors[13] = (decode_colors[1] + 2*decode_colors[5]) / 3;
146 decode_colors[14] = (decode_colors[2] + 2*decode_colors[6]) / 3;
147 decode_colors[15] = 255;
148 } else
149 {
150 // 1 interpolated color, alpha
151 decode_colors[8] = (decode_colors[0] + decode_colors[4]) / 2;
152 decode_colors[9] = (decode_colors[1] + decode_colors[5]) / 2;
153 decode_colors[10] = (decode_colors[2] + decode_colors[6]) / 2;
154 decode_colors[11] = 255;
155 decode_colors[12] = 0;
156 decode_colors[13] = 0;
157 decode_colors[14] = 0;
158 decode_colors[15] = 0;
159 }
160 // decode the block
161 for( i = 0; i < 16*4; i += 4 )
162 {
163 int idx = ((compressed[next_bit>>3] >> (next_bit & 7)) & 3) * 4;
164 next_bit += 2;
165 uncompressed[i+0] = decode_colors[idx+0];
166 uncompressed[i+1] = decode_colors[idx+1];
167 uncompressed[i+2] = decode_colors[idx+2];
168 uncompressed[i+3] = decode_colors[idx+3];
169 }
170 // done
171}
172void stbi_decode_DXT23_alpha_block(
173 unsigned char uncompressed[16*4],
174 unsigned char compressed[8] )
175{
176 int i, next_bit = 0;
177 // each alpha value gets 4 bits
178 for( i = 3; i < 16*4; i += 4 )
179 {
180 uncompressed[i] = stbi_convert_bit_range(
181 (compressed[next_bit>>3] >> (next_bit&7)) & 15,
182 4, 8 );
183 next_bit += 4;
184 }
185}
186void stbi_decode_DXT45_alpha_block(
187 unsigned char uncompressed[16*4],
188 unsigned char compressed[8] )
189{
190 int i, next_bit = 8*2;
191 unsigned char decode_alpha[8];
192 // each alpha value gets 3 bits, and the 1st 2 bytes are the range
193 decode_alpha[0] = compressed[0];
194 decode_alpha[1] = compressed[1];
195 if( decode_alpha[0] > decode_alpha[1] )
196 {
197 // 6 step intermediate
198 decode_alpha[2] = (6*decode_alpha[0] + 1*decode_alpha[1]) / 7;
199 decode_alpha[3] = (5*decode_alpha[0] + 2*decode_alpha[1]) / 7;
200 decode_alpha[4] = (4*decode_alpha[0] + 3*decode_alpha[1]) / 7;
201 decode_alpha[5] = (3*decode_alpha[0] + 4*decode_alpha[1]) / 7;
202 decode_alpha[6] = (2*decode_alpha[0] + 5*decode_alpha[1]) / 7;
203 decode_alpha[7] = (1*decode_alpha[0] + 6*decode_alpha[1]) / 7;
204 } else
205 {
206 // 4 step intermediate, pluss full and none
207 decode_alpha[2] = (4*decode_alpha[0] + 1*decode_alpha[1]) / 5;
208 decode_alpha[3] = (3*decode_alpha[0] + 2*decode_alpha[1]) / 5;
209 decode_alpha[4] = (2*decode_alpha[0] + 3*decode_alpha[1]) / 5;
210 decode_alpha[5] = (1*decode_alpha[0] + 4*decode_alpha[1]) / 5;
211 decode_alpha[6] = 0;
212 decode_alpha[7] = 255;
213 }
214 for( i = 3; i < 16*4; i += 4 )
215 {
216 int idx = 0, bit;
217 bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
218 idx += bit << 0;
219 ++next_bit;
220 bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
221 idx += bit << 1;
222 ++next_bit;
223 bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
224 idx += bit << 2;
225 ++next_bit;
226 uncompressed[i] = decode_alpha[idx & 7];
227 }
228 // done
229}
230void stbi_decode_DXT_color_block(
231 unsigned char uncompressed[16*4],
232 unsigned char compressed[8] )
233{
234 int next_bit = 4*8;
235 int i, r, g, b;
236 int c0, c1;
237 unsigned char decode_colors[4*3];
238 // find the 2 primary colors
239 c0 = compressed[0] + (compressed[1] << 8);
240 c1 = compressed[2] + (compressed[3] << 8);
241 stbi_rgb_888_from_565( c0, &r, &g, &b );
242 decode_colors[0] = r;
243 decode_colors[1] = g;
244 decode_colors[2] = b;
245 stbi_rgb_888_from_565( c1, &r, &g, &b );
246 decode_colors[3] = r;
247 decode_colors[4] = g;
248 decode_colors[5] = b;
249 // Like DXT1, but no choicees:
250 // no alpha, 2 interpolated colors
251 decode_colors[6] = (2*decode_colors[0] + decode_colors[3]) / 3;
252 decode_colors[7] = (2*decode_colors[1] + decode_colors[4]) / 3;
253 decode_colors[8] = (2*decode_colors[2] + decode_colors[5]) / 3;
254 decode_colors[9] = (decode_colors[0] + 2*decode_colors[3]) / 3;
255 decode_colors[10] = (decode_colors[1] + 2*decode_colors[4]) / 3;
256 decode_colors[11] = (decode_colors[2] + 2*decode_colors[5]) / 3;
257 // decode the block
258 for( i = 0; i < 16*4; i += 4 )
259 {
260 int idx = ((compressed[next_bit>>3] >> (next_bit & 7)) & 3) * 3;
261 next_bit += 2;
262 uncompressed[i+0] = decode_colors[idx+0];
263 uncompressed[i+1] = decode_colors[idx+1];
264 uncompressed[i+2] = decode_colors[idx+2];
265 }
266 // done
267}
268static stbi_uc *dds_load(stbi *s, int *x, int *y, int *comp, int req_comp)
269{
270 // all variables go up front
271 stbi_uc *dds_data = NULL((void*)0);
272 stbi_uc block[16*4];
273 stbi_uc compressed[8];
274 unsigned int flags;
275 int DXT_family;
276 int has_alpha, has_mipmap;
277 int is_compressed, cubemap_faces;
278 int block_pitch, cf;
279 DDS_header header;
280 unsigned int i, num_blocks, sz;
281 // load the header
282 if( sizeof( DDS_header ) != 128 )
283 {
284 return NULL((void*)0);
285 }
286 getn( s, (stbi_uc*)(&header), 128 );
287 // and do some checking
288 if( header.dwMagic != (('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24)) ) return NULL((void*)0);
289 if( header.dwSize != 124 ) return NULL((void*)0);
290 flags = DDSD_CAPS0x00000001 | DDSD_HEIGHT0x00000002 | DDSD_WIDTH0x00000004 | DDSD_PIXELFORMAT0x00001000;
291 if( (header.dwFlags & flags) != flags ) return NULL((void*)0);
292 /* According to the MSDN spec, the dwFlags should contain
293 DDSD_LINEARSIZE if it's compressed, or DDSD_PITCH if
294 uncompressed. Some DDS writers do not conform to the
295 spec, so I need to make my reader more tolerant */
296 if( header.sPixelFormat.dwSize != 32 ) return NULL((void*)0);
297 flags = DDPF_FOURCC0x00000004 | DDPF_RGB0x00000040;
298 if( (header.sPixelFormat.dwFlags & flags) == 0 ) return NULL((void*)0);
299 if( (header.sCaps.dwCaps1 & DDSCAPS_TEXTURE0x00001000) == 0 ) return NULL((void*)0);
300 // get the image data
301 s->img_x = header.dwWidth;
302 s->img_y = header.dwHeight;
303 s->img_n = 4;
304 is_compressed = (header.sPixelFormat.dwFlags & DDPF_FOURCC0x00000004) / DDPF_FOURCC0x00000004;
305 has_alpha = (header.sPixelFormat.dwFlags & DDPF_ALPHAPIXELS0x00000001) / DDPF_ALPHAPIXELS0x00000001;
306 has_mipmap = (header.sCaps.dwCaps1 & DDSCAPS_MIPMAP0x00400000) && (header.dwMipMapCount > 1);
307 cubemap_faces = (header.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP0x00000200) / DDSCAPS2_CUBEMAP0x00000200;
308 /* I need cubemaps to have square faces */
309 cubemap_faces &= (s->img_x == s->img_y);
310 cubemap_faces *= 5;
311 cubemap_faces += 1;
312 block_pitch = (s->img_x+3) >> 2;
313 num_blocks = block_pitch * ((s->img_y+3) >> 2);
314 /* let the user know what's going on */
315 *x = s->img_x;
316 *y = s->img_y;
317 *comp = s->img_n;
318 /* is this uncompressed? */
319 if( is_compressed )
320 {
321 /* compressed */
322 // note: header.sPixelFormat.dwFourCC is something like (('D'<<0)|('X'<<8)|('T'<<16)|('1'<<24))
323 DXT_family = 1 + (header.sPixelFormat.dwFourCC >> 24) - '1';
324 if( (DXT_family < 1) || (DXT_family > 5) ) return NULL((void*)0);
325 /* check the expected size...oops, nevermind...
326 those non-compliant writers leave
327 dwPitchOrLinearSize == 0 */
328 // passed all the tests, get the RAM for decoding
329 sz = (s->img_x)*(s->img_y)*4*cubemap_faces;
330 dds_data = (unsigned char*)malloc( sz );
331 /* do this once for each face */
332 for( cf = 0; cf < cubemap_faces; ++ cf )
333 {
334 // now read and decode all the blocks
335 for( i = 0; i < num_blocks; ++i )
336 {
337 // where are we?
338 int bx, by, bw=4, bh=4;
339 unsigned int ref_x = 4 * (i % block_pitch);
340 unsigned int ref_y = 4 * (i / block_pitch);
341 // get the next block's worth of compressed data, and decompress it
342 if( DXT_family == 1 )
343 {
344 // DXT1
345 getn( s, compressed, 8 );
346 stbi_decode_DXT1_block( block, compressed );
347 } else if( DXT_family < 4 )
348 {
349 // DXT2/3
350 getn( s, compressed, 8 );
351 stbi_decode_DXT23_alpha_block ( block, compressed );
352 getn( s, compressed, 8 );
353 stbi_decode_DXT_color_block ( block, compressed );
354 } else
355 {
356 // DXT4/5
357 getn( s, compressed, 8 );
358 stbi_decode_DXT45_alpha_block ( block, compressed );
359 getn( s, compressed, 8 );
360 stbi_decode_DXT_color_block ( block, compressed );
361 }
362 // is this a partial block?
363 if( ref_x + 4 > s->img_x )
364 {
365 bw = s->img_x - ref_x;
366 }
367 if( ref_y + 4 > s->img_y )
368 {
369 bh = s->img_y - ref_y;
370 }
371 // now drop our decompressed data into the buffer
372 for( by = 0; by < bh; ++by )
373 {
374 int idx = 4*((ref_y+by+cf*s->img_x)*s->img_x + ref_x);
375 for( bx = 0; bx < bw*4; ++bx )
376 {
377
378 dds_data[idx+bx] = block[by*16+bx];
379 }
380 }
381 }
382 /* done reading and decoding the main image...
383 skip MIPmaps if present */
384 if( has_mipmap )
385 {
386 int block_size = 16;
387 if( DXT_family == 1 )
388 {
389 block_size = 8;
390 }
391 for( i = 1; i < header.dwMipMapCount; ++i )
392 {
393 int mx = s->img_x >> (i + 2);
394 int my = s->img_y >> (i + 2);
395 if( mx < 1 )
396 {
397 mx = 1;
398 }
399 if( my < 1 )
400 {
401 my = 1;
402 }
403 skip( s, mx*my*block_size );
404 }
405 }
406 }/* per cubemap face */
407 } else
408 {
409 /* uncompressed */
410 DXT_family = 0;
Value stored to 'DXT_family' is never read
411 s->img_n = 3;
412 if( has_alpha )
413 {
414 s->img_n = 4;
415 }
416 *comp = s->img_n;
417 sz = s->img_x*s->img_y*s->img_n*cubemap_faces;
418 dds_data = (unsigned char*)malloc( sz );
419 /* do this once for each face */
420 for( cf = 0; cf < cubemap_faces; ++ cf )
421 {
422 /* read the main image for this face */
423 getn( s, &dds_data[cf*s->img_x*s->img_y*s->img_n], s->img_x*s->img_y*s->img_n );
424 /* done reading and decoding the main image...
425 skip MIPmaps if present */
426 if( has_mipmap )
427 {
428 for( i = 1; i < header.dwMipMapCount; ++i )
429 {
430 int mx = s->img_x >> i;
431 int my = s->img_y >> i;
432 if( mx < 1 )
433 {
434 mx = 1;
435 }
436 if( my < 1 )
437 {
438 my = 1;
439 }
440 skip( s, mx*my*s->img_n );
441 }
442 }
443 }
444 /* data was BGR, I need it RGB */
445 for( i = 0; i < sz; i += s->img_n )
446 {
447 unsigned char temp = dds_data[i];
448 dds_data[i] = dds_data[i+2];
449 dds_data[i+2] = temp;
450 }
451 }
452 /* finished decompressing into RGBA,
453 adjust the y size if we have a cubemap
454 note: sz is already up to date */
455 s->img_y *= cubemap_faces;
456 *y = s->img_y;
457 // did the user want something else, or
458 // see if all the alpha values are 255 (i.e. no transparency)
459 has_alpha = 0;
460 if( s->img_n == 4)
461 {
462 for( i = 3; (i < sz) && (has_alpha == 0); i += 4 )
463 {
464 has_alpha |= (dds_data[i] < 255);
465 }
466 }
467 if( (req_comp <= 4) && (req_comp >= 1) )
468 {
469 // user has some requirements, meet them
470 if( req_comp != s->img_n )
471 {
472 dds_data = convert_format( dds_data, s->img_n, req_comp, s->img_x, s->img_y );
473 *comp = s->img_n;
474 }
475 } else
476 {
477 // user had no requirements, only drop to RGB is no alpha
478 if( (has_alpha == 0) && (s->img_n == 4) )
479 {
480 dds_data = convert_format( dds_data, 4, 3, s->img_x, s->img_y );
481 *comp = 3;
482 }
483 }
484 // OK, done
485 return dds_data;
486}
487
488#ifndef STBI_NO_STDIO
489stbi_uc *stbi_dds_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
490{
491 stbi s;
492 start_file(&s,f);
493 return dds_load(&s,x,y,comp,req_comp);
494}
495
496stbi_uc *stbi_dds_load (char *filename, int *x, int *y, int *comp, int req_comp)
497{
498 stbi_uc *data;
499 FILE *f = fopen(filename, "rb");
500 if (!f) return NULL((void*)0);
501 data = stbi_dds_load_from_file(f,x,y,comp,req_comp);
502 fclose(f);
503 return data;
504}
505#endif
506
507stbi_uc *stbi_dds_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
508{
509 stbi s;
510 start_mem(&s,buffer, len);
511 return dds_load(&s,x,y,comp,req_comp);
512}