363ce427e3fb4a6700358a8f32ae183e97aa90f9
1 /* stb_image - v2.23 - public domain image loader - http://nothings.org/stb
2 no warranty implied; use at your own risk
4 Do this:
5 #define STB_IMAGE_IMPLEMENTATION
6 before you include this file in *one* C or C++ file to create the implementation.
8 // i.e. it should look like this:
9 #include ...
10 #include ...
11 #include ...
12 #define STB_IMAGE_IMPLEMENTATION
13 #include "stb_image.h"
15 You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.
16 And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
19 QUICK NOTES:
20 Primarily of interest to game developers and other people who can
21 avoid problematic images and only need the trivial interface
23 JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
24 PNG 1/2/4/8/16-bit-per-channel
26 TGA (not sure what subset, if a subset)
27 BMP non-1bpp, non-RLE
28 PSD (composited view only, no extra channels, 8/16 bit-per-channel)
30 GIF (*comp always reports as 4-channel)
31 HDR (radiance rgbE format)
32 PIC (Softimage PIC)
33 PNM (PPM and PGM binary only)
35 Animated GIF still needs a proper API, but here's one way to do it:
36 http://gist.github.com/urraka/685d9a6340b26b830d49
38 - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
39 - decode from arbitrary I/O callbacks
40 - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)
42 Full documentation under "DOCUMENTATION" below.
45 LICENSE
47 See end of file for license information.
49 RECENT REVISION HISTORY:
51 2.23 (2019-08-11) fix clang static analysis warning
52 2.22 (2019-03-04) gif fixes, fix warnings
53 2.21 (2019-02-25) fix typo in comment
54 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
55 2.19 (2018-02-11) fix warning
56 2.18 (2018-01-30) fix warnings
57 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
58 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes
59 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC
60 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
61 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
62 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
63 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
64 RGB-format JPEG; remove white matting in PSD;
65 allocate large structures on the stack;
66 correct channel count for PNG & BMP
67 2.10 (2016-01-22) avoid warning introduced in 2.09
68 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
70 See end of file for full revision history.
73 ============================ Contributors =========================
75 Image formats Extensions, features
76 Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info)
77 Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info)
78 Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG)
79 Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks)
80 Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)
81 Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)
82 Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)
83 github:urraka (animated gif) Junggon Kim (PNM comments)
84 Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA)
85 socks-the-fox (16-bit PNG)
86 Jeremy Sawicki (handle all ImageNet JPGs)
87 Optimizations & bugfixes Mikhail Morozov (1-bit BMP)
88 Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
89 Arseny Kapoulkine
90 John-Mark Allen
91 Carmelo J Fdez-Aguera
93 Bug & warning fixes
94 Marc LeBlanc David Woo Guillaume George Martins Mozeiko
95 Christpher Lloyd Jerry Jansson Joseph Thomson Phil Jordan
96 Dave Moore Roy Eltham Hayaki Saito Nathan Reed
97 Won Chun Luke Graham Johan Duparc Nick Verigakis
98 the Horde3D community Thomas Ruf Ronny Chevalier github:rlyeh
99 Janez Zemva John Bartholomew Michal Cichon github:romigrou
100 Jonathan Blow Ken Hamada Tero Hanninen github:svdijk
101 Laurent Gomila Cort Stratton Sergio Gonzalez github:snagar
102 Aruelien Pocheville Thibault Reuille Cass Everitt github:Zelex
103 Ryamond Barbiero Paul Du Bois Engin Manap github:grim210
104 Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw
105 Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus
106 Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppo
107 Christian Floisand Kevin Schmidt JR Smith github:darealshinji
108 Blazej Dariusz Roszkowski github:Michaelangel007
109 */
111 #ifndef STBI_INCLUDE_STB_IMAGE_H
112 #define STBI_INCLUDE_STB_IMAGE_H
114 // DOCUMENTATION
115 //
116 // Limitations:
117 // - no 12-bit-per-channel JPEG
118 // - no JPEGs with arithmetic coding
119 // - GIF always returns *comp=4
120 //
121 // Basic usage (see HDR discussion below for HDR usage):
122 // int x,y,n;
123 // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
124 // // ... process data if not NULL ...
125 // // ... x = width, y = height, n = # 8-bit components per pixel ...
126 // // ... replace '0' with '1'..'4' to force that many components per pixel
127 // // ... but 'n' will always be the number that it would have been if you said 0
128 // stbi_image_free(data)
129 //
130 // Standard parameters:
131 // int *x -- outputs image width in pixels
132 // int *y -- outputs image height in pixels
133 // int *channels_in_file -- outputs # of image components in image file
134 // int desired_channels -- if non-zero, # of image components requested in result
135 //
136 // The return value from an image loader is an 'unsigned char *' which points
137 // to the pixel data, or NULL on an allocation failure or if the image is
138 // corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
139 // with each pixel consisting of N interleaved 8-bit components; the first
140 // pixel pointed to is top-left-most in the image. There is no padding between
141 // image scanlines or between pixels, regardless of format. The number of
142 // components N is 'desired_channels' if desired_channels is non-zero, or
143 // *channels_in_file otherwise. If desired_channels is non-zero,
144 // *channels_in_file has the number of components that _would_ have been
145 // output otherwise. E.g. if you set desired_channels to 4, you will always
146 // get RGBA output, but you can check *channels_in_file to see if it's trivially
147 // opaque because e.g. there were only 3 channels in the source image.
148 //
149 // An output image with N components has the following components interleaved
150 // in this order in each pixel:
151 //
152 // N=#comp components
153 // 1 grey
154 // 2 grey, alpha
155 // 3 red, green, blue
156 // 4 red, green, blue, alpha
157 //
158 // If image loading fails for any reason, the return value will be NULL,
159 // and *x, *y, *channels_in_file will be unchanged. The function
160 // stbi_failure_reason() can be queried for an extremely brief, end-user
161 // unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS
162 // to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
163 // more user-friendly ones.
164 //
165 // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
166 //
167 // ===========================================================================
168 //
169 // UNICODE:
170 //
171 // If compiling for Windows and you wish to use Unicode filenames, compile
172 // with
173 // #define STBI_WINDOWS_UTF8
174 // and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert
175 // Windows wchar_t filenames to utf8.
176 //
177 // ===========================================================================
178 //
179 // Philosophy
180 //
181 // stb libraries are designed with the following priorities:
182 //
183 // 1. easy to use
184 // 2. easy to maintain
185 // 3. good performance
186 //
187 // Sometimes I let "good performance" creep up in priority over "easy to maintain",
188 // and for best performance I may provide less-easy-to-use APIs that give higher
189 // performance, in addition to the easy-to-use ones. Nevertheless, it's important
190 // to keep in mind that from the standpoint of you, a client of this library,
191 // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.
192 //
193 // Some secondary priorities arise directly from the first two, some of which
194 // provide more explicit reasons why performance can't be emphasized.
195 //
196 // - Portable ("ease of use")
197 // - Small source code footprint ("easy to maintain")
198 // - No dependencies ("ease of use")
199 //
200 // ===========================================================================
201 //
202 // I/O callbacks
203 //
204 // I/O callbacks allow you to read from arbitrary sources, like packaged
205 // files or some other source. Data read from callbacks are processed
206 // through a small internal buffer (currently 128 bytes) to try to reduce
207 // overhead.
208 //
209 // The three functions you must define are "read" (reads some bytes of data),
210 // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
211 //
212 // ===========================================================================
213 //
214 // SIMD support
215 //
216 // The JPEG decoder will try to automatically use SIMD kernels on x86 when
217 // supported by the compiler. For ARM Neon support, you must explicitly
218 // request it.
219 //
220 // (The old do-it-yourself SIMD API is no longer supported in the current
221 // code.)
222 //
223 // On x86, SSE2 will automatically be used when available based on a run-time
224 // test; if not, the generic C versions are used as a fall-back. On ARM targets,
225 // the typical path is to have separate builds for NEON and non-NEON devices
226 // (at least this is true for iOS and Android). Therefore, the NEON support is
227 // toggled by a build flag: define STBI_NEON to get NEON loops.
228 //
229 // If for some reason you do not want to use any of SIMD code, or if
230 // you have issues compiling it, you can disable it entirely by
231 // defining STBI_NO_SIMD.
232 //
233 // ===========================================================================
234 //
235 // HDR image support (disable by defining STBI_NO_HDR)
236 //
237 // stb_image supports loading HDR images in general, and currently the Radiance
238 // .HDR file format specifically. You can still load any file through the existing
239 // interface; if you attempt to load an HDR file, it will be automatically remapped
240 // to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
241 // both of these constants can be reconfigured through this interface:
242 //
243 // stbi_hdr_to_ldr_gamma(2.2f);
244 // stbi_hdr_to_ldr_scale(1.0f);
245 //
246 // (note, do not use _inverse_ constants; stbi_image will invert them
247 // appropriately).
248 //
249 // Additionally, there is a new, parallel interface for loading files as
250 // (linear) floats to preserve the full dynamic range:
251 //
252 // float *data = stbi_loadf(filename, &x, &y, &n, 0);
253 //
254 // If you load LDR images through this interface, those images will
255 // be promoted to floating point values, run through the inverse of
256 // constants corresponding to the above:
257 //
258 // stbi_ldr_to_hdr_scale(1.0f);
259 // stbi_ldr_to_hdr_gamma(2.2f);
260 //
261 // Finally, given a filename (or an open file or memory block--see header
262 // file for details) containing image data, you can query for the "most
263 // appropriate" interface to use (that is, whether the image is HDR or
264 // not), using:
265 //
266 // stbi_is_hdr(char *filename);
267 //
268 // ===========================================================================
269 //
270 // iPhone PNG support:
271 //
272 // By default we convert iphone-formatted PNGs back to RGB, even though
273 // they are internally encoded differently. You can disable this conversion
274 // by calling stbi_convert_iphone_png_to_rgb(0), in which case
275 // you will always just get the native iphone "format" through (which
276 // is BGR stored in RGB).
277 //
278 // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
279 // pixel to remove any premultiplied alpha *only* if the image file explicitly
280 // says there's premultiplied data (currently only happens in iPhone images,
281 // and only if iPhone convert-to-rgb processing is on).
282 //
283 // ===========================================================================
284 //
285 // ADDITIONAL CONFIGURATION
286 //
287 // - You can suppress implementation of any of the decoders to reduce
288 // your code footprint by #defining one or more of the following
289 // symbols before creating the implementation.
290 //
291 // STBI_NO_JPEG
292 // STBI_NO_PNG
293 // STBI_NO_BMP
294 // STBI_NO_PSD
295 // STBI_NO_TGA
296 // STBI_NO_GIF
297 // STBI_NO_HDR
298 // STBI_NO_PIC
299 // STBI_NO_PNM (.ppm and .pgm)
300 //
301 // - You can request *only* certain decoders and suppress all other ones
302 // (this will be more forward-compatible, as addition of new decoders
303 // doesn't require you to disable them explicitly):
304 //
305 // STBI_ONLY_JPEG
306 // STBI_ONLY_PNG
307 // STBI_ONLY_BMP
308 // STBI_ONLY_PSD
309 // STBI_ONLY_TGA
310 // STBI_ONLY_GIF
311 // STBI_ONLY_HDR
312 // STBI_ONLY_PIC
313 // STBI_ONLY_PNM (.ppm and .pgm)
314 //
315 // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
316 // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
317 //
320 #ifndef STBI_NO_STDIO
321 #include <stdio.h>
324 #define STBI_VERSION 1
326 enum
327 {
334 };
336 #include <stdlib.h>
340 #ifdef __cplusplus
342 #endif
344 #ifndef STBIDEF
345 #ifdef STB_IMAGE_STATIC
346 #define STBIDEF static
347 #else
348 #define STBIDEF extern
349 #endif
350 #endif
352 //////////////////////////////////////////////////////////////////////////////
353 //
354 // PRIMARY API - works on images of any type
355 //
357 //
358 // load image by filename, open file, or memory buffer
359 //
362 {
363 int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
364 void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
368 ////////////////////////////////////
369 //
370 // 8-bits-per-channel interface
371 //
373 STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
374 STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);
376 #ifndef STBI_NO_STDIO
377 STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
378 STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
379 // for stbi_load_from_file, file pointer is left pointing immediately after image
380 #endif
382 #ifndef STBI_NO_GIF
383 STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
384 #endif
386 #ifdef STBI_WINDOWS_UTF8
388 #endif
390 ////////////////////////////////////
391 //
392 // 16-bits-per-channel interface
393 //
395 STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
396 STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
398 #ifndef STBI_NO_STDIO
399 STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
400 STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
401 #endif
403 ////////////////////////////////////
404 //
405 // float-per-channel interface
406 //
407 #ifndef STBI_NO_LINEAR
408 STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
409 STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
411 #ifndef STBI_NO_STDIO
412 STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
413 STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
414 #endif
415 #endif
417 #ifndef STBI_NO_HDR
422 #ifndef STBI_NO_LINEAR
427 // stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR
430 #ifndef STBI_NO_STDIO
436 // get a VERY brief reason for failure
437 // NOT THREADSAFE
440 // free the loaded image -- this is just free()
443 // get image dimensions & components without fully decoding
445 STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
449 #ifndef STBI_NO_STDIO
454 #endif
458 // for image formats that explicitly notate that they have premultiplied alpha,
459 // we just return the colors as stored in the file. set this flag to force
460 // unpremultiplication. results are undefined if the unpremultiply overflow.
463 // indicate whether we should process iphone images back to canonical format,
464 // or just pass them through "as-is"
467 // flip the image vertically, so the first pixel in the output array is the bottom left
470 // ZLIB client - used by PNG, available for other purposes
472 STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
473 STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
478 STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
481 #ifdef __cplusplus
482 }
483 #endif
485 //
486 //
487 //// end header file /////////////////////////////////////////////////////
490 #ifdef STB_IMAGE_IMPLEMENTATION
492 #if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \
493 || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \
494 || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \
495 || defined(STBI_ONLY_ZLIB)
496 #ifndef STBI_ONLY_JPEG
497 #define STBI_NO_JPEG
498 #endif
499 #ifndef STBI_ONLY_PNG
500 #define STBI_NO_PNG
501 #endif
502 #ifndef STBI_ONLY_BMP
503 #define STBI_NO_BMP
504 #endif
505 #ifndef STBI_ONLY_PSD
506 #define STBI_NO_PSD
507 #endif
508 #ifndef STBI_ONLY_TGA
509 #define STBI_NO_TGA
510 #endif
511 #ifndef STBI_ONLY_GIF
512 #define STBI_NO_GIF
513 #endif
514 #ifndef STBI_ONLY_HDR
515 #define STBI_NO_HDR
516 #endif
517 #ifndef STBI_ONLY_PIC
518 #define STBI_NO_PIC
519 #endif
520 #ifndef STBI_ONLY_PNM
521 #define STBI_NO_PNM
522 #endif
523 #endif
525 #if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB)
526 #define STBI_NO_ZLIB
527 #endif
530 #include <stdarg.h>
532 #include <stdlib.h>
533 #include <string.h>
534 #include <limits.h>
536 #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
538 #endif
540 #ifndef STBI_NO_STDIO
541 #include <stdio.h>
542 #endif
544 #ifndef STBI_ASSERT
545 #include <assert.h>
546 #define STBI_ASSERT(x) assert(x)
547 #endif
549 #ifdef __cplusplus
551 #else
552 #define STBI_EXTERN extern
553 #endif
556 #ifndef _MSC_VER
557 #ifdef __cplusplus
558 #define stbi_inline inline
559 #else
560 #define stbi_inline
561 #endif
562 #else
563 #define stbi_inline __forceinline
564 #endif
567 #ifdef _MSC_VER
572 #else
573 #include <stdint.h>
578 #endif
580 // should produce compiler error if size is wrong
583 #ifdef _MSC_VER
584 #define STBI_NOTUSED(v) (void)(v)
585 #else
586 #define STBI_NOTUSED(v) (void)sizeof(v)
587 #endif
589 #ifdef _MSC_VER
590 #define STBI_HAS_LROTL
591 #endif
593 #ifdef STBI_HAS_LROTL
594 #define stbi_lrot(x,y) _lrotl(x,y)
595 #else
596 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
597 #endif
599 #if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))
600 // ok
601 #elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED)
602 // ok
603 #else
604 #error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)."
605 #endif
607 #ifndef STBI_MALLOC
608 #define STBI_MALLOC(sz) malloc(sz)
609 #define STBI_REALLOC(p,newsz) realloc(p,newsz)
610 #define STBI_FREE(p) free(p)
611 #endif
613 #ifndef STBI_REALLOC_SIZED
614 #define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)
615 #endif
617 // x86/x64 detection
618 #if defined(__x86_64__) || defined(_M_X64)
619 #define STBI__X64_TARGET
620 #elif defined(__i386) || defined(_M_IX86)
621 #define STBI__X86_TARGET
622 #endif
624 #if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD)
625 // gcc doesn't support sse2 intrinsics unless you compile with -msse2,
626 // which in turn means it gets to use SSE2 everywhere. This is unfortunate,
627 // but previous attempts to provide the SSE2 functions with runtime
628 // detection caused numerous issues. The way architecture extensions are
629 // exposed in GCC/Clang is, sadly, not really suited for one-file libs.
630 // New behavior: if compiled with -msse2, we use SSE2 without any
631 // detection; if not, we don't use it at all.
632 #define STBI_NO_SIMD
633 #endif
635 #if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)
636 // Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET
637 //
638 // 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the
639 // Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant.
640 // As a result, enabling SSE2 on 32-bit MinGW is dangerous when not
641 // simultaneously enabling "-mstackrealign".
642 //
643 // See https://github.com/nothings/stb/issues/81 for more information.
644 //
645 // So default to no SSE2 on 32-bit MinGW. If you've read this far and added
646 // -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2.
647 #define STBI_NO_SIMD
648 #endif
650 #if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))
651 #define STBI_SSE2
652 #include <emmintrin.h>
654 #ifdef _MSC_VER
659 {
663 }
664 #else
666 {
668 __asm {
670 cpuid
671 mov res,edx
672 }
674 }
675 #endif
677 #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
679 #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
681 {
684 }
685 #endif
688 #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
690 #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
692 {
693 // If we're even attempting to compile this on GCC/Clang, that means
694 // -msse2 is on, which means the compiler is allowed to use SSE2
695 // instructions at will, and so are we.
697 }
698 #endif
700 #endif
701 #endif
703 // ARM NEON
704 #if defined(STBI_NO_SIMD) && defined(STBI_NEON)
705 #undef STBI_NEON
706 #endif
708 #ifdef STBI_NEON
709 #include <arm_neon.h>
710 // assume GCC or Clang on ARM targets
711 #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
712 #endif
714 #ifndef STBI_SIMD_ALIGN
715 #define STBI_SIMD_ALIGN(type, name) type name
716 #endif
718 ///////////////////////////////////////////////
719 //
720 // stbi__context struct and start_xxx functions
722 // stbi__context structure is our basic context used by all images, so it
723 // contains all the IO context, plus some basic image information
725 {
729 stbi_io_callbacks io;
743 // initialize a memory-decode context
745 {
750 }
752 // initialize a callback-based context
754 {
762 }
764 #ifndef STBI_NO_STDIO
767 {
769 }
772 {
774 }
777 {
779 }
782 {
783 stbi__stdio_read,
784 stbi__stdio_skip,
785 stbi__stdio_eof,
786 };
789 {
791 }
793 //static void stop_file(stbi__context *s) { }
798 {
799 // conceptually rewind SHOULD rewind to the beginning of the stream,
800 // but we just rewind to the beginning of the initial buffer, because
801 // we only use it after doing 'test', which only ever looks at at most 92 bytes
804 }
806 enum
807 {
808 STBI_ORDER_RGB,
809 STBI_ORDER_BGR
810 };
813 {
819 #ifndef STBI_NO_JPEG
821 static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
823 #endif
825 #ifndef STBI_NO_PNG
827 static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
830 #endif
832 #ifndef STBI_NO_BMP
834 static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
836 #endif
838 #ifndef STBI_NO_TGA
840 static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
842 #endif
844 #ifndef STBI_NO_PSD
846 static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);
849 #endif
851 #ifndef STBI_NO_HDR
853 static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
855 #endif
857 #ifndef STBI_NO_PIC
859 static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
861 #endif
863 #ifndef STBI_NO_GIF
865 static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
866 static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
868 #endif
870 #ifndef STBI_NO_PNM
872 static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
874 #endif
876 // this is not threadsafe
880 {
882 }
885 {
888 }
891 {
893 }
895 // stb_image uses ints pervasively, including for offset calculations.
896 // therefore the largest decoded image size we can support with the
897 // current code, even on 64-bit targets, is INT_MAX. this is not a
898 // significant limitation for the intended use case.
899 //
900 // we do, however, need to make sure our size calculations don't
901 // overflow. hence a few helper functions for size calculations that
902 // multiply integers together, making sure that they're non-negative
903 // and no overflow occurs.
905 // return 1 if the sum is valid, 0 on overflow.
906 // negative terms are considered invalid.
908 {
910 // now 0 <= b <= INT_MAX, hence also
911 // 0 <= INT_MAX - b <= INTMAX.
912 // And "a + b <= INT_MAX" (which might overflow) is the
913 // same as a <= INT_MAX - b (no overflow)
915 }
917 // returns 1 if the product is valid, 0 on overflow.
918 // negative factors are considered invalid.
920 {
923 // portable way to check for no overflows in a*b
925 }
927 // returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
929 {
931 }
933 // returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow
935 {
938 }
940 // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
941 #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
943 {
946 }
947 #endif
949 // mallocs with size overflow checking
951 {
954 }
957 {
960 }
962 #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
964 {
967 }
968 #endif
970 // stbi__err - error
971 // stbi__errpf - error returning pointer to float
972 // stbi__errpuc - error returning pointer to unsigned char
974 #ifdef STBI_NO_FAILURE_STRINGS
975 #define stbi__err(x,y) 0
976 #elif defined(STBI_FAILURE_USERMSG)
977 #define stbi__err(x,y) stbi__err(y)
978 #else
979 #define stbi__err(x,y) stbi__err(x)
980 #endif
982 #define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL))
983 #define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL))
986 {
988 }
990 #ifndef STBI_NO_LINEAR
992 #endif
994 #ifndef STBI_NO_HDR
996 #endif
1001 {
1003 }
1005 static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
1006 {
1009 ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order
1012 #ifndef STBI_NO_JPEG
1014 #endif
1015 #ifndef STBI_NO_PNG
1017 #endif
1018 #ifndef STBI_NO_BMP
1020 #endif
1021 #ifndef STBI_NO_GIF
1023 #endif
1024 #ifndef STBI_NO_PSD
1026 #endif
1027 #ifndef STBI_NO_PIC
1029 #endif
1030 #ifndef STBI_NO_PNM
1032 #endif
1034 #ifndef STBI_NO_HDR
1038 }
1039 #endif
1041 #ifndef STBI_NO_TGA
1042 // test tga last because it's a crappy test!
1045 #endif
1048 }
1051 {
1060 reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling
1064 }
1067 {
1076 enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff
1080 }
1083 {
1092 // swap row0 with row1
1102 }
1103 }
1104 }
1106 #ifndef STBI_NO_GIF
1108 {
1116 }
1117 }
1118 #endif
1120 static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1121 {
1122 stbi__result_info ri;
1130 result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
1132 }
1134 // @TODO: move stbi__convert_format to here
1139 }
1142 }
1144 static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1145 {
1146 stbi__result_info ri;
1156 }
1158 // @TODO: move stbi__convert_format16 to here
1159 // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision
1164 }
1167 }
1169 #if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR)
1171 {
1175 }
1176 }
1177 #endif
1179 #ifndef STBI_NO_STDIO
1181 #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1182 STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
1183 STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
1184 #endif
1186 #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1188 {
1189 return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
1190 }
1191 #endif
1194 {
1196 #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1205 #if _MSC_VER >= 1400
1208 #else
1210 #endif
1212 #elif defined(_MSC_VER) && _MSC_VER >= 1400
1215 #else
1217 #endif
1219 }
1223 {
1230 }
1233 {
1235 stbi__context s;
1239 // need to 'unget' all the characters in the IO buffer
1241 }
1243 }
1246 {
1248 stbi__context s;
1252 // need to 'unget' all the characters in the IO buffer
1254 }
1256 }
1259 {
1266 }
1271 STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)
1272 {
1273 stbi__context s;
1276 }
1278 STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)
1279 {
1280 stbi__context s;
1283 }
1285 STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
1286 {
1287 stbi__context s;
1290 }
1292 STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
1293 {
1294 stbi__context s;
1297 }
1299 #ifndef STBI_NO_GIF
1300 STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
1301 {
1303 stbi__context s;
1309 }
1312 }
1313 #endif
1315 #ifndef STBI_NO_LINEAR
1317 {
1319 #ifndef STBI_NO_HDR
1321 stbi__result_info ri;
1326 }
1327 #endif
1332 }
1334 STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
1335 {
1336 stbi__context s;
1339 }
1341 STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
1342 {
1343 stbi__context s;
1346 }
1348 #ifndef STBI_NO_STDIO
1350 {
1357 }
1360 {
1361 stbi__context s;
1364 }
1369 // these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is
1370 // defined, for API simplicity; if STBI_NO_LINEAR is defined, it always
1371 // reports false!
1374 {
1375 #ifndef STBI_NO_HDR
1376 stbi__context s;
1379 #else
1383 #endif
1384 }
1386 #ifndef STBI_NO_STDIO
1388 {
1394 }
1396 }
1399 {
1400 #ifndef STBI_NO_HDR
1403 stbi__context s;
1408 #else
1411 #endif
1412 }
1416 {
1417 #ifndef STBI_NO_HDR
1418 stbi__context s;
1421 #else
1425 #endif
1426 }
1428 #ifndef STBI_NO_LINEAR
1433 #endif
1441 //////////////////////////////////////////////////////////////////////////////
1442 //
1443 // Common code used by all image loaders
1444 //
1446 enum
1447 {
1449 STBI__SCAN_type,
1450 STBI__SCAN_header
1451 };
1454 {
1457 // at end of file, treat same as if from memory, but need to handle case
1458 // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file
1466 }
1467 }
1470 {
1476 }
1478 }
1481 {
1484 // if feof() is true, check if buffer = end
1485 // special case: we've only got the special 0 character at the end
1487 }
1490 }
1493 {
1497 }
1504 }
1505 }
1507 }
1510 {
1522 }
1523 }
1531 }
1534 {
1537 }
1540 {
1543 }
1545 #if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)
1546 // nothing
1547 #else
1549 {
1552 }
1553 #endif
1555 #ifndef STBI_NO_BMP
1557 {
1560 }
1561 #endif
1566 //////////////////////////////////////////////////////////////////////////////
1567 //
1568 // generic converter from built-in img_n to req_comp
1569 // individual types do this automatically as much as possible (e.g. jpeg
1570 // does all cases internally since it needs to colorspace convert anyway,
1571 // and it never has alpha, so very few cases ). png can automatically
1572 // interleave an alpha=255 channel, but falls back to this for other cases
1573 //
1574 // assume data buffer is malloced, so malloc a new one and free that one
1575 // only failure mode is malloc failing
1578 {
1580 }
1582 static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
1583 {
1594 }
1600 #define STBI__COMBO(a,b) ((a)*8+(b))
1601 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1602 // convert source image with img_n components to one with req_comp components;
1603 // avoid switch per pixel, so use switch per scanline and massive macros
1618 }
1619 #undef STBI__CASE
1620 }
1624 }
1627 {
1629 }
1631 static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
1632 {
1643 }
1649 #define STBI__COMBO(a,b) ((a)*8+(b))
1650 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1651 // convert source image with img_n components to one with req_comp components;
1652 // avoid switch per pixel, so use switch per scanline and massive macros
1667 }
1668 #undef STBI__CASE
1669 }
1673 }
1675 #ifndef STBI_NO_LINEAR
1677 {
1683 // compute number of non-alpha components
1688 }
1689 }
1693 }
1694 }
1697 }
1698 #endif
1700 #ifndef STBI_NO_HDR
1701 #define stbi__float2int(x) ((int) (x))
1703 {
1709 // compute number of non-alpha components
1717 }
1723 }
1724 }
1727 }
1728 #endif
1730 //////////////////////////////////////////////////////////////////////////////
1731 //
1732 // "baseline" JPEG/JFIF decoder
1733 //
1734 // simple implementation
1735 // - doesn't support delayed output of y-dimension
1736 // - simple interface (only one output format: 8-bit interleaved RGB)
1737 // - doesn't try to recover corrupt jpegs
1738 // - doesn't allow partial loading, loading multiple at once
1739 // - still fast on x86 (copying globals into locals doesn't help x86)
1740 // - allocates lots of intermediate memory (full size of all components)
1741 // - non-interleaved case requires this anyway
1742 // - allows good upsampling (see next)
1743 // high-quality
1744 // - upsampled channels are bilinearly interpolated, even across blocks
1745 // - quality integer IDCT derived from IJG's 'slow'
1746 // performance
1747 // - fast huffman; reasonable integer IDCT
1748 // - some SIMD kernels for common paths on targets with SSE2/NEON
1749 // - uses a lot of intermediate memory, could cache poorly
1751 #ifndef STBI_NO_JPEG
1753 // huffman decoding acceleration
1757 {
1759 // weirdly, repacking this into AoS is a 10% speed loss, instead of a win
1768 {
1775 // sizes for components, interleaved MCUs
1780 // definition of jpeg image component
1781 struct
1782 {
1815 // kernels
1817 void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step);
1818 stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs);
1822 {
1825 // build size list for each symbol (from JPEG spec)
1831 // compute actual symbols (from jpeg spec)
1835 // compute delta to add to code to compute symbol id
1841 }
1842 // compute largest code + 1 for this size, preshifted as needed later
1845 }
1848 // build non-spec acceleration table; 255 is flag for not-accelerated
1857 }
1858 }
1859 }
1861 }
1863 // build a table that decodes both magnitude and value of small ACs in
1864 // one go.
1866 {
1878 // magnitude code followed by receive_extend code
1882 // if the result is small enough, we can fit it in fast_ac table
1885 }
1886 }
1887 }
1888 }
1891 {
1901 }
1902 }
1906 }
1908 // (1 << n) - 1
1909 static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
1911 // decode a jpeg huffman value from the bitstream
1913 {
1919 // look at the top FAST_BITS and determine what symbol ID it is,
1920 // if the code is <= FAST_BITS
1930 }
1932 // naive test is to shift the code_buffer down so k bits are
1933 // valid, then test against maxcode. To speed this up, we've
1934 // preshifted maxcode left so that it has (16-k) 0s at the
1935 // end; in other words, regardless of the number of bits, it
1936 // wants to be compared against something shifted to have 16;
1937 // that way we don't need to shift inside the loop.
1943 // error! code not found
1946 }
1951 // convert the huffman code to the symbol id
1953 STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
1955 // convert the id to a symbol
1959 }
1961 // bias[n] = (-1<<n) + 1
1962 static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};
1964 // combined JPEG 'receive' and JPEG 'extend', since baseline
1965 // always extends everything it receives.
1967 {
1979 }
1981 // get some unsigned bits
1983 {
1991 }
1994 {
2001 }
2003 // given a value that's at position X in the zigzag stream,
2004 // where does it appear in the 8x8 matrix coded as row-major?
2006 {
2015 // let corrupt input sample past end
2018 };
2020 // decode one 64-entry block--
2021 static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, stbi__int16 *fac, int b, stbi__uint16 *dequant)
2022 {
2030 // 0 all the ac values now so we can do it 32-bits at a time
2038 // decode AC components, see JPEG spec
2051 // decode into unzigzag'd location
2064 // decode into unzigzag'd location
2067 }
2068 }
2071 }
2073 static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b)
2074 {
2082 // first scan for DC coefficient, must be first
2091 // refinement scan for DC coefficient
2094 }
2096 }
2098 // @OPTIMIZE: store non-zigzagged during the decode passes,
2099 // and only de-zigzag when dequantizing
2100 static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac)
2101 {
2111 }
2139 }
2145 }
2146 }
2149 // refinement scan for these AC coefficients
2162 else
2164 }
2165 }
2170 int rs = stbi__jpeg_huff_decode(j, hac); // @OPTIMIZE see if we can use the fast path here, advance-by-r is so slow, eh
2181 // r=15 s=0 should write 16 0s, so we just do
2182 // a run of 15 0s and then write s (which is 0),
2183 // so we don't have to do anything special here
2184 }
2187 // sign bit
2190 else
2192 }
2194 // advance by r
2202 else
2204 }
2209 }
2211 }
2212 }
2214 }
2215 }
2217 }
2219 // take a -128..127 value and stbi__clamp it and convert to 0..255
2221 {
2222 // trick to use a single test to catch both cases
2226 }
2228 }
2230 #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5)))
2231 #define stbi__fsh(x) ((x) * 4096)
2233 // derived from jidctint -- DCT_ISLOW
2234 #define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
2235 int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
2236 p2 = s2; \
2237 p3 = s6; \
2238 p1 = (p2+p3) * stbi__f2f(0.5411961f); \
2239 t2 = p1 + p3*stbi__f2f(-1.847759065f); \
2240 t3 = p1 + p2*stbi__f2f( 0.765366865f); \
2241 p2 = s0; \
2242 p3 = s4; \
2243 t0 = stbi__fsh(p2+p3); \
2244 t1 = stbi__fsh(p2-p3); \
2245 x0 = t0+t3; \
2246 x3 = t0-t3; \
2247 x1 = t1+t2; \
2248 x2 = t1-t2; \
2249 t0 = s7; \
2250 t1 = s5; \
2251 t2 = s3; \
2252 t3 = s1; \
2253 p3 = t0+t2; \
2254 p4 = t1+t3; \
2255 p1 = t0+t3; \
2256 p2 = t1+t2; \
2257 p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
2258 t0 = t0*stbi__f2f( 0.298631336f); \
2259 t1 = t1*stbi__f2f( 2.053119869f); \
2260 t2 = t2*stbi__f2f( 3.072711026f); \
2261 t3 = t3*stbi__f2f( 1.501321110f); \
2262 p1 = p5 + p1*stbi__f2f(-0.899976223f); \
2263 p2 = p5 + p2*stbi__f2f(-2.562915447f); \
2264 p3 = p3*stbi__f2f(-1.961570560f); \
2265 p4 = p4*stbi__f2f(-0.390180644f); \
2266 t3 += p1+p4; \
2267 t2 += p2+p3; \
2268 t1 += p2+p4; \
2269 t0 += p1+p3;
2272 {
2277 // columns
2279 // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
2282 // no shortcut 0 seconds
2283 // (1|2|3|4|5|6|7)==0 0 seconds
2284 // all separate -0.047 seconds
2285 // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds
2290 // constants scaled things up by 1<<12; let's bring them back
2291 // down, but keep 2 extra bits of precision
2301 }
2302 }
2305 // no fast case since the first 1D IDCT spread components out
2307 // constants scaled things up by 1<<12, plus we had 1<<2 from first
2308 // loop, plus horizontal and vertical each scale by sqrt(8) so together
2309 // we've got an extra 1<<3, so 1<<17 total we need to remove.
2310 // so we want to round that, which means adding 0.5 * 1<<17,
2311 // aka 65536. Also, we'll end up with -128 to 127 that we want
2312 // to encode as 0..255 by adding 128, so we'll add that before the shift
2317 // tried computing the shifts into temps, or'ing the temps to see
2318 // if any were out of range, but that was slower
2327 }
2328 }
2330 #ifdef STBI_SSE2
2331 // sse2 integer IDCT. not the fastest possible implementation but it
2332 // produces bit-identical results to the generic C version so it's
2333 // fully "transparent".
2335 {
2336 // This is constructed to match our regular (generic) integer IDCT exactly.
2338 __m128i tmp;
2340 // dot product constant: even elems=x, odd elems=y
2341 #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y))
2343 // out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit)
2344 // out(1) = c1[even]*x + c1[odd]*y
2345 #define dct_rot(out0,out1, x,y,c0,c1) \
2346 __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \
2347 __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \
2348 __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \
2349 __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \
2350 __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \
2351 __m128i out1##_h = _mm_madd_epi16(c0##hi, c1)
2353 // out = in << 12 (in 16-bit, out 32-bit)
2354 #define dct_widen(out, in) \
2355 __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \
2356 __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4)
2358 // wide add
2359 #define dct_wadd(out, a, b) \
2360 __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \
2361 __m128i out##_h = _mm_add_epi32(a##_h, b##_h)
2363 // wide sub
2364 #define dct_wsub(out, a, b) \
2365 __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \
2366 __m128i out##_h = _mm_sub_epi32(a##_h, b##_h)
2368 // butterfly a/b, add bias, then shift by "s" and pack
2369 #define dct_bfly32o(out0, out1, a,b,bias,s) \
2370 { \
2371 __m128i abiased_l = _mm_add_epi32(a##_l, bias); \
2372 __m128i abiased_h = _mm_add_epi32(a##_h, bias); \
2373 dct_wadd(sum, abiased, b); \
2374 dct_wsub(dif, abiased, b); \
2375 out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \
2376 out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \
2377 }
2379 // 8-bit interleave step (for transposes)
2380 #define dct_interleave8(a, b) \
2381 tmp = a; \
2382 a = _mm_unpacklo_epi8(a, b); \
2383 b = _mm_unpackhi_epi8(tmp, b)
2385 // 16-bit interleave step (for transposes)
2386 #define dct_interleave16(a, b) \
2387 tmp = a; \
2388 a = _mm_unpacklo_epi16(a, b); \
2389 b = _mm_unpackhi_epi16(tmp, b)
2391 #define dct_pass(bias,shift) \
2392 { \
2394 dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \
2395 __m128i sum04 = _mm_add_epi16(row0, row4); \
2396 __m128i dif04 = _mm_sub_epi16(row0, row4); \
2397 dct_widen(t0e, sum04); \
2398 dct_widen(t1e, dif04); \
2399 dct_wadd(x0, t0e, t3e); \
2400 dct_wsub(x3, t0e, t3e); \
2401 dct_wadd(x1, t1e, t2e); \
2402 dct_wsub(x2, t1e, t2e); \
2404 dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \
2405 dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \
2406 __m128i sum17 = _mm_add_epi16(row1, row7); \
2407 __m128i sum35 = _mm_add_epi16(row3, row5); \
2408 dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \
2409 dct_wadd(x4, y0o, y4o); \
2410 dct_wadd(x5, y1o, y5o); \
2411 dct_wadd(x6, y2o, y5o); \
2412 dct_wadd(x7, y3o, y4o); \
2413 dct_bfly32o(row0,row7, x0,x7,bias,shift); \
2414 dct_bfly32o(row1,row6, x1,x6,bias,shift); \
2415 dct_bfly32o(row2,row5, x2,x5,bias,shift); \
2416 dct_bfly32o(row3,row4, x3,x4,bias,shift); \
2417 }
2419 __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f));
2420 __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f));
2421 __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f));
2422 __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f));
2423 __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f));
2424 __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f));
2425 __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f));
2426 __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f));
2428 // rounding biases in column/row passes, see stbi__idct_block for explanation.
2432 // load
2442 // column pass
2445 {
2446 // 16bit 8x8 transpose pass 1
2452 // transpose pass 2
2458 // transpose pass 3
2463 }
2465 // row pass
2468 {
2469 // pack
2475 // 8bit 8x8 transpose pass 1
2479 // transpose pass 2
2483 // transpose pass 3
2487 // store
2496 }
2498 #undef dct_const
2499 #undef dct_rot
2500 #undef dct_widen
2501 #undef dct_wadd
2502 #undef dct_wsub
2503 #undef dct_bfly32o
2504 #undef dct_interleave8
2505 #undef dct_interleave16
2506 #undef dct_pass
2507 }
2511 #ifdef STBI_NEON
2513 // NEON integer IDCT. should produce bit-identical
2514 // results to the generic C version.
2516 {
2532 #define dct_long_mul(out, inq, coeff) \
2533 int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \
2534 int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff)
2536 #define dct_long_mac(out, acc, inq, coeff) \
2537 int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \
2538 int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff)
2540 #define dct_widen(out, inq) \
2541 int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \
2542 int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12)
2544 // wide add
2545 #define dct_wadd(out, a, b) \
2546 int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \
2547 int32x4_t out##_h = vaddq_s32(a##_h, b##_h)
2549 // wide sub
2550 #define dct_wsub(out, a, b) \
2551 int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \
2552 int32x4_t out##_h = vsubq_s32(a##_h, b##_h)
2554 // butterfly a/b, then shift using "shiftop" by "s" and pack
2555 #define dct_bfly32o(out0,out1, a,b,shiftop,s) \
2556 { \
2557 dct_wadd(sum, a, b); \
2558 dct_wsub(dif, a, b); \
2559 out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \
2560 out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \
2561 }
2563 #define dct_pass(shiftop, shift) \
2564 { \
2566 int16x8_t sum26 = vaddq_s16(row2, row6); \
2567 dct_long_mul(p1e, sum26, rot0_0); \
2568 dct_long_mac(t2e, p1e, row6, rot0_1); \
2569 dct_long_mac(t3e, p1e, row2, rot0_2); \
2570 int16x8_t sum04 = vaddq_s16(row0, row4); \
2571 int16x8_t dif04 = vsubq_s16(row0, row4); \
2572 dct_widen(t0e, sum04); \
2573 dct_widen(t1e, dif04); \
2574 dct_wadd(x0, t0e, t3e); \
2575 dct_wsub(x3, t0e, t3e); \
2576 dct_wadd(x1, t1e, t2e); \
2577 dct_wsub(x2, t1e, t2e); \
2579 int16x8_t sum15 = vaddq_s16(row1, row5); \
2580 int16x8_t sum17 = vaddq_s16(row1, row7); \
2581 int16x8_t sum35 = vaddq_s16(row3, row5); \
2582 int16x8_t sum37 = vaddq_s16(row3, row7); \
2583 int16x8_t sumodd = vaddq_s16(sum17, sum35); \
2584 dct_long_mul(p5o, sumodd, rot1_0); \
2585 dct_long_mac(p1o, p5o, sum17, rot1_1); \
2586 dct_long_mac(p2o, p5o, sum35, rot1_2); \
2587 dct_long_mul(p3o, sum37, rot2_0); \
2588 dct_long_mul(p4o, sum15, rot2_1); \
2589 dct_wadd(sump13o, p1o, p3o); \
2590 dct_wadd(sump24o, p2o, p4o); \
2591 dct_wadd(sump23o, p2o, p3o); \
2592 dct_wadd(sump14o, p1o, p4o); \
2593 dct_long_mac(x4, sump13o, row7, rot3_0); \
2594 dct_long_mac(x5, sump24o, row5, rot3_1); \
2595 dct_long_mac(x6, sump23o, row3, rot3_2); \
2596 dct_long_mac(x7, sump14o, row1, rot3_3); \
2597 dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \
2598 dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \
2599 dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \
2600 dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \
2601 }
2603 // load
2613 // add DC bias
2616 // column pass
2619 // 16bit 8x8 transpose
2620 {
2621 // these three map to a single VTRN.16, VTRN.32, and VSWP, respectively.
2622 // whether compilers actually get this is another story, sadly.
2623 #define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; }
2624 #define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); }
2625 #define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); }
2627 // pass 1
2633 // pass 2
2639 // pass 3
2645 #undef dct_trn16
2646 #undef dct_trn32
2647 #undef dct_trn64
2648 }
2650 // row pass
2651 // vrshrn_n_s32 only supports shifts up to 16, we need
2652 // 17. so do a non-rounding shift of 16 first then follow
2653 // up with a rounding shift by 1.
2656 {
2657 // pack and round
2667 // again, these can translate into one instruction, but often don't.
2668 #define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; }
2669 #define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); }
2670 #define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); }
2672 // sadly can't use interleaved stores here since we only write
2673 // 8 bytes to each scan line!
2675 // 8x8 8-bit transpose pass 1
2681 // pass 2
2687 // pass 3
2693 // store
2703 #undef dct_trn8_8
2704 #undef dct_trn8_16
2705 #undef dct_trn8_32
2706 }
2708 #undef dct_long_mul
2709 #undef dct_long_mac
2710 #undef dct_widen
2711 #undef dct_wadd
2712 #undef dct_wsub
2713 #undef dct_bfly32o
2714 #undef dct_pass
2715 }
2719 #define STBI__MARKER_none 0xff
2720 // if there's a pending marker from the entropy stream, return that
2721 // otherwise, fetch from the stream and get a marker. if there's no
2722 // marker, return 0xff, which is never a valid marker value
2724 {
2725 stbi_uc x;
2726 if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; }
2732 }
2734 // in each scan, we'll have scan_n components, and the order
2735 // of the components is specified by order[]
2736 #define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
2738 // after a restart interval, stbi__jpeg_reset the entropy decoder and
2739 // the dc prediction
2741 {
2745 j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = j->img_comp[3].dc_pred = 0;
2749 // no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
2750 // since we don't even allow 1<<30 pixels
2751 }
2754 {
2761 // non-interleaved data, we just need to process one block at a time,
2762 // in trivial scanline order
2763 // number of blocks to do just depends on how many actual "pixels" this
2764 // component has, independent of interleaved MCU blocking and such
2770 if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0;
2772 // every data block is an MCU, so countdown the restart interval
2775 // if it's NOT a restart, then just bail, so we get corrupt data
2776 // rather than no data
2779 }
2780 }
2781 }
2788 // scan an interleaved mcu... process scan_n components in order
2791 // scan out an mcu's worth of this component; that's just determined
2792 // by the basic H and V specified for the component
2798 if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0;
2800 }
2801 }
2802 }
2803 // after all interleaved components, that's an interleaved MCU,
2804 // so now count down the restart interval
2809 }
2810 }
2811 }
2813 }
2818 // non-interleaved data, we just need to process one block at a time,
2819 // in trivial scanline order
2820 // number of blocks to do just depends on how many actual "pixels" this
2821 // component has, independent of interleaved MCU blocking and such
2834 }
2835 // every data block is an MCU, so countdown the restart interval
2840 }
2841 }
2842 }
2848 // scan an interleaved mcu... process scan_n components in order
2851 // scan out an mcu's worth of this component; that's just determined
2852 // by the basic H and V specified for the component
2860 }
2861 }
2862 }
2863 // after all interleaved components, that's an interleaved MCU,
2864 // so now count down the restart interval
2869 }
2870 }
2871 }
2873 }
2874 }
2875 }
2878 {
2882 }
2885 {
2887 // dequantize and idct the data
2897 }
2898 }
2899 }
2900 }
2901 }
2904 {
2925 z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s));
2927 }
2942 }
2950 }
2956 }
2958 }
2960 // check for comment block or APP blocks
2966 else
2968 }
2995 }
2996 }
3000 }
3003 }
3005 // after we see SOS
3007 {
3011 if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad SOS component count","Corrupt JPEG");
3020 z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG");
3021 z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG");
3023 }
3025 {
3033 if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13)
3039 }
3040 }
3043 }
3046 {
3053 }
3058 }
3062 }
3063 }
3065 }
3068 {
3072 p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
3073 s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
3074 s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires
3081 }
3092 z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG");
3093 z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG");
3094 z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG");
3095 }
3099 if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode");
3104 }
3106 // compute interleaved mcu info
3111 // these sizes can't be more than 17 bits
3116 // number of effective pixels (e.g. for non-interleaved MCU)
3119 // to simplify generation, we'll allocate enough memory to decode
3120 // the bogus oversized data from using interleaved MCUs and their
3121 // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't
3122 // discard the extra data until colorspace conversion
3123 //
3124 // img_mcu_x, img_mcu_y: <=17 bits; comp[i].h and .v are <=4 (checked earlier)
3125 // so these muls can't overflow with 32-bit ints (which we require)
3134 // align blocks for idct using mmx/sse
3137 // w2, h2 are multiples of 8 (see above)
3140 z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15);
3144 }
3145 }
3148 }
3150 // use comparisons since in some cases we handle more than one case (e.g. SOF)
3151 #define stbi__DNL(x) ((x) == 0xdc)
3152 #define stbi__SOI(x) ((x) == 0xd8)
3153 #define stbi__EOI(x) ((x) == 0xd9)
3154 #define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2)
3155 #define stbi__SOS(x) ((x) == 0xda)
3157 #define stbi__SOF_progressive(x) ((x) == 0xc2)
3160 {
3173 // some files have extra padding after their blocks, so ok, we'll scan
3176 }
3177 }
3181 }
3183 // decode image to YCbCr format
3185 {
3190 }
3199 // handle 0s at the end of image data from IP Kamera 9060
3205 }
3206 }
3207 // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0
3208 }
3216 }
3218 }
3222 }
3224 // static jfif-centered resampling (across block boundaries)
3229 #define stbi__div4(x) ((stbi_uc) ((x) >> 2))
3232 {
3238 }
3240 static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3241 {
3242 // need to generate two samples vertically for every one in input
3248 }
3250 static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3251 {
3252 // need to generate two samples horizontally for every one in input
3257 // if only one sample, can't do any interpolation
3260 }
3268 }
3276 }
3278 #define stbi__div16(x) ((stbi_uc) ((x) >> 4))
3280 static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3281 {
3282 // need to generate 2x2 samples for every one in input
3287 }
3296 }
3302 }
3304 #if defined(STBI_SSE2) || defined(STBI_NEON)
3305 static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3306 {
3307 // need to generate 2x2 samples for every one in input
3313 }
3316 // process groups of 8 pixels for as long as we can.
3317 // note we can't handle the last pixel in a row in this loop
3318 // because we need to handle the filter boundary conditions.
3320 #if defined(STBI_SSE2)
3321 // load and perform the vertical filtering pass
3322 // this uses 3*x + y = 4*x + (y - x)
3332 // horizontal filter works the same based on shifted vers of current
3333 // row. "prev" is current row shifted right by 1 pixel; we need to
3334 // insert the previous pixel value (from t1).
3335 // "next" is current row shifted left by 1 pixel, with first pixel
3336 // of next block of 8 pixels added in.
3342 // horizontal filter, polyphase implementation since it's convenient:
3343 // even pixels = 3*cur + prev = cur*4 + (prev - cur)
3344 // odd pixels = 3*cur + next = cur*4 + (next - cur)
3345 // note the shared term.
3354 // interleave even and odd pixels, then undo scaling.
3360 // pack and write output
3363 #elif defined(STBI_NEON)
3364 // load and perform the vertical filtering pass
3365 // this uses 3*x + y = 4*x + (y - x)
3372 // horizontal filter works the same based on shifted vers of current
3373 // row. "prev" is current row shifted right by 1 pixel; we need to
3374 // insert the previous pixel value (from t1).
3375 // "next" is current row shifted left by 1 pixel, with first pixel
3376 // of next block of 8 pixels added in.
3382 // horizontal filter, polyphase implementation since it's convenient:
3383 // even pixels = 3*cur + prev = cur*4 + (prev - cur)
3384 // odd pixels = 3*cur + next = cur*4 + (next - cur)
3385 // note the shared term.
3392 // undo scaling and round, then store with even/odd phases interleaved
3393 uint8x8x2_t o;
3397 #endif
3399 // "previous" value for next iter
3401 }
3412 }
3418 }
3419 #endif
3421 static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3422 {
3423 // resample with nearest-neighbor
3430 }
3432 // this is a reduced-precision calculation of YCbCr-to-RGB introduced
3433 // to make sure the code produces the same results in both SIMD and scalar
3434 #define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8)
3435 static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step)
3436 {
3444 g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000);
3457 }
3458 }
3460 #if defined(STBI_SSE2) || defined(STBI_NEON)
3461 static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step)
3462 {
3465 #ifdef STBI_SSE2
3466 // step == 3 is pretty ugly on the final interleave, and i'm not convinced
3467 // it's useful in practice (you wouldn't use it for textures, for example).
3468 // so just accelerate step == 4 case.
3470 // this is a fairly straightforward implementation and not super-optimized.
3480 // load
3487 // unpack to short (and left-shift cr, cb by 8)
3492 // color transform
3503 // descale
3508 // back to byte, set up for transpose
3512 // transpose to interleave channels
3518 // store
3522 }
3523 }
3524 #endif
3526 #ifdef STBI_NEON
3527 // in this version, step=3 support would be easy to add. but is there demand?
3529 // this is a fairly straightforward implementation and not super-optimized.
3537 // load
3544 // expand to s16
3549 // color transform
3558 // undo scaling, round, convert to byte
3559 uint8x8x4_t o;
3565 // store, interleaving r/g/b/a
3568 }
3569 }
3570 #endif
3578 g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000);
3591 }
3592 }
3593 #endif
3595 // set up the kernels
3597 {
3602 #ifdef STBI_SSE2
3607 }
3608 #endif
3610 #ifdef STBI_NEON
3614 #endif
3615 }
3617 // clean up the temporary component buffers
3619 {
3621 }
3624 {
3625 resample_row_func resample;
3633 // fast 0..255 * 0..255 => 0..255 rounded multiplication
3635 {
3638 }
3640 static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
3641 {
3645 // validate req_comp
3648 // load a jpeg image from whichever source, but leave in YCbCr format
3651 // determine actual number of components to generate
3658 else
3661 // resample and color-convert
3662 {
3673 // allocate line buffer big enough for upsampling off the edges
3674 // with upsample factor of 4
3676 if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
3690 }
3692 // can't error after this so, this is safe
3696 // now go ahead and resample
3711 }
3712 }
3723 }
3726 }
3736 }
3745 }
3748 }
3754 }
3764 }
3765 }
3775 }
3781 }
3786 else
3788 }
3789 }
3790 }
3796 }
3797 }
3799 static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
3800 {
3809 }
3812 {
3821 }
3824 {
3828 }
3833 }
3836 {
3843 }
3844 #endif
3846 // public domain zlib decode v0.2 Sean Barrett 2006-11-18
3847 // simple implementation
3848 // - all input must be provided in an upfront buffer
3849 // - all output is written to a single output buffer (can malloc/realloc)
3850 // performance
3851 // - fast huffman
3853 #ifndef STBI_NO_ZLIB
3855 // fast-way is faster to check than jpeg huffman, but slow way is slower
3857 #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
3859 // zlib-style huffman encoding
3860 // (jpegs packs from left, zlib from right, so can't share code)
3862 {
3872 {
3878 }
3881 {
3883 // to bit reverse n bits, reverse 16 and shift
3884 // e.g. 11 bits, bit reverse and shift away 5
3886 }
3889 {
3893 // DEFLATE spec for generating codes
3913 }
3927 }
3928 }
3930 }
3931 }
3933 }
3935 // zlib-from-memory implementation for PNG reading
3936 // because PNG allows splitting the zlib stream arbitrarily,
3937 // and it's annoying structurally to have PNG call ZLIB call PNG,
3938 // we require PNG read all the IDATs and combine them into a single
3939 // memory buffer
3942 {
3945 stbi__uint32 code_buffer;
3956 {
3959 }
3962 {
3968 }
3971 {
3978 }
3981 {
3983 // not resolved by fast table, so compute it the slow way
3984 // use jpeg approach, which requires MSbits at top
3990 // code size is s, so:
3996 }
3999 {
4008 }
4010 }
4013 {
4029 }
4046 {
4055 }
4063 }
4075 }
4082 }
4083 }
4084 }
4085 }
4088 {
4090 stbi__zhuffman z_codelength;
4104 }
4124 }
4128 }
4129 }
4134 }
4137 {
4142 // drain the bit-packed data into header
4148 }
4150 // now fill header the normal way
4163 }
4166 {
4169 /* int cinfo = cmf >> 4; */
4172 if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
4174 // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
4176 }
4179 {
4189 };
4191 {
4193 };
4194 /*
4195 Init algorithm:
4196 {
4197 int i; // use <= to match clearly with spec
4198 for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8;
4199 for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9;
4200 for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7;
4201 for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8;
4203 for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5;
4204 }
4205 */
4208 {
4223 // use fixed code lengths
4228 }
4230 }
4233 }
4236 {
4243 }
4245 STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
4246 {
4247 stbi__zbuf a;
4258 }
4259 }
4262 {
4264 }
4266 STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
4267 {
4268 stbi__zbuf a;
4279 }
4280 }
4283 {
4284 stbi__zbuf a;
4289 else
4291 }
4294 {
4295 stbi__zbuf a;
4306 }
4307 }
4309 STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
4310 {
4311 stbi__zbuf a;
4316 else
4318 }
4319 #endif
4321 // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18
4322 // simple implementation
4323 // - only 8-bit samples
4324 // - no CRC checking
4325 // - allocates lots of intermediate memory
4326 // - avoids problem of streaming data between subsystems
4327 // - avoids explicit window management
4328 // performance
4329 // - uses stb_zlib, a PD zlib implementation with fast huffman decoding
4331 #ifndef STBI_NO_PNG
4333 {
4334 stbi__uint32 length;
4335 stbi__uint32 type;
4339 {
4340 stbi__pngchunk c;
4344 }
4347 {
4353 }
4356 {
4369 // synthetic filters used for first scanline to avoid needing a dummy row of 0s
4370 STBI__F_avg_first,
4371 STBI__F_paeth_first
4372 };
4375 {
4376 STBI__F_none,
4377 STBI__F_sub,
4378 STBI__F_none,
4379 STBI__F_avg_first,
4380 STBI__F_paeth_first
4381 };
4384 {
4392 }
4396 // create the png data from post-deflated data
4397 static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color)
4398 {
4411 a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into
4418 // we used to check for exact match between raw_len and img_len on non-interlaced PNGs,
4419 // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros),
4420 // so just check for raw_len < img_len always.
4433 cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place
4436 }
4439 // if first row, use special filter that doesn't sample previous row
4442 // handle first byte explicitly
4452 }
4453 }
4465 }
4473 }
4475 // this is a little gross, so that we don't switch per-pixel or per-component
4478 #define STBI__CASE(f) \
4479 case f: \
4480 for (k=0; k < nk; ++k)
4482 // "none" filter turns into a memcpy here; make that explicit.
4486 STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); } break;
4487 STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); } break;
4488 STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); } break;
4489 STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); } break;
4490 }
4491 #undef STBI__CASE
4495 #define STBI__CASE(f) \
4496 case f: \
4497 for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) \
4498 for (k=0; k < filter_bytes; ++k)
4503 STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); } break;
4504 STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); } break;
4505 STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); } break;
4506 STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); } break;
4507 }
4508 #undef STBI__CASE
4510 // the loop above sets the high byte of the pixels' alpha, but for
4511 // 16 bit png files we also need the low byte set. we'll do that here.
4516 }
4517 }
4518 }
4519 }
4521 // we make a separate pass to expand bits to pixels; for performance,
4522 // this could run two scanlines behind the above code, so it won't
4523 // intefere with filtering but will still be in the cache.
4528 // unpack 1/2/4-bit into a 8-bit buffer. allows us to keep the common 8-bit path optimal at minimal cost for 1/2/4-bit
4529 // png guarante byte alignment, if width is not multiple of 8/4/2 we'll decode dummy trailing data that will be skipped in the later loop
4530 stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range
4532 // note that the final byte might overshoot and write more data than desired.
4533 // we can allocate enough data that this never writes out of memory, but it
4534 // could also overwrite the next scanline. can it overwrite non-empty data
4535 // on the next scanline? yes, consider 1-pixel-wide scanlines with 1-bit-per-pixel.
4536 // so we need to explicitly clamp the final ones
4542 }
4550 }
4564 }
4572 }
4575 // insert alpha = 255
4581 }
4589 }
4590 }
4591 }
4592 }
4594 // force the image data from big-endian to platform-native.
4595 // this is done in a separate pass due to the decoding relying
4596 // on the data being untouched, but could probably be done
4597 // per-line during decode if care is taken.
4603 }
4604 }
4607 }
4609 static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced)
4610 {
4616 return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color);
4618 // de-interlacing
4626 // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
4634 }
4641 }
4642 }
4646 }
4647 }
4651 }
4654 {
4659 // compute color-based transparency, assuming we've
4660 // already got 255 as the alpha value in the output
4667 }
4673 }
4674 }
4676 }
4679 {
4684 // compute color-based transparency, assuming we've
4685 // already got 65535 as the alpha value in the output
4692 }
4698 }
4699 }
4701 }
4704 {
4711 // between here and free(out) below, exitting would leak
4721 }
4730 }
4731 }
4738 }
4744 {
4746 }
4749 {
4751 }
4754 {
4765 }
4769 // convert bgr to rgb and unpremultiply
4781 }
4783 }
4785 // convert bgr to rgb
4791 }
4792 }
4793 }
4794 }
4796 #define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d))
4799 {
4827 s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
4828 s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
4829 z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only");
4832 if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
4835 interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
4839 if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
4842 // if paletted, then pal_n is our final components, and
4843 // img_n is # components to decompress/filter.
4846 // if SCAN_header, have to scan to see if we have a tRNS
4847 }
4849 }
4861 }
4863 }
4880 for (k = 0; k < s->img_n; ++k) tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is
4882 for (k = 0; k < s->img_n; ++k) tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger
4883 }
4884 }
4886 }
4900 p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
4902 }
4906 }
4913 // initial guess for decoded data size to avoid unnecessary reallocs
4916 z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);
4921 else
4923 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0;
4929 }
4930 }
4934 // pal_img_n == 3 or 4
4941 // non-paletted image with tRNS -> source image has (constant) alpha
4943 }
4946 }
4949 // if critical, fail
4952 #ifndef STBI_NO_FAILURE_STRINGS
4953 // not threadsafe
4959 #endif
4961 }
4964 }
4965 // end of PNG chunk, read and skip CRC
4967 }
4968 }
4970 static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri)
4971 {
4977 else
4983 result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
4984 else
4985 result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
4988 }
4992 }
4998 }
5000 static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
5001 {
5002 stbi__png p;
5005 }
5008 {
5013 }
5016 {
5020 }
5025 }
5028 {
5029 stbi__png p;
5032 }
5035 {
5036 stbi__png p;
5043 }
5045 }
5046 #endif
5048 // Microsoft/Windows BMP image
5050 #ifndef STBI_NO_BMP
5052 {
5064 }
5067 {
5071 }
5074 // returns 0..31 for the highest set bit
5076 {
5085 }
5088 {
5095 }
5097 // extract an arbitrarily-aligned N-bit value (N=bits)
5098 // from v, and then make it 8-bits long and fractionally
5099 // extend it to full full range.
5101 {
5106 };
5109 };
5112 else
5118 }
5121 {
5127 {
5129 if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP");
5137 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown");
5144 }
5149 if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");
5161 }
5174 }
5179 // not documented, but generated by photoshop and handled by mspaint
5181 // ?!?!?
5183 }
5186 }
5203 }
5204 }
5205 }
5207 }
5210 static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
5211 {
5217 stbi__bmp_data info;
5239 }
5243 else
5247 else
5250 // sanity-check size
5258 if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
5265 }
5285 }
5286 }
5288 }
5296 }
5307 }
5309 }
5310 }
5325 }
5328 // right shift amt to put high bit in position #7
5333 }
5345 }
5357 }
5358 }
5360 }
5361 }
5363 // if alpha channel is all 0s, replace with all 255s
5369 stbi_uc t;
5375 }
5376 }
5377 }
5382 }
5388 }
5389 #endif
5391 // Targa Truevision - TGA
5392 // by Jonathan Dummer
5393 #ifndef STBI_NO_TGA
5394 // returns STBI_rgb or whatever, 0 on error
5396 {
5397 // only RGB or RGBA (incl. 16bit) or grey allowed
5402 // fallthrough
5408 }
5409 }
5412 {
5420 }
5426 }
5432 }
5436 if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) {
5439 }
5442 }
5447 }
5452 }
5457 // when using a colormap, tga_bits_per_pixel is the size of the indexes
5458 // I don't think anything but 8 or 16bit indexes makes sense
5461 }
5464 tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
5465 }
5469 }
5474 }
5477 {
5491 if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE
5493 }
5497 if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index
5502 errorEnd:
5505 }
5507 // read 16bit value and convert to 24bit RGB
5509 {
5512 // we have 3 channels with 5bits each
5516 // Note that this saves the data in RGB(A) order, so it doesn't need to be swapped later
5521 // some people claim that the most significant bit might be used for alpha
5522 // (possibly if an alpha-bit is set in the "image descriptor byte")
5523 // but that only made 16bit test images completely translucent..
5524 // so let's treat all 15 and 16bit TGAs as RGB with no alpha.
5525 }
5527 static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
5528 {
5529 // read in the TGA header stuff
5544 // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?)
5545 // image data
5557 // do a tiny bit of precessing
5559 {
5562 }
5565 // If I'm paletted, then I'll use the number of bits from the palette
5569 if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency
5572 // tga info
5583 // skip to the data's starting position (offset usually = 0)
5591 }
5593 // do I need to load a palette?
5595 {
5596 // any data to skip? (offset usually = 0)
5598 // load the palette
5603 }
5610 }
5615 }
5616 }
5617 // load the data
5619 {
5620 // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk?
5622 {
5624 {
5625 // yep, get the next byte as a RLE command
5631 {
5633 }
5635 {
5637 }
5638 // OK, if I need to read a pixel, do it now
5640 {
5641 // load however much data we did have
5643 {
5644 // read in index, then perform the lookup
5647 // invalid index
5649 }
5653 }
5658 // read in the data raw
5661 }
5662 }
5663 // clear the reading flag for the next pixel
5667 // copy data
5671 // in case we're in RLE mode, keep counting down
5673 }
5674 // do I need to invert the image?
5676 {
5678 {
5682 {
5688 }
5689 }
5690 }
5691 // clear my palette, if I had one
5693 {
5695 }
5696 }
5698 // swap RGB - if the source data was RGB16, it already is in the right order
5700 {
5703 {
5708 }
5709 }
5711 // convert to target component count
5715 // the things I do to get rid of an error message, and yet keep
5716 // Microsoft's C compilers happy... [8^(
5720 // OK, done
5722 }
5723 #endif
5725 // *************************************************************************************************
5726 // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
5728 #ifndef STBI_NO_PSD
5730 {
5734 }
5737 {
5744 // No-op.
5746 // Copy next len+1 bytes literally.
5747 len++;
5753 len--;
5754 }
5756 stbi_uc val;
5757 // Next -len+1 bytes in the dest are replicated from next source byte.
5758 // (Interpret len as a negative 8-bit int.)
5766 len--;
5767 }
5768 }
5769 }
5772 }
5774 static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
5775 {
5784 // Check identifier
5788 // Check file type version.
5792 // Skip 6 reserved bytes.
5795 // Read the number of channels (R, G, B, A, etc).
5800 // Read the rows and columns of the image.
5804 // Make sure the depth is 8 bits.
5809 // Make sure the color mode is RGB.
5810 // Valid options are:
5811 // 0: Bitmap
5812 // 1: Grayscale
5813 // 2: Indexed color
5814 // 3: RGB color
5815 // 4: CMYK color
5816 // 7: Multichannel
5817 // 8: Duotone
5818 // 9: Lab color
5822 // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.)
5825 // Skip the image resources. (resolution, pen tool paths, etc)
5828 // Skip the reserved data.
5831 // Find out if the data is compressed.
5832 // Known values:
5833 // 0: no compression
5834 // 1: RLE compressed
5839 // Check size
5843 // Create the destination image.
5854 // Initialize the data to zero.
5855 //memset( out, 0, pixelCount * 4 );
5857 // Finally, the image data.
5859 // RLE as used by .PSD and .TIFF
5860 // Loop until you get the number of unpacked bytes you are expecting:
5861 // Read the next source byte into n.
5862 // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
5863 // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
5864 // Else if n is 128, noop.
5865 // Endloop
5867 // The RLE-compressed data is preceded by a 2-byte data count for each row in the data,
5868 // which we're going to just skip.
5871 // Read the RLE data by channel.
5877 // Fill this channel with default data.
5881 // Read the RLE data.
5885 }
5886 }
5887 }
5890 // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
5891 // where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image.
5893 // Read the data by channel.
5896 // Fill this channel with default data.
5907 }
5921 }
5922 }
5923 }
5924 }
5925 }
5927 // remove weird white matte from PSD
5939 }
5940 }
5951 }
5952 }
5953 }
5954 }
5956 // convert to desired output format
5960 else
5963 }
5970 }
5971 #endif
5973 // *************************************************************************************************
5974 // Softimage PIC loader
5975 // by Tom Seddon
5976 //
5977 // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
5978 // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
5980 #ifndef STBI_NO_PIC
5982 {
5989 }
5992 {
6005 }
6008 {
6013 {
6020 }
6021 }
6024 }
6027 {
6033 }
6035 static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
6036 {
6040 // this will (should...) cater for even some bizarre stuff like having data
6041 // for the same channel in multiple packets.
6081 }
6084 {
6101 }
6102 }
6116 else
6133 }
6135 }
6137 }
6138 }
6139 }
6140 }
6143 }
6145 static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri)
6146 {
6159 if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode");
6165 // intermediate buffer is RGBA
6172 }
6179 }
6182 {
6186 }
6187 #endif
6189 // *************************************************************************************************
6190 // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
6192 #ifndef STBI_NO_GIF
6194 {
6195 stbi__int16 prefix;
6196 stbi_uc first;
6197 stbi_uc suffix;
6201 {
6221 {
6223 if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0;
6228 }
6231 {
6235 }
6237 static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp)
6238 {
6245 }
6246 }
6249 {
6250 stbi_uc version;
6251 if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8')
6266 if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
6274 }
6277 {
6283 }
6288 }
6291 {
6295 // recurse to decode the prefixes, since the linked-list is backwards,
6296 // and working backwards through an interleaved image would be nasty
6312 }
6323 }
6324 }
6325 }
6328 {
6329 stbi_uc lzw_cs;
6331 stbi__uint32 first;
6347 }
6349 // support no starting clear code
6360 }
6368 // @OPTIMIZE: is there some way we can accelerate the non-clear path?
6383 }
6389 }
6400 codesize++;
6402 }
6407 }
6408 }
6409 }
6410 }
6412 // this function is designed to support animated gifs, although stb_image doesn't support it
6413 // two back is the image from two frames ago, used for a very specific disposal format
6414 static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back)
6415 {
6422 // on first frame, any non-written pixels get the background colour (non-transparent)
6425 if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
6435 // image is treated as "transparent" at the start - ie, nothing overwrites the current background;
6436 // background colour is only used for pixels that are not rendered first frame, after that "background"
6437 // color refers to the color that was there the previous frame.
6443 // second frame - how do we dispoase of the previous one?
6449 }
6455 }
6456 }
6458 // restore what was changed last frame to background before that frame;
6462 }
6463 }
6465 // This is a non-disposal case eithe way, so just
6466 // leave the pixels as is, and they will become the new background
6467 // 1: do not dispose
6468 // 0: not specified.
6469 }
6471 // background is what out is after the undoing of the previou frame;
6473 }
6475 // clear my history;
6482 {
6501 // if the width of the specified rectangle is 0, that means
6502 // we may not see *any* pixels or the image is malformed;
6503 // to make sure this is caught, move the current y down to
6504 // max_y (which is what out_gif_code checks).
6516 }
6519 stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
6529 // if this was the first frame,
6532 // if first frame, any pixel not drawn to gets the background color
6535 g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be;
6537 }
6538 }
6539 }
6542 }
6545 {
6554 // unset old transparent
6557 }
6562 }
6564 // don't need transparent
6567 }
6571 }
6572 }
6575 }
6577 }
6584 }
6585 }
6586 }
6588 static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
6589 {
6595 stbi__gif g;
6600 }
6616 }
6621 }
6622 }
6626 }
6630 }
6631 }
6634 // free temp buffer;
6639 // do the final conversion after loading everything;
6647 }
6648 }
6650 static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
6651 {
6653 stbi__gif g;
6663 // moved conversion to after successful load so that the same
6664 // can be done for multiple frames.
6668 // if there was an error and we allocated an image buffer, free it!
6670 }
6672 // free buffers needed for multiple frame loading;
6677 }
6680 {
6682 }
6683 #endif
6685 // *************************************************************************************************
6686 // Radiance RGBE HDR loader
6687 // originally by Nicolas Schulz
6688 #ifndef STBI_NO_HDR
6690 {
6697 }
6700 {
6706 }
6708 }
6710 #define STBI__HDR_BUFLEN 1024
6712 {
6721 // flush to end of line
6723 ;
6725 }
6727 }
6731 }
6734 {
6737 // Exponent
6745 }
6756 }
6757 }
6758 }
6760 static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
6761 {
6774 // Check identifier
6779 // Parse header
6784 }
6788 // Parse width and height
6789 // can't use sscanf() if we're not using stdio!
6791 if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
6795 if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
6808 // Read data
6813 // Load image data
6814 // image data is stored as some number of sca
6816 // Read flat data
6820 main_decode_loop:
6823 }
6824 }
6826 // Read RLE-encoded data
6834 // not run-length encoded, so we have to actually use THIS data as a decoded
6835 // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
6846 }
6849 if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
6855 }
6856 }
6864 // Run
6867 if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
6871 // Dump
6872 if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
6875 }
6876 }
6877 }
6880 }
6883 }
6886 }
6889 {
6902 }
6908 }
6913 }
6918 }
6925 }
6930 }
6933 #ifndef STBI_NO_BMP
6935 {
6937 stbi__bmp_data info;
6949 else
6951 }
6953 }
6954 #endif
6956 #ifndef STBI_NO_PSD
6958 {
6966 }
6970 }
6976 }
6983 }
6987 }
6990 }
6993 {
6998 }
7002 }
7008 }
7015 }
7017 }
7018 #endif
7020 #ifndef STBI_NO_PIC
7022 {
7033 }
7042 }
7046 }
7066 }
7070 }
7076 }
7077 #endif
7079 // *************************************************************************************************
7080 // Portable Gray Map and Portable Pixel Map loader
7081 // by Ken Miller
7082 //
7083 // PGM: http://netpbm.sourceforge.net/doc/pgm.html
7084 // PPM: http://netpbm.sourceforge.net/doc/ppm.html
7085 //
7086 // Known limitations:
7087 // Does not support comments in the header section
7088 // Does not support ASCII image data (formats P2 and P3)
7089 // Does not support 16-bit-per-channel
7091 #ifndef STBI_NO_PNM
7094 {
7101 }
7103 }
7105 static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
7106 {
7127 }
7129 }
7132 {
7134 }
7137 {
7147 }
7148 }
7151 {
7153 }
7156 {
7162 }
7165 }
7168 {
7178 // Get identifier
7184 }
7201 else
7203 }
7204 #endif
7207 {
7208 #ifndef STBI_NO_JPEG
7210 #endif
7212 #ifndef STBI_NO_PNG
7214 #endif
7216 #ifndef STBI_NO_GIF
7218 #endif
7220 #ifndef STBI_NO_BMP
7222 #endif
7224 #ifndef STBI_NO_PSD
7226 #endif
7228 #ifndef STBI_NO_PIC
7230 #endif
7232 #ifndef STBI_NO_PNM
7234 #endif
7236 #ifndef STBI_NO_HDR
7238 #endif
7240 // test tga last because it's a crappy test!
7241 #ifndef STBI_NO_TGA
7244 #endif
7246 }
7249 {
7250 #ifndef STBI_NO_PNG
7252 #endif
7254 #ifndef STBI_NO_PSD
7256 #endif
7259 }
7261 #ifndef STBI_NO_STDIO
7263 {
7270 }
7273 {
7275 stbi__context s;
7281 }
7284 {
7291 }
7294 {
7296 stbi__context s;
7302 }
7306 {
7307 stbi__context s;
7310 }
7312 STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
7313 {
7314 stbi__context s;
7317 }
7320 {
7321 stbi__context s;
7324 }
7327 {
7328 stbi__context s;
7331 }
7335 /*
7336 revision history:
7337 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
7338 2.19 (2018-02-11) fix warning
7339 2.18 (2018-01-30) fix warnings
7340 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug
7341 1-bit BMP
7342 *_is_16_bit api
7343 avoid warnings
7344 2.16 (2017-07-23) all functions have 16-bit variants;
7345 STBI_NO_STDIO works again;
7346 compilation fixes;
7347 fix rounding in unpremultiply;
7348 optimize vertical flip;
7349 disable raw_len validation;
7350 documentation fixes
7351 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode;
7352 warning fixes; disable run-time SSE detection on gcc;
7353 uniform handling of optional "return" values;
7354 thread-safe initialization of zlib tables
7355 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
7356 2.13 (2016-11-29) add 16-bit API, only supported for PNG right now
7357 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
7358 2.11 (2016-04-02) allocate large structures on the stack
7359 remove white matting for transparent PSD
7360 fix reported channel count for PNG & BMP
7361 re-enable SSE2 in non-gcc 64-bit
7362 support RGB-formatted JPEG
7363 read 16-bit PNGs (only as 8-bit)
7364 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED
7365 2.09 (2016-01-16) allow comments in PNM files
7366 16-bit-per-pixel TGA (not bit-per-component)
7367 info() for TGA could break due to .hdr handling
7368 info() for BMP to shares code instead of sloppy parse
7369 can use STBI_REALLOC_SIZED if allocator doesn't support realloc
7370 code cleanup
7371 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA
7372 2.07 (2015-09-13) fix compiler warnings
7373 partial animated GIF support
7374 limited 16-bpc PSD support
7375 #ifdef unused functions
7376 bug with < 92 byte PIC,PNM,HDR,TGA
7377 2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value
7378 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning
7379 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit
7380 2.03 (2015-04-12) extra corruption checking (mmozeiko)
7381 stbi_set_flip_vertically_on_load (nguillemot)
7382 fix NEON support; fix mingw support
7383 2.02 (2015-01-19) fix incorrect assert, fix warning
7384 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2
7385 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG
7386 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg)
7387 progressive JPEG (stb)
7388 PGM/PPM support (Ken Miller)
7389 STBI_MALLOC,STBI_REALLOC,STBI_FREE
7390 GIF bugfix -- seemingly never worked
7391 STBI_NO_*, STBI_ONLY_*
7392 1.48 (2014-12-14) fix incorrectly-named assert()
7393 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb)
7394 optimize PNG (ryg)
7395 fix bug in interlaced PNG with user-specified channel count (stb)
7396 1.46 (2014-08-26)
7397 fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG
7398 1.45 (2014-08-16)
7399 fix MSVC-ARM internal compiler error by wrapping malloc
7400 1.44 (2014-08-07)
7401 various warning fixes from Ronny Chevalier
7402 1.43 (2014-07-15)
7403 fix MSVC-only compiler problem in code changed in 1.42
7404 1.42 (2014-07-09)
7405 don't define _CRT_SECURE_NO_WARNINGS (affects user code)
7406 fixes to stbi__cleanup_jpeg path
7407 added STBI_ASSERT to avoid requiring assert.h
7408 1.41 (2014-06-25)
7409 fix search&replace from 1.36 that messed up comments/error messages
7410 1.40 (2014-06-22)
7411 fix gcc struct-initialization warning
7412 1.39 (2014-06-15)
7413 fix to TGA optimization when req_comp != number of components in TGA;
7414 fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite)
7415 add support for BMP version 5 (more ignored fields)
7416 1.38 (2014-06-06)
7417 suppress MSVC warnings on integer casts truncating values
7418 fix accidental rename of 'skip' field of I/O
7419 1.37 (2014-06-04)
7420 remove duplicate typedef
7421 1.36 (2014-06-03)
7422 convert to header file single-file library
7423 if de-iphone isn't set, load iphone images color-swapped instead of returning NULL
7424 1.35 (2014-05-27)
7425 various warnings
7426 fix broken STBI_SIMD path
7427 fix bug where stbi_load_from_file no longer left file pointer in correct place
7428 fix broken non-easy path for 32-bit BMP (possibly never used)
7429 TGA optimization by Arseny Kapoulkine
7430 1.34 (unknown)
7431 use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case
7432 1.33 (2011-07-14)
7433 make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
7434 1.32 (2011-07-13)
7435 support for "info" function for all supported filetypes (SpartanJ)
7436 1.31 (2011-06-20)
7437 a few more leak fixes, bug in PNG handling (SpartanJ)
7438 1.30 (2011-06-11)
7439 added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
7440 removed deprecated format-specific test/load functions
7441 removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
7442 error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
7443 fix inefficiency in decoding 32-bit BMP (David Woo)
7444 1.29 (2010-08-16)
7445 various warning fixes from Aurelien Pocheville
7446 1.28 (2010-08-01)
7447 fix bug in GIF palette transparency (SpartanJ)
7448 1.27 (2010-08-01)
7449 cast-to-stbi_uc to fix warnings
7450 1.26 (2010-07-24)
7451 fix bug in file buffering for PNG reported by SpartanJ
7452 1.25 (2010-07-17)
7453 refix trans_data warning (Won Chun)
7454 1.24 (2010-07-12)
7455 perf improvements reading from files on platforms with lock-heavy fgetc()
7456 minor perf improvements for jpeg
7457 deprecated type-specific functions so we'll get feedback if they're needed
7458 attempt to fix trans_data warning (Won Chun)
7459 1.23 fixed bug in iPhone support
7460 1.22 (2010-07-10)
7461 removed image *writing* support
7462 stbi_info support from Jetro Lauha
7463 GIF support from Jean-Marc Lienher
7464 iPhone PNG-extensions from James Brown
7465 warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva)
7466 1.21 fix use of 'stbi_uc' in header (reported by jon blow)
7467 1.20 added support for Softimage PIC, by Tom Seddon
7468 1.19 bug in interlaced PNG corruption check (found by ryg)
7469 1.18 (2008-08-02)
7470 fix a threading bug (local mutable static)
7471 1.17 support interlaced PNG
7472 1.16 major bugfix - stbi__convert_format converted one too many pixels
7473 1.15 initialize some fields for thread safety
7474 1.14 fix threadsafe conversion bug
7475 header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
7476 1.13 threadsafe
7477 1.12 const qualifiers in the API
7478 1.11 Support installable IDCT, colorspace conversion routines
7479 1.10 Fixes for 64-bit (don't use "unsigned long")
7480 optimized upsampling by Fabian "ryg" Giesen
7481 1.09 Fix format-conversion for PSD code (bad global variables!)
7482 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
7483 1.07 attempt to fix C++ warning/errors again
7484 1.06 attempt to fix C++ warning/errors again
7485 1.05 fix TGA loading to return correct *comp and use good luminance calc
7486 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
7487 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
7488 1.02 support for (subset of) HDR files, float interface for preferred access to them
7489 1.01 fix bug: possible bug in handling right-side up bmps... not sure
7490 fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all
7491 1.00 interface to zlib that skips zlib header
7492 0.99 correct handling of alpha in palette
7493 0.98 TGA loader by lonesock; dynamically add loaders (untested)
7494 0.97 jpeg errors on too large a file; also catch another malloc failure
7495 0.96 fix detection of invalid v value - particleman@mollyrocket forum
7496 0.95 during header scan, seek to markers in case of padding
7497 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
7498 0.93 handle jpegtran output; verbose errors
7499 0.92 read 4,8,16,24,32-bit BMP files of several formats
7500 0.91 output 24-bit Windows 3.0 BMP files
7501 0.90 fix a few more warnings; bump version number to approach 1.0
7502 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
7503 0.60 fix compiling as c++
7504 0.59 fix warnings: merge Dave Moore's -Wall fixes
7505 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
7506 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
7507 0.56 fix bug: zlib uncompressed mode len vs. nlen
7508 0.55 fix bug: restart_interval not initialized to 0
7509 0.54 allow NULL for 'int *comp'
7510 0.53 fix bug in png 3->4; speedup png decoding
7511 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
7512 0.51 obey req_comp requests, 1-component jpegs return as 1-component,
7513 on 'test' only check type, not whether we support this variant
7514 0.50 (2006-11-19)
7515 first released version
7516 */
7519 /*
7520 ------------------------------------------------------------------------------
7521 This software is available under 2 licenses -- choose whichever you prefer.
7522 ------------------------------------------------------------------------------
7523 ALTERNATIVE A - MIT License
7524 Copyright (c) 2017 Sean Barrett
7525 Permission is hereby granted, free of charge, to any person obtaining a copy of
7526 this software and associated documentation files (the "Software"), to deal in
7527 the Software without restriction, including without limitation the rights to
7528 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7529 of the Software, and to permit persons to whom the Software is furnished to do
7530 so, subject to the following conditions:
7531 The above copyright notice and this permission notice shall be included in all
7532 copies or substantial portions of the Software.
7533 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7534 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7535 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7536 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7537 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
7538 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
7539 SOFTWARE.
7540 ------------------------------------------------------------------------------
7541 ALTERNATIVE B - Public Domain (www.unlicense.org)
7542 This is free and unencumbered software released into the public domain.
7543 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
7544 software, either in source code form or as a compiled binary, for any purpose,
7545 commercial or non-commercial, and by any means.
7546 In jurisdictions that recognize copyright laws, the author or authors of this
7547 software dedicate any and all copyright interest in the software to the public
7548 domain. We make this dedication for the benefit of the public at large and to
7549 the detriment of our heirs and successors. We intend this dedication to be an
7550 overt act of relinquishment in perpetuity of all present and future rights to
7551 this software under copyright law.
7552 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7553 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7554 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7555 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
7556 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
7557 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7558 ------------------------------------------------------------------------------
7559 */