added stb, more binaryout changes"
[henge/apc.git] / stb / deprecated / stb_image.c
1 /* stb_image - v1.35 - 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 QUICK NOTES:
6 Primarily of interest to game developers and other people who can
7 avoid problematic images and only need the trivial interface
8
9 JPEG baseline (no JPEG progressive)
10 PNG 8-bit-per-channel only
11
12 TGA (not sure what subset, if a subset)
13 BMP non-1bpp, non-RLE
14 PSD (composited view only, no extra channels)
15
16 GIF (*comp always reports as 4-channel)
17 HDR (radiance rgbE format)
18 PIC (Softimage PIC)
19
20 - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
21 - decode from arbitrary I/O callbacks
22 - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
23
24 Latest revisions:
25 1.35 (2014-05-27) warnings, bugfixes, TGA optimization, etc
26 1.34 (unknown ) warning fix
27 1.33 (2011-07-14) minor fixes suggested by Dave Moore
28 1.32 (2011-07-13) info support for all filetypes (SpartanJ)
29 1.31 (2011-06-19) a few more leak fixes, bug in PNG handling (SpartanJ)
30 1.30 (2011-06-11) added ability to load files via io callbacks (Ben Wenger)
31 1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
32 1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
33
34 See end of file for full revision history.
35
36 TODO:
37 stbi_info support for BMP,PSD,HDR,PIC
38
39
40 ============================ Contributors =========================
41
42 Image formats Bug fixes & warning fixes
43 Sean Barrett (jpeg, png, bmp) Marc LeBlanc
44 Nicolas Schulz (hdr, psd) Christpher Lloyd
45 Jonathan Dummer (tga) Dave Moore
46 Jean-Marc Lienher (gif) Won Chun
47 Tom Seddon (pic) the Horde3D community
48 Thatcher Ulrich (psd) Janez Zemva
49 Jonathan Blow
50 Laurent Gomila
51 Extensions, features Aruelien Pocheville
52 Jetro Lauha (stbi_info) Ryamond Barbiero
53 James "moose2000" Brown (iPhone PNG) David Woo
54 Ben "Disch" Wenger (io callbacks) Roy Eltham
55 Martin "SpartanJ" Golini Luke Graham
56 Thomas Ruf
57 John Bartholomew
58 Optimizations & bugfixes Ken Hamada
59 Fabian "ryg" Giesen Cort Stratton
60 Arseny Kapoulkine Blazej Dariusz Roszkowski
61 Thibault Reuille
62 If your name should be here but Paul Du Bois
63 isn't let Sean know. Guillaume George
64
65 */
66
67 #ifndef STBI_INCLUDE_STB_IMAGE_H
68 #define STBI_INCLUDE_STB_IMAGE_H
69
70 // To get a header file for this, either cut and paste the header,
71 // or create stb_image.h, #define STBI_HEADER_FILE_ONLY, and
72 // then include stb_image.c from it.
73
74 //// begin header file ////////////////////////////////////////////////////
75 //
76 // Limitations:
77 // - no jpeg progressive support
78 // - non-HDR formats support 8-bit samples only (jpeg, png)
79 // - no delayed line count (jpeg) -- IJG doesn't support either
80 // - no 1-bit BMP
81 // - GIF always returns *comp=4
82 //
83 // Basic usage (see HDR discussion below):
84 // int x,y,n;
85 // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
86 // // ... process data if not NULL ...
87 // // ... x = width, y = height, n = # 8-bit components per pixel ...
88 // // ... replace '0' with '1'..'4' to force that many components per pixel
89 // // ... but 'n' will always be the number that it would have been if you said 0
90 // stbi_image_free(data)
91 //
92 // Standard parameters:
93 // int *x -- outputs image width in pixels
94 // int *y -- outputs image height in pixels
95 // int *comp -- outputs # of image components in image file
96 // int req_comp -- if non-zero, # of image components requested in result
97 //
98 // The return value from an image loader is an 'unsigned char *' which points
99 // to the pixel data. The pixel data consists of *y scanlines of *x pixels,
100 // with each pixel consisting of N interleaved 8-bit components; the first
101 // pixel pointed to is top-left-most in the image. There is no padding between
102 // image scanlines or between pixels, regardless of format. The number of
103 // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
104 // If req_comp is non-zero, *comp has the number of components that _would_
105 // have been output otherwise. E.g. if you set req_comp to 4, you will always
106 // get RGBA output, but you can check *comp to easily see if it's opaque.
107 //
108 // An output image with N components has the following components interleaved
109 // in this order in each pixel:
110 //
111 // N=#comp components
112 // 1 grey
113 // 2 grey, alpha
114 // 3 red, green, blue
115 // 4 red, green, blue, alpha
116 //
117 // If image loading fails for any reason, the return value will be NULL,
118 // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
119 // can be queried for an extremely brief, end-user unfriendly explanation
120 // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
121 // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
122 // more user-friendly ones.
123 //
124 // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
125 //
126 // ===========================================================================
127 //
128 // iPhone PNG support:
129 //
130 // By default we convert iphone-formatted PNGs back to RGB; nominally they
131 // would silently load as BGR, except the existing code should have just
132 // failed on such iPhone PNGs. But you can disable this conversion by
133 // by calling stbi_convert_iphone_png_to_rgb(0), in which case
134 // you will always just get the native iphone "format" through.
135 //
136 // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
137 // pixel to remove any premultiplied alpha *only* if the image file explicitly
138 // says there's premultiplied data (currently only happens in iPhone images,
139 // and only if iPhone convert-to-rgb processing is on).
140 //
141 // ===========================================================================
142 //
143 // HDR image support (disable by defining STBI_NO_HDR)
144 //
145 // stb_image now supports loading HDR images in general, and currently
146 // the Radiance .HDR file format, although the support is provided
147 // generically. You can still load any file through the existing interface;
148 // if you attempt to load an HDR file, it will be automatically remapped to
149 // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
150 // both of these constants can be reconfigured through this interface:
151 //
152 // stbi_hdr_to_ldr_gamma(2.2f);
153 // stbi_hdr_to_ldr_scale(1.0f);
154 //
155 // (note, do not use _inverse_ constants; stbi_image will invert them
156 // appropriately).
157 //
158 // Additionally, there is a new, parallel interface for loading files as
159 // (linear) floats to preserve the full dynamic range:
160 //
161 // float *data = stbi_loadf(filename, &x, &y, &n, 0);
162 //
163 // If you load LDR images through this interface, those images will
164 // be promoted to floating point values, run through the inverse of
165 // constants corresponding to the above:
166 //
167 // stbi_ldr_to_hdr_scale(1.0f);
168 // stbi_ldr_to_hdr_gamma(2.2f);
169 //
170 // Finally, given a filename (or an open file or memory block--see header
171 // file for details) containing image data, you can query for the "most
172 // appropriate" interface to use (that is, whether the image is HDR or
173 // not), using:
174 //
175 // stbi_is_hdr(char *filename);
176 //
177 // ===========================================================================
178 //
179 // I/O callbacks
180 //
181 // I/O callbacks allow you to read from arbitrary sources, like packaged
182 // files or some other source. Data read from callbacks are processed
183 // through a small internal buffer (currently 128 bytes) to try to reduce
184 // overhead.
185 //
186 // The three functions you must define are "read" (reads some bytes of data),
187 // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
188
189
190 #ifndef STBI_NO_STDIO
191
192 #if defined(_MSC_VER) && _MSC_VER >= 1400
193 #define _CRT_SECURE_NO_WARNINGS // suppress warnings about fopen()
194 #pragma warning(push)
195 #pragma warning(disable:4996) // suppress even more warnings about fopen()
196 #endif
197 #include <stdio.h>
198 #endif // STBI_NO_STDIO
199
200 #define STBI_VERSION 1
201
202 enum
203 {
204 STBI_default = 0, // only used for req_comp
205
206 STBI_grey = 1,
207 STBI_grey_alpha = 2,
208 STBI_rgb = 3,
209 STBI_rgb_alpha = 4
210 };
211
212 typedef unsigned char stbi_uc;
213
214 #ifdef __cplusplus
215 extern "C" {
216 #endif
217
218 //////////////////////////////////////////////////////////////////////////////
219 //
220 // PRIMARY API - works on images of any type
221 //
222
223 //
224 // load image by filename, open file, or memory buffer
225 //
226
227 extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
228
229 #ifndef STBI_NO_STDIO
230 extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
231 extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
232 // for stbi_load_from_file, file pointer is left pointing immediately after image
233 #endif
234
235 typedef struct
236 {
237 int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
238 void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
239 int (*eof) (void *user); // returns nonzero if we are at end of file/data
240 } stbi_io_callbacks;
241
242 extern stbi_uc *stbi_load_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
243
244 #ifndef STBI_NO_HDR
245 extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
246
247 #ifndef STBI_NO_STDIO
248 extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
249 extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
250 #endif
251
252 extern float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
253
254 extern void stbi_hdr_to_ldr_gamma(float gamma);
255 extern void stbi_hdr_to_ldr_scale(float scale);
256
257 extern void stbi_ldr_to_hdr_gamma(float gamma);
258 extern void stbi_ldr_to_hdr_scale(float scale);
259 #endif // STBI_NO_HDR
260
261 // stbi_is_hdr is always defined
262 extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
263 extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
264 #ifndef STBI_NO_STDIO
265 extern int stbi_is_hdr (char const *filename);
266 extern int stbi_is_hdr_from_file(FILE *f);
267 #endif // STBI_NO_STDIO
268
269
270 // get a VERY brief reason for failure
271 // NOT THREADSAFE
272 extern const char *stbi_failure_reason (void);
273
274 // free the loaded image -- this is just free()
275 extern void stbi_image_free (void *retval_from_stbi_load);
276
277 // get image dimensions & components without fully decoding
278 extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
279 extern int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
280
281 #ifndef STBI_NO_STDIO
282 extern int stbi_info (char const *filename, int *x, int *y, int *comp);
283 extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
284
285 #endif
286
287
288
289 // for image formats that explicitly notate that they have premultiplied alpha,
290 // we just return the colors as stored in the file. set this flag to force
291 // unpremultiplication. results are undefined if the unpremultiply overflow.
292 extern void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
293
294 // indicate whether we should process iphone images back to canonical format,
295 // or just pass them through "as-is"
296 extern void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
297
298
299 // ZLIB client - used by PNG, available for other purposes
300
301 extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
302 extern char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
303 extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
304 extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
305
306 extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
307 extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
308
309
310 // define faster low-level operations (typically SIMD support)
311 #ifdef STBI_SIMD
312 typedef void (*stbi_idct_8x8)(stbi_uc *out, int out_stride, short data[64], unsigned short *dequantize);
313 // compute an integer IDCT on "input"
314 // input[x] = data[x] * dequantize[x]
315 // write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
316 // CLAMP results to 0..255
317 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);
318 // compute a conversion from YCbCr to RGB
319 // 'count' pixels
320 // write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
321 // y: Y input channel
322 // cb: Cb input channel; scale/biased to be 0..255
323 // cr: Cr input channel; scale/biased to be 0..255
324
325 extern void stbi_install_idct(stbi_idct_8x8 func);
326 extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
327 #endif // STBI_SIMD
328
329
330 #ifdef __cplusplus
331 }
332 #endif
333
334 //
335 //
336 //// end header file /////////////////////////////////////////////////////
337 #endif // STBI_INCLUDE_STB_IMAGE_H
338
339 #ifndef STBI_HEADER_FILE_ONLY
340
341 #ifndef STBI_NO_HDR
342 #include <math.h> // ldexp
343 #include <string.h> // strcmp, strtok
344 #endif
345
346 #ifndef STBI_NO_STDIO
347 #include <stdio.h>
348 #endif
349 #include <stdlib.h>
350 #include <memory.h>
351 #include <assert.h>
352 #include <stdarg.h>
353 #include <stddef.h> // ptrdiff_t on osx
354
355 #ifndef _MSC_VER
356 #ifdef __cplusplus
357 #define stbi_inline inline
358 #else
359 #define stbi_inline
360 #endif
361 #else
362 #define stbi_inline __forceinline
363 #endif
364
365
366 #ifdef _MSC_VER
367 typedef unsigned char stbi__uint8;
368 typedef unsigned short stbi__uint16;
369 typedef signed short stbi__int16;
370 typedef unsigned int stbi__uint32;
371 typedef signed int stbi__int32;
372 #else
373 #include <stdint.h>
374 typedef uint8_t stbi__uint8;
375 typedef uint16_t stbi__uint16;
376 typedef int16_t stbi__int16;
377 typedef uint32_t stbi__uint32;
378 typedef int32_t stbi__int32;
379 #endif
380
381 // should produce compiler error if size is wrong
382 typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
383
384 #ifdef _MSC_VER
385 #define STBI_NOTUSED(v) (void)(v)
386 #else
387 #define STBI_NOTUSED(v) (void)sizeof(v)
388 #endif
389
390 #ifdef _MSC_VER
391 #define STBI_HAS_LROTL
392 #endif
393
394 #ifdef STBI_HAS_LROTL
395 #define stbi_lrot(x,y) _lrotl(x,y)
396 #else
397 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
398 #endif
399
400 ///////////////////////////////////////////////
401 //
402 // stbi struct and start_xxx functions
403
404 // stbi structure is our basic context used by all images, so it
405 // contains all the IO context, plus some basic image information
406 typedef struct
407 {
408 stbi__uint32 img_x, img_y;
409 int img_n, img_out_n;
410
411 stbi_io_callbacks io;
412 void *io_user_data;
413
414 int read_from_callbacks;
415 int buflen;
416 stbi__uint8 buffer_start[128];
417
418 stbi__uint8 *img_buffer, *img_buffer_end;
419 stbi__uint8 *img_buffer_original;
420 } stbi;
421
422
423 static void refill_buffer(stbi *s);
424
425 // initialize a memory-decode context
426 static void start_mem(stbi *s, stbi__uint8 const *buffer, int len)
427 {
428 s->io.read = NULL;
429 s->read_from_callbacks = 0;
430 s->img_buffer = s->img_buffer_original = (stbi__uint8 *) buffer;
431 s->img_buffer_end = (stbi__uint8 *) buffer+len;
432 }
433
434 // initialize a callback-based context
435 static void start_callbacks(stbi *s, stbi_io_callbacks *c, void *user)
436 {
437 s->io = *c;
438 s->io_user_data = user;
439 s->buflen = sizeof(s->buffer_start);
440 s->read_from_callbacks = 1;
441 s->img_buffer_original = s->buffer_start;
442 refill_buffer(s);
443 }
444
445 #ifndef STBI_NO_STDIO
446
447 static int stdio_read(void *user, char *data, int size)
448 {
449 return (int) fread(data,1,size,(FILE*) user);
450 }
451
452 static void stdio_skip(void *user, int n)
453 {
454 fseek((FILE*) user, n, SEEK_CUR);
455 }
456
457 static int stdio_eof(void *user)
458 {
459 return feof((FILE*) user);
460 }
461
462 static stbi_io_callbacks stbi_stdio_callbacks =
463 {
464 stdio_read,
465 stdio_skip,
466 stdio_eof,
467 };
468
469 static void start_file(stbi *s, FILE *f)
470 {
471 start_callbacks(s, &stbi_stdio_callbacks, (void *) f);
472 }
473
474 //static void stop_file(stbi *s) { }
475
476 #endif // !STBI_NO_STDIO
477
478 static void stbi_rewind(stbi *s)
479 {
480 // conceptually rewind SHOULD rewind to the beginning of the stream,
481 // but we just rewind to the beginning of the initial buffer, because
482 // we only use it after doing 'test', which only ever looks at at most 92 bytes
483 s->img_buffer = s->img_buffer_original;
484 }
485
486 static int stbi_jpeg_test(stbi *s);
487 static stbi_uc *stbi_jpeg_load(stbi *s, int *x, int *y, int *comp, int req_comp);
488 static int stbi_jpeg_info(stbi *s, int *x, int *y, int *comp);
489 static int stbi_png_test(stbi *s);
490 static stbi_uc *stbi_png_load(stbi *s, int *x, int *y, int *comp, int req_comp);
491 static int stbi_png_info(stbi *s, int *x, int *y, int *comp);
492 static int stbi_bmp_test(stbi *s);
493 static stbi_uc *stbi_bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp);
494 static int stbi_tga_test(stbi *s);
495 static stbi_uc *stbi_tga_load(stbi *s, int *x, int *y, int *comp, int req_comp);
496 static int stbi_tga_info(stbi *s, int *x, int *y, int *comp);
497 static int stbi_psd_test(stbi *s);
498 static stbi_uc *stbi_psd_load(stbi *s, int *x, int *y, int *comp, int req_comp);
499 #ifndef STBI_NO_HDR
500 static int stbi_hdr_test(stbi *s);
501 static float *stbi_hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp);
502 #endif
503 static int stbi_pic_test(stbi *s);
504 static stbi_uc *stbi_pic_load(stbi *s, int *x, int *y, int *comp, int req_comp);
505 static int stbi_gif_test(stbi *s);
506 static stbi_uc *stbi_gif_load(stbi *s, int *x, int *y, int *comp, int req_comp);
507 static int stbi_gif_info(stbi *s, int *x, int *y, int *comp);
508
509
510 // this is not threadsafe
511 static const char *failure_reason;
512
513 const char *stbi_failure_reason(void)
514 {
515 return failure_reason;
516 }
517
518 static int e(const char *str)
519 {
520 failure_reason = str;
521 return 0;
522 }
523
524 // e - error
525 // epf - error returning pointer to float
526 // epuc - error returning pointer to unsigned char
527
528 #ifdef STBI_NO_FAILURE_STRINGS
529 #define e(x,y) 0
530 #elif defined(STBI_FAILURE_USERMSG)
531 #define e(x,y) e(y)
532 #else
533 #define e(x,y) e(x)
534 #endif
535
536 #define epf(x,y) ((float *) (e(x,y)?NULL:NULL))
537 #define epuc(x,y) ((unsigned char *) (e(x,y)?NULL:NULL))
538
539 void stbi_image_free(void *retval_from_stbi_load)
540 {
541 free(retval_from_stbi_load);
542 }
543
544 #ifndef STBI_NO_HDR
545 static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
546 static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp);
547 #endif
548
549 static unsigned char *stbi_load_main(stbi *s, int *x, int *y, int *comp, int req_comp)
550 {
551 if (stbi_jpeg_test(s)) return stbi_jpeg_load(s,x,y,comp,req_comp);
552 if (stbi_png_test(s)) return stbi_png_load(s,x,y,comp,req_comp);
553 if (stbi_bmp_test(s)) return stbi_bmp_load(s,x,y,comp,req_comp);
554 if (stbi_gif_test(s)) return stbi_gif_load(s,x,y,comp,req_comp);
555 if (stbi_psd_test(s)) return stbi_psd_load(s,x,y,comp,req_comp);
556 if (stbi_pic_test(s)) return stbi_pic_load(s,x,y,comp,req_comp);
557
558 #ifndef STBI_NO_HDR
559 if (stbi_hdr_test(s)) {
560 float *hdr = stbi_hdr_load(s, x,y,comp,req_comp);
561 return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
562 }
563 #endif
564
565 // test tga last because it's a crappy test!
566 if (stbi_tga_test(s))
567 return stbi_tga_load(s,x,y,comp,req_comp);
568 return epuc("unknown image type", "Image not of any known type, or corrupt");
569 }
570
571 #ifndef STBI_NO_STDIO
572 unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
573 {
574 FILE *f = fopen(filename, "rb");
575 unsigned char *result;
576 if (!f) return epuc("can't fopen", "Unable to open file");
577 result = stbi_load_from_file(f,x,y,comp,req_comp);
578 fclose(f);
579 return result;
580 }
581
582 unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
583 {
584 unsigned char *result;
585 stbi s;
586 start_file(&s,f);
587 result = stbi_load_main(&s,x,y,comp,req_comp);
588 if (result) {
589 // need to 'unget' all the characters in the IO buffer
590 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
591 }
592 return result;
593 }
594 #endif //!STBI_NO_STDIO
595
596 unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
597 {
598 stbi s;
599 start_mem(&s,buffer,len);
600 return stbi_load_main(&s,x,y,comp,req_comp);
601 }
602
603 unsigned char *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
604 {
605 stbi s;
606 start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
607 return stbi_load_main(&s,x,y,comp,req_comp);
608 }
609
610 #ifndef STBI_NO_HDR
611
612 float *stbi_loadf_main(stbi *s, int *x, int *y, int *comp, int req_comp)
613 {
614 unsigned char *data;
615 #ifndef STBI_NO_HDR
616 if (stbi_hdr_test(s))
617 return stbi_hdr_load(s,x,y,comp,req_comp);
618 #endif
619 data = stbi_load_main(s, x, y, comp, req_comp);
620 if (data)
621 return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
622 return epf("unknown image type", "Image not of any known type, or corrupt");
623 }
624
625 float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
626 {
627 stbi s;
628 start_mem(&s,buffer,len);
629 return stbi_loadf_main(&s,x,y,comp,req_comp);
630 }
631
632 float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
633 {
634 stbi s;
635 start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
636 return stbi_loadf_main(&s,x,y,comp,req_comp);
637 }
638
639 #ifndef STBI_NO_STDIO
640 float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
641 {
642 FILE *f = fopen(filename, "rb");
643 float *result;
644 if (!f) return epf("can't fopen", "Unable to open file");
645 result = stbi_loadf_from_file(f,x,y,comp,req_comp);
646 fclose(f);
647 return result;
648 }
649
650 float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
651 {
652 stbi s;
653 start_file(&s,f);
654 return stbi_loadf_main(&s,x,y,comp,req_comp);
655 }
656 #endif // !STBI_NO_STDIO
657
658 #endif // !STBI_NO_HDR
659
660 // these is-hdr-or-not is defined independent of whether STBI_NO_HDR is
661 // defined, for API simplicity; if STBI_NO_HDR is defined, it always
662 // reports false!
663
664 int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
665 {
666 #ifndef STBI_NO_HDR
667 stbi s;
668 start_mem(&s,buffer,len);
669 return stbi_hdr_test(&s);
670 #else
671 STBI_NOTUSED(buffer);
672 STBI_NOTUSED(len);
673 return 0;
674 #endif
675 }
676
677 #ifndef STBI_NO_STDIO
678 extern int stbi_is_hdr (char const *filename)
679 {
680 FILE *f = fopen(filename, "rb");
681 int result=0;
682 if (f) {
683 result = stbi_is_hdr_from_file(f);
684 fclose(f);
685 }
686 return result;
687 }
688
689 extern int stbi_is_hdr_from_file(FILE *f)
690 {
691 #ifndef STBI_NO_HDR
692 stbi s;
693 start_file(&s,f);
694 return stbi_hdr_test(&s);
695 #else
696 return 0;
697 #endif
698 }
699 #endif // !STBI_NO_STDIO
700
701 extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
702 {
703 #ifndef STBI_NO_HDR
704 stbi s;
705 start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
706 return stbi_hdr_test(&s);
707 #else
708 return 0;
709 #endif
710 }
711
712 #ifndef STBI_NO_HDR
713 static float h2l_gamma_i=1.0f/2.2f, h2l_scale_i=1.0f;
714 static float l2h_gamma=2.2f, l2h_scale=1.0f;
715
716 void stbi_hdr_to_ldr_gamma(float gamma) { h2l_gamma_i = 1/gamma; }
717 void stbi_hdr_to_ldr_scale(float scale) { h2l_scale_i = 1/scale; }
718
719 void stbi_ldr_to_hdr_gamma(float gamma) { l2h_gamma = gamma; }
720 void stbi_ldr_to_hdr_scale(float scale) { l2h_scale = scale; }
721 #endif
722
723
724 //////////////////////////////////////////////////////////////////////////////
725 //
726 // Common code used by all image loaders
727 //
728
729 enum
730 {
731 SCAN_load=0,
732 SCAN_type,
733 SCAN_header
734 };
735
736 static void refill_buffer(stbi *s)
737 {
738 int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
739 if (n == 0) {
740 // at end of file, treat same as if from memory, but need to handle case
741 // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file
742 s->read_from_callbacks = 0;
743 s->img_buffer = s->buffer_start;
744 s->img_buffer_end = s->buffer_start+1;
745 *s->img_buffer = 0;
746 } else {
747 s->img_buffer = s->buffer_start;
748 s->img_buffer_end = s->buffer_start + n;
749 }
750 }
751
752 stbi_inline static int get8(stbi *s)
753 {
754 if (s->img_buffer < s->img_buffer_end)
755 return *s->img_buffer++;
756 if (s->read_from_callbacks) {
757 refill_buffer(s);
758 return *s->img_buffer++;
759 }
760 return 0;
761 }
762
763 stbi_inline static int at_eof(stbi *s)
764 {
765 if (s->io.read) {
766 if (!(s->io.eof)(s->io_user_data)) return 0;
767 // if feof() is true, check if buffer = end
768 // special case: we've only got the special 0 character at the end
769 if (s->read_from_callbacks == 0) return 1;
770 }
771
772 return s->img_buffer >= s->img_buffer_end;
773 }
774
775 stbi_inline static stbi__uint8 get8u(stbi *s)
776 {
777 return (stbi__uint8) get8(s);
778 }
779
780 static void skip(stbi *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 getn(stbi *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 get16(stbi *s)
818 {
819 int z = get8(s);
820 return (z << 8) + get8(s);
821 }
822
823 static stbi__uint32 get32(stbi *s)
824 {
825 stbi__uint32 z = get16(s);
826 return (z << 16) + get16(s);
827 }
828
829 static int get16le(stbi *s)
830 {
831 int z = get8(s);
832 return z + (get8(s) << 8);
833 }
834
835 static stbi__uint32 get32le(stbi *s)
836 {
837 stbi__uint32 z = get16le(s);
838 return z + (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 (e.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__uint8 compute_y(int r, int g, int b)
853 {
854 return (stbi__uint8) (((r*77) + (g*150) + (29*b)) >> 8);
855 }
856
857 static unsigned char *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 epuc("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]=compute_y(src[0],src[1],src[2]); break;
888 CASE(3,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
889 CASE(4,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
890 CASE(4,2) dest[0]=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 *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 epf("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, l2h_gamma) * 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 float2int(x) ((int) (x))
920 static stbi_uc *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 epuc("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]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f;
930 if (z < 0) z = 0;
931 if (z > 255) z = 255;
932 output[i*comp + k] = (stbi__uint8) 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__uint8) 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__uint8 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__uint8 values[256];
982 stbi__uint8 size[257];
983 unsigned int maxcode[18];
984 int delta[17]; // old 'firstsymbol' - old 'firstcode'
985 } huffman;
986
987 typedef struct
988 {
989 #ifdef STBI_SIMD
990 unsigned short dequant2[4][64];
991 #endif
992 stbi *s;
993 huffman huff_dc[4];
994 huffman huff_ac[4];
995 stbi__uint8 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__uint8 *data;
1013 void *raw_data;
1014 stbi__uint8 *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 } jpeg;
1025
1026 static int build_huffman(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__uint8) (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 e("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__uint8) i;
1061 }
1062 }
1063 }
1064 return 1;
1065 }
1066
1067 static void grow_buffer_unsafe(jpeg *j)
1068 {
1069 do {
1070 int b = j->nomore ? 0 : get8(j->s);
1071 if (b == 0xff) {
1072 int c = 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 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 decode(jpeg *j, huffman *h)
1089 {
1090 unsigned int temp;
1091 int c,k;
1092
1093 if (j->code_bits < 16) 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)) & bmask[k]) + h->delta[k];
1129 assert((((j->code_buffer) >> (32 - h->size[c])) & 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 extend_receive(jpeg *j, int n)
1140 {
1141 unsigned int m = 1 << (n-1);
1142 unsigned int k;
1143 if (j->code_bits < n) grow_buffer_unsafe(j);
1144
1145 #if 1
1146 k = stbi_lrot(j->code_buffer, n);
1147 j->code_buffer = k & ~bmask[n];
1148 k &= bmask[n];
1149 j->code_bits -= n;
1150 #else
1151 k = (j->code_buffer >> (32 - n)) & 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__uint8 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 decode_block(jpeg *j, short data[64], huffman *hdc, huffman *hac, int b)
1183 {
1184 int diff,dc,k;
1185 int t = decode(j, hdc);
1186 if (t < 0) return e("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 ? 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 = decode(j, hac);
1201 if (rs < 0) return e("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[dezigzag[k++]] = (short) extend_receive(j,s);
1211 }
1212 } while (k < 64);
1213 return 1;
1214 }
1215
1216 // take a -128..127 value and clamp it and convert to 0..255
1217 stbi_inline static stbi__uint8 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__uint8) x;
1225 }
1226
1227 #define f2f(x) (int) (((x) * 4096 + 0.5))
1228 #define fsh(x) ((x) << 12)
1229
1230 // derived from jidctint -- DCT_ISLOW
1231 #define 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) * f2f(0.5411961f); \
1236 t2 = p1 + p3*f2f(-1.847759065f); \
1237 t3 = p1 + p2*f2f( 0.765366865f); \
1238 p2 = s0; \
1239 p3 = s4; \
1240 t0 = fsh(p2+p3); \
1241 t1 = 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)*f2f( 1.175875602f); \
1255 t0 = t0*f2f( 0.298631336f); \
1256 t1 = t1*f2f( 2.053119869f); \
1257 t2 = t2*f2f( 3.072711026f); \
1258 t3 = t3*f2f( 1.501321110f); \
1259 p1 = p5 + p1*f2f(-0.899976223f); \
1260 p2 = p5 + p2*f2f(-2.562915447f); \
1261 p3 = p3*f2f(-1.961570560f); \
1262 p4 = p4*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__uint8 stbi_dequantize_t;
1272 #endif
1273
1274 // .344 seconds on 3*anemones.jpg
1275 static void idct_block(stbi__uint8 *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__uint8 *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 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 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] = clamp((x0+t3) >> 17);
1326 o[7] = clamp((x0-t3) >> 17);
1327 o[1] = clamp((x1+t2) >> 17);
1328 o[6] = clamp((x1-t2) >> 17);
1329 o[2] = clamp((x2+t1) >> 17);
1330 o[5] = clamp((x2-t1) >> 17);
1331 o[3] = clamp((x3+t0) >> 17);
1332 o[4] = clamp((x3-t0) >> 17);
1333 }
1334 }
1335
1336 #ifdef STBI_SIMD
1337 static stbi_idct_8x8 stbi_idct_installed = idct_block;
1338
1339 void stbi_install_idct(stbi_idct_8x8 func)
1340 {
1341 stbi_idct_installed = func;
1342 }
1343 #endif
1344
1345 #define 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__uint8 get_marker(jpeg *j)
1350 {
1351 stbi__uint8 x;
1352 if (j->marker != MARKER_none) { x = j->marker; j->marker = MARKER_none; return x; }
1353 x = get8u(j->s);
1354 if (x != 0xff) return MARKER_none;
1355 while (x == 0xff)
1356 x = get8u(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 RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
1363
1364 // after a restart interval, reset the entropy decoder and
1365 // the dc prediction
1366 static void reset(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 = 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 parse_entropy_coded_data(jpeg *z)
1379 {
1380 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 (!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 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) 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 (!RESTART(z->marker)) return 1;
1408 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 (!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 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) 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 (!RESTART(z->marker)) return 1;
1442 reset(z);
1443 }
1444 }
1445 }
1446 }
1447 return 1;
1448 }
1449
1450 static int process_marker(jpeg *z, int m)
1451 {
1452 int L;
1453 switch (m) {
1454 case MARKER_none: // no marker found
1455 return e("expected marker","Corrupt JPEG");
1456
1457 case 0xC2: // SOF - progressive
1458 return e("progressive jpeg","JPEG format not supported (progressive)");
1459
1460 case 0xDD: // DRI - specify restart interval
1461 if (get16(z->s) != 4) return e("bad DRI len","Corrupt JPEG");
1462 z->restart_interval = get16(z->s);
1463 return 1;
1464
1465 case 0xDB: // DQT - define quantization table
1466 L = get16(z->s)-2;
1467 while (L > 0) {
1468 int q = get8(z->s);
1469 int p = q >> 4;
1470 int t = q & 15,i;
1471 if (p != 0) return e("bad DQT type","Corrupt JPEG");
1472 if (t > 3) return e("bad DQT table","Corrupt JPEG");
1473 for (i=0; i < 64; ++i)
1474 z->dequant[t][dezigzag[i]] = get8u(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 = get16(z->s)-2;
1485 while (L > 0) {
1486 stbi__uint8 *v;
1487 int sizes[16],i,n=0;
1488 int q = get8(z->s);
1489 int tc = q >> 4;
1490 int th = q & 15;
1491 if (tc > 1 || th > 3) return e("bad DHT header","Corrupt JPEG");
1492 for (i=0; i < 16; ++i) {
1493 sizes[i] = get8(z->s);
1494 n += sizes[i];
1495 }
1496 L -= 17;
1497 if (tc == 0) {
1498 if (!build_huffman(z->huff_dc+th, sizes)) return 0;
1499 v = z->huff_dc[th].values;
1500 } else {
1501 if (!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] = get8u(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 skip(z->s, get16(z->s)-2);
1513 return 1;
1514 }
1515 return 0;
1516 }
1517
1518 // after we see SOS
1519 static int process_scan_header(jpeg *z)
1520 {
1521 int i;
1522 int Ls = get16(z->s);
1523 z->scan_n = get8(z->s);
1524 if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return e("bad SOS component count","Corrupt JPEG");
1525 if (Ls != 6+2*z->scan_n) return e("bad SOS len","Corrupt JPEG");
1526 for (i=0; i < z->scan_n; ++i) {
1527 int id = get8(z->s), which;
1528 int q = 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 e("bad DC huff","Corrupt JPEG");
1534 z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return e("bad AC huff","Corrupt JPEG");
1535 z->order[i] = which;
1536 }
1537 if (get8(z->s) != 0) return e("bad SOS","Corrupt JPEG");
1538 get8(z->s); // should be 63, but might be 0
1539 if (get8(z->s) != 0) return e("bad SOS","Corrupt JPEG");
1540
1541 return 1;
1542 }
1543
1544 static int process_frame_header(jpeg *z, int scan)
1545 {
1546 stbi *s = z->s;
1547 int Lf,p,i,q, h_max=1,v_max=1,c;
1548 Lf = get16(s); if (Lf < 11) return e("bad SOF len","Corrupt JPEG"); // JPEG
1549 p = get8(s); if (p != 8) return e("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
1550 s->img_y = get16(s); if (s->img_y == 0) return e("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
1551 s->img_x = get16(s); if (s->img_x == 0) return e("0 width","Corrupt JPEG"); // JPEG requires
1552 c = get8(s);
1553 if (c != 3 && c != 1) return e("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 e("bad SOF len","Corrupt JPEG");
1561
1562 for (i=0; i < s->img_n; ++i) {
1563 z->img_comp[i].id = 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 e("bad component ID","Corrupt JPEG");
1567 q = get8(s);
1568 z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return e("bad H","Corrupt JPEG");
1569 z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return e("bad V","Corrupt JPEG");
1570 z->img_comp[i].tq = get8(s); if (z->img_comp[i].tq > 3) return e("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 e("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 (e.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 (e.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 e("outofmem", "Out of memory");
1607 }
1608 // align blocks for installable-idct using mmx/sse
1609 z->img_comp[i].data = (stbi__uint8*) (((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 (e.g. SOF)
1617 #define DNL(x) ((x) == 0xdc)
1618 #define SOI(x) ((x) == 0xd8)
1619 #define EOI(x) ((x) == 0xd9)
1620 #define SOF(x) ((x) == 0xc0 || (x) == 0xc1)
1621 #define SOS(x) ((x) == 0xda)
1622
1623 static int decode_jpeg_header(jpeg *z, int scan)
1624 {
1625 int m;
1626 z->marker = MARKER_none; // initialize cached marker to empty
1627 m = get_marker(z);
1628 if (!SOI(m)) return e("no SOI","Corrupt JPEG");
1629 if (scan == SCAN_type) return 1;
1630 m = get_marker(z);
1631 while (!SOF(m)) {
1632 if (!process_marker(z,m)) return 0;
1633 m = get_marker(z);
1634 while (m == MARKER_none) {
1635 // some files have extra padding after their blocks, so ok, we'll scan
1636 if (at_eof(z->s)) return e("no SOF", "Corrupt JPEG");
1637 m = get_marker(z);
1638 }
1639 }
1640 if (!process_frame_header(z, scan)) return 0;
1641 return 1;
1642 }
1643
1644 static int decode_jpeg_image(jpeg *j)
1645 {
1646 int m;
1647 j->restart_interval = 0;
1648 if (!decode_jpeg_header(j, SCAN_load)) return 0;
1649 m = get_marker(j);
1650 while (!EOI(m)) {
1651 if (SOS(m)) {
1652 if (!process_scan_header(j)) return 0;
1653 if (!parse_entropy_coded_data(j)) return 0;
1654 if (j->marker == MARKER_none ) {
1655 // handle 0s at the end of image data from IP Kamera 9060
1656 while (!at_eof(j->s)) {
1657 int x = get8(j->s);
1658 if (x == 255) {
1659 j->marker = get8u(j->s);
1660 break;
1661 } else if (x != 0) {
1662 return 0;
1663 }
1664 }
1665 // if we reach eof without hitting a marker, get_marker() below will fail and we'll eventually return 0
1666 }
1667 } else {
1668 if (!process_marker(j, m)) return 0;
1669 }
1670 m = get_marker(j);
1671 }
1672 return 1;
1673 }
1674
1675 // static jfif-centered resampling (across block boundaries)
1676
1677 typedef stbi__uint8 *(*resample_row_func)(stbi__uint8 *out, stbi__uint8 *in0, stbi__uint8 *in1,
1678 int w, int hs);
1679
1680 #define div4(x) ((stbi__uint8) ((x) >> 2))
1681
1682 static stbi__uint8 *resample_row_1(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *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__uint8* resample_row_v_2(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *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] = div4(3*in_near[i] + in_far[i] + 2);
1698 return out;
1699 }
1700
1701 static stbi__uint8* resample_row_h_2(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *in_far, int w, int hs)
1702 {
1703 // need to generate two samples horizontally for every one in input
1704 int i;
1705 stbi__uint8 *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] = 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] = div4(n+input[i-1]);
1718 out[i*2+1] = div4(n+input[i+1]);
1719 }
1720 out[i*2+0] = 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 div16(x) ((stbi__uint8) ((x) >> 4))
1730
1731 static stbi__uint8 *resample_row_hv_2(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *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] = 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] = 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] = div16(3*t0 + t1 + 8);
1746 out[i*2 ] = div16(3*t1 + t0 + 8);
1747 }
1748 out[w*2-1] = div4(t1+2);
1749
1750 STBI_NOTUSED(hs);
1751
1752 return out;
1753 }
1754
1755 static stbi__uint8 *resample_row_generic(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *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 YCbCr_to_RGB_row(stbi__uint8 *out, const stbi__uint8 *y, const stbi__uint8 *pcb, const stbi__uint8 *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__uint8)r;
1788 out[1] = (stbi__uint8)g;
1789 out[2] = (stbi__uint8)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 = YCbCr_to_RGB_row;
1797
1798 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 cleanup_jpeg(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__uint8 *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__uint8 *load_jpeg_image(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 epuc("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)) { 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__uint8 *output;
1854 stbi__uint8 *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__uint8 *) malloc(z->s->img_x + 3);
1864 if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("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 = resample_row_v_2;
1875 else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2;
1876 else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2;
1877 else r->resample = resample_row_generic;
1878 }
1879
1880 // can't error after this so, this is safe
1881 output = (stbi__uint8 *) malloc(n * z->s->img_x * z->s->img_y + 1);
1882 if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
1883
1884 // now go ahead and resample
1885 for (j=0; j < z->s->img_y; ++j) {
1886 stbi__uint8 *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__uint8 *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 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__uint8 *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 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 *s, int *x, int *y, int *comp, int req_comp)
1932 {
1933 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 *s)
1939 {
1940 int r;
1941 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(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 *s, int *x, int *y, int *comp)
1961 {
1962 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 ZFAST_BITS 9 // accelerate all cases in default tables
1976 #define ZFAST_MASK ((1 << 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 << ZFAST_BITS];
1983 stbi__uint16 firstcode[16];
1984 int maxcode[17];
1985 stbi__uint16 firstsymbol[16];
1986 stbi__uint8 size[288];
1987 stbi__uint16 value[288];
1988 } zhuffman;
1989
1990 stbi_inline static int 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 bit_reverse(int v, int bits)
2000 {
2001 assert(bits <= 16);
2002 // to bit reverse n bits, reverse 16 and shift
2003 // e.g. 11 bits, bit reverse and shift away 5
2004 return bitreverse16(v) >> (16-bits);
2005 }
2006
2007 static int zbuild_huffman(zhuffman *z, stbi__uint8 *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 e("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__uint8)s;
2038 z->value[c] = (stbi__uint16)i;
2039 if (s <= ZFAST_BITS) {
2040 int k = bit_reverse(next_code[s],s);
2041 while (k < (1 << 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__uint8 *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 zhuffman z_length, z_distance;
2070 } zbuf;
2071
2072 stbi_inline static int zget8(zbuf *z)
2073 {
2074 if (z->zbuffer >= z->zbuffer_end) return 0;
2075 return *z->zbuffer++;
2076 }
2077
2078 static void fill_bits(zbuf *z)
2079 {
2080 do {
2081 assert(z->code_buffer < (1U << z->num_bits));
2082 z->code_buffer |= zget8(z) << z->num_bits;
2083 z->num_bits += 8;
2084 } while (z->num_bits <= 24);
2085 }
2086
2087 stbi_inline static unsigned int zreceive(zbuf *z, int n)
2088 {
2089 unsigned int k;
2090 if (z->num_bits < n) 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 zhuffman_decode(zbuf *a, zhuffman *z)
2098 {
2099 int b,s,k;
2100 if (a->num_bits < 16) fill_bits(a);
2101 b = z->fast[a->code_buffer & 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 = bit_reverse(a->code_buffer, 16);
2112 for (s=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 expand(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 e("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 e("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 length_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 length_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 dist_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 dist_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 parse_huffman_block(zbuf *a)
2156 {
2157 for(;;) {
2158 int z = zhuffman_decode(a, &a->z_length);
2159 if (z < 256) {
2160 if (z < 0) return e("bad huffman code","Corrupt PNG"); // error in huffman codes
2161 if (a->zout >= a->zout_end) if (!expand(a, 1)) return 0;
2162 *a->zout++ = (char) z;
2163 } else {
2164 stbi__uint8 *p;
2165 int len,dist;
2166 if (z == 256) return 1;
2167 z -= 257;
2168 len = length_base[z];
2169 if (length_extra[z]) len += zreceive(a, length_extra[z]);
2170 z = zhuffman_decode(a, &a->z_distance);
2171 if (z < 0) return e("bad huffman code","Corrupt PNG");
2172 dist = dist_base[z];
2173 if (dist_extra[z]) dist += zreceive(a, dist_extra[z]);
2174 if (a->zout - a->zout_start < dist) return e("bad dist","Corrupt PNG");
2175 if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0;
2176 p = (stbi__uint8 *) (a->zout - dist);
2177 while (len--)
2178 *a->zout++ = *p++;
2179 }
2180 }
2181 }
2182
2183 static int compute_huffman_codes(zbuf *a)
2184 {
2185 static stbi__uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
2186 zhuffman z_codelength;
2187 stbi__uint8 lencodes[286+32+137];//padding for maximum single op
2188 stbi__uint8 codelength_sizes[19];
2189 int i,n;
2190
2191 int hlit = zreceive(a,5) + 257;
2192 int hdist = zreceive(a,5) + 1;
2193 int hclen = zreceive(a,4) + 4;
2194
2195 memset(codelength_sizes, 0, sizeof(codelength_sizes));
2196 for (i=0; i < hclen; ++i) {
2197 int s = zreceive(a,3);
2198 codelength_sizes[length_dezigzag[i]] = (stbi__uint8) s;
2199 }
2200 if (!zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
2201
2202 n = 0;
2203 while (n < hlit + hdist) {
2204 int c = zhuffman_decode(a, &z_codelength);
2205 assert(c >= 0 && c < 19);
2206 if (c < 16)
2207 lencodes[n++] = (stbi__uint8) c;
2208 else if (c == 16) {
2209 c = zreceive(a,2)+3;
2210 memset(lencodes+n, lencodes[n-1], c);
2211 n += c;
2212 } else if (c == 17) {
2213 c = zreceive(a,3)+3;
2214 memset(lencodes+n, 0, c);
2215 n += c;
2216 } else {
2217 assert(c == 18);
2218 c = zreceive(a,7)+11;
2219 memset(lencodes+n, 0, c);
2220 n += c;
2221 }
2222 }
2223 if (n != hlit+hdist) return e("bad codelengths","Corrupt PNG");
2224 if (!zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
2225 if (!zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
2226 return 1;
2227 }
2228
2229 static int parse_uncompressed_block(zbuf *a)
2230 {
2231 stbi__uint8 header[4];
2232 int len,nlen,k;
2233 if (a->num_bits & 7)
2234 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__uint8) (a->code_buffer & 255); // wtf this warns?
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__uint8) zget8(a);
2246 len = header[1] * 256 + header[0];
2247 nlen = header[3] * 256 + header[2];
2248 if (nlen != (len ^ 0xffff)) return e("zlib corrupt","Corrupt PNG");
2249 if (a->zbuffer + len > a->zbuffer_end) return e("read past buffer","Corrupt PNG");
2250 if (a->zout + len > a->zout_end)
2251 if (!expand(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 parse_zlib_header(zbuf *a)
2259 {
2260 int cmf = zget8(a);
2261 int cm = cmf & 15;
2262 /* int cinfo = cmf >> 4; */
2263 int flg = zget8(a);
2264 if ((cmf*256+flg) % 31 != 0) return e("bad zlib header","Corrupt PNG"); // zlib spec
2265 if (flg & 32) return e("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
2266 if (cm != 8) return e("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__uint8 default_length[288], default_distance[32];
2273 static void init_defaults(void)
2274 {
2275 int i; // use <= to match clearly with spec
2276 for (i=0; i <= 143; ++i) default_length[i] = 8;
2277 for ( ; i <= 255; ++i) default_length[i] = 9;
2278 for ( ; i <= 279; ++i) default_length[i] = 7;
2279 for ( ; i <= 287; ++i) default_length[i] = 8;
2280
2281 for (i=0; i <= 31; ++i) default_distance[i] = 5;
2282 }
2283
2284 int stbi_png_partial; // a quick hack to only allow decoding some of a PNG... I should implement real streaming support instead
2285 static int parse_zlib(zbuf *a, int parse_header)
2286 {
2287 int final, type;
2288 if (parse_header)
2289 if (!parse_zlib_header(a)) return 0;
2290 a->num_bits = 0;
2291 a->code_buffer = 0;
2292 do {
2293 final = zreceive(a,1);
2294 type = zreceive(a,2);
2295 if (type == 0) {
2296 if (!parse_uncompressed_block(a)) return 0;
2297 } else if (type == 3) {
2298 return 0;
2299 } else {
2300 if (type == 1) {
2301 // use fixed code lengths
2302 if (!default_distance[31]) init_defaults();
2303 if (!zbuild_huffman(&a->z_length , default_length , 288)) return 0;
2304 if (!zbuild_huffman(&a->z_distance, default_distance, 32)) return 0;
2305 } else {
2306 if (!compute_huffman_codes(a)) return 0;
2307 }
2308 if (!parse_huffman_block(a)) return 0;
2309 }
2310 if (stbi_png_partial && a->zout - a->zout_start > 65536)
2311 break;
2312 } while (!final);
2313 return 1;
2314 }
2315
2316 static int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header)
2317 {
2318 a->zout_start = obuf;
2319 a->zout = obuf;
2320 a->zout_end = obuf + olen;
2321 a->z_expandable = exp;
2322
2323 return parse_zlib(a, parse_header);
2324 }
2325
2326 char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
2327 {
2328 zbuf a;
2329 char *p = (char *) malloc(initial_size);
2330 if (p == NULL) return NULL;
2331 a.zbuffer = (stbi__uint8 *) buffer;
2332 a.zbuffer_end = (stbi__uint8 *) buffer + len;
2333 if (do_zlib(&a, p, initial_size, 1, 1)) {
2334 if (outlen) *outlen = (int) (a.zout - a.zout_start);
2335 return a.zout_start;
2336 } else {
2337 free(a.zout_start);
2338 return NULL;
2339 }
2340 }
2341
2342 char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
2343 {
2344 return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
2345 }
2346
2347 char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
2348 {
2349 zbuf a;
2350 char *p = (char *) malloc(initial_size);
2351 if (p == NULL) return NULL;
2352 a.zbuffer = (stbi__uint8 *) buffer;
2353 a.zbuffer_end = (stbi__uint8 *) buffer + len;
2354 if (do_zlib(&a, p, initial_size, 1, parse_header)) {
2355 if (outlen) *outlen = (int) (a.zout - a.zout_start);
2356 return a.zout_start;
2357 } else {
2358 free(a.zout_start);
2359 return NULL;
2360 }
2361 }
2362
2363 int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
2364 {
2365 zbuf a;
2366 a.zbuffer = (stbi__uint8 *) ibuffer;
2367 a.zbuffer_end = (stbi__uint8 *) ibuffer + ilen;
2368 if (do_zlib(&a, obuffer, olen, 0, 1))
2369 return (int) (a.zout - a.zout_start);
2370 else
2371 return -1;
2372 }
2373
2374 char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
2375 {
2376 zbuf a;
2377 char *p = (char *) malloc(16384);
2378 if (p == NULL) return NULL;
2379 a.zbuffer = (stbi__uint8 *) buffer;
2380 a.zbuffer_end = (stbi__uint8 *) buffer+len;
2381 if (do_zlib(&a, p, 16384, 1, 0)) {
2382 if (outlen) *outlen = (int) (a.zout - a.zout_start);
2383 return a.zout_start;
2384 } else {
2385 free(a.zout_start);
2386 return NULL;
2387 }
2388 }
2389
2390 int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
2391 {
2392 zbuf a;
2393 a.zbuffer = (stbi__uint8 *) ibuffer;
2394 a.zbuffer_end = (stbi__uint8 *) ibuffer + ilen;
2395 if (do_zlib(&a, obuffer, olen, 0, 0))
2396 return (int) (a.zout - a.zout_start);
2397 else
2398 return -1;
2399 }
2400
2401 // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18
2402 // simple implementation
2403 // - only 8-bit samples
2404 // - no CRC checking
2405 // - allocates lots of intermediate memory
2406 // - avoids problem of streaming data between subsystems
2407 // - avoids explicit window management
2408 // performance
2409 // - uses stb_zlib, a PD zlib implementation with fast huffman decoding
2410
2411
2412 typedef struct
2413 {
2414 stbi__uint32 length;
2415 stbi__uint32 type;
2416 } chunk;
2417
2418 #define PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
2419
2420 static chunk get_chunk_header(stbi *s)
2421 {
2422 chunk c;
2423 c.length = get32(s);
2424 c.type = get32(s);
2425 return c;
2426 }
2427
2428 static int check_png_header(stbi *s)
2429 {
2430 static stbi__uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 };
2431 int i;
2432 for (i=0; i < 8; ++i)
2433 if (get8u(s) != png_sig[i]) return e("bad png sig","Not a PNG");
2434 return 1;
2435 }
2436
2437 typedef struct
2438 {
2439 stbi *s;
2440 stbi__uint8 *idata, *expanded, *out;
2441 } png;
2442
2443
2444 enum {
2445 F_none=0, F_sub=1, F_up=2, F_avg=3, F_paeth=4,
2446 F_avg_first, F_paeth_first
2447 };
2448
2449 static stbi__uint8 first_row_filter[5] =
2450 {
2451 F_none, F_sub, F_none, F_avg_first, F_paeth_first
2452 };
2453
2454 static int paeth(int a, int b, int c)
2455 {
2456 int p = a + b - c;
2457 int pa = abs(p-a);
2458 int pb = abs(p-b);
2459 int pc = abs(p-c);
2460 if (pa <= pb && pa <= pc) return a;
2461 if (pb <= pc) return b;
2462 return c;
2463 }
2464
2465 // create the png data from post-deflated data
2466 static int create_png_image_raw(png *a, stbi__uint8 *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y)
2467 {
2468 stbi *s = a->s;
2469 stbi__uint32 i,j,stride = x*out_n;
2470 int k;
2471 int img_n = s->img_n; // copy it into a local for later
2472 assert(out_n == s->img_n || out_n == s->img_n+1);
2473 if (stbi_png_partial) y = 1;
2474 a->out = (stbi__uint8 *) malloc(x * y * out_n);
2475 if (!a->out) return e("outofmem", "Out of memory");
2476 if (!stbi_png_partial) {
2477 if (s->img_x == x && s->img_y == y) {
2478 if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
2479 } else { // interlaced:
2480 if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
2481 }
2482 }
2483 for (j=0; j < y; ++j) {
2484 stbi__uint8 *cur = a->out + stride*j;
2485 stbi__uint8 *prior = cur - stride;
2486 int filter = *raw++;
2487 if (filter > 4) return e("invalid filter","Corrupt PNG");
2488 // if first row, use special filter that doesn't sample previous row
2489 if (j == 0) filter = first_row_filter[filter];
2490 // handle first pixel explicitly
2491 for (k=0; k < img_n; ++k) {
2492 switch (filter) {
2493 case F_none : cur[k] = raw[k]; break;
2494 case F_sub : cur[k] = raw[k]; break;
2495 case F_up : cur[k] = raw[k] + prior[k]; break;
2496 case F_avg : cur[k] = raw[k] + (prior[k]>>1); break;
2497 case F_paeth : cur[k] = (stbi__uint8) (raw[k] + paeth(0,prior[k],0)); break;
2498 case F_avg_first : cur[k] = raw[k]; break;
2499 case F_paeth_first: cur[k] = raw[k]; break;
2500 }
2501 }
2502 if (img_n != out_n) cur[img_n] = 255;
2503 raw += img_n;
2504 cur += out_n;
2505 prior += out_n;
2506 // this is a little gross, so that we don't switch per-pixel or per-component
2507 if (img_n == out_n) {
2508 #define CASE(f) \
2509 case f: \
2510 for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \
2511 for (k=0; k < img_n; ++k)
2512 switch (filter) {
2513 CASE(F_none) cur[k] = raw[k]; break;
2514 CASE(F_sub) cur[k] = raw[k] + cur[k-img_n]; break;
2515 CASE(F_up) cur[k] = raw[k] + prior[k]; break;
2516 CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-img_n])>>1); break;
2517 CASE(F_paeth) cur[k] = (stbi__uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;
2518 CASE(F_avg_first) cur[k] = raw[k] + (cur[k-img_n] >> 1); break;
2519 CASE(F_paeth_first) cur[k] = (stbi__uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break;
2520 }
2521 #undef CASE
2522 } else {
2523 assert(img_n+1 == out_n);
2524 #define CASE(f) \
2525 case f: \
2526 for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
2527 for (k=0; k < img_n; ++k)
2528 switch (filter) {
2529 CASE(F_none) cur[k] = raw[k]; break;
2530 CASE(F_sub) cur[k] = raw[k] + cur[k-out_n]; break;
2531 CASE(F_up) cur[k] = raw[k] + prior[k]; break;
2532 CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-out_n])>>1); break;
2533 CASE(F_paeth) cur[k] = (stbi__uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
2534 CASE(F_avg_first) cur[k] = raw[k] + (cur[k-out_n] >> 1); break;
2535 CASE(F_paeth_first) cur[k] = (stbi__uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break;
2536 }
2537 #undef CASE
2538 }
2539 }
2540 return 1;
2541 }
2542
2543 static int create_png_image(png *a, stbi__uint8 *raw, stbi__uint32 raw_len, int out_n, int interlaced)
2544 {
2545 stbi__uint8 *final;
2546 int p;
2547 int save;
2548 if (!interlaced)
2549 return create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y);
2550 save = stbi_png_partial;
2551 stbi_png_partial = 0;
2552
2553 // de-interlacing
2554 final = (stbi__uint8 *) malloc(a->s->img_x * a->s->img_y * out_n);
2555 for (p=0; p < 7; ++p) {
2556 int xorig[] = { 0,4,0,2,0,1,0 };
2557 int yorig[] = { 0,0,4,0,2,0,1 };
2558 int xspc[] = { 8,8,4,4,2,2,1 };
2559 int yspc[] = { 8,8,8,4,4,2,2 };
2560 int i,j,x,y;
2561 // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
2562 x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
2563 y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
2564 if (x && y) {
2565 if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
2566 free(final);
2567 return 0;
2568 }
2569 for (j=0; j < y; ++j)
2570 for (i=0; i < x; ++i)
2571 memcpy(final + (j*yspc[p]+yorig[p])*a->s->img_x*out_n + (i*xspc[p]+xorig[p])*out_n,
2572 a->out + (j*x+i)*out_n, out_n);
2573 free(a->out);
2574 raw += (x*out_n+1)*y;
2575 raw_len -= (x*out_n+1)*y;
2576 }
2577 }
2578 a->out = final;
2579
2580 stbi_png_partial = save;
2581 return 1;
2582 }
2583
2584 static int compute_transparency(png *z, stbi__uint8 tc[3], int out_n)
2585 {
2586 stbi *s = z->s;
2587 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
2588 stbi__uint8 *p = z->out;
2589
2590 // compute color-based transparency, assuming we've
2591 // already got 255 as the alpha value in the output
2592 assert(out_n == 2 || out_n == 4);
2593
2594 if (out_n == 2) {
2595 for (i=0; i < pixel_count; ++i) {
2596 p[1] = (p[0] == tc[0] ? 0 : 255);
2597 p += 2;
2598 }
2599 } else {
2600 for (i=0; i < pixel_count; ++i) {
2601 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
2602 p[3] = 0;
2603 p += 4;
2604 }
2605 }
2606 return 1;
2607 }
2608
2609 static int expand_palette(png *a, stbi__uint8 *palette, int len, int pal_img_n)
2610 {
2611 stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
2612 stbi__uint8 *p, *temp_out, *orig = a->out;
2613
2614 p = (stbi__uint8 *) malloc(pixel_count * pal_img_n);
2615 if (p == NULL) return e("outofmem", "Out of memory");
2616
2617 // between here and free(out) below, exitting would leak
2618 temp_out = p;
2619
2620 if (pal_img_n == 3) {
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;
2627 }
2628 } else {
2629 for (i=0; i < pixel_count; ++i) {
2630 int n = orig[i]*4;
2631 p[0] = palette[n ];
2632 p[1] = palette[n+1];
2633 p[2] = palette[n+2];
2634 p[3] = palette[n+3];
2635 p += 4;
2636 }
2637 }
2638 free(a->out);
2639 a->out = temp_out;
2640
2641 STBI_NOTUSED(len);
2642
2643 return 1;
2644 }
2645
2646 static int stbi_unpremultiply_on_load = 0;
2647 static int stbi_de_iphone_flag = 0;
2648
2649 void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
2650 {
2651 stbi_unpremultiply_on_load = flag_true_if_should_unpremultiply;
2652 }
2653 void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
2654 {
2655 stbi_de_iphone_flag = flag_true_if_should_convert;
2656 }
2657
2658 static void stbi_de_iphone(png *z)
2659 {
2660 stbi *s = z->s;
2661 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
2662 stbi__uint8 *p = z->out;
2663
2664 if (s->img_out_n == 3) { // convert bgr to rgb
2665 for (i=0; i < pixel_count; ++i) {
2666 stbi__uint8 t = p[0];
2667 p[0] = p[2];
2668 p[2] = t;
2669 p += 3;
2670 }
2671 } else {
2672 assert(s->img_out_n == 4);
2673 if (stbi_unpremultiply_on_load) {
2674 // convert bgr to rgb and unpremultiply
2675 for (i=0; i < pixel_count; ++i) {
2676 stbi__uint8 a = p[3];
2677 stbi__uint8 t = p[0];
2678 if (a) {
2679 p[0] = p[2] * 255 / a;
2680 p[1] = p[1] * 255 / a;
2681 p[2] = t * 255 / a;
2682 } else {
2683 p[0] = p[2];
2684 p[2] = t;
2685 }
2686 p += 4;
2687 }
2688 } else {
2689 // convert bgr to rgb
2690 for (i=0; i < pixel_count; ++i) {
2691 stbi__uint8 t = p[0];
2692 p[0] = p[2];
2693 p[2] = t;
2694 p += 4;
2695 }
2696 }
2697 }
2698 }
2699
2700 static int parse_png_file(png *z, int scan, int req_comp)
2701 {
2702 stbi__uint8 palette[1024], pal_img_n=0;
2703 stbi__uint8 has_trans=0, tc[3];
2704 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
2705 int first=1,k,interlace=0, iphone=0;
2706 stbi *s = z->s;
2707
2708 z->expanded = NULL;
2709 z->idata = NULL;
2710 z->out = NULL;
2711
2712 if (!check_png_header(s)) return 0;
2713
2714 if (scan == SCAN_type) return 1;
2715
2716 for (;;) {
2717 chunk c = get_chunk_header(s);
2718 switch (c.type) {
2719 case PNG_TYPE('C','g','B','I'):
2720 iphone = stbi_de_iphone_flag;
2721 skip(s, c.length);
2722 break;
2723 case PNG_TYPE('I','H','D','R'): {
2724 int depth,color,comp,filter;
2725 if (!first) return e("multiple IHDR","Corrupt PNG");
2726 first = 0;
2727 if (c.length != 13) return e("bad IHDR len","Corrupt PNG");
2728 s->img_x = get32(s); if (s->img_x > (1 << 24)) return e("too large","Very large image (corrupt?)");
2729 s->img_y = get32(s); if (s->img_y > (1 << 24)) return e("too large","Very large image (corrupt?)");
2730 depth = get8(s); if (depth != 8) return e("8bit only","PNG not supported: 8-bit only");
2731 color = get8(s); if (color > 6) return e("bad ctype","Corrupt PNG");
2732 if (color == 3) pal_img_n = 3; else if (color & 1) return e("bad ctype","Corrupt PNG");
2733 comp = get8(s); if (comp) return e("bad comp method","Corrupt PNG");
2734 filter= get8(s); if (filter) return e("bad filter method","Corrupt PNG");
2735 interlace = get8(s); if (interlace>1) return e("bad interlace method","Corrupt PNG");
2736 if (!s->img_x || !s->img_y) return e("0-pixel image","Corrupt PNG");
2737 if (!pal_img_n) {
2738 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
2739 if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
2740 if (scan == SCAN_header) return 1;
2741 } else {
2742 // if paletted, then pal_n is our final components, and
2743 // img_n is # components to decompress/filter.
2744 s->img_n = 1;
2745 if ((1 << 30) / s->img_x / 4 < s->img_y) return e("too large","Corrupt PNG");
2746 // if SCAN_header, have to scan to see if we have a tRNS
2747 }
2748 break;
2749 }
2750
2751 case PNG_TYPE('P','L','T','E'): {
2752 if (first) return e("first not IHDR", "Corrupt PNG");
2753 if (c.length > 256*3) return e("invalid PLTE","Corrupt PNG");
2754 pal_len = c.length / 3;
2755 if (pal_len * 3 != c.length) return e("invalid PLTE","Corrupt PNG");
2756 for (i=0; i < pal_len; ++i) {
2757 palette[i*4+0] = get8u(s);
2758 palette[i*4+1] = get8u(s);
2759 palette[i*4+2] = get8u(s);
2760 palette[i*4+3] = 255;
2761 }
2762 break;
2763 }
2764
2765 case PNG_TYPE('t','R','N','S'): {
2766 if (first) return e("first not IHDR", "Corrupt PNG");
2767 if (z->idata) return e("tRNS after IDAT","Corrupt PNG");
2768 if (pal_img_n) {
2769 if (scan == SCAN_header) { s->img_n = 4; return 1; }
2770 if (pal_len == 0) return e("tRNS before PLTE","Corrupt PNG");
2771 if (c.length > pal_len) return e("bad tRNS len","Corrupt PNG");
2772 pal_img_n = 4;
2773 for (i=0; i < c.length; ++i)
2774 palette[i*4+3] = get8u(s);
2775 } else {
2776 if (!(s->img_n & 1)) return e("tRNS with alpha","Corrupt PNG");
2777 if (c.length != (stbi__uint32) s->img_n*2) return e("bad tRNS len","Corrupt PNG");
2778 has_trans = 1;
2779 for (k=0; k < s->img_n; ++k)
2780 tc[k] = (stbi__uint8) get16(s); // non 8-bit images will be larger
2781 }
2782 break;
2783 }
2784
2785 case PNG_TYPE('I','D','A','T'): {
2786 if (first) return e("first not IHDR", "Corrupt PNG");
2787 if (pal_img_n && !pal_len) return e("no PLTE","Corrupt PNG");
2788 if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; }
2789 if (ioff + c.length > idata_limit) {
2790 stbi__uint8 *p;
2791 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
2792 while (ioff + c.length > idata_limit)
2793 idata_limit *= 2;
2794 p = (stbi__uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e("outofmem", "Out of memory");
2795 z->idata = p;
2796 }
2797 if (!getn(s, z->idata+ioff,c.length)) return e("outofdata","Corrupt PNG");
2798 ioff += c.length;
2799 break;
2800 }
2801
2802 case PNG_TYPE('I','E','N','D'): {
2803 stbi__uint32 raw_len;
2804 if (first) return e("first not IHDR", "Corrupt PNG");
2805 if (scan != SCAN_load) return 1;
2806 if (z->idata == NULL) return e("no IDAT","Corrupt PNG");
2807 z->expanded = (stbi__uint8 *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !iphone);
2808 if (z->expanded == NULL) return 0; // zlib should set error
2809 free(z->idata); z->idata = NULL;
2810 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
2811 s->img_out_n = s->img_n+1;
2812 else
2813 s->img_out_n = s->img_n;
2814 if (!create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0;
2815 if (has_trans)
2816 if (!compute_transparency(z, tc, s->img_out_n)) return 0;
2817 if (iphone && s->img_out_n > 2)
2818 stbi_de_iphone(z);
2819 if (pal_img_n) {
2820 // pal_img_n == 3 or 4
2821 s->img_n = pal_img_n; // record the actual colors we had
2822 s->img_out_n = pal_img_n;
2823 if (req_comp >= 3) s->img_out_n = req_comp;
2824 if (!expand_palette(z, palette, pal_len, s->img_out_n))
2825 return 0;
2826 }
2827 free(z->expanded); z->expanded = NULL;
2828 return 1;
2829 }
2830
2831 default:
2832 // if critical, fail
2833 if (first) return e("first not IHDR", "Corrupt PNG");
2834 if ((c.type & (1 << 29)) == 0) {
2835 #ifndef STBI_NO_FAILURE_STRINGS
2836 // not threadsafe
2837 static char invalid_chunk[] = "XXXX chunk not known";
2838 invalid_chunk[0] = (stbi__uint8) (c.type >> 24);
2839 invalid_chunk[1] = (stbi__uint8) (c.type >> 16);
2840 invalid_chunk[2] = (stbi__uint8) (c.type >> 8);
2841 invalid_chunk[3] = (stbi__uint8) (c.type >> 0);
2842 #endif
2843 return e(invalid_chunk, "PNG not supported: unknown chunk type");
2844 }
2845 skip(s, c.length);
2846 break;
2847 }
2848 // end of chunk, read and skip CRC
2849 get32(s);
2850 }
2851 }
2852
2853 static unsigned char *do_png(png *p, int *x, int *y, int *n, int req_comp)
2854 {
2855 unsigned char *result=NULL;
2856 if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
2857 if (parse_png_file(p, SCAN_load, req_comp)) {
2858 result = p->out;
2859 p->out = NULL;
2860 if (req_comp && req_comp != p->s->img_out_n) {
2861 result = convert_format(result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
2862 p->s->img_out_n = req_comp;
2863 if (result == NULL) return result;
2864 }
2865 *x = p->s->img_x;
2866 *y = p->s->img_y;
2867 if (n) *n = p->s->img_n;
2868 }
2869 free(p->out); p->out = NULL;
2870 free(p->expanded); p->expanded = NULL;
2871 free(p->idata); p->idata = NULL;
2872
2873 return result;
2874 }
2875
2876 static unsigned char *stbi_png_load(stbi *s, int *x, int *y, int *comp, int req_comp)
2877 {
2878 png p;
2879 p.s = s;
2880 return do_png(&p, x,y,comp,req_comp);
2881 }
2882
2883 static int stbi_png_test(stbi *s)
2884 {
2885 int r;
2886 r = check_png_header(s);
2887 stbi_rewind(s);
2888 return r;
2889 }
2890
2891 static int stbi_png_info_raw(png *p, int *x, int *y, int *comp)
2892 {
2893 if (!parse_png_file(p, SCAN_header, 0)) {
2894 stbi_rewind( p->s );
2895 return 0;
2896 }
2897 if (x) *x = p->s->img_x;
2898 if (y) *y = p->s->img_y;
2899 if (comp) *comp = p->s->img_n;
2900 return 1;
2901 }
2902
2903 static int stbi_png_info(stbi *s, int *x, int *y, int *comp)
2904 {
2905 png p;
2906 p.s = s;
2907 return stbi_png_info_raw(&p, x, y, comp);
2908 }
2909
2910 // Microsoft/Windows BMP image
2911
2912 static int bmp_test(stbi *s)
2913 {
2914 int sz;
2915 if (get8(s) != 'B') return 0;
2916 if (get8(s) != 'M') return 0;
2917 get32le(s); // discard filesize
2918 get16le(s); // discard reserved
2919 get16le(s); // discard reserved
2920 get32le(s); // discard data offset
2921 sz = get32le(s);
2922 if (sz == 12 || sz == 40 || sz == 56 || sz == 108) return 1;
2923 return 0;
2924 }
2925
2926 static int stbi_bmp_test(stbi *s)
2927 {
2928 int r = bmp_test(s);
2929 stbi_rewind(s);
2930 return r;
2931 }
2932
2933
2934 // returns 0..31 for the highest set bit
2935 static int high_bit(unsigned int z)
2936 {
2937 int n=0;
2938 if (z == 0) return -1;
2939 if (z >= 0x10000) n += 16, z >>= 16;
2940 if (z >= 0x00100) n += 8, z >>= 8;
2941 if (z >= 0x00010) n += 4, z >>= 4;
2942 if (z >= 0x00004) n += 2, z >>= 2;
2943 if (z >= 0x00002) n += 1, z >>= 1;
2944 return n;
2945 }
2946
2947 static int bitcount(unsigned int a)
2948 {
2949 a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
2950 a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
2951 a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
2952 a = (a + (a >> 8)); // max 16 per 8 bits
2953 a = (a + (a >> 16)); // max 32 per 8 bits
2954 return a & 0xff;
2955 }
2956
2957 static int shiftsigned(int v, int shift, int bits)
2958 {
2959 int result;
2960 int z=0;
2961
2962 if (shift < 0) v <<= -shift;
2963 else v >>= shift;
2964 result = v;
2965
2966 z = bits;
2967 while (z < 8) {
2968 result += v >> z;
2969 z += bits;
2970 }
2971 return result;
2972 }
2973
2974 static stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp)
2975 {
2976 stbi__uint8 *out;
2977 unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
2978 stbi_uc pal[256][4];
2979 int psize=0,i,j,compress=0,width;
2980 int bpp, flip_vertically, pad, target, offset, hsz;
2981 if (get8(s) != 'B' || get8(s) != 'M') return epuc("not BMP", "Corrupt BMP");
2982 get32le(s); // discard filesize
2983 get16le(s); // discard reserved
2984 get16le(s); // discard reserved
2985 offset = get32le(s);
2986 hsz = get32le(s);
2987 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) return epuc("unknown BMP", "BMP type not supported: unknown");
2988 if (hsz == 12) {
2989 s->img_x = get16le(s);
2990 s->img_y = get16le(s);
2991 } else {
2992 s->img_x = get32le(s);
2993 s->img_y = get32le(s);
2994 }
2995 if (get16le(s) != 1) return epuc("bad BMP", "bad BMP");
2996 bpp = get16le(s);
2997 if (bpp == 1) return epuc("monochrome", "BMP type not supported: 1-bit");
2998 flip_vertically = ((int) s->img_y) > 0;
2999 s->img_y = abs((int) s->img_y);
3000 if (hsz == 12) {
3001 if (bpp < 24)
3002 psize = (offset - 14 - 24) / 3;
3003 } else {
3004 compress = get32le(s);
3005 if (compress == 1 || compress == 2) return epuc("BMP RLE", "BMP type not supported: RLE");
3006 get32le(s); // discard sizeof
3007 get32le(s); // discard hres
3008 get32le(s); // discard vres
3009 get32le(s); // discard colorsused
3010 get32le(s); // discard max important
3011 if (hsz == 40 || hsz == 56) {
3012 if (hsz == 56) {
3013 get32le(s);
3014 get32le(s);
3015 get32le(s);
3016 get32le(s);
3017 }
3018 if (bpp == 16 || bpp == 32) {
3019 mr = mg = mb = 0;
3020 if (compress == 0) {
3021 if (bpp == 32) {
3022 mr = 0xffu << 16;
3023 mg = 0xffu << 8;
3024 mb = 0xffu << 0;
3025 ma = 0xffu << 24;
3026 fake_a = 1; // @TODO: check for cases like alpha value is all 0 and switch it to 255
3027 STBI_NOTUSED(fake_a);
3028 } else {
3029 mr = 31u << 10;
3030 mg = 31u << 5;
3031 mb = 31u << 0;
3032 }
3033 } else if (compress == 3) {
3034 mr = get32le(s);
3035 mg = get32le(s);
3036 mb = get32le(s);
3037 // not documented, but generated by photoshop and handled by mspaint
3038 if (mr == mg && mg == mb) {
3039 // ?!?!?
3040 return epuc("bad BMP", "bad BMP");
3041 }
3042 } else
3043 return epuc("bad BMP", "bad BMP");
3044 }
3045 } else {
3046 assert(hsz == 108);
3047 mr = get32le(s);
3048 mg = get32le(s);
3049 mb = get32le(s);
3050 ma = get32le(s);
3051 get32le(s); // discard color space
3052 for (i=0; i < 12; ++i)
3053 get32le(s); // discard color space parameters
3054 }
3055 if (bpp < 16)
3056 psize = (offset - 14 - hsz) >> 2;
3057 }
3058 s->img_n = ma ? 4 : 3;
3059 if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
3060 target = req_comp;
3061 else
3062 target = s->img_n; // if they want monochrome, we'll post-convert
3063 out = (stbi_uc *) malloc(target * s->img_x * s->img_y);
3064 if (!out) return epuc("outofmem", "Out of memory");
3065 if (bpp < 16) {
3066 int z=0;
3067 if (psize == 0 || psize > 256) { free(out); return epuc("invalid", "Corrupt BMP"); }
3068 for (i=0; i < psize; ++i) {
3069 pal[i][2] = get8u(s);
3070 pal[i][1] = get8u(s);
3071 pal[i][0] = get8u(s);
3072 if (hsz != 12) get8(s);
3073 pal[i][3] = 255;
3074 }
3075 skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
3076 if (bpp == 4) width = (s->img_x + 1) >> 1;
3077 else if (bpp == 8) width = s->img_x;
3078 else { free(out); return epuc("bad bpp", "Corrupt BMP"); }
3079 pad = (-width)&3;
3080 for (j=0; j < (int) s->img_y; ++j) {
3081 for (i=0; i < (int) s->img_x; i += 2) {
3082 int v=get8(s),v2=0;
3083 if (bpp == 4) {
3084 v2 = v & 15;
3085 v >>= 4;
3086 }
3087 out[z++] = pal[v][0];
3088 out[z++] = pal[v][1];
3089 out[z++] = pal[v][2];
3090 if (target == 4) out[z++] = 255;
3091 if (i+1 == (int) s->img_x) break;
3092 v = (bpp == 8) ? get8(s) : v2;
3093 out[z++] = pal[v][0];
3094 out[z++] = pal[v][1];
3095 out[z++] = pal[v][2];
3096 if (target == 4) out[z++] = 255;
3097 }
3098 skip(s, pad);
3099 }
3100 } else {
3101 int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
3102 int z = 0;
3103 int easy=0;
3104 skip(s, offset - 14 - hsz);
3105 if (bpp == 24) width = 3 * s->img_x;
3106 else if (bpp == 16) width = 2*s->img_x;
3107 else /* bpp = 32 and pad = 0 */ width=0;
3108 pad = (-width) & 3;
3109 if (bpp == 24) {
3110 easy = 1;
3111 } else if (bpp == 32) {
3112 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
3113 easy = 2;
3114 }
3115 if (!easy) {
3116 if (!mr || !mg || !mb) { free(out); return epuc("bad masks", "Corrupt BMP"); }
3117 // right shift amt to put high bit in position #7
3118 rshift = high_bit(mr)-7; rcount = bitcount(mr);
3119 gshift = high_bit(mg)-7; gcount = bitcount(mg);
3120 bshift = high_bit(mb)-7; bcount = bitcount(mb);
3121 ashift = high_bit(ma)-7; acount = bitcount(ma);
3122 }
3123 for (j=0; j < (int) s->img_y; ++j) {
3124 if (easy) {
3125 for (i=0; i < (int) s->img_x; ++i) {
3126 int a;
3127 out[z+2] = get8u(s);
3128 out[z+1] = get8u(s);
3129 out[z+0] = get8u(s);
3130 z += 3;
3131 a = (easy == 2 ? get8(s) : 255);
3132 if (target == 4) out[z++] = (stbi__uint8) a;
3133 }
3134 } else {
3135 for (i=0; i < (int) s->img_x; ++i) {
3136 stbi__uint32 v = (stbi__uint32) (bpp == 16 ? get16le(s) : get32le(s));
3137 int a;
3138 out[z++] = (stbi__uint8) shiftsigned(v & mr, rshift, rcount);
3139 out[z++] = (stbi__uint8) shiftsigned(v & mg, gshift, gcount);
3140 out[z++] = (stbi__uint8) shiftsigned(v & mb, bshift, bcount);
3141 a = (ma ? shiftsigned(v & ma, ashift, acount) : 255);
3142 if (target == 4) out[z++] = (stbi__uint8) a;
3143 }
3144 }
3145 skip(s, pad);
3146 }
3147 }
3148 if (flip_vertically) {
3149 stbi_uc t;
3150 for (j=0; j < (int) s->img_y>>1; ++j) {
3151 stbi_uc *p1 = out + j *s->img_x*target;
3152 stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
3153 for (i=0; i < (int) s->img_x*target; ++i) {
3154 t = p1[i], p1[i] = p2[i], p2[i] = t;
3155 }
3156 }
3157 }
3158
3159 if (req_comp && req_comp != target) {
3160 out = convert_format(out, target, req_comp, s->img_x, s->img_y);
3161 if (out == NULL) return out; // convert_format frees input on failure
3162 }
3163
3164 *x = s->img_x;
3165 *y = s->img_y;
3166 if (comp) *comp = s->img_n;
3167 return out;
3168 }
3169
3170 static stbi_uc *stbi_bmp_load(stbi *s,int *x, int *y, int *comp, int req_comp)
3171 {
3172 return bmp_load(s, x,y,comp,req_comp);
3173 }
3174
3175
3176 // Targa Truevision - TGA
3177 // by Jonathan Dummer
3178
3179 static int tga_info(stbi *s, int *x, int *y, int *comp)
3180 {
3181 int tga_w, tga_h, tga_comp;
3182 int sz;
3183 get8u(s); // discard Offset
3184 sz = get8u(s); // color type
3185 if( sz > 1 ) {
3186 stbi_rewind(s);
3187 return 0; // only RGB or indexed allowed
3188 }
3189 sz = get8u(s); // image type
3190 // only RGB or grey allowed, +/- RLE
3191 if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11)) return 0;
3192 skip(s,9);
3193 tga_w = get16le(s);
3194 if( tga_w < 1 ) {
3195 stbi_rewind(s);
3196 return 0; // test width
3197 }
3198 tga_h = get16le(s);
3199 if( tga_h < 1 ) {
3200 stbi_rewind(s);
3201 return 0; // test height
3202 }
3203 sz = get8(s); // bits per pixel
3204 // only RGB or RGBA or grey allowed
3205 if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) {
3206 stbi_rewind(s);
3207 return 0;
3208 }
3209 tga_comp = sz;
3210 if (x) *x = tga_w;
3211 if (y) *y = tga_h;
3212 if (comp) *comp = tga_comp / 8;
3213 return 1; // seems to have passed everything
3214 }
3215
3216 int stbi_tga_info(stbi *s, int *x, int *y, int *comp)
3217 {
3218 return tga_info(s, x, y, comp);
3219 }
3220
3221 static int tga_test(stbi *s)
3222 {
3223 int sz;
3224 get8u(s); // discard Offset
3225 sz = get8u(s); // color type
3226 if ( sz > 1 ) return 0; // only RGB or indexed allowed
3227 sz = get8u(s); // image type
3228 if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0; // only RGB or grey allowed, +/- RLE
3229 get16(s); // discard palette start
3230 get16(s); // discard palette length
3231 get8(s); // discard bits per palette color entry
3232 get16(s); // discard x origin
3233 get16(s); // discard y origin
3234 if ( get16(s) < 1 ) return 0; // test width
3235 if ( get16(s) < 1 ) return 0; // test height
3236 sz = get8(s); // bits per pixel
3237 if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) ) return 0; // only RGB or RGBA or grey allowed
3238 return 1; // seems to have passed everything
3239 }
3240
3241 static int stbi_tga_test(stbi *s)
3242 {
3243 int res = tga_test(s);
3244 stbi_rewind(s);
3245 return res;
3246 }
3247
3248 static stbi_uc *tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3249 {
3250 // read in the TGA header stuff
3251 int tga_offset = get8u(s);
3252 int tga_indexed = get8u(s);
3253 int tga_image_type = get8u(s);
3254 int tga_is_RLE = 0;
3255 int tga_palette_start = get16le(s);
3256 int tga_palette_len = get16le(s);
3257 int tga_palette_bits = get8u(s);
3258 int tga_x_origin = get16le(s);
3259 int tga_y_origin = get16le(s);
3260 int tga_width = get16le(s);
3261 int tga_height = get16le(s);
3262 int tga_bits_per_pixel = get8u(s);
3263 int tga_comp = tga_bits_per_pixel / 8;
3264 int tga_inverted = get8u(s);
3265 // image data
3266 unsigned char *tga_data;
3267 unsigned char *tga_palette = NULL;
3268 int i, j;
3269 unsigned char raw_data[4];
3270 int RLE_count = 0;
3271 int RLE_repeating = 0;
3272 int read_next_pixel = 1;
3273
3274 // do a tiny bit of precessing
3275 if ( tga_image_type >= 8 )
3276 {
3277 tga_image_type -= 8;
3278 tga_is_RLE = 1;
3279 }
3280 /* int tga_alpha_bits = tga_inverted & 15; */
3281 tga_inverted = 1 - ((tga_inverted >> 5) & 1);
3282
3283 // error check
3284 if ( //(tga_indexed) ||
3285 (tga_width < 1) || (tga_height < 1) ||
3286 (tga_image_type < 1) || (tga_image_type > 3) ||
3287 ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
3288 (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
3289 )
3290 {
3291 return NULL; // we don't report this as a bad TGA because we don't even know if it's TGA
3292 }
3293
3294 // If I'm paletted, then I'll use the number of bits from the palette
3295 if ( tga_indexed )
3296 {
3297 tga_comp = tga_palette_bits / 8;
3298 }
3299
3300 // tga info
3301 *x = tga_width;
3302 *y = tga_height;
3303 if (comp) *comp = tga_comp;
3304
3305 tga_data = (unsigned char*)malloc( tga_width * tga_height * req_comp );
3306 if (!tga_data) return epuc("outofmem", "Out of memory");
3307
3308 // skip to the data's starting position (offset usually = 0)
3309 skip(s, tga_offset );
3310
3311 if ( !tga_indexed && !tga_is_RLE) {
3312 for (i=0; i < tga_height; ++i) {
3313 int y = tga_inverted ? tga_height -i - 1 : i;
3314 stbi__uint8 *tga_row = tga_data + y*tga_width*tga_comp;
3315 getn(s, tga_row, tga_width * tga_comp);
3316 }
3317 } else {
3318 // do I need to load a palette?
3319 if ( tga_indexed)
3320 {
3321 // any data to skip? (offset usually = 0)
3322 skip(s, tga_palette_start );
3323 // load the palette
3324 tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
3325 if (!tga_palette) {
3326 free(tga_data);
3327 return epuc("outofmem", "Out of memory");
3328 }
3329 if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {
3330 free(tga_data);
3331 free(tga_palette);
3332 return epuc("bad palette", "Corrupt TGA");
3333 }
3334 }
3335 // load the data
3336 for (i=0; i < tga_width * tga_height; ++i)
3337 {
3338 // if I'm in RLE mode, do I need to get a RLE chunk?
3339 if ( tga_is_RLE )
3340 {
3341 if ( RLE_count == 0 )
3342 {
3343 // yep, get the next byte as a RLE command
3344 int RLE_cmd = get8u(s);
3345 RLE_count = 1 + (RLE_cmd & 127);
3346 RLE_repeating = RLE_cmd >> 7;
3347 read_next_pixel = 1;
3348 } else if ( !RLE_repeating )
3349 {
3350 read_next_pixel = 1;
3351 }
3352 } else
3353 {
3354 read_next_pixel = 1;
3355 }
3356 // OK, if I need to read a pixel, do it now
3357 if ( read_next_pixel )
3358 {
3359 // load however much data we did have
3360 if ( tga_indexed )
3361 {
3362 // read in 1 byte, then perform the lookup
3363 int pal_idx = get8u(s);
3364 if ( pal_idx >= tga_palette_len )
3365 {
3366 // invalid index
3367 pal_idx = 0;
3368 }
3369 pal_idx *= tga_bits_per_pixel / 8;
3370 for (j = 0; j*8 < tga_bits_per_pixel; ++j)
3371 {
3372 raw_data[j] = tga_palette[pal_idx+j];
3373 }
3374 } else
3375 {
3376 // read in the data raw
3377 for (j = 0; j*8 < tga_bits_per_pixel; ++j)
3378 {
3379 raw_data[j] = get8u(s);
3380 }
3381 }
3382 // clear the reading flag for the next pixel
3383 read_next_pixel = 0;
3384 } // end of reading a pixel
3385
3386 // copy data
3387 for (j = 0; j < tga_comp; ++j)
3388 tga_data[i*tga_comp+j] = raw_data[j];
3389
3390 // in case we're in RLE mode, keep counting down
3391 --RLE_count;
3392 }
3393 // do I need to invert the image?
3394 if ( tga_inverted )
3395 {
3396 for (j = 0; j*2 < tga_height; ++j)
3397 {
3398 int index1 = j * tga_width * req_comp;
3399 int index2 = (tga_height - 1 - j) * tga_width * req_comp;
3400 for (i = tga_width * req_comp; i > 0; --i)
3401 {
3402 unsigned char temp = tga_data[index1];
3403 tga_data[index1] = tga_data[index2];
3404 tga_data[index2] = temp;
3405 ++index1;
3406 ++index2;
3407 }
3408 }
3409 }
3410 // clear my palette, if I had one
3411 if ( tga_palette != NULL )
3412 {
3413 free( tga_palette );
3414 }
3415 }
3416
3417 // swap RGB
3418 if (tga_comp >= 3)
3419 {
3420 unsigned char* tga_pixel = tga_data;
3421 for (i=0; i < tga_width * tga_height; ++i)
3422 {
3423 unsigned char temp = tga_pixel[0];
3424 tga_pixel[0] = tga_pixel[2];
3425 tga_pixel[2] = temp;
3426 tga_pixel += tga_comp;
3427 }
3428 }
3429
3430 // convert to target component count
3431 if (req_comp && req_comp != tga_comp)
3432 tga_data = convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
3433
3434 // the things I do to get rid of an error message, and yet keep
3435 // Microsoft's C compilers happy... [8^(
3436 tga_palette_start = tga_palette_len = tga_palette_bits =
3437 tga_x_origin = tga_y_origin = 0;
3438 // OK, done
3439 return tga_data;
3440 }
3441
3442 static stbi_uc *stbi_tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3443 {
3444 return tga_load(s,x,y,comp,req_comp);
3445 }
3446
3447
3448 // *************************************************************************************************
3449 // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
3450
3451 static int psd_test(stbi *s)
3452 {
3453 if (get32(s) != 0x38425053) return 0; // "8BPS"
3454 else return 1;
3455 }
3456
3457 static int stbi_psd_test(stbi *s)
3458 {
3459 int r = psd_test(s);
3460 stbi_rewind(s);
3461 return r;
3462 }
3463
3464 static stbi_uc *psd_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3465 {
3466 int pixelCount;
3467 int channelCount, compression;
3468 int channel, i, count, len;
3469 int w,h;
3470 stbi__uint8 *out;
3471
3472 // Check identifier
3473 if (get32(s) != 0x38425053) // "8BPS"
3474 return epuc("not PSD", "Corrupt PSD image");
3475
3476 // Check file type version.
3477 if (get16(s) != 1)
3478 return epuc("wrong version", "Unsupported version of PSD image");
3479
3480 // Skip 6 reserved bytes.
3481 skip(s, 6 );
3482
3483 // Read the number of channels (R, G, B, A, etc).
3484 channelCount = get16(s);
3485 if (channelCount < 0 || channelCount > 16)
3486 return epuc("wrong channel count", "Unsupported number of channels in PSD image");
3487
3488 // Read the rows and columns of the image.
3489 h = get32(s);
3490 w = get32(s);
3491
3492 // Make sure the depth is 8 bits.
3493 if (get16(s) != 8)
3494 return epuc("unsupported bit depth", "PSD bit depth is not 8 bit");
3495
3496 // Make sure the color mode is RGB.
3497 // Valid options are:
3498 // 0: Bitmap
3499 // 1: Grayscale
3500 // 2: Indexed color
3501 // 3: RGB color
3502 // 4: CMYK color
3503 // 7: Multichannel
3504 // 8: Duotone
3505 // 9: Lab color
3506 if (get16(s) != 3)
3507 return epuc("wrong color format", "PSD is not in RGB color format");
3508
3509 // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.)
3510 skip(s,get32(s) );
3511
3512 // Skip the image resources. (resolution, pen tool paths, etc)
3513 skip(s, get32(s) );
3514
3515 // Skip the reserved data.
3516 skip(s, get32(s) );
3517
3518 // Find out if the data is compressed.
3519 // Known values:
3520 // 0: no compression
3521 // 1: RLE compressed
3522 compression = get16(s);
3523 if (compression > 1)
3524 return epuc("bad compression", "PSD has an unknown compression format");
3525
3526 // Create the destination image.
3527 out = (stbi_uc *) malloc(4 * w*h);
3528 if (!out) return epuc("outofmem", "Out of memory");
3529 pixelCount = w*h;
3530
3531 // Initialize the data to zero.
3532 //memset( out, 0, pixelCount * 4 );
3533
3534 // Finally, the image data.
3535 if (compression) {
3536 // RLE as used by .PSD and .TIFF
3537 // Loop until you get the number of unpacked bytes you are expecting:
3538 // Read the next source byte into n.
3539 // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
3540 // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
3541 // Else if n is 128, noop.
3542 // Endloop
3543
3544 // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
3545 // which we're going to just skip.
3546 skip(s, h * channelCount * 2 );
3547
3548 // Read the RLE data by channel.
3549 for (channel = 0; channel < 4; channel++) {
3550 stbi__uint8 *p;
3551
3552 p = out+channel;
3553 if (channel >= channelCount) {
3554 // Fill this channel with default data.
3555 for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
3556 } else {
3557 // Read the RLE data.
3558 count = 0;
3559 while (count < pixelCount) {
3560 len = get8(s);
3561 if (len == 128) {
3562 // No-op.
3563 } else if (len < 128) {
3564 // Copy next len+1 bytes literally.
3565 len++;
3566 count += len;
3567 while (len) {
3568 *p = get8u(s);
3569 p += 4;
3570 len--;
3571 }
3572 } else if (len > 128) {
3573 stbi__uint8 val;
3574 // Next -len+1 bytes in the dest are replicated from next source byte.
3575 // (Interpret len as a negative 8-bit int.)
3576 len ^= 0x0FF;
3577 len += 2;
3578 val = get8u(s);
3579 count += len;
3580 while (len) {
3581 *p = val;
3582 p += 4;
3583 len--;
3584 }
3585 }
3586 }
3587 }
3588 }
3589
3590 } else {
3591 // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
3592 // where each channel consists of an 8-bit value for each pixel in the image.
3593
3594 // Read the data by channel.
3595 for (channel = 0; channel < 4; channel++) {
3596 stbi__uint8 *p;
3597
3598 p = out + channel;
3599 if (channel > channelCount) {
3600 // Fill this channel with default data.
3601 for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
3602 } else {
3603 // Read the data.
3604 for (i = 0; i < pixelCount; i++)
3605 *p = get8u(s), p += 4;
3606 }
3607 }
3608 }
3609
3610 if (req_comp && req_comp != 4) {
3611 out = convert_format(out, 4, req_comp, w, h);
3612 if (out == NULL) return out; // convert_format frees input on failure
3613 }
3614
3615 if (comp) *comp = channelCount;
3616 *y = h;
3617 *x = w;
3618
3619 return out;
3620 }
3621
3622 static stbi_uc *stbi_psd_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3623 {
3624 return psd_load(s,x,y,comp,req_comp);
3625 }
3626
3627 // *************************************************************************************************
3628 // Softimage PIC loader
3629 // by Tom Seddon
3630 //
3631 // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
3632 // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
3633
3634 static int pic_is4(stbi *s,const char *str)
3635 {
3636 int i;
3637 for (i=0; i<4; ++i)
3638 if (get8(s) != (stbi_uc)str[i])
3639 return 0;
3640
3641 return 1;
3642 }
3643
3644 static int pic_test(stbi *s)
3645 {
3646 int i;
3647
3648 if (!pic_is4(s,"\x53\x80\xF6\x34"))
3649 return 0;
3650
3651 for(i=0;i<84;++i)
3652 get8(s);
3653
3654 if (!pic_is4(s,"PICT"))
3655 return 0;
3656
3657 return 1;
3658 }
3659
3660 typedef struct
3661 {
3662 stbi_uc size,type,channel;
3663 } pic_packet_t;
3664
3665 static stbi_uc *pic_readval(stbi *s, int channel, stbi_uc *dest)
3666 {
3667 int mask=0x80, i;
3668
3669 for (i=0; i<4; ++i, mask>>=1) {
3670 if (channel & mask) {
3671 if (at_eof(s)) return epuc("bad file","PIC file too short");
3672 dest[i]=get8u(s);
3673 }
3674 }
3675
3676 return dest;
3677 }
3678
3679 static void pic_copyval(int channel,stbi_uc *dest,const stbi_uc *src)
3680 {
3681 int mask=0x80,i;
3682
3683 for (i=0;i<4; ++i, mask>>=1)
3684 if (channel&mask)
3685 dest[i]=src[i];
3686 }
3687
3688 static stbi_uc *pic_load2(stbi *s,int width,int height,int *comp, stbi_uc *result)
3689 {
3690 int act_comp=0,num_packets=0,y,chained;
3691 pic_packet_t packets[10];
3692
3693 // this will (should...) cater for even some bizarre stuff like having data
3694 // for the same channel in multiple packets.
3695 do {
3696 pic_packet_t *packet;
3697
3698 if (num_packets==sizeof(packets)/sizeof(packets[0]))
3699 return epuc("bad format","too many packets");
3700
3701 packet = &packets[num_packets++];
3702
3703 chained = get8(s);
3704 packet->size = get8u(s);
3705 packet->type = get8u(s);
3706 packet->channel = get8u(s);
3707
3708 act_comp |= packet->channel;
3709
3710 if (at_eof(s)) return epuc("bad file","file too short (reading packets)");
3711 if (packet->size != 8) return epuc("bad format","packet isn't 8bpp");
3712 } while (chained);
3713
3714 *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
3715
3716 for(y=0; y<height; ++y) {
3717 int packet_idx;
3718
3719 for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
3720 pic_packet_t *packet = &packets[packet_idx];
3721 stbi_uc *dest = result+y*width*4;
3722
3723 switch (packet->type) {
3724 default:
3725 return epuc("bad format","packet has bad compression type");
3726
3727 case 0: {//uncompressed
3728 int x;
3729
3730 for(x=0;x<width;++x, dest+=4)
3731 if (!pic_readval(s,packet->channel,dest))
3732 return 0;
3733 break;
3734 }
3735
3736 case 1://Pure RLE
3737 {
3738 int left=width, i;
3739
3740 while (left>0) {
3741 stbi_uc count,value[4];
3742
3743 count=get8u(s);
3744 if (at_eof(s)) return epuc("bad file","file too short (pure read count)");
3745
3746 if (count > left)
3747 count = (stbi__uint8) left;
3748
3749 if (!pic_readval(s,packet->channel,value)) return 0;
3750
3751 for(i=0; i<count; ++i,dest+=4)
3752 pic_copyval(packet->channel,dest,value);
3753 left -= count;
3754 }
3755 }
3756 break;
3757
3758 case 2: {//Mixed RLE
3759 int left=width;
3760 while (left>0) {
3761 int count = get8(s), i;
3762 if (at_eof(s)) return epuc("bad file","file too short (mixed read count)");
3763
3764 if (count >= 128) { // Repeated
3765 stbi_uc value[4];
3766 int i;
3767
3768 if (count==128)
3769 count = get16(s);
3770 else
3771 count -= 127;
3772 if (count > left)
3773 return epuc("bad file","scanline overrun");
3774
3775 if (!pic_readval(s,packet->channel,value))
3776 return 0;
3777
3778 for(i=0;i<count;++i, dest += 4)
3779 pic_copyval(packet->channel,dest,value);
3780 } else { // Raw
3781 ++count;
3782 if (count>left) return epuc("bad file","scanline overrun");
3783
3784 for(i=0;i<count;++i, dest+=4)
3785 if (!pic_readval(s,packet->channel,dest))
3786 return 0;
3787 }
3788 left-=count;
3789 }
3790 break;
3791 }
3792 }
3793 }
3794 }
3795
3796 return result;
3797 }
3798
3799 static stbi_uc *pic_load(stbi *s,int *px,int *py,int *comp,int req_comp)
3800 {
3801 stbi_uc *result;
3802 int i, x,y;
3803
3804 for (i=0; i<92; ++i)
3805 get8(s);
3806
3807 x = get16(s);
3808 y = get16(s);
3809 if (at_eof(s)) return epuc("bad file","file too short (pic header)");
3810 if ((1 << 28) / x < y) return epuc("too large", "Image too large to decode");
3811
3812 get32(s); //skip `ratio'
3813 get16(s); //skip `fields'
3814 get16(s); //skip `pad'
3815
3816 // intermediate buffer is RGBA
3817 result = (stbi_uc *) malloc(x*y*4);
3818 memset(result, 0xff, x*y*4);
3819
3820 if (!pic_load2(s,x,y,comp, result)) {
3821 free(result);
3822 result=0;
3823 }
3824 *px = x;
3825 *py = y;
3826 if (req_comp == 0) req_comp = *comp;
3827 result=convert_format(result,4,req_comp,x,y);
3828
3829 return result;
3830 }
3831
3832 static int stbi_pic_test(stbi *s)
3833 {
3834 int r = pic_test(s);
3835 stbi_rewind(s);
3836 return r;
3837 }
3838
3839 static stbi_uc *stbi_pic_load(stbi *s, int *x, int *y, int *comp, int req_comp)
3840 {
3841 return pic_load(s,x,y,comp,req_comp);
3842 }
3843
3844 // *************************************************************************************************
3845 // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
3846 typedef struct stbi_gif_lzw_struct {
3847 stbi__int16 prefix;
3848 stbi__uint8 first;
3849 stbi__uint8 suffix;
3850 } stbi_gif_lzw;
3851
3852 typedef struct stbi_gif_struct
3853 {
3854 int w,h;
3855 stbi_uc *out; // output buffer (always 4 components)
3856 int flags, bgindex, ratio, transparent, eflags;
3857 stbi__uint8 pal[256][4];
3858 stbi__uint8 lpal[256][4];
3859 stbi_gif_lzw codes[4096];
3860 stbi__uint8 *color_table;
3861 int parse, step;
3862 int lflags;
3863 int start_x, start_y;
3864 int max_x, max_y;
3865 int cur_x, cur_y;
3866 int line_size;
3867 } stbi_gif;
3868
3869 static int gif_test(stbi *s)
3870 {
3871 int sz;
3872 if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8') return 0;
3873 sz = get8(s);
3874 if (sz != '9' && sz != '7') return 0;
3875 if (get8(s) != 'a') return 0;
3876 return 1;
3877 }
3878
3879 static int stbi_gif_test(stbi *s)
3880 {
3881 int r = gif_test(s);
3882 stbi_rewind(s);
3883 return r;
3884 }
3885
3886 static void stbi_gif_parse_colortable(stbi *s, stbi__uint8 pal[256][4], int num_entries, int transp)
3887 {
3888 int i;
3889 for (i=0; i < num_entries; ++i) {
3890 pal[i][2] = get8u(s);
3891 pal[i][1] = get8u(s);
3892 pal[i][0] = get8u(s);
3893 pal[i][3] = transp ? 0 : 255;
3894 }
3895 }
3896
3897 static int stbi_gif_header(stbi *s, stbi_gif *g, int *comp, int is_info)
3898 {
3899 stbi__uint8 version;
3900 if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8')
3901 return e("not GIF", "Corrupt GIF");
3902
3903 version = get8u(s);
3904 if (version != '7' && version != '9') return e("not GIF", "Corrupt GIF");
3905 if (get8(s) != 'a') return e("not GIF", "Corrupt GIF");
3906
3907 failure_reason = "";
3908 g->w = get16le(s);
3909 g->h = get16le(s);
3910 g->flags = get8(s);
3911 g->bgindex = get8(s);
3912 g->ratio = get8(s);
3913 g->transparent = -1;
3914
3915 if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
3916
3917 if (is_info) return 1;
3918
3919 if (g->flags & 0x80)
3920 stbi_gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
3921
3922 return 1;
3923 }
3924
3925 static int stbi_gif_info_raw(stbi *s, int *x, int *y, int *comp)
3926 {
3927 stbi_gif g;
3928 if (!stbi_gif_header(s, &g, comp, 1)) {
3929 stbi_rewind( s );
3930 return 0;
3931 }
3932 if (x) *x = g.w;
3933 if (y) *y = g.h;
3934 return 1;
3935 }
3936
3937 static void stbi_out_gif_code(stbi_gif *g, stbi__uint16 code)
3938 {
3939 stbi__uint8 *p, *c;
3940
3941 // recurse to decode the prefixes, since the linked-list is backwards,
3942 // and working backwards through an interleaved image would be nasty
3943 if (g->codes[code].prefix >= 0)
3944 stbi_out_gif_code(g, g->codes[code].prefix);
3945
3946 if (g->cur_y >= g->max_y) return;
3947
3948 p = &g->out[g->cur_x + g->cur_y];
3949 c = &g->color_table[g->codes[code].suffix * 4];
3950
3951 if (c[3] >= 128) {
3952 p[0] = c[2];
3953 p[1] = c[1];
3954 p[2] = c[0];
3955 p[3] = c[3];
3956 }
3957 g->cur_x += 4;
3958
3959 if (g->cur_x >= g->max_x) {
3960 g->cur_x = g->start_x;
3961 g->cur_y += g->step;
3962
3963 while (g->cur_y >= g->max_y && g->parse > 0) {
3964 g->step = (1 << g->parse) * g->line_size;
3965 g->cur_y = g->start_y + (g->step >> 1);
3966 --g->parse;
3967 }
3968 }
3969 }
3970
3971 static stbi__uint8 *stbi_process_gif_raster(stbi *s, stbi_gif *g)
3972 {
3973 stbi__uint8 lzw_cs;
3974 stbi__int32 len, code;
3975 stbi__uint32 first;
3976 stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
3977 stbi_gif_lzw *p;
3978
3979 lzw_cs = get8u(s);
3980 clear = 1 << lzw_cs;
3981 first = 1;
3982 codesize = lzw_cs + 1;
3983 codemask = (1 << codesize) - 1;
3984 bits = 0;
3985 valid_bits = 0;
3986 for (code = 0; code < clear; code++) {
3987 g->codes[code].prefix = -1;
3988 g->codes[code].first = (stbi__uint8) code;
3989 g->codes[code].suffix = (stbi__uint8) code;
3990 }
3991
3992 // support no starting clear code
3993 avail = clear+2;
3994 oldcode = -1;
3995
3996 len = 0;
3997 for(;;) {
3998 if (valid_bits < codesize) {
3999 if (len == 0) {
4000 len = get8(s); // start new block
4001 if (len == 0)
4002 return g->out;
4003 }
4004 --len;
4005 bits |= (stbi__int32) get8(s) << valid_bits;
4006 valid_bits += 8;
4007 } else {
4008 stbi__int32 code = bits & codemask;
4009 bits >>= codesize;
4010 valid_bits -= codesize;
4011 // @OPTIMIZE: is there some way we can accelerate the non-clear path?
4012 if (code == clear) { // clear code
4013 codesize = lzw_cs + 1;
4014 codemask = (1 << codesize) - 1;
4015 avail = clear + 2;
4016 oldcode = -1;
4017 first = 0;
4018 } else if (code == clear + 1) { // end of stream code
4019 skip(s, len);
4020 while ((len = get8(s)) > 0)
4021 skip(s,len);
4022 return g->out;
4023 } else if (code <= avail) {
4024 if (first) return epuc("no clear code", "Corrupt GIF");
4025
4026 if (oldcode >= 0) {
4027 p = &g->codes[avail++];
4028 if (avail > 4096) return epuc("too many codes", "Corrupt GIF");
4029 p->prefix = (stbi__int16) oldcode;
4030 p->first = g->codes[oldcode].first;
4031 p->suffix = (code == avail) ? p->first : g->codes[code].first;
4032 } else if (code == avail)
4033 return epuc("illegal code in raster", "Corrupt GIF");
4034
4035 stbi_out_gif_code(g, (stbi__uint16) code);
4036
4037 if ((avail & codemask) == 0 && avail <= 0x0FFF) {
4038 codesize++;
4039 codemask = (1 << codesize) - 1;
4040 }
4041
4042 oldcode = code;
4043 } else {
4044 return epuc("illegal code in raster", "Corrupt GIF");
4045 }
4046 }
4047 }
4048 }
4049
4050 static void stbi_fill_gif_background(stbi_gif *g)
4051 {
4052 int i;
4053 stbi__uint8 *c = g->pal[g->bgindex];
4054 // @OPTIMIZE: write a dword at a time
4055 for (i = 0; i < g->w * g->h * 4; i += 4) {
4056 stbi__uint8 *p = &g->out[i];
4057 p[0] = c[2];
4058 p[1] = c[1];
4059 p[2] = c[0];
4060 p[3] = c[3];
4061 }
4062 }
4063
4064 // this function is designed to support animated gifs, although stb_image doesn't support it
4065 static stbi__uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp)
4066 {
4067 int i;
4068 stbi__uint8 *old_out = 0;
4069
4070 if (g->out == 0) {
4071 if (!stbi_gif_header(s, g, comp,0)) return 0; // failure_reason set by stbi_gif_header
4072 g->out = (stbi__uint8 *) malloc(4 * g->w * g->h);
4073 if (g->out == 0) return epuc("outofmem", "Out of memory");
4074 stbi_fill_gif_background(g);
4075 } else {
4076 // animated-gif-only path
4077 if (((g->eflags & 0x1C) >> 2) == 3) {
4078 old_out = g->out;
4079 g->out = (stbi__uint8 *) malloc(4 * g->w * g->h);
4080 if (g->out == 0) return epuc("outofmem", "Out of memory");
4081 memcpy(g->out, old_out, g->w*g->h*4);
4082 }
4083 }
4084
4085 for (;;) {
4086 switch (get8(s)) {
4087 case 0x2C: /* Image Descriptor */
4088 {
4089 stbi__int32 x, y, w, h;
4090 stbi__uint8 *o;
4091
4092 x = get16le(s);
4093 y = get16le(s);
4094 w = get16le(s);
4095 h = get16le(s);
4096 if (((x + w) > (g->w)) || ((y + h) > (g->h)))
4097 return epuc("bad Image Descriptor", "Corrupt GIF");
4098
4099 g->line_size = g->w * 4;
4100 g->start_x = x * 4;
4101 g->start_y = y * g->line_size;
4102 g->max_x = g->start_x + w * 4;
4103 g->max_y = g->start_y + h * g->line_size;
4104 g->cur_x = g->start_x;
4105 g->cur_y = g->start_y;
4106
4107 g->lflags = get8(s);
4108
4109 if (g->lflags & 0x40) {
4110 g->step = 8 * g->line_size; // first interlaced spacing
4111 g->parse = 3;
4112 } else {
4113 g->step = g->line_size;
4114 g->parse = 0;
4115 }
4116
4117 if (g->lflags & 0x80) {
4118 stbi_gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
4119 g->color_table = (stbi__uint8 *) g->lpal;
4120 } else if (g->flags & 0x80) {
4121 for (i=0; i < 256; ++i) // @OPTIMIZE: reset only the previous transparent
4122 g->pal[i][3] = 255;
4123 if (g->transparent >= 0 && (g->eflags & 0x01))
4124 g->pal[g->transparent][3] = 0;
4125 g->color_table = (stbi__uint8 *) g->pal;
4126 } else
4127 return epuc("missing color table", "Corrupt GIF");
4128
4129 o = stbi_process_gif_raster(s, g);
4130 if (o == NULL) return NULL;
4131
4132 if (req_comp && req_comp != 4)
4133 o = convert_format(o, 4, req_comp, g->w, g->h);
4134 return o;
4135 }
4136
4137 case 0x21: // Comment Extension.
4138 {
4139 int len;
4140 if (get8(s) == 0xF9) { // Graphic Control Extension.
4141 len = get8(s);
4142 if (len == 4) {
4143 g->eflags = get8(s);
4144 get16le(s); // delay
4145 g->transparent = get8(s);
4146 } else {
4147 skip(s, len);
4148 break;
4149 }
4150 }
4151 while ((len = get8(s)) != 0)
4152 skip(s, len);
4153 break;
4154 }
4155
4156 case 0x3B: // gif stream termination code
4157 return (stbi__uint8 *) 1;
4158
4159 default:
4160 return epuc("unknown code", "Corrupt GIF");
4161 }
4162 }
4163 }
4164
4165 static stbi_uc *stbi_gif_load(stbi *s, int *x, int *y, int *comp, int req_comp)
4166 {
4167 stbi__uint8 *u = 0;
4168 stbi_gif g={0};
4169
4170 u = stbi_gif_load_next(s, &g, comp, req_comp);
4171 if (u == (void *) 1) u = 0; // end of animated gif marker
4172 if (u) {
4173 *x = g.w;
4174 *y = g.h;
4175 }
4176
4177 return u;
4178 }
4179
4180 static int stbi_gif_info(stbi *s, int *x, int *y, int *comp)
4181 {
4182 return stbi_gif_info_raw(s,x,y,comp);
4183 }
4184
4185
4186 // *************************************************************************************************
4187 // Radiance RGBE HDR loader
4188 // originally by Nicolas Schulz
4189 #ifndef STBI_NO_HDR
4190 static int hdr_test(stbi *s)
4191 {
4192 const char *signature = "#?RADIANCE\n";
4193 int i;
4194 for (i=0; signature[i]; ++i)
4195 if (get8(s) != signature[i])
4196 return 0;
4197 return 1;
4198 }
4199
4200 static int stbi_hdr_test(stbi* s)
4201 {
4202 int r = hdr_test(s);
4203 stbi_rewind(s);
4204 return r;
4205 }
4206
4207 #define HDR_BUFLEN 1024
4208 static char *hdr_gettoken(stbi *z, char *buffer)
4209 {
4210 int len=0;
4211 char c = '\0';
4212
4213 c = (char) get8(z);
4214
4215 while (!at_eof(z) && c != '\n') {
4216 buffer[len++] = c;
4217 if (len == HDR_BUFLEN-1) {
4218 // flush to end of line
4219 while (!at_eof(z) && get8(z) != '\n')
4220 ;
4221 break;
4222 }
4223 c = (char) get8(z);
4224 }
4225
4226 buffer[len] = 0;
4227 return buffer;
4228 }
4229
4230 static void hdr_convert(float *output, stbi_uc *input, int req_comp)
4231 {
4232 if ( input[3] != 0 ) {
4233 float f1;
4234 // Exponent
4235 f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
4236 if (req_comp <= 2)
4237 output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
4238 else {
4239 output[0] = input[0] * f1;
4240 output[1] = input[1] * f1;
4241 output[2] = input[2] * f1;
4242 }
4243 if (req_comp == 2) output[1] = 1;
4244 if (req_comp == 4) output[3] = 1;
4245 } else {
4246 switch (req_comp) {
4247 case 4: output[3] = 1; /* fallthrough */
4248 case 3: output[0] = output[1] = output[2] = 0;
4249 break;
4250 case 2: output[1] = 1; /* fallthrough */
4251 case 1: output[0] = 0;
4252 break;
4253 }
4254 }
4255 }
4256
4257 static float *hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp)
4258 {
4259 char buffer[HDR_BUFLEN];
4260 char *token;
4261 int valid = 0;
4262 int width, height;
4263 stbi_uc *scanline;
4264 float *hdr_data;
4265 int len;
4266 unsigned char count, value;
4267 int i, j, k, c1,c2, z;
4268
4269
4270 // Check identifier
4271 if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
4272 return epf("not HDR", "Corrupt HDR image");
4273
4274 // Parse header
4275 for(;;) {
4276 token = hdr_gettoken(s,buffer);
4277 if (token[0] == 0) break;
4278 if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
4279 }
4280
4281 if (!valid) return epf("unsupported format", "Unsupported HDR format");
4282
4283 // Parse width and height
4284 // can't use sscanf() if we're not using stdio!
4285 token = hdr_gettoken(s,buffer);
4286 if (strncmp(token, "-Y ", 3)) return epf("unsupported data layout", "Unsupported HDR format");
4287 token += 3;
4288 height = (int) strtol(token, &token, 10);
4289 while (*token == ' ') ++token;
4290 if (strncmp(token, "+X ", 3)) return epf("unsupported data layout", "Unsupported HDR format");
4291 token += 3;
4292 width = (int) strtol(token, NULL, 10);
4293
4294 *x = width;
4295 *y = height;
4296
4297 *comp = 3;
4298 if (req_comp == 0) req_comp = 3;
4299
4300 // Read data
4301 hdr_data = (float *) malloc(height * width * req_comp * sizeof(float));
4302
4303 // Load image data
4304 // image data is stored as some number of sca
4305 if ( width < 8 || width >= 32768) {
4306 // Read flat data
4307 for (j=0; j < height; ++j) {
4308 for (i=0; i < width; ++i) {
4309 stbi_uc rgbe[4];
4310 main_decode_loop:
4311 getn(s, rgbe, 4);
4312 hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
4313 }
4314 }
4315 } else {
4316 // Read RLE-encoded data
4317 scanline = NULL;
4318
4319 for (j = 0; j < height; ++j) {
4320 c1 = get8(s);
4321 c2 = get8(s);
4322 len = get8(s);
4323 if (c1 != 2 || c2 != 2 || (len & 0x80)) {
4324 // not run-length encoded, so we have to actually use THIS data as a decoded
4325 // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
4326 stbi__uint8 rgbe[4];
4327 rgbe[0] = (stbi__uint8) c1;
4328 rgbe[1] = (stbi__uint8) c2;
4329 rgbe[2] = (stbi__uint8) len;
4330 rgbe[3] = (stbi__uint8) get8u(s);
4331 hdr_convert(hdr_data, rgbe, req_comp);
4332 i = 1;
4333 j = 0;
4334 free(scanline);
4335 goto main_decode_loop; // yes, this makes no sense
4336 }
4337 len <<= 8;
4338 len |= get8(s);
4339 if (len != width) { free(hdr_data); free(scanline); return epf("invalid decoded scanline length", "corrupt HDR"); }
4340 if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4);
4341
4342 for (k = 0; k < 4; ++k) {
4343 i = 0;
4344 while (i < width) {
4345 count = get8u(s);
4346 if (count > 128) {
4347 // Run
4348 value = get8u(s);
4349 count -= 128;
4350 for (z = 0; z < count; ++z)
4351 scanline[i++ * 4 + k] = value;
4352 } else {
4353 // Dump
4354 for (z = 0; z < count; ++z)
4355 scanline[i++ * 4 + k] = get8u(s);
4356 }
4357 }
4358 }
4359 for (i=0; i < width; ++i)
4360 hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
4361 }
4362 free(scanline);
4363 }
4364
4365 return hdr_data;
4366 }
4367
4368 static float *stbi_hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp)
4369 {
4370 return hdr_load(s,x,y,comp,req_comp);
4371 }
4372
4373 static int stbi_hdr_info(stbi *s, int *x, int *y, int *comp)
4374 {
4375 char buffer[HDR_BUFLEN];
4376 char *token;
4377 int valid = 0;
4378
4379 if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0) {
4380 stbi_rewind( s );
4381 return 0;
4382 }
4383
4384 for(;;) {
4385 token = hdr_gettoken(s,buffer);
4386 if (token[0] == 0) break;
4387 if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
4388 }
4389
4390 if (!valid) {
4391 stbi_rewind( s );
4392 return 0;
4393 }
4394 token = hdr_gettoken(s,buffer);
4395 if (strncmp(token, "-Y ", 3)) {
4396 stbi_rewind( s );
4397 return 0;
4398 }
4399 token += 3;
4400 *y = (int) strtol(token, &token, 10);
4401 while (*token == ' ') ++token;
4402 if (strncmp(token, "+X ", 3)) {
4403 stbi_rewind( s );
4404 return 0;
4405 }
4406 token += 3;
4407 *x = (int) strtol(token, NULL, 10);
4408 *comp = 3;
4409 return 1;
4410 }
4411 #endif // STBI_NO_HDR
4412
4413 static int stbi_bmp_info(stbi *s, int *x, int *y, int *comp)
4414 {
4415 int hsz;
4416 if (get8(s) != 'B' || get8(s) != 'M') {
4417 stbi_rewind( s );
4418 return 0;
4419 }
4420 skip(s,12);
4421 hsz = get32le(s);
4422 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) {
4423 stbi_rewind( s );
4424 return 0;
4425 }
4426 if (hsz == 12) {
4427 *x = get16le(s);
4428 *y = get16le(s);
4429 } else {
4430 *x = get32le(s);
4431 *y = get32le(s);
4432 }
4433 if (get16le(s) != 1) {
4434 stbi_rewind( s );
4435 return 0;
4436 }
4437 *comp = get16le(s) / 8;
4438 return 1;
4439 }
4440
4441 static int stbi_psd_info(stbi *s, int *x, int *y, int *comp)
4442 {
4443 int channelCount;
4444 if (get32(s) != 0x38425053) {
4445 stbi_rewind( s );
4446 return 0;
4447 }
4448 if (get16(s) != 1) {
4449 stbi_rewind( s );
4450 return 0;
4451 }
4452 skip(s, 6);
4453 channelCount = get16(s);
4454 if (channelCount < 0 || channelCount > 16) {
4455 stbi_rewind( s );
4456 return 0;
4457 }
4458 *y = get32(s);
4459 *x = get32(s);
4460 if (get16(s) != 8) {
4461 stbi_rewind( s );
4462 return 0;
4463 }
4464 if (get16(s) != 3) {
4465 stbi_rewind( s );
4466 return 0;
4467 }
4468 *comp = 4;
4469 return 1;
4470 }
4471
4472 static int stbi_pic_info(stbi *s, int *x, int *y, int *comp)
4473 {
4474 int act_comp=0,num_packets=0,chained;
4475 pic_packet_t packets[10];
4476
4477 skip(s, 92);
4478
4479 *x = get16(s);
4480 *y = get16(s);
4481 if (at_eof(s)) return 0;
4482 if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
4483 stbi_rewind( s );
4484 return 0;
4485 }
4486
4487 skip(s, 8);
4488
4489 do {
4490 pic_packet_t *packet;
4491
4492 if (num_packets==sizeof(packets)/sizeof(packets[0]))
4493 return 0;
4494
4495 packet = &packets[num_packets++];
4496 chained = get8(s);
4497 packet->size = get8u(s);
4498 packet->type = get8u(s);
4499 packet->channel = get8u(s);
4500 act_comp |= packet->channel;
4501
4502 if (at_eof(s)) {
4503 stbi_rewind( s );
4504 return 0;
4505 }
4506 if (packet->size != 8) {
4507 stbi_rewind( s );
4508 return 0;
4509 }
4510 } while (chained);
4511
4512 *comp = (act_comp & 0x10 ? 4 : 3);
4513
4514 return 1;
4515 }
4516
4517 static int stbi_info_main(stbi *s, int *x, int *y, int *comp)
4518 {
4519 if (stbi_jpeg_info(s, x, y, comp))
4520 return 1;
4521 if (stbi_png_info(s, x, y, comp))
4522 return 1;
4523 if (stbi_gif_info(s, x, y, comp))
4524 return 1;
4525 if (stbi_bmp_info(s, x, y, comp))
4526 return 1;
4527 if (stbi_psd_info(s, x, y, comp))
4528 return 1;
4529 if (stbi_pic_info(s, x, y, comp))
4530 return 1;
4531 #ifndef STBI_NO_HDR
4532 if (stbi_hdr_info(s, x, y, comp))
4533 return 1;
4534 #endif
4535 // test tga last because it's a crappy test!
4536 if (stbi_tga_info(s, x, y, comp))
4537 return 1;
4538 return e("unknown image type", "Image not of any known type, or corrupt");
4539 }
4540
4541 #ifndef STBI_NO_STDIO
4542 int stbi_info(char const *filename, int *x, int *y, int *comp)
4543 {
4544 FILE *f = fopen(filename, "rb");
4545 int result;
4546 if (!f) return e("can't fopen", "Unable to open file");
4547 result = stbi_info_from_file(f, x, y, comp);
4548 fclose(f);
4549 return result;
4550 }
4551
4552 int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
4553 {
4554 int r;
4555 stbi s;
4556 long pos = ftell(f);
4557 start_file(&s, f);
4558 r = stbi_info_main(&s,x,y,comp);
4559 fseek(f,pos,SEEK_SET);
4560 return r;
4561 }
4562 #endif // !STBI_NO_STDIO
4563
4564 int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
4565 {
4566 stbi s;
4567 start_mem(&s,buffer,len);
4568 return stbi_info_main(&s,x,y,comp);
4569 }
4570
4571 int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
4572 {
4573 stbi s;
4574 start_callbacks(&s, (stbi_io_callbacks *) c, user);
4575 return stbi_info_main(&s,x,y,comp);
4576 }
4577
4578 #endif // STBI_HEADER_FILE_ONLY
4579
4580 #if !defined(STBI_NO_STDIO) && defined(_MSC_VER) && _MSC_VER >= 1400
4581 #pragma warning(pop)
4582 #endif
4583
4584
4585 /*
4586 revision history:
4587 1.35 (2014-05-27)
4588 various warnings
4589 fix broken STBI_SIMD path
4590 fix bug where stbi_load_from_file no longer left file pointer in correct place
4591 fix broken non-easy path for 32-bit BMP (possibly never used)
4592 TGA optimization by Arseny Kapoulkine
4593 1.34 (unknown)
4594 use STBI_NOTUSED in resample_row_generic(), fix one more leak in tga failure case
4595 1.33 (2011-07-14)
4596 make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
4597 1.32 (2011-07-13)
4598 support for "info" function for all supported filetypes (SpartanJ)
4599 1.31 (2011-06-20)
4600 a few more leak fixes, bug in PNG handling (SpartanJ)
4601 1.30 (2011-06-11)
4602 added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
4603 removed deprecated format-specific test/load functions
4604 removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
4605 error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
4606 fix inefficiency in decoding 32-bit BMP (David Woo)
4607 1.29 (2010-08-16)
4608 various warning fixes from Aurelien Pocheville
4609 1.28 (2010-08-01)
4610 fix bug in GIF palette transparency (SpartanJ)
4611 1.27 (2010-08-01)
4612 cast-to-stbi__uint8 to fix warnings
4613 1.26 (2010-07-24)
4614 fix bug in file buffering for PNG reported by SpartanJ
4615 1.25 (2010-07-17)
4616 refix trans_data warning (Won Chun)
4617 1.24 (2010-07-12)
4618 perf improvements reading from files on platforms with lock-heavy fgetc()
4619 minor perf improvements for jpeg
4620 deprecated type-specific functions so we'll get feedback if they're needed
4621 attempt to fix trans_data warning (Won Chun)
4622 1.23 fixed bug in iPhone support
4623 1.22 (2010-07-10)
4624 removed image *writing* support
4625 stbi_info support from Jetro Lauha
4626 GIF support from Jean-Marc Lienher
4627 iPhone PNG-extensions from James Brown
4628 warning-fixes from Nicolas Schulz and Janez Zemva (i.e. Janez (U+017D)emva)
4629 1.21 fix use of 'stbi__uint8' in header (reported by jon blow)
4630 1.20 added support for Softimage PIC, by Tom Seddon
4631 1.19 bug in interlaced PNG corruption check (found by ryg)
4632 1.18 2008-08-02
4633 fix a threading bug (local mutable static)
4634 1.17 support interlaced PNG
4635 1.16 major bugfix - convert_format converted one too many pixels
4636 1.15 initialize some fields for thread safety
4637 1.14 fix threadsafe conversion bug
4638 header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
4639 1.13 threadsafe
4640 1.12 const qualifiers in the API
4641 1.11 Support installable IDCT, colorspace conversion routines
4642 1.10 Fixes for 64-bit (don't use "unsigned long")
4643 optimized upsampling by Fabian "ryg" Giesen
4644 1.09 Fix format-conversion for PSD code (bad global variables!)
4645 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
4646 1.07 attempt to fix C++ warning/errors again
4647 1.06 attempt to fix C++ warning/errors again
4648 1.05 fix TGA loading to return correct *comp and use good luminance calc
4649 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
4650 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
4651 1.02 support for (subset of) HDR files, float interface for preferred access to them
4652 1.01 fix bug: possible bug in handling right-side up bmps... not sure
4653 fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
4654 1.00 interface to zlib that skips zlib header
4655 0.99 correct handling of alpha in palette
4656 0.98 TGA loader by lonesock; dynamically add loaders (untested)
4657 0.97 jpeg errors on too large a file; also catch another malloc failure
4658 0.96 fix detection of invalid v value - particleman@mollyrocket forum
4659 0.95 during header scan, seek to markers in case of padding
4660 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
4661 0.93 handle jpegtran output; verbose errors
4662 0.92 read 4,8,16,24,32-bit BMP files of several formats
4663 0.91 output 24-bit Windows 3.0 BMP files
4664 0.90 fix a few more warnings; bump version number to approach 1.0
4665 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
4666 0.60 fix compiling as c++
4667 0.59 fix warnings: merge Dave Moore's -Wall fixes
4668 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
4669 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
4670 0.56 fix bug: zlib uncompressed mode len vs. nlen
4671 0.55 fix bug: restart_interval not initialized to 0
4672 0.54 allow NULL for 'int *comp'
4673 0.53 fix bug in png 3->4; speedup png decoding
4674 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
4675 0.51 obey req_comp requests, 1-component jpegs return as 1-component,
4676 on 'test' only check type, not whether we support this variant
4677 0.50 first released version
4678 */