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