1 #include "stb_image/stb_image.h"
2 
3 #ifndef STBI_HEADER_FILE_ONLY
4 
5 #ifndef STBI_NO_HDR
6 #include <math.h>  // ldexp
7 #include <string.h> // strcmp, strtok
8 #endif
9 
10 #ifndef STBI_NO_STDIO
11 #include <stdio.h>
12 #endif
13 #include <stdlib.h>
14 #include <memory.h>
15 #include <assert.h>
16 #include <stdarg.h>
17 
18 #ifndef _MSC_VER
19    #ifdef __cplusplus
20    #define stbi_inline inline
21    #else
22    #define stbi_inline
23    #endif
24 #else
25    #define stbi_inline __forceinline
26 #endif
27 
28 
29 // implementation:
30 typedef unsigned char  uint8;
31 typedef unsigned short uint16;
32 typedef   signed short  int16;
33 typedef unsigned int   uint32;
34 typedef   signed int    int32;
35 typedef unsigned int   uint;
36 
37 // should produce compiler error if size is wrong
38 typedef unsigned char validate_uint32[sizeof(uint32)==4 ? 1 : -1];
39 
40 #if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE)
41 #define STBI_NO_WRITE
42 #endif
43 
44 #define STBI_NOTUSED(v)  (void)sizeof(v)
45 
46 #ifdef _MSC_VER
47 #define STBI_HAS_LROTL
48 #endif
49 
50 #ifdef STBI_HAS_LROTL
51    #define stbi_lrot(x,y)  _lrotl(x,y)
52 #else
53    #define stbi_lrot(x,y)  (((x) << (y)) | ((x) >> (32 - (y))))
54 #endif
55 
56 ///////////////////////////////////////////////
57 //
58 //  stbi struct and start_xxx functions
59 
60 // stbi structure is our basic context used by all images, so it
61 // contains all the IO context, plus some basic image information
62 typedef struct
63 {
64    uint32 img_x, img_y;
65    int img_n, img_out_n;
66 
67    stbi_io_callbacks io;
68    void *io_user_data;
69 
70    int read_from_callbacks;
71    int buflen;
72    uint8 buffer_start[128];
73 
74    uint8 *img_buffer, *img_buffer_end;
75    uint8 *img_buffer_original;
76 } stbi;
77 
78 
79 static void refill_buffer(stbi *s);
80 
81 // initialize a memory-decode context
start_mem(stbi * s,uint8 const * buffer,int len)82 static void start_mem(stbi *s, uint8 const *buffer, int len)
83 {
84    s->io.read = NULL;
85    s->read_from_callbacks = 0;
86    s->img_buffer = s->img_buffer_original = (uint8 *) buffer;
87    s->img_buffer_end = (uint8 *) buffer+len;
88 }
89 
90 // initialize a callback-based context
start_callbacks(stbi * s,stbi_io_callbacks * c,void * user)91 static void start_callbacks(stbi *s, stbi_io_callbacks *c, void *user)
92 {
93    s->io = *c;
94    s->io_user_data = user;
95    s->buflen = sizeof(s->buffer_start);
96    s->read_from_callbacks = 1;
97    s->img_buffer_original = s->buffer_start;
98    refill_buffer(s);
99 }
100 
101 #ifndef STBI_NO_STDIO
102 
stdio_read(void * user,char * data,int size)103 static int stdio_read(void *user, char *data, int size)
104 {
105    return (int) fread(data,1,size,(FILE*) user);
106 }
107 
stdio_skip(void * user,unsigned n)108 static void stdio_skip(void *user, unsigned n)
109 {
110    fseek((FILE*) user, n, SEEK_CUR);
111 }
112 
stdio_eof(void * user)113 static int stdio_eof(void *user)
114 {
115    return feof((FILE*) user);
116 }
117 
118 static stbi_io_callbacks stbi_stdio_callbacks =
119 {
120    stdio_read,
121    stdio_skip,
122    stdio_eof,
123 };
124 
start_file(stbi * s,FILE * f)125 static void start_file(stbi *s, FILE *f)
126 {
127    start_callbacks(s, &stbi_stdio_callbacks, (void *) f);
128 }
129 
130 //static void stop_file(stbi *s) { }
131 
132 #endif // !STBI_NO_STDIO
133 
stbi_rewind(stbi * s)134 static void stbi_rewind(stbi *s)
135 {
136    // conceptually rewind SHOULD rewind to the beginning of the stream,
137    // but we just rewind to the beginning of the initial buffer, because
138    // we only use it after doing 'test', which only ever looks at at most 92 bytes
139    s->img_buffer = s->img_buffer_original;
140 }
141 
142 static int      stbi_jpeg_test(stbi *s);
143 static stbi_uc *stbi_jpeg_load(stbi *s, int *x, int *y, int *comp, int req_comp);
144 static int      stbi_jpeg_info(stbi *s, int *x, int *y, int *comp);
145 static int      stbi_png_test(stbi *s);
146 static stbi_uc *stbi_png_load(stbi *s, int *x, int *y, int *comp, int req_comp);
147 static int      stbi_png_info(stbi *s, int *x, int *y, int *comp);
148 static int      stbi_bmp_test(stbi *s);
149 static stbi_uc *stbi_bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp);
150 static int      stbi_tga_test(stbi *s);
151 static stbi_uc *stbi_tga_load(stbi *s, int *x, int *y, int *comp, int req_comp);
152 static int      stbi_tga_info(stbi *s, int *x, int *y, int *comp);
153 static int      stbi_psd_test(stbi *s);
154 static stbi_uc *stbi_psd_load(stbi *s, int *x, int *y, int *comp, int req_comp);
155 static int      stbi_hdr_test(stbi *s);
156 static float   *stbi_hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp);
157 static int      stbi_pic_test(stbi *s);
158 static stbi_uc *stbi_pic_load(stbi *s, int *x, int *y, int *comp, int req_comp);
159 static int      stbi_gif_test(stbi *s);
160 static stbi_uc *stbi_gif_load(stbi *s, int *x, int *y, int *comp, int req_comp);
161 static int      stbi_gif_info(stbi *s, int *x, int *y, int *comp);
162 
163 
164 // this is not threadsafe
165 static const char *failure_reason;
166 
stbi_failure_reason(void)167 const char *stbi_failure_reason(void)
168 {
169    return failure_reason;
170 }
171 
e(const char * str)172 static int e(const char *str)
173 {
174    failure_reason = str;
175    return 0;
176 }
177 
178 // e - error
179 // epf - error returning pointer to float
180 // epuc - error returning pointer to unsigned char
181 
182 #ifdef STBI_NO_FAILURE_STRINGS
183    #define e(x,y)  0
184 #elif defined(STBI_FAILURE_USERMSG)
185    #define e(x,y)  e(y)
186 #else
187    #define e(x,y)  e(x)
188 #endif
189 
190 #define epf(x,y)   ((float *) (e(x,y)?NULL:NULL))
191 #define epuc(x,y)  ((unsigned char *) (e(x,y)?NULL:NULL))
192 
stbi_image_free(void * retval_from_stbi_load)193 void stbi_image_free(void *retval_from_stbi_load)
194 {
195    free(retval_from_stbi_load);
196 }
197 
198 #ifndef STBI_NO_HDR
199 static float   *ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
200 static stbi_uc *hdr_to_ldr(float   *data, int x, int y, int comp);
201 #endif
202 
stbi_load_main(stbi * s,int * x,int * y,int * comp,int req_comp)203 static unsigned char *stbi_load_main(stbi *s, int *x, int *y, int *comp, int req_comp)
204 {
205    if (stbi_jpeg_test(s)) return stbi_jpeg_load(s,x,y,comp,req_comp);
206    if (stbi_png_test(s))  return stbi_png_load(s,x,y,comp,req_comp);
207    if (stbi_bmp_test(s))  return stbi_bmp_load(s,x,y,comp,req_comp);
208    if (stbi_gif_test(s))  return stbi_gif_load(s,x,y,comp,req_comp);
209    if (stbi_psd_test(s))  return stbi_psd_load(s,x,y,comp,req_comp);
210    if (stbi_pic_test(s))  return stbi_pic_load(s,x,y,comp,req_comp);
211 
212    #ifndef STBI_NO_HDR
213    if (stbi_hdr_test(s)) {
214       float *hdr = stbi_hdr_load(s, x,y,comp,req_comp);
215       return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
216    }
217    #endif
218 
219    // test tga last because it's a crappy test!
220    if (stbi_tga_test(s))
221       return stbi_tga_load(s,x,y,comp,req_comp);
222    return epuc("unknown image type", "Image not of any known type, or corrupt");
223 }
224 
225 #ifndef STBI_NO_STDIO
stbi_load(char const * filename,int * x,int * y,int * comp,int req_comp)226 unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
227 {
228    FILE *f = fopen(filename, "rb");
229    unsigned char *result;
230    if (!f) return epuc("can't fopen", "Unable to open file");
231    result = stbi_load_from_file(f,x,y,comp,req_comp);
232    fclose(f);
233    return result;
234 }
235 
stbi_load_from_file(FILE * f,int * x,int * y,int * comp,int req_comp)236 unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
237 {
238    stbi s;
239    start_file(&s,f);
240    return stbi_load_main(&s,x,y,comp,req_comp);
241 }
242 #endif //!STBI_NO_STDIO
243 
stbi_load_from_memory(stbi_uc const * buffer,int len,int * x,int * y,int * comp,int req_comp)244 unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
245 {
246    stbi s;
247    start_mem(&s,buffer,len);
248    return stbi_load_main(&s,x,y,comp,req_comp);
249 }
250 
stbi_load_from_callbacks(stbi_io_callbacks const * clbk,void * user,int * x,int * y,int * comp,int req_comp)251 unsigned char *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
252 {
253    stbi s;
254    start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
255    return stbi_load_main(&s,x,y,comp,req_comp);
256 }
257 
258 #ifndef STBI_NO_HDR
259 
stbi_loadf_main(stbi * s,int * x,int * y,int * comp,int req_comp)260 float *stbi_loadf_main(stbi *s, int *x, int *y, int *comp, int req_comp)
261 {
262    unsigned char *data;
263    #ifndef STBI_NO_HDR
264    if (stbi_hdr_test(s))
265       return stbi_hdr_load(s,x,y,comp,req_comp);
266    #endif
267    data = stbi_load_main(s, x, y, comp, req_comp);
268    if (data)
269       return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
270    return epf("unknown image type", "Image not of any known type, or corrupt");
271 }
272 
stbi_loadf_from_memory(stbi_uc const * buffer,int len,int * x,int * y,int * comp,int req_comp)273 float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
274 {
275    stbi s;
276    start_mem(&s,buffer,len);
277    return stbi_loadf_main(&s,x,y,comp,req_comp);
278 }
279 
stbi_loadf_from_callbacks(stbi_io_callbacks const * clbk,void * user,int * x,int * y,int * comp,int req_comp)280 float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
281 {
282    stbi s;
283    start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
284    return stbi_loadf_main(&s,x,y,comp,req_comp);
285 }
286 
287 #ifndef STBI_NO_STDIO
stbi_loadf(char const * filename,int * x,int * y,int * comp,int req_comp)288 float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
289 {
290    FILE *f = fopen(filename, "rb");
291    float *result;
292    if (!f) return epf("can't fopen", "Unable to open file");
293    result = stbi_loadf_from_file(f,x,y,comp,req_comp);
294    fclose(f);
295    return result;
296 }
297 
stbi_loadf_from_file(FILE * f,int * x,int * y,int * comp,int req_comp)298 float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
299 {
300    stbi s;
301    start_file(&s,f);
302    return stbi_loadf_main(&s,x,y,comp,req_comp);
303 }
304 #endif // !STBI_NO_STDIO
305 
306 #endif // !STBI_NO_HDR
307 
308 // these is-hdr-or-not is defined independent of whether STBI_NO_HDR is
309 // defined, for API simplicity; if STBI_NO_HDR is defined, it always
310 // reports false!
311 
stbi_is_hdr_from_memory(stbi_uc const * buffer,int len)312 int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
313 {
314    #ifndef STBI_NO_HDR
315    stbi s;
316    start_mem(&s,buffer,len);
317    return stbi_hdr_test(&s);
318    #else
319    STBI_NOTUSED(buffer);
320    STBI_NOTUSED(len);
321    return 0;
322    #endif
323 }
324 
325 #ifndef STBI_NO_STDIO
stbi_is_hdr(char const * filename)326 extern int      stbi_is_hdr          (char const *filename)
327 {
328    FILE *f = fopen(filename, "rb");
329    int result=0;
330    if (f) {
331       result = stbi_is_hdr_from_file(f);
332       fclose(f);
333    }
334    return result;
335 }
336 
stbi_is_hdr_from_file(FILE * f)337 extern int      stbi_is_hdr_from_file(FILE *f)
338 {
339    #ifndef STBI_NO_HDR
340    stbi s;
341    start_file(&s,f);
342    return stbi_hdr_test(&s);
343    #else
344    return 0;
345    #endif
346 }
347 #endif // !STBI_NO_STDIO
348 
stbi_is_hdr_from_callbacks(stbi_io_callbacks const * clbk,void * user)349 extern int      stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
350 {
351    #ifndef STBI_NO_HDR
352    stbi s;
353    start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
354    return stbi_hdr_test(&s);
355    #else
356    return 0;
357    #endif
358 }
359 
360 #ifndef STBI_NO_HDR
361 static float h2l_gamma_i=1.0f/2.2f, h2l_scale_i=1.0f;
362 static float l2h_gamma=2.2f, l2h_scale=1.0f;
363 
stbi_hdr_to_ldr_gamma(float gamma)364 void   stbi_hdr_to_ldr_gamma(float gamma) { h2l_gamma_i = 1/gamma; }
stbi_hdr_to_ldr_scale(float scale)365 void   stbi_hdr_to_ldr_scale(float scale) { h2l_scale_i = 1/scale; }
366 
stbi_ldr_to_hdr_gamma(float gamma)367 void   stbi_ldr_to_hdr_gamma(float gamma) { l2h_gamma = gamma; }
stbi_ldr_to_hdr_scale(float scale)368 void   stbi_ldr_to_hdr_scale(float scale) { l2h_scale = scale; }
369 #endif
370 
371 
372 //////////////////////////////////////////////////////////////////////////////
373 //
374 // Common code used by all image loaders
375 //
376 
377 enum
378 {
379    SCAN_load=0,
380    SCAN_type,
381    SCAN_header
382 };
383 
refill_buffer(stbi * s)384 static void refill_buffer(stbi *s)
385 {
386    int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
387    if (n == 0) {
388       // at end of file, treat same as if from memory
389       s->read_from_callbacks = 0;
390       s->img_buffer = s->img_buffer_end-1;
391       *s->img_buffer = 0;
392    } else {
393       s->img_buffer = s->buffer_start;
394       s->img_buffer_end = s->buffer_start + n;
395    }
396 }
397 
get8(stbi * s)398 stbi_inline static int get8(stbi *s)
399 {
400    if (s->img_buffer < s->img_buffer_end)
401       return *s->img_buffer++;
402    if (s->read_from_callbacks) {
403       refill_buffer(s);
404       return *s->img_buffer++;
405    }
406    return 0;
407 }
408 
at_eof(stbi * s)409 stbi_inline static int at_eof(stbi *s)
410 {
411    if (s->io.read) {
412       if (!(s->io.eof)(s->io_user_data)) return 0;
413       // if feof() is true, check if buffer = end
414       // special case: we've only got the special 0 character at the end
415       if (s->read_from_callbacks == 0) return 1;
416    }
417 
418    return s->img_buffer >= s->img_buffer_end;
419 }
420 
get8u(stbi * s)421 stbi_inline static uint8 get8u(stbi *s)
422 {
423    return (uint8) get8(s);
424 }
425 
skip(stbi * s,int n)426 static void skip(stbi *s, int n)
427 {
428    if (s->io.read) {
429       int blen = s->img_buffer_end - s->img_buffer;
430       if (blen < n) {
431          s->img_buffer = s->img_buffer_end;
432          (s->io.skip)(s->io_user_data, n - blen);
433          return;
434       }
435    }
436    s->img_buffer += n;
437 }
438 
getn(stbi * s,stbi_uc * buffer,int n)439 static int getn(stbi *s, stbi_uc *buffer, int n)
440 {
441    if (s->io.read) {
442       int blen = s->img_buffer_end - s->img_buffer;
443       if (blen < n) {
444          int res, count;
445 
446          memcpy(buffer, s->img_buffer, blen);
447 
448          count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);
449          res = (count == (n-blen));
450          s->img_buffer = s->img_buffer_end;
451          return res;
452       }
453    }
454 
455    if (s->img_buffer+n <= s->img_buffer_end) {
456       memcpy(buffer, s->img_buffer, n);
457       s->img_buffer += n;
458       return 1;
459    } else
460       return 0;
461 }
462 
get16(stbi * s)463 static int get16(stbi *s)
464 {
465    int z = get8(s);
466    return (z << 8) + get8(s);
467 }
468 
get32(stbi * s)469 static uint32 get32(stbi *s)
470 {
471    uint32 z = get16(s);
472    return (z << 16) + get16(s);
473 }
474 
get16le(stbi * s)475 static int get16le(stbi *s)
476 {
477    int z = get8(s);
478    return z + (get8(s) << 8);
479 }
480 
get32le(stbi * s)481 static uint32 get32le(stbi *s)
482 {
483    uint32 z = get16le(s);
484    return z + (get16le(s) << 16);
485 }
486 
487 //////////////////////////////////////////////////////////////////////////////
488 //
489 //  generic converter from built-in img_n to req_comp
490 //    individual types do this automatically as much as possible (e.g. jpeg
491 //    does all cases internally since it needs to colorspace convert anyway,
492 //    and it never has alpha, so very few cases ). png can automatically
493 //    interleave an alpha=255 channel, but falls back to this for other cases
494 //
495 //  assume data buffer is malloced, so malloc a new one and free that one
496 //  only failure mode is malloc failing
497 
compute_y(int r,int g,int b)498 static uint8 compute_y(int r, int g, int b)
499 {
500    return (uint8) (((r*77) + (g*150) +  (29*b)) >> 8);
501 }
502 
convert_format(unsigned char * data,int img_n,int req_comp,uint x,uint y)503 static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y)
504 {
505    int i,j;
506    unsigned char *good;
507 
508    if (req_comp == img_n) return data;
509    assert(req_comp >= 1 && req_comp <= 4);
510 
511    good = (unsigned char *) malloc(req_comp * x * y);
512    if (good == NULL) {
513       free(data);
514       return epuc("outofmem", "Out of memory");
515    }
516 
517    for (j=0; j < (int) y; ++j) {
518       unsigned char *src  = data + j * x * img_n   ;
519       unsigned char *dest = good + j * x * req_comp;
520 
521       #define COMBO(a,b)  ((a)*8+(b))
522       #define CASE(a,b)   case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
523       // convert source image with img_n components to one with req_comp components;
524       // avoid switch per pixel, so use switch per scanline and massive macros
525       switch (COMBO(img_n, req_comp)) {
526          CASE(1,2) dest[0]=src[0], dest[1]=255; break;
527          CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;
528          CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;
529          CASE(2,1) dest[0]=src[0]; break;
530          CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;
531          CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;
532          CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;
533          CASE(3,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
534          CASE(3,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
535          CASE(4,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
536          CASE(4,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;
537          CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;
538          default: assert(0);
539       }
540       #undef CASE
541    }
542 
543    free(data);
544    return good;
545 }
546 
547 #ifndef STBI_NO_HDR
ldr_to_hdr(stbi_uc * data,int x,int y,int comp)548 static float   *ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
549 {
550    int i,k,n;
551    float *output = (float *) malloc(x * y * comp * sizeof(float));
552    if (output == NULL) { free(data); return epf("outofmem", "Out of memory"); }
553    // compute number of non-alpha components
554    if (comp & 1) n = comp; else n = comp-1;
555    for (i=0; i < x*y; ++i) {
556       for (k=0; k < n; ++k) {
557          output[i*comp + k] = (float) pow(data[i*comp+k]/255.0f, l2h_gamma) * l2h_scale;
558       }
559       if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
560    }
561    free(data);
562    return output;
563 }
564 
565 #define float2int(x)   ((int) (x))
hdr_to_ldr(float * data,int x,int y,int comp)566 static stbi_uc *hdr_to_ldr(float   *data, int x, int y, int comp)
567 {
568    int i,k,n;
569    stbi_uc *output = (stbi_uc *) malloc(x * y * comp);
570    if (output == NULL) { free(data); return epuc("outofmem", "Out of memory"); }
571    // compute number of non-alpha components
572    if (comp & 1) n = comp; else n = comp-1;
573    for (i=0; i < x*y; ++i) {
574       for (k=0; k < n; ++k) {
575          float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f;
576          if (z < 0) z = 0;
577          if (z > 255) z = 255;
578          output[i*comp + k] = (uint8) float2int(z);
579       }
580       if (k < comp) {
581          float z = data[i*comp+k] * 255 + 0.5f;
582          if (z < 0) z = 0;
583          if (z > 255) z = 255;
584          output[i*comp + k] = (uint8) float2int(z);
585       }
586    }
587    free(data);
588    return output;
589 }
590 #endif
591 
592 //////////////////////////////////////////////////////////////////////////////
593 //
594 //  "baseline" JPEG/JFIF decoder (not actually fully baseline implementation)
595 //
596 //    simple implementation
597 //      - channel subsampling of at most 2 in each dimension
598 //      - doesn't support delayed output of y-dimension
599 //      - simple interface (only one output format: 8-bit interleaved RGB)
600 //      - doesn't try to recover corrupt jpegs
601 //      - doesn't allow partial loading, loading multiple at once
602 //      - still fast on x86 (copying globals into locals doesn't help x86)
603 //      - allocates lots of intermediate memory (full size of all components)
604 //        - non-interleaved case requires this anyway
605 //        - allows good upsampling (see next)
606 //    high-quality
607 //      - upsampled channels are bilinearly interpolated, even across blocks
608 //      - quality integer IDCT derived from IJG's 'slow'
609 //    performance
610 //      - fast huffman; reasonable integer IDCT
611 //      - uses a lot of intermediate memory, could cache poorly
612 //      - load http://nothings.org/remote/anemones.jpg 3 times on 2.8Ghz P4
613 //          stb_jpeg:   1.34 seconds (MSVC6, default release build)
614 //          stb_jpeg:   1.06 seconds (MSVC6, processor = Pentium Pro)
615 //          IJL11.dll:  1.08 seconds (compiled by intel)
616 //          IJG 1998:   0.98 seconds (MSVC6, makefile provided by IJG)
617 //          IJG 1998:   0.95 seconds (MSVC6, makefile + proc=PPro)
618 
619 // huffman decoding acceleration
620 #define FAST_BITS   9  // larger handles more cases; smaller stomps less cache
621 
622 typedef struct
623 {
624    uint8  fast[1 << FAST_BITS];
625    // weirdly, repacking this into AoS is a 10% speed loss, instead of a win
626    uint16 code[256];
627    uint8  values[256];
628    uint8  size[257];
629    unsigned int maxcode[18];
630    int    delta[17];   // old 'firstsymbol' - old 'firstcode'
631 } huffman;
632 
633 typedef struct
634 {
635    #ifdef STBI_SIMD
636    unsigned short dequant2[4][64];
637    #endif
638    stbi *s;
639    huffman huff_dc[4];
640    huffman huff_ac[4];
641    uint8 dequant[4][64];
642 
643 // sizes for components, interleaved MCUs
644    int img_h_max, img_v_max;
645    int img_mcu_x, img_mcu_y;
646    int img_mcu_w, img_mcu_h;
647 
648 // definition of jpeg image component
649    struct
650    {
651       int id;
652       int h,v;
653       int tq;
654       int hd,ha;
655       int dc_pred;
656 
657       int x,y,w2,h2;
658       uint8 *data;
659       void *raw_data;
660       uint8 *linebuf;
661    } img_comp[4];
662 
663    uint32         code_buffer; // jpeg entropy-coded buffer
664    int            code_bits;   // number of valid bits
665    unsigned char  marker;      // marker seen while filling entropy buffer
666    int            nomore;      // flag if we saw a marker so must stop
667 
668    int scan_n, order[4];
669    int restart_interval, todo;
670 } jpeg;
671 
build_huffman(huffman * h,int * count)672 static int build_huffman(huffman *h, int *count)
673 {
674    int i,j,k=0,code;
675    // build size list for each symbol (from JPEG spec)
676    for (i=0; i < 16; ++i)
677       for (j=0; j < count[i]; ++j)
678          h->size[k++] = (uint8) (i+1);
679    h->size[k] = 0;
680 
681    // compute actual symbols (from jpeg spec)
682    code = 0;
683    k = 0;
684    for(j=1; j <= 16; ++j) {
685       // compute delta to add to code to compute symbol id
686       h->delta[j] = k - code;
687       if (h->size[k] == j) {
688          while (h->size[k] == j)
689             h->code[k++] = (uint16) (code++);
690          if (code-1 >= (1 << j)) return e("bad code lengths","Corrupt JPEG");
691       }
692       // compute largest code + 1 for this size, preshifted as needed later
693       h->maxcode[j] = code << (16-j);
694       code <<= 1;
695    }
696    h->maxcode[j] = 0xffffffff;
697 
698    // build non-spec acceleration table; 255 is flag for not-accelerated
699    memset(h->fast, 255, 1 << FAST_BITS);
700    for (i=0; i < k; ++i) {
701       int s = h->size[i];
702       if (s <= FAST_BITS) {
703          int c = h->code[i] << (FAST_BITS-s);
704          int m = 1 << (FAST_BITS-s);
705          for (j=0; j < m; ++j) {
706             h->fast[c+j] = (uint8) i;
707          }
708       }
709    }
710    return 1;
711 }
712 
grow_buffer_unsafe(jpeg * j)713 static void grow_buffer_unsafe(jpeg *j)
714 {
715    do {
716       int b = j->nomore ? 0 : get8(j->s);
717       if (b == 0xff) {
718          int c = get8(j->s);
719          if (c != 0) {
720             j->marker = (unsigned char) c;
721             j->nomore = 1;
722             return;
723          }
724       }
725       j->code_buffer |= b << (24 - j->code_bits);
726       j->code_bits += 8;
727    } while (j->code_bits <= 24);
728 }
729 
730 // (1 << n) - 1
731 static uint32 bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
732 
733 // decode a jpeg huffman value from the bitstream
decode(jpeg * j,huffman * h)734 stbi_inline static int decode(jpeg *j, huffman *h)
735 {
736    unsigned int temp;
737    int c,k;
738 
739    if (j->code_bits < 16) grow_buffer_unsafe(j);
740 
741    // look at the top FAST_BITS and determine what symbol ID it is,
742    // if the code is <= FAST_BITS
743    c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
744    k = h->fast[c];
745    if (k < 255) {
746       int s = h->size[k];
747       if (s > j->code_bits)
748          return -1;
749       j->code_buffer <<= s;
750       j->code_bits -= s;
751       return h->values[k];
752    }
753 
754    // naive test is to shift the code_buffer down so k bits are
755    // valid, then test against maxcode. To speed this up, we've
756    // preshifted maxcode left so that it has (16-k) 0s at the
757    // end; in other words, regardless of the number of bits, it
758    // wants to be compared against something shifted to have 16;
759    // that way we don't need to shift inside the loop.
760    temp = j->code_buffer >> 16;
761    for (k=FAST_BITS+1 ; ; ++k)
762       if (temp < h->maxcode[k])
763          break;
764    if (k == 17) {
765       // error! code not found
766       j->code_bits -= 16;
767       return -1;
768    }
769 
770    if (k > j->code_bits)
771       return -1;
772 
773    // convert the huffman code to the symbol id
774    c = ((j->code_buffer >> (32 - k)) & bmask[k]) + h->delta[k];
775    assert((((j->code_buffer) >> (32 - h->size[c])) & bmask[h->size[c]]) == h->code[c]);
776 
777    // convert the id to a symbol
778    j->code_bits -= k;
779    j->code_buffer <<= k;
780    return h->values[c];
781 }
782 
783 // combined JPEG 'receive' and JPEG 'extend', since baseline
784 // always extends everything it receives.
extend_receive(jpeg * j,int n)785 stbi_inline static int extend_receive(jpeg *j, int n)
786 {
787    unsigned int m = 1 << (n-1);
788    unsigned int k;
789    if (j->code_bits < n) grow_buffer_unsafe(j);
790 
791    #if 1
792    k = stbi_lrot(j->code_buffer, n);
793    j->code_buffer = k & ~bmask[n];
794    k &= bmask[n];
795    j->code_bits -= n;
796    #else
797    k = (j->code_buffer >> (32 - n)) & bmask[n];
798    j->code_bits -= n;
799    j->code_buffer <<= n;
800    #endif
801    // the following test is probably a random branch that won't
802    // predict well. I tried to table accelerate it but failed.
803    // maybe it's compiling as a conditional move?
804    if (k < m)
805       return (-1 << n) + k + 1;
806    else
807       return k;
808 }
809 
810 // given a value that's at position X in the zigzag stream,
811 // where does it appear in the 8x8 matrix coded as row-major?
812 static uint8 dezigzag[64+15] =
813 {
814     0,  1,  8, 16,  9,  2,  3, 10,
815    17, 24, 32, 25, 18, 11,  4,  5,
816    12, 19, 26, 33, 40, 48, 41, 34,
817    27, 20, 13,  6,  7, 14, 21, 28,
818    35, 42, 49, 56, 57, 50, 43, 36,
819    29, 22, 15, 23, 30, 37, 44, 51,
820    58, 59, 52, 45, 38, 31, 39, 46,
821    53, 60, 61, 54, 47, 55, 62, 63,
822    // let corrupt input sample past end
823    63, 63, 63, 63, 63, 63, 63, 63,
824    63, 63, 63, 63, 63, 63, 63
825 };
826 
827 // decode one 64-entry block--
decode_block(jpeg * j,short data[64],huffman * hdc,huffman * hac,int b)828 static int decode_block(jpeg *j, short data[64], huffman *hdc, huffman *hac, int b)
829 {
830    int diff,dc,k;
831    int t = decode(j, hdc);
832    if (t < 0) return e("bad huffman code","Corrupt JPEG");
833 
834    // 0 all the ac values now so we can do it 32-bits at a time
835    memset(data,0,64*sizeof(data[0]));
836 
837    diff = t ? extend_receive(j, t) : 0;
838    dc = j->img_comp[b].dc_pred + diff;
839    j->img_comp[b].dc_pred = dc;
840    data[0] = (short) dc;
841 
842    // decode AC components, see JPEG spec
843    k = 1;
844    do {
845       int r,s;
846       int rs = decode(j, hac);
847       if (rs < 0) return e("bad huffman code","Corrupt JPEG");
848       s = rs & 15;
849       r = rs >> 4;
850       if (s == 0) {
851          if (rs != 0xf0) break; // end block
852          k += 16;
853       } else {
854          k += r;
855          // decode into unzigzag'd location
856          data[dezigzag[k++]] = (short) extend_receive(j,s);
857       }
858    } while (k < 64);
859    return 1;
860 }
861 
862 // take a -128..127 value and clamp it and convert to 0..255
clamp(int x)863 stbi_inline static uint8 clamp(int x)
864 {
865    // trick to use a single test to catch both cases
866    if ((unsigned int) x > 255) {
867       if (x < 0) return 0;
868       if (x > 255) return 255;
869    }
870    return (uint8) x;
871 }
872 
873 #define f2f(x)  (int) (((x) * 4096 + 0.5))
874 #define fsh(x)  ((x) << 12)
875 
876 // derived from jidctint -- DCT_ISLOW
877 #define IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7)       \
878    int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
879    p2 = s2;                                    \
880    p3 = s6;                                    \
881    p1 = (p2+p3) * f2f(0.5411961f);             \
882    t2 = p1 + p3*f2f(-1.847759065f);            \
883    t3 = p1 + p2*f2f( 0.765366865f);            \
884    p2 = s0;                                    \
885    p3 = s4;                                    \
886    t0 = fsh(p2+p3);                            \
887    t1 = fsh(p2-p3);                            \
888    x0 = t0+t3;                                 \
889    x3 = t0-t3;                                 \
890    x1 = t1+t2;                                 \
891    x2 = t1-t2;                                 \
892    t0 = s7;                                    \
893    t1 = s5;                                    \
894    t2 = s3;                                    \
895    t3 = s1;                                    \
896    p3 = t0+t2;                                 \
897    p4 = t1+t3;                                 \
898    p1 = t0+t3;                                 \
899    p2 = t1+t2;                                 \
900    p5 = (p3+p4)*f2f( 1.175875602f);            \
901    t0 = t0*f2f( 0.298631336f);                 \
902    t1 = t1*f2f( 2.053119869f);                 \
903    t2 = t2*f2f( 3.072711026f);                 \
904    t3 = t3*f2f( 1.501321110f);                 \
905    p1 = p5 + p1*f2f(-0.899976223f);            \
906    p2 = p5 + p2*f2f(-2.562915447f);            \
907    p3 = p3*f2f(-1.961570560f);                 \
908    p4 = p4*f2f(-0.390180644f);                 \
909    t3 += p1+p4;                                \
910    t2 += p2+p3;                                \
911    t1 += p2+p4;                                \
912    t0 += p1+p3;
913 
914 #ifdef STBI_SIMD
915 typedef unsigned short stbi_dequantize_t;
916 #else
917 typedef uint8 stbi_dequantize_t;
918 #endif
919 
920 // .344 seconds on 3*anemones.jpg
idct_block(uint8 * out,int out_stride,short data[64],stbi_dequantize_t * dequantize)921 static void idct_block(uint8 *out, int out_stride, short data[64], stbi_dequantize_t *dequantize)
922 {
923    int i,val[64],*v=val;
924    stbi_dequantize_t *dq = dequantize;
925    uint8 *o;
926    short *d = data;
927 
928    // columns
929    for (i=0; i < 8; ++i,++d,++dq, ++v) {
930       // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
931       if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
932            && d[40]==0 && d[48]==0 && d[56]==0) {
933          //    no shortcut                 0     seconds
934          //    (1|2|3|4|5|6|7)==0          0     seconds
935          //    all separate               -0.047 seconds
936          //    1 && 2|3 && 4|5 && 6|7:    -0.047 seconds
937          int dcterm = d[0] * dq[0] << 2;
938          v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
939       } else {
940          IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],
941                  d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])
942          // constants scaled things up by 1<<12; let's bring them back
943          // down, but keep 2 extra bits of precision
944          x0 += 512; x1 += 512; x2 += 512; x3 += 512;
945          v[ 0] = (x0+t3) >> 10;
946          v[56] = (x0-t3) >> 10;
947          v[ 8] = (x1+t2) >> 10;
948          v[48] = (x1-t2) >> 10;
949          v[16] = (x2+t1) >> 10;
950          v[40] = (x2-t1) >> 10;
951          v[24] = (x3+t0) >> 10;
952          v[32] = (x3-t0) >> 10;
953       }
954    }
955 
956    for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
957       // no fast case since the first 1D IDCT spread components out
958       IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
959       // constants scaled things up by 1<<12, plus we had 1<<2 from first
960       // loop, plus horizontal and vertical each scale by sqrt(8) so together
961       // we've got an extra 1<<3, so 1<<17 total we need to remove.
962       // so we want to round that, which means adding 0.5 * 1<<17,
963       // aka 65536. Also, we'll end up with -128 to 127 that we want
964       // to encode as 0..255 by adding 128, so we'll add that before the shift
965       x0 += 65536 + (128<<17);
966       x1 += 65536 + (128<<17);
967       x2 += 65536 + (128<<17);
968       x3 += 65536 + (128<<17);
969       // tried computing the shifts into temps, or'ing the temps to see
970       // if any were out of range, but that was slower
971       o[0] = clamp((x0+t3) >> 17);
972       o[7] = clamp((x0-t3) >> 17);
973       o[1] = clamp((x1+t2) >> 17);
974       o[6] = clamp((x1-t2) >> 17);
975       o[2] = clamp((x2+t1) >> 17);
976       o[5] = clamp((x2-t1) >> 17);
977       o[3] = clamp((x3+t0) >> 17);
978       o[4] = clamp((x3-t0) >> 17);
979    }
980 }
981 
982 #ifdef STBI_SIMD
983 static stbi_idct_8x8 stbi_idct_installed = idct_block;
984 
stbi_install_idct(stbi_idct_8x8 func)985 void stbi_install_idct(stbi_idct_8x8 func)
986 {
987    stbi_idct_installed = func;
988 }
989 #endif
990 
991 #define MARKER_none  0xff
992 // if there's a pending marker from the entropy stream, return that
993 // otherwise, fetch from the stream and get a marker. if there's no
994 // marker, return 0xff, which is never a valid marker value
get_marker(jpeg * j)995 static uint8 get_marker(jpeg *j)
996 {
997    uint8 x;
998    if (j->marker != MARKER_none) { x = j->marker; j->marker = MARKER_none; return x; }
999    x = get8u(j->s);
1000    if (x != 0xff) return MARKER_none;
1001    while (x == 0xff)
1002       x = get8u(j->s);
1003    return x;
1004 }
1005 
1006 // in each scan, we'll have scan_n components, and the order
1007 // of the components is specified by order[]
1008 #define RESTART(x)     ((x) >= 0xd0 && (x) <= 0xd7)
1009 
1010 // after a restart interval, reset the entropy decoder and
1011 // the dc prediction
reset(jpeg * j)1012 static void reset(jpeg *j)
1013 {
1014    j->code_bits = 0;
1015    j->code_buffer = 0;
1016    j->nomore = 0;
1017    j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
1018    j->marker = MARKER_none;
1019    j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
1020    // no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
1021    // since we don't even allow 1<<30 pixels
1022 }
1023 
parse_entropy_coded_data(jpeg * z)1024 static int parse_entropy_coded_data(jpeg *z)
1025 {
1026    reset(z);
1027    if (z->scan_n == 1) {
1028       int i,j;
1029       #ifdef STBI_SIMD
1030       __declspec(align(16))
1031       #endif
1032       short data[64];
1033       int n = z->order[0];
1034       // non-interleaved data, we just need to process one block at a time,
1035       // in trivial scanline order
1036       // number of blocks to do just depends on how many actual "pixels" this
1037       // component has, independent of interleaved MCU blocking and such
1038       int w = (z->img_comp[n].x+7) >> 3;
1039       int h = (z->img_comp[n].y+7) >> 3;
1040       for (j=0; j < h; ++j) {
1041          for (i=0; i < w; ++i) {
1042             if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
1043             #ifdef STBI_SIMD
1044             stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
1045             #else
1046             idct_block(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
1047             #endif
1048             // every data block is an MCU, so countdown the restart interval
1049             if (--z->todo <= 0) {
1050                if (z->code_bits < 24) grow_buffer_unsafe(z);
1051                // if it's NOT a restart, then just bail, so we get corrupt data
1052                // rather than no data
1053                if (!RESTART(z->marker)) return 1;
1054                reset(z);
1055             }
1056          }
1057       }
1058    } else { // interleaved!
1059       int i,j,k,x,y;
1060       short data[64];
1061       for (j=0; j < z->img_mcu_y; ++j) {
1062          for (i=0; i < z->img_mcu_x; ++i) {
1063             // scan an interleaved mcu... process scan_n components in order
1064             for (k=0; k < z->scan_n; ++k) {
1065                int n = z->order[k];
1066                // scan out an mcu's worth of this component; that's just determined
1067                // by the basic H and V specified for the component
1068                for (y=0; y < z->img_comp[n].v; ++y) {
1069                   for (x=0; x < z->img_comp[n].h; ++x) {
1070                      int x2 = (i*z->img_comp[n].h + x)*8;
1071                      int y2 = (j*z->img_comp[n].v + y)*8;
1072                      if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
1073                      #ifdef STBI_SIMD
1074                      stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
1075                      #else
1076                      idct_block(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
1077                      #endif
1078                   }
1079                }
1080             }
1081             // after all interleaved components, that's an interleaved MCU,
1082             // so now count down the restart interval
1083             if (--z->todo <= 0) {
1084                if (z->code_bits < 24) grow_buffer_unsafe(z);
1085                // if it's NOT a restart, then just bail, so we get corrupt data
1086                // rather than no data
1087                if (!RESTART(z->marker)) return 1;
1088                reset(z);
1089             }
1090          }
1091       }
1092    }
1093    return 1;
1094 }
1095 
process_marker(jpeg * z,int m)1096 static int process_marker(jpeg *z, int m)
1097 {
1098    int L;
1099    switch (m) {
1100       case MARKER_none: // no marker found
1101          return e("expected marker","Corrupt JPEG");
1102 
1103       case 0xC2: // SOF - progressive
1104          return e("progressive jpeg","JPEG format not supported (progressive)");
1105 
1106       case 0xDD: // DRI - specify restart interval
1107          if (get16(z->s) != 4) return e("bad DRI len","Corrupt JPEG");
1108          z->restart_interval = get16(z->s);
1109          return 1;
1110 
1111       case 0xDB: // DQT - define quantization table
1112          L = get16(z->s)-2;
1113          while (L > 0) {
1114             int q = get8(z->s);
1115             int p = q >> 4;
1116             int t = q & 15,i;
1117             if (p != 0) return e("bad DQT type","Corrupt JPEG");
1118             if (t > 3) return e("bad DQT table","Corrupt JPEG");
1119             for (i=0; i < 64; ++i)
1120                z->dequant[t][dezigzag[i]] = get8u(z->s);
1121             #ifdef STBI_SIMD
1122             for (i=0; i < 64; ++i)
1123                z->dequant2[t][i] = z->dequant[t][i];
1124             #endif
1125             L -= 65;
1126          }
1127          return L==0;
1128 
1129       case 0xC4: // DHT - define huffman table
1130          L = get16(z->s)-2;
1131          while (L > 0) {
1132             uint8 *v;
1133             int sizes[16],i,m=0;
1134             int q = get8(z->s);
1135             int tc = q >> 4;
1136             int th = q & 15;
1137             if (tc > 1 || th > 3) return e("bad DHT header","Corrupt JPEG");
1138             for (i=0; i < 16; ++i) {
1139                sizes[i] = get8(z->s);
1140                m += sizes[i];
1141             }
1142             L -= 17;
1143             if (tc == 0) {
1144                if (!build_huffman(z->huff_dc+th, sizes)) return 0;
1145                v = z->huff_dc[th].values;
1146             } else {
1147                if (!build_huffman(z->huff_ac+th, sizes)) return 0;
1148                v = z->huff_ac[th].values;
1149             }
1150             for (i=0; i < m; ++i)
1151                v[i] = get8u(z->s);
1152             L -= m;
1153          }
1154          return L==0;
1155    }
1156    // check for comment block or APP blocks
1157    if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
1158       skip(z->s, get16(z->s)-2);
1159       return 1;
1160    }
1161    return 0;
1162 }
1163 
1164 // after we see SOS
process_scan_header(jpeg * z)1165 static int process_scan_header(jpeg *z)
1166 {
1167    int i;
1168    int Ls = get16(z->s);
1169    z->scan_n = get8(z->s);
1170    if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return e("bad SOS component count","Corrupt JPEG");
1171    if (Ls != 6+2*z->scan_n) return e("bad SOS len","Corrupt JPEG");
1172    for (i=0; i < z->scan_n; ++i) {
1173       int id = get8(z->s), which;
1174       int q = get8(z->s);
1175       for (which = 0; which < z->s->img_n; ++which)
1176          if (z->img_comp[which].id == id)
1177             break;
1178       if (which == z->s->img_n) return 0;
1179       z->img_comp[which].hd = q >> 4;   if (z->img_comp[which].hd > 3) return e("bad DC huff","Corrupt JPEG");
1180       z->img_comp[which].ha = q & 15;   if (z->img_comp[which].ha > 3) return e("bad AC huff","Corrupt JPEG");
1181       z->order[i] = which;
1182    }
1183    if (get8(z->s) != 0) return e("bad SOS","Corrupt JPEG");
1184    get8(z->s); // should be 63, but might be 0
1185    if (get8(z->s) != 0) return e("bad SOS","Corrupt JPEG");
1186 
1187    return 1;
1188 }
1189 
process_frame_header(jpeg * z,int scan)1190 static int process_frame_header(jpeg *z, int scan)
1191 {
1192    stbi *s = z->s;
1193    int Lf,p,i,q, h_max=1,v_max=1,c;
1194    Lf = get16(s);         if (Lf < 11) return e("bad SOF len","Corrupt JPEG"); // JPEG
1195    p  = get8(s);          if (p != 8) return e("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
1196    s->img_y = get16(s);   if (s->img_y == 0) return e("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
1197    s->img_x = get16(s);   if (s->img_x == 0) return e("0 width","Corrupt JPEG"); // JPEG requires
1198    c = get8(s);
1199    if (c != 3 && c != 1) return e("bad component count","Corrupt JPEG");    // JFIF requires
1200    s->img_n = c;
1201    for (i=0; i < c; ++i) {
1202       z->img_comp[i].data = NULL;
1203       z->img_comp[i].linebuf = NULL;
1204    }
1205 
1206    if (Lf != 8+3*s->img_n) return e("bad SOF len","Corrupt JPEG");
1207 
1208    for (i=0; i < s->img_n; ++i) {
1209       z->img_comp[i].id = get8(s);
1210       if (z->img_comp[i].id != i+1)   // JFIF requires
1211          if (z->img_comp[i].id != i)  // some version of jpegtran outputs non-JFIF-compliant files!
1212             return e("bad component ID","Corrupt JPEG");
1213       q = get8(s);
1214       z->img_comp[i].h = (q >> 4);  if (!z->img_comp[i].h || z->img_comp[i].h > 4) return e("bad H","Corrupt JPEG");
1215       z->img_comp[i].v = q & 15;    if (!z->img_comp[i].v || z->img_comp[i].v > 4) return e("bad V","Corrupt JPEG");
1216       z->img_comp[i].tq = get8(s);  if (z->img_comp[i].tq > 3) return e("bad TQ","Corrupt JPEG");
1217    }
1218 
1219    if (scan != SCAN_load) return 1;
1220 
1221    if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
1222 
1223    for (i=0; i < s->img_n; ++i) {
1224       if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
1225       if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
1226    }
1227 
1228    // compute interleaved mcu info
1229    z->img_h_max = h_max;
1230    z->img_v_max = v_max;
1231    z->img_mcu_w = h_max * 8;
1232    z->img_mcu_h = v_max * 8;
1233    z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
1234    z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
1235 
1236    for (i=0; i < s->img_n; ++i) {
1237       // number of effective pixels (e.g. for non-interleaved MCU)
1238       z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
1239       z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
1240       // to simplify generation, we'll allocate enough memory to decode
1241       // the bogus oversized data from using interleaved MCUs and their
1242       // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't
1243       // discard the extra data until colorspace conversion
1244       z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
1245       z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
1246       z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
1247       if (z->img_comp[i].raw_data == NULL) {
1248          for(--i; i >= 0; --i) {
1249             free(z->img_comp[i].raw_data);
1250             z->img_comp[i].data = NULL;
1251          }
1252          return e("outofmem", "Out of memory");
1253       }
1254       // align blocks for installable-idct using mmx/sse
1255       z->img_comp[i].data = (uint8*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
1256       z->img_comp[i].linebuf = NULL;
1257    }
1258 
1259    return 1;
1260 }
1261 
1262 // use comparisons since in some cases we handle more than one case (e.g. SOF)
1263 #define DNL(x)         ((x) == 0xdc)
1264 #define SOI(x)         ((x) == 0xd8)
1265 #define EOI(x)         ((x) == 0xd9)
1266 #define SOF(x)         ((x) == 0xc0 || (x) == 0xc1)
1267 #define SOS(x)         ((x) == 0xda)
1268 
decode_jpeg_header(jpeg * z,int scan)1269 static int decode_jpeg_header(jpeg *z, int scan)
1270 {
1271    int m;
1272    z->marker = MARKER_none; // initialize cached marker to empty
1273    m = get_marker(z);
1274    if (!SOI(m)) return e("no SOI","Corrupt JPEG");
1275    if (scan == SCAN_type) return 1;
1276    m = get_marker(z);
1277    while (!SOF(m)) {
1278       if (!process_marker(z,m)) return 0;
1279       m = get_marker(z);
1280       while (m == MARKER_none) {
1281          // some files have extra padding after their blocks, so ok, we'll scan
1282          if (at_eof(z->s)) return e("no SOF", "Corrupt JPEG");
1283          m = get_marker(z);
1284       }
1285    }
1286    if (!process_frame_header(z, scan)) return 0;
1287    return 1;
1288 }
1289 
decode_jpeg_image(jpeg * j)1290 static int decode_jpeg_image(jpeg *j)
1291 {
1292    int m;
1293    j->restart_interval = 0;
1294    if (!decode_jpeg_header(j, SCAN_load)) return 0;
1295    m = get_marker(j);
1296    while (!EOI(m)) {
1297       if (SOS(m)) {
1298          if (!process_scan_header(j)) return 0;
1299          if (!parse_entropy_coded_data(j)) return 0;
1300          if (j->marker == MARKER_none ) {
1301             // handle 0s at the end of image data from IP Kamera 9060
1302             while (!at_eof(j->s)) {
1303                int x = get8(j->s);
1304                if (x == 255) {
1305                   j->marker = get8u(j->s);
1306                   break;
1307                } else if (x != 0) {
1308                   return 0;
1309                }
1310             }
1311             // if we reach eof without hitting a marker, get_marker() below will fail and we'll eventually return 0
1312          }
1313       } else {
1314          if (!process_marker(j, m)) return 0;
1315       }
1316       m = get_marker(j);
1317    }
1318    return 1;
1319 }
1320 
1321 // static jfif-centered resampling (across block boundaries)
1322 
1323 typedef uint8 *(*resample_row_func)(uint8 *out, uint8 *in0, uint8 *in1,
1324                                     int w, int hs);
1325 
1326 #define div4(x) ((uint8) ((x) >> 2))
1327 
resample_row_1(uint8 * out,uint8 * in_near,uint8 * in_far,int w,int hs)1328 static uint8 *resample_row_1(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
1329 {
1330    STBI_NOTUSED(out);
1331    STBI_NOTUSED(in_far);
1332    STBI_NOTUSED(w);
1333    STBI_NOTUSED(hs);
1334    return in_near;
1335 }
1336 
resample_row_v_2(uint8 * out,uint8 * in_near,uint8 * in_far,int w,int hs)1337 static uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
1338 {
1339    // need to generate two samples vertically for every one in input
1340    int i;
1341    STBI_NOTUSED(hs);
1342    for (i=0; i < w; ++i)
1343       out[i] = div4(3*in_near[i] + in_far[i] + 2);
1344    return out;
1345 }
1346 
resample_row_h_2(uint8 * out,uint8 * in_near,uint8 * in_far,int w,int hs)1347 static uint8*  resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
1348 {
1349    // need to generate two samples horizontally for every one in input
1350    int i;
1351    uint8 *input = in_near;
1352 
1353    if (w == 1) {
1354       // if only one sample, can't do any interpolation
1355       out[0] = out[1] = input[0];
1356       return out;
1357    }
1358 
1359    out[0] = input[0];
1360    out[1] = div4(input[0]*3 + input[1] + 2);
1361    for (i=1; i < w-1; ++i) {
1362       int n = 3*input[i]+2;
1363       out[i*2+0] = div4(n+input[i-1]);
1364       out[i*2+1] = div4(n+input[i+1]);
1365    }
1366    out[i*2+0] = div4(input[w-2]*3 + input[w-1] + 2);
1367    out[i*2+1] = input[w-1];
1368 
1369    STBI_NOTUSED(in_far);
1370    STBI_NOTUSED(hs);
1371 
1372    return out;
1373 }
1374 
1375 #define div16(x) ((uint8) ((x) >> 4))
1376 
resample_row_hv_2(uint8 * out,uint8 * in_near,uint8 * in_far,int w,int hs)1377 static uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
1378 {
1379    // need to generate 2x2 samples for every one in input
1380    int i,t0,t1;
1381    if (w == 1) {
1382       out[0] = out[1] = div4(3*in_near[0] + in_far[0] + 2);
1383       return out;
1384    }
1385 
1386    t1 = 3*in_near[0] + in_far[0];
1387    out[0] = div4(t1+2);
1388    for (i=1; i < w; ++i) {
1389       t0 = t1;
1390       t1 = 3*in_near[i]+in_far[i];
1391       out[i*2-1] = div16(3*t0 + t1 + 8);
1392       out[i*2  ] = div16(3*t1 + t0 + 8);
1393    }
1394    out[w*2-1] = div4(t1+2);
1395 
1396    STBI_NOTUSED(hs);
1397 
1398    return out;
1399 }
1400 
resample_row_generic(uint8 * out,uint8 * in_near,uint8 * in_far,int w,int hs)1401 static uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
1402 {
1403    // resample with nearest-neighbor
1404    int i,j;
1405    in_far = in_far;
1406    for (i=0; i < w; ++i)
1407       for (j=0; j < hs; ++j)
1408          out[i*hs+j] = in_near[i];
1409    return out;
1410 }
1411 
1412 #define float2fixed(x)  ((int) ((x) * 65536 + 0.5))
1413 
1414 // 0.38 seconds on 3*anemones.jpg   (0.25 with processor = Pro)
1415 // VC6 without processor=Pro is generating multiple LEAs per multiply!
YCbCr_to_RGB_row(uint8 * out,const uint8 * y,const uint8 * pcb,const uint8 * pcr,int count,int step)1416 static void YCbCr_to_RGB_row(uint8 *out, const uint8 *y, const uint8 *pcb, const uint8 *pcr, int count, int step)
1417 {
1418    int i;
1419    for (i=0; i < count; ++i) {
1420       int y_fixed = (y[i] << 16) + 32768; // rounding
1421       int r,g,b;
1422       int cr = pcr[i] - 128;
1423       int cb = pcb[i] - 128;
1424       r = y_fixed + cr*float2fixed(1.40200f);
1425       g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);
1426       b = y_fixed                            + cb*float2fixed(1.77200f);
1427       r >>= 16;
1428       g >>= 16;
1429       b >>= 16;
1430       if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
1431       if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
1432       if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
1433       out[0] = (uint8)r;
1434       out[1] = (uint8)g;
1435       out[2] = (uint8)b;
1436       out[3] = 255;
1437       out += step;
1438    }
1439 }
1440 
1441 #ifdef STBI_SIMD
1442 static stbi_YCbCr_to_RGB_run stbi_YCbCr_installed = YCbCr_to_RGB_row;
1443 
stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)1444 void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)
1445 {
1446    stbi_YCbCr_installed = func;
1447 }
1448 #endif
1449 
1450 
1451 // clean up the temporary component buffers
cleanup_jpeg(jpeg * j)1452 static void cleanup_jpeg(jpeg *j)
1453 {
1454    int i;
1455    for (i=0; i < j->s->img_n; ++i) {
1456       if (j->img_comp[i].data) {
1457          free(j->img_comp[i].raw_data);
1458          j->img_comp[i].data = NULL;
1459       }
1460       if (j->img_comp[i].linebuf) {
1461          free(j->img_comp[i].linebuf);
1462          j->img_comp[i].linebuf = NULL;
1463       }
1464    }
1465 }
1466 
1467 typedef struct
1468 {
1469    resample_row_func resample;
1470    uint8 *line0,*line1;
1471    int hs,vs;   // expansion factor in each axis
1472    int w_lores; // horizontal pixels pre-expansion
1473    int ystep;   // how far through vertical expansion we are
1474    int ypos;    // which pre-expansion row we're on
1475 } stbi_resample;
1476 
load_jpeg_image(jpeg * z,int * out_x,int * out_y,int * comp,int req_comp)1477 static uint8 *load_jpeg_image(jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
1478 {
1479    int n, decode_n;
1480    // validate req_comp
1481    if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
1482    z->s->img_n = 0;
1483 
1484    // load a jpeg image from whichever source
1485    if (!decode_jpeg_image(z)) { cleanup_jpeg(z); return NULL; }
1486 
1487    // determine actual number of components to generate
1488    n = req_comp ? req_comp : z->s->img_n;
1489 
1490    if (z->s->img_n == 3 && n < 3)
1491       decode_n = 1;
1492    else
1493       decode_n = z->s->img_n;
1494 
1495    // resample and color-convert
1496    {
1497       int k;
1498       uint i,j;
1499       uint8 *output;
1500       uint8 *coutput[4];
1501 
1502       stbi_resample res_comp[4];
1503 
1504       for (k=0; k < decode_n; ++k) {
1505          stbi_resample *r = &res_comp[k];
1506 
1507          // allocate line buffer big enough for upsampling off the edges
1508          // with upsample factor of 4
1509          z->img_comp[k].linebuf = (uint8 *) malloc(z->s->img_x + 3);
1510          if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
1511 
1512          r->hs      = z->img_h_max / z->img_comp[k].h;
1513          r->vs      = z->img_v_max / z->img_comp[k].v;
1514          r->ystep   = r->vs >> 1;
1515          r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
1516          r->ypos    = 0;
1517          r->line0   = r->line1 = z->img_comp[k].data;
1518 
1519          if      (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
1520          else if (r->hs == 1 && r->vs == 2) r->resample = resample_row_v_2;
1521          else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2;
1522          else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2;
1523          else                               r->resample = resample_row_generic;
1524       }
1525 
1526       // can't error after this so, this is safe
1527       output = (uint8 *) malloc(n * z->s->img_x * z->s->img_y + 1);
1528       if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
1529 
1530       // now go ahead and resample
1531       for (j=0; j < z->s->img_y; ++j) {
1532          uint8 *out = output + n * z->s->img_x * j;
1533          for (k=0; k < decode_n; ++k) {
1534             stbi_resample *r = &res_comp[k];
1535             int y_bot = r->ystep >= (r->vs >> 1);
1536             coutput[k] = r->resample(z->img_comp[k].linebuf,
1537                                      y_bot ? r->line1 : r->line0,
1538                                      y_bot ? r->line0 : r->line1,
1539                                      r->w_lores, r->hs);
1540             if (++r->ystep >= r->vs) {
1541                r->ystep = 0;
1542                r->line0 = r->line1;
1543                if (++r->ypos < z->img_comp[k].y)
1544                   r->line1 += z->img_comp[k].w2;
1545             }
1546          }
1547          if (n >= 3) {
1548             uint8 *y = coutput[0];
1549             if (z->s->img_n == 3) {
1550                #ifdef STBI_SIMD
1551                stbi_YCbCr_installed(out, y, coutput[1], coutput[2], z->s.img_x, n);
1552                #else
1553                YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s->img_x, n);
1554                #endif
1555             } else
1556                for (i=0; i < z->s->img_x; ++i) {
1557                   out[0] = out[1] = out[2] = y[i];
1558                   out[3] = 255; // not used if n==3
1559                   out += n;
1560                }
1561          } else {
1562             uint8 *y = coutput[0];
1563             if (n == 1)
1564                for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
1565             else
1566                for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
1567          }
1568       }
1569       cleanup_jpeg(z);
1570       *out_x = z->s->img_x;
1571       *out_y = z->s->img_y;
1572       if (comp) *comp  = z->s->img_n; // report original components, not output
1573       return output;
1574    }
1575 }
1576 
stbi_jpeg_load(stbi * s,int * x,int * y,int * comp,int req_comp)1577 static unsigned char *stbi_jpeg_load(stbi *s, int *x, int *y, int *comp, int req_comp)
1578 {
1579    jpeg j;
1580    j.s = s;
1581    return load_jpeg_image(&j, x,y,comp,req_comp);
1582 }
1583 
stbi_jpeg_test(stbi * s)1584 static int stbi_jpeg_test(stbi *s)
1585 {
1586    int r;
1587    jpeg j;
1588    j.s = s;
1589    r = decode_jpeg_header(&j, SCAN_type);
1590    stbi_rewind(s);
1591    return r;
1592 }
1593 
stbi_jpeg_info_raw(jpeg * j,int * x,int * y,int * comp)1594 static int stbi_jpeg_info_raw(jpeg *j, int *x, int *y, int *comp)
1595 {
1596    if (!decode_jpeg_header(j, SCAN_header)) {
1597       stbi_rewind( j->s );
1598       return 0;
1599    }
1600    if (x) *x = j->s->img_x;
1601    if (y) *y = j->s->img_y;
1602    if (comp) *comp = j->s->img_n;
1603    return 1;
1604 }
1605 
stbi_jpeg_info(stbi * s,int * x,int * y,int * comp)1606 static int stbi_jpeg_info(stbi *s, int *x, int *y, int *comp)
1607 {
1608    jpeg j;
1609    j.s = s;
1610    return stbi_jpeg_info_raw(&j, x, y, comp);
1611 }
1612 
1613 // public domain zlib decode    v0.2  Sean Barrett 2006-11-18
1614 //    simple implementation
1615 //      - all input must be provided in an upfront buffer
1616 //      - all output is written to a single output buffer (can malloc/realloc)
1617 //    performance
1618 //      - fast huffman
1619 
1620 // fast-way is faster to check than jpeg huffman, but slow way is slower
1621 #define ZFAST_BITS  9 // accelerate all cases in default tables
1622 #define ZFAST_MASK  ((1 << ZFAST_BITS) - 1)
1623 
1624 // zlib-style huffman encoding
1625 // (jpegs packs from left, zlib from right, so can't share code)
1626 typedef struct
1627 {
1628    uint16 fast[1 << ZFAST_BITS];
1629    uint16 firstcode[16];
1630    int maxcode[17];
1631    uint16 firstsymbol[16];
1632    uint8  size[288];
1633    uint16 value[288];
1634 } zhuffman;
1635 
bitreverse16(int n)1636 stbi_inline static int bitreverse16(int n)
1637 {
1638   n = ((n & 0xAAAA) >>  1) | ((n & 0x5555) << 1);
1639   n = ((n & 0xCCCC) >>  2) | ((n & 0x3333) << 2);
1640   n = ((n & 0xF0F0) >>  4) | ((n & 0x0F0F) << 4);
1641   n = ((n & 0xFF00) >>  8) | ((n & 0x00FF) << 8);
1642   return n;
1643 }
1644 
bit_reverse(int v,int bits)1645 stbi_inline static int bit_reverse(int v, int bits)
1646 {
1647    assert(bits <= 16);
1648    // to bit reverse n bits, reverse 16 and shift
1649    // e.g. 11 bits, bit reverse and shift away 5
1650    return bitreverse16(v) >> (16-bits);
1651 }
1652 
zbuild_huffman(zhuffman * z,uint8 * sizelist,int num)1653 static int zbuild_huffman(zhuffman *z, uint8 *sizelist, int num)
1654 {
1655    int i,k=0;
1656    int code, next_code[16], sizes[17];
1657 
1658    // DEFLATE spec for generating codes
1659    memset(sizes, 0, sizeof(sizes));
1660    memset(z->fast, 255, sizeof(z->fast));
1661    for (i=0; i < num; ++i)
1662       ++sizes[sizelist[i]];
1663    sizes[0] = 0;
1664    for (i=1; i < 16; ++i)
1665       assert(sizes[i] <= (1 << i));
1666    code = 0;
1667    for (i=1; i < 16; ++i) {
1668       next_code[i] = code;
1669       z->firstcode[i] = (uint16) code;
1670       z->firstsymbol[i] = (uint16) k;
1671       code = (code + sizes[i]);
1672       if (sizes[i])
1673          if (code-1 >= (1 << i)) return e("bad codelengths","Corrupt JPEG");
1674       z->maxcode[i] = code << (16-i); // preshift for inner loop
1675       code <<= 1;
1676       k += sizes[i];
1677    }
1678    z->maxcode[16] = 0x10000; // sentinel
1679    for (i=0; i < num; ++i) {
1680       int s = sizelist[i];
1681       if (s) {
1682          int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
1683          z->size[c] = (uint8)s;
1684          z->value[c] = (uint16)i;
1685          if (s <= ZFAST_BITS) {
1686             int k = bit_reverse(next_code[s],s);
1687             while (k < (1 << ZFAST_BITS)) {
1688                z->fast[k] = (uint16) c;
1689                k += (1 << s);
1690             }
1691          }
1692          ++next_code[s];
1693       }
1694    }
1695    return 1;
1696 }
1697 
1698 // zlib-from-memory implementation for PNG reading
1699 //    because PNG allows splitting the zlib stream arbitrarily,
1700 //    and it's annoying structurally to have PNG call ZLIB call PNG,
1701 //    we require PNG read all the IDATs and combine them into a single
1702 //    memory buffer
1703 
1704 typedef struct
1705 {
1706    uint8 *zbuffer, *zbuffer_end;
1707    int num_bits;
1708    uint32 code_buffer;
1709 
1710    char *zout;
1711    char *zout_start;
1712    char *zout_end;
1713    int   z_expandable;
1714 
1715    zhuffman z_length, z_distance;
1716 } zbuf;
1717 
zget8(zbuf * z)1718 stbi_inline static int zget8(zbuf *z)
1719 {
1720    if (z->zbuffer >= z->zbuffer_end) return 0;
1721    return *z->zbuffer++;
1722 }
1723 
fill_bits(zbuf * z)1724 static void fill_bits(zbuf *z)
1725 {
1726    do {
1727       assert(z->code_buffer < (1U << z->num_bits));
1728       z->code_buffer |= zget8(z) << z->num_bits;
1729       z->num_bits += 8;
1730    } while (z->num_bits <= 24);
1731 }
1732 
zreceive(zbuf * z,int n)1733 stbi_inline static unsigned int zreceive(zbuf *z, int n)
1734 {
1735    unsigned int k;
1736    if (z->num_bits < n) fill_bits(z);
1737    k = z->code_buffer & ((1 << n) - 1);
1738    z->code_buffer >>= n;
1739    z->num_bits -= n;
1740    return k;
1741 }
1742 
zhuffman_decode(zbuf * a,zhuffman * z)1743 stbi_inline static int zhuffman_decode(zbuf *a, zhuffman *z)
1744 {
1745    int b,s,k;
1746    if (a->num_bits < 16) fill_bits(a);
1747    b = z->fast[a->code_buffer & ZFAST_MASK];
1748    if (b < 0xffff) {
1749       s = z->size[b];
1750       a->code_buffer >>= s;
1751       a->num_bits -= s;
1752       return z->value[b];
1753    }
1754 
1755    // not resolved by fast table, so compute it the slow way
1756    // use jpeg approach, which requires MSbits at top
1757    k = bit_reverse(a->code_buffer, 16);
1758    for (s=ZFAST_BITS+1; ; ++s)
1759       if (k < z->maxcode[s])
1760          break;
1761    if (s == 16) return -1; // invalid code!
1762    // code size is s, so:
1763    b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
1764    assert(z->size[b] == s);
1765    a->code_buffer >>= s;
1766    a->num_bits -= s;
1767    return z->value[b];
1768 }
1769 
expand(zbuf * z,int n)1770 static int expand(zbuf *z, int n)  // need to make room for n bytes
1771 {
1772    char *q;
1773    int cur, limit;
1774    if (!z->z_expandable) return e("output buffer limit","Corrupt PNG");
1775    cur   = (int) (z->zout     - z->zout_start);
1776    limit = (int) (z->zout_end - z->zout_start);
1777    while (cur + n > limit)
1778       limit *= 2;
1779    q = (char *) realloc(z->zout_start, limit);
1780    if (q == NULL) return e("outofmem", "Out of memory");
1781    z->zout_start = q;
1782    z->zout       = q + cur;
1783    z->zout_end   = q + limit;
1784    return 1;
1785 }
1786 
1787 static int length_base[31] = {
1788    3,4,5,6,7,8,9,10,11,13,
1789    15,17,19,23,27,31,35,43,51,59,
1790    67,83,99,115,131,163,195,227,258,0,0 };
1791 
1792 static int length_extra[31]=
1793 { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
1794 
1795 static int dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
1796 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
1797 
1798 static int dist_extra[32] =
1799 { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
1800 
parse_huffman_block(zbuf * a)1801 static int parse_huffman_block(zbuf *a)
1802 {
1803    for(;;) {
1804       int z = zhuffman_decode(a, &a->z_length);
1805       if (z < 256) {
1806          if (z < 0) return e("bad huffman code","Corrupt PNG"); // error in huffman codes
1807          if (a->zout >= a->zout_end) if (!expand(a, 1)) return 0;
1808          *a->zout++ = (char) z;
1809       } else {
1810          uint8 *p;
1811          int len,dist;
1812          if (z == 256) return 1;
1813          z -= 257;
1814          len = length_base[z];
1815          if (length_extra[z]) len += zreceive(a, length_extra[z]);
1816          z = zhuffman_decode(a, &a->z_distance);
1817          if (z < 0) return e("bad huffman code","Corrupt PNG");
1818          dist = dist_base[z];
1819          if (dist_extra[z]) dist += zreceive(a, dist_extra[z]);
1820          if (a->zout - a->zout_start < dist) return e("bad dist","Corrupt PNG");
1821          if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0;
1822          p = (uint8 *) (a->zout - dist);
1823          while (len--)
1824             *a->zout++ = *p++;
1825       }
1826    }
1827 }
1828 
compute_huffman_codes(zbuf * a)1829 static int compute_huffman_codes(zbuf *a)
1830 {
1831    static uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
1832    zhuffman z_codelength;
1833    uint8 lencodes[286+32+137];//padding for maximum single op
1834    uint8 codelength_sizes[19];
1835    int i,n;
1836 
1837    int hlit  = zreceive(a,5) + 257;
1838    int hdist = zreceive(a,5) + 1;
1839    int hclen = zreceive(a,4) + 4;
1840 
1841    memset(codelength_sizes, 0, sizeof(codelength_sizes));
1842    for (i=0; i < hclen; ++i) {
1843       int s = zreceive(a,3);
1844       codelength_sizes[length_dezigzag[i]] = (uint8) s;
1845    }
1846    if (!zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
1847 
1848    n = 0;
1849    while (n < hlit + hdist) {
1850       int c = zhuffman_decode(a, &z_codelength);
1851       assert(c >= 0 && c < 19);
1852       if (c < 16)
1853          lencodes[n++] = (uint8) c;
1854       else if (c == 16) {
1855          c = zreceive(a,2)+3;
1856          memset(lencodes+n, lencodes[n-1], c);
1857          n += c;
1858       } else if (c == 17) {
1859          c = zreceive(a,3)+3;
1860          memset(lencodes+n, 0, c);
1861          n += c;
1862       } else {
1863          assert(c == 18);
1864          c = zreceive(a,7)+11;
1865          memset(lencodes+n, 0, c);
1866          n += c;
1867       }
1868    }
1869    if (n != hlit+hdist) return e("bad codelengths","Corrupt PNG");
1870    if (!zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
1871    if (!zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
1872    return 1;
1873 }
1874 
parse_uncompressed_block(zbuf * a)1875 static int parse_uncompressed_block(zbuf *a)
1876 {
1877    uint8 header[4];
1878    int len,nlen,k;
1879    if (a->num_bits & 7)
1880       zreceive(a, a->num_bits & 7); // discard
1881    // drain the bit-packed data into header
1882    k = 0;
1883    while (a->num_bits > 0) {
1884       header[k++] = (uint8) (a->code_buffer & 255); // wtf this warns?
1885       a->code_buffer >>= 8;
1886       a->num_bits -= 8;
1887    }
1888    assert(a->num_bits == 0);
1889    // now fill header the normal way
1890    while (k < 4)
1891       header[k++] = (uint8) zget8(a);
1892    len  = header[1] * 256 + header[0];
1893    nlen = header[3] * 256 + header[2];
1894    if (nlen != (len ^ 0xffff)) return e("zlib corrupt","Corrupt PNG");
1895    if (a->zbuffer + len > a->zbuffer_end) return e("read past buffer","Corrupt PNG");
1896    if (a->zout + len > a->zout_end)
1897       if (!expand(a, len)) return 0;
1898    memcpy(a->zout, a->zbuffer, len);
1899    a->zbuffer += len;
1900    a->zout += len;
1901    return 1;
1902 }
1903 
parse_zlib_header(zbuf * a)1904 static int parse_zlib_header(zbuf *a)
1905 {
1906    int cmf   = zget8(a);
1907    int cm    = cmf & 15;
1908    /* int cinfo = cmf >> 4; */
1909    int flg   = zget8(a);
1910    if ((cmf*256+flg) % 31 != 0) return e("bad zlib header","Corrupt PNG"); // zlib spec
1911    if (flg & 32) return e("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
1912    if (cm != 8) return e("bad compression","Corrupt PNG"); // DEFLATE required for png
1913    // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
1914    return 1;
1915 }
1916 
1917 // @TODO: should statically initialize these for optimal thread safety
1918 static uint8 default_length[288], default_distance[32];
init_defaults(void)1919 static void init_defaults(void)
1920 {
1921    int i;   // use <= to match clearly with spec
1922    for (i=0; i <= 143; ++i)     default_length[i]   = 8;
1923    for (   ; i <= 255; ++i)     default_length[i]   = 9;
1924    for (   ; i <= 279; ++i)     default_length[i]   = 7;
1925    for (   ; i <= 287; ++i)     default_length[i]   = 8;
1926 
1927    for (i=0; i <=  31; ++i)     default_distance[i] = 5;
1928 }
1929 
1930 int stbi_png_partial; // a quick hack to only allow decoding some of a PNG... I should implement real streaming support instead
parse_zlib(zbuf * a,int parse_header)1931 static int parse_zlib(zbuf *a, int parse_header)
1932 {
1933    int final, type;
1934    if (parse_header)
1935       if (!parse_zlib_header(a)) return 0;
1936    a->num_bits = 0;
1937    a->code_buffer = 0;
1938    do {
1939       final = zreceive(a,1);
1940       type = zreceive(a,2);
1941       if (type == 0) {
1942          if (!parse_uncompressed_block(a)) return 0;
1943       } else if (type == 3) {
1944          return 0;
1945       } else {
1946          if (type == 1) {
1947             // use fixed code lengths
1948             if (!default_distance[31]) init_defaults();
1949             if (!zbuild_huffman(&a->z_length  , default_length  , 288)) return 0;
1950             if (!zbuild_huffman(&a->z_distance, default_distance,  32)) return 0;
1951          } else {
1952             if (!compute_huffman_codes(a)) return 0;
1953          }
1954          if (!parse_huffman_block(a)) return 0;
1955       }
1956       if (stbi_png_partial && a->zout - a->zout_start > 65536)
1957          break;
1958    } while (!final);
1959    return 1;
1960 }
1961 
do_zlib(zbuf * a,char * obuf,int olen,int exp,int parse_header)1962 static int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header)
1963 {
1964    a->zout_start = obuf;
1965    a->zout       = obuf;
1966    a->zout_end   = obuf + olen;
1967    a->z_expandable = exp;
1968 
1969    return parse_zlib(a, parse_header);
1970 }
1971 
stbi_zlib_decode_malloc_guesssize(const char * buffer,int len,int initial_size,int * outlen)1972 char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
1973 {
1974    zbuf a;
1975    char *p = (char *) malloc(initial_size);
1976    if (p == NULL) return NULL;
1977    a.zbuffer = (uint8 *) buffer;
1978    a.zbuffer_end = (uint8 *) buffer + len;
1979    if (do_zlib(&a, p, initial_size, 1, 1)) {
1980       if (outlen) *outlen = (int) (a.zout - a.zout_start);
1981       return a.zout_start;
1982    } else {
1983       free(a.zout_start);
1984       return NULL;
1985    }
1986 }
1987 
stbi_zlib_decode_malloc(char const * buffer,int len,int * outlen)1988 char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
1989 {
1990    return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
1991 }
1992 
stbi_zlib_decode_malloc_guesssize_headerflag(const char * buffer,int len,int initial_size,int * outlen,int parse_header)1993 char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
1994 {
1995    zbuf a;
1996    char *p = (char *) malloc(initial_size);
1997    if (p == NULL) return NULL;
1998    a.zbuffer = (uint8 *) buffer;
1999    a.zbuffer_end = (uint8 *) buffer + len;
2000    if (do_zlib(&a, p, initial_size, 1, parse_header)) {
2001       if (outlen) *outlen = (int) (a.zout - a.zout_start);
2002       return a.zout_start;
2003    } else {
2004       free(a.zout_start);
2005       return NULL;
2006    }
2007 }
2008 
stbi_zlib_decode_buffer(char * obuffer,int olen,char const * ibuffer,int ilen)2009 int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
2010 {
2011    zbuf a;
2012    a.zbuffer = (uint8 *) ibuffer;
2013    a.zbuffer_end = (uint8 *) ibuffer + ilen;
2014    if (do_zlib(&a, obuffer, olen, 0, 1))
2015       return (int) (a.zout - a.zout_start);
2016    else
2017       return -1;
2018 }
2019 
stbi_zlib_decode_noheader_malloc(char const * buffer,int len,int * outlen)2020 char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
2021 {
2022    zbuf a;
2023    char *p = (char *) malloc(16384);
2024    if (p == NULL) return NULL;
2025    a.zbuffer = (uint8 *) buffer;
2026    a.zbuffer_end = (uint8 *) buffer+len;
2027    if (do_zlib(&a, p, 16384, 1, 0)) {
2028       if (outlen) *outlen = (int) (a.zout - a.zout_start);
2029       return a.zout_start;
2030    } else {
2031       free(a.zout_start);
2032       return NULL;
2033    }
2034 }
2035 
stbi_zlib_decode_noheader_buffer(char * obuffer,int olen,const char * ibuffer,int ilen)2036 int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
2037 {
2038    zbuf a;
2039    a.zbuffer = (uint8 *) ibuffer;
2040    a.zbuffer_end = (uint8 *) ibuffer + ilen;
2041    if (do_zlib(&a, obuffer, olen, 0, 0))
2042       return (int) (a.zout - a.zout_start);
2043    else
2044       return -1;
2045 }
2046 
2047 // public domain "baseline" PNG decoder   v0.10  Sean Barrett 2006-11-18
2048 //    simple implementation
2049 //      - only 8-bit samples
2050 //      - no CRC checking
2051 //      - allocates lots of intermediate memory
2052 //        - avoids problem of streaming data between subsystems
2053 //        - avoids explicit window management
2054 //    performance
2055 //      - uses stb_zlib, a PD zlib implementation with fast huffman decoding
2056 
2057 
2058 typedef struct
2059 {
2060    uint32 length;
2061    uint32 type;
2062 } chunk;
2063 
2064 #define PNG_TYPE(a,b,c,d)  (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
2065 
get_chunk_header(stbi * s)2066 static chunk get_chunk_header(stbi *s)
2067 {
2068    chunk c;
2069    c.length = get32(s);
2070    c.type   = get32(s);
2071    return c;
2072 }
2073 
check_png_header(stbi * s)2074 static int check_png_header(stbi *s)
2075 {
2076    static uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 };
2077    int i;
2078    for (i=0; i < 8; ++i)
2079       if (get8u(s) != png_sig[i]) return e("bad png sig","Not a PNG");
2080    return 1;
2081 }
2082 
2083 typedef struct
2084 {
2085    stbi *s;
2086    uint8 *idata, *expanded, *out;
2087 } png;
2088 
2089 
2090 enum {
2091    F_none=0, F_sub=1, F_up=2, F_avg=3, F_paeth=4,
2092    F_avg_first, F_paeth_first
2093 };
2094 
2095 static uint8 first_row_filter[5] =
2096 {
2097    F_none, F_sub, F_none, F_avg_first, F_paeth_first
2098 };
2099 
paeth(int a,int b,int c)2100 static int paeth(int a, int b, int c)
2101 {
2102    int p = a + b - c;
2103    int pa = abs(p-a);
2104    int pb = abs(p-b);
2105    int pc = abs(p-c);
2106    if (pa <= pb && pa <= pc) return a;
2107    if (pb <= pc) return b;
2108    return c;
2109 }
2110 
2111 // create the png data from post-deflated data
create_png_image_raw(png * a,uint8 * raw,uint32 raw_len,int out_n,uint32 x,uint32 y)2112 static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, uint32 x, uint32 y)
2113 {
2114    stbi *s = a->s;
2115    uint32 i,j,stride = x*out_n;
2116    int k;
2117    int img_n = s->img_n; // copy it into a local for later
2118    assert(out_n == s->img_n || out_n == s->img_n+1);
2119    if (stbi_png_partial) y = 1;
2120    a->out = (uint8 *) malloc(x * y * out_n);
2121    if (!a->out) return e("outofmem", "Out of memory");
2122    if (!stbi_png_partial) {
2123       if (s->img_x == x && s->img_y == y) {
2124          if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
2125       } else { // interlaced:
2126          if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
2127       }
2128    }
2129    for (j=0; j < y; ++j) {
2130       uint8 *cur = a->out + stride*j;
2131       uint8 *prior = cur - stride;
2132       int filter = *raw++;
2133       if (filter > 4) return e("invalid filter","Corrupt PNG");
2134       // if first row, use special filter that doesn't sample previous row
2135       if (j == 0) filter = first_row_filter[filter];
2136       // handle first pixel explicitly
2137       for (k=0; k < img_n; ++k) {
2138          switch (filter) {
2139             case F_none       : cur[k] = raw[k]; break;
2140             case F_sub        : cur[k] = raw[k]; break;
2141             case F_up         : cur[k] = raw[k] + prior[k]; break;
2142             case F_avg        : cur[k] = raw[k] + (prior[k]>>1); break;
2143             case F_paeth      : cur[k] = (uint8) (raw[k] + paeth(0,prior[k],0)); break;
2144             case F_avg_first  : cur[k] = raw[k]; break;
2145             case F_paeth_first: cur[k] = raw[k]; break;
2146          }
2147       }
2148       if (img_n != out_n) cur[img_n] = 255;
2149       raw += img_n;
2150       cur += out_n;
2151       prior += out_n;
2152       // this is a little gross, so that we don't switch per-pixel or per-component
2153       if (img_n == out_n) {
2154          #define CASE(f) \
2155              case f:     \
2156                 for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \
2157                    for (k=0; k < img_n; ++k)
2158          switch (filter) {
2159             CASE(F_none)  cur[k] = raw[k]; break;
2160             CASE(F_sub)   cur[k] = raw[k] + cur[k-img_n]; break;
2161             CASE(F_up)    cur[k] = raw[k] + prior[k]; break;
2162             CASE(F_avg)   cur[k] = raw[k] + ((prior[k] + cur[k-img_n])>>1); break;
2163             CASE(F_paeth)  cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;
2164             CASE(F_avg_first)    cur[k] = raw[k] + (cur[k-img_n] >> 1); break;
2165             CASE(F_paeth_first)  cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break;
2166          }
2167          #undef CASE
2168       } else {
2169          assert(img_n+1 == out_n);
2170          #define CASE(f) \
2171              case f:     \
2172                 for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
2173                    for (k=0; k < img_n; ++k)
2174          switch (filter) {
2175             CASE(F_none)  cur[k] = raw[k]; break;
2176             CASE(F_sub)   cur[k] = raw[k] + cur[k-out_n]; break;
2177             CASE(F_up)    cur[k] = raw[k] + prior[k]; break;
2178             CASE(F_avg)   cur[k] = raw[k] + ((prior[k] + cur[k-out_n])>>1); break;
2179             CASE(F_paeth)  cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
2180             CASE(F_avg_first)    cur[k] = raw[k] + (cur[k-out_n] >> 1); break;
2181             CASE(F_paeth_first)  cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break;
2182          }
2183          #undef CASE
2184       }
2185    }
2186    return 1;
2187 }
2188 
create_png_image(png * a,uint8 * raw,uint32 raw_len,int out_n,int interlaced)2189 static int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n, int interlaced)
2190 {
2191    uint8 *final;
2192    int p;
2193    int save;
2194    if (!interlaced)
2195       return create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y);
2196    save = stbi_png_partial;
2197    stbi_png_partial = 0;
2198 
2199    // de-interlacing
2200    final = (uint8 *) malloc(a->s->img_x * a->s->img_y * out_n);
2201    for (p=0; p < 7; ++p) {
2202       int xorig[] = { 0,4,0,2,0,1,0 };
2203       int yorig[] = { 0,0,4,0,2,0,1 };
2204       int xspc[]  = { 8,8,4,4,2,2,1 };
2205       int yspc[]  = { 8,8,8,4,4,2,2 };
2206       int i,j,x,y;
2207       // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
2208       x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
2209       y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
2210       if (x && y) {
2211          if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
2212             free(final);
2213             return 0;
2214          }
2215          for (j=0; j < y; ++j)
2216             for (i=0; i < x; ++i)
2217                memcpy(final + (j*yspc[p]+yorig[p])*a->s->img_x*out_n + (i*xspc[p]+xorig[p])*out_n,
2218                       a->out + (j*x+i)*out_n, out_n);
2219          free(a->out);
2220          raw += (x*out_n+1)*y;
2221          raw_len -= (x*out_n+1)*y;
2222       }
2223    }
2224    a->out = final;
2225 
2226    stbi_png_partial = save;
2227    return 1;
2228 }
2229 
compute_transparency(png * z,uint8 tc[3],int out_n)2230 static int compute_transparency(png *z, uint8 tc[3], int out_n)
2231 {
2232    stbi *s = z->s;
2233    uint32 i, pixel_count = s->img_x * s->img_y;
2234    uint8 *p = z->out;
2235 
2236    // compute color-based transparency, assuming we've
2237    // already got 255 as the alpha value in the output
2238    assert(out_n == 2 || out_n == 4);
2239 
2240    if (out_n == 2) {
2241       for (i=0; i < pixel_count; ++i) {
2242          p[1] = (p[0] == tc[0] ? 0 : 255);
2243          p += 2;
2244       }
2245    } else {
2246       for (i=0; i < pixel_count; ++i) {
2247          if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
2248             p[3] = 0;
2249          p += 4;
2250       }
2251    }
2252    return 1;
2253 }
2254 
expand_palette(png * a,uint8 * palette,int len,int pal_img_n)2255 static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n)
2256 {
2257    uint32 i, pixel_count = a->s->img_x * a->s->img_y;
2258    uint8 *p, *temp_out, *orig = a->out;
2259 
2260    p = (uint8 *) malloc(pixel_count * pal_img_n);
2261    if (p == NULL) return e("outofmem", "Out of memory");
2262 
2263    // between here and free(out) below, exitting would leak
2264    temp_out = p;
2265 
2266    if (pal_img_n == 3) {
2267       for (i=0; i < pixel_count; ++i) {
2268          int n = orig[i]*4;
2269          p[0] = palette[n  ];
2270          p[1] = palette[n+1];
2271          p[2] = palette[n+2];
2272          p += 3;
2273       }
2274    } else {
2275       for (i=0; i < pixel_count; ++i) {
2276          int n = orig[i]*4;
2277          p[0] = palette[n  ];
2278          p[1] = palette[n+1];
2279          p[2] = palette[n+2];
2280          p[3] = palette[n+3];
2281          p += 4;
2282       }
2283    }
2284    free(a->out);
2285    a->out = temp_out;
2286 
2287    STBI_NOTUSED(len);
2288 
2289    return 1;
2290 }
2291 
2292 static int stbi_unpremultiply_on_load = 0;
2293 static int stbi_de_iphone_flag = 0;
2294 
stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)2295 void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
2296 {
2297    stbi_unpremultiply_on_load = flag_true_if_should_unpremultiply;
2298 }
stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)2299 void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
2300 {
2301    stbi_de_iphone_flag = flag_true_if_should_convert;
2302 }
2303 
stbi_de_iphone(png * z)2304 static void stbi_de_iphone(png *z)
2305 {
2306    stbi *s = z->s;
2307    uint32 i, pixel_count = s->img_x * s->img_y;
2308    uint8 *p = z->out;
2309 
2310    if (s->img_out_n == 3) {  // convert bgr to rgb
2311       for (i=0; i < pixel_count; ++i) {
2312          uint8 t = p[0];
2313          p[0] = p[2];
2314          p[2] = t;
2315          p += 3;
2316       }
2317    } else {
2318       assert(s->img_out_n == 4);
2319       if (stbi_unpremultiply_on_load) {
2320          // convert bgr to rgb and unpremultiply
2321          for (i=0; i < pixel_count; ++i) {
2322             uint8 a = p[3];
2323             uint8 t = p[0];
2324             if (a) {
2325                p[0] = p[2] * 255 / a;
2326                p[1] = p[1] * 255 / a;
2327                p[2] =  t   * 255 / a;
2328             } else {
2329                p[0] = p[2];
2330                p[2] = t;
2331             }
2332             p += 4;
2333          }
2334       } else {
2335          // convert bgr to rgb
2336          for (i=0; i < pixel_count; ++i) {
2337             uint8 t = p[0];
2338             p[0] = p[2];
2339             p[2] = t;
2340             p += 4;
2341          }
2342       }
2343    }
2344 }
2345 
parse_png_file(png * z,int scan,int req_comp)2346 static int parse_png_file(png *z, int scan, int req_comp)
2347 {
2348    uint8 palette[1024], pal_img_n=0;
2349    uint8 has_trans=0, tc[3];
2350    uint32 ioff=0, idata_limit=0, i, pal_len=0;
2351    int first=1,k,interlace=0, iphone=0;
2352    stbi *s = z->s;
2353 
2354    z->expanded = NULL;
2355    z->idata = NULL;
2356    z->out = NULL;
2357 
2358    if (!check_png_header(s)) return 0;
2359 
2360    if (scan == SCAN_type) return 1;
2361 
2362    for (;;) {
2363       chunk c = get_chunk_header(s);
2364       switch (c.type) {
2365          case PNG_TYPE('C','g','B','I'):
2366             iphone = stbi_de_iphone_flag;
2367             skip(s, c.length);
2368             break;
2369          case PNG_TYPE('I','H','D','R'): {
2370             int depth,color,comp,filter;
2371             if (!first) return e("multiple IHDR","Corrupt PNG");
2372             first = 0;
2373             if (c.length != 13) return e("bad IHDR len","Corrupt PNG");
2374             s->img_x = get32(s); if (s->img_x > (1 << 24)) return e("too large","Very large image (corrupt?)");
2375             s->img_y = get32(s); if (s->img_y > (1 << 24)) return e("too large","Very large image (corrupt?)");
2376             depth = get8(s);  if (depth != 8)        return e("8bit only","PNG not supported: 8-bit only");
2377             color = get8(s);  if (color > 6)         return e("bad ctype","Corrupt PNG");
2378             if (color == 3) pal_img_n = 3; else if (color & 1) return e("bad ctype","Corrupt PNG");
2379             comp  = get8(s);  if (comp) return e("bad comp method","Corrupt PNG");
2380             filter= get8(s);  if (filter) return e("bad filter method","Corrupt PNG");
2381             interlace = get8(s); if (interlace>1) return e("bad interlace method","Corrupt PNG");
2382             if (!s->img_x || !s->img_y) return e("0-pixel image","Corrupt PNG");
2383             if (!pal_img_n) {
2384                s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
2385                if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
2386                if (scan == SCAN_header) return 1;
2387             } else {
2388                // if paletted, then pal_n is our final components, and
2389                // img_n is # components to decompress/filter.
2390                s->img_n = 1;
2391                if ((1 << 30) / s->img_x / 4 < s->img_y) return e("too large","Corrupt PNG");
2392                // if SCAN_header, have to scan to see if we have a tRNS
2393             }
2394             break;
2395          }
2396 
2397          case PNG_TYPE('P','L','T','E'):  {
2398             if (first) return e("first not IHDR", "Corrupt PNG");
2399             if (c.length > 256*3) return e("invalid PLTE","Corrupt PNG");
2400             pal_len = c.length / 3;
2401             if (pal_len * 3 != c.length) return e("invalid PLTE","Corrupt PNG");
2402             for (i=0; i < pal_len; ++i) {
2403                palette[i*4+0] = get8u(s);
2404                palette[i*4+1] = get8u(s);
2405                palette[i*4+2] = get8u(s);
2406                palette[i*4+3] = 255;
2407             }
2408             break;
2409          }
2410 
2411          case PNG_TYPE('t','R','N','S'): {
2412             if (first) return e("first not IHDR", "Corrupt PNG");
2413             if (z->idata) return e("tRNS after IDAT","Corrupt PNG");
2414             if (pal_img_n) {
2415                if (scan == SCAN_header) { s->img_n = 4; return 1; }
2416                if (pal_len == 0) return e("tRNS before PLTE","Corrupt PNG");
2417                if (c.length > pal_len) return e("bad tRNS len","Corrupt PNG");
2418                pal_img_n = 4;
2419                for (i=0; i < c.length; ++i)
2420                   palette[i*4+3] = get8u(s);
2421             } else {
2422                if (!(s->img_n & 1)) return e("tRNS with alpha","Corrupt PNG");
2423                if (c.length != (uint32) s->img_n*2) return e("bad tRNS len","Corrupt PNG");
2424                has_trans = 1;
2425                for (k=0; k < s->img_n; ++k)
2426                   tc[k] = (uint8) get16(s); // non 8-bit images will be larger
2427             }
2428             break;
2429          }
2430 
2431          case PNG_TYPE('I','D','A','T'): {
2432             if (first) return e("first not IHDR", "Corrupt PNG");
2433             if (pal_img_n && !pal_len) return e("no PLTE","Corrupt PNG");
2434             if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; }
2435             if (ioff + c.length > idata_limit) {
2436                uint8 *p;
2437                if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
2438                while (ioff + c.length > idata_limit)
2439                   idata_limit *= 2;
2440                p = (uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e("outofmem", "Out of memory");
2441                z->idata = p;
2442             }
2443             if (!getn(s, z->idata+ioff,c.length)) return e("outofdata","Corrupt PNG");
2444             ioff += c.length;
2445             break;
2446          }
2447 
2448          case PNG_TYPE('I','E','N','D'): {
2449             uint32 raw_len;
2450             if (first) return e("first not IHDR", "Corrupt PNG");
2451             if (scan != SCAN_load) return 1;
2452             if (z->idata == NULL) return e("no IDAT","Corrupt PNG");
2453             z->expanded = (uint8 *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !iphone);
2454             if (z->expanded == NULL) return 0; // zlib should set error
2455             free(z->idata); z->idata = NULL;
2456             if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
2457                s->img_out_n = s->img_n+1;
2458             else
2459                s->img_out_n = s->img_n;
2460             if (!create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0;
2461             if (has_trans)
2462                if (!compute_transparency(z, tc, s->img_out_n)) return 0;
2463             if (iphone && s->img_out_n > 2)
2464                stbi_de_iphone(z);
2465             if (pal_img_n) {
2466                // pal_img_n == 3 or 4
2467                s->img_n = pal_img_n; // record the actual colors we had
2468                s->img_out_n = pal_img_n;
2469                if (req_comp >= 3) s->img_out_n = req_comp;
2470                if (!expand_palette(z, palette, pal_len, s->img_out_n))
2471                   return 0;
2472             }
2473             free(z->expanded); z->expanded = NULL;
2474             return 1;
2475          }
2476 
2477          default:
2478             // if critical, fail
2479             if (first) return e("first not IHDR", "Corrupt PNG");
2480             if ((c.type & (1 << 29)) == 0) {
2481                #ifndef STBI_NO_FAILURE_STRINGS
2482                // not threadsafe
2483                static char invalid_chunk[] = "XXXX chunk not known";
2484                invalid_chunk[0] = (uint8) (c.type >> 24);
2485                invalid_chunk[1] = (uint8) (c.type >> 16);
2486                invalid_chunk[2] = (uint8) (c.type >>  8);
2487                invalid_chunk[3] = (uint8) (c.type >>  0);
2488                #endif
2489                return e(invalid_chunk, "PNG not supported: unknown chunk type");
2490             }
2491             skip(s, c.length);
2492             break;
2493       }
2494       // end of chunk, read and skip CRC
2495       get32(s);
2496    }
2497 }
2498 
do_png(png * p,int * x,int * y,int * n,int req_comp)2499 static unsigned char *do_png(png *p, int *x, int *y, int *n, int req_comp)
2500 {
2501    unsigned char *result=NULL;
2502    if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
2503    if (parse_png_file(p, SCAN_load, req_comp)) {
2504       result = p->out;
2505       p->out = NULL;
2506       if (req_comp && req_comp != p->s->img_out_n) {
2507          result = convert_format(result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
2508          p->s->img_out_n = req_comp;
2509          if (result == NULL) return result;
2510       }
2511       *x = p->s->img_x;
2512       *y = p->s->img_y;
2513       if (n) *n = p->s->img_n;
2514    }
2515    free(p->out);      p->out      = NULL;
2516    free(p->expanded); p->expanded = NULL;
2517    free(p->idata);    p->idata    = NULL;
2518 
2519    return result;
2520 }
2521 
stbi_png_load(stbi * s,int * x,int * y,int * comp,int req_comp)2522 static unsigned char *stbi_png_load(stbi *s, int *x, int *y, int *comp, int req_comp)
2523 {
2524    png p;
2525    p.s = s;
2526    return do_png(&p, x,y,comp,req_comp);
2527 }
2528 
stbi_png_test(stbi * s)2529 static int stbi_png_test(stbi *s)
2530 {
2531    int r;
2532    r = check_png_header(s);
2533    stbi_rewind(s);
2534    return r;
2535 }
2536 
stbi_png_info_raw(png * p,int * x,int * y,int * comp)2537 static int stbi_png_info_raw(png *p, int *x, int *y, int *comp)
2538 {
2539    if (!parse_png_file(p, SCAN_header, 0)) {
2540       stbi_rewind( p->s );
2541       return 0;
2542    }
2543    if (x) *x = p->s->img_x;
2544    if (y) *y = p->s->img_y;
2545    if (comp) *comp = p->s->img_n;
2546    return 1;
2547 }
2548 
stbi_png_info(stbi * s,int * x,int * y,int * comp)2549 static int      stbi_png_info(stbi *s, int *x, int *y, int *comp)
2550 {
2551    png p;
2552    p.s = s;
2553    return stbi_png_info_raw(&p, x, y, comp);
2554 }
2555 
2556 // Microsoft/Windows BMP image
2557 
bmp_test(stbi * s)2558 static int bmp_test(stbi *s)
2559 {
2560    int sz;
2561    if (get8(s) != 'B') return 0;
2562    if (get8(s) != 'M') return 0;
2563    get32le(s); // discard filesize
2564    get16le(s); // discard reserved
2565    get16le(s); // discard reserved
2566    get32le(s); // discard data offset
2567    sz = get32le(s);
2568    if (sz == 12 || sz == 40 || sz == 56 || sz == 108) return 1;
2569    return 0;
2570 }
2571 
stbi_bmp_test(stbi * s)2572 static int stbi_bmp_test(stbi *s)
2573 {
2574    int r = bmp_test(s);
2575    stbi_rewind(s);
2576    return r;
2577 }
2578 
2579 
2580 // returns 0..31 for the highest set bit
high_bit(unsigned int z)2581 static int high_bit(unsigned int z)
2582 {
2583    int n=0;
2584    if (z == 0) return -1;
2585    if (z >= 0x10000) n += 16, z >>= 16;
2586    if (z >= 0x00100) n +=  8, z >>=  8;
2587    if (z >= 0x00010) n +=  4, z >>=  4;
2588    if (z >= 0x00004) n +=  2, z >>=  2;
2589    if (z >= 0x00002) n +=  1, z >>=  1;
2590    return n;
2591 }
2592 
bitcount(unsigned int a)2593 static int bitcount(unsigned int a)
2594 {
2595    a = (a & 0x55555555) + ((a >>  1) & 0x55555555); // max 2
2596    a = (a & 0x33333333) + ((a >>  2) & 0x33333333); // max 4
2597    a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
2598    a = (a + (a >> 8)); // max 16 per 8 bits
2599    a = (a + (a >> 16)); // max 32 per 8 bits
2600    return a & 0xff;
2601 }
2602 
shiftsigned(int v,int shift,int bits)2603 static int shiftsigned(int v, int shift, int bits)
2604 {
2605    int result;
2606    int z=0;
2607 
2608    if (shift < 0) v <<= -shift;
2609    else v >>= shift;
2610    result = v;
2611 
2612    z = bits;
2613    while (z < 8) {
2614       result += v >> z;
2615       z += bits;
2616    }
2617    return result;
2618 }
2619 
bmp_load(stbi * s,int * x,int * y,int * comp,int req_comp)2620 static stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp)
2621 {
2622    uint8 *out;
2623    unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
2624    stbi_uc pal[256][4];
2625    int psize=0,i,j,compress=0,width;
2626    int bpp, flip_vertically, pad, target, offset, hsz;
2627    if (get8(s) != 'B' || get8(s) != 'M') return epuc("not BMP", "Corrupt BMP");
2628    get32le(s); // discard filesize
2629    get16le(s); // discard reserved
2630    get16le(s); // discard reserved
2631    offset = get32le(s);
2632    hsz = get32le(s);
2633    if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) return epuc("unknown BMP", "BMP type not supported: unknown");
2634    if (hsz == 12) {
2635       s->img_x = get16le(s);
2636       s->img_y = get16le(s);
2637    } else {
2638       s->img_x = get32le(s);
2639       s->img_y = get32le(s);
2640    }
2641    if (get16le(s) != 1) return epuc("bad BMP", "bad BMP");
2642    bpp = get16le(s);
2643    if (bpp == 1) return epuc("monochrome", "BMP type not supported: 1-bit");
2644    flip_vertically = ((int) s->img_y) > 0;
2645    s->img_y = abs((int) s->img_y);
2646    if (hsz == 12) {
2647       if (bpp < 24)
2648          psize = (offset - 14 - 24) / 3;
2649    } else {
2650       compress = get32le(s);
2651       if (compress == 1 || compress == 2) return epuc("BMP RLE", "BMP type not supported: RLE");
2652       get32le(s); // discard sizeof
2653       get32le(s); // discard hres
2654       get32le(s); // discard vres
2655       get32le(s); // discard colorsused
2656       get32le(s); // discard max important
2657       if (hsz == 40 || hsz == 56) {
2658          if (hsz == 56) {
2659             get32le(s);
2660             get32le(s);
2661             get32le(s);
2662             get32le(s);
2663          }
2664          if (bpp == 16 || bpp == 32) {
2665             mr = mg = mb = 0;
2666             if (compress == 0) {
2667                if (bpp == 32) {
2668                   mr = 0xffu << 16;
2669                   mg = 0xffu <<  8;
2670                   mb = 0xffu <<  0;
2671                   ma = 0xffu << 24;
2672                   fake_a = 1; // @TODO: check for cases like alpha value is all 0 and switch it to 255
2673                } else {
2674                   mr = 31u << 10;
2675                   mg = 31u <<  5;
2676                   mb = 31u <<  0;
2677                }
2678             } else if (compress == 3) {
2679                mr = get32le(s);
2680                mg = get32le(s);
2681                mb = get32le(s);
2682                // not documented, but generated by photoshop and handled by mspaint
2683                if (mr == mg && mg == mb) {
2684                   // ?!?!?
2685                   return epuc("bad BMP", "bad BMP");
2686                }
2687             } else
2688                return epuc("bad BMP", "bad BMP");
2689          }
2690       } else {
2691          assert(hsz == 108);
2692          mr = get32le(s);
2693          mg = get32le(s);
2694          mb = get32le(s);
2695          ma = get32le(s);
2696          get32le(s); // discard color space
2697          for (i=0; i < 12; ++i)
2698             get32le(s); // discard color space parameters
2699       }
2700       if (bpp < 16)
2701          psize = (offset - 14 - hsz) >> 2;
2702    }
2703    s->img_n = ma ? 4 : 3;
2704    if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
2705       target = req_comp;
2706    else
2707       target = s->img_n; // if they want monochrome, we'll post-convert
2708    out = (stbi_uc *) malloc(target * s->img_x * s->img_y);
2709    if (!out) return epuc("outofmem", "Out of memory");
2710    if (bpp < 16) {
2711       int z=0;
2712       if (psize == 0 || psize > 256) { free(out); return epuc("invalid", "Corrupt BMP"); }
2713       for (i=0; i < psize; ++i) {
2714          pal[i][2] = get8u(s);
2715          pal[i][1] = get8u(s);
2716          pal[i][0] = get8u(s);
2717          if (hsz != 12) get8(s);
2718          pal[i][3] = 255;
2719       }
2720       skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
2721       if (bpp == 4) width = (s->img_x + 1) >> 1;
2722       else if (bpp == 8) width = s->img_x;
2723       else { free(out); return epuc("bad bpp", "Corrupt BMP"); }
2724       pad = (-width)&3;
2725       for (j=0; j < (int) s->img_y; ++j) {
2726          for (i=0; i < (int) s->img_x; i += 2) {
2727             int v=get8(s),v2=0;
2728             if (bpp == 4) {
2729                v2 = v & 15;
2730                v >>= 4;
2731             }
2732             out[z++] = pal[v][0];
2733             out[z++] = pal[v][1];
2734             out[z++] = pal[v][2];
2735             if (target == 4) out[z++] = 255;
2736             if (i+1 == (int) s->img_x) break;
2737             v = (bpp == 8) ? get8(s) : v2;
2738             out[z++] = pal[v][0];
2739             out[z++] = pal[v][1];
2740             out[z++] = pal[v][2];
2741             if (target == 4) out[z++] = 255;
2742          }
2743          skip(s, pad);
2744       }
2745    } else {
2746       int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
2747       int z = 0;
2748       int easy=0;
2749       skip(s, offset - 14 - hsz);
2750       if (bpp == 24) width = 3 * s->img_x;
2751       else if (bpp == 16) width = 2*s->img_x;
2752       else /* bpp = 32 and pad = 0 */ width=0;
2753       pad = (-width) & 3;
2754       if (bpp == 24) {
2755          easy = 1;
2756       } else if (bpp == 32) {
2757          if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
2758             easy = 2;
2759       }
2760       if (!easy) {
2761          if (!mr || !mg || !mb) { free(out); return epuc("bad masks", "Corrupt BMP"); }
2762          // right shift amt to put high bit in position #7
2763          rshift = high_bit(mr)-7; rcount = bitcount(mr);
2764          gshift = high_bit(mg)-7; gcount = bitcount(mr);
2765          bshift = high_bit(mb)-7; bcount = bitcount(mr);
2766          ashift = high_bit(ma)-7; acount = bitcount(mr);
2767       }
2768       for (j=0; j < (int) s->img_y; ++j) {
2769          if (easy) {
2770             for (i=0; i < (int) s->img_x; ++i) {
2771                int a;
2772                out[z+2] = get8u(s);
2773                out[z+1] = get8u(s);
2774                out[z+0] = get8u(s);
2775                z += 3;
2776                a = (easy == 2 ? get8(s) : 255);
2777                if (target == 4) out[z++] = (uint8) a;
2778             }
2779          } else {
2780             for (i=0; i < (int) s->img_x; ++i) {
2781                uint32 v = (bpp == 16 ? get16le(s) : get32le(s));
2782                int a;
2783                out[z++] = (uint8) shiftsigned(v & mr, rshift, rcount);
2784                out[z++] = (uint8) shiftsigned(v & mg, gshift, gcount);
2785                out[z++] = (uint8) shiftsigned(v & mb, bshift, bcount);
2786                a = (ma ? shiftsigned(v & ma, ashift, acount) : 255);
2787                if (target == 4) out[z++] = (uint8) a;
2788             }
2789          }
2790          skip(s, pad);
2791       }
2792    }
2793    if (flip_vertically) {
2794       stbi_uc t;
2795       for (j=0; j < (int) s->img_y>>1; ++j) {
2796          stbi_uc *p1 = out +      j     *s->img_x*target;
2797          stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
2798          for (i=0; i < (int) s->img_x*target; ++i) {
2799             t = p1[i], p1[i] = p2[i], p2[i] = t;
2800          }
2801       }
2802    }
2803 
2804    if (req_comp && req_comp != target) {
2805       out = convert_format(out, target, req_comp, s->img_x, s->img_y);
2806       if (out == NULL) return out; // convert_format frees input on failure
2807    }
2808 
2809    *x = s->img_x;
2810    *y = s->img_y;
2811    if (comp) *comp = s->img_n;
2812    return out;
2813 }
2814 
stbi_bmp_load(stbi * s,int * x,int * y,int * comp,int req_comp)2815 static stbi_uc *stbi_bmp_load(stbi *s,int *x, int *y, int *comp, int req_comp)
2816 {
2817    return bmp_load(s, x,y,comp,req_comp);
2818 }
2819 
2820 
2821 // Targa Truevision - TGA
2822 // by Jonathan Dummer
2823 
tga_info(stbi * s,int * x,int * y,int * comp)2824 static int tga_info(stbi *s, int *x, int *y, int *comp)
2825 {
2826     int tga_w, tga_h, tga_comp;
2827     int sz;
2828     get8u(s);                   // discard Offset
2829     sz = get8u(s);              // color type
2830     if( sz > 1 ) {
2831         stbi_rewind(s);
2832         return 0;      // only RGB or indexed allowed
2833     }
2834     sz = get8u(s);              // image type
2835     // only RGB or grey allowed, +/- RLE
2836     if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11)) return 0;
2837     skip(s,9);
2838     tga_w = get16le(s);
2839     if( tga_w < 1 ) {
2840         stbi_rewind(s);
2841         return 0;   // test width
2842     }
2843     tga_h = get16le(s);
2844     if( tga_h < 1 ) {
2845         stbi_rewind(s);
2846         return 0;   // test height
2847     }
2848     sz = get8(s);               // bits per pixel
2849     // only RGB or RGBA or grey allowed
2850     if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) {
2851         stbi_rewind(s);
2852         return 0;
2853     }
2854     tga_comp = sz;
2855     if (x) *x = tga_w;
2856     if (y) *y = tga_h;
2857     if (comp) *comp = tga_comp / 8;
2858     return 1;                   // seems to have passed everything
2859 }
2860 
stbi_tga_info(stbi * s,int * x,int * y,int * comp)2861 int stbi_tga_info(stbi *s, int *x, int *y, int *comp)
2862 {
2863     return tga_info(s, x, y, comp);
2864 }
2865 
tga_test(stbi * s)2866 static int tga_test(stbi *s)
2867 {
2868    int sz;
2869    get8u(s);      //   discard Offset
2870    sz = get8u(s);   //   color type
2871    if ( sz > 1 ) return 0;   //   only RGB or indexed allowed
2872    sz = get8u(s);   //   image type
2873    if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0;   //   only RGB or grey allowed, +/- RLE
2874    get16(s);      //   discard palette start
2875    get16(s);      //   discard palette length
2876    get8(s);         //   discard bits per palette color entry
2877    get16(s);      //   discard x origin
2878    get16(s);      //   discard y origin
2879    if ( get16(s) < 1 ) return 0;      //   test width
2880    if ( get16(s) < 1 ) return 0;      //   test height
2881    sz = get8(s);   //   bits per pixel
2882    if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) ) return 0;   //   only RGB or RGBA or grey allowed
2883    return 1;      //   seems to have passed everything
2884 }
2885 
stbi_tga_test(stbi * s)2886 static int stbi_tga_test(stbi *s)
2887 {
2888    int res = tga_test(s);
2889    stbi_rewind(s);
2890    return res;
2891 }
2892 
tga_load(stbi * s,int * x,int * y,int * comp,int req_comp)2893 static stbi_uc *tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)
2894 {
2895    //   read in the TGA header stuff
2896    int tga_offset = get8u(s);
2897    int tga_indexed = get8u(s);
2898    int tga_image_type = get8u(s);
2899    int tga_is_RLE = 0;
2900    int tga_palette_start = get16le(s);
2901    int tga_palette_len = get16le(s);
2902    int tga_palette_bits = get8u(s);
2903    int tga_x_origin = get16le(s);
2904    int tga_y_origin = get16le(s);
2905    int tga_width = get16le(s);
2906    int tga_height = get16le(s);
2907    int tga_bits_per_pixel = get8u(s);
2908    int tga_inverted = get8u(s);
2909    //   image data
2910    unsigned char *tga_data;
2911    unsigned char *tga_palette = NULL;
2912    int i, j;
2913    unsigned char raw_data[4];
2914    unsigned char trans_data[4];
2915    int RLE_count = 0;
2916    int RLE_repeating = 0;
2917    int read_next_pixel = 1;
2918 
2919    //   do a tiny bit of precessing
2920    if ( tga_image_type >= 8 )
2921    {
2922       tga_image_type -= 8;
2923       tga_is_RLE = 1;
2924    }
2925    /* int tga_alpha_bits = tga_inverted & 15; */
2926    tga_inverted = 1 - ((tga_inverted >> 5) & 1);
2927 
2928    //   error check
2929    if ( //(tga_indexed) ||
2930       (tga_width < 1) || (tga_height < 1) ||
2931       (tga_image_type < 1) || (tga_image_type > 3) ||
2932       ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
2933       (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
2934       )
2935    {
2936       return NULL; // we don't report this as a bad TGA because we don't even know if it's TGA
2937    }
2938 
2939    //   If I'm paletted, then I'll use the number of bits from the palette
2940    if ( tga_indexed )
2941    {
2942       tga_bits_per_pixel = tga_palette_bits;
2943    }
2944 
2945    //   tga info
2946    *x = tga_width;
2947    *y = tga_height;
2948    if ( (req_comp < 1) || (req_comp > 4) )
2949    {
2950       //   just use whatever the file was
2951       req_comp = tga_bits_per_pixel / 8;
2952       *comp = req_comp;
2953    } else
2954    {
2955       //   force a new number of components
2956       *comp = tga_bits_per_pixel/8;
2957    }
2958    tga_data = (unsigned char*)malloc( tga_width * tga_height * req_comp );
2959    if (!tga_data) return epuc("outofmem", "Out of memory");
2960 
2961    //   skip to the data's starting position (offset usually = 0)
2962    skip(s, tga_offset );
2963    //   do I need to load a palette?
2964    if ( tga_indexed )
2965    {
2966       //   any data to skip? (offset usually = 0)
2967       skip(s, tga_palette_start );
2968       //   load the palette
2969       tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
2970       if (!tga_palette) return epuc("outofmem", "Out of memory");
2971       if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {
2972          free(tga_data);
2973          free(tga_palette);
2974          return epuc("bad palette", "Corrupt TGA");
2975       }
2976    }
2977    //   load the data
2978    trans_data[0] = trans_data[1] = trans_data[2] = trans_data[3] = 0;
2979    for (i=0; i < tga_width * tga_height; ++i)
2980    {
2981       //   if I'm in RLE mode, do I need to get a RLE chunk?
2982       if ( tga_is_RLE )
2983       {
2984          if ( RLE_count == 0 )
2985          {
2986             //   yep, get the next byte as a RLE command
2987             int RLE_cmd = get8u(s);
2988             RLE_count = 1 + (RLE_cmd & 127);
2989             RLE_repeating = RLE_cmd >> 7;
2990             read_next_pixel = 1;
2991          } else if ( !RLE_repeating )
2992          {
2993             read_next_pixel = 1;
2994          }
2995       } else
2996       {
2997          read_next_pixel = 1;
2998       }
2999       //   OK, if I need to read a pixel, do it now
3000       if ( read_next_pixel )
3001       {
3002          //   load however much data we did have
3003          if ( tga_indexed )
3004          {
3005             //   read in 1 byte, then perform the lookup
3006             int pal_idx = get8u(s);
3007             if ( pal_idx >= tga_palette_len )
3008             {
3009                //   invalid index
3010                pal_idx = 0;
3011             }
3012             pal_idx *= tga_bits_per_pixel / 8;
3013             for (j = 0; j*8 < tga_bits_per_pixel; ++j)
3014             {
3015                raw_data[j] = tga_palette[pal_idx+j];
3016             }
3017          } else
3018          {
3019             //   read in the data raw
3020             for (j = 0; j*8 < tga_bits_per_pixel; ++j)
3021             {
3022                raw_data[j] = get8u(s);
3023             }
3024          }
3025          //   convert raw to the intermediate format
3026          switch (tga_bits_per_pixel)
3027          {
3028          case 8:
3029             //   Luminous => RGBA
3030             trans_data[0] = raw_data[0];
3031             trans_data[1] = raw_data[0];
3032             trans_data[2] = raw_data[0];
3033             trans_data[3] = 255;
3034             break;
3035          case 16:
3036             //   Luminous,Alpha => RGBA
3037             trans_data[0] = raw_data[0];
3038             trans_data[1] = raw_data[0];
3039             trans_data[2] = raw_data[0];
3040             trans_data[3] = raw_data[1];
3041             break;
3042          case 24:
3043             //   BGR => RGBA
3044             trans_data[0] = raw_data[2];
3045             trans_data[1] = raw_data[1];
3046             trans_data[2] = raw_data[0];
3047             trans_data[3] = 255;
3048             break;
3049          case 32:
3050             //   BGRA => RGBA
3051             trans_data[0] = raw_data[2];
3052             trans_data[1] = raw_data[1];
3053             trans_data[2] = raw_data[0];
3054             trans_data[3] = raw_data[3];
3055             break;
3056          }
3057          //   clear the reading flag for the next pixel
3058          read_next_pixel = 0;
3059       } // end of reading a pixel
3060       //   convert to final format
3061       switch (req_comp)
3062       {
3063       case 1:
3064          //   RGBA => Luminance
3065          tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);
3066          break;
3067       case 2:
3068          //   RGBA => Luminance,Alpha
3069          tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);
3070          tga_data[i*req_comp+1] = trans_data[3];
3071          break;
3072       case 3:
3073          //   RGBA => RGB
3074          tga_data[i*req_comp+0] = trans_data[0];
3075          tga_data[i*req_comp+1] = trans_data[1];
3076          tga_data[i*req_comp+2] = trans_data[2];
3077          break;
3078       case 4:
3079          //   RGBA => RGBA
3080          tga_data[i*req_comp+0] = trans_data[0];
3081          tga_data[i*req_comp+1] = trans_data[1];
3082          tga_data[i*req_comp+2] = trans_data[2];
3083          tga_data[i*req_comp+3] = trans_data[3];
3084          break;
3085       }
3086       //   in case we're in RLE mode, keep counting down
3087       --RLE_count;
3088    }
3089    //   do I need to invert the image?
3090    if ( tga_inverted )
3091    {
3092       for (j = 0; j*2 < tga_height; ++j)
3093       {
3094          int index1 = j * tga_width * req_comp;
3095          int index2 = (tga_height - 1 - j) * tga_width * req_comp;
3096          for (i = tga_width * req_comp; i > 0; --i)
3097          {
3098             unsigned char temp = tga_data[index1];
3099             tga_data[index1] = tga_data[index2];
3100             tga_data[index2] = temp;
3101             ++index1;
3102             ++index2;
3103          }
3104       }
3105    }
3106    //   clear my palette, if I had one
3107    if ( tga_palette != NULL )
3108    {
3109       free( tga_palette );
3110    }
3111    //   the things I do to get rid of an error message, and yet keep
3112    //   Microsoft's C compilers happy... [8^(
3113    tga_palette_start = tga_palette_len = tga_palette_bits =
3114          tga_x_origin = tga_y_origin = 0;
3115    //   OK, done
3116    return tga_data;
3117 }
3118 
stbi_tga_load(stbi * s,int * x,int * y,int * comp,int req_comp)3119 static stbi_uc *stbi_tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3120 {
3121    return tga_load(s,x,y,comp,req_comp);
3122 }
3123 
3124 
3125 // *************************************************************************************************
3126 // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
3127 
psd_test(stbi * s)3128 static int psd_test(stbi *s)
3129 {
3130    if (get32(s) != 0x38425053) return 0;   // "8BPS"
3131    else return 1;
3132 }
3133 
stbi_psd_test(stbi * s)3134 static int stbi_psd_test(stbi *s)
3135 {
3136    int r = psd_test(s);
3137    stbi_rewind(s);
3138    return r;
3139 }
3140 
psd_load(stbi * s,int * x,int * y,int * comp,int req_comp)3141 static stbi_uc *psd_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3142 {
3143    int   pixelCount;
3144    int channelCount, compression;
3145    int channel, i, count, len;
3146    int w,h;
3147    uint8 *out;
3148 
3149    // Check identifier
3150    if (get32(s) != 0x38425053)   // "8BPS"
3151       return epuc("not PSD", "Corrupt PSD image");
3152 
3153    // Check file type version.
3154    if (get16(s) != 1)
3155       return epuc("wrong version", "Unsupported version of PSD image");
3156 
3157    // Skip 6 reserved bytes.
3158    skip(s, 6 );
3159 
3160    // Read the number of channels (R, G, B, A, etc).
3161    channelCount = get16(s);
3162    if (channelCount < 0 || channelCount > 16)
3163       return epuc("wrong channel count", "Unsupported number of channels in PSD image");
3164 
3165    // Read the rows and columns of the image.
3166    h = get32(s);
3167    w = get32(s);
3168 
3169    // Make sure the depth is 8 bits.
3170    if (get16(s) != 8)
3171       return epuc("unsupported bit depth", "PSD bit depth is not 8 bit");
3172 
3173    // Make sure the color mode is RGB.
3174    // Valid options are:
3175    //   0: Bitmap
3176    //   1: Grayscale
3177    //   2: Indexed color
3178    //   3: RGB color
3179    //   4: CMYK color
3180    //   7: Multichannel
3181    //   8: Duotone
3182    //   9: Lab color
3183    if (get16(s) != 3)
3184       return epuc("wrong color format", "PSD is not in RGB color format");
3185 
3186    // Skip the Mode Data.  (It's the palette for indexed color; other info for other modes.)
3187    skip(s,get32(s) );
3188 
3189    // Skip the image resources.  (resolution, pen tool paths, etc)
3190    skip(s, get32(s) );
3191 
3192    // Skip the reserved data.
3193    skip(s, get32(s) );
3194 
3195    // Find out if the data is compressed.
3196    // Known values:
3197    //   0: no compression
3198    //   1: RLE compressed
3199    compression = get16(s);
3200    if (compression > 1)
3201       return epuc("bad compression", "PSD has an unknown compression format");
3202 
3203    // Create the destination image.
3204    out = (stbi_uc *) malloc(4 * w*h);
3205    if (!out) return epuc("outofmem", "Out of memory");
3206    pixelCount = w*h;
3207 
3208    // Initialize the data to zero.
3209    //memset( out, 0, pixelCount * 4 );
3210 
3211    // Finally, the image data.
3212    if (compression) {
3213       // RLE as used by .PSD and .TIFF
3214       // Loop until you get the number of unpacked bytes you are expecting:
3215       //     Read the next source byte into n.
3216       //     If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
3217       //     Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
3218       //     Else if n is 128, noop.
3219       // Endloop
3220 
3221       // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
3222       // which we're going to just skip.
3223       skip(s, h * channelCount * 2 );
3224 
3225       // Read the RLE data by channel.
3226       for (channel = 0; channel < 4; channel++) {
3227          uint8 *p;
3228 
3229          p = out+channel;
3230          if (channel >= channelCount) {
3231             // Fill this channel with default data.
3232             for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
3233          } else {
3234             // Read the RLE data.
3235             count = 0;
3236             while (count < pixelCount) {
3237                len = get8(s);
3238                if (len == 128) {
3239                   // No-op.
3240                } else if (len < 128) {
3241                   // Copy next len+1 bytes literally.
3242                   len++;
3243                   count += len;
3244                   while (len) {
3245                      *p = get8u(s);
3246                      p += 4;
3247                      len--;
3248                   }
3249                } else if (len > 128) {
3250                   uint8   val;
3251                   // Next -len+1 bytes in the dest are replicated from next source byte.
3252                   // (Interpret len as a negative 8-bit int.)
3253                   len ^= 0x0FF;
3254                   len += 2;
3255                   val = get8u(s);
3256                   count += len;
3257                   while (len) {
3258                      *p = val;
3259                      p += 4;
3260                      len--;
3261                   }
3262                }
3263             }
3264          }
3265       }
3266 
3267    } else {
3268       // We're at the raw image data.  It's each channel in order (Red, Green, Blue, Alpha, ...)
3269       // where each channel consists of an 8-bit value for each pixel in the image.
3270 
3271       // Read the data by channel.
3272       for (channel = 0; channel < 4; channel++) {
3273          uint8 *p;
3274 
3275          p = out + channel;
3276          if (channel > channelCount) {
3277             // Fill this channel with default data.
3278             for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
3279          } else {
3280             // Read the data.
3281             for (i = 0; i < pixelCount; i++)
3282                *p = get8u(s), p += 4;
3283          }
3284       }
3285    }
3286 
3287    if (req_comp && req_comp != 4) {
3288       out = convert_format(out, 4, req_comp, w, h);
3289       if (out == NULL) return out; // convert_format frees input on failure
3290    }
3291 
3292    if (comp) *comp = channelCount;
3293    *y = h;
3294    *x = w;
3295 
3296    return out;
3297 }
3298 
stbi_psd_load(stbi * s,int * x,int * y,int * comp,int req_comp)3299 static stbi_uc *stbi_psd_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3300 {
3301    return psd_load(s,x,y,comp,req_comp);
3302 }
3303 
3304 // *************************************************************************************************
3305 // Softimage PIC loader
3306 // by Tom Seddon
3307 //
3308 // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
3309 // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
3310 
pic_is4(stbi * s,const char * str)3311 static int pic_is4(stbi *s,const char *str)
3312 {
3313    int i;
3314    for (i=0; i<4; ++i)
3315       if (get8(s) != (stbi_uc)str[i])
3316          return 0;
3317 
3318    return 1;
3319 }
3320 
pic_test(stbi * s)3321 static int pic_test(stbi *s)
3322 {
3323    int i;
3324 
3325    if (!pic_is4(s,"\x53\x80\xF6\x34"))
3326       return 0;
3327 
3328    for(i=0;i<84;++i)
3329       get8(s);
3330 
3331    if (!pic_is4(s,"PICT"))
3332       return 0;
3333 
3334    return 1;
3335 }
3336 
3337 typedef struct
3338 {
3339    stbi_uc size,type,channel;
3340 } pic_packet_t;
3341 
pic_readval(stbi * s,int channel,stbi_uc * dest)3342 static stbi_uc *pic_readval(stbi *s, int channel, stbi_uc *dest)
3343 {
3344    int mask=0x80, i;
3345 
3346    for (i=0; i<4; ++i, mask>>=1) {
3347       if (channel & mask) {
3348          if (at_eof(s)) return epuc("bad file","PIC file too short");
3349          dest[i]=get8u(s);
3350       }
3351    }
3352 
3353    return dest;
3354 }
3355 
pic_copyval(int channel,stbi_uc * dest,const stbi_uc * src)3356 static void pic_copyval(int channel,stbi_uc *dest,const stbi_uc *src)
3357 {
3358    int mask=0x80,i;
3359 
3360    for (i=0;i<4; ++i, mask>>=1)
3361       if (channel&mask)
3362          dest[i]=src[i];
3363 }
3364 
pic_load2(stbi * s,int width,int height,int * comp,stbi_uc * result)3365 static stbi_uc *pic_load2(stbi *s,int width,int height,int *comp, stbi_uc *result)
3366 {
3367    int act_comp=0,num_packets=0,y,chained;
3368    pic_packet_t packets[10];
3369 
3370    // this will (should...) cater for even some bizarre stuff like having data
3371     // for the same channel in multiple packets.
3372    do {
3373       pic_packet_t *packet;
3374 
3375       if (num_packets==sizeof(packets)/sizeof(packets[0]))
3376          return epuc("bad format","too many packets");
3377 
3378       packet = &packets[num_packets++];
3379 
3380       chained = get8(s);
3381       packet->size    = get8u(s);
3382       packet->type    = get8u(s);
3383       packet->channel = get8u(s);
3384 
3385       act_comp |= packet->channel;
3386 
3387       if (at_eof(s))          return epuc("bad file","file too short (reading packets)");
3388       if (packet->size != 8)  return epuc("bad format","packet isn't 8bpp");
3389    } while (chained);
3390 
3391    *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
3392 
3393    for(y=0; y<height; ++y) {
3394       int packet_idx;
3395 
3396       for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
3397          pic_packet_t *packet = &packets[packet_idx];
3398          stbi_uc *dest = result+y*width*4;
3399 
3400          switch (packet->type) {
3401             default:
3402                return epuc("bad format","packet has bad compression type");
3403 
3404             case 0: {//uncompressed
3405                int x;
3406 
3407                for(x=0;x<width;++x, dest+=4)
3408                   if (!pic_readval(s,packet->channel,dest))
3409                      return 0;
3410                break;
3411             }
3412 
3413             case 1://Pure RLE
3414                {
3415                   int left=width, i;
3416 
3417                   while (left>0) {
3418                      stbi_uc count,value[4];
3419 
3420                      count=get8u(s);
3421                      if (at_eof(s))   return epuc("bad file","file too short (pure read count)");
3422 
3423                      if (count > left)
3424                         count = (uint8) left;
3425 
3426                      if (!pic_readval(s,packet->channel,value))  return 0;
3427 
3428                      for(i=0; i<count; ++i,dest+=4)
3429                         pic_copyval(packet->channel,dest,value);
3430                      left -= count;
3431                   }
3432                }
3433                break;
3434 
3435             case 2: {//Mixed RLE
3436                int left=width;
3437                while (left>0) {
3438                   int count = get8(s), i;
3439                   if (at_eof(s))  return epuc("bad file","file too short (mixed read count)");
3440 
3441                   if (count >= 128) { // Repeated
3442                      stbi_uc value[4];
3443                      int i;
3444 
3445                      if (count==128)
3446                         count = get16(s);
3447                      else
3448                         count -= 127;
3449                      if (count > left)
3450                         return epuc("bad file","scanline overrun");
3451 
3452                      if (!pic_readval(s,packet->channel,value))
3453                         return 0;
3454 
3455                      for(i=0;i<count;++i, dest += 4)
3456                         pic_copyval(packet->channel,dest,value);
3457                   } else { // Raw
3458                      ++count;
3459                      if (count>left) return epuc("bad file","scanline overrun");
3460 
3461                      for(i=0;i<count;++i, dest+=4)
3462                         if (!pic_readval(s,packet->channel,dest))
3463                            return 0;
3464                   }
3465                   left-=count;
3466                }
3467                break;
3468             }
3469          }
3470       }
3471    }
3472 
3473    return result;
3474 }
3475 
pic_load(stbi * s,int * px,int * py,int * comp,int req_comp)3476 static stbi_uc *pic_load(stbi *s,int *px,int *py,int *comp,int req_comp)
3477 {
3478    stbi_uc *result;
3479    int i, x,y;
3480 
3481    for (i=0; i<92; ++i)
3482       get8(s);
3483 
3484    x = get16(s);
3485    y = get16(s);
3486    if (at_eof(s))  return epuc("bad file","file too short (pic header)");
3487    if ((1 << 28) / x < y) return epuc("too large", "Image too large to decode");
3488 
3489    get32(s); //skip `ratio'
3490    get16(s); //skip `fields'
3491    get16(s); //skip `pad'
3492 
3493    // intermediate buffer is RGBA
3494    result = (stbi_uc *) malloc(x*y*4);
3495    memset(result, 0xff, x*y*4);
3496 
3497    if (!pic_load2(s,x,y,comp, result)) {
3498       free(result);
3499       result=0;
3500    }
3501    *px = x;
3502    *py = y;
3503    if (req_comp == 0) req_comp = *comp;
3504    result=convert_format(result,4,req_comp,x,y);
3505 
3506    return result;
3507 }
3508 
stbi_pic_test(stbi * s)3509 static int stbi_pic_test(stbi *s)
3510 {
3511    int r = pic_test(s);
3512    stbi_rewind(s);
3513    return r;
3514 }
3515 
stbi_pic_load(stbi * s,int * x,int * y,int * comp,int req_comp)3516 static stbi_uc *stbi_pic_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3517 {
3518    return pic_load(s,x,y,comp,req_comp);
3519 }
3520 
3521 // *************************************************************************************************
3522 // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
3523 typedef struct stbi_gif_lzw_struct {
3524    int16 prefix;
3525    uint8 first;
3526    uint8 suffix;
3527 } stbi_gif_lzw;
3528 
3529 typedef struct stbi_gif_struct
3530 {
3531    int w,h;
3532    stbi_uc *out;                 // output buffer (always 4 components)
3533    int flags, bgindex, ratio, transparent, eflags;
3534    uint8  pal[256][4];
3535    uint8 lpal[256][4];
3536    stbi_gif_lzw codes[4096];
3537    uint8 *color_table;
3538    int parse, step;
3539    int lflags;
3540    int start_x, start_y;
3541    int max_x, max_y;
3542    int cur_x, cur_y;
3543    int line_size;
3544 } stbi_gif;
3545 
gif_test(stbi * s)3546 static int gif_test(stbi *s)
3547 {
3548    int sz;
3549    if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8') return 0;
3550    sz = get8(s);
3551    if (sz != '9' && sz != '7') return 0;
3552    if (get8(s) != 'a') return 0;
3553    return 1;
3554 }
3555 
stbi_gif_test(stbi * s)3556 static int stbi_gif_test(stbi *s)
3557 {
3558    int r = gif_test(s);
3559    stbi_rewind(s);
3560    return r;
3561 }
3562 
stbi_gif_parse_colortable(stbi * s,uint8 pal[256][4],int num_entries,int transp)3563 static void stbi_gif_parse_colortable(stbi *s, uint8 pal[256][4], int num_entries, int transp)
3564 {
3565    int i;
3566    for (i=0; i < num_entries; ++i) {
3567       pal[i][2] = get8u(s);
3568       pal[i][1] = get8u(s);
3569       pal[i][0] = get8u(s);
3570       pal[i][3] = transp ? 0 : 255;
3571    }
3572 }
3573 
stbi_gif_header(stbi * s,stbi_gif * g,int * comp,int is_info)3574 static int stbi_gif_header(stbi *s, stbi_gif *g, int *comp, int is_info)
3575 {
3576    uint8 version;
3577    if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8')
3578       return e("not GIF", "Corrupt GIF");
3579 
3580    version = get8u(s);
3581    if (version != '7' && version != '9')    return e("not GIF", "Corrupt GIF");
3582    if (get8(s) != 'a')                      return e("not GIF", "Corrupt GIF");
3583 
3584    failure_reason = "";
3585    g->w = get16le(s);
3586    g->h = get16le(s);
3587    g->flags = get8(s);
3588    g->bgindex = get8(s);
3589    g->ratio = get8(s);
3590    g->transparent = -1;
3591 
3592    if (comp != 0) *comp = 4;  // can't actually tell whether it's 3 or 4 until we parse the comments
3593 
3594    if (is_info) return 1;
3595 
3596    if (g->flags & 0x80)
3597       stbi_gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
3598 
3599    return 1;
3600 }
3601 
stbi_gif_info_raw(stbi * s,int * x,int * y,int * comp)3602 static int stbi_gif_info_raw(stbi *s, int *x, int *y, int *comp)
3603 {
3604    stbi_gif g;
3605    if (!stbi_gif_header(s, &g, comp, 1)) {
3606       stbi_rewind( s );
3607       return 0;
3608    }
3609    if (x) *x = g.w;
3610    if (y) *y = g.h;
3611    return 1;
3612 }
3613 
stbi_out_gif_code(stbi_gif * g,uint16 code)3614 static void stbi_out_gif_code(stbi_gif *g, uint16 code)
3615 {
3616    uint8 *p, *c;
3617 
3618    // recurse to decode the prefixes, since the linked-list is backwards,
3619    // and working backwards through an interleaved image would be nasty
3620    if (g->codes[code].prefix >= 0)
3621       stbi_out_gif_code(g, g->codes[code].prefix);
3622 
3623    if (g->cur_y >= g->max_y) return;
3624 
3625    p = &g->out[g->cur_x + g->cur_y];
3626    c = &g->color_table[g->codes[code].suffix * 4];
3627 
3628    if (c[3] >= 128) {
3629       p[0] = c[2];
3630       p[1] = c[1];
3631       p[2] = c[0];
3632       p[3] = c[3];
3633    }
3634    g->cur_x += 4;
3635 
3636    if (g->cur_x >= g->max_x) {
3637       g->cur_x = g->start_x;
3638       g->cur_y += g->step;
3639 
3640       while (g->cur_y >= g->max_y && g->parse > 0) {
3641          g->step = (1 << g->parse) * g->line_size;
3642          g->cur_y = g->start_y + (g->step >> 1);
3643          --g->parse;
3644       }
3645    }
3646 }
3647 
stbi_process_gif_raster(stbi * s,stbi_gif * g)3648 static uint8 *stbi_process_gif_raster(stbi *s, stbi_gif *g)
3649 {
3650    uint8 lzw_cs;
3651    int32 len, code;
3652    uint32 first;
3653    int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
3654    stbi_gif_lzw *p;
3655 
3656    lzw_cs = get8u(s);
3657    clear = 1 << lzw_cs;
3658    first = 1;
3659    codesize = lzw_cs + 1;
3660    codemask = (1 << codesize) - 1;
3661    bits = 0;
3662    valid_bits = 0;
3663    for (code = 0; code < clear; code++) {
3664       g->codes[code].prefix = -1;
3665       g->codes[code].first = (uint8) code;
3666       g->codes[code].suffix = (uint8) code;
3667    }
3668 
3669    // support no starting clear code
3670    avail = clear+2;
3671    oldcode = -1;
3672 
3673    len = 0;
3674    for(;;) {
3675       if (valid_bits < codesize) {
3676          if (len == 0) {
3677             len = get8(s); // start new block
3678             if (len == 0)
3679                return g->out;
3680          }
3681          --len;
3682          bits |= (int32) get8(s) << valid_bits;
3683          valid_bits += 8;
3684       } else {
3685          int32 code = bits & codemask;
3686          bits >>= codesize;
3687          valid_bits -= codesize;
3688          // @OPTIMIZE: is there some way we can accelerate the non-clear path?
3689          if (code == clear) {  // clear code
3690             codesize = lzw_cs + 1;
3691             codemask = (1 << codesize) - 1;
3692             avail = clear + 2;
3693             oldcode = -1;
3694             first = 0;
3695          } else if (code == clear + 1) { // end of stream code
3696             skip(s, len);
3697             while ((len = get8(s)) > 0)
3698                skip(s,len);
3699             return g->out;
3700          } else if (code <= avail) {
3701             if (first) return epuc("no clear code", "Corrupt GIF");
3702 
3703             if (oldcode >= 0) {
3704                p = &g->codes[avail++];
3705                if (avail > 4096)        return epuc("too many codes", "Corrupt GIF");
3706                p->prefix = (int16) oldcode;
3707                p->first = g->codes[oldcode].first;
3708                p->suffix = (code == avail) ? p->first : g->codes[code].first;
3709             } else if (code == avail)
3710                return epuc("illegal code in raster", "Corrupt GIF");
3711 
3712             stbi_out_gif_code(g, (uint16) code);
3713 
3714             if ((avail & codemask) == 0 && avail <= 0x0FFF) {
3715                codesize++;
3716                codemask = (1 << codesize) - 1;
3717             }
3718 
3719             oldcode = code;
3720          } else {
3721             return epuc("illegal code in raster", "Corrupt GIF");
3722          }
3723       }
3724    }
3725 }
3726 
stbi_fill_gif_background(stbi_gif * g)3727 static void stbi_fill_gif_background(stbi_gif *g)
3728 {
3729    int i;
3730    uint8 *c = g->pal[g->bgindex];
3731    // @OPTIMIZE: write a dword at a time
3732    for (i = 0; i < g->w * g->h * 4; i += 4) {
3733       uint8 *p  = &g->out[i];
3734       p[0] = c[2];
3735       p[1] = c[1];
3736       p[2] = c[0];
3737       p[3] = c[3];
3738    }
3739 }
3740 
3741 // this function is designed to support animated gifs, although stb_image doesn't support it
stbi_gif_load_next(stbi * s,stbi_gif * g,int * comp,int req_comp)3742 static uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp)
3743 {
3744    int i;
3745    uint8 *old_out = 0;
3746 
3747    if (g->out == 0) {
3748       if (!stbi_gif_header(s, g, comp,0))     return 0; // failure_reason set by stbi_gif_header
3749       g->out = (uint8 *) malloc(4 * g->w * g->h);
3750       if (g->out == 0)                      return epuc("outofmem", "Out of memory");
3751       stbi_fill_gif_background(g);
3752    } else {
3753       // animated-gif-only path
3754       if (((g->eflags & 0x1C) >> 2) == 3) {
3755          old_out = g->out;
3756          g->out = (uint8 *) malloc(4 * g->w * g->h);
3757          if (g->out == 0)                   return epuc("outofmem", "Out of memory");
3758          memcpy(g->out, old_out, g->w*g->h*4);
3759       }
3760    }
3761 
3762    for (;;) {
3763       switch (get8(s)) {
3764          case 0x2C: /* Image Descriptor */
3765          {
3766             int32 x, y, w, h;
3767             uint8 *o;
3768 
3769             x = get16le(s);
3770             y = get16le(s);
3771             w = get16le(s);
3772             h = get16le(s);
3773             if (((x + w) > (g->w)) || ((y + h) > (g->h)))
3774                return epuc("bad Image Descriptor", "Corrupt GIF");
3775 
3776             g->line_size = g->w * 4;
3777             g->start_x = x * 4;
3778             g->start_y = y * g->line_size;
3779             g->max_x   = g->start_x + w * 4;
3780             g->max_y   = g->start_y + h * g->line_size;
3781             g->cur_x   = g->start_x;
3782             g->cur_y   = g->start_y;
3783 
3784             g->lflags = get8(s);
3785 
3786             if (g->lflags & 0x40) {
3787                g->step = 8 * g->line_size; // first interlaced spacing
3788                g->parse = 3;
3789             } else {
3790                g->step = g->line_size;
3791                g->parse = 0;
3792             }
3793 
3794             if (g->lflags & 0x80) {
3795                stbi_gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
3796                g->color_table = (uint8 *) g->lpal;
3797             } else if (g->flags & 0x80) {
3798                for (i=0; i < 256; ++i)  // @OPTIMIZE: reset only the previous transparent
3799                   g->pal[i][3] = 255;
3800                if (g->transparent >= 0 && (g->eflags & 0x01))
3801                   g->pal[g->transparent][3] = 0;
3802                g->color_table = (uint8 *) g->pal;
3803             } else
3804                return epuc("missing color table", "Corrupt GIF");
3805 
3806             o = stbi_process_gif_raster(s, g);
3807             if (o == NULL) return NULL;
3808 
3809             if (req_comp && req_comp != 4)
3810                o = convert_format(o, 4, req_comp, g->w, g->h);
3811             return o;
3812          }
3813 
3814          case 0x21: // Comment Extension.
3815          {
3816             int len;
3817             if (get8(s) == 0xF9) { // Graphic Control Extension.
3818                len = get8(s);
3819                if (len == 4) {
3820                   g->eflags = get8(s);
3821                   get16le(s); // delay
3822                   g->transparent = get8(s);
3823                } else {
3824                   skip(s, len);
3825                   break;
3826                }
3827             }
3828             while ((len = get8(s)) != 0)
3829                skip(s, len);
3830             break;
3831          }
3832 
3833          case 0x3B: // gif stream termination code
3834             return (uint8 *) 1;
3835 
3836          default:
3837             return epuc("unknown code", "Corrupt GIF");
3838       }
3839    }
3840 }
3841 
stbi_gif_load(stbi * s,int * x,int * y,int * comp,int req_comp)3842 static stbi_uc *stbi_gif_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3843 {
3844    uint8 *u = 0;
3845    stbi_gif g={0};
3846 
3847    u = stbi_gif_load_next(s, &g, comp, req_comp);
3848    if (u == (void *) 1) u = 0;  // end of animated gif marker
3849    if (u) {
3850       *x = g.w;
3851       *y = g.h;
3852    }
3853 
3854    return u;
3855 }
3856 
stbi_gif_info(stbi * s,int * x,int * y,int * comp)3857 static int stbi_gif_info(stbi *s, int *x, int *y, int *comp)
3858 {
3859    return stbi_gif_info_raw(s,x,y,comp);
3860 }
3861 
3862 
3863 // *************************************************************************************************
3864 // Radiance RGBE HDR loader
3865 // originally by Nicolas Schulz
3866 #ifndef STBI_NO_HDR
hdr_test(stbi * s)3867 static int hdr_test(stbi *s)
3868 {
3869    const char *signature = "#?RADIANCE\n";
3870    int i;
3871    for (i=0; signature[i]; ++i)
3872       if (get8(s) != signature[i])
3873          return 0;
3874    return 1;
3875 }
3876 
stbi_hdr_test(stbi * s)3877 static int stbi_hdr_test(stbi* s)
3878 {
3879    int r = hdr_test(s);
3880    stbi_rewind(s);
3881    return r;
3882 }
3883 
3884 #define HDR_BUFLEN  1024
hdr_gettoken(stbi * z,char * buffer)3885 static char *hdr_gettoken(stbi *z, char *buffer)
3886 {
3887    int len=0;
3888    char c = '\0';
3889 
3890    c = (char) get8(z);
3891 
3892    while (!at_eof(z) && c != '\n') {
3893       buffer[len++] = c;
3894       if (len == HDR_BUFLEN-1) {
3895          // flush to end of line
3896          while (!at_eof(z) && get8(z) != '\n')
3897             ;
3898          break;
3899       }
3900       c = (char) get8(z);
3901    }
3902 
3903    buffer[len] = 0;
3904    return buffer;
3905 }
3906 
hdr_convert(float * output,stbi_uc * input,int req_comp)3907 static void hdr_convert(float *output, stbi_uc *input, int req_comp)
3908 {
3909    if ( input[3] != 0 ) {
3910       float f1;
3911       // Exponent
3912       f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
3913       if (req_comp <= 2)
3914          output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
3915       else {
3916          output[0] = input[0] * f1;
3917          output[1] = input[1] * f1;
3918          output[2] = input[2] * f1;
3919       }
3920       if (req_comp == 2) output[1] = 1;
3921       if (req_comp == 4) output[3] = 1;
3922    } else {
3923       switch (req_comp) {
3924          case 4: output[3] = 1; /* fallthrough */
3925          case 3: output[0] = output[1] = output[2] = 0;
3926                  break;
3927          case 2: output[1] = 1; /* fallthrough */
3928          case 1: output[0] = 0;
3929                  break;
3930       }
3931    }
3932 }
3933 
hdr_load(stbi * s,int * x,int * y,int * comp,int req_comp)3934 static float *hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3935 {
3936    char buffer[HDR_BUFLEN];
3937    char *token;
3938    int valid = 0;
3939    int width, height;
3940    stbi_uc *scanline;
3941    float *hdr_data;
3942    int len;
3943    unsigned char count, value;
3944    int i, j, k, c1,c2, z;
3945 
3946 
3947    // Check identifier
3948    if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
3949       return epf("not HDR", "Corrupt HDR image");
3950 
3951    // Parse header
3952    for(;;) {
3953       token = hdr_gettoken(s,buffer);
3954       if (token[0] == 0) break;
3955       if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
3956    }
3957 
3958    if (!valid)    return epf("unsupported format", "Unsupported HDR format");
3959 
3960    // Parse width and height
3961    // can't use sscanf() if we're not using stdio!
3962    token = hdr_gettoken(s,buffer);
3963    if (strncmp(token, "-Y ", 3))  return epf("unsupported data layout", "Unsupported HDR format");
3964    token += 3;
3965    height = strtol(token, &token, 10);
3966    while (*token == ' ') ++token;
3967    if (strncmp(token, "+X ", 3))  return epf("unsupported data layout", "Unsupported HDR format");
3968    token += 3;
3969    width = strtol(token, NULL, 10);
3970 
3971    *x = width;
3972    *y = height;
3973 
3974    *comp = 3;
3975    if (req_comp == 0) req_comp = 3;
3976 
3977    // Read data
3978    hdr_data = (float *) malloc(height * width * req_comp * sizeof(float));
3979 
3980    // Load image data
3981    // image data is stored as some number of sca
3982    if ( width < 8 || width >= 32768) {
3983       // Read flat data
3984       for (j=0; j < height; ++j) {
3985          for (i=0; i < width; ++i) {
3986             stbi_uc rgbe[4];
3987            main_decode_loop:
3988             getn(s, rgbe, 4);
3989             hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
3990          }
3991       }
3992    } else {
3993       // Read RLE-encoded data
3994       scanline = NULL;
3995 
3996       for (j = 0; j < height; ++j) {
3997          c1 = get8(s);
3998          c2 = get8(s);
3999          len = get8(s);
4000          if (c1 != 2 || c2 != 2 || (len & 0x80)) {
4001             // not run-length encoded, so we have to actually use THIS data as a decoded
4002             // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
4003             uint8 rgbe[4];
4004             rgbe[0] = (uint8) c1;
4005             rgbe[1] = (uint8) c2;
4006             rgbe[2] = (uint8) len;
4007             rgbe[3] = (uint8) get8u(s);
4008             hdr_convert(hdr_data, rgbe, req_comp);
4009             i = 1;
4010             j = 0;
4011             free(scanline);
4012             goto main_decode_loop; // yes, this makes no sense
4013          }
4014          len <<= 8;
4015          len |= get8(s);
4016          if (len != width) { free(hdr_data); free(scanline); return epf("invalid decoded scanline length", "corrupt HDR"); }
4017          if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4);
4018 
4019          for (k = 0; k < 4; ++k) {
4020             i = 0;
4021             while (i < width) {
4022                count = get8u(s);
4023                if (count > 128) {
4024                   // Run
4025                   value = get8u(s);
4026                   count -= 128;
4027                   for (z = 0; z < count; ++z)
4028                      scanline[i++ * 4 + k] = value;
4029                } else {
4030                   // Dump
4031                   for (z = 0; z < count; ++z)
4032                      scanline[i++ * 4 + k] = get8u(s);
4033                }
4034             }
4035          }
4036          for (i=0; i < width; ++i)
4037             hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
4038       }
4039       free(scanline);
4040    }
4041 
4042    return hdr_data;
4043 }
4044 
stbi_hdr_load(stbi * s,int * x,int * y,int * comp,int req_comp)4045 static float *stbi_hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp)
4046 {
4047    return hdr_load(s,x,y,comp,req_comp);
4048 }
4049 
stbi_hdr_info(stbi * s,int * x,int * y,int * comp)4050 static int stbi_hdr_info(stbi *s, int *x, int *y, int *comp)
4051 {
4052    char buffer[HDR_BUFLEN];
4053    char *token;
4054    int valid = 0;
4055 
4056    if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0) {
4057        stbi_rewind( s );
4058        return 0;
4059    }
4060 
4061    for(;;) {
4062       token = hdr_gettoken(s,buffer);
4063       if (token[0] == 0) break;
4064       if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
4065    }
4066 
4067    if (!valid) {
4068        stbi_rewind( s );
4069        return 0;
4070    }
4071    token = hdr_gettoken(s,buffer);
4072    if (strncmp(token, "-Y ", 3)) {
4073        stbi_rewind( s );
4074        return 0;
4075    }
4076    token += 3;
4077    *y = strtol(token, &token, 10);
4078    while (*token == ' ') ++token;
4079    if (strncmp(token, "+X ", 3)) {
4080        stbi_rewind( s );
4081        return 0;
4082    }
4083    token += 3;
4084    *x = strtol(token, NULL, 10);
4085    *comp = 3;
4086    return 1;
4087 }
4088 #endif // STBI_NO_HDR
4089 
stbi_bmp_info(stbi * s,int * x,int * y,int * comp)4090 static int stbi_bmp_info(stbi *s, int *x, int *y, int *comp)
4091 {
4092    int hsz;
4093    if (get8(s) != 'B' || get8(s) != 'M') {
4094        stbi_rewind( s );
4095        return 0;
4096    }
4097    skip(s,12);
4098    hsz = get32le(s);
4099    if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) {
4100        stbi_rewind( s );
4101        return 0;
4102    }
4103    if (hsz == 12) {
4104       *x = get16le(s);
4105       *y = get16le(s);
4106    } else {
4107       *x = get32le(s);
4108       *y = get32le(s);
4109    }
4110    if (get16le(s) != 1) {
4111        stbi_rewind( s );
4112        return 0;
4113    }
4114    *comp = get16le(s) / 8;
4115    return 1;
4116 }
4117 
stbi_psd_info(stbi * s,int * x,int * y,int * comp)4118 static int stbi_psd_info(stbi *s, int *x, int *y, int *comp)
4119 {
4120    int channelCount;
4121    if (get32(s) != 0x38425053) {
4122        stbi_rewind( s );
4123        return 0;
4124    }
4125    if (get16(s) != 1) {
4126        stbi_rewind( s );
4127        return 0;
4128    }
4129    skip(s, 6);
4130    channelCount = get16(s);
4131    if (channelCount < 0 || channelCount > 16) {
4132        stbi_rewind( s );
4133        return 0;
4134    }
4135    *y = get32(s);
4136    *x = get32(s);
4137    if (get16(s) != 8) {
4138        stbi_rewind( s );
4139        return 0;
4140    }
4141    if (get16(s) != 3) {
4142        stbi_rewind( s );
4143        return 0;
4144    }
4145    *comp = 4;
4146    return 1;
4147 }
4148 
stbi_pic_info(stbi * s,int * x,int * y,int * comp)4149 static int stbi_pic_info(stbi *s, int *x, int *y, int *comp)
4150 {
4151    int act_comp=0,num_packets=0,chained;
4152    pic_packet_t packets[10];
4153 
4154    skip(s, 92);
4155 
4156    *x = get16(s);
4157    *y = get16(s);
4158    if (at_eof(s))  return 0;
4159    if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
4160        stbi_rewind( s );
4161        return 0;
4162    }
4163 
4164    skip(s, 8);
4165 
4166    do {
4167       pic_packet_t *packet;
4168 
4169       if (num_packets==sizeof(packets)/sizeof(packets[0]))
4170          return 0;
4171 
4172       packet = &packets[num_packets++];
4173       chained = get8(s);
4174       packet->size    = get8u(s);
4175       packet->type    = get8u(s);
4176       packet->channel = get8u(s);
4177       act_comp |= packet->channel;
4178 
4179       if (at_eof(s)) {
4180           stbi_rewind( s );
4181           return 0;
4182       }
4183       if (packet->size != 8) {
4184           stbi_rewind( s );
4185           return 0;
4186       }
4187    } while (chained);
4188 
4189    *comp = (act_comp & 0x10 ? 4 : 3);
4190 
4191    return 1;
4192 }
4193 
stbi_info_main(stbi * s,int * x,int * y,int * comp)4194 static int stbi_info_main(stbi *s, int *x, int *y, int *comp)
4195 {
4196    if (stbi_jpeg_info(s, x, y, comp))
4197        return 1;
4198    if (stbi_png_info(s, x, y, comp))
4199        return 1;
4200    if (stbi_gif_info(s, x, y, comp))
4201        return 1;
4202    if (stbi_bmp_info(s, x, y, comp))
4203        return 1;
4204    if (stbi_psd_info(s, x, y, comp))
4205        return 1;
4206    if (stbi_pic_info(s, x, y, comp))
4207        return 1;
4208    #ifndef STBI_NO_HDR
4209    if (stbi_hdr_info(s, x, y, comp))
4210        return 1;
4211    #endif
4212    // test tga last because it's a crappy test!
4213    if (stbi_tga_info(s, x, y, comp))
4214        return 1;
4215    return e("unknown image type", "Image not of any known type, or corrupt");
4216 }
4217 
4218 #ifndef STBI_NO_STDIO
stbi_info(char const * filename,int * x,int * y,int * comp)4219 int stbi_info(char const *filename, int *x, int *y, int *comp)
4220 {
4221     FILE *f = fopen(filename, "rb");
4222     int result;
4223     if (!f) return e("can't fopen", "Unable to open file");
4224     result = stbi_info_from_file(f, x, y, comp);
4225     fclose(f);
4226     return result;
4227 }
4228 
stbi_info_from_file(FILE * f,int * x,int * y,int * comp)4229 int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
4230 {
4231    int r;
4232    stbi s;
4233    long pos = ftell(f);
4234    start_file(&s, f);
4235    r = stbi_info_main(&s,x,y,comp);
4236    fseek(f,pos,SEEK_SET);
4237    return r;
4238 }
4239 #endif // !STBI_NO_STDIO
4240 
stbi_info_from_memory(stbi_uc const * buffer,int len,int * x,int * y,int * comp)4241 int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
4242 {
4243    stbi s;
4244    start_mem(&s,buffer,len);
4245    return stbi_info_main(&s,x,y,comp);
4246 }
4247 
stbi_info_from_callbacks(stbi_io_callbacks const * c,void * user,int * x,int * y,int * comp)4248 int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
4249 {
4250    stbi s;
4251    start_callbacks(&s, (stbi_io_callbacks *) c, user);
4252    return stbi_info_main(&s,x,y,comp);
4253 }
4254 
4255 #endif // STBI_HEADER_FILE_ONLY
4256 
4257 /*
4258    revision history:
4259       1.33 (2011-07-14)
4260              make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
4261       1.32 (2011-07-13)
4262              support for "info" function for all supported filetypes (SpartanJ)
4263       1.31 (2011-06-20)
4264              a few more leak fixes, bug in PNG handling (SpartanJ)
4265       1.30 (2011-06-11)
4266              added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
4267              removed deprecated format-specific test/load functions
4268              removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
4269              error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
4270              fix inefficiency in decoding 32-bit BMP (David Woo)
4271       1.29 (2010-08-16)
4272              various warning fixes from Aurelien Pocheville
4273       1.28 (2010-08-01)
4274              fix bug in GIF palette transparency (SpartanJ)
4275       1.27 (2010-08-01)
4276              cast-to-uint8 to fix warnings
4277       1.26 (2010-07-24)
4278              fix bug in file buffering for PNG reported by SpartanJ
4279       1.25 (2010-07-17)
4280              refix trans_data warning (Won Chun)
4281       1.24 (2010-07-12)
4282              perf improvements reading from files on platforms with lock-heavy fgetc()
4283              minor perf improvements for jpeg
4284              deprecated type-specific functions so we'll get feedback if they're needed
4285              attempt to fix trans_data warning (Won Chun)
4286       1.23   fixed bug in iPhone support
4287       1.22 (2010-07-10)
4288              removed image *writing* support
4289              stbi_info support from Jetro Lauha
4290              GIF support from Jean-Marc Lienher
4291              iPhone PNG-extensions from James Brown
4292              warning-fixes from Nicolas Schulz and Janez Zemva (i.e. Janez (U+017D)emva)
4293       1.21   fix use of 'uint8' in header (reported by jon blow)
4294       1.20   added support for Softimage PIC, by Tom Seddon
4295       1.19   bug in interlaced PNG corruption check (found by ryg)
4296       1.18 2008-08-02
4297              fix a threading bug (local mutable static)
4298       1.17   support interlaced PNG
4299       1.16   major bugfix - convert_format converted one too many pixels
4300       1.15   initialize some fields for thread safety
4301       1.14   fix threadsafe conversion bug
4302              header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
4303       1.13   threadsafe
4304       1.12   const qualifiers in the API
4305       1.11   Support installable IDCT, colorspace conversion routines
4306       1.10   Fixes for 64-bit (don't use "unsigned long")
4307              optimized upsampling by Fabian "ryg" Giesen
4308       1.09   Fix format-conversion for PSD code (bad global variables!)
4309       1.08   Thatcher Ulrich's PSD code integrated by Nicolas Schulz
4310       1.07   attempt to fix C++ warning/errors again
4311       1.06   attempt to fix C++ warning/errors again
4312       1.05   fix TGA loading to return correct *comp and use good luminance calc
4313       1.04   default float alpha is 1, not 255; use 'void *' for stbi_image_free
4314       1.03   bugfixes to STBI_NO_STDIO, STBI_NO_HDR
4315       1.02   support for (subset of) HDR files, float interface for preferred access to them
4316       1.01   fix bug: possible bug in handling right-side up bmps... not sure
4317              fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
4318       1.00   interface to zlib that skips zlib header
4319       0.99   correct handling of alpha in palette
4320       0.98   TGA loader by lonesock; dynamically add loaders (untested)
4321       0.97   jpeg errors on too large a file; also catch another malloc failure
4322       0.96   fix detection of invalid v value - particleman@mollyrocket forum
4323       0.95   during header scan, seek to markers in case of padding
4324       0.94   STBI_NO_STDIO to disable stdio usage; rename all #defines the same
4325       0.93   handle jpegtran output; verbose errors
4326       0.92   read 4,8,16,24,32-bit BMP files of several formats
4327       0.91   output 24-bit Windows 3.0 BMP files
4328       0.90   fix a few more warnings; bump version number to approach 1.0
4329       0.61   bugfixes due to Marc LeBlanc, Christopher Lloyd
4330       0.60   fix compiling as c++
4331       0.59   fix warnings: merge Dave Moore's -Wall fixes
4332       0.58   fix bug: zlib uncompressed mode len/nlen was wrong endian
4333       0.57   fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
4334       0.56   fix bug: zlib uncompressed mode len vs. nlen
4335       0.55   fix bug: restart_interval not initialized to 0
4336       0.54   allow NULL for 'int *comp'
4337       0.53   fix bug in png 3->4; speedup png decoding
4338       0.52   png handles req_comp=3,4 directly; minor cleanup; jpeg comments
4339       0.51   obey req_comp requests, 1-component jpegs return as 1-component,
4340              on 'test' only check type, not whether we support this variant
4341       0.50   first released version
4342 */
4343