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