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