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