1*c217d954SCole Faust /* stb_image - v2.19 - public domain image loader - http://nothings.org/stb
2*c217d954SCole Faust no warranty implied; use at your own risk
3*c217d954SCole Faust
4*c217d954SCole Faust Do this:
5*c217d954SCole Faust #define STB_IMAGE_IMPLEMENTATION
6*c217d954SCole Faust before you include this file in *one* C or C++ file to create the implementation.
7*c217d954SCole Faust
8*c217d954SCole Faust // i.e. it should look like this:
9*c217d954SCole Faust #include ...
10*c217d954SCole Faust #include ...
11*c217d954SCole Faust #include ...
12*c217d954SCole Faust #define STB_IMAGE_IMPLEMENTATION
13*c217d954SCole Faust #include "stb_image.h"
14*c217d954SCole Faust
15*c217d954SCole Faust You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.
16*c217d954SCole Faust And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
17*c217d954SCole Faust
18*c217d954SCole Faust
19*c217d954SCole Faust QUICK NOTES:
20*c217d954SCole Faust Primarily of interest to game developers and other people who can
21*c217d954SCole Faust avoid problematic images and only need the trivial interface
22*c217d954SCole Faust
23*c217d954SCole Faust JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
24*c217d954SCole Faust PNG 1/2/4/8/16-bit-per-channel
25*c217d954SCole Faust
26*c217d954SCole Faust TGA (not sure what subset, if a subset)
27*c217d954SCole Faust BMP non-1bpp, non-RLE
28*c217d954SCole Faust PSD (composited view only, no extra channels, 8/16 bit-per-channel)
29*c217d954SCole Faust
30*c217d954SCole Faust GIF (*comp always reports as 4-channel)
31*c217d954SCole Faust HDR (radiance rgbE format)
32*c217d954SCole Faust PIC (Softimage PIC)
33*c217d954SCole Faust PNM (PPM and PGM binary only)
34*c217d954SCole Faust
35*c217d954SCole Faust Animated GIF still needs a proper API, but here's one way to do it:
36*c217d954SCole Faust http://gist.github.com/urraka/685d9a6340b26b830d49
37*c217d954SCole Faust
38*c217d954SCole Faust - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
39*c217d954SCole Faust - decode from arbitrary I/O callbacks
40*c217d954SCole Faust - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)
41*c217d954SCole Faust
42*c217d954SCole Faust Full documentation under "DOCUMENTATION" below.
43*c217d954SCole Faust
44*c217d954SCole Faust
45*c217d954SCole Faust LICENSE
46*c217d954SCole Faust
47*c217d954SCole Faust See end of file for license information.
48*c217d954SCole Faust
49*c217d954SCole Faust RECENT REVISION HISTORY:
50*c217d954SCole Faust
51*c217d954SCole Faust 2.19 (2018-02-11) fix warning
52*c217d954SCole Faust 2.18 (2018-01-30) fix warnings
53*c217d954SCole Faust 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
54*c217d954SCole Faust 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes
55*c217d954SCole Faust 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC
56*c217d954SCole Faust 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
57*c217d954SCole Faust 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
58*c217d954SCole Faust 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
59*c217d954SCole Faust 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
60*c217d954SCole Faust RGB-format JPEG; remove white matting in PSD;
61*c217d954SCole Faust allocate large structures on the stack;
62*c217d954SCole Faust correct channel count for PNG & BMP
63*c217d954SCole Faust 2.10 (2016-01-22) avoid warning introduced in 2.09
64*c217d954SCole Faust 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
65*c217d954SCole Faust
66*c217d954SCole Faust See end of file for full revision history.
67*c217d954SCole Faust
68*c217d954SCole Faust
69*c217d954SCole Faust ============================ Contributors =========================
70*c217d954SCole Faust
71*c217d954SCole Faust Image formats Extensions, features
72*c217d954SCole Faust Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info)
73*c217d954SCole Faust Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info)
74*c217d954SCole Faust Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG)
75*c217d954SCole Faust Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks)
76*c217d954SCole Faust Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)
77*c217d954SCole Faust Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)
78*c217d954SCole Faust Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)
79*c217d954SCole Faust github:urraka (animated gif) Junggon Kim (PNM comments)
80*c217d954SCole Faust Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA)
81*c217d954SCole Faust socks-the-fox (16-bit PNG)
82*c217d954SCole Faust Jeremy Sawicki (handle all ImageNet JPGs)
83*c217d954SCole Faust Optimizations & bugfixes Mikhail Morozov (1-bit BMP)
84*c217d954SCole Faust Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
85*c217d954SCole Faust Arseny Kapoulkine
86*c217d954SCole Faust John-Mark Allen
87*c217d954SCole Faust
88*c217d954SCole Faust Bug & warning fixes
89*c217d954SCole Faust Marc LeBlanc David Woo Guillaume George Martins Mozeiko
90*c217d954SCole Faust Christpher Lloyd Jerry Jansson Joseph Thomson Phil Jordan
91*c217d954SCole Faust Dave Moore Roy Eltham Hayaki Saito Nathan Reed
92*c217d954SCole Faust Won Chun Luke Graham Johan Duparc Nick Verigakis
93*c217d954SCole Faust the Horde3D community Thomas Ruf Ronny Chevalier github:rlyeh
94*c217d954SCole Faust Janez Zemva John Bartholomew Michal Cichon github:romigrou
95*c217d954SCole Faust Jonathan Blow Ken Hamada Tero Hanninen github:svdijk
96*c217d954SCole Faust Laurent Gomila Cort Stratton Sergio Gonzalez github:snagar
97*c217d954SCole Faust Aruelien Pocheville Thibault Reuille Cass Everitt github:Zelex
98*c217d954SCole Faust Ryamond Barbiero Paul Du Bois Engin Manap github:grim210
99*c217d954SCole Faust Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw
100*c217d954SCole Faust Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus
101*c217d954SCole Faust Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppo
102*c217d954SCole Faust Christian Floisand Kevin Schmidt github:darealshinji
103*c217d954SCole Faust Blazej Dariusz Roszkowski github:Michaelangel007
104*c217d954SCole Faust */
105*c217d954SCole Faust
106*c217d954SCole Faust #ifndef STBI_INCLUDE_STB_IMAGE_H
107*c217d954SCole Faust #define STBI_INCLUDE_STB_IMAGE_H
108*c217d954SCole Faust
109*c217d954SCole Faust // DOCUMENTATION
110*c217d954SCole Faust //
111*c217d954SCole Faust // Limitations:
112*c217d954SCole Faust // - no 12-bit-per-channel JPEG
113*c217d954SCole Faust // - no JPEGs with arithmetic coding
114*c217d954SCole Faust // - GIF always returns *comp=4
115*c217d954SCole Faust //
116*c217d954SCole Faust // Basic usage (see HDR discussion below for HDR usage):
117*c217d954SCole Faust // int x,y,n;
118*c217d954SCole Faust // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
119*c217d954SCole Faust // // ... process data if not NULL ...
120*c217d954SCole Faust // // ... x = width, y = height, n = # 8-bit components per pixel ...
121*c217d954SCole Faust // // ... replace '0' with '1'..'4' to force that many components per pixel
122*c217d954SCole Faust // // ... but 'n' will always be the number that it would have been if you said 0
123*c217d954SCole Faust // stbi_image_free(data)
124*c217d954SCole Faust //
125*c217d954SCole Faust // Standard parameters:
126*c217d954SCole Faust // int *x -- outputs image width in pixels
127*c217d954SCole Faust // int *y -- outputs image height in pixels
128*c217d954SCole Faust // int *channels_in_file -- outputs # of image components in image file
129*c217d954SCole Faust // int desired_channels -- if non-zero, # of image components requested in result
130*c217d954SCole Faust //
131*c217d954SCole Faust // The return value from an image loader is an 'unsigned char *' which points
132*c217d954SCole Faust // to the pixel data, or NULL on an allocation failure or if the image is
133*c217d954SCole Faust // corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
134*c217d954SCole Faust // with each pixel consisting of N interleaved 8-bit components; the first
135*c217d954SCole Faust // pixel pointed to is top-left-most in the image. There is no padding between
136*c217d954SCole Faust // image scanlines or between pixels, regardless of format. The number of
137*c217d954SCole Faust // components N is 'desired_channels' if desired_channels is non-zero, or
138*c217d954SCole Faust // *channels_in_file otherwise. If desired_channels is non-zero,
139*c217d954SCole Faust // *channels_in_file has the number of components that _would_ have been
140*c217d954SCole Faust // output otherwise. E.g. if you set desired_channels to 4, you will always
141*c217d954SCole Faust // get RGBA output, but you can check *channels_in_file to see if it's trivially
142*c217d954SCole Faust // opaque because e.g. there were only 3 channels in the source image.
143*c217d954SCole Faust //
144*c217d954SCole Faust // An output image with N components has the following components interleaved
145*c217d954SCole Faust // in this order in each pixel:
146*c217d954SCole Faust //
147*c217d954SCole Faust // N=#comp components
148*c217d954SCole Faust // 1 grey
149*c217d954SCole Faust // 2 grey, alpha
150*c217d954SCole Faust // 3 red, green, blue
151*c217d954SCole Faust // 4 red, green, blue, alpha
152*c217d954SCole Faust //
153*c217d954SCole Faust // If image loading fails for any reason, the return value will be NULL,
154*c217d954SCole Faust // and *x, *y, *channels_in_file will be unchanged. The function
155*c217d954SCole Faust // stbi_failure_reason() can be queried for an extremely brief, end-user
156*c217d954SCole Faust // unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS
157*c217d954SCole Faust // to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
158*c217d954SCole Faust // more user-friendly ones.
159*c217d954SCole Faust //
160*c217d954SCole Faust // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
161*c217d954SCole Faust //
162*c217d954SCole Faust // ===========================================================================
163*c217d954SCole Faust //
164*c217d954SCole Faust // Philosophy
165*c217d954SCole Faust //
166*c217d954SCole Faust // stb libraries are designed with the following priorities:
167*c217d954SCole Faust //
168*c217d954SCole Faust // 1. easy to use
169*c217d954SCole Faust // 2. easy to maintain
170*c217d954SCole Faust // 3. good performance
171*c217d954SCole Faust //
172*c217d954SCole Faust // Sometimes I let "good performance" creep up in priority over "easy to maintain",
173*c217d954SCole Faust // and for best performance I may provide less-easy-to-use APIs that give higher
174*c217d954SCole Faust // performance, in addition to the easy to use ones. Nevertheless, it's important
175*c217d954SCole Faust // to keep in mind that from the standpoint of you, a client of this library,
176*c217d954SCole Faust // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.
177*c217d954SCole Faust //
178*c217d954SCole Faust // Some secondary priorities arise directly from the first two, some of which
179*c217d954SCole Faust // make more explicit reasons why performance can't be emphasized.
180*c217d954SCole Faust //
181*c217d954SCole Faust // - Portable ("ease of use")
182*c217d954SCole Faust // - Small source code footprint ("easy to maintain")
183*c217d954SCole Faust // - No dependencies ("ease of use")
184*c217d954SCole Faust //
185*c217d954SCole Faust // ===========================================================================
186*c217d954SCole Faust //
187*c217d954SCole Faust // I/O callbacks
188*c217d954SCole Faust //
189*c217d954SCole Faust // I/O callbacks allow you to read from arbitrary sources, like packaged
190*c217d954SCole Faust // files or some other source. Data read from callbacks are processed
191*c217d954SCole Faust // through a small internal buffer (currently 128 bytes) to try to reduce
192*c217d954SCole Faust // overhead.
193*c217d954SCole Faust //
194*c217d954SCole Faust // The three functions you must define are "read" (reads some bytes of data),
195*c217d954SCole Faust // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
196*c217d954SCole Faust //
197*c217d954SCole Faust // ===========================================================================
198*c217d954SCole Faust //
199*c217d954SCole Faust // SIMD support
200*c217d954SCole Faust //
201*c217d954SCole Faust // The JPEG decoder will try to automatically use SIMD kernels on x86 when
202*c217d954SCole Faust // supported by the compiler. For ARM Neon support, you must explicitly
203*c217d954SCole Faust // request it.
204*c217d954SCole Faust //
205*c217d954SCole Faust // (The old do-it-yourself SIMD API is no longer supported in the current
206*c217d954SCole Faust // code.)
207*c217d954SCole Faust //
208*c217d954SCole Faust // On x86, SSE2 will automatically be used when available based on a run-time
209*c217d954SCole Faust // test; if not, the generic C versions are used as a fall-back. On ARM targets,
210*c217d954SCole Faust // the typical path is to have separate builds for NEON and non-NEON devices
211*c217d954SCole Faust // (at least this is true for iOS and Android). Therefore, the NEON support is
212*c217d954SCole Faust // toggled by a build flag: define STBI_NEON to get NEON loops.
213*c217d954SCole Faust //
214*c217d954SCole Faust // If for some reason you do not want to use any of SIMD code, or if
215*c217d954SCole Faust // you have issues compiling it, you can disable it entirely by
216*c217d954SCole Faust // defining STBI_NO_SIMD.
217*c217d954SCole Faust //
218*c217d954SCole Faust // ===========================================================================
219*c217d954SCole Faust //
220*c217d954SCole Faust // HDR image support (disable by defining STBI_NO_HDR)
221*c217d954SCole Faust //
222*c217d954SCole Faust // stb_image now supports loading HDR images in general, and currently
223*c217d954SCole Faust // the Radiance .HDR file format, although the support is provided
224*c217d954SCole Faust // generically. You can still load any file through the existing interface;
225*c217d954SCole Faust // if you attempt to load an HDR file, it will be automatically remapped to
226*c217d954SCole Faust // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
227*c217d954SCole Faust // both of these constants can be reconfigured through this interface:
228*c217d954SCole Faust //
229*c217d954SCole Faust // stbi_hdr_to_ldr_gamma(2.2f);
230*c217d954SCole Faust // stbi_hdr_to_ldr_scale(1.0f);
231*c217d954SCole Faust //
232*c217d954SCole Faust // (note, do not use _inverse_ constants; stbi_image will invert them
233*c217d954SCole Faust // appropriately).
234*c217d954SCole Faust //
235*c217d954SCole Faust // Additionally, there is a new, parallel interface for loading files as
236*c217d954SCole Faust // (linear) floats to preserve the full dynamic range:
237*c217d954SCole Faust //
238*c217d954SCole Faust // float *data = stbi_loadf(filename, &x, &y, &n, 0);
239*c217d954SCole Faust //
240*c217d954SCole Faust // If you load LDR images through this interface, those images will
241*c217d954SCole Faust // be promoted to floating point values, run through the inverse of
242*c217d954SCole Faust // constants corresponding to the above:
243*c217d954SCole Faust //
244*c217d954SCole Faust // stbi_ldr_to_hdr_scale(1.0f);
245*c217d954SCole Faust // stbi_ldr_to_hdr_gamma(2.2f);
246*c217d954SCole Faust //
247*c217d954SCole Faust // Finally, given a filename (or an open file or memory block--see header
248*c217d954SCole Faust // file for details) containing image data, you can query for the "most
249*c217d954SCole Faust // appropriate" interface to use (that is, whether the image is HDR or
250*c217d954SCole Faust // not), using:
251*c217d954SCole Faust //
252*c217d954SCole Faust // stbi_is_hdr(char *filename);
253*c217d954SCole Faust //
254*c217d954SCole Faust // ===========================================================================
255*c217d954SCole Faust //
256*c217d954SCole Faust // iPhone PNG support:
257*c217d954SCole Faust //
258*c217d954SCole Faust // By default we convert iphone-formatted PNGs back to RGB, even though
259*c217d954SCole Faust // they are internally encoded differently. You can disable this conversion
260*c217d954SCole Faust // by by calling stbi_convert_iphone_png_to_rgb(0), in which case
261*c217d954SCole Faust // you will always just get the native iphone "format" through (which
262*c217d954SCole Faust // is BGR stored in RGB).
263*c217d954SCole Faust //
264*c217d954SCole Faust // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
265*c217d954SCole Faust // pixel to remove any premultiplied alpha *only* if the image file explicitly
266*c217d954SCole Faust // says there's premultiplied data (currently only happens in iPhone images,
267*c217d954SCole Faust // and only if iPhone convert-to-rgb processing is on).
268*c217d954SCole Faust //
269*c217d954SCole Faust // ===========================================================================
270*c217d954SCole Faust //
271*c217d954SCole Faust // ADDITIONAL CONFIGURATION
272*c217d954SCole Faust //
273*c217d954SCole Faust // - You can suppress implementation of any of the decoders to reduce
274*c217d954SCole Faust // your code footprint by #defining one or more of the following
275*c217d954SCole Faust // symbols before creating the implementation.
276*c217d954SCole Faust //
277*c217d954SCole Faust // STBI_NO_JPEG
278*c217d954SCole Faust // STBI_NO_PNG
279*c217d954SCole Faust // STBI_NO_BMP
280*c217d954SCole Faust // STBI_NO_PSD
281*c217d954SCole Faust // STBI_NO_TGA
282*c217d954SCole Faust // STBI_NO_GIF
283*c217d954SCole Faust // STBI_NO_HDR
284*c217d954SCole Faust // STBI_NO_PIC
285*c217d954SCole Faust // STBI_NO_PNM (.ppm and .pgm)
286*c217d954SCole Faust //
287*c217d954SCole Faust // - You can request *only* certain decoders and suppress all other ones
288*c217d954SCole Faust // (this will be more forward-compatible, as addition of new decoders
289*c217d954SCole Faust // doesn't require you to disable them explicitly):
290*c217d954SCole Faust //
291*c217d954SCole Faust // STBI_ONLY_JPEG
292*c217d954SCole Faust // STBI_ONLY_PNG
293*c217d954SCole Faust // STBI_ONLY_BMP
294*c217d954SCole Faust // STBI_ONLY_PSD
295*c217d954SCole Faust // STBI_ONLY_TGA
296*c217d954SCole Faust // STBI_ONLY_GIF
297*c217d954SCole Faust // STBI_ONLY_HDR
298*c217d954SCole Faust // STBI_ONLY_PIC
299*c217d954SCole Faust // STBI_ONLY_PNM (.ppm and .pgm)
300*c217d954SCole Faust //
301*c217d954SCole Faust // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
302*c217d954SCole Faust // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
303*c217d954SCole Faust //
304*c217d954SCole Faust
305*c217d954SCole Faust
306*c217d954SCole Faust #ifndef STBI_NO_STDIO
307*c217d954SCole Faust #include <stdio.h>
308*c217d954SCole Faust #endif // STBI_NO_STDIO
309*c217d954SCole Faust
310*c217d954SCole Faust #define STBI_VERSION 1
311*c217d954SCole Faust
312*c217d954SCole Faust enum
313*c217d954SCole Faust {
314*c217d954SCole Faust STBI_default = 0, // only used for desired_channels
315*c217d954SCole Faust
316*c217d954SCole Faust STBI_grey = 1,
317*c217d954SCole Faust STBI_grey_alpha = 2,
318*c217d954SCole Faust STBI_rgb = 3,
319*c217d954SCole Faust STBI_rgb_alpha = 4
320*c217d954SCole Faust };
321*c217d954SCole Faust
322*c217d954SCole Faust typedef unsigned char stbi_uc;
323*c217d954SCole Faust typedef unsigned short stbi_us;
324*c217d954SCole Faust
325*c217d954SCole Faust #ifdef __cplusplus
326*c217d954SCole Faust extern "C" {
327*c217d954SCole Faust #endif
328*c217d954SCole Faust
329*c217d954SCole Faust #ifdef STB_IMAGE_STATIC
330*c217d954SCole Faust #define STBIDEF static
331*c217d954SCole Faust #else
332*c217d954SCole Faust #define STBIDEF extern
333*c217d954SCole Faust #endif
334*c217d954SCole Faust
335*c217d954SCole Faust //////////////////////////////////////////////////////////////////////////////
336*c217d954SCole Faust //
337*c217d954SCole Faust // PRIMARY API - works on images of any type
338*c217d954SCole Faust //
339*c217d954SCole Faust
340*c217d954SCole Faust //
341*c217d954SCole Faust // load image by filename, open file, or memory buffer
342*c217d954SCole Faust //
343*c217d954SCole Faust
344*c217d954SCole Faust typedef struct
345*c217d954SCole Faust {
346*c217d954SCole Faust int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
347*c217d954SCole Faust void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
348*c217d954SCole Faust int (*eof) (void *user); // returns nonzero if we are at end of file/data
349*c217d954SCole Faust } stbi_io_callbacks;
350*c217d954SCole Faust
351*c217d954SCole Faust ////////////////////////////////////
352*c217d954SCole Faust //
353*c217d954SCole Faust // 8-bits-per-channel interface
354*c217d954SCole Faust //
355*c217d954SCole Faust
356*c217d954SCole Faust STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
357*c217d954SCole Faust 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);
358*c217d954SCole Faust #ifndef STBI_NO_GIF
359*c217d954SCole Faust 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);
360*c217d954SCole Faust #endif
361*c217d954SCole Faust
362*c217d954SCole Faust
363*c217d954SCole Faust #ifndef STBI_NO_STDIO
364*c217d954SCole Faust STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
365*c217d954SCole Faust STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
366*c217d954SCole Faust // for stbi_load_from_file, file pointer is left pointing immediately after image
367*c217d954SCole Faust #endif
368*c217d954SCole Faust
369*c217d954SCole Faust ////////////////////////////////////
370*c217d954SCole Faust //
371*c217d954SCole Faust // 16-bits-per-channel interface
372*c217d954SCole Faust //
373*c217d954SCole Faust
374*c217d954SCole Faust 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);
375*c217d954SCole Faust 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);
376*c217d954SCole Faust
377*c217d954SCole Faust #ifndef STBI_NO_STDIO
378*c217d954SCole Faust STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
379*c217d954SCole Faust STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
380*c217d954SCole Faust #endif
381*c217d954SCole Faust
382*c217d954SCole Faust ////////////////////////////////////
383*c217d954SCole Faust //
384*c217d954SCole Faust // float-per-channel interface
385*c217d954SCole Faust //
386*c217d954SCole Faust #ifndef STBI_NO_LINEAR
387*c217d954SCole Faust STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
388*c217d954SCole Faust STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
389*c217d954SCole Faust
390*c217d954SCole Faust #ifndef STBI_NO_STDIO
391*c217d954SCole Faust STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
392*c217d954SCole Faust STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
393*c217d954SCole Faust #endif
394*c217d954SCole Faust #endif
395*c217d954SCole Faust
396*c217d954SCole Faust #ifndef STBI_NO_HDR
397*c217d954SCole Faust STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);
398*c217d954SCole Faust STBIDEF void stbi_hdr_to_ldr_scale(float scale);
399*c217d954SCole Faust #endif // STBI_NO_HDR
400*c217d954SCole Faust
401*c217d954SCole Faust #ifndef STBI_NO_LINEAR
402*c217d954SCole Faust STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);
403*c217d954SCole Faust STBIDEF void stbi_ldr_to_hdr_scale(float scale);
404*c217d954SCole Faust #endif // STBI_NO_LINEAR
405*c217d954SCole Faust
406*c217d954SCole Faust // stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR
407*c217d954SCole Faust STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
408*c217d954SCole Faust STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
409*c217d954SCole Faust #ifndef STBI_NO_STDIO
410*c217d954SCole Faust STBIDEF int stbi_is_hdr (char const *filename);
411*c217d954SCole Faust STBIDEF int stbi_is_hdr_from_file(FILE *f);
412*c217d954SCole Faust #endif // STBI_NO_STDIO
413*c217d954SCole Faust
414*c217d954SCole Faust
415*c217d954SCole Faust // get a VERY brief reason for failure
416*c217d954SCole Faust // NOT THREADSAFE
417*c217d954SCole Faust STBIDEF const char *stbi_failure_reason (void);
418*c217d954SCole Faust
419*c217d954SCole Faust // free the loaded image -- this is just free()
420*c217d954SCole Faust STBIDEF void stbi_image_free (void *retval_from_stbi_load);
421*c217d954SCole Faust
422*c217d954SCole Faust // get image dimensions & components without fully decoding
423*c217d954SCole Faust STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
424*c217d954SCole Faust STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
425*c217d954SCole Faust STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);
426*c217d954SCole Faust STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user);
427*c217d954SCole Faust
428*c217d954SCole Faust #ifndef STBI_NO_STDIO
429*c217d954SCole Faust STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);
430*c217d954SCole Faust STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
431*c217d954SCole Faust STBIDEF int stbi_is_16_bit (char const *filename);
432*c217d954SCole Faust STBIDEF int stbi_is_16_bit_from_file(FILE *f);
433*c217d954SCole Faust #endif
434*c217d954SCole Faust
435*c217d954SCole Faust
436*c217d954SCole Faust
437*c217d954SCole Faust // for image formats that explicitly notate that they have premultiplied alpha,
438*c217d954SCole Faust // we just return the colors as stored in the file. set this flag to force
439*c217d954SCole Faust // unpremultiplication. results are undefined if the unpremultiply overflow.
440*c217d954SCole Faust STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
441*c217d954SCole Faust
442*c217d954SCole Faust // indicate whether we should process iphone images back to canonical format,
443*c217d954SCole Faust // or just pass them through "as-is"
444*c217d954SCole Faust STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
445*c217d954SCole Faust
446*c217d954SCole Faust // flip the image vertically, so the first pixel in the output array is the bottom left
447*c217d954SCole Faust STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);
448*c217d954SCole Faust
449*c217d954SCole Faust // ZLIB client - used by PNG, available for other purposes
450*c217d954SCole Faust
451*c217d954SCole Faust STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
452*c217d954SCole Faust STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
453*c217d954SCole Faust STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
454*c217d954SCole Faust STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
455*c217d954SCole Faust
456*c217d954SCole Faust STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
457*c217d954SCole Faust STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
458*c217d954SCole Faust
459*c217d954SCole Faust
460*c217d954SCole Faust #ifdef __cplusplus
461*c217d954SCole Faust }
462*c217d954SCole Faust #endif
463*c217d954SCole Faust
464*c217d954SCole Faust //
465*c217d954SCole Faust //
466*c217d954SCole Faust //// end header file /////////////////////////////////////////////////////
467*c217d954SCole Faust #endif // STBI_INCLUDE_STB_IMAGE_H
468*c217d954SCole Faust
469*c217d954SCole Faust #ifdef STB_IMAGE_IMPLEMENTATION
470*c217d954SCole Faust
471*c217d954SCole Faust #if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \
472*c217d954SCole Faust || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \
473*c217d954SCole Faust || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \
474*c217d954SCole Faust || defined(STBI_ONLY_ZLIB)
475*c217d954SCole Faust #ifndef STBI_ONLY_JPEG
476*c217d954SCole Faust #define STBI_NO_JPEG
477*c217d954SCole Faust #endif
478*c217d954SCole Faust #ifndef STBI_ONLY_PNG
479*c217d954SCole Faust #define STBI_NO_PNG
480*c217d954SCole Faust #endif
481*c217d954SCole Faust #ifndef STBI_ONLY_BMP
482*c217d954SCole Faust #define STBI_NO_BMP
483*c217d954SCole Faust #endif
484*c217d954SCole Faust #ifndef STBI_ONLY_PSD
485*c217d954SCole Faust #define STBI_NO_PSD
486*c217d954SCole Faust #endif
487*c217d954SCole Faust #ifndef STBI_ONLY_TGA
488*c217d954SCole Faust #define STBI_NO_TGA
489*c217d954SCole Faust #endif
490*c217d954SCole Faust #ifndef STBI_ONLY_GIF
491*c217d954SCole Faust #define STBI_NO_GIF
492*c217d954SCole Faust #endif
493*c217d954SCole Faust #ifndef STBI_ONLY_HDR
494*c217d954SCole Faust #define STBI_NO_HDR
495*c217d954SCole Faust #endif
496*c217d954SCole Faust #ifndef STBI_ONLY_PIC
497*c217d954SCole Faust #define STBI_NO_PIC
498*c217d954SCole Faust #endif
499*c217d954SCole Faust #ifndef STBI_ONLY_PNM
500*c217d954SCole Faust #define STBI_NO_PNM
501*c217d954SCole Faust #endif
502*c217d954SCole Faust #endif
503*c217d954SCole Faust
504*c217d954SCole Faust #if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB)
505*c217d954SCole Faust #define STBI_NO_ZLIB
506*c217d954SCole Faust #endif
507*c217d954SCole Faust
508*c217d954SCole Faust
509*c217d954SCole Faust #include <stdarg.h>
510*c217d954SCole Faust #include <stddef.h> // ptrdiff_t on osx
511*c217d954SCole Faust #include <stdlib.h>
512*c217d954SCole Faust #include <string.h>
513*c217d954SCole Faust #include <limits.h>
514*c217d954SCole Faust
515*c217d954SCole Faust #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
516*c217d954SCole Faust #include <math.h> // ldexp, pow
517*c217d954SCole Faust #endif
518*c217d954SCole Faust
519*c217d954SCole Faust #ifndef STBI_NO_STDIO
520*c217d954SCole Faust #include <stdio.h>
521*c217d954SCole Faust #endif
522*c217d954SCole Faust
523*c217d954SCole Faust #ifndef STBI_ASSERT
524*c217d954SCole Faust #include <assert.h>
525*c217d954SCole Faust #define STBI_ASSERT(x) assert(x)
526*c217d954SCole Faust #endif
527*c217d954SCole Faust
528*c217d954SCole Faust
529*c217d954SCole Faust #ifndef _MSC_VER
530*c217d954SCole Faust #ifdef __cplusplus
531*c217d954SCole Faust #define stbi_inline inline
532*c217d954SCole Faust #else
533*c217d954SCole Faust #define stbi_inline
534*c217d954SCole Faust #endif
535*c217d954SCole Faust #else
536*c217d954SCole Faust #define stbi_inline __forceinline
537*c217d954SCole Faust #endif
538*c217d954SCole Faust
539*c217d954SCole Faust
540*c217d954SCole Faust #ifdef _MSC_VER
541*c217d954SCole Faust typedef unsigned short stbi__uint16;
542*c217d954SCole Faust typedef signed short stbi__int16;
543*c217d954SCole Faust typedef unsigned int stbi__uint32;
544*c217d954SCole Faust typedef signed int stbi__int32;
545*c217d954SCole Faust #else
546*c217d954SCole Faust #include <stdint.h>
547*c217d954SCole Faust typedef uint16_t stbi__uint16;
548*c217d954SCole Faust typedef int16_t stbi__int16;
549*c217d954SCole Faust typedef uint32_t stbi__uint32;
550*c217d954SCole Faust typedef int32_t stbi__int32;
551*c217d954SCole Faust #endif
552*c217d954SCole Faust
553*c217d954SCole Faust // should produce compiler error if size is wrong
554*c217d954SCole Faust typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
555*c217d954SCole Faust
556*c217d954SCole Faust #ifdef _MSC_VER
557*c217d954SCole Faust #define STBI_NOTUSED(v) (void)(v)
558*c217d954SCole Faust #else
559*c217d954SCole Faust #define STBI_NOTUSED(v) (void)sizeof(v)
560*c217d954SCole Faust #endif
561*c217d954SCole Faust
562*c217d954SCole Faust #ifdef _MSC_VER
563*c217d954SCole Faust #define STBI_HAS_LROTL
564*c217d954SCole Faust #endif
565*c217d954SCole Faust
566*c217d954SCole Faust #ifdef STBI_HAS_LROTL
567*c217d954SCole Faust #define stbi_lrot(x,y) _lrotl(x,y)
568*c217d954SCole Faust #else
569*c217d954SCole Faust #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
570*c217d954SCole Faust #endif
571*c217d954SCole Faust
572*c217d954SCole Faust #if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))
573*c217d954SCole Faust // ok
574*c217d954SCole Faust #elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED)
575*c217d954SCole Faust // ok
576*c217d954SCole Faust #else
577*c217d954SCole Faust #error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)."
578*c217d954SCole Faust #endif
579*c217d954SCole Faust
580*c217d954SCole Faust #ifndef STBI_MALLOC
581*c217d954SCole Faust #define STBI_MALLOC(sz) malloc(sz)
582*c217d954SCole Faust #define STBI_REALLOC(p,newsz) realloc(p,newsz)
583*c217d954SCole Faust #define STBI_FREE(p) free(p)
584*c217d954SCole Faust #endif
585*c217d954SCole Faust
586*c217d954SCole Faust #ifndef STBI_REALLOC_SIZED
587*c217d954SCole Faust #define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)
588*c217d954SCole Faust #endif
589*c217d954SCole Faust
590*c217d954SCole Faust // x86/x64 detection
591*c217d954SCole Faust #if defined(__x86_64__) || defined(_M_X64)
592*c217d954SCole Faust #define STBI__X64_TARGET
593*c217d954SCole Faust #elif defined(__i386) || defined(_M_IX86)
594*c217d954SCole Faust #define STBI__X86_TARGET
595*c217d954SCole Faust #endif
596*c217d954SCole Faust
597*c217d954SCole Faust #if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD)
598*c217d954SCole Faust // gcc doesn't support sse2 intrinsics unless you compile with -msse2,
599*c217d954SCole Faust // which in turn means it gets to use SSE2 everywhere. This is unfortunate,
600*c217d954SCole Faust // but previous attempts to provide the SSE2 functions with runtime
601*c217d954SCole Faust // detection caused numerous issues. The way architecture extensions are
602*c217d954SCole Faust // exposed in GCC/Clang is, sadly, not really suited for one-file libs.
603*c217d954SCole Faust // New behavior: if compiled with -msse2, we use SSE2 without any
604*c217d954SCole Faust // detection; if not, we don't use it at all.
605*c217d954SCole Faust #define STBI_NO_SIMD
606*c217d954SCole Faust #endif
607*c217d954SCole Faust
608*c217d954SCole Faust #if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)
609*c217d954SCole Faust // Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET
610*c217d954SCole Faust //
611*c217d954SCole Faust // 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the
612*c217d954SCole Faust // Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant.
613*c217d954SCole Faust // As a result, enabling SSE2 on 32-bit MinGW is dangerous when not
614*c217d954SCole Faust // simultaneously enabling "-mstackrealign".
615*c217d954SCole Faust //
616*c217d954SCole Faust // See https://github.com/nothings/stb/issues/81 for more information.
617*c217d954SCole Faust //
618*c217d954SCole Faust // So default to no SSE2 on 32-bit MinGW. If you've read this far and added
619*c217d954SCole Faust // -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2.
620*c217d954SCole Faust #define STBI_NO_SIMD
621*c217d954SCole Faust #endif
622*c217d954SCole Faust
623*c217d954SCole Faust #if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))
624*c217d954SCole Faust #define STBI_SSE2
625*c217d954SCole Faust #include <emmintrin.h>
626*c217d954SCole Faust
627*c217d954SCole Faust #ifdef _MSC_VER
628*c217d954SCole Faust
629*c217d954SCole Faust #if _MSC_VER >= 1400 // not VC6
630*c217d954SCole Faust #include <intrin.h> // __cpuid
stbi__cpuid3(void)631*c217d954SCole Faust static int stbi__cpuid3(void)
632*c217d954SCole Faust {
633*c217d954SCole Faust int info[4];
634*c217d954SCole Faust __cpuid(info,1);
635*c217d954SCole Faust return info[3];
636*c217d954SCole Faust }
637*c217d954SCole Faust #else
stbi__cpuid3(void)638*c217d954SCole Faust static int stbi__cpuid3(void)
639*c217d954SCole Faust {
640*c217d954SCole Faust int res;
641*c217d954SCole Faust __asm {
642*c217d954SCole Faust mov eax,1
643*c217d954SCole Faust cpuid
644*c217d954SCole Faust mov res,edx
645*c217d954SCole Faust }
646*c217d954SCole Faust return res;
647*c217d954SCole Faust }
648*c217d954SCole Faust #endif
649*c217d954SCole Faust
650*c217d954SCole Faust #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
651*c217d954SCole Faust
stbi__sse2_available(void)652*c217d954SCole Faust static int stbi__sse2_available(void)
653*c217d954SCole Faust {
654*c217d954SCole Faust int info3 = stbi__cpuid3();
655*c217d954SCole Faust return ((info3 >> 26) & 1) != 0;
656*c217d954SCole Faust }
657*c217d954SCole Faust #else // assume GCC-style if not VC++
658*c217d954SCole Faust #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
659*c217d954SCole Faust
stbi__sse2_available(void)660*c217d954SCole Faust static int stbi__sse2_available(void)
661*c217d954SCole Faust {
662*c217d954SCole Faust // If we're even attempting to compile this on GCC/Clang, that means
663*c217d954SCole Faust // -msse2 is on, which means the compiler is allowed to use SSE2
664*c217d954SCole Faust // instructions at will, and so are we.
665*c217d954SCole Faust return 1;
666*c217d954SCole Faust }
667*c217d954SCole Faust #endif
668*c217d954SCole Faust #endif
669*c217d954SCole Faust
670*c217d954SCole Faust // ARM NEON
671*c217d954SCole Faust #if defined(STBI_NO_SIMD) && defined(STBI_NEON)
672*c217d954SCole Faust #undef STBI_NEON
673*c217d954SCole Faust #endif
674*c217d954SCole Faust
675*c217d954SCole Faust #ifdef STBI_NEON
676*c217d954SCole Faust #include <arm_neon.h>
677*c217d954SCole Faust // assume GCC or Clang on ARM targets
678*c217d954SCole Faust #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
679*c217d954SCole Faust #endif
680*c217d954SCole Faust
681*c217d954SCole Faust #ifndef STBI_SIMD_ALIGN
682*c217d954SCole Faust #define STBI_SIMD_ALIGN(type, name) type name
683*c217d954SCole Faust #endif
684*c217d954SCole Faust
685*c217d954SCole Faust ///////////////////////////////////////////////
686*c217d954SCole Faust //
687*c217d954SCole Faust // stbi__context struct and start_xxx functions
688*c217d954SCole Faust
689*c217d954SCole Faust // stbi__context structure is our basic context used by all images, so it
690*c217d954SCole Faust // contains all the IO context, plus some basic image information
691*c217d954SCole Faust typedef struct
692*c217d954SCole Faust {
693*c217d954SCole Faust stbi__uint32 img_x, img_y;
694*c217d954SCole Faust int img_n, img_out_n;
695*c217d954SCole Faust
696*c217d954SCole Faust stbi_io_callbacks io;
697*c217d954SCole Faust void *io_user_data;
698*c217d954SCole Faust
699*c217d954SCole Faust int read_from_callbacks;
700*c217d954SCole Faust int buflen;
701*c217d954SCole Faust stbi_uc buffer_start[128];
702*c217d954SCole Faust
703*c217d954SCole Faust stbi_uc *img_buffer, *img_buffer_end;
704*c217d954SCole Faust stbi_uc *img_buffer_original, *img_buffer_original_end;
705*c217d954SCole Faust } stbi__context;
706*c217d954SCole Faust
707*c217d954SCole Faust
708*c217d954SCole Faust static void stbi__refill_buffer(stbi__context *s);
709*c217d954SCole Faust
710*c217d954SCole Faust // initialize a memory-decode context
stbi__start_mem(stbi__context * s,stbi_uc const * buffer,int len)711*c217d954SCole Faust static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)
712*c217d954SCole Faust {
713*c217d954SCole Faust s->io.read = NULL;
714*c217d954SCole Faust s->read_from_callbacks = 0;
715*c217d954SCole Faust s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;
716*c217d954SCole Faust s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len;
717*c217d954SCole Faust }
718*c217d954SCole Faust
719*c217d954SCole Faust // initialize a callback-based context
stbi__start_callbacks(stbi__context * s,stbi_io_callbacks * c,void * user)720*c217d954SCole Faust static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user)
721*c217d954SCole Faust {
722*c217d954SCole Faust s->io = *c;
723*c217d954SCole Faust s->io_user_data = user;
724*c217d954SCole Faust s->buflen = sizeof(s->buffer_start);
725*c217d954SCole Faust s->read_from_callbacks = 1;
726*c217d954SCole Faust s->img_buffer_original = s->buffer_start;
727*c217d954SCole Faust stbi__refill_buffer(s);
728*c217d954SCole Faust s->img_buffer_original_end = s->img_buffer_end;
729*c217d954SCole Faust }
730*c217d954SCole Faust
731*c217d954SCole Faust #ifndef STBI_NO_STDIO
732*c217d954SCole Faust
stbi__stdio_read(void * user,char * data,int size)733*c217d954SCole Faust static int stbi__stdio_read(void *user, char *data, int size)
734*c217d954SCole Faust {
735*c217d954SCole Faust return (int) fread(data,1,size,(FILE*) user);
736*c217d954SCole Faust }
737*c217d954SCole Faust
stbi__stdio_skip(void * user,int n)738*c217d954SCole Faust static void stbi__stdio_skip(void *user, int n)
739*c217d954SCole Faust {
740*c217d954SCole Faust fseek((FILE*) user, n, SEEK_CUR);
741*c217d954SCole Faust }
742*c217d954SCole Faust
stbi__stdio_eof(void * user)743*c217d954SCole Faust static int stbi__stdio_eof(void *user)
744*c217d954SCole Faust {
745*c217d954SCole Faust return feof((FILE*) user);
746*c217d954SCole Faust }
747*c217d954SCole Faust
748*c217d954SCole Faust static stbi_io_callbacks stbi__stdio_callbacks =
749*c217d954SCole Faust {
750*c217d954SCole Faust stbi__stdio_read,
751*c217d954SCole Faust stbi__stdio_skip,
752*c217d954SCole Faust stbi__stdio_eof,
753*c217d954SCole Faust };
754*c217d954SCole Faust
stbi__start_file(stbi__context * s,FILE * f)755*c217d954SCole Faust static void stbi__start_file(stbi__context *s, FILE *f)
756*c217d954SCole Faust {
757*c217d954SCole Faust stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);
758*c217d954SCole Faust }
759*c217d954SCole Faust
760*c217d954SCole Faust //static void stop_file(stbi__context *s) { }
761*c217d954SCole Faust
762*c217d954SCole Faust #endif // !STBI_NO_STDIO
763*c217d954SCole Faust
stbi__rewind(stbi__context * s)764*c217d954SCole Faust static void stbi__rewind(stbi__context *s)
765*c217d954SCole Faust {
766*c217d954SCole Faust // conceptually rewind SHOULD rewind to the beginning of the stream,
767*c217d954SCole Faust // but we just rewind to the beginning of the initial buffer, because
768*c217d954SCole Faust // we only use it after doing 'test', which only ever looks at at most 92 bytes
769*c217d954SCole Faust s->img_buffer = s->img_buffer_original;
770*c217d954SCole Faust s->img_buffer_end = s->img_buffer_original_end;
771*c217d954SCole Faust }
772*c217d954SCole Faust
773*c217d954SCole Faust enum
774*c217d954SCole Faust {
775*c217d954SCole Faust STBI_ORDER_RGB,
776*c217d954SCole Faust STBI_ORDER_BGR
777*c217d954SCole Faust };
778*c217d954SCole Faust
779*c217d954SCole Faust typedef struct
780*c217d954SCole Faust {
781*c217d954SCole Faust int bits_per_channel;
782*c217d954SCole Faust int num_channels;
783*c217d954SCole Faust int channel_order;
784*c217d954SCole Faust } stbi__result_info;
785*c217d954SCole Faust
786*c217d954SCole Faust #ifndef STBI_NO_JPEG
787*c217d954SCole Faust static int stbi__jpeg_test(stbi__context *s);
788*c217d954SCole Faust static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
789*c217d954SCole Faust static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp);
790*c217d954SCole Faust #endif
791*c217d954SCole Faust
792*c217d954SCole Faust #ifndef STBI_NO_PNG
793*c217d954SCole Faust static int stbi__png_test(stbi__context *s);
794*c217d954SCole Faust static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
795*c217d954SCole Faust static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);
796*c217d954SCole Faust static int stbi__png_is16(stbi__context *s);
797*c217d954SCole Faust #endif
798*c217d954SCole Faust
799*c217d954SCole Faust #ifndef STBI_NO_BMP
800*c217d954SCole Faust static int stbi__bmp_test(stbi__context *s);
801*c217d954SCole Faust static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
802*c217d954SCole Faust static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp);
803*c217d954SCole Faust #endif
804*c217d954SCole Faust
805*c217d954SCole Faust #ifndef STBI_NO_TGA
806*c217d954SCole Faust static int stbi__tga_test(stbi__context *s);
807*c217d954SCole Faust static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
808*c217d954SCole Faust static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp);
809*c217d954SCole Faust #endif
810*c217d954SCole Faust
811*c217d954SCole Faust #ifndef STBI_NO_PSD
812*c217d954SCole Faust static int stbi__psd_test(stbi__context *s);
813*c217d954SCole Faust static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);
814*c217d954SCole Faust static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);
815*c217d954SCole Faust static int stbi__psd_is16(stbi__context *s);
816*c217d954SCole Faust #endif
817*c217d954SCole Faust
818*c217d954SCole Faust #ifndef STBI_NO_HDR
819*c217d954SCole Faust static int stbi__hdr_test(stbi__context *s);
820*c217d954SCole Faust static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
821*c217d954SCole Faust static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp);
822*c217d954SCole Faust #endif
823*c217d954SCole Faust
824*c217d954SCole Faust #ifndef STBI_NO_PIC
825*c217d954SCole Faust static int stbi__pic_test(stbi__context *s);
826*c217d954SCole Faust static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
827*c217d954SCole Faust static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp);
828*c217d954SCole Faust #endif
829*c217d954SCole Faust
830*c217d954SCole Faust #ifndef STBI_NO_GIF
831*c217d954SCole Faust static int stbi__gif_test(stbi__context *s);
832*c217d954SCole Faust static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
833*c217d954SCole Faust static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
834*c217d954SCole Faust static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);
835*c217d954SCole Faust #endif
836*c217d954SCole Faust
837*c217d954SCole Faust #ifndef STBI_NO_PNM
838*c217d954SCole Faust static int stbi__pnm_test(stbi__context *s);
839*c217d954SCole Faust static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
840*c217d954SCole Faust static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);
841*c217d954SCole Faust #endif
842*c217d954SCole Faust
843*c217d954SCole Faust // this is not threadsafe
844*c217d954SCole Faust static const char *stbi__g_failure_reason;
845*c217d954SCole Faust
stbi_failure_reason(void)846*c217d954SCole Faust STBIDEF const char *stbi_failure_reason(void)
847*c217d954SCole Faust {
848*c217d954SCole Faust return stbi__g_failure_reason;
849*c217d954SCole Faust }
850*c217d954SCole Faust
stbi__err(const char * str)851*c217d954SCole Faust static int stbi__err(const char *str)
852*c217d954SCole Faust {
853*c217d954SCole Faust stbi__g_failure_reason = str;
854*c217d954SCole Faust return 0;
855*c217d954SCole Faust }
856*c217d954SCole Faust
stbi__malloc(size_t size)857*c217d954SCole Faust static void *stbi__malloc(size_t size)
858*c217d954SCole Faust {
859*c217d954SCole Faust return STBI_MALLOC(size);
860*c217d954SCole Faust }
861*c217d954SCole Faust
862*c217d954SCole Faust // stb_image uses ints pervasively, including for offset calculations.
863*c217d954SCole Faust // therefore the largest decoded image size we can support with the
864*c217d954SCole Faust // current code, even on 64-bit targets, is INT_MAX. this is not a
865*c217d954SCole Faust // significant limitation for the intended use case.
866*c217d954SCole Faust //
867*c217d954SCole Faust // we do, however, need to make sure our size calculations don't
868*c217d954SCole Faust // overflow. hence a few helper functions for size calculations that
869*c217d954SCole Faust // multiply integers together, making sure that they're non-negative
870*c217d954SCole Faust // and no overflow occurs.
871*c217d954SCole Faust
872*c217d954SCole Faust // return 1 if the sum is valid, 0 on overflow.
873*c217d954SCole Faust // negative terms are considered invalid.
stbi__addsizes_valid(int a,int b)874*c217d954SCole Faust static int stbi__addsizes_valid(int a, int b)
875*c217d954SCole Faust {
876*c217d954SCole Faust if (b < 0) return 0;
877*c217d954SCole Faust // now 0 <= b <= INT_MAX, hence also
878*c217d954SCole Faust // 0 <= INT_MAX - b <= INTMAX.
879*c217d954SCole Faust // And "a + b <= INT_MAX" (which might overflow) is the
880*c217d954SCole Faust // same as a <= INT_MAX - b (no overflow)
881*c217d954SCole Faust return a <= INT_MAX - b;
882*c217d954SCole Faust }
883*c217d954SCole Faust
884*c217d954SCole Faust // returns 1 if the product is valid, 0 on overflow.
885*c217d954SCole Faust // negative factors are considered invalid.
stbi__mul2sizes_valid(int a,int b)886*c217d954SCole Faust static int stbi__mul2sizes_valid(int a, int b)
887*c217d954SCole Faust {
888*c217d954SCole Faust if (a < 0 || b < 0) return 0;
889*c217d954SCole Faust if (b == 0) return 1; // mul-by-0 is always safe
890*c217d954SCole Faust // portable way to check for no overflows in a*b
891*c217d954SCole Faust return a <= INT_MAX/b;
892*c217d954SCole Faust }
893*c217d954SCole Faust
894*c217d954SCole Faust // returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
stbi__mad2sizes_valid(int a,int b,int add)895*c217d954SCole Faust static int stbi__mad2sizes_valid(int a, int b, int add)
896*c217d954SCole Faust {
897*c217d954SCole Faust return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add);
898*c217d954SCole Faust }
899*c217d954SCole Faust
900*c217d954SCole Faust // returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow
stbi__mad3sizes_valid(int a,int b,int c,int add)901*c217d954SCole Faust static int stbi__mad3sizes_valid(int a, int b, int c, int add)
902*c217d954SCole Faust {
903*c217d954SCole Faust return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
904*c217d954SCole Faust stbi__addsizes_valid(a*b*c, add);
905*c217d954SCole Faust }
906*c217d954SCole Faust
907*c217d954SCole Faust // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
908*c217d954SCole Faust #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
stbi__mad4sizes_valid(int a,int b,int c,int d,int add)909*c217d954SCole Faust static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
910*c217d954SCole Faust {
911*c217d954SCole Faust return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
912*c217d954SCole Faust stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add);
913*c217d954SCole Faust }
914*c217d954SCole Faust #endif
915*c217d954SCole Faust
916*c217d954SCole Faust // mallocs with size overflow checking
stbi__malloc_mad2(int a,int b,int add)917*c217d954SCole Faust static void *stbi__malloc_mad2(int a, int b, int add)
918*c217d954SCole Faust {
919*c217d954SCole Faust if (!stbi__mad2sizes_valid(a, b, add)) return NULL;
920*c217d954SCole Faust return stbi__malloc(a*b + add);
921*c217d954SCole Faust }
922*c217d954SCole Faust
stbi__malloc_mad3(int a,int b,int c,int add)923*c217d954SCole Faust static void *stbi__malloc_mad3(int a, int b, int c, int add)
924*c217d954SCole Faust {
925*c217d954SCole Faust if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL;
926*c217d954SCole Faust return stbi__malloc(a*b*c + add);
927*c217d954SCole Faust }
928*c217d954SCole Faust
929*c217d954SCole Faust #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
stbi__malloc_mad4(int a,int b,int c,int d,int add)930*c217d954SCole Faust static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
931*c217d954SCole Faust {
932*c217d954SCole Faust if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;
933*c217d954SCole Faust return stbi__malloc(a*b*c*d + add);
934*c217d954SCole Faust }
935*c217d954SCole Faust #endif
936*c217d954SCole Faust
937*c217d954SCole Faust // stbi__err - error
938*c217d954SCole Faust // stbi__errpf - error returning pointer to float
939*c217d954SCole Faust // stbi__errpuc - error returning pointer to unsigned char
940*c217d954SCole Faust
941*c217d954SCole Faust #ifdef STBI_NO_FAILURE_STRINGS
942*c217d954SCole Faust #define stbi__err(x,y) 0
943*c217d954SCole Faust #elif defined(STBI_FAILURE_USERMSG)
944*c217d954SCole Faust #define stbi__err(x,y) stbi__err(y)
945*c217d954SCole Faust #else
946*c217d954SCole Faust #define stbi__err(x,y) stbi__err(x)
947*c217d954SCole Faust #endif
948*c217d954SCole Faust
949*c217d954SCole Faust #define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL))
950*c217d954SCole Faust #define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL))
951*c217d954SCole Faust
stbi_image_free(void * retval_from_stbi_load)952*c217d954SCole Faust STBIDEF void stbi_image_free(void *retval_from_stbi_load)
953*c217d954SCole Faust {
954*c217d954SCole Faust STBI_FREE(retval_from_stbi_load);
955*c217d954SCole Faust }
956*c217d954SCole Faust
957*c217d954SCole Faust #ifndef STBI_NO_LINEAR
958*c217d954SCole Faust static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
959*c217d954SCole Faust #endif
960*c217d954SCole Faust
961*c217d954SCole Faust #ifndef STBI_NO_HDR
962*c217d954SCole Faust static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);
963*c217d954SCole Faust #endif
964*c217d954SCole Faust
965*c217d954SCole Faust static int stbi__vertically_flip_on_load = 0;
966*c217d954SCole Faust
stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)967*c217d954SCole Faust STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
968*c217d954SCole Faust {
969*c217d954SCole Faust stbi__vertically_flip_on_load = flag_true_if_should_flip;
970*c217d954SCole Faust }
971*c217d954SCole Faust
stbi__load_main(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri,int bpc)972*c217d954SCole Faust static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
973*c217d954SCole Faust {
974*c217d954SCole Faust memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields
975*c217d954SCole Faust ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed
976*c217d954SCole Faust ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order
977*c217d954SCole Faust ri->num_channels = 0;
978*c217d954SCole Faust
979*c217d954SCole Faust #ifndef STBI_NO_JPEG
980*c217d954SCole Faust if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri);
981*c217d954SCole Faust #endif
982*c217d954SCole Faust #ifndef STBI_NO_PNG
983*c217d954SCole Faust if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri);
984*c217d954SCole Faust #endif
985*c217d954SCole Faust #ifndef STBI_NO_BMP
986*c217d954SCole Faust if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri);
987*c217d954SCole Faust #endif
988*c217d954SCole Faust #ifndef STBI_NO_GIF
989*c217d954SCole Faust if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri);
990*c217d954SCole Faust #endif
991*c217d954SCole Faust #ifndef STBI_NO_PSD
992*c217d954SCole Faust if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc);
993*c217d954SCole Faust #endif
994*c217d954SCole Faust #ifndef STBI_NO_PIC
995*c217d954SCole Faust if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);
996*c217d954SCole Faust #endif
997*c217d954SCole Faust #ifndef STBI_NO_PNM
998*c217d954SCole Faust if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri);
999*c217d954SCole Faust #endif
1000*c217d954SCole Faust
1001*c217d954SCole Faust #ifndef STBI_NO_HDR
1002*c217d954SCole Faust if (stbi__hdr_test(s)) {
1003*c217d954SCole Faust float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri);
1004*c217d954SCole Faust return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
1005*c217d954SCole Faust }
1006*c217d954SCole Faust #endif
1007*c217d954SCole Faust
1008*c217d954SCole Faust #ifndef STBI_NO_TGA
1009*c217d954SCole Faust // test tga last because it's a crappy test!
1010*c217d954SCole Faust if (stbi__tga_test(s))
1011*c217d954SCole Faust return stbi__tga_load(s,x,y,comp,req_comp, ri);
1012*c217d954SCole Faust #endif
1013*c217d954SCole Faust
1014*c217d954SCole Faust return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt");
1015*c217d954SCole Faust }
1016*c217d954SCole Faust
stbi__convert_16_to_8(stbi__uint16 * orig,int w,int h,int channels)1017*c217d954SCole Faust static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels)
1018*c217d954SCole Faust {
1019*c217d954SCole Faust int i;
1020*c217d954SCole Faust int img_len = w * h * channels;
1021*c217d954SCole Faust stbi_uc *reduced;
1022*c217d954SCole Faust
1023*c217d954SCole Faust reduced = (stbi_uc *) stbi__malloc(img_len);
1024*c217d954SCole Faust if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory");
1025*c217d954SCole Faust
1026*c217d954SCole Faust for (i = 0; i < img_len; ++i)
1027*c217d954SCole Faust reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling
1028*c217d954SCole Faust
1029*c217d954SCole Faust STBI_FREE(orig);
1030*c217d954SCole Faust return reduced;
1031*c217d954SCole Faust }
1032*c217d954SCole Faust
stbi__convert_8_to_16(stbi_uc * orig,int w,int h,int channels)1033*c217d954SCole Faust static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels)
1034*c217d954SCole Faust {
1035*c217d954SCole Faust int i;
1036*c217d954SCole Faust int img_len = w * h * channels;
1037*c217d954SCole Faust stbi__uint16 *enlarged;
1038*c217d954SCole Faust
1039*c217d954SCole Faust enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);
1040*c217d954SCole Faust if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
1041*c217d954SCole Faust
1042*c217d954SCole Faust for (i = 0; i < img_len; ++i)
1043*c217d954SCole Faust enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff
1044*c217d954SCole Faust
1045*c217d954SCole Faust STBI_FREE(orig);
1046*c217d954SCole Faust return enlarged;
1047*c217d954SCole Faust }
1048*c217d954SCole Faust
stbi__vertical_flip(void * image,int w,int h,int bytes_per_pixel)1049*c217d954SCole Faust static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
1050*c217d954SCole Faust {
1051*c217d954SCole Faust int row;
1052*c217d954SCole Faust size_t bytes_per_row = (size_t)w * bytes_per_pixel;
1053*c217d954SCole Faust stbi_uc temp[2048];
1054*c217d954SCole Faust stbi_uc *bytes = (stbi_uc *)image;
1055*c217d954SCole Faust
1056*c217d954SCole Faust for (row = 0; row < (h>>1); row++) {
1057*c217d954SCole Faust stbi_uc *row0 = bytes + row*bytes_per_row;
1058*c217d954SCole Faust stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row;
1059*c217d954SCole Faust // swap row0 with row1
1060*c217d954SCole Faust size_t bytes_left = bytes_per_row;
1061*c217d954SCole Faust while (bytes_left) {
1062*c217d954SCole Faust size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp);
1063*c217d954SCole Faust memcpy(temp, row0, bytes_copy);
1064*c217d954SCole Faust memcpy(row0, row1, bytes_copy);
1065*c217d954SCole Faust memcpy(row1, temp, bytes_copy);
1066*c217d954SCole Faust row0 += bytes_copy;
1067*c217d954SCole Faust row1 += bytes_copy;
1068*c217d954SCole Faust bytes_left -= bytes_copy;
1069*c217d954SCole Faust }
1070*c217d954SCole Faust }
1071*c217d954SCole Faust }
1072*c217d954SCole Faust
stbi__vertical_flip_slices(void * image,int w,int h,int z,int bytes_per_pixel)1073*c217d954SCole Faust static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)
1074*c217d954SCole Faust {
1075*c217d954SCole Faust int slice;
1076*c217d954SCole Faust int slice_size = w * h * bytes_per_pixel;
1077*c217d954SCole Faust
1078*c217d954SCole Faust stbi_uc *bytes = (stbi_uc *)image;
1079*c217d954SCole Faust for (slice = 0; slice < z; ++slice) {
1080*c217d954SCole Faust stbi__vertical_flip(bytes, w, h, bytes_per_pixel);
1081*c217d954SCole Faust bytes += slice_size;
1082*c217d954SCole Faust }
1083*c217d954SCole Faust }
1084*c217d954SCole Faust
stbi__load_and_postprocess_8bit(stbi__context * s,int * x,int * y,int * comp,int req_comp)1085*c217d954SCole Faust static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1086*c217d954SCole Faust {
1087*c217d954SCole Faust stbi__result_info ri;
1088*c217d954SCole Faust void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);
1089*c217d954SCole Faust
1090*c217d954SCole Faust if (result == NULL)
1091*c217d954SCole Faust return NULL;
1092*c217d954SCole Faust
1093*c217d954SCole Faust if (ri.bits_per_channel != 8) {
1094*c217d954SCole Faust STBI_ASSERT(ri.bits_per_channel == 16);
1095*c217d954SCole Faust result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
1096*c217d954SCole Faust ri.bits_per_channel = 8;
1097*c217d954SCole Faust }
1098*c217d954SCole Faust
1099*c217d954SCole Faust // @TODO: move stbi__convert_format to here
1100*c217d954SCole Faust
1101*c217d954SCole Faust if (stbi__vertically_flip_on_load) {
1102*c217d954SCole Faust int channels = req_comp ? req_comp : *comp;
1103*c217d954SCole Faust stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc));
1104*c217d954SCole Faust }
1105*c217d954SCole Faust
1106*c217d954SCole Faust return (unsigned char *) result;
1107*c217d954SCole Faust }
1108*c217d954SCole Faust
stbi__load_and_postprocess_16bit(stbi__context * s,int * x,int * y,int * comp,int req_comp)1109*c217d954SCole Faust static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1110*c217d954SCole Faust {
1111*c217d954SCole Faust stbi__result_info ri;
1112*c217d954SCole Faust void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16);
1113*c217d954SCole Faust
1114*c217d954SCole Faust if (result == NULL)
1115*c217d954SCole Faust return NULL;
1116*c217d954SCole Faust
1117*c217d954SCole Faust if (ri.bits_per_channel != 16) {
1118*c217d954SCole Faust STBI_ASSERT(ri.bits_per_channel == 8);
1119*c217d954SCole Faust result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
1120*c217d954SCole Faust ri.bits_per_channel = 16;
1121*c217d954SCole Faust }
1122*c217d954SCole Faust
1123*c217d954SCole Faust // @TODO: move stbi__convert_format16 to here
1124*c217d954SCole Faust // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision
1125*c217d954SCole Faust
1126*c217d954SCole Faust if (stbi__vertically_flip_on_load) {
1127*c217d954SCole Faust int channels = req_comp ? req_comp : *comp;
1128*c217d954SCole Faust stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16));
1129*c217d954SCole Faust }
1130*c217d954SCole Faust
1131*c217d954SCole Faust return (stbi__uint16 *) result;
1132*c217d954SCole Faust }
1133*c217d954SCole Faust
1134*c217d954SCole Faust #if !defined(STBI_NO_HDR) || !defined(STBI_NO_LINEAR)
stbi__float_postprocess(float * result,int * x,int * y,int * comp,int req_comp)1135*c217d954SCole Faust static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)
1136*c217d954SCole Faust {
1137*c217d954SCole Faust if (stbi__vertically_flip_on_load && result != NULL) {
1138*c217d954SCole Faust int channels = req_comp ? req_comp : *comp;
1139*c217d954SCole Faust stbi__vertical_flip(result, *x, *y, channels * sizeof(float));
1140*c217d954SCole Faust }
1141*c217d954SCole Faust }
1142*c217d954SCole Faust #endif
1143*c217d954SCole Faust
1144*c217d954SCole Faust #ifndef STBI_NO_STDIO
1145*c217d954SCole Faust
stbi__fopen(char const * filename,char const * mode)1146*c217d954SCole Faust static FILE *stbi__fopen(char const *filename, char const *mode)
1147*c217d954SCole Faust {
1148*c217d954SCole Faust FILE *f;
1149*c217d954SCole Faust #if defined(_MSC_VER) && _MSC_VER >= 1400
1150*c217d954SCole Faust if (0 != fopen_s(&f, filename, mode))
1151*c217d954SCole Faust f=0;
1152*c217d954SCole Faust #else
1153*c217d954SCole Faust f = fopen(filename, mode);
1154*c217d954SCole Faust #endif
1155*c217d954SCole Faust return f;
1156*c217d954SCole Faust }
1157*c217d954SCole Faust
1158*c217d954SCole Faust
stbi_load(char const * filename,int * x,int * y,int * comp,int req_comp)1159*c217d954SCole Faust STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
1160*c217d954SCole Faust {
1161*c217d954SCole Faust FILE *f = stbi__fopen(filename, "rb");
1162*c217d954SCole Faust unsigned char *result;
1163*c217d954SCole Faust if (!f) return stbi__errpuc("can't fopen", "Unable to open file");
1164*c217d954SCole Faust result = stbi_load_from_file(f,x,y,comp,req_comp);
1165*c217d954SCole Faust fclose(f);
1166*c217d954SCole Faust return result;
1167*c217d954SCole Faust }
1168*c217d954SCole Faust
stbi_load_from_file(FILE * f,int * x,int * y,int * comp,int req_comp)1169*c217d954SCole Faust STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
1170*c217d954SCole Faust {
1171*c217d954SCole Faust unsigned char *result;
1172*c217d954SCole Faust stbi__context s;
1173*c217d954SCole Faust stbi__start_file(&s,f);
1174*c217d954SCole Faust result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1175*c217d954SCole Faust if (result) {
1176*c217d954SCole Faust // need to 'unget' all the characters in the IO buffer
1177*c217d954SCole Faust fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
1178*c217d954SCole Faust }
1179*c217d954SCole Faust return result;
1180*c217d954SCole Faust }
1181*c217d954SCole Faust
stbi_load_from_file_16(FILE * f,int * x,int * y,int * comp,int req_comp)1182*c217d954SCole Faust STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp)
1183*c217d954SCole Faust {
1184*c217d954SCole Faust stbi__uint16 *result;
1185*c217d954SCole Faust stbi__context s;
1186*c217d954SCole Faust stbi__start_file(&s,f);
1187*c217d954SCole Faust result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp);
1188*c217d954SCole Faust if (result) {
1189*c217d954SCole Faust // need to 'unget' all the characters in the IO buffer
1190*c217d954SCole Faust fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
1191*c217d954SCole Faust }
1192*c217d954SCole Faust return result;
1193*c217d954SCole Faust }
1194*c217d954SCole Faust
stbi_load_16(char const * filename,int * x,int * y,int * comp,int req_comp)1195*c217d954SCole Faust STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp)
1196*c217d954SCole Faust {
1197*c217d954SCole Faust FILE *f = stbi__fopen(filename, "rb");
1198*c217d954SCole Faust stbi__uint16 *result;
1199*c217d954SCole Faust if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file");
1200*c217d954SCole Faust result = stbi_load_from_file_16(f,x,y,comp,req_comp);
1201*c217d954SCole Faust fclose(f);
1202*c217d954SCole Faust return result;
1203*c217d954SCole Faust }
1204*c217d954SCole Faust
1205*c217d954SCole Faust
1206*c217d954SCole Faust #endif //!STBI_NO_STDIO
1207*c217d954SCole Faust
stbi_load_16_from_memory(stbi_uc const * buffer,int len,int * x,int * y,int * channels_in_file,int desired_channels)1208*c217d954SCole Faust 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)
1209*c217d954SCole Faust {
1210*c217d954SCole Faust stbi__context s;
1211*c217d954SCole Faust stbi__start_mem(&s,buffer,len);
1212*c217d954SCole Faust return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
1213*c217d954SCole Faust }
1214*c217d954SCole Faust
stbi_load_16_from_callbacks(stbi_io_callbacks const * clbk,void * user,int * x,int * y,int * channels_in_file,int desired_channels)1215*c217d954SCole Faust 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)
1216*c217d954SCole Faust {
1217*c217d954SCole Faust stbi__context s;
1218*c217d954SCole Faust stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user);
1219*c217d954SCole Faust return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
1220*c217d954SCole Faust }
1221*c217d954SCole Faust
stbi_load_from_memory(stbi_uc const * buffer,int len,int * x,int * y,int * comp,int req_comp)1222*c217d954SCole Faust STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
1223*c217d954SCole Faust {
1224*c217d954SCole Faust stbi__context s;
1225*c217d954SCole Faust stbi__start_mem(&s,buffer,len);
1226*c217d954SCole Faust return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1227*c217d954SCole Faust }
1228*c217d954SCole Faust
stbi_load_from_callbacks(stbi_io_callbacks const * clbk,void * user,int * x,int * y,int * comp,int req_comp)1229*c217d954SCole Faust STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
1230*c217d954SCole Faust {
1231*c217d954SCole Faust stbi__context s;
1232*c217d954SCole Faust stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1233*c217d954SCole Faust return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1234*c217d954SCole Faust }
1235*c217d954SCole Faust
1236*c217d954SCole Faust #ifndef STBI_NO_GIF
stbi_load_gif_from_memory(stbi_uc const * buffer,int len,int ** delays,int * x,int * y,int * z,int * comp,int req_comp)1237*c217d954SCole Faust 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)
1238*c217d954SCole Faust {
1239*c217d954SCole Faust unsigned char *result;
1240*c217d954SCole Faust stbi__context s;
1241*c217d954SCole Faust stbi__start_mem(&s,buffer,len);
1242*c217d954SCole Faust
1243*c217d954SCole Faust result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
1244*c217d954SCole Faust if (stbi__vertically_flip_on_load) {
1245*c217d954SCole Faust stbi__vertical_flip_slices( result, *x, *y, *z, *comp );
1246*c217d954SCole Faust }
1247*c217d954SCole Faust
1248*c217d954SCole Faust return result;
1249*c217d954SCole Faust }
1250*c217d954SCole Faust #endif
1251*c217d954SCole Faust
1252*c217d954SCole Faust #ifndef STBI_NO_LINEAR
stbi__loadf_main(stbi__context * s,int * x,int * y,int * comp,int req_comp)1253*c217d954SCole Faust static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1254*c217d954SCole Faust {
1255*c217d954SCole Faust unsigned char *data;
1256*c217d954SCole Faust #ifndef STBI_NO_HDR
1257*c217d954SCole Faust if (stbi__hdr_test(s)) {
1258*c217d954SCole Faust stbi__result_info ri;
1259*c217d954SCole Faust float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri);
1260*c217d954SCole Faust if (hdr_data)
1261*c217d954SCole Faust stbi__float_postprocess(hdr_data,x,y,comp,req_comp);
1262*c217d954SCole Faust return hdr_data;
1263*c217d954SCole Faust }
1264*c217d954SCole Faust #endif
1265*c217d954SCole Faust data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
1266*c217d954SCole Faust if (data)
1267*c217d954SCole Faust return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
1268*c217d954SCole Faust return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");
1269*c217d954SCole Faust }
1270*c217d954SCole Faust
stbi_loadf_from_memory(stbi_uc const * buffer,int len,int * x,int * y,int * comp,int req_comp)1271*c217d954SCole Faust STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
1272*c217d954SCole Faust {
1273*c217d954SCole Faust stbi__context s;
1274*c217d954SCole Faust stbi__start_mem(&s,buffer,len);
1275*c217d954SCole Faust return stbi__loadf_main(&s,x,y,comp,req_comp);
1276*c217d954SCole Faust }
1277*c217d954SCole Faust
stbi_loadf_from_callbacks(stbi_io_callbacks const * clbk,void * user,int * x,int * y,int * comp,int req_comp)1278*c217d954SCole Faust STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
1279*c217d954SCole Faust {
1280*c217d954SCole Faust stbi__context s;
1281*c217d954SCole Faust stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1282*c217d954SCole Faust return stbi__loadf_main(&s,x,y,comp,req_comp);
1283*c217d954SCole Faust }
1284*c217d954SCole Faust
1285*c217d954SCole Faust #ifndef STBI_NO_STDIO
stbi_loadf(char const * filename,int * x,int * y,int * comp,int req_comp)1286*c217d954SCole Faust STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
1287*c217d954SCole Faust {
1288*c217d954SCole Faust float *result;
1289*c217d954SCole Faust FILE *f = stbi__fopen(filename, "rb");
1290*c217d954SCole Faust if (!f) return stbi__errpf("can't fopen", "Unable to open file");
1291*c217d954SCole Faust result = stbi_loadf_from_file(f,x,y,comp,req_comp);
1292*c217d954SCole Faust fclose(f);
1293*c217d954SCole Faust return result;
1294*c217d954SCole Faust }
1295*c217d954SCole Faust
stbi_loadf_from_file(FILE * f,int * x,int * y,int * comp,int req_comp)1296*c217d954SCole Faust STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
1297*c217d954SCole Faust {
1298*c217d954SCole Faust stbi__context s;
1299*c217d954SCole Faust stbi__start_file(&s,f);
1300*c217d954SCole Faust return stbi__loadf_main(&s,x,y,comp,req_comp);
1301*c217d954SCole Faust }
1302*c217d954SCole Faust #endif // !STBI_NO_STDIO
1303*c217d954SCole Faust
1304*c217d954SCole Faust #endif // !STBI_NO_LINEAR
1305*c217d954SCole Faust
1306*c217d954SCole Faust // these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is
1307*c217d954SCole Faust // defined, for API simplicity; if STBI_NO_LINEAR is defined, it always
1308*c217d954SCole Faust // reports false!
1309*c217d954SCole Faust
stbi_is_hdr_from_memory(stbi_uc const * buffer,int len)1310*c217d954SCole Faust STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
1311*c217d954SCole Faust {
1312*c217d954SCole Faust #ifndef STBI_NO_HDR
1313*c217d954SCole Faust stbi__context s;
1314*c217d954SCole Faust stbi__start_mem(&s,buffer,len);
1315*c217d954SCole Faust return stbi__hdr_test(&s);
1316*c217d954SCole Faust #else
1317*c217d954SCole Faust STBI_NOTUSED(buffer);
1318*c217d954SCole Faust STBI_NOTUSED(len);
1319*c217d954SCole Faust return 0;
1320*c217d954SCole Faust #endif
1321*c217d954SCole Faust }
1322*c217d954SCole Faust
1323*c217d954SCole Faust #ifndef STBI_NO_STDIO
stbi_is_hdr(char const * filename)1324*c217d954SCole Faust STBIDEF int stbi_is_hdr (char const *filename)
1325*c217d954SCole Faust {
1326*c217d954SCole Faust FILE *f = stbi__fopen(filename, "rb");
1327*c217d954SCole Faust int result=0;
1328*c217d954SCole Faust if (f) {
1329*c217d954SCole Faust result = stbi_is_hdr_from_file(f);
1330*c217d954SCole Faust fclose(f);
1331*c217d954SCole Faust }
1332*c217d954SCole Faust return result;
1333*c217d954SCole Faust }
1334*c217d954SCole Faust
stbi_is_hdr_from_file(FILE * f)1335*c217d954SCole Faust STBIDEF int stbi_is_hdr_from_file(FILE *f)
1336*c217d954SCole Faust {
1337*c217d954SCole Faust #ifndef STBI_NO_HDR
1338*c217d954SCole Faust long pos = ftell(f);
1339*c217d954SCole Faust int res;
1340*c217d954SCole Faust stbi__context s;
1341*c217d954SCole Faust stbi__start_file(&s,f);
1342*c217d954SCole Faust res = stbi__hdr_test(&s);
1343*c217d954SCole Faust fseek(f, pos, SEEK_SET);
1344*c217d954SCole Faust return res;
1345*c217d954SCole Faust #else
1346*c217d954SCole Faust STBI_NOTUSED(f);
1347*c217d954SCole Faust return 0;
1348*c217d954SCole Faust #endif
1349*c217d954SCole Faust }
1350*c217d954SCole Faust #endif // !STBI_NO_STDIO
1351*c217d954SCole Faust
stbi_is_hdr_from_callbacks(stbi_io_callbacks const * clbk,void * user)1352*c217d954SCole Faust STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
1353*c217d954SCole Faust {
1354*c217d954SCole Faust #ifndef STBI_NO_HDR
1355*c217d954SCole Faust stbi__context s;
1356*c217d954SCole Faust stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1357*c217d954SCole Faust return stbi__hdr_test(&s);
1358*c217d954SCole Faust #else
1359*c217d954SCole Faust STBI_NOTUSED(clbk);
1360*c217d954SCole Faust STBI_NOTUSED(user);
1361*c217d954SCole Faust return 0;
1362*c217d954SCole Faust #endif
1363*c217d954SCole Faust }
1364*c217d954SCole Faust
1365*c217d954SCole Faust #ifndef STBI_NO_LINEAR
1366*c217d954SCole Faust static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;
1367*c217d954SCole Faust
stbi_ldr_to_hdr_gamma(float gamma)1368*c217d954SCole Faust STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; }
stbi_ldr_to_hdr_scale(float scale)1369*c217d954SCole Faust STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; }
1370*c217d954SCole Faust #endif
1371*c217d954SCole Faust
1372*c217d954SCole Faust static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;
1373*c217d954SCole Faust
stbi_hdr_to_ldr_gamma(float gamma)1374*c217d954SCole Faust STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; }
stbi_hdr_to_ldr_scale(float scale)1375*c217d954SCole Faust STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; }
1376*c217d954SCole Faust
1377*c217d954SCole Faust
1378*c217d954SCole Faust //////////////////////////////////////////////////////////////////////////////
1379*c217d954SCole Faust //
1380*c217d954SCole Faust // Common code used by all image loaders
1381*c217d954SCole Faust //
1382*c217d954SCole Faust
1383*c217d954SCole Faust enum
1384*c217d954SCole Faust {
1385*c217d954SCole Faust STBI__SCAN_load=0,
1386*c217d954SCole Faust STBI__SCAN_type,
1387*c217d954SCole Faust STBI__SCAN_header
1388*c217d954SCole Faust };
1389*c217d954SCole Faust
stbi__refill_buffer(stbi__context * s)1390*c217d954SCole Faust static void stbi__refill_buffer(stbi__context *s)
1391*c217d954SCole Faust {
1392*c217d954SCole Faust int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
1393*c217d954SCole Faust if (n == 0) {
1394*c217d954SCole Faust // at end of file, treat same as if from memory, but need to handle case
1395*c217d954SCole Faust // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file
1396*c217d954SCole Faust s->read_from_callbacks = 0;
1397*c217d954SCole Faust s->img_buffer = s->buffer_start;
1398*c217d954SCole Faust s->img_buffer_end = s->buffer_start+1;
1399*c217d954SCole Faust *s->img_buffer = 0;
1400*c217d954SCole Faust } else {
1401*c217d954SCole Faust s->img_buffer = s->buffer_start;
1402*c217d954SCole Faust s->img_buffer_end = s->buffer_start + n;
1403*c217d954SCole Faust }
1404*c217d954SCole Faust }
1405*c217d954SCole Faust
stbi__get8(stbi__context * s)1406*c217d954SCole Faust stbi_inline static stbi_uc stbi__get8(stbi__context *s)
1407*c217d954SCole Faust {
1408*c217d954SCole Faust if (s->img_buffer < s->img_buffer_end)
1409*c217d954SCole Faust return *s->img_buffer++;
1410*c217d954SCole Faust if (s->read_from_callbacks) {
1411*c217d954SCole Faust stbi__refill_buffer(s);
1412*c217d954SCole Faust return *s->img_buffer++;
1413*c217d954SCole Faust }
1414*c217d954SCole Faust return 0;
1415*c217d954SCole Faust }
1416*c217d954SCole Faust
stbi__at_eof(stbi__context * s)1417*c217d954SCole Faust stbi_inline static int stbi__at_eof(stbi__context *s)
1418*c217d954SCole Faust {
1419*c217d954SCole Faust if (s->io.read) {
1420*c217d954SCole Faust if (!(s->io.eof)(s->io_user_data)) return 0;
1421*c217d954SCole Faust // if feof() is true, check if buffer = end
1422*c217d954SCole Faust // special case: we've only got the special 0 character at the end
1423*c217d954SCole Faust if (s->read_from_callbacks == 0) return 1;
1424*c217d954SCole Faust }
1425*c217d954SCole Faust
1426*c217d954SCole Faust return s->img_buffer >= s->img_buffer_end;
1427*c217d954SCole Faust }
1428*c217d954SCole Faust
stbi__skip(stbi__context * s,int n)1429*c217d954SCole Faust static void stbi__skip(stbi__context *s, int n)
1430*c217d954SCole Faust {
1431*c217d954SCole Faust if (n < 0) {
1432*c217d954SCole Faust s->img_buffer = s->img_buffer_end;
1433*c217d954SCole Faust return;
1434*c217d954SCole Faust }
1435*c217d954SCole Faust if (s->io.read) {
1436*c217d954SCole Faust int blen = (int) (s->img_buffer_end - s->img_buffer);
1437*c217d954SCole Faust if (blen < n) {
1438*c217d954SCole Faust s->img_buffer = s->img_buffer_end;
1439*c217d954SCole Faust (s->io.skip)(s->io_user_data, n - blen);
1440*c217d954SCole Faust return;
1441*c217d954SCole Faust }
1442*c217d954SCole Faust }
1443*c217d954SCole Faust s->img_buffer += n;
1444*c217d954SCole Faust }
1445*c217d954SCole Faust
stbi__getn(stbi__context * s,stbi_uc * buffer,int n)1446*c217d954SCole Faust static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1447*c217d954SCole Faust {
1448*c217d954SCole Faust if (s->io.read) {
1449*c217d954SCole Faust int blen = (int) (s->img_buffer_end - s->img_buffer);
1450*c217d954SCole Faust if (blen < n) {
1451*c217d954SCole Faust int res, count;
1452*c217d954SCole Faust
1453*c217d954SCole Faust memcpy(buffer, s->img_buffer, blen);
1454*c217d954SCole Faust
1455*c217d954SCole Faust count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);
1456*c217d954SCole Faust res = (count == (n-blen));
1457*c217d954SCole Faust s->img_buffer = s->img_buffer_end;
1458*c217d954SCole Faust return res;
1459*c217d954SCole Faust }
1460*c217d954SCole Faust }
1461*c217d954SCole Faust
1462*c217d954SCole Faust if (s->img_buffer+n <= s->img_buffer_end) {
1463*c217d954SCole Faust memcpy(buffer, s->img_buffer, n);
1464*c217d954SCole Faust s->img_buffer += n;
1465*c217d954SCole Faust return 1;
1466*c217d954SCole Faust } else
1467*c217d954SCole Faust return 0;
1468*c217d954SCole Faust }
1469*c217d954SCole Faust
stbi__get16be(stbi__context * s)1470*c217d954SCole Faust static int stbi__get16be(stbi__context *s)
1471*c217d954SCole Faust {
1472*c217d954SCole Faust int z = stbi__get8(s);
1473*c217d954SCole Faust return (z << 8) + stbi__get8(s);
1474*c217d954SCole Faust }
1475*c217d954SCole Faust
stbi__get32be(stbi__context * s)1476*c217d954SCole Faust static stbi__uint32 stbi__get32be(stbi__context *s)
1477*c217d954SCole Faust {
1478*c217d954SCole Faust stbi__uint32 z = stbi__get16be(s);
1479*c217d954SCole Faust return (z << 16) + stbi__get16be(s);
1480*c217d954SCole Faust }
1481*c217d954SCole Faust
1482*c217d954SCole Faust #if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)
1483*c217d954SCole Faust // nothing
1484*c217d954SCole Faust #else
stbi__get16le(stbi__context * s)1485*c217d954SCole Faust static int stbi__get16le(stbi__context *s)
1486*c217d954SCole Faust {
1487*c217d954SCole Faust int z = stbi__get8(s);
1488*c217d954SCole Faust return z + (stbi__get8(s) << 8);
1489*c217d954SCole Faust }
1490*c217d954SCole Faust #endif
1491*c217d954SCole Faust
1492*c217d954SCole Faust #ifndef STBI_NO_BMP
stbi__get32le(stbi__context * s)1493*c217d954SCole Faust static stbi__uint32 stbi__get32le(stbi__context *s)
1494*c217d954SCole Faust {
1495*c217d954SCole Faust stbi__uint32 z = stbi__get16le(s);
1496*c217d954SCole Faust return z + (stbi__get16le(s) << 16);
1497*c217d954SCole Faust }
1498*c217d954SCole Faust #endif
1499*c217d954SCole Faust
1500*c217d954SCole Faust #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
1501*c217d954SCole Faust
1502*c217d954SCole Faust
1503*c217d954SCole Faust //////////////////////////////////////////////////////////////////////////////
1504*c217d954SCole Faust //
1505*c217d954SCole Faust // generic converter from built-in img_n to req_comp
1506*c217d954SCole Faust // individual types do this automatically as much as possible (e.g. jpeg
1507*c217d954SCole Faust // does all cases internally since it needs to colorspace convert anyway,
1508*c217d954SCole Faust // and it never has alpha, so very few cases ). png can automatically
1509*c217d954SCole Faust // interleave an alpha=255 channel, but falls back to this for other cases
1510*c217d954SCole Faust //
1511*c217d954SCole Faust // assume data buffer is malloced, so malloc a new one and free that one
1512*c217d954SCole Faust // only failure mode is malloc failing
1513*c217d954SCole Faust
stbi__compute_y(int r,int g,int b)1514*c217d954SCole Faust static stbi_uc stbi__compute_y(int r, int g, int b)
1515*c217d954SCole Faust {
1516*c217d954SCole Faust return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);
1517*c217d954SCole Faust }
1518*c217d954SCole Faust
stbi__convert_format(unsigned char * data,int img_n,int req_comp,unsigned int x,unsigned int y)1519*c217d954SCole Faust static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
1520*c217d954SCole Faust {
1521*c217d954SCole Faust int i,j;
1522*c217d954SCole Faust unsigned char *good;
1523*c217d954SCole Faust
1524*c217d954SCole Faust if (req_comp == img_n) return data;
1525*c217d954SCole Faust STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1526*c217d954SCole Faust
1527*c217d954SCole Faust good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);
1528*c217d954SCole Faust if (good == NULL) {
1529*c217d954SCole Faust STBI_FREE(data);
1530*c217d954SCole Faust return stbi__errpuc("outofmem", "Out of memory");
1531*c217d954SCole Faust }
1532*c217d954SCole Faust
1533*c217d954SCole Faust for (j=0; j < (int) y; ++j) {
1534*c217d954SCole Faust unsigned char *src = data + j * x * img_n ;
1535*c217d954SCole Faust unsigned char *dest = good + j * x * req_comp;
1536*c217d954SCole Faust
1537*c217d954SCole Faust #define STBI__COMBO(a,b) ((a)*8+(b))
1538*c217d954SCole Faust #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1539*c217d954SCole Faust // convert source image with img_n components to one with req_comp components;
1540*c217d954SCole Faust // avoid switch per pixel, so use switch per scanline and massive macros
1541*c217d954SCole Faust switch (STBI__COMBO(img_n, req_comp)) {
1542*c217d954SCole Faust STBI__CASE(1,2) { dest[0]=src[0], dest[1]=255; } break;
1543*c217d954SCole Faust STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1544*c217d954SCole Faust STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; } break;
1545*c217d954SCole Faust STBI__CASE(2,1) { dest[0]=src[0]; } break;
1546*c217d954SCole Faust STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1547*c217d954SCole Faust STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
1548*c217d954SCole Faust STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; } break;
1549*c217d954SCole Faust STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
1550*c217d954SCole Faust STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; } break;
1551*c217d954SCole Faust STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
1552*c217d954SCole Faust STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; } break;
1553*c217d954SCole Faust STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
1554*c217d954SCole Faust default: STBI_ASSERT(0);
1555*c217d954SCole Faust }
1556*c217d954SCole Faust #undef STBI__CASE
1557*c217d954SCole Faust }
1558*c217d954SCole Faust
1559*c217d954SCole Faust STBI_FREE(data);
1560*c217d954SCole Faust return good;
1561*c217d954SCole Faust }
1562*c217d954SCole Faust
stbi__compute_y_16(int r,int g,int b)1563*c217d954SCole Faust static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
1564*c217d954SCole Faust {
1565*c217d954SCole Faust return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);
1566*c217d954SCole Faust }
1567*c217d954SCole Faust
stbi__convert_format16(stbi__uint16 * data,int img_n,int req_comp,unsigned int x,unsigned int y)1568*c217d954SCole Faust static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
1569*c217d954SCole Faust {
1570*c217d954SCole Faust int i,j;
1571*c217d954SCole Faust stbi__uint16 *good;
1572*c217d954SCole Faust
1573*c217d954SCole Faust if (req_comp == img_n) return data;
1574*c217d954SCole Faust STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1575*c217d954SCole Faust
1576*c217d954SCole Faust good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
1577*c217d954SCole Faust if (good == NULL) {
1578*c217d954SCole Faust STBI_FREE(data);
1579*c217d954SCole Faust return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
1580*c217d954SCole Faust }
1581*c217d954SCole Faust
1582*c217d954SCole Faust for (j=0; j < (int) y; ++j) {
1583*c217d954SCole Faust stbi__uint16 *src = data + j * x * img_n ;
1584*c217d954SCole Faust stbi__uint16 *dest = good + j * x * req_comp;
1585*c217d954SCole Faust
1586*c217d954SCole Faust #define STBI__COMBO(a,b) ((a)*8+(b))
1587*c217d954SCole Faust #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1588*c217d954SCole Faust // convert source image with img_n components to one with req_comp components;
1589*c217d954SCole Faust // avoid switch per pixel, so use switch per scanline and massive macros
1590*c217d954SCole Faust switch (STBI__COMBO(img_n, req_comp)) {
1591*c217d954SCole Faust STBI__CASE(1,2) { dest[0]=src[0], dest[1]=0xffff; } break;
1592*c217d954SCole Faust STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1593*c217d954SCole Faust STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=0xffff; } break;
1594*c217d954SCole Faust STBI__CASE(2,1) { dest[0]=src[0]; } break;
1595*c217d954SCole Faust STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1596*c217d954SCole Faust STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
1597*c217d954SCole Faust STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=0xffff; } break;
1598*c217d954SCole Faust STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
1599*c217d954SCole Faust STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = 0xffff; } break;
1600*c217d954SCole Faust STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
1601*c217d954SCole Faust STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = src[3]; } break;
1602*c217d954SCole Faust STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
1603*c217d954SCole Faust default: STBI_ASSERT(0);
1604*c217d954SCole Faust }
1605*c217d954SCole Faust #undef STBI__CASE
1606*c217d954SCole Faust }
1607*c217d954SCole Faust
1608*c217d954SCole Faust STBI_FREE(data);
1609*c217d954SCole Faust return good;
1610*c217d954SCole Faust }
1611*c217d954SCole Faust
1612*c217d954SCole Faust #ifndef STBI_NO_LINEAR
stbi__ldr_to_hdr(stbi_uc * data,int x,int y,int comp)1613*c217d954SCole Faust static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
1614*c217d954SCole Faust {
1615*c217d954SCole Faust int i,k,n;
1616*c217d954SCole Faust float *output;
1617*c217d954SCole Faust if (!data) return NULL;
1618*c217d954SCole Faust output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0);
1619*c217d954SCole Faust if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); }
1620*c217d954SCole Faust // compute number of non-alpha components
1621*c217d954SCole Faust if (comp & 1) n = comp; else n = comp-1;
1622*c217d954SCole Faust for (i=0; i < x*y; ++i) {
1623*c217d954SCole Faust for (k=0; k < n; ++k) {
1624*c217d954SCole Faust output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
1625*c217d954SCole Faust }
1626*c217d954SCole Faust if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
1627*c217d954SCole Faust }
1628*c217d954SCole Faust STBI_FREE(data);
1629*c217d954SCole Faust return output;
1630*c217d954SCole Faust }
1631*c217d954SCole Faust #endif
1632*c217d954SCole Faust
1633*c217d954SCole Faust #ifndef STBI_NO_HDR
1634*c217d954SCole Faust #define stbi__float2int(x) ((int) (x))
stbi__hdr_to_ldr(float * data,int x,int y,int comp)1635*c217d954SCole Faust static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)
1636*c217d954SCole Faust {
1637*c217d954SCole Faust int i,k,n;
1638*c217d954SCole Faust stbi_uc *output;
1639*c217d954SCole Faust if (!data) return NULL;
1640*c217d954SCole Faust output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0);
1641*c217d954SCole Faust if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); }
1642*c217d954SCole Faust // compute number of non-alpha components
1643*c217d954SCole Faust if (comp & 1) n = comp; else n = comp-1;
1644*c217d954SCole Faust for (i=0; i < x*y; ++i) {
1645*c217d954SCole Faust for (k=0; k < n; ++k) {
1646*c217d954SCole Faust float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;
1647*c217d954SCole Faust if (z < 0) z = 0;
1648*c217d954SCole Faust if (z > 255) z = 255;
1649*c217d954SCole Faust output[i*comp + k] = (stbi_uc) stbi__float2int(z);
1650*c217d954SCole Faust }
1651*c217d954SCole Faust if (k < comp) {
1652*c217d954SCole Faust float z = data[i*comp+k] * 255 + 0.5f;
1653*c217d954SCole Faust if (z < 0) z = 0;
1654*c217d954SCole Faust if (z > 255) z = 255;
1655*c217d954SCole Faust output[i*comp + k] = (stbi_uc) stbi__float2int(z);
1656*c217d954SCole Faust }
1657*c217d954SCole Faust }
1658*c217d954SCole Faust STBI_FREE(data);
1659*c217d954SCole Faust return output;
1660*c217d954SCole Faust }
1661*c217d954SCole Faust #endif
1662*c217d954SCole Faust
1663*c217d954SCole Faust //////////////////////////////////////////////////////////////////////////////
1664*c217d954SCole Faust //
1665*c217d954SCole Faust // "baseline" JPEG/JFIF decoder
1666*c217d954SCole Faust //
1667*c217d954SCole Faust // simple implementation
1668*c217d954SCole Faust // - doesn't support delayed output of y-dimension
1669*c217d954SCole Faust // - simple interface (only one output format: 8-bit interleaved RGB)
1670*c217d954SCole Faust // - doesn't try to recover corrupt jpegs
1671*c217d954SCole Faust // - doesn't allow partial loading, loading multiple at once
1672*c217d954SCole Faust // - still fast on x86 (copying globals into locals doesn't help x86)
1673*c217d954SCole Faust // - allocates lots of intermediate memory (full size of all components)
1674*c217d954SCole Faust // - non-interleaved case requires this anyway
1675*c217d954SCole Faust // - allows good upsampling (see next)
1676*c217d954SCole Faust // high-quality
1677*c217d954SCole Faust // - upsampled channels are bilinearly interpolated, even across blocks
1678*c217d954SCole Faust // - quality integer IDCT derived from IJG's 'slow'
1679*c217d954SCole Faust // performance
1680*c217d954SCole Faust // - fast huffman; reasonable integer IDCT
1681*c217d954SCole Faust // - some SIMD kernels for common paths on targets with SSE2/NEON
1682*c217d954SCole Faust // - uses a lot of intermediate memory, could cache poorly
1683*c217d954SCole Faust
1684*c217d954SCole Faust #ifndef STBI_NO_JPEG
1685*c217d954SCole Faust
1686*c217d954SCole Faust // huffman decoding acceleration
1687*c217d954SCole Faust #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
1688*c217d954SCole Faust
1689*c217d954SCole Faust typedef struct
1690*c217d954SCole Faust {
1691*c217d954SCole Faust stbi_uc fast[1 << FAST_BITS];
1692*c217d954SCole Faust // weirdly, repacking this into AoS is a 10% speed loss, instead of a win
1693*c217d954SCole Faust stbi__uint16 code[256];
1694*c217d954SCole Faust stbi_uc values[256];
1695*c217d954SCole Faust stbi_uc size[257];
1696*c217d954SCole Faust unsigned int maxcode[18];
1697*c217d954SCole Faust int delta[17]; // old 'firstsymbol' - old 'firstcode'
1698*c217d954SCole Faust } stbi__huffman;
1699*c217d954SCole Faust
1700*c217d954SCole Faust typedef struct
1701*c217d954SCole Faust {
1702*c217d954SCole Faust stbi__context *s;
1703*c217d954SCole Faust stbi__huffman huff_dc[4];
1704*c217d954SCole Faust stbi__huffman huff_ac[4];
1705*c217d954SCole Faust stbi__uint16 dequant[4][64];
1706*c217d954SCole Faust stbi__int16 fast_ac[4][1 << FAST_BITS];
1707*c217d954SCole Faust
1708*c217d954SCole Faust // sizes for components, interleaved MCUs
1709*c217d954SCole Faust int img_h_max, img_v_max;
1710*c217d954SCole Faust int img_mcu_x, img_mcu_y;
1711*c217d954SCole Faust int img_mcu_w, img_mcu_h;
1712*c217d954SCole Faust
1713*c217d954SCole Faust // definition of jpeg image component
1714*c217d954SCole Faust struct
1715*c217d954SCole Faust {
1716*c217d954SCole Faust int id;
1717*c217d954SCole Faust int h,v;
1718*c217d954SCole Faust int tq;
1719*c217d954SCole Faust int hd,ha;
1720*c217d954SCole Faust int dc_pred;
1721*c217d954SCole Faust
1722*c217d954SCole Faust int x,y,w2,h2;
1723*c217d954SCole Faust stbi_uc *data;
1724*c217d954SCole Faust void *raw_data, *raw_coeff;
1725*c217d954SCole Faust stbi_uc *linebuf;
1726*c217d954SCole Faust short *coeff; // progressive only
1727*c217d954SCole Faust int coeff_w, coeff_h; // number of 8x8 coefficient blocks
1728*c217d954SCole Faust } img_comp[4];
1729*c217d954SCole Faust
1730*c217d954SCole Faust stbi__uint32 code_buffer; // jpeg entropy-coded buffer
1731*c217d954SCole Faust int code_bits; // number of valid bits
1732*c217d954SCole Faust unsigned char marker; // marker seen while filling entropy buffer
1733*c217d954SCole Faust int nomore; // flag if we saw a marker so must stop
1734*c217d954SCole Faust
1735*c217d954SCole Faust int progressive;
1736*c217d954SCole Faust int spec_start;
1737*c217d954SCole Faust int spec_end;
1738*c217d954SCole Faust int succ_high;
1739*c217d954SCole Faust int succ_low;
1740*c217d954SCole Faust int eob_run;
1741*c217d954SCole Faust int jfif;
1742*c217d954SCole Faust int app14_color_transform; // Adobe APP14 tag
1743*c217d954SCole Faust int rgb;
1744*c217d954SCole Faust
1745*c217d954SCole Faust int scan_n, order[4];
1746*c217d954SCole Faust int restart_interval, todo;
1747*c217d954SCole Faust
1748*c217d954SCole Faust // kernels
1749*c217d954SCole Faust void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]);
1750*c217d954SCole Faust void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step);
1751*c217d954SCole Faust stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs);
1752*c217d954SCole Faust } stbi__jpeg;
1753*c217d954SCole Faust
stbi__build_huffman(stbi__huffman * h,int * count)1754*c217d954SCole Faust static int stbi__build_huffman(stbi__huffman *h, int *count)
1755*c217d954SCole Faust {
1756*c217d954SCole Faust int i,j,k=0;
1757*c217d954SCole Faust unsigned int code;
1758*c217d954SCole Faust // build size list for each symbol (from JPEG spec)
1759*c217d954SCole Faust for (i=0; i < 16; ++i)
1760*c217d954SCole Faust for (j=0; j < count[i]; ++j)
1761*c217d954SCole Faust h->size[k++] = (stbi_uc) (i+1);
1762*c217d954SCole Faust h->size[k] = 0;
1763*c217d954SCole Faust
1764*c217d954SCole Faust // compute actual symbols (from jpeg spec)
1765*c217d954SCole Faust code = 0;
1766*c217d954SCole Faust k = 0;
1767*c217d954SCole Faust for(j=1; j <= 16; ++j) {
1768*c217d954SCole Faust // compute delta to add to code to compute symbol id
1769*c217d954SCole Faust h->delta[j] = k - code;
1770*c217d954SCole Faust if (h->size[k] == j) {
1771*c217d954SCole Faust while (h->size[k] == j)
1772*c217d954SCole Faust h->code[k++] = (stbi__uint16) (code++);
1773*c217d954SCole Faust if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG");
1774*c217d954SCole Faust }
1775*c217d954SCole Faust // compute largest code + 1 for this size, preshifted as needed later
1776*c217d954SCole Faust h->maxcode[j] = code << (16-j);
1777*c217d954SCole Faust code <<= 1;
1778*c217d954SCole Faust }
1779*c217d954SCole Faust h->maxcode[j] = 0xffffffff;
1780*c217d954SCole Faust
1781*c217d954SCole Faust // build non-spec acceleration table; 255 is flag for not-accelerated
1782*c217d954SCole Faust memset(h->fast, 255, 1 << FAST_BITS);
1783*c217d954SCole Faust for (i=0; i < k; ++i) {
1784*c217d954SCole Faust int s = h->size[i];
1785*c217d954SCole Faust if (s <= FAST_BITS) {
1786*c217d954SCole Faust int c = h->code[i] << (FAST_BITS-s);
1787*c217d954SCole Faust int m = 1 << (FAST_BITS-s);
1788*c217d954SCole Faust for (j=0; j < m; ++j) {
1789*c217d954SCole Faust h->fast[c+j] = (stbi_uc) i;
1790*c217d954SCole Faust }
1791*c217d954SCole Faust }
1792*c217d954SCole Faust }
1793*c217d954SCole Faust return 1;
1794*c217d954SCole Faust }
1795*c217d954SCole Faust
1796*c217d954SCole Faust // build a table that decodes both magnitude and value of small ACs in
1797*c217d954SCole Faust // one go.
stbi__build_fast_ac(stbi__int16 * fast_ac,stbi__huffman * h)1798*c217d954SCole Faust static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h)
1799*c217d954SCole Faust {
1800*c217d954SCole Faust int i;
1801*c217d954SCole Faust for (i=0; i < (1 << FAST_BITS); ++i) {
1802*c217d954SCole Faust stbi_uc fast = h->fast[i];
1803*c217d954SCole Faust fast_ac[i] = 0;
1804*c217d954SCole Faust if (fast < 255) {
1805*c217d954SCole Faust int rs = h->values[fast];
1806*c217d954SCole Faust int run = (rs >> 4) & 15;
1807*c217d954SCole Faust int magbits = rs & 15;
1808*c217d954SCole Faust int len = h->size[fast];
1809*c217d954SCole Faust
1810*c217d954SCole Faust if (magbits && len + magbits <= FAST_BITS) {
1811*c217d954SCole Faust // magnitude code followed by receive_extend code
1812*c217d954SCole Faust int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits);
1813*c217d954SCole Faust int m = 1 << (magbits - 1);
1814*c217d954SCole Faust if (k < m) k += (~0U << magbits) + 1;
1815*c217d954SCole Faust // if the result is small enough, we can fit it in fast_ac table
1816*c217d954SCole Faust if (k >= -128 && k <= 127)
1817*c217d954SCole Faust fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits));
1818*c217d954SCole Faust }
1819*c217d954SCole Faust }
1820*c217d954SCole Faust }
1821*c217d954SCole Faust }
1822*c217d954SCole Faust
stbi__grow_buffer_unsafe(stbi__jpeg * j)1823*c217d954SCole Faust static void stbi__grow_buffer_unsafe(stbi__jpeg *j)
1824*c217d954SCole Faust {
1825*c217d954SCole Faust do {
1826*c217d954SCole Faust unsigned int b = j->nomore ? 0 : stbi__get8(j->s);
1827*c217d954SCole Faust if (b == 0xff) {
1828*c217d954SCole Faust int c = stbi__get8(j->s);
1829*c217d954SCole Faust while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes
1830*c217d954SCole Faust if (c != 0) {
1831*c217d954SCole Faust j->marker = (unsigned char) c;
1832*c217d954SCole Faust j->nomore = 1;
1833*c217d954SCole Faust return;
1834*c217d954SCole Faust }
1835*c217d954SCole Faust }
1836*c217d954SCole Faust j->code_buffer |= b << (24 - j->code_bits);
1837*c217d954SCole Faust j->code_bits += 8;
1838*c217d954SCole Faust } while (j->code_bits <= 24);
1839*c217d954SCole Faust }
1840*c217d954SCole Faust
1841*c217d954SCole Faust // (1 << n) - 1
1842*c217d954SCole Faust static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
1843*c217d954SCole Faust
1844*c217d954SCole Faust // decode a jpeg huffman value from the bitstream
stbi__jpeg_huff_decode(stbi__jpeg * j,stbi__huffman * h)1845*c217d954SCole Faust stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
1846*c217d954SCole Faust {
1847*c217d954SCole Faust unsigned int temp;
1848*c217d954SCole Faust int c,k;
1849*c217d954SCole Faust
1850*c217d954SCole Faust if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1851*c217d954SCole Faust
1852*c217d954SCole Faust // look at the top FAST_BITS and determine what symbol ID it is,
1853*c217d954SCole Faust // if the code is <= FAST_BITS
1854*c217d954SCole Faust c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
1855*c217d954SCole Faust k = h->fast[c];
1856*c217d954SCole Faust if (k < 255) {
1857*c217d954SCole Faust int s = h->size[k];
1858*c217d954SCole Faust if (s > j->code_bits)
1859*c217d954SCole Faust return -1;
1860*c217d954SCole Faust j->code_buffer <<= s;
1861*c217d954SCole Faust j->code_bits -= s;
1862*c217d954SCole Faust return h->values[k];
1863*c217d954SCole Faust }
1864*c217d954SCole Faust
1865*c217d954SCole Faust // naive test is to shift the code_buffer down so k bits are
1866*c217d954SCole Faust // valid, then test against maxcode. To speed this up, we've
1867*c217d954SCole Faust // preshifted maxcode left so that it has (16-k) 0s at the
1868*c217d954SCole Faust // end; in other words, regardless of the number of bits, it
1869*c217d954SCole Faust // wants to be compared against something shifted to have 16;
1870*c217d954SCole Faust // that way we don't need to shift inside the loop.
1871*c217d954SCole Faust temp = j->code_buffer >> 16;
1872*c217d954SCole Faust for (k=FAST_BITS+1 ; ; ++k)
1873*c217d954SCole Faust if (temp < h->maxcode[k])
1874*c217d954SCole Faust break;
1875*c217d954SCole Faust if (k == 17) {
1876*c217d954SCole Faust // error! code not found
1877*c217d954SCole Faust j->code_bits -= 16;
1878*c217d954SCole Faust return -1;
1879*c217d954SCole Faust }
1880*c217d954SCole Faust
1881*c217d954SCole Faust if (k > j->code_bits)
1882*c217d954SCole Faust return -1;
1883*c217d954SCole Faust
1884*c217d954SCole Faust // convert the huffman code to the symbol id
1885*c217d954SCole Faust c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
1886*c217d954SCole Faust STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
1887*c217d954SCole Faust
1888*c217d954SCole Faust // convert the id to a symbol
1889*c217d954SCole Faust j->code_bits -= k;
1890*c217d954SCole Faust j->code_buffer <<= k;
1891*c217d954SCole Faust return h->values[c];
1892*c217d954SCole Faust }
1893*c217d954SCole Faust
1894*c217d954SCole Faust // bias[n] = (-1<<n) + 1
1895*c217d954SCole Faust static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};
1896*c217d954SCole Faust
1897*c217d954SCole Faust // combined JPEG 'receive' and JPEG 'extend', since baseline
1898*c217d954SCole Faust // always extends everything it receives.
stbi__extend_receive(stbi__jpeg * j,int n)1899*c217d954SCole Faust stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)
1900*c217d954SCole Faust {
1901*c217d954SCole Faust unsigned int k;
1902*c217d954SCole Faust int sgn;
1903*c217d954SCole Faust if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
1904*c217d954SCole Faust
1905*c217d954SCole Faust sgn = (stbi__int32)j->code_buffer >> 31; // sign bit is always in MSB
1906*c217d954SCole Faust k = stbi_lrot(j->code_buffer, n);
1907*c217d954SCole Faust STBI_ASSERT(n >= 0 && n < (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask)));
1908*c217d954SCole Faust j->code_buffer = k & ~stbi__bmask[n];
1909*c217d954SCole Faust k &= stbi__bmask[n];
1910*c217d954SCole Faust j->code_bits -= n;
1911*c217d954SCole Faust return k + (stbi__jbias[n] & ~sgn);
1912*c217d954SCole Faust }
1913*c217d954SCole Faust
1914*c217d954SCole Faust // get some unsigned bits
stbi__jpeg_get_bits(stbi__jpeg * j,int n)1915*c217d954SCole Faust stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n)
1916*c217d954SCole Faust {
1917*c217d954SCole Faust unsigned int k;
1918*c217d954SCole Faust if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
1919*c217d954SCole Faust k = stbi_lrot(j->code_buffer, n);
1920*c217d954SCole Faust j->code_buffer = k & ~stbi__bmask[n];
1921*c217d954SCole Faust k &= stbi__bmask[n];
1922*c217d954SCole Faust j->code_bits -= n;
1923*c217d954SCole Faust return k;
1924*c217d954SCole Faust }
1925*c217d954SCole Faust
stbi__jpeg_get_bit(stbi__jpeg * j)1926*c217d954SCole Faust stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j)
1927*c217d954SCole Faust {
1928*c217d954SCole Faust unsigned int k;
1929*c217d954SCole Faust if (j->code_bits < 1) stbi__grow_buffer_unsafe(j);
1930*c217d954SCole Faust k = j->code_buffer;
1931*c217d954SCole Faust j->code_buffer <<= 1;
1932*c217d954SCole Faust --j->code_bits;
1933*c217d954SCole Faust return k & 0x80000000;
1934*c217d954SCole Faust }
1935*c217d954SCole Faust
1936*c217d954SCole Faust // given a value that's at position X in the zigzag stream,
1937*c217d954SCole Faust // where does it appear in the 8x8 matrix coded as row-major?
1938*c217d954SCole Faust static const stbi_uc stbi__jpeg_dezigzag[64+15] =
1939*c217d954SCole Faust {
1940*c217d954SCole Faust 0, 1, 8, 16, 9, 2, 3, 10,
1941*c217d954SCole Faust 17, 24, 32, 25, 18, 11, 4, 5,
1942*c217d954SCole Faust 12, 19, 26, 33, 40, 48, 41, 34,
1943*c217d954SCole Faust 27, 20, 13, 6, 7, 14, 21, 28,
1944*c217d954SCole Faust 35, 42, 49, 56, 57, 50, 43, 36,
1945*c217d954SCole Faust 29, 22, 15, 23, 30, 37, 44, 51,
1946*c217d954SCole Faust 58, 59, 52, 45, 38, 31, 39, 46,
1947*c217d954SCole Faust 53, 60, 61, 54, 47, 55, 62, 63,
1948*c217d954SCole Faust // let corrupt input sample past end
1949*c217d954SCole Faust 63, 63, 63, 63, 63, 63, 63, 63,
1950*c217d954SCole Faust 63, 63, 63, 63, 63, 63, 63
1951*c217d954SCole Faust };
1952*c217d954SCole Faust
1953*c217d954SCole Faust // decode one 64-entry block--
stbi__jpeg_decode_block(stbi__jpeg * j,short data[64],stbi__huffman * hdc,stbi__huffman * hac,stbi__int16 * fac,int b,stbi__uint16 * dequant)1954*c217d954SCole Faust 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)
1955*c217d954SCole Faust {
1956*c217d954SCole Faust int diff,dc,k;
1957*c217d954SCole Faust int t;
1958*c217d954SCole Faust
1959*c217d954SCole Faust if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1960*c217d954SCole Faust t = stbi__jpeg_huff_decode(j, hdc);
1961*c217d954SCole Faust if (t < 0) return stbi__err("bad huffman code","Corrupt JPEG");
1962*c217d954SCole Faust
1963*c217d954SCole Faust // 0 all the ac values now so we can do it 32-bits at a time
1964*c217d954SCole Faust memset(data,0,64*sizeof(data[0]));
1965*c217d954SCole Faust
1966*c217d954SCole Faust diff = t ? stbi__extend_receive(j, t) : 0;
1967*c217d954SCole Faust dc = j->img_comp[b].dc_pred + diff;
1968*c217d954SCole Faust j->img_comp[b].dc_pred = dc;
1969*c217d954SCole Faust data[0] = (short) (dc * dequant[0]);
1970*c217d954SCole Faust
1971*c217d954SCole Faust // decode AC components, see JPEG spec
1972*c217d954SCole Faust k = 1;
1973*c217d954SCole Faust do {
1974*c217d954SCole Faust unsigned int zig;
1975*c217d954SCole Faust int c,r,s;
1976*c217d954SCole Faust if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1977*c217d954SCole Faust c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
1978*c217d954SCole Faust r = fac[c];
1979*c217d954SCole Faust if (r) { // fast-AC path
1980*c217d954SCole Faust k += (r >> 4) & 15; // run
1981*c217d954SCole Faust s = r & 15; // combined length
1982*c217d954SCole Faust j->code_buffer <<= s;
1983*c217d954SCole Faust j->code_bits -= s;
1984*c217d954SCole Faust // decode into unzigzag'd location
1985*c217d954SCole Faust zig = stbi__jpeg_dezigzag[k++];
1986*c217d954SCole Faust data[zig] = (short) ((r >> 8) * dequant[zig]);
1987*c217d954SCole Faust } else {
1988*c217d954SCole Faust int rs = stbi__jpeg_huff_decode(j, hac);
1989*c217d954SCole Faust if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
1990*c217d954SCole Faust s = rs & 15;
1991*c217d954SCole Faust r = rs >> 4;
1992*c217d954SCole Faust if (s == 0) {
1993*c217d954SCole Faust if (rs != 0xf0) break; // end block
1994*c217d954SCole Faust k += 16;
1995*c217d954SCole Faust } else {
1996*c217d954SCole Faust k += r;
1997*c217d954SCole Faust // decode into unzigzag'd location
1998*c217d954SCole Faust zig = stbi__jpeg_dezigzag[k++];
1999*c217d954SCole Faust data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]);
2000*c217d954SCole Faust }
2001*c217d954SCole Faust }
2002*c217d954SCole Faust } while (k < 64);
2003*c217d954SCole Faust return 1;
2004*c217d954SCole Faust }
2005*c217d954SCole Faust
stbi__jpeg_decode_block_prog_dc(stbi__jpeg * j,short data[64],stbi__huffman * hdc,int b)2006*c217d954SCole Faust static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b)
2007*c217d954SCole Faust {
2008*c217d954SCole Faust int diff,dc;
2009*c217d954SCole Faust int t;
2010*c217d954SCole Faust if (j->spec_end != 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2011*c217d954SCole Faust
2012*c217d954SCole Faust if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
2013*c217d954SCole Faust
2014*c217d954SCole Faust if (j->succ_high == 0) {
2015*c217d954SCole Faust // first scan for DC coefficient, must be first
2016*c217d954SCole Faust memset(data,0,64*sizeof(data[0])); // 0 all the ac values now
2017*c217d954SCole Faust t = stbi__jpeg_huff_decode(j, hdc);
2018*c217d954SCole Faust diff = t ? stbi__extend_receive(j, t) : 0;
2019*c217d954SCole Faust
2020*c217d954SCole Faust dc = j->img_comp[b].dc_pred + diff;
2021*c217d954SCole Faust j->img_comp[b].dc_pred = dc;
2022*c217d954SCole Faust data[0] = (short) (dc << j->succ_low);
2023*c217d954SCole Faust } else {
2024*c217d954SCole Faust // refinement scan for DC coefficient
2025*c217d954SCole Faust if (stbi__jpeg_get_bit(j))
2026*c217d954SCole Faust data[0] += (short) (1 << j->succ_low);
2027*c217d954SCole Faust }
2028*c217d954SCole Faust return 1;
2029*c217d954SCole Faust }
2030*c217d954SCole Faust
2031*c217d954SCole Faust // @OPTIMIZE: store non-zigzagged during the decode passes,
2032*c217d954SCole Faust // and only de-zigzag when dequantizing
stbi__jpeg_decode_block_prog_ac(stbi__jpeg * j,short data[64],stbi__huffman * hac,stbi__int16 * fac)2033*c217d954SCole Faust static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac)
2034*c217d954SCole Faust {
2035*c217d954SCole Faust int k;
2036*c217d954SCole Faust if (j->spec_start == 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2037*c217d954SCole Faust
2038*c217d954SCole Faust if (j->succ_high == 0) {
2039*c217d954SCole Faust int shift = j->succ_low;
2040*c217d954SCole Faust
2041*c217d954SCole Faust if (j->eob_run) {
2042*c217d954SCole Faust --j->eob_run;
2043*c217d954SCole Faust return 1;
2044*c217d954SCole Faust }
2045*c217d954SCole Faust
2046*c217d954SCole Faust k = j->spec_start;
2047*c217d954SCole Faust do {
2048*c217d954SCole Faust unsigned int zig;
2049*c217d954SCole Faust int c,r,s;
2050*c217d954SCole Faust if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
2051*c217d954SCole Faust c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
2052*c217d954SCole Faust r = fac[c];
2053*c217d954SCole Faust if (r) { // fast-AC path
2054*c217d954SCole Faust k += (r >> 4) & 15; // run
2055*c217d954SCole Faust s = r & 15; // combined length
2056*c217d954SCole Faust j->code_buffer <<= s;
2057*c217d954SCole Faust j->code_bits -= s;
2058*c217d954SCole Faust zig = stbi__jpeg_dezigzag[k++];
2059*c217d954SCole Faust data[zig] = (short) ((r >> 8) << shift);
2060*c217d954SCole Faust } else {
2061*c217d954SCole Faust int rs = stbi__jpeg_huff_decode(j, hac);
2062*c217d954SCole Faust if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
2063*c217d954SCole Faust s = rs & 15;
2064*c217d954SCole Faust r = rs >> 4;
2065*c217d954SCole Faust if (s == 0) {
2066*c217d954SCole Faust if (r < 15) {
2067*c217d954SCole Faust j->eob_run = (1 << r);
2068*c217d954SCole Faust if (r)
2069*c217d954SCole Faust j->eob_run += stbi__jpeg_get_bits(j, r);
2070*c217d954SCole Faust --j->eob_run;
2071*c217d954SCole Faust break;
2072*c217d954SCole Faust }
2073*c217d954SCole Faust k += 16;
2074*c217d954SCole Faust } else {
2075*c217d954SCole Faust k += r;
2076*c217d954SCole Faust zig = stbi__jpeg_dezigzag[k++];
2077*c217d954SCole Faust data[zig] = (short) (stbi__extend_receive(j,s) << shift);
2078*c217d954SCole Faust }
2079*c217d954SCole Faust }
2080*c217d954SCole Faust } while (k <= j->spec_end);
2081*c217d954SCole Faust } else {
2082*c217d954SCole Faust // refinement scan for these AC coefficients
2083*c217d954SCole Faust
2084*c217d954SCole Faust short bit = (short) (1 << j->succ_low);
2085*c217d954SCole Faust
2086*c217d954SCole Faust if (j->eob_run) {
2087*c217d954SCole Faust --j->eob_run;
2088*c217d954SCole Faust for (k = j->spec_start; k <= j->spec_end; ++k) {
2089*c217d954SCole Faust short *p = &data[stbi__jpeg_dezigzag[k]];
2090*c217d954SCole Faust if (*p != 0)
2091*c217d954SCole Faust if (stbi__jpeg_get_bit(j))
2092*c217d954SCole Faust if ((*p & bit)==0) {
2093*c217d954SCole Faust if (*p > 0)
2094*c217d954SCole Faust *p += bit;
2095*c217d954SCole Faust else
2096*c217d954SCole Faust *p -= bit;
2097*c217d954SCole Faust }
2098*c217d954SCole Faust }
2099*c217d954SCole Faust } else {
2100*c217d954SCole Faust k = j->spec_start;
2101*c217d954SCole Faust do {
2102*c217d954SCole Faust int r,s;
2103*c217d954SCole Faust 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
2104*c217d954SCole Faust if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
2105*c217d954SCole Faust s = rs & 15;
2106*c217d954SCole Faust r = rs >> 4;
2107*c217d954SCole Faust if (s == 0) {
2108*c217d954SCole Faust if (r < 15) {
2109*c217d954SCole Faust j->eob_run = (1 << r) - 1;
2110*c217d954SCole Faust if (r)
2111*c217d954SCole Faust j->eob_run += stbi__jpeg_get_bits(j, r);
2112*c217d954SCole Faust r = 64; // force end of block
2113*c217d954SCole Faust } else {
2114*c217d954SCole Faust // r=15 s=0 should write 16 0s, so we just do
2115*c217d954SCole Faust // a run of 15 0s and then write s (which is 0),
2116*c217d954SCole Faust // so we don't have to do anything special here
2117*c217d954SCole Faust }
2118*c217d954SCole Faust } else {
2119*c217d954SCole Faust if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG");
2120*c217d954SCole Faust // sign bit
2121*c217d954SCole Faust if (stbi__jpeg_get_bit(j))
2122*c217d954SCole Faust s = bit;
2123*c217d954SCole Faust else
2124*c217d954SCole Faust s = -bit;
2125*c217d954SCole Faust }
2126*c217d954SCole Faust
2127*c217d954SCole Faust // advance by r
2128*c217d954SCole Faust while (k <= j->spec_end) {
2129*c217d954SCole Faust short *p = &data[stbi__jpeg_dezigzag[k++]];
2130*c217d954SCole Faust if (*p != 0) {
2131*c217d954SCole Faust if (stbi__jpeg_get_bit(j))
2132*c217d954SCole Faust if ((*p & bit)==0) {
2133*c217d954SCole Faust if (*p > 0)
2134*c217d954SCole Faust *p += bit;
2135*c217d954SCole Faust else
2136*c217d954SCole Faust *p -= bit;
2137*c217d954SCole Faust }
2138*c217d954SCole Faust } else {
2139*c217d954SCole Faust if (r == 0) {
2140*c217d954SCole Faust *p = (short) s;
2141*c217d954SCole Faust break;
2142*c217d954SCole Faust }
2143*c217d954SCole Faust --r;
2144*c217d954SCole Faust }
2145*c217d954SCole Faust }
2146*c217d954SCole Faust } while (k <= j->spec_end);
2147*c217d954SCole Faust }
2148*c217d954SCole Faust }
2149*c217d954SCole Faust return 1;
2150*c217d954SCole Faust }
2151*c217d954SCole Faust
2152*c217d954SCole Faust // take a -128..127 value and stbi__clamp it and convert to 0..255
stbi__clamp(int x)2153*c217d954SCole Faust stbi_inline static stbi_uc stbi__clamp(int x)
2154*c217d954SCole Faust {
2155*c217d954SCole Faust // trick to use a single test to catch both cases
2156*c217d954SCole Faust if ((unsigned int) x > 255) {
2157*c217d954SCole Faust if (x < 0) return 0;
2158*c217d954SCole Faust if (x > 255) return 255;
2159*c217d954SCole Faust }
2160*c217d954SCole Faust return (stbi_uc) x;
2161*c217d954SCole Faust }
2162*c217d954SCole Faust
2163*c217d954SCole Faust #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5)))
2164*c217d954SCole Faust #define stbi__fsh(x) ((x) * 4096)
2165*c217d954SCole Faust
2166*c217d954SCole Faust // derived from jidctint -- DCT_ISLOW
2167*c217d954SCole Faust #define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
2168*c217d954SCole Faust int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
2169*c217d954SCole Faust p2 = s2; \
2170*c217d954SCole Faust p3 = s6; \
2171*c217d954SCole Faust p1 = (p2+p3) * stbi__f2f(0.5411961f); \
2172*c217d954SCole Faust t2 = p1 + p3*stbi__f2f(-1.847759065f); \
2173*c217d954SCole Faust t3 = p1 + p2*stbi__f2f( 0.765366865f); \
2174*c217d954SCole Faust p2 = s0; \
2175*c217d954SCole Faust p3 = s4; \
2176*c217d954SCole Faust t0 = stbi__fsh(p2+p3); \
2177*c217d954SCole Faust t1 = stbi__fsh(p2-p3); \
2178*c217d954SCole Faust x0 = t0+t3; \
2179*c217d954SCole Faust x3 = t0-t3; \
2180*c217d954SCole Faust x1 = t1+t2; \
2181*c217d954SCole Faust x2 = t1-t2; \
2182*c217d954SCole Faust t0 = s7; \
2183*c217d954SCole Faust t1 = s5; \
2184*c217d954SCole Faust t2 = s3; \
2185*c217d954SCole Faust t3 = s1; \
2186*c217d954SCole Faust p3 = t0+t2; \
2187*c217d954SCole Faust p4 = t1+t3; \
2188*c217d954SCole Faust p1 = t0+t3; \
2189*c217d954SCole Faust p2 = t1+t2; \
2190*c217d954SCole Faust p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
2191*c217d954SCole Faust t0 = t0*stbi__f2f( 0.298631336f); \
2192*c217d954SCole Faust t1 = t1*stbi__f2f( 2.053119869f); \
2193*c217d954SCole Faust t2 = t2*stbi__f2f( 3.072711026f); \
2194*c217d954SCole Faust t3 = t3*stbi__f2f( 1.501321110f); \
2195*c217d954SCole Faust p1 = p5 + p1*stbi__f2f(-0.899976223f); \
2196*c217d954SCole Faust p2 = p5 + p2*stbi__f2f(-2.562915447f); \
2197*c217d954SCole Faust p3 = p3*stbi__f2f(-1.961570560f); \
2198*c217d954SCole Faust p4 = p4*stbi__f2f(-0.390180644f); \
2199*c217d954SCole Faust t3 += p1+p4; \
2200*c217d954SCole Faust t2 += p2+p3; \
2201*c217d954SCole Faust t1 += p2+p4; \
2202*c217d954SCole Faust t0 += p1+p3;
2203*c217d954SCole Faust
stbi__idct_block(stbi_uc * out,int out_stride,short data[64])2204*c217d954SCole Faust static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64])
2205*c217d954SCole Faust {
2206*c217d954SCole Faust int i,val[64],*v=val;
2207*c217d954SCole Faust stbi_uc *o;
2208*c217d954SCole Faust short *d = data;
2209*c217d954SCole Faust
2210*c217d954SCole Faust // columns
2211*c217d954SCole Faust for (i=0; i < 8; ++i,++d, ++v) {
2212*c217d954SCole Faust // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
2213*c217d954SCole Faust if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
2214*c217d954SCole Faust && d[40]==0 && d[48]==0 && d[56]==0) {
2215*c217d954SCole Faust // no shortcut 0 seconds
2216*c217d954SCole Faust // (1|2|3|4|5|6|7)==0 0 seconds
2217*c217d954SCole Faust // all separate -0.047 seconds
2218*c217d954SCole Faust // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds
2219*c217d954SCole Faust int dcterm = d[0]*4;
2220*c217d954SCole Faust v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
2221*c217d954SCole Faust } else {
2222*c217d954SCole Faust STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56])
2223*c217d954SCole Faust // constants scaled things up by 1<<12; let's bring them back
2224*c217d954SCole Faust // down, but keep 2 extra bits of precision
2225*c217d954SCole Faust x0 += 512; x1 += 512; x2 += 512; x3 += 512;
2226*c217d954SCole Faust v[ 0] = (x0+t3) >> 10;
2227*c217d954SCole Faust v[56] = (x0-t3) >> 10;
2228*c217d954SCole Faust v[ 8] = (x1+t2) >> 10;
2229*c217d954SCole Faust v[48] = (x1-t2) >> 10;
2230*c217d954SCole Faust v[16] = (x2+t1) >> 10;
2231*c217d954SCole Faust v[40] = (x2-t1) >> 10;
2232*c217d954SCole Faust v[24] = (x3+t0) >> 10;
2233*c217d954SCole Faust v[32] = (x3-t0) >> 10;
2234*c217d954SCole Faust }
2235*c217d954SCole Faust }
2236*c217d954SCole Faust
2237*c217d954SCole Faust for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
2238*c217d954SCole Faust // no fast case since the first 1D IDCT spread components out
2239*c217d954SCole Faust STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
2240*c217d954SCole Faust // constants scaled things up by 1<<12, plus we had 1<<2 from first
2241*c217d954SCole Faust // loop, plus horizontal and vertical each scale by sqrt(8) so together
2242*c217d954SCole Faust // we've got an extra 1<<3, so 1<<17 total we need to remove.
2243*c217d954SCole Faust // so we want to round that, which means adding 0.5 * 1<<17,
2244*c217d954SCole Faust // aka 65536. Also, we'll end up with -128 to 127 that we want
2245*c217d954SCole Faust // to encode as 0..255 by adding 128, so we'll add that before the shift
2246*c217d954SCole Faust x0 += 65536 + (128<<17);
2247*c217d954SCole Faust x1 += 65536 + (128<<17);
2248*c217d954SCole Faust x2 += 65536 + (128<<17);
2249*c217d954SCole Faust x3 += 65536 + (128<<17);
2250*c217d954SCole Faust // tried computing the shifts into temps, or'ing the temps to see
2251*c217d954SCole Faust // if any were out of range, but that was slower
2252*c217d954SCole Faust o[0] = stbi__clamp((x0+t3) >> 17);
2253*c217d954SCole Faust o[7] = stbi__clamp((x0-t3) >> 17);
2254*c217d954SCole Faust o[1] = stbi__clamp((x1+t2) >> 17);
2255*c217d954SCole Faust o[6] = stbi__clamp((x1-t2) >> 17);
2256*c217d954SCole Faust o[2] = stbi__clamp((x2+t1) >> 17);
2257*c217d954SCole Faust o[5] = stbi__clamp((x2-t1) >> 17);
2258*c217d954SCole Faust o[3] = stbi__clamp((x3+t0) >> 17);
2259*c217d954SCole Faust o[4] = stbi__clamp((x3-t0) >> 17);
2260*c217d954SCole Faust }
2261*c217d954SCole Faust }
2262*c217d954SCole Faust
2263*c217d954SCole Faust #ifdef STBI_SSE2
2264*c217d954SCole Faust // sse2 integer IDCT. not the fastest possible implementation but it
2265*c217d954SCole Faust // produces bit-identical results to the generic C version so it's
2266*c217d954SCole Faust // fully "transparent".
stbi__idct_simd(stbi_uc * out,int out_stride,short data[64])2267*c217d954SCole Faust static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
2268*c217d954SCole Faust {
2269*c217d954SCole Faust // This is constructed to match our regular (generic) integer IDCT exactly.
2270*c217d954SCole Faust __m128i row0, row1, row2, row3, row4, row5, row6, row7;
2271*c217d954SCole Faust __m128i tmp;
2272*c217d954SCole Faust
2273*c217d954SCole Faust // dot product constant: even elems=x, odd elems=y
2274*c217d954SCole Faust #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y))
2275*c217d954SCole Faust
2276*c217d954SCole Faust // out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit)
2277*c217d954SCole Faust // out(1) = c1[even]*x + c1[odd]*y
2278*c217d954SCole Faust #define dct_rot(out0,out1, x,y,c0,c1) \
2279*c217d954SCole Faust __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \
2280*c217d954SCole Faust __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \
2281*c217d954SCole Faust __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \
2282*c217d954SCole Faust __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \
2283*c217d954SCole Faust __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \
2284*c217d954SCole Faust __m128i out1##_h = _mm_madd_epi16(c0##hi, c1)
2285*c217d954SCole Faust
2286*c217d954SCole Faust // out = in << 12 (in 16-bit, out 32-bit)
2287*c217d954SCole Faust #define dct_widen(out, in) \
2288*c217d954SCole Faust __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \
2289*c217d954SCole Faust __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4)
2290*c217d954SCole Faust
2291*c217d954SCole Faust // wide add
2292*c217d954SCole Faust #define dct_wadd(out, a, b) \
2293*c217d954SCole Faust __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \
2294*c217d954SCole Faust __m128i out##_h = _mm_add_epi32(a##_h, b##_h)
2295*c217d954SCole Faust
2296*c217d954SCole Faust // wide sub
2297*c217d954SCole Faust #define dct_wsub(out, a, b) \
2298*c217d954SCole Faust __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \
2299*c217d954SCole Faust __m128i out##_h = _mm_sub_epi32(a##_h, b##_h)
2300*c217d954SCole Faust
2301*c217d954SCole Faust // butterfly a/b, add bias, then shift by "s" and pack
2302*c217d954SCole Faust #define dct_bfly32o(out0, out1, a,b,bias,s) \
2303*c217d954SCole Faust { \
2304*c217d954SCole Faust __m128i abiased_l = _mm_add_epi32(a##_l, bias); \
2305*c217d954SCole Faust __m128i abiased_h = _mm_add_epi32(a##_h, bias); \
2306*c217d954SCole Faust dct_wadd(sum, abiased, b); \
2307*c217d954SCole Faust dct_wsub(dif, abiased, b); \
2308*c217d954SCole Faust out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \
2309*c217d954SCole Faust out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \
2310*c217d954SCole Faust }
2311*c217d954SCole Faust
2312*c217d954SCole Faust // 8-bit interleave step (for transposes)
2313*c217d954SCole Faust #define dct_interleave8(a, b) \
2314*c217d954SCole Faust tmp = a; \
2315*c217d954SCole Faust a = _mm_unpacklo_epi8(a, b); \
2316*c217d954SCole Faust b = _mm_unpackhi_epi8(tmp, b)
2317*c217d954SCole Faust
2318*c217d954SCole Faust // 16-bit interleave step (for transposes)
2319*c217d954SCole Faust #define dct_interleave16(a, b) \
2320*c217d954SCole Faust tmp = a; \
2321*c217d954SCole Faust a = _mm_unpacklo_epi16(a, b); \
2322*c217d954SCole Faust b = _mm_unpackhi_epi16(tmp, b)
2323*c217d954SCole Faust
2324*c217d954SCole Faust #define dct_pass(bias,shift) \
2325*c217d954SCole Faust { \
2326*c217d954SCole Faust /* even part */ \
2327*c217d954SCole Faust dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \
2328*c217d954SCole Faust __m128i sum04 = _mm_add_epi16(row0, row4); \
2329*c217d954SCole Faust __m128i dif04 = _mm_sub_epi16(row0, row4); \
2330*c217d954SCole Faust dct_widen(t0e, sum04); \
2331*c217d954SCole Faust dct_widen(t1e, dif04); \
2332*c217d954SCole Faust dct_wadd(x0, t0e, t3e); \
2333*c217d954SCole Faust dct_wsub(x3, t0e, t3e); \
2334*c217d954SCole Faust dct_wadd(x1, t1e, t2e); \
2335*c217d954SCole Faust dct_wsub(x2, t1e, t2e); \
2336*c217d954SCole Faust /* odd part */ \
2337*c217d954SCole Faust dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \
2338*c217d954SCole Faust dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \
2339*c217d954SCole Faust __m128i sum17 = _mm_add_epi16(row1, row7); \
2340*c217d954SCole Faust __m128i sum35 = _mm_add_epi16(row3, row5); \
2341*c217d954SCole Faust dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \
2342*c217d954SCole Faust dct_wadd(x4, y0o, y4o); \
2343*c217d954SCole Faust dct_wadd(x5, y1o, y5o); \
2344*c217d954SCole Faust dct_wadd(x6, y2o, y5o); \
2345*c217d954SCole Faust dct_wadd(x7, y3o, y4o); \
2346*c217d954SCole Faust dct_bfly32o(row0,row7, x0,x7,bias,shift); \
2347*c217d954SCole Faust dct_bfly32o(row1,row6, x1,x6,bias,shift); \
2348*c217d954SCole Faust dct_bfly32o(row2,row5, x2,x5,bias,shift); \
2349*c217d954SCole Faust dct_bfly32o(row3,row4, x3,x4,bias,shift); \
2350*c217d954SCole Faust }
2351*c217d954SCole Faust
2352*c217d954SCole Faust __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f));
2353*c217d954SCole Faust __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f));
2354*c217d954SCole Faust __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f));
2355*c217d954SCole Faust __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f));
2356*c217d954SCole Faust __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f));
2357*c217d954SCole Faust __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f));
2358*c217d954SCole Faust __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f));
2359*c217d954SCole Faust __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f));
2360*c217d954SCole Faust
2361*c217d954SCole Faust // rounding biases in column/row passes, see stbi__idct_block for explanation.
2362*c217d954SCole Faust __m128i bias_0 = _mm_set1_epi32(512);
2363*c217d954SCole Faust __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17));
2364*c217d954SCole Faust
2365*c217d954SCole Faust // load
2366*c217d954SCole Faust row0 = _mm_load_si128((const __m128i *) (data + 0*8));
2367*c217d954SCole Faust row1 = _mm_load_si128((const __m128i *) (data + 1*8));
2368*c217d954SCole Faust row2 = _mm_load_si128((const __m128i *) (data + 2*8));
2369*c217d954SCole Faust row3 = _mm_load_si128((const __m128i *) (data + 3*8));
2370*c217d954SCole Faust row4 = _mm_load_si128((const __m128i *) (data + 4*8));
2371*c217d954SCole Faust row5 = _mm_load_si128((const __m128i *) (data + 5*8));
2372*c217d954SCole Faust row6 = _mm_load_si128((const __m128i *) (data + 6*8));
2373*c217d954SCole Faust row7 = _mm_load_si128((const __m128i *) (data + 7*8));
2374*c217d954SCole Faust
2375*c217d954SCole Faust // column pass
2376*c217d954SCole Faust dct_pass(bias_0, 10);
2377*c217d954SCole Faust
2378*c217d954SCole Faust {
2379*c217d954SCole Faust // 16bit 8x8 transpose pass 1
2380*c217d954SCole Faust dct_interleave16(row0, row4);
2381*c217d954SCole Faust dct_interleave16(row1, row5);
2382*c217d954SCole Faust dct_interleave16(row2, row6);
2383*c217d954SCole Faust dct_interleave16(row3, row7);
2384*c217d954SCole Faust
2385*c217d954SCole Faust // transpose pass 2
2386*c217d954SCole Faust dct_interleave16(row0, row2);
2387*c217d954SCole Faust dct_interleave16(row1, row3);
2388*c217d954SCole Faust dct_interleave16(row4, row6);
2389*c217d954SCole Faust dct_interleave16(row5, row7);
2390*c217d954SCole Faust
2391*c217d954SCole Faust // transpose pass 3
2392*c217d954SCole Faust dct_interleave16(row0, row1);
2393*c217d954SCole Faust dct_interleave16(row2, row3);
2394*c217d954SCole Faust dct_interleave16(row4, row5);
2395*c217d954SCole Faust dct_interleave16(row6, row7);
2396*c217d954SCole Faust }
2397*c217d954SCole Faust
2398*c217d954SCole Faust // row pass
2399*c217d954SCole Faust dct_pass(bias_1, 17);
2400*c217d954SCole Faust
2401*c217d954SCole Faust {
2402*c217d954SCole Faust // pack
2403*c217d954SCole Faust __m128i p0 = _mm_packus_epi16(row0, row1); // a0a1a2a3...a7b0b1b2b3...b7
2404*c217d954SCole Faust __m128i p1 = _mm_packus_epi16(row2, row3);
2405*c217d954SCole Faust __m128i p2 = _mm_packus_epi16(row4, row5);
2406*c217d954SCole Faust __m128i p3 = _mm_packus_epi16(row6, row7);
2407*c217d954SCole Faust
2408*c217d954SCole Faust // 8bit 8x8 transpose pass 1
2409*c217d954SCole Faust dct_interleave8(p0, p2); // a0e0a1e1...
2410*c217d954SCole Faust dct_interleave8(p1, p3); // c0g0c1g1...
2411*c217d954SCole Faust
2412*c217d954SCole Faust // transpose pass 2
2413*c217d954SCole Faust dct_interleave8(p0, p1); // a0c0e0g0...
2414*c217d954SCole Faust dct_interleave8(p2, p3); // b0d0f0h0...
2415*c217d954SCole Faust
2416*c217d954SCole Faust // transpose pass 3
2417*c217d954SCole Faust dct_interleave8(p0, p2); // a0b0c0d0...
2418*c217d954SCole Faust dct_interleave8(p1, p3); // a4b4c4d4...
2419*c217d954SCole Faust
2420*c217d954SCole Faust // store
2421*c217d954SCole Faust _mm_storel_epi64((__m128i *) out, p0); out += out_stride;
2422*c217d954SCole Faust _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride;
2423*c217d954SCole Faust _mm_storel_epi64((__m128i *) out, p2); out += out_stride;
2424*c217d954SCole Faust _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride;
2425*c217d954SCole Faust _mm_storel_epi64((__m128i *) out, p1); out += out_stride;
2426*c217d954SCole Faust _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride;
2427*c217d954SCole Faust _mm_storel_epi64((__m128i *) out, p3); out += out_stride;
2428*c217d954SCole Faust _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e));
2429*c217d954SCole Faust }
2430*c217d954SCole Faust
2431*c217d954SCole Faust #undef dct_const
2432*c217d954SCole Faust #undef dct_rot
2433*c217d954SCole Faust #undef dct_widen
2434*c217d954SCole Faust #undef dct_wadd
2435*c217d954SCole Faust #undef dct_wsub
2436*c217d954SCole Faust #undef dct_bfly32o
2437*c217d954SCole Faust #undef dct_interleave8
2438*c217d954SCole Faust #undef dct_interleave16
2439*c217d954SCole Faust #undef dct_pass
2440*c217d954SCole Faust }
2441*c217d954SCole Faust
2442*c217d954SCole Faust #endif // STBI_SSE2
2443*c217d954SCole Faust
2444*c217d954SCole Faust #ifdef STBI_NEON
2445*c217d954SCole Faust
2446*c217d954SCole Faust // NEON integer IDCT. should produce bit-identical
2447*c217d954SCole Faust // results to the generic C version.
stbi__idct_simd(stbi_uc * out,int out_stride,short data[64])2448*c217d954SCole Faust static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
2449*c217d954SCole Faust {
2450*c217d954SCole Faust int16x8_t row0, row1, row2, row3, row4, row5, row6, row7;
2451*c217d954SCole Faust
2452*c217d954SCole Faust int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f));
2453*c217d954SCole Faust int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f));
2454*c217d954SCole Faust int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f));
2455*c217d954SCole Faust int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f));
2456*c217d954SCole Faust int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f));
2457*c217d954SCole Faust int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f));
2458*c217d954SCole Faust int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f));
2459*c217d954SCole Faust int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f));
2460*c217d954SCole Faust int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f));
2461*c217d954SCole Faust int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f));
2462*c217d954SCole Faust int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f));
2463*c217d954SCole Faust int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f));
2464*c217d954SCole Faust
2465*c217d954SCole Faust #define dct_long_mul(out, inq, coeff) \
2466*c217d954SCole Faust int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \
2467*c217d954SCole Faust int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff)
2468*c217d954SCole Faust
2469*c217d954SCole Faust #define dct_long_mac(out, acc, inq, coeff) \
2470*c217d954SCole Faust int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \
2471*c217d954SCole Faust int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff)
2472*c217d954SCole Faust
2473*c217d954SCole Faust #define dct_widen(out, inq) \
2474*c217d954SCole Faust int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \
2475*c217d954SCole Faust int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12)
2476*c217d954SCole Faust
2477*c217d954SCole Faust // wide add
2478*c217d954SCole Faust #define dct_wadd(out, a, b) \
2479*c217d954SCole Faust int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \
2480*c217d954SCole Faust int32x4_t out##_h = vaddq_s32(a##_h, b##_h)
2481*c217d954SCole Faust
2482*c217d954SCole Faust // wide sub
2483*c217d954SCole Faust #define dct_wsub(out, a, b) \
2484*c217d954SCole Faust int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \
2485*c217d954SCole Faust int32x4_t out##_h = vsubq_s32(a##_h, b##_h)
2486*c217d954SCole Faust
2487*c217d954SCole Faust // butterfly a/b, then shift using "shiftop" by "s" and pack
2488*c217d954SCole Faust #define dct_bfly32o(out0,out1, a,b,shiftop,s) \
2489*c217d954SCole Faust { \
2490*c217d954SCole Faust dct_wadd(sum, a, b); \
2491*c217d954SCole Faust dct_wsub(dif, a, b); \
2492*c217d954SCole Faust out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \
2493*c217d954SCole Faust out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \
2494*c217d954SCole Faust }
2495*c217d954SCole Faust
2496*c217d954SCole Faust #define dct_pass(shiftop, shift) \
2497*c217d954SCole Faust { \
2498*c217d954SCole Faust /* even part */ \
2499*c217d954SCole Faust int16x8_t sum26 = vaddq_s16(row2, row6); \
2500*c217d954SCole Faust dct_long_mul(p1e, sum26, rot0_0); \
2501*c217d954SCole Faust dct_long_mac(t2e, p1e, row6, rot0_1); \
2502*c217d954SCole Faust dct_long_mac(t3e, p1e, row2, rot0_2); \
2503*c217d954SCole Faust int16x8_t sum04 = vaddq_s16(row0, row4); \
2504*c217d954SCole Faust int16x8_t dif04 = vsubq_s16(row0, row4); \
2505*c217d954SCole Faust dct_widen(t0e, sum04); \
2506*c217d954SCole Faust dct_widen(t1e, dif04); \
2507*c217d954SCole Faust dct_wadd(x0, t0e, t3e); \
2508*c217d954SCole Faust dct_wsub(x3, t0e, t3e); \
2509*c217d954SCole Faust dct_wadd(x1, t1e, t2e); \
2510*c217d954SCole Faust dct_wsub(x2, t1e, t2e); \
2511*c217d954SCole Faust /* odd part */ \
2512*c217d954SCole Faust int16x8_t sum15 = vaddq_s16(row1, row5); \
2513*c217d954SCole Faust int16x8_t sum17 = vaddq_s16(row1, row7); \
2514*c217d954SCole Faust int16x8_t sum35 = vaddq_s16(row3, row5); \
2515*c217d954SCole Faust int16x8_t sum37 = vaddq_s16(row3, row7); \
2516*c217d954SCole Faust int16x8_t sumodd = vaddq_s16(sum17, sum35); \
2517*c217d954SCole Faust dct_long_mul(p5o, sumodd, rot1_0); \
2518*c217d954SCole Faust dct_long_mac(p1o, p5o, sum17, rot1_1); \
2519*c217d954SCole Faust dct_long_mac(p2o, p5o, sum35, rot1_2); \
2520*c217d954SCole Faust dct_long_mul(p3o, sum37, rot2_0); \
2521*c217d954SCole Faust dct_long_mul(p4o, sum15, rot2_1); \
2522*c217d954SCole Faust dct_wadd(sump13o, p1o, p3o); \
2523*c217d954SCole Faust dct_wadd(sump24o, p2o, p4o); \
2524*c217d954SCole Faust dct_wadd(sump23o, p2o, p3o); \
2525*c217d954SCole Faust dct_wadd(sump14o, p1o, p4o); \
2526*c217d954SCole Faust dct_long_mac(x4, sump13o, row7, rot3_0); \
2527*c217d954SCole Faust dct_long_mac(x5, sump24o, row5, rot3_1); \
2528*c217d954SCole Faust dct_long_mac(x6, sump23o, row3, rot3_2); \
2529*c217d954SCole Faust dct_long_mac(x7, sump14o, row1, rot3_3); \
2530*c217d954SCole Faust dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \
2531*c217d954SCole Faust dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \
2532*c217d954SCole Faust dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \
2533*c217d954SCole Faust dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \
2534*c217d954SCole Faust }
2535*c217d954SCole Faust
2536*c217d954SCole Faust // load
2537*c217d954SCole Faust row0 = vld1q_s16(data + 0*8);
2538*c217d954SCole Faust row1 = vld1q_s16(data + 1*8);
2539*c217d954SCole Faust row2 = vld1q_s16(data + 2*8);
2540*c217d954SCole Faust row3 = vld1q_s16(data + 3*8);
2541*c217d954SCole Faust row4 = vld1q_s16(data + 4*8);
2542*c217d954SCole Faust row5 = vld1q_s16(data + 5*8);
2543*c217d954SCole Faust row6 = vld1q_s16(data + 6*8);
2544*c217d954SCole Faust row7 = vld1q_s16(data + 7*8);
2545*c217d954SCole Faust
2546*c217d954SCole Faust // add DC bias
2547*c217d954SCole Faust row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0));
2548*c217d954SCole Faust
2549*c217d954SCole Faust // column pass
2550*c217d954SCole Faust dct_pass(vrshrn_n_s32, 10);
2551*c217d954SCole Faust
2552*c217d954SCole Faust // 16bit 8x8 transpose
2553*c217d954SCole Faust {
2554*c217d954SCole Faust // these three map to a single VTRN.16, VTRN.32, and VSWP, respectively.
2555*c217d954SCole Faust // whether compilers actually get this is another story, sadly.
2556*c217d954SCole Faust #define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; }
2557*c217d954SCole Faust #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]); }
2558*c217d954SCole Faust #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)); }
2559*c217d954SCole Faust
2560*c217d954SCole Faust // pass 1
2561*c217d954SCole Faust dct_trn16(row0, row1); // a0b0a2b2a4b4a6b6
2562*c217d954SCole Faust dct_trn16(row2, row3);
2563*c217d954SCole Faust dct_trn16(row4, row5);
2564*c217d954SCole Faust dct_trn16(row6, row7);
2565*c217d954SCole Faust
2566*c217d954SCole Faust // pass 2
2567*c217d954SCole Faust dct_trn32(row0, row2); // a0b0c0d0a4b4c4d4
2568*c217d954SCole Faust dct_trn32(row1, row3);
2569*c217d954SCole Faust dct_trn32(row4, row6);
2570*c217d954SCole Faust dct_trn32(row5, row7);
2571*c217d954SCole Faust
2572*c217d954SCole Faust // pass 3
2573*c217d954SCole Faust dct_trn64(row0, row4); // a0b0c0d0e0f0g0h0
2574*c217d954SCole Faust dct_trn64(row1, row5);
2575*c217d954SCole Faust dct_trn64(row2, row6);
2576*c217d954SCole Faust dct_trn64(row3, row7);
2577*c217d954SCole Faust
2578*c217d954SCole Faust #undef dct_trn16
2579*c217d954SCole Faust #undef dct_trn32
2580*c217d954SCole Faust #undef dct_trn64
2581*c217d954SCole Faust }
2582*c217d954SCole Faust
2583*c217d954SCole Faust // row pass
2584*c217d954SCole Faust // vrshrn_n_s32 only supports shifts up to 16, we need
2585*c217d954SCole Faust // 17. so do a non-rounding shift of 16 first then follow
2586*c217d954SCole Faust // up with a rounding shift by 1.
2587*c217d954SCole Faust dct_pass(vshrn_n_s32, 16);
2588*c217d954SCole Faust
2589*c217d954SCole Faust {
2590*c217d954SCole Faust // pack and round
2591*c217d954SCole Faust uint8x8_t p0 = vqrshrun_n_s16(row0, 1);
2592*c217d954SCole Faust uint8x8_t p1 = vqrshrun_n_s16(row1, 1);
2593*c217d954SCole Faust uint8x8_t p2 = vqrshrun_n_s16(row2, 1);
2594*c217d954SCole Faust uint8x8_t p3 = vqrshrun_n_s16(row3, 1);
2595*c217d954SCole Faust uint8x8_t p4 = vqrshrun_n_s16(row4, 1);
2596*c217d954SCole Faust uint8x8_t p5 = vqrshrun_n_s16(row5, 1);
2597*c217d954SCole Faust uint8x8_t p6 = vqrshrun_n_s16(row6, 1);
2598*c217d954SCole Faust uint8x8_t p7 = vqrshrun_n_s16(row7, 1);
2599*c217d954SCole Faust
2600*c217d954SCole Faust // again, these can translate into one instruction, but often don't.
2601*c217d954SCole Faust #define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; }
2602*c217d954SCole Faust #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]); }
2603*c217d954SCole Faust #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]); }
2604*c217d954SCole Faust
2605*c217d954SCole Faust // sadly can't use interleaved stores here since we only write
2606*c217d954SCole Faust // 8 bytes to each scan line!
2607*c217d954SCole Faust
2608*c217d954SCole Faust // 8x8 8-bit transpose pass 1
2609*c217d954SCole Faust dct_trn8_8(p0, p1);
2610*c217d954SCole Faust dct_trn8_8(p2, p3);
2611*c217d954SCole Faust dct_trn8_8(p4, p5);
2612*c217d954SCole Faust dct_trn8_8(p6, p7);
2613*c217d954SCole Faust
2614*c217d954SCole Faust // pass 2
2615*c217d954SCole Faust dct_trn8_16(p0, p2);
2616*c217d954SCole Faust dct_trn8_16(p1, p3);
2617*c217d954SCole Faust dct_trn8_16(p4, p6);
2618*c217d954SCole Faust dct_trn8_16(p5, p7);
2619*c217d954SCole Faust
2620*c217d954SCole Faust // pass 3
2621*c217d954SCole Faust dct_trn8_32(p0, p4);
2622*c217d954SCole Faust dct_trn8_32(p1, p5);
2623*c217d954SCole Faust dct_trn8_32(p2, p6);
2624*c217d954SCole Faust dct_trn8_32(p3, p7);
2625*c217d954SCole Faust
2626*c217d954SCole Faust // store
2627*c217d954SCole Faust vst1_u8(out, p0); out += out_stride;
2628*c217d954SCole Faust vst1_u8(out, p1); out += out_stride;
2629*c217d954SCole Faust vst1_u8(out, p2); out += out_stride;
2630*c217d954SCole Faust vst1_u8(out, p3); out += out_stride;
2631*c217d954SCole Faust vst1_u8(out, p4); out += out_stride;
2632*c217d954SCole Faust vst1_u8(out, p5); out += out_stride;
2633*c217d954SCole Faust vst1_u8(out, p6); out += out_stride;
2634*c217d954SCole Faust vst1_u8(out, p7);
2635*c217d954SCole Faust
2636*c217d954SCole Faust #undef dct_trn8_8
2637*c217d954SCole Faust #undef dct_trn8_16
2638*c217d954SCole Faust #undef dct_trn8_32
2639*c217d954SCole Faust }
2640*c217d954SCole Faust
2641*c217d954SCole Faust #undef dct_long_mul
2642*c217d954SCole Faust #undef dct_long_mac
2643*c217d954SCole Faust #undef dct_widen
2644*c217d954SCole Faust #undef dct_wadd
2645*c217d954SCole Faust #undef dct_wsub
2646*c217d954SCole Faust #undef dct_bfly32o
2647*c217d954SCole Faust #undef dct_pass
2648*c217d954SCole Faust }
2649*c217d954SCole Faust
2650*c217d954SCole Faust #endif // STBI_NEON
2651*c217d954SCole Faust
2652*c217d954SCole Faust #define STBI__MARKER_none 0xff
2653*c217d954SCole Faust // if there's a pending marker from the entropy stream, return that
2654*c217d954SCole Faust // otherwise, fetch from the stream and get a marker. if there's no
2655*c217d954SCole Faust // marker, return 0xff, which is never a valid marker value
stbi__get_marker(stbi__jpeg * j)2656*c217d954SCole Faust static stbi_uc stbi__get_marker(stbi__jpeg *j)
2657*c217d954SCole Faust {
2658*c217d954SCole Faust stbi_uc x;
2659*c217d954SCole Faust if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; }
2660*c217d954SCole Faust x = stbi__get8(j->s);
2661*c217d954SCole Faust if (x != 0xff) return STBI__MARKER_none;
2662*c217d954SCole Faust while (x == 0xff)
2663*c217d954SCole Faust x = stbi__get8(j->s); // consume repeated 0xff fill bytes
2664*c217d954SCole Faust return x;
2665*c217d954SCole Faust }
2666*c217d954SCole Faust
2667*c217d954SCole Faust // in each scan, we'll have scan_n components, and the order
2668*c217d954SCole Faust // of the components is specified by order[]
2669*c217d954SCole Faust #define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
2670*c217d954SCole Faust
2671*c217d954SCole Faust // after a restart interval, stbi__jpeg_reset the entropy decoder and
2672*c217d954SCole Faust // the dc prediction
stbi__jpeg_reset(stbi__jpeg * j)2673*c217d954SCole Faust static void stbi__jpeg_reset(stbi__jpeg *j)
2674*c217d954SCole Faust {
2675*c217d954SCole Faust j->code_bits = 0;
2676*c217d954SCole Faust j->code_buffer = 0;
2677*c217d954SCole Faust j->nomore = 0;
2678*c217d954SCole Faust 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;
2679*c217d954SCole Faust j->marker = STBI__MARKER_none;
2680*c217d954SCole Faust j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
2681*c217d954SCole Faust j->eob_run = 0;
2682*c217d954SCole Faust // no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
2683*c217d954SCole Faust // since we don't even allow 1<<30 pixels
2684*c217d954SCole Faust }
2685*c217d954SCole Faust
stbi__parse_entropy_coded_data(stbi__jpeg * z)2686*c217d954SCole Faust static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
2687*c217d954SCole Faust {
2688*c217d954SCole Faust stbi__jpeg_reset(z);
2689*c217d954SCole Faust if (!z->progressive) {
2690*c217d954SCole Faust if (z->scan_n == 1) {
2691*c217d954SCole Faust int i,j;
2692*c217d954SCole Faust STBI_SIMD_ALIGN(short, data[64]);
2693*c217d954SCole Faust int n = z->order[0];
2694*c217d954SCole Faust // non-interleaved data, we just need to process one block at a time,
2695*c217d954SCole Faust // in trivial scanline order
2696*c217d954SCole Faust // number of blocks to do just depends on how many actual "pixels" this
2697*c217d954SCole Faust // component has, independent of interleaved MCU blocking and such
2698*c217d954SCole Faust int w = (z->img_comp[n].x+7) >> 3;
2699*c217d954SCole Faust int h = (z->img_comp[n].y+7) >> 3;
2700*c217d954SCole Faust for (j=0; j < h; ++j) {
2701*c217d954SCole Faust for (i=0; i < w; ++i) {
2702*c217d954SCole Faust int ha = z->img_comp[n].ha;
2703*c217d954SCole Faust 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;
2704*c217d954SCole Faust z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data);
2705*c217d954SCole Faust // every data block is an MCU, so countdown the restart interval
2706*c217d954SCole Faust if (--z->todo <= 0) {
2707*c217d954SCole Faust if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
2708*c217d954SCole Faust // if it's NOT a restart, then just bail, so we get corrupt data
2709*c217d954SCole Faust // rather than no data
2710*c217d954SCole Faust if (!STBI__RESTART(z->marker)) return 1;
2711*c217d954SCole Faust stbi__jpeg_reset(z);
2712*c217d954SCole Faust }
2713*c217d954SCole Faust }
2714*c217d954SCole Faust }
2715*c217d954SCole Faust return 1;
2716*c217d954SCole Faust } else { // interleaved
2717*c217d954SCole Faust int i,j,k,x,y;
2718*c217d954SCole Faust STBI_SIMD_ALIGN(short, data[64]);
2719*c217d954SCole Faust for (j=0; j < z->img_mcu_y; ++j) {
2720*c217d954SCole Faust for (i=0; i < z->img_mcu_x; ++i) {
2721*c217d954SCole Faust // scan an interleaved mcu... process scan_n components in order
2722*c217d954SCole Faust for (k=0; k < z->scan_n; ++k) {
2723*c217d954SCole Faust int n = z->order[k];
2724*c217d954SCole Faust // scan out an mcu's worth of this component; that's just determined
2725*c217d954SCole Faust // by the basic H and V specified for the component
2726*c217d954SCole Faust for (y=0; y < z->img_comp[n].v; ++y) {
2727*c217d954SCole Faust for (x=0; x < z->img_comp[n].h; ++x) {
2728*c217d954SCole Faust int x2 = (i*z->img_comp[n].h + x)*8;
2729*c217d954SCole Faust int y2 = (j*z->img_comp[n].v + y)*8;
2730*c217d954SCole Faust int ha = z->img_comp[n].ha;
2731*c217d954SCole Faust 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;
2732*c217d954SCole Faust z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data);
2733*c217d954SCole Faust }
2734*c217d954SCole Faust }
2735*c217d954SCole Faust }
2736*c217d954SCole Faust // after all interleaved components, that's an interleaved MCU,
2737*c217d954SCole Faust // so now count down the restart interval
2738*c217d954SCole Faust if (--z->todo <= 0) {
2739*c217d954SCole Faust if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
2740*c217d954SCole Faust if (!STBI__RESTART(z->marker)) return 1;
2741*c217d954SCole Faust stbi__jpeg_reset(z);
2742*c217d954SCole Faust }
2743*c217d954SCole Faust }
2744*c217d954SCole Faust }
2745*c217d954SCole Faust return 1;
2746*c217d954SCole Faust }
2747*c217d954SCole Faust } else {
2748*c217d954SCole Faust if (z->scan_n == 1) {
2749*c217d954SCole Faust int i,j;
2750*c217d954SCole Faust int n = z->order[0];
2751*c217d954SCole Faust // non-interleaved data, we just need to process one block at a time,
2752*c217d954SCole Faust // in trivial scanline order
2753*c217d954SCole Faust // number of blocks to do just depends on how many actual "pixels" this
2754*c217d954SCole Faust // component has, independent of interleaved MCU blocking and such
2755*c217d954SCole Faust int w = (z->img_comp[n].x+7) >> 3;
2756*c217d954SCole Faust int h = (z->img_comp[n].y+7) >> 3;
2757*c217d954SCole Faust for (j=0; j < h; ++j) {
2758*c217d954SCole Faust for (i=0; i < w; ++i) {
2759*c217d954SCole Faust short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
2760*c217d954SCole Faust if (z->spec_start == 0) {
2761*c217d954SCole Faust if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
2762*c217d954SCole Faust return 0;
2763*c217d954SCole Faust } else {
2764*c217d954SCole Faust int ha = z->img_comp[n].ha;
2765*c217d954SCole Faust if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha]))
2766*c217d954SCole Faust return 0;
2767*c217d954SCole Faust }
2768*c217d954SCole Faust // every data block is an MCU, so countdown the restart interval
2769*c217d954SCole Faust if (--z->todo <= 0) {
2770*c217d954SCole Faust if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
2771*c217d954SCole Faust if (!STBI__RESTART(z->marker)) return 1;
2772*c217d954SCole Faust stbi__jpeg_reset(z);
2773*c217d954SCole Faust }
2774*c217d954SCole Faust }
2775*c217d954SCole Faust }
2776*c217d954SCole Faust return 1;
2777*c217d954SCole Faust } else { // interleaved
2778*c217d954SCole Faust int i,j,k,x,y;
2779*c217d954SCole Faust for (j=0; j < z->img_mcu_y; ++j) {
2780*c217d954SCole Faust for (i=0; i < z->img_mcu_x; ++i) {
2781*c217d954SCole Faust // scan an interleaved mcu... process scan_n components in order
2782*c217d954SCole Faust for (k=0; k < z->scan_n; ++k) {
2783*c217d954SCole Faust int n = z->order[k];
2784*c217d954SCole Faust // scan out an mcu's worth of this component; that's just determined
2785*c217d954SCole Faust // by the basic H and V specified for the component
2786*c217d954SCole Faust for (y=0; y < z->img_comp[n].v; ++y) {
2787*c217d954SCole Faust for (x=0; x < z->img_comp[n].h; ++x) {
2788*c217d954SCole Faust int x2 = (i*z->img_comp[n].h + x);
2789*c217d954SCole Faust int y2 = (j*z->img_comp[n].v + y);
2790*c217d954SCole Faust short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w);
2791*c217d954SCole Faust if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
2792*c217d954SCole Faust return 0;
2793*c217d954SCole Faust }
2794*c217d954SCole Faust }
2795*c217d954SCole Faust }
2796*c217d954SCole Faust // after all interleaved components, that's an interleaved MCU,
2797*c217d954SCole Faust // so now count down the restart interval
2798*c217d954SCole Faust if (--z->todo <= 0) {
2799*c217d954SCole Faust if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
2800*c217d954SCole Faust if (!STBI__RESTART(z->marker)) return 1;
2801*c217d954SCole Faust stbi__jpeg_reset(z);
2802*c217d954SCole Faust }
2803*c217d954SCole Faust }
2804*c217d954SCole Faust }
2805*c217d954SCole Faust return 1;
2806*c217d954SCole Faust }
2807*c217d954SCole Faust }
2808*c217d954SCole Faust }
2809*c217d954SCole Faust
stbi__jpeg_dequantize(short * data,stbi__uint16 * dequant)2810*c217d954SCole Faust static void stbi__jpeg_dequantize(short *data, stbi__uint16 *dequant)
2811*c217d954SCole Faust {
2812*c217d954SCole Faust int i;
2813*c217d954SCole Faust for (i=0; i < 64; ++i)
2814*c217d954SCole Faust data[i] *= dequant[i];
2815*c217d954SCole Faust }
2816*c217d954SCole Faust
stbi__jpeg_finish(stbi__jpeg * z)2817*c217d954SCole Faust static void stbi__jpeg_finish(stbi__jpeg *z)
2818*c217d954SCole Faust {
2819*c217d954SCole Faust if (z->progressive) {
2820*c217d954SCole Faust // dequantize and idct the data
2821*c217d954SCole Faust int i,j,n;
2822*c217d954SCole Faust for (n=0; n < z->s->img_n; ++n) {
2823*c217d954SCole Faust int w = (z->img_comp[n].x+7) >> 3;
2824*c217d954SCole Faust int h = (z->img_comp[n].y+7) >> 3;
2825*c217d954SCole Faust for (j=0; j < h; ++j) {
2826*c217d954SCole Faust for (i=0; i < w; ++i) {
2827*c217d954SCole Faust short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
2828*c217d954SCole Faust stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]);
2829*c217d954SCole Faust z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data);
2830*c217d954SCole Faust }
2831*c217d954SCole Faust }
2832*c217d954SCole Faust }
2833*c217d954SCole Faust }
2834*c217d954SCole Faust }
2835*c217d954SCole Faust
stbi__process_marker(stbi__jpeg * z,int m)2836*c217d954SCole Faust static int stbi__process_marker(stbi__jpeg *z, int m)
2837*c217d954SCole Faust {
2838*c217d954SCole Faust int L;
2839*c217d954SCole Faust switch (m) {
2840*c217d954SCole Faust case STBI__MARKER_none: // no marker found
2841*c217d954SCole Faust return stbi__err("expected marker","Corrupt JPEG");
2842*c217d954SCole Faust
2843*c217d954SCole Faust case 0xDD: // DRI - specify restart interval
2844*c217d954SCole Faust if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG");
2845*c217d954SCole Faust z->restart_interval = stbi__get16be(z->s);
2846*c217d954SCole Faust return 1;
2847*c217d954SCole Faust
2848*c217d954SCole Faust case 0xDB: // DQT - define quantization table
2849*c217d954SCole Faust L = stbi__get16be(z->s)-2;
2850*c217d954SCole Faust while (L > 0) {
2851*c217d954SCole Faust int q = stbi__get8(z->s);
2852*c217d954SCole Faust int p = q >> 4, sixteen = (p != 0);
2853*c217d954SCole Faust int t = q & 15,i;
2854*c217d954SCole Faust if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG");
2855*c217d954SCole Faust if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG");
2856*c217d954SCole Faust
2857*c217d954SCole Faust for (i=0; i < 64; ++i)
2858*c217d954SCole Faust z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s));
2859*c217d954SCole Faust L -= (sixteen ? 129 : 65);
2860*c217d954SCole Faust }
2861*c217d954SCole Faust return L==0;
2862*c217d954SCole Faust
2863*c217d954SCole Faust case 0xC4: // DHT - define huffman table
2864*c217d954SCole Faust L = stbi__get16be(z->s)-2;
2865*c217d954SCole Faust while (L > 0) {
2866*c217d954SCole Faust stbi_uc *v;
2867*c217d954SCole Faust int sizes[16],i,n=0;
2868*c217d954SCole Faust int q = stbi__get8(z->s);
2869*c217d954SCole Faust int tc = q >> 4;
2870*c217d954SCole Faust int th = q & 15;
2871*c217d954SCole Faust if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG");
2872*c217d954SCole Faust for (i=0; i < 16; ++i) {
2873*c217d954SCole Faust sizes[i] = stbi__get8(z->s);
2874*c217d954SCole Faust n += sizes[i];
2875*c217d954SCole Faust }
2876*c217d954SCole Faust L -= 17;
2877*c217d954SCole Faust if (tc == 0) {
2878*c217d954SCole Faust if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0;
2879*c217d954SCole Faust v = z->huff_dc[th].values;
2880*c217d954SCole Faust } else {
2881*c217d954SCole Faust if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0;
2882*c217d954SCole Faust v = z->huff_ac[th].values;
2883*c217d954SCole Faust }
2884*c217d954SCole Faust for (i=0; i < n; ++i)
2885*c217d954SCole Faust v[i] = stbi__get8(z->s);
2886*c217d954SCole Faust if (tc != 0)
2887*c217d954SCole Faust stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th);
2888*c217d954SCole Faust L -= n;
2889*c217d954SCole Faust }
2890*c217d954SCole Faust return L==0;
2891*c217d954SCole Faust }
2892*c217d954SCole Faust
2893*c217d954SCole Faust // check for comment block or APP blocks
2894*c217d954SCole Faust if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
2895*c217d954SCole Faust L = stbi__get16be(z->s);
2896*c217d954SCole Faust if (L < 2) {
2897*c217d954SCole Faust if (m == 0xFE)
2898*c217d954SCole Faust return stbi__err("bad COM len","Corrupt JPEG");
2899*c217d954SCole Faust else
2900*c217d954SCole Faust return stbi__err("bad APP len","Corrupt JPEG");
2901*c217d954SCole Faust }
2902*c217d954SCole Faust L -= 2;
2903*c217d954SCole Faust
2904*c217d954SCole Faust if (m == 0xE0 && L >= 5) { // JFIF APP0 segment
2905*c217d954SCole Faust static const unsigned char tag[5] = {'J','F','I','F','\0'};
2906*c217d954SCole Faust int ok = 1;
2907*c217d954SCole Faust int i;
2908*c217d954SCole Faust for (i=0; i < 5; ++i)
2909*c217d954SCole Faust if (stbi__get8(z->s) != tag[i])
2910*c217d954SCole Faust ok = 0;
2911*c217d954SCole Faust L -= 5;
2912*c217d954SCole Faust if (ok)
2913*c217d954SCole Faust z->jfif = 1;
2914*c217d954SCole Faust } else if (m == 0xEE && L >= 12) { // Adobe APP14 segment
2915*c217d954SCole Faust static const unsigned char tag[6] = {'A','d','o','b','e','\0'};
2916*c217d954SCole Faust int ok = 1;
2917*c217d954SCole Faust int i;
2918*c217d954SCole Faust for (i=0; i < 6; ++i)
2919*c217d954SCole Faust if (stbi__get8(z->s) != tag[i])
2920*c217d954SCole Faust ok = 0;
2921*c217d954SCole Faust L -= 6;
2922*c217d954SCole Faust if (ok) {
2923*c217d954SCole Faust stbi__get8(z->s); // version
2924*c217d954SCole Faust stbi__get16be(z->s); // flags0
2925*c217d954SCole Faust stbi__get16be(z->s); // flags1
2926*c217d954SCole Faust z->app14_color_transform = stbi__get8(z->s); // color transform
2927*c217d954SCole Faust L -= 6;
2928*c217d954SCole Faust }
2929*c217d954SCole Faust }
2930*c217d954SCole Faust
2931*c217d954SCole Faust stbi__skip(z->s, L);
2932*c217d954SCole Faust return 1;
2933*c217d954SCole Faust }
2934*c217d954SCole Faust
2935*c217d954SCole Faust return stbi__err("unknown marker","Corrupt JPEG");
2936*c217d954SCole Faust }
2937*c217d954SCole Faust
2938*c217d954SCole Faust // after we see SOS
stbi__process_scan_header(stbi__jpeg * z)2939*c217d954SCole Faust static int stbi__process_scan_header(stbi__jpeg *z)
2940*c217d954SCole Faust {
2941*c217d954SCole Faust int i;
2942*c217d954SCole Faust int Ls = stbi__get16be(z->s);
2943*c217d954SCole Faust z->scan_n = stbi__get8(z->s);
2944*c217d954SCole Faust 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");
2945*c217d954SCole Faust if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len","Corrupt JPEG");
2946*c217d954SCole Faust for (i=0; i < z->scan_n; ++i) {
2947*c217d954SCole Faust int id = stbi__get8(z->s), which;
2948*c217d954SCole Faust int q = stbi__get8(z->s);
2949*c217d954SCole Faust for (which = 0; which < z->s->img_n; ++which)
2950*c217d954SCole Faust if (z->img_comp[which].id == id)
2951*c217d954SCole Faust break;
2952*c217d954SCole Faust if (which == z->s->img_n) return 0; // no match
2953*c217d954SCole Faust z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG");
2954*c217d954SCole Faust z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG");
2955*c217d954SCole Faust z->order[i] = which;
2956*c217d954SCole Faust }
2957*c217d954SCole Faust
2958*c217d954SCole Faust {
2959*c217d954SCole Faust int aa;
2960*c217d954SCole Faust z->spec_start = stbi__get8(z->s);
2961*c217d954SCole Faust z->spec_end = stbi__get8(z->s); // should be 63, but might be 0
2962*c217d954SCole Faust aa = stbi__get8(z->s);
2963*c217d954SCole Faust z->succ_high = (aa >> 4);
2964*c217d954SCole Faust z->succ_low = (aa & 15);
2965*c217d954SCole Faust if (z->progressive) {
2966*c217d954SCole Faust if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13)
2967*c217d954SCole Faust return stbi__err("bad SOS", "Corrupt JPEG");
2968*c217d954SCole Faust } else {
2969*c217d954SCole Faust if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG");
2970*c217d954SCole Faust if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG");
2971*c217d954SCole Faust z->spec_end = 63;
2972*c217d954SCole Faust }
2973*c217d954SCole Faust }
2974*c217d954SCole Faust
2975*c217d954SCole Faust return 1;
2976*c217d954SCole Faust }
2977*c217d954SCole Faust
stbi__free_jpeg_components(stbi__jpeg * z,int ncomp,int why)2978*c217d954SCole Faust static int stbi__free_jpeg_components(stbi__jpeg *z, int ncomp, int why)
2979*c217d954SCole Faust {
2980*c217d954SCole Faust int i;
2981*c217d954SCole Faust for (i=0; i < ncomp; ++i) {
2982*c217d954SCole Faust if (z->img_comp[i].raw_data) {
2983*c217d954SCole Faust STBI_FREE(z->img_comp[i].raw_data);
2984*c217d954SCole Faust z->img_comp[i].raw_data = NULL;
2985*c217d954SCole Faust z->img_comp[i].data = NULL;
2986*c217d954SCole Faust }
2987*c217d954SCole Faust if (z->img_comp[i].raw_coeff) {
2988*c217d954SCole Faust STBI_FREE(z->img_comp[i].raw_coeff);
2989*c217d954SCole Faust z->img_comp[i].raw_coeff = 0;
2990*c217d954SCole Faust z->img_comp[i].coeff = 0;
2991*c217d954SCole Faust }
2992*c217d954SCole Faust if (z->img_comp[i].linebuf) {
2993*c217d954SCole Faust STBI_FREE(z->img_comp[i].linebuf);
2994*c217d954SCole Faust z->img_comp[i].linebuf = NULL;
2995*c217d954SCole Faust }
2996*c217d954SCole Faust }
2997*c217d954SCole Faust return why;
2998*c217d954SCole Faust }
2999*c217d954SCole Faust
stbi__process_frame_header(stbi__jpeg * z,int scan)3000*c217d954SCole Faust static int stbi__process_frame_header(stbi__jpeg *z, int scan)
3001*c217d954SCole Faust {
3002*c217d954SCole Faust stbi__context *s = z->s;
3003*c217d954SCole Faust int Lf,p,i,q, h_max=1,v_max=1,c;
3004*c217d954SCole Faust Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG"); // JPEG
3005*c217d954SCole Faust p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
3006*c217d954SCole Faust 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
3007*c217d954SCole Faust s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires
3008*c217d954SCole Faust c = stbi__get8(s);
3009*c217d954SCole Faust if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG");
3010*c217d954SCole Faust s->img_n = c;
3011*c217d954SCole Faust for (i=0; i < c; ++i) {
3012*c217d954SCole Faust z->img_comp[i].data = NULL;
3013*c217d954SCole Faust z->img_comp[i].linebuf = NULL;
3014*c217d954SCole Faust }
3015*c217d954SCole Faust
3016*c217d954SCole Faust if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG");
3017*c217d954SCole Faust
3018*c217d954SCole Faust z->rgb = 0;
3019*c217d954SCole Faust for (i=0; i < s->img_n; ++i) {
3020*c217d954SCole Faust static const unsigned char rgb[3] = { 'R', 'G', 'B' };
3021*c217d954SCole Faust z->img_comp[i].id = stbi__get8(s);
3022*c217d954SCole Faust if (s->img_n == 3 && z->img_comp[i].id == rgb[i])
3023*c217d954SCole Faust ++z->rgb;
3024*c217d954SCole Faust q = stbi__get8(s);
3025*c217d954SCole Faust 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");
3026*c217d954SCole Faust 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");
3027*c217d954SCole Faust z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG");
3028*c217d954SCole Faust }
3029*c217d954SCole Faust
3030*c217d954SCole Faust if (scan != STBI__SCAN_load) return 1;
3031*c217d954SCole Faust
3032*c217d954SCole Faust if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode");
3033*c217d954SCole Faust
3034*c217d954SCole Faust for (i=0; i < s->img_n; ++i) {
3035*c217d954SCole Faust if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
3036*c217d954SCole Faust if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
3037*c217d954SCole Faust }
3038*c217d954SCole Faust
3039*c217d954SCole Faust // compute interleaved mcu info
3040*c217d954SCole Faust z->img_h_max = h_max;
3041*c217d954SCole Faust z->img_v_max = v_max;
3042*c217d954SCole Faust z->img_mcu_w = h_max * 8;
3043*c217d954SCole Faust z->img_mcu_h = v_max * 8;
3044*c217d954SCole Faust // these sizes can't be more than 17 bits
3045*c217d954SCole Faust z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
3046*c217d954SCole Faust z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
3047*c217d954SCole Faust
3048*c217d954SCole Faust for (i=0; i < s->img_n; ++i) {
3049*c217d954SCole Faust // number of effective pixels (e.g. for non-interleaved MCU)
3050*c217d954SCole Faust z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
3051*c217d954SCole Faust z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
3052*c217d954SCole Faust // to simplify generation, we'll allocate enough memory to decode
3053*c217d954SCole Faust // the bogus oversized data from using interleaved MCUs and their
3054*c217d954SCole Faust // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't
3055*c217d954SCole Faust // discard the extra data until colorspace conversion
3056*c217d954SCole Faust //
3057*c217d954SCole Faust // img_mcu_x, img_mcu_y: <=17 bits; comp[i].h and .v are <=4 (checked earlier)
3058*c217d954SCole Faust // so these muls can't overflow with 32-bit ints (which we require)
3059*c217d954SCole Faust z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
3060*c217d954SCole Faust z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
3061*c217d954SCole Faust z->img_comp[i].coeff = 0;
3062*c217d954SCole Faust z->img_comp[i].raw_coeff = 0;
3063*c217d954SCole Faust z->img_comp[i].linebuf = NULL;
3064*c217d954SCole Faust z->img_comp[i].raw_data = stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15);
3065*c217d954SCole Faust if (z->img_comp[i].raw_data == NULL)
3066*c217d954SCole Faust return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory"));
3067*c217d954SCole Faust // align blocks for idct using mmx/sse
3068*c217d954SCole Faust z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
3069*c217d954SCole Faust if (z->progressive) {
3070*c217d954SCole Faust // w2, h2 are multiples of 8 (see above)
3071*c217d954SCole Faust z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8;
3072*c217d954SCole Faust z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8;
3073*c217d954SCole Faust z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15);
3074*c217d954SCole Faust if (z->img_comp[i].raw_coeff == NULL)
3075*c217d954SCole Faust return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory"));
3076*c217d954SCole Faust z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15);
3077*c217d954SCole Faust }
3078*c217d954SCole Faust }
3079*c217d954SCole Faust
3080*c217d954SCole Faust return 1;
3081*c217d954SCole Faust }
3082*c217d954SCole Faust
3083*c217d954SCole Faust // use comparisons since in some cases we handle more than one case (e.g. SOF)
3084*c217d954SCole Faust #define stbi__DNL(x) ((x) == 0xdc)
3085*c217d954SCole Faust #define stbi__SOI(x) ((x) == 0xd8)
3086*c217d954SCole Faust #define stbi__EOI(x) ((x) == 0xd9)
3087*c217d954SCole Faust #define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2)
3088*c217d954SCole Faust #define stbi__SOS(x) ((x) == 0xda)
3089*c217d954SCole Faust
3090*c217d954SCole Faust #define stbi__SOF_progressive(x) ((x) == 0xc2)
3091*c217d954SCole Faust
stbi__decode_jpeg_header(stbi__jpeg * z,int scan)3092*c217d954SCole Faust static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan)
3093*c217d954SCole Faust {
3094*c217d954SCole Faust int m;
3095*c217d954SCole Faust z->jfif = 0;
3096*c217d954SCole Faust z->app14_color_transform = -1; // valid values are 0,1,2
3097*c217d954SCole Faust z->marker = STBI__MARKER_none; // initialize cached marker to empty
3098*c217d954SCole Faust m = stbi__get_marker(z);
3099*c217d954SCole Faust if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG");
3100*c217d954SCole Faust if (scan == STBI__SCAN_type) return 1;
3101*c217d954SCole Faust m = stbi__get_marker(z);
3102*c217d954SCole Faust while (!stbi__SOF(m)) {
3103*c217d954SCole Faust if (!stbi__process_marker(z,m)) return 0;
3104*c217d954SCole Faust m = stbi__get_marker(z);
3105*c217d954SCole Faust while (m == STBI__MARKER_none) {
3106*c217d954SCole Faust // some files have extra padding after their blocks, so ok, we'll scan
3107*c217d954SCole Faust if (stbi__at_eof(z->s)) return stbi__err("no SOF", "Corrupt JPEG");
3108*c217d954SCole Faust m = stbi__get_marker(z);
3109*c217d954SCole Faust }
3110*c217d954SCole Faust }
3111*c217d954SCole Faust z->progressive = stbi__SOF_progressive(m);
3112*c217d954SCole Faust if (!stbi__process_frame_header(z, scan)) return 0;
3113*c217d954SCole Faust return 1;
3114*c217d954SCole Faust }
3115*c217d954SCole Faust
3116*c217d954SCole Faust // decode image to YCbCr format
stbi__decode_jpeg_image(stbi__jpeg * j)3117*c217d954SCole Faust static int stbi__decode_jpeg_image(stbi__jpeg *j)
3118*c217d954SCole Faust {
3119*c217d954SCole Faust int m;
3120*c217d954SCole Faust for (m = 0; m < 4; m++) {
3121*c217d954SCole Faust j->img_comp[m].raw_data = NULL;
3122*c217d954SCole Faust j->img_comp[m].raw_coeff = NULL;
3123*c217d954SCole Faust }
3124*c217d954SCole Faust j->restart_interval = 0;
3125*c217d954SCole Faust if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0;
3126*c217d954SCole Faust m = stbi__get_marker(j);
3127*c217d954SCole Faust while (!stbi__EOI(m)) {
3128*c217d954SCole Faust if (stbi__SOS(m)) {
3129*c217d954SCole Faust if (!stbi__process_scan_header(j)) return 0;
3130*c217d954SCole Faust if (!stbi__parse_entropy_coded_data(j)) return 0;
3131*c217d954SCole Faust if (j->marker == STBI__MARKER_none ) {
3132*c217d954SCole Faust // handle 0s at the end of image data from IP Kamera 9060
3133*c217d954SCole Faust while (!stbi__at_eof(j->s)) {
3134*c217d954SCole Faust int x = stbi__get8(j->s);
3135*c217d954SCole Faust if (x == 255) {
3136*c217d954SCole Faust j->marker = stbi__get8(j->s);
3137*c217d954SCole Faust break;
3138*c217d954SCole Faust }
3139*c217d954SCole Faust }
3140*c217d954SCole Faust // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0
3141*c217d954SCole Faust }
3142*c217d954SCole Faust } else if (stbi__DNL(m)) {
3143*c217d954SCole Faust int Ld = stbi__get16be(j->s);
3144*c217d954SCole Faust stbi__uint32 NL = stbi__get16be(j->s);
3145*c217d954SCole Faust if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG");
3146*c217d954SCole Faust if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG");
3147*c217d954SCole Faust } else {
3148*c217d954SCole Faust if (!stbi__process_marker(j, m)) return 0;
3149*c217d954SCole Faust }
3150*c217d954SCole Faust m = stbi__get_marker(j);
3151*c217d954SCole Faust }
3152*c217d954SCole Faust if (j->progressive)
3153*c217d954SCole Faust stbi__jpeg_finish(j);
3154*c217d954SCole Faust return 1;
3155*c217d954SCole Faust }
3156*c217d954SCole Faust
3157*c217d954SCole Faust // static jfif-centered resampling (across block boundaries)
3158*c217d954SCole Faust
3159*c217d954SCole Faust typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1,
3160*c217d954SCole Faust int w, int hs);
3161*c217d954SCole Faust
3162*c217d954SCole Faust #define stbi__div4(x) ((stbi_uc) ((x) >> 2))
3163*c217d954SCole Faust
resample_row_1(stbi_uc * out,stbi_uc * in_near,stbi_uc * in_far,int w,int hs)3164*c217d954SCole Faust static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3165*c217d954SCole Faust {
3166*c217d954SCole Faust STBI_NOTUSED(out);
3167*c217d954SCole Faust STBI_NOTUSED(in_far);
3168*c217d954SCole Faust STBI_NOTUSED(w);
3169*c217d954SCole Faust STBI_NOTUSED(hs);
3170*c217d954SCole Faust return in_near;
3171*c217d954SCole Faust }
3172*c217d954SCole Faust
stbi__resample_row_v_2(stbi_uc * out,stbi_uc * in_near,stbi_uc * in_far,int w,int hs)3173*c217d954SCole Faust static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3174*c217d954SCole Faust {
3175*c217d954SCole Faust // need to generate two samples vertically for every one in input
3176*c217d954SCole Faust int i;
3177*c217d954SCole Faust STBI_NOTUSED(hs);
3178*c217d954SCole Faust for (i=0; i < w; ++i)
3179*c217d954SCole Faust out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2);
3180*c217d954SCole Faust return out;
3181*c217d954SCole Faust }
3182*c217d954SCole Faust
stbi__resample_row_h_2(stbi_uc * out,stbi_uc * in_near,stbi_uc * in_far,int w,int hs)3183*c217d954SCole Faust static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3184*c217d954SCole Faust {
3185*c217d954SCole Faust // need to generate two samples horizontally for every one in input
3186*c217d954SCole Faust int i;
3187*c217d954SCole Faust stbi_uc *input = in_near;
3188*c217d954SCole Faust
3189*c217d954SCole Faust if (w == 1) {
3190*c217d954SCole Faust // if only one sample, can't do any interpolation
3191*c217d954SCole Faust out[0] = out[1] = input[0];
3192*c217d954SCole Faust return out;
3193*c217d954SCole Faust }
3194*c217d954SCole Faust
3195*c217d954SCole Faust out[0] = input[0];
3196*c217d954SCole Faust out[1] = stbi__div4(input[0]*3 + input[1] + 2);
3197*c217d954SCole Faust for (i=1; i < w-1; ++i) {
3198*c217d954SCole Faust int n = 3*input[i]+2;
3199*c217d954SCole Faust out[i*2+0] = stbi__div4(n+input[i-1]);
3200*c217d954SCole Faust out[i*2+1] = stbi__div4(n+input[i+1]);
3201*c217d954SCole Faust }
3202*c217d954SCole Faust out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2);
3203*c217d954SCole Faust out[i*2+1] = input[w-1];
3204*c217d954SCole Faust
3205*c217d954SCole Faust STBI_NOTUSED(in_far);
3206*c217d954SCole Faust STBI_NOTUSED(hs);
3207*c217d954SCole Faust
3208*c217d954SCole Faust return out;
3209*c217d954SCole Faust }
3210*c217d954SCole Faust
3211*c217d954SCole Faust #define stbi__div16(x) ((stbi_uc) ((x) >> 4))
3212*c217d954SCole Faust
stbi__resample_row_hv_2(stbi_uc * out,stbi_uc * in_near,stbi_uc * in_far,int w,int hs)3213*c217d954SCole Faust static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3214*c217d954SCole Faust {
3215*c217d954SCole Faust // need to generate 2x2 samples for every one in input
3216*c217d954SCole Faust int i,t0,t1;
3217*c217d954SCole Faust if (w == 1) {
3218*c217d954SCole Faust out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
3219*c217d954SCole Faust return out;
3220*c217d954SCole Faust }
3221*c217d954SCole Faust
3222*c217d954SCole Faust t1 = 3*in_near[0] + in_far[0];
3223*c217d954SCole Faust out[0] = stbi__div4(t1+2);
3224*c217d954SCole Faust for (i=1; i < w; ++i) {
3225*c217d954SCole Faust t0 = t1;
3226*c217d954SCole Faust t1 = 3*in_near[i]+in_far[i];
3227*c217d954SCole Faust out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
3228*c217d954SCole Faust out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
3229*c217d954SCole Faust }
3230*c217d954SCole Faust out[w*2-1] = stbi__div4(t1+2);
3231*c217d954SCole Faust
3232*c217d954SCole Faust STBI_NOTUSED(hs);
3233*c217d954SCole Faust
3234*c217d954SCole Faust return out;
3235*c217d954SCole Faust }
3236*c217d954SCole Faust
3237*c217d954SCole Faust #if defined(STBI_SSE2) || defined(STBI_NEON)
stbi__resample_row_hv_2_simd(stbi_uc * out,stbi_uc * in_near,stbi_uc * in_far,int w,int hs)3238*c217d954SCole Faust static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3239*c217d954SCole Faust {
3240*c217d954SCole Faust // need to generate 2x2 samples for every one in input
3241*c217d954SCole Faust int i=0,t0,t1;
3242*c217d954SCole Faust
3243*c217d954SCole Faust if (w == 1) {
3244*c217d954SCole Faust out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
3245*c217d954SCole Faust return out;
3246*c217d954SCole Faust }
3247*c217d954SCole Faust
3248*c217d954SCole Faust t1 = 3*in_near[0] + in_far[0];
3249*c217d954SCole Faust // process groups of 8 pixels for as long as we can.
3250*c217d954SCole Faust // note we can't handle the last pixel in a row in this loop
3251*c217d954SCole Faust // because we need to handle the filter boundary conditions.
3252*c217d954SCole Faust for (; i < ((w-1) & ~7); i += 8) {
3253*c217d954SCole Faust #if defined(STBI_SSE2)
3254*c217d954SCole Faust // load and perform the vertical filtering pass
3255*c217d954SCole Faust // this uses 3*x + y = 4*x + (y - x)
3256*c217d954SCole Faust __m128i zero = _mm_setzero_si128();
3257*c217d954SCole Faust __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i));
3258*c217d954SCole Faust __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i));
3259*c217d954SCole Faust __m128i farw = _mm_unpacklo_epi8(farb, zero);
3260*c217d954SCole Faust __m128i nearw = _mm_unpacklo_epi8(nearb, zero);
3261*c217d954SCole Faust __m128i diff = _mm_sub_epi16(farw, nearw);
3262*c217d954SCole Faust __m128i nears = _mm_slli_epi16(nearw, 2);
3263*c217d954SCole Faust __m128i curr = _mm_add_epi16(nears, diff); // current row
3264*c217d954SCole Faust
3265*c217d954SCole Faust // horizontal filter works the same based on shifted vers of current
3266*c217d954SCole Faust // row. "prev" is current row shifted right by 1 pixel; we need to
3267*c217d954SCole Faust // insert the previous pixel value (from t1).
3268*c217d954SCole Faust // "next" is current row shifted left by 1 pixel, with first pixel
3269*c217d954SCole Faust // of next block of 8 pixels added in.
3270*c217d954SCole Faust __m128i prv0 = _mm_slli_si128(curr, 2);
3271*c217d954SCole Faust __m128i nxt0 = _mm_srli_si128(curr, 2);
3272*c217d954SCole Faust __m128i prev = _mm_insert_epi16(prv0, t1, 0);
3273*c217d954SCole Faust __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7);
3274*c217d954SCole Faust
3275*c217d954SCole Faust // horizontal filter, polyphase implementation since it's convenient:
3276*c217d954SCole Faust // even pixels = 3*cur + prev = cur*4 + (prev - cur)
3277*c217d954SCole Faust // odd pixels = 3*cur + next = cur*4 + (next - cur)
3278*c217d954SCole Faust // note the shared term.
3279*c217d954SCole Faust __m128i bias = _mm_set1_epi16(8);
3280*c217d954SCole Faust __m128i curs = _mm_slli_epi16(curr, 2);
3281*c217d954SCole Faust __m128i prvd = _mm_sub_epi16(prev, curr);
3282*c217d954SCole Faust __m128i nxtd = _mm_sub_epi16(next, curr);
3283*c217d954SCole Faust __m128i curb = _mm_add_epi16(curs, bias);
3284*c217d954SCole Faust __m128i even = _mm_add_epi16(prvd, curb);
3285*c217d954SCole Faust __m128i odd = _mm_add_epi16(nxtd, curb);
3286*c217d954SCole Faust
3287*c217d954SCole Faust // interleave even and odd pixels, then undo scaling.
3288*c217d954SCole Faust __m128i int0 = _mm_unpacklo_epi16(even, odd);
3289*c217d954SCole Faust __m128i int1 = _mm_unpackhi_epi16(even, odd);
3290*c217d954SCole Faust __m128i de0 = _mm_srli_epi16(int0, 4);
3291*c217d954SCole Faust __m128i de1 = _mm_srli_epi16(int1, 4);
3292*c217d954SCole Faust
3293*c217d954SCole Faust // pack and write output
3294*c217d954SCole Faust __m128i outv = _mm_packus_epi16(de0, de1);
3295*c217d954SCole Faust _mm_storeu_si128((__m128i *) (out + i*2), outv);
3296*c217d954SCole Faust #elif defined(STBI_NEON)
3297*c217d954SCole Faust // load and perform the vertical filtering pass
3298*c217d954SCole Faust // this uses 3*x + y = 4*x + (y - x)
3299*c217d954SCole Faust uint8x8_t farb = vld1_u8(in_far + i);
3300*c217d954SCole Faust uint8x8_t nearb = vld1_u8(in_near + i);
3301*c217d954SCole Faust int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb));
3302*c217d954SCole Faust int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2));
3303*c217d954SCole Faust int16x8_t curr = vaddq_s16(nears, diff); // current row
3304*c217d954SCole Faust
3305*c217d954SCole Faust // horizontal filter works the same based on shifted vers of current
3306*c217d954SCole Faust // row. "prev" is current row shifted right by 1 pixel; we need to
3307*c217d954SCole Faust // insert the previous pixel value (from t1).
3308*c217d954SCole Faust // "next" is current row shifted left by 1 pixel, with first pixel
3309*c217d954SCole Faust // of next block of 8 pixels added in.
3310*c217d954SCole Faust int16x8_t prv0 = vextq_s16(curr, curr, 7);
3311*c217d954SCole Faust int16x8_t nxt0 = vextq_s16(curr, curr, 1);
3312*c217d954SCole Faust int16x8_t prev = vsetq_lane_s16(t1, prv0, 0);
3313*c217d954SCole Faust int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7);
3314*c217d954SCole Faust
3315*c217d954SCole Faust // horizontal filter, polyphase implementation since it's convenient:
3316*c217d954SCole Faust // even pixels = 3*cur + prev = cur*4 + (prev - cur)
3317*c217d954SCole Faust // odd pixels = 3*cur + next = cur*4 + (next - cur)
3318*c217d954SCole Faust // note the shared term.
3319*c217d954SCole Faust int16x8_t curs = vshlq_n_s16(curr, 2);
3320*c217d954SCole Faust int16x8_t prvd = vsubq_s16(prev, curr);
3321*c217d954SCole Faust int16x8_t nxtd = vsubq_s16(next, curr);
3322*c217d954SCole Faust int16x8_t even = vaddq_s16(curs, prvd);
3323*c217d954SCole Faust int16x8_t odd = vaddq_s16(curs, nxtd);
3324*c217d954SCole Faust
3325*c217d954SCole Faust // undo scaling and round, then store with even/odd phases interleaved
3326*c217d954SCole Faust uint8x8x2_t o;
3327*c217d954SCole Faust o.val[0] = vqrshrun_n_s16(even, 4);
3328*c217d954SCole Faust o.val[1] = vqrshrun_n_s16(odd, 4);
3329*c217d954SCole Faust vst2_u8(out + i*2, o);
3330*c217d954SCole Faust #endif
3331*c217d954SCole Faust
3332*c217d954SCole Faust // "previous" value for next iter
3333*c217d954SCole Faust t1 = 3*in_near[i+7] + in_far[i+7];
3334*c217d954SCole Faust }
3335*c217d954SCole Faust
3336*c217d954SCole Faust t0 = t1;
3337*c217d954SCole Faust t1 = 3*in_near[i] + in_far[i];
3338*c217d954SCole Faust out[i*2] = stbi__div16(3*t1 + t0 + 8);
3339*c217d954SCole Faust
3340*c217d954SCole Faust for (++i; i < w; ++i) {
3341*c217d954SCole Faust t0 = t1;
3342*c217d954SCole Faust t1 = 3*in_near[i]+in_far[i];
3343*c217d954SCole Faust out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
3344*c217d954SCole Faust out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
3345*c217d954SCole Faust }
3346*c217d954SCole Faust out[w*2-1] = stbi__div4(t1+2);
3347*c217d954SCole Faust
3348*c217d954SCole Faust STBI_NOTUSED(hs);
3349*c217d954SCole Faust
3350*c217d954SCole Faust return out;
3351*c217d954SCole Faust }
3352*c217d954SCole Faust #endif
3353*c217d954SCole Faust
stbi__resample_row_generic(stbi_uc * out,stbi_uc * in_near,stbi_uc * in_far,int w,int hs)3354*c217d954SCole Faust static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3355*c217d954SCole Faust {
3356*c217d954SCole Faust // resample with nearest-neighbor
3357*c217d954SCole Faust int i,j;
3358*c217d954SCole Faust STBI_NOTUSED(in_far);
3359*c217d954SCole Faust for (i=0; i < w; ++i)
3360*c217d954SCole Faust for (j=0; j < hs; ++j)
3361*c217d954SCole Faust out[i*hs+j] = in_near[i];
3362*c217d954SCole Faust return out;
3363*c217d954SCole Faust }
3364*c217d954SCole Faust
3365*c217d954SCole Faust // this is a reduced-precision calculation of YCbCr-to-RGB introduced
3366*c217d954SCole Faust // to make sure the code produces the same results in both SIMD and scalar
3367*c217d954SCole Faust #define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8)
stbi__YCbCr_to_RGB_row(stbi_uc * out,const stbi_uc * y,const stbi_uc * pcb,const stbi_uc * pcr,int count,int step)3368*c217d954SCole Faust 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)
3369*c217d954SCole Faust {
3370*c217d954SCole Faust int i;
3371*c217d954SCole Faust for (i=0; i < count; ++i) {
3372*c217d954SCole Faust int y_fixed = (y[i] << 20) + (1<<19); // rounding
3373*c217d954SCole Faust int r,g,b;
3374*c217d954SCole Faust int cr = pcr[i] - 128;
3375*c217d954SCole Faust int cb = pcb[i] - 128;
3376*c217d954SCole Faust r = y_fixed + cr* stbi__float2fixed(1.40200f);
3377*c217d954SCole Faust g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000);
3378*c217d954SCole Faust b = y_fixed + cb* stbi__float2fixed(1.77200f);
3379*c217d954SCole Faust r >>= 20;
3380*c217d954SCole Faust g >>= 20;
3381*c217d954SCole Faust b >>= 20;
3382*c217d954SCole Faust if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
3383*c217d954SCole Faust if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
3384*c217d954SCole Faust if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
3385*c217d954SCole Faust out[0] = (stbi_uc)r;
3386*c217d954SCole Faust out[1] = (stbi_uc)g;
3387*c217d954SCole Faust out[2] = (stbi_uc)b;
3388*c217d954SCole Faust out[3] = 255;
3389*c217d954SCole Faust out += step;
3390*c217d954SCole Faust }
3391*c217d954SCole Faust }
3392*c217d954SCole Faust
3393*c217d954SCole Faust #if defined(STBI_SSE2) || defined(STBI_NEON)
stbi__YCbCr_to_RGB_simd(stbi_uc * out,stbi_uc const * y,stbi_uc const * pcb,stbi_uc const * pcr,int count,int step)3394*c217d954SCole Faust 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)
3395*c217d954SCole Faust {
3396*c217d954SCole Faust int i = 0;
3397*c217d954SCole Faust
3398*c217d954SCole Faust #ifdef STBI_SSE2
3399*c217d954SCole Faust // step == 3 is pretty ugly on the final interleave, and i'm not convinced
3400*c217d954SCole Faust // it's useful in practice (you wouldn't use it for textures, for example).
3401*c217d954SCole Faust // so just accelerate step == 4 case.
3402*c217d954SCole Faust if (step == 4) {
3403*c217d954SCole Faust // this is a fairly straightforward implementation and not super-optimized.
3404*c217d954SCole Faust __m128i signflip = _mm_set1_epi8(-0x80);
3405*c217d954SCole Faust __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f));
3406*c217d954SCole Faust __m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f));
3407*c217d954SCole Faust __m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f));
3408*c217d954SCole Faust __m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f));
3409*c217d954SCole Faust __m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128);
3410*c217d954SCole Faust __m128i xw = _mm_set1_epi16(255); // alpha channel
3411*c217d954SCole Faust
3412*c217d954SCole Faust for (; i+7 < count; i += 8) {
3413*c217d954SCole Faust // load
3414*c217d954SCole Faust __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i));
3415*c217d954SCole Faust __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i));
3416*c217d954SCole Faust __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i));
3417*c217d954SCole Faust __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip); // -128
3418*c217d954SCole Faust __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip); // -128
3419*c217d954SCole Faust
3420*c217d954SCole Faust // unpack to short (and left-shift cr, cb by 8)
3421*c217d954SCole Faust __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes);
3422*c217d954SCole Faust __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased);
3423*c217d954SCole Faust __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased);
3424*c217d954SCole Faust
3425*c217d954SCole Faust // color transform
3426*c217d954SCole Faust __m128i yws = _mm_srli_epi16(yw, 4);
3427*c217d954SCole Faust __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw);
3428*c217d954SCole Faust __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw);
3429*c217d954SCole Faust __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1);
3430*c217d954SCole Faust __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1);
3431*c217d954SCole Faust __m128i rws = _mm_add_epi16(cr0, yws);
3432*c217d954SCole Faust __m128i gwt = _mm_add_epi16(cb0, yws);
3433*c217d954SCole Faust __m128i bws = _mm_add_epi16(yws, cb1);
3434*c217d954SCole Faust __m128i gws = _mm_add_epi16(gwt, cr1);
3435*c217d954SCole Faust
3436*c217d954SCole Faust // descale
3437*c217d954SCole Faust __m128i rw = _mm_srai_epi16(rws, 4);
3438*c217d954SCole Faust __m128i bw = _mm_srai_epi16(bws, 4);
3439*c217d954SCole Faust __m128i gw = _mm_srai_epi16(gws, 4);
3440*c217d954SCole Faust
3441*c217d954SCole Faust // back to byte, set up for transpose
3442*c217d954SCole Faust __m128i brb = _mm_packus_epi16(rw, bw);
3443*c217d954SCole Faust __m128i gxb = _mm_packus_epi16(gw, xw);
3444*c217d954SCole Faust
3445*c217d954SCole Faust // transpose to interleave channels
3446*c217d954SCole Faust __m128i t0 = _mm_unpacklo_epi8(brb, gxb);
3447*c217d954SCole Faust __m128i t1 = _mm_unpackhi_epi8(brb, gxb);
3448*c217d954SCole Faust __m128i o0 = _mm_unpacklo_epi16(t0, t1);
3449*c217d954SCole Faust __m128i o1 = _mm_unpackhi_epi16(t0, t1);
3450*c217d954SCole Faust
3451*c217d954SCole Faust // store
3452*c217d954SCole Faust _mm_storeu_si128((__m128i *) (out + 0), o0);
3453*c217d954SCole Faust _mm_storeu_si128((__m128i *) (out + 16), o1);
3454*c217d954SCole Faust out += 32;
3455*c217d954SCole Faust }
3456*c217d954SCole Faust }
3457*c217d954SCole Faust #endif
3458*c217d954SCole Faust
3459*c217d954SCole Faust #ifdef STBI_NEON
3460*c217d954SCole Faust // in this version, step=3 support would be easy to add. but is there demand?
3461*c217d954SCole Faust if (step == 4) {
3462*c217d954SCole Faust // this is a fairly straightforward implementation and not super-optimized.
3463*c217d954SCole Faust uint8x8_t signflip = vdup_n_u8(0x80);
3464*c217d954SCole Faust int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f));
3465*c217d954SCole Faust int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f));
3466*c217d954SCole Faust int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f));
3467*c217d954SCole Faust int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f));
3468*c217d954SCole Faust
3469*c217d954SCole Faust for (; i+7 < count; i += 8) {
3470*c217d954SCole Faust // load
3471*c217d954SCole Faust uint8x8_t y_bytes = vld1_u8(y + i);
3472*c217d954SCole Faust uint8x8_t cr_bytes = vld1_u8(pcr + i);
3473*c217d954SCole Faust uint8x8_t cb_bytes = vld1_u8(pcb + i);
3474*c217d954SCole Faust int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip));
3475*c217d954SCole Faust int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip));
3476*c217d954SCole Faust
3477*c217d954SCole Faust // expand to s16
3478*c217d954SCole Faust int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4));
3479*c217d954SCole Faust int16x8_t crw = vshll_n_s8(cr_biased, 7);
3480*c217d954SCole Faust int16x8_t cbw = vshll_n_s8(cb_biased, 7);
3481*c217d954SCole Faust
3482*c217d954SCole Faust // color transform
3483*c217d954SCole Faust int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0);
3484*c217d954SCole Faust int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0);
3485*c217d954SCole Faust int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1);
3486*c217d954SCole Faust int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1);
3487*c217d954SCole Faust int16x8_t rws = vaddq_s16(yws, cr0);
3488*c217d954SCole Faust int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1);
3489*c217d954SCole Faust int16x8_t bws = vaddq_s16(yws, cb1);
3490*c217d954SCole Faust
3491*c217d954SCole Faust // undo scaling, round, convert to byte
3492*c217d954SCole Faust uint8x8x4_t o;
3493*c217d954SCole Faust o.val[0] = vqrshrun_n_s16(rws, 4);
3494*c217d954SCole Faust o.val[1] = vqrshrun_n_s16(gws, 4);
3495*c217d954SCole Faust o.val[2] = vqrshrun_n_s16(bws, 4);
3496*c217d954SCole Faust o.val[3] = vdup_n_u8(255);
3497*c217d954SCole Faust
3498*c217d954SCole Faust // store, interleaving r/g/b/a
3499*c217d954SCole Faust vst4_u8(out, o);
3500*c217d954SCole Faust out += 8*4;
3501*c217d954SCole Faust }
3502*c217d954SCole Faust }
3503*c217d954SCole Faust #endif
3504*c217d954SCole Faust
3505*c217d954SCole Faust for (; i < count; ++i) {
3506*c217d954SCole Faust int y_fixed = (y[i] << 20) + (1<<19); // rounding
3507*c217d954SCole Faust int r,g,b;
3508*c217d954SCole Faust int cr = pcr[i] - 128;
3509*c217d954SCole Faust int cb = pcb[i] - 128;
3510*c217d954SCole Faust r = y_fixed + cr* stbi__float2fixed(1.40200f);
3511*c217d954SCole Faust g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000);
3512*c217d954SCole Faust b = y_fixed + cb* stbi__float2fixed(1.77200f);
3513*c217d954SCole Faust r >>= 20;
3514*c217d954SCole Faust g >>= 20;
3515*c217d954SCole Faust b >>= 20;
3516*c217d954SCole Faust if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
3517*c217d954SCole Faust if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
3518*c217d954SCole Faust if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
3519*c217d954SCole Faust out[0] = (stbi_uc)r;
3520*c217d954SCole Faust out[1] = (stbi_uc)g;
3521*c217d954SCole Faust out[2] = (stbi_uc)b;
3522*c217d954SCole Faust out[3] = 255;
3523*c217d954SCole Faust out += step;
3524*c217d954SCole Faust }
3525*c217d954SCole Faust }
3526*c217d954SCole Faust #endif
3527*c217d954SCole Faust
3528*c217d954SCole Faust // set up the kernels
stbi__setup_jpeg(stbi__jpeg * j)3529*c217d954SCole Faust static void stbi__setup_jpeg(stbi__jpeg *j)
3530*c217d954SCole Faust {
3531*c217d954SCole Faust j->idct_block_kernel = stbi__idct_block;
3532*c217d954SCole Faust j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row;
3533*c217d954SCole Faust j->resample_row_hv_2_kernel = stbi__resample_row_hv_2;
3534*c217d954SCole Faust
3535*c217d954SCole Faust #ifdef STBI_SSE2
3536*c217d954SCole Faust if (stbi__sse2_available()) {
3537*c217d954SCole Faust j->idct_block_kernel = stbi__idct_simd;
3538*c217d954SCole Faust j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
3539*c217d954SCole Faust j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
3540*c217d954SCole Faust }
3541*c217d954SCole Faust #endif
3542*c217d954SCole Faust
3543*c217d954SCole Faust #ifdef STBI_NEON
3544*c217d954SCole Faust j->idct_block_kernel = stbi__idct_simd;
3545*c217d954SCole Faust j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
3546*c217d954SCole Faust j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
3547*c217d954SCole Faust #endif
3548*c217d954SCole Faust }
3549*c217d954SCole Faust
3550*c217d954SCole Faust // clean up the temporary component buffers
stbi__cleanup_jpeg(stbi__jpeg * j)3551*c217d954SCole Faust static void stbi__cleanup_jpeg(stbi__jpeg *j)
3552*c217d954SCole Faust {
3553*c217d954SCole Faust stbi__free_jpeg_components(j, j->s->img_n, 0);
3554*c217d954SCole Faust }
3555*c217d954SCole Faust
3556*c217d954SCole Faust typedef struct
3557*c217d954SCole Faust {
3558*c217d954SCole Faust resample_row_func resample;
3559*c217d954SCole Faust stbi_uc *line0,*line1;
3560*c217d954SCole Faust int hs,vs; // expansion factor in each axis
3561*c217d954SCole Faust int w_lores; // horizontal pixels pre-expansion
3562*c217d954SCole Faust int ystep; // how far through vertical expansion we are
3563*c217d954SCole Faust int ypos; // which pre-expansion row we're on
3564*c217d954SCole Faust } stbi__resample;
3565*c217d954SCole Faust
3566*c217d954SCole Faust // fast 0..255 * 0..255 => 0..255 rounded multiplication
stbi__blinn_8x8(stbi_uc x,stbi_uc y)3567*c217d954SCole Faust static stbi_uc stbi__blinn_8x8(stbi_uc x, stbi_uc y)
3568*c217d954SCole Faust {
3569*c217d954SCole Faust unsigned int t = x*y + 128;
3570*c217d954SCole Faust return (stbi_uc) ((t + (t >>8)) >> 8);
3571*c217d954SCole Faust }
3572*c217d954SCole Faust
load_jpeg_image(stbi__jpeg * z,int * out_x,int * out_y,int * comp,int req_comp)3573*c217d954SCole Faust static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
3574*c217d954SCole Faust {
3575*c217d954SCole Faust int n, decode_n, is_rgb;
3576*c217d954SCole Faust z->s->img_n = 0; // make stbi__cleanup_jpeg safe
3577*c217d954SCole Faust
3578*c217d954SCole Faust // validate req_comp
3579*c217d954SCole Faust if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
3580*c217d954SCole Faust
3581*c217d954SCole Faust // load a jpeg image from whichever source, but leave in YCbCr format
3582*c217d954SCole Faust if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; }
3583*c217d954SCole Faust
3584*c217d954SCole Faust // determine actual number of components to generate
3585*c217d954SCole Faust n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1;
3586*c217d954SCole Faust
3587*c217d954SCole Faust is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif));
3588*c217d954SCole Faust
3589*c217d954SCole Faust if (z->s->img_n == 3 && n < 3 && !is_rgb)
3590*c217d954SCole Faust decode_n = 1;
3591*c217d954SCole Faust else
3592*c217d954SCole Faust decode_n = z->s->img_n;
3593*c217d954SCole Faust
3594*c217d954SCole Faust // resample and color-convert
3595*c217d954SCole Faust {
3596*c217d954SCole Faust int k;
3597*c217d954SCole Faust unsigned int i,j;
3598*c217d954SCole Faust stbi_uc *output;
3599*c217d954SCole Faust stbi_uc *coutput[4];
3600*c217d954SCole Faust
3601*c217d954SCole Faust stbi__resample res_comp[4];
3602*c217d954SCole Faust
3603*c217d954SCole Faust for (k=0; k < decode_n; ++k) {
3604*c217d954SCole Faust stbi__resample *r = &res_comp[k];
3605*c217d954SCole Faust
3606*c217d954SCole Faust // allocate line buffer big enough for upsampling off the edges
3607*c217d954SCole Faust // with upsample factor of 4
3608*c217d954SCole Faust z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3);
3609*c217d954SCole Faust if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
3610*c217d954SCole Faust
3611*c217d954SCole Faust r->hs = z->img_h_max / z->img_comp[k].h;
3612*c217d954SCole Faust r->vs = z->img_v_max / z->img_comp[k].v;
3613*c217d954SCole Faust r->ystep = r->vs >> 1;
3614*c217d954SCole Faust r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
3615*c217d954SCole Faust r->ypos = 0;
3616*c217d954SCole Faust r->line0 = r->line1 = z->img_comp[k].data;
3617*c217d954SCole Faust
3618*c217d954SCole Faust if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
3619*c217d954SCole Faust else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2;
3620*c217d954SCole Faust else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2;
3621*c217d954SCole Faust else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel;
3622*c217d954SCole Faust else r->resample = stbi__resample_row_generic;
3623*c217d954SCole Faust }
3624*c217d954SCole Faust
3625*c217d954SCole Faust // can't error after this so, this is safe
3626*c217d954SCole Faust output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1);
3627*c217d954SCole Faust if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
3628*c217d954SCole Faust
3629*c217d954SCole Faust // now go ahead and resample
3630*c217d954SCole Faust for (j=0; j < z->s->img_y; ++j) {
3631*c217d954SCole Faust stbi_uc *out = output + n * z->s->img_x * j;
3632*c217d954SCole Faust for (k=0; k < decode_n; ++k) {
3633*c217d954SCole Faust stbi__resample *r = &res_comp[k];
3634*c217d954SCole Faust int y_bot = r->ystep >= (r->vs >> 1);
3635*c217d954SCole Faust coutput[k] = r->resample(z->img_comp[k].linebuf,
3636*c217d954SCole Faust y_bot ? r->line1 : r->line0,
3637*c217d954SCole Faust y_bot ? r->line0 : r->line1,
3638*c217d954SCole Faust r->w_lores, r->hs);
3639*c217d954SCole Faust if (++r->ystep >= r->vs) {
3640*c217d954SCole Faust r->ystep = 0;
3641*c217d954SCole Faust r->line0 = r->line1;
3642*c217d954SCole Faust if (++r->ypos < z->img_comp[k].y)
3643*c217d954SCole Faust r->line1 += z->img_comp[k].w2;
3644*c217d954SCole Faust }
3645*c217d954SCole Faust }
3646*c217d954SCole Faust if (n >= 3) {
3647*c217d954SCole Faust stbi_uc *y = coutput[0];
3648*c217d954SCole Faust if (z->s->img_n == 3) {
3649*c217d954SCole Faust if (is_rgb) {
3650*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i) {
3651*c217d954SCole Faust out[0] = y[i];
3652*c217d954SCole Faust out[1] = coutput[1][i];
3653*c217d954SCole Faust out[2] = coutput[2][i];
3654*c217d954SCole Faust out[3] = 255;
3655*c217d954SCole Faust out += n;
3656*c217d954SCole Faust }
3657*c217d954SCole Faust } else {
3658*c217d954SCole Faust z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
3659*c217d954SCole Faust }
3660*c217d954SCole Faust } else if (z->s->img_n == 4) {
3661*c217d954SCole Faust if (z->app14_color_transform == 0) { // CMYK
3662*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i) {
3663*c217d954SCole Faust stbi_uc m = coutput[3][i];
3664*c217d954SCole Faust out[0] = stbi__blinn_8x8(coutput[0][i], m);
3665*c217d954SCole Faust out[1] = stbi__blinn_8x8(coutput[1][i], m);
3666*c217d954SCole Faust out[2] = stbi__blinn_8x8(coutput[2][i], m);
3667*c217d954SCole Faust out[3] = 255;
3668*c217d954SCole Faust out += n;
3669*c217d954SCole Faust }
3670*c217d954SCole Faust } else if (z->app14_color_transform == 2) { // YCCK
3671*c217d954SCole Faust z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
3672*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i) {
3673*c217d954SCole Faust stbi_uc m = coutput[3][i];
3674*c217d954SCole Faust out[0] = stbi__blinn_8x8(255 - out[0], m);
3675*c217d954SCole Faust out[1] = stbi__blinn_8x8(255 - out[1], m);
3676*c217d954SCole Faust out[2] = stbi__blinn_8x8(255 - out[2], m);
3677*c217d954SCole Faust out += n;
3678*c217d954SCole Faust }
3679*c217d954SCole Faust } else { // YCbCr + alpha? Ignore the fourth channel for now
3680*c217d954SCole Faust z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
3681*c217d954SCole Faust }
3682*c217d954SCole Faust } else
3683*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i) {
3684*c217d954SCole Faust out[0] = out[1] = out[2] = y[i];
3685*c217d954SCole Faust out[3] = 255; // not used if n==3
3686*c217d954SCole Faust out += n;
3687*c217d954SCole Faust }
3688*c217d954SCole Faust } else {
3689*c217d954SCole Faust if (is_rgb) {
3690*c217d954SCole Faust if (n == 1)
3691*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i)
3692*c217d954SCole Faust *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);
3693*c217d954SCole Faust else {
3694*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i, out += 2) {
3695*c217d954SCole Faust out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);
3696*c217d954SCole Faust out[1] = 255;
3697*c217d954SCole Faust }
3698*c217d954SCole Faust }
3699*c217d954SCole Faust } else if (z->s->img_n == 4 && z->app14_color_transform == 0) {
3700*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i) {
3701*c217d954SCole Faust stbi_uc m = coutput[3][i];
3702*c217d954SCole Faust stbi_uc r = stbi__blinn_8x8(coutput[0][i], m);
3703*c217d954SCole Faust stbi_uc g = stbi__blinn_8x8(coutput[1][i], m);
3704*c217d954SCole Faust stbi_uc b = stbi__blinn_8x8(coutput[2][i], m);
3705*c217d954SCole Faust out[0] = stbi__compute_y(r, g, b);
3706*c217d954SCole Faust out[1] = 255;
3707*c217d954SCole Faust out += n;
3708*c217d954SCole Faust }
3709*c217d954SCole Faust } else if (z->s->img_n == 4 && z->app14_color_transform == 2) {
3710*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i) {
3711*c217d954SCole Faust out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]);
3712*c217d954SCole Faust out[1] = 255;
3713*c217d954SCole Faust out += n;
3714*c217d954SCole Faust }
3715*c217d954SCole Faust } else {
3716*c217d954SCole Faust stbi_uc *y = coutput[0];
3717*c217d954SCole Faust if (n == 1)
3718*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
3719*c217d954SCole Faust else
3720*c217d954SCole Faust for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
3721*c217d954SCole Faust }
3722*c217d954SCole Faust }
3723*c217d954SCole Faust }
3724*c217d954SCole Faust stbi__cleanup_jpeg(z);
3725*c217d954SCole Faust *out_x = z->s->img_x;
3726*c217d954SCole Faust *out_y = z->s->img_y;
3727*c217d954SCole Faust if (comp) *comp = z->s->img_n >= 3 ? 3 : 1; // report original components, not output
3728*c217d954SCole Faust return output;
3729*c217d954SCole Faust }
3730*c217d954SCole Faust }
3731*c217d954SCole Faust
stbi__jpeg_load(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri)3732*c217d954SCole Faust static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
3733*c217d954SCole Faust {
3734*c217d954SCole Faust unsigned char* result;
3735*c217d954SCole Faust stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg));
3736*c217d954SCole Faust STBI_NOTUSED(ri);
3737*c217d954SCole Faust j->s = s;
3738*c217d954SCole Faust stbi__setup_jpeg(j);
3739*c217d954SCole Faust result = load_jpeg_image(j, x,y,comp,req_comp);
3740*c217d954SCole Faust STBI_FREE(j);
3741*c217d954SCole Faust return result;
3742*c217d954SCole Faust }
3743*c217d954SCole Faust
stbi__jpeg_test(stbi__context * s)3744*c217d954SCole Faust static int stbi__jpeg_test(stbi__context *s)
3745*c217d954SCole Faust {
3746*c217d954SCole Faust int r;
3747*c217d954SCole Faust stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg));
3748*c217d954SCole Faust j->s = s;
3749*c217d954SCole Faust stbi__setup_jpeg(j);
3750*c217d954SCole Faust r = stbi__decode_jpeg_header(j, STBI__SCAN_type);
3751*c217d954SCole Faust stbi__rewind(s);
3752*c217d954SCole Faust STBI_FREE(j);
3753*c217d954SCole Faust return r;
3754*c217d954SCole Faust }
3755*c217d954SCole Faust
stbi__jpeg_info_raw(stbi__jpeg * j,int * x,int * y,int * comp)3756*c217d954SCole Faust static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp)
3757*c217d954SCole Faust {
3758*c217d954SCole Faust if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) {
3759*c217d954SCole Faust stbi__rewind( j->s );
3760*c217d954SCole Faust return 0;
3761*c217d954SCole Faust }
3762*c217d954SCole Faust if (x) *x = j->s->img_x;
3763*c217d954SCole Faust if (y) *y = j->s->img_y;
3764*c217d954SCole Faust if (comp) *comp = j->s->img_n >= 3 ? 3 : 1;
3765*c217d954SCole Faust return 1;
3766*c217d954SCole Faust }
3767*c217d954SCole Faust
stbi__jpeg_info(stbi__context * s,int * x,int * y,int * comp)3768*c217d954SCole Faust static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp)
3769*c217d954SCole Faust {
3770*c217d954SCole Faust int result;
3771*c217d954SCole Faust stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg)));
3772*c217d954SCole Faust j->s = s;
3773*c217d954SCole Faust result = stbi__jpeg_info_raw(j, x, y, comp);
3774*c217d954SCole Faust STBI_FREE(j);
3775*c217d954SCole Faust return result;
3776*c217d954SCole Faust }
3777*c217d954SCole Faust #endif
3778*c217d954SCole Faust
3779*c217d954SCole Faust // public domain zlib decode v0.2 Sean Barrett 2006-11-18
3780*c217d954SCole Faust // simple implementation
3781*c217d954SCole Faust // - all input must be provided in an upfront buffer
3782*c217d954SCole Faust // - all output is written to a single output buffer (can malloc/realloc)
3783*c217d954SCole Faust // performance
3784*c217d954SCole Faust // - fast huffman
3785*c217d954SCole Faust
3786*c217d954SCole Faust #ifndef STBI_NO_ZLIB
3787*c217d954SCole Faust
3788*c217d954SCole Faust // fast-way is faster to check than jpeg huffman, but slow way is slower
3789*c217d954SCole Faust #define STBI__ZFAST_BITS 9 // accelerate all cases in default tables
3790*c217d954SCole Faust #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
3791*c217d954SCole Faust
3792*c217d954SCole Faust // zlib-style huffman encoding
3793*c217d954SCole Faust // (jpegs packs from left, zlib from right, so can't share code)
3794*c217d954SCole Faust typedef struct
3795*c217d954SCole Faust {
3796*c217d954SCole Faust stbi__uint16 fast[1 << STBI__ZFAST_BITS];
3797*c217d954SCole Faust stbi__uint16 firstcode[16];
3798*c217d954SCole Faust int maxcode[17];
3799*c217d954SCole Faust stbi__uint16 firstsymbol[16];
3800*c217d954SCole Faust stbi_uc size[288];
3801*c217d954SCole Faust stbi__uint16 value[288];
3802*c217d954SCole Faust } stbi__zhuffman;
3803*c217d954SCole Faust
stbi__bitreverse16(int n)3804*c217d954SCole Faust stbi_inline static int stbi__bitreverse16(int n)
3805*c217d954SCole Faust {
3806*c217d954SCole Faust n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
3807*c217d954SCole Faust n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
3808*c217d954SCole Faust n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
3809*c217d954SCole Faust n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
3810*c217d954SCole Faust return n;
3811*c217d954SCole Faust }
3812*c217d954SCole Faust
stbi__bit_reverse(int v,int bits)3813*c217d954SCole Faust stbi_inline static int stbi__bit_reverse(int v, int bits)
3814*c217d954SCole Faust {
3815*c217d954SCole Faust STBI_ASSERT(bits <= 16);
3816*c217d954SCole Faust // to bit reverse n bits, reverse 16 and shift
3817*c217d954SCole Faust // e.g. 11 bits, bit reverse and shift away 5
3818*c217d954SCole Faust return stbi__bitreverse16(v) >> (16-bits);
3819*c217d954SCole Faust }
3820*c217d954SCole Faust
stbi__zbuild_huffman(stbi__zhuffman * z,const stbi_uc * sizelist,int num)3821*c217d954SCole Faust static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num)
3822*c217d954SCole Faust {
3823*c217d954SCole Faust int i,k=0;
3824*c217d954SCole Faust int code, next_code[16], sizes[17];
3825*c217d954SCole Faust
3826*c217d954SCole Faust // DEFLATE spec for generating codes
3827*c217d954SCole Faust memset(sizes, 0, sizeof(sizes));
3828*c217d954SCole Faust memset(z->fast, 0, sizeof(z->fast));
3829*c217d954SCole Faust for (i=0; i < num; ++i)
3830*c217d954SCole Faust ++sizes[sizelist[i]];
3831*c217d954SCole Faust sizes[0] = 0;
3832*c217d954SCole Faust for (i=1; i < 16; ++i)
3833*c217d954SCole Faust if (sizes[i] > (1 << i))
3834*c217d954SCole Faust return stbi__err("bad sizes", "Corrupt PNG");
3835*c217d954SCole Faust code = 0;
3836*c217d954SCole Faust for (i=1; i < 16; ++i) {
3837*c217d954SCole Faust next_code[i] = code;
3838*c217d954SCole Faust z->firstcode[i] = (stbi__uint16) code;
3839*c217d954SCole Faust z->firstsymbol[i] = (stbi__uint16) k;
3840*c217d954SCole Faust code = (code + sizes[i]);
3841*c217d954SCole Faust if (sizes[i])
3842*c217d954SCole Faust if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG");
3843*c217d954SCole Faust z->maxcode[i] = code << (16-i); // preshift for inner loop
3844*c217d954SCole Faust code <<= 1;
3845*c217d954SCole Faust k += sizes[i];
3846*c217d954SCole Faust }
3847*c217d954SCole Faust z->maxcode[16] = 0x10000; // sentinel
3848*c217d954SCole Faust for (i=0; i < num; ++i) {
3849*c217d954SCole Faust int s = sizelist[i];
3850*c217d954SCole Faust if (s) {
3851*c217d954SCole Faust int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
3852*c217d954SCole Faust stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i);
3853*c217d954SCole Faust z->size [c] = (stbi_uc ) s;
3854*c217d954SCole Faust z->value[c] = (stbi__uint16) i;
3855*c217d954SCole Faust if (s <= STBI__ZFAST_BITS) {
3856*c217d954SCole Faust int j = stbi__bit_reverse(next_code[s],s);
3857*c217d954SCole Faust while (j < (1 << STBI__ZFAST_BITS)) {
3858*c217d954SCole Faust z->fast[j] = fastv;
3859*c217d954SCole Faust j += (1 << s);
3860*c217d954SCole Faust }
3861*c217d954SCole Faust }
3862*c217d954SCole Faust ++next_code[s];
3863*c217d954SCole Faust }
3864*c217d954SCole Faust }
3865*c217d954SCole Faust return 1;
3866*c217d954SCole Faust }
3867*c217d954SCole Faust
3868*c217d954SCole Faust // zlib-from-memory implementation for PNG reading
3869*c217d954SCole Faust // because PNG allows splitting the zlib stream arbitrarily,
3870*c217d954SCole Faust // and it's annoying structurally to have PNG call ZLIB call PNG,
3871*c217d954SCole Faust // we require PNG read all the IDATs and combine them into a single
3872*c217d954SCole Faust // memory buffer
3873*c217d954SCole Faust
3874*c217d954SCole Faust typedef struct
3875*c217d954SCole Faust {
3876*c217d954SCole Faust stbi_uc *zbuffer, *zbuffer_end;
3877*c217d954SCole Faust int num_bits;
3878*c217d954SCole Faust stbi__uint32 code_buffer;
3879*c217d954SCole Faust
3880*c217d954SCole Faust char *zout;
3881*c217d954SCole Faust char *zout_start;
3882*c217d954SCole Faust char *zout_end;
3883*c217d954SCole Faust int z_expandable;
3884*c217d954SCole Faust
3885*c217d954SCole Faust stbi__zhuffman z_length, z_distance;
3886*c217d954SCole Faust } stbi__zbuf;
3887*c217d954SCole Faust
stbi__zget8(stbi__zbuf * z)3888*c217d954SCole Faust stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z)
3889*c217d954SCole Faust {
3890*c217d954SCole Faust if (z->zbuffer >= z->zbuffer_end) return 0;
3891*c217d954SCole Faust return *z->zbuffer++;
3892*c217d954SCole Faust }
3893*c217d954SCole Faust
stbi__fill_bits(stbi__zbuf * z)3894*c217d954SCole Faust static void stbi__fill_bits(stbi__zbuf *z)
3895*c217d954SCole Faust {
3896*c217d954SCole Faust do {
3897*c217d954SCole Faust STBI_ASSERT(z->code_buffer < (1U << z->num_bits));
3898*c217d954SCole Faust z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits;
3899*c217d954SCole Faust z->num_bits += 8;
3900*c217d954SCole Faust } while (z->num_bits <= 24);
3901*c217d954SCole Faust }
3902*c217d954SCole Faust
stbi__zreceive(stbi__zbuf * z,int n)3903*c217d954SCole Faust stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n)
3904*c217d954SCole Faust {
3905*c217d954SCole Faust unsigned int k;
3906*c217d954SCole Faust if (z->num_bits < n) stbi__fill_bits(z);
3907*c217d954SCole Faust k = z->code_buffer & ((1 << n) - 1);
3908*c217d954SCole Faust z->code_buffer >>= n;
3909*c217d954SCole Faust z->num_bits -= n;
3910*c217d954SCole Faust return k;
3911*c217d954SCole Faust }
3912*c217d954SCole Faust
stbi__zhuffman_decode_slowpath(stbi__zbuf * a,stbi__zhuffman * z)3913*c217d954SCole Faust static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
3914*c217d954SCole Faust {
3915*c217d954SCole Faust int b,s,k;
3916*c217d954SCole Faust // not resolved by fast table, so compute it the slow way
3917*c217d954SCole Faust // use jpeg approach, which requires MSbits at top
3918*c217d954SCole Faust k = stbi__bit_reverse(a->code_buffer, 16);
3919*c217d954SCole Faust for (s=STBI__ZFAST_BITS+1; ; ++s)
3920*c217d954SCole Faust if (k < z->maxcode[s])
3921*c217d954SCole Faust break;
3922*c217d954SCole Faust if (s == 16) return -1; // invalid code!
3923*c217d954SCole Faust // code size is s, so:
3924*c217d954SCole Faust b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
3925*c217d954SCole Faust STBI_ASSERT(z->size[b] == s);
3926*c217d954SCole Faust a->code_buffer >>= s;
3927*c217d954SCole Faust a->num_bits -= s;
3928*c217d954SCole Faust return z->value[b];
3929*c217d954SCole Faust }
3930*c217d954SCole Faust
stbi__zhuffman_decode(stbi__zbuf * a,stbi__zhuffman * z)3931*c217d954SCole Faust stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
3932*c217d954SCole Faust {
3933*c217d954SCole Faust int b,s;
3934*c217d954SCole Faust if (a->num_bits < 16) stbi__fill_bits(a);
3935*c217d954SCole Faust b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
3936*c217d954SCole Faust if (b) {
3937*c217d954SCole Faust s = b >> 9;
3938*c217d954SCole Faust a->code_buffer >>= s;
3939*c217d954SCole Faust a->num_bits -= s;
3940*c217d954SCole Faust return b & 511;
3941*c217d954SCole Faust }
3942*c217d954SCole Faust return stbi__zhuffman_decode_slowpath(a, z);
3943*c217d954SCole Faust }
3944*c217d954SCole Faust
stbi__zexpand(stbi__zbuf * z,char * zout,int n)3945*c217d954SCole Faust static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes
3946*c217d954SCole Faust {
3947*c217d954SCole Faust char *q;
3948*c217d954SCole Faust int cur, limit, old_limit;
3949*c217d954SCole Faust z->zout = zout;
3950*c217d954SCole Faust if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
3951*c217d954SCole Faust cur = (int) (z->zout - z->zout_start);
3952*c217d954SCole Faust limit = old_limit = (int) (z->zout_end - z->zout_start);
3953*c217d954SCole Faust while (cur + n > limit)
3954*c217d954SCole Faust limit *= 2;
3955*c217d954SCole Faust q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit);
3956*c217d954SCole Faust STBI_NOTUSED(old_limit);
3957*c217d954SCole Faust if (q == NULL) return stbi__err("outofmem", "Out of memory");
3958*c217d954SCole Faust z->zout_start = q;
3959*c217d954SCole Faust z->zout = q + cur;
3960*c217d954SCole Faust z->zout_end = q + limit;
3961*c217d954SCole Faust return 1;
3962*c217d954SCole Faust }
3963*c217d954SCole Faust
3964*c217d954SCole Faust static const int stbi__zlength_base[31] = {
3965*c217d954SCole Faust 3,4,5,6,7,8,9,10,11,13,
3966*c217d954SCole Faust 15,17,19,23,27,31,35,43,51,59,
3967*c217d954SCole Faust 67,83,99,115,131,163,195,227,258,0,0 };
3968*c217d954SCole Faust
3969*c217d954SCole Faust static const int stbi__zlength_extra[31]=
3970*c217d954SCole Faust { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
3971*c217d954SCole Faust
3972*c217d954SCole Faust static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
3973*c217d954SCole Faust 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
3974*c217d954SCole Faust
3975*c217d954SCole Faust static const int stbi__zdist_extra[32] =
3976*c217d954SCole Faust { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
3977*c217d954SCole Faust
stbi__parse_huffman_block(stbi__zbuf * a)3978*c217d954SCole Faust static int stbi__parse_huffman_block(stbi__zbuf *a)
3979*c217d954SCole Faust {
3980*c217d954SCole Faust char *zout = a->zout;
3981*c217d954SCole Faust for(;;) {
3982*c217d954SCole Faust int z = stbi__zhuffman_decode(a, &a->z_length);
3983*c217d954SCole Faust if (z < 256) {
3984*c217d954SCole Faust if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes
3985*c217d954SCole Faust if (zout >= a->zout_end) {
3986*c217d954SCole Faust if (!stbi__zexpand(a, zout, 1)) return 0;
3987*c217d954SCole Faust zout = a->zout;
3988*c217d954SCole Faust }
3989*c217d954SCole Faust *zout++ = (char) z;
3990*c217d954SCole Faust } else {
3991*c217d954SCole Faust stbi_uc *p;
3992*c217d954SCole Faust int len,dist;
3993*c217d954SCole Faust if (z == 256) {
3994*c217d954SCole Faust a->zout = zout;
3995*c217d954SCole Faust return 1;
3996*c217d954SCole Faust }
3997*c217d954SCole Faust z -= 257;
3998*c217d954SCole Faust len = stbi__zlength_base[z];
3999*c217d954SCole Faust if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
4000*c217d954SCole Faust z = stbi__zhuffman_decode(a, &a->z_distance);
4001*c217d954SCole Faust if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");
4002*c217d954SCole Faust dist = stbi__zdist_base[z];
4003*c217d954SCole Faust if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
4004*c217d954SCole Faust if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");
4005*c217d954SCole Faust if (zout + len > a->zout_end) {
4006*c217d954SCole Faust if (!stbi__zexpand(a, zout, len)) return 0;
4007*c217d954SCole Faust zout = a->zout;
4008*c217d954SCole Faust }
4009*c217d954SCole Faust p = (stbi_uc *) (zout - dist);
4010*c217d954SCole Faust if (dist == 1) { // run of one byte; common in images.
4011*c217d954SCole Faust stbi_uc v = *p;
4012*c217d954SCole Faust if (len) { do *zout++ = v; while (--len); }
4013*c217d954SCole Faust } else {
4014*c217d954SCole Faust if (len) { do *zout++ = *p++; while (--len); }
4015*c217d954SCole Faust }
4016*c217d954SCole Faust }
4017*c217d954SCole Faust }
4018*c217d954SCole Faust }
4019*c217d954SCole Faust
stbi__compute_huffman_codes(stbi__zbuf * a)4020*c217d954SCole Faust static int stbi__compute_huffman_codes(stbi__zbuf *a)
4021*c217d954SCole Faust {
4022*c217d954SCole Faust static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
4023*c217d954SCole Faust stbi__zhuffman z_codelength;
4024*c217d954SCole Faust stbi_uc lencodes[286+32+137];//padding for maximum single op
4025*c217d954SCole Faust stbi_uc codelength_sizes[19];
4026*c217d954SCole Faust int i,n;
4027*c217d954SCole Faust
4028*c217d954SCole Faust int hlit = stbi__zreceive(a,5) + 257;
4029*c217d954SCole Faust int hdist = stbi__zreceive(a,5) + 1;
4030*c217d954SCole Faust int hclen = stbi__zreceive(a,4) + 4;
4031*c217d954SCole Faust int ntot = hlit + hdist;
4032*c217d954SCole Faust
4033*c217d954SCole Faust memset(codelength_sizes, 0, sizeof(codelength_sizes));
4034*c217d954SCole Faust for (i=0; i < hclen; ++i) {
4035*c217d954SCole Faust int s = stbi__zreceive(a,3);
4036*c217d954SCole Faust codelength_sizes[length_dezigzag[i]] = (stbi_uc) s;
4037*c217d954SCole Faust }
4038*c217d954SCole Faust if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
4039*c217d954SCole Faust
4040*c217d954SCole Faust n = 0;
4041*c217d954SCole Faust while (n < ntot) {
4042*c217d954SCole Faust int c = stbi__zhuffman_decode(a, &z_codelength);
4043*c217d954SCole Faust if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG");
4044*c217d954SCole Faust if (c < 16)
4045*c217d954SCole Faust lencodes[n++] = (stbi_uc) c;
4046*c217d954SCole Faust else {
4047*c217d954SCole Faust stbi_uc fill = 0;
4048*c217d954SCole Faust if (c == 16) {
4049*c217d954SCole Faust c = stbi__zreceive(a,2)+3;
4050*c217d954SCole Faust if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG");
4051*c217d954SCole Faust fill = lencodes[n-1];
4052*c217d954SCole Faust } else if (c == 17)
4053*c217d954SCole Faust c = stbi__zreceive(a,3)+3;
4054*c217d954SCole Faust else {
4055*c217d954SCole Faust STBI_ASSERT(c == 18);
4056*c217d954SCole Faust c = stbi__zreceive(a,7)+11;
4057*c217d954SCole Faust }
4058*c217d954SCole Faust if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG");
4059*c217d954SCole Faust memset(lencodes+n, fill, c);
4060*c217d954SCole Faust n += c;
4061*c217d954SCole Faust }
4062*c217d954SCole Faust }
4063*c217d954SCole Faust if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG");
4064*c217d954SCole Faust if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
4065*c217d954SCole Faust if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
4066*c217d954SCole Faust return 1;
4067*c217d954SCole Faust }
4068*c217d954SCole Faust
stbi__parse_uncompressed_block(stbi__zbuf * a)4069*c217d954SCole Faust static int stbi__parse_uncompressed_block(stbi__zbuf *a)
4070*c217d954SCole Faust {
4071*c217d954SCole Faust stbi_uc header[4];
4072*c217d954SCole Faust int len,nlen,k;
4073*c217d954SCole Faust if (a->num_bits & 7)
4074*c217d954SCole Faust stbi__zreceive(a, a->num_bits & 7); // discard
4075*c217d954SCole Faust // drain the bit-packed data into header
4076*c217d954SCole Faust k = 0;
4077*c217d954SCole Faust while (a->num_bits > 0) {
4078*c217d954SCole Faust header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check
4079*c217d954SCole Faust a->code_buffer >>= 8;
4080*c217d954SCole Faust a->num_bits -= 8;
4081*c217d954SCole Faust }
4082*c217d954SCole Faust STBI_ASSERT(a->num_bits == 0);
4083*c217d954SCole Faust // now fill header the normal way
4084*c217d954SCole Faust while (k < 4)
4085*c217d954SCole Faust header[k++] = stbi__zget8(a);
4086*c217d954SCole Faust len = header[1] * 256 + header[0];
4087*c217d954SCole Faust nlen = header[3] * 256 + header[2];
4088*c217d954SCole Faust if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG");
4089*c217d954SCole Faust if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG");
4090*c217d954SCole Faust if (a->zout + len > a->zout_end)
4091*c217d954SCole Faust if (!stbi__zexpand(a, a->zout, len)) return 0;
4092*c217d954SCole Faust memcpy(a->zout, a->zbuffer, len);
4093*c217d954SCole Faust a->zbuffer += len;
4094*c217d954SCole Faust a->zout += len;
4095*c217d954SCole Faust return 1;
4096*c217d954SCole Faust }
4097*c217d954SCole Faust
stbi__parse_zlib_header(stbi__zbuf * a)4098*c217d954SCole Faust static int stbi__parse_zlib_header(stbi__zbuf *a)
4099*c217d954SCole Faust {
4100*c217d954SCole Faust int cmf = stbi__zget8(a);
4101*c217d954SCole Faust int cm = cmf & 15;
4102*c217d954SCole Faust /* int cinfo = cmf >> 4; */
4103*c217d954SCole Faust int flg = stbi__zget8(a);
4104*c217d954SCole Faust if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec
4105*c217d954SCole Faust if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
4106*c217d954SCole Faust if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png
4107*c217d954SCole Faust // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
4108*c217d954SCole Faust return 1;
4109*c217d954SCole Faust }
4110*c217d954SCole Faust
4111*c217d954SCole Faust static const stbi_uc stbi__zdefault_length[288] =
4112*c217d954SCole Faust {
4113*c217d954SCole Faust 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4114*c217d954SCole Faust 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4115*c217d954SCole Faust 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4116*c217d954SCole Faust 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4117*c217d954SCole Faust 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
4118*c217d954SCole Faust 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
4119*c217d954SCole Faust 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
4120*c217d954SCole Faust 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
4121*c217d954SCole Faust 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8
4122*c217d954SCole Faust };
4123*c217d954SCole Faust static const stbi_uc stbi__zdefault_distance[32] =
4124*c217d954SCole Faust {
4125*c217d954SCole Faust 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
4126*c217d954SCole Faust };
4127*c217d954SCole Faust /*
4128*c217d954SCole Faust Init algorithm:
4129*c217d954SCole Faust {
4130*c217d954SCole Faust int i; // use <= to match clearly with spec
4131*c217d954SCole Faust for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8;
4132*c217d954SCole Faust for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9;
4133*c217d954SCole Faust for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7;
4134*c217d954SCole Faust for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8;
4135*c217d954SCole Faust
4136*c217d954SCole Faust for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5;
4137*c217d954SCole Faust }
4138*c217d954SCole Faust */
4139*c217d954SCole Faust
stbi__parse_zlib(stbi__zbuf * a,int parse_header)4140*c217d954SCole Faust static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
4141*c217d954SCole Faust {
4142*c217d954SCole Faust int final, type;
4143*c217d954SCole Faust if (parse_header)
4144*c217d954SCole Faust if (!stbi__parse_zlib_header(a)) return 0;
4145*c217d954SCole Faust a->num_bits = 0;
4146*c217d954SCole Faust a->code_buffer = 0;
4147*c217d954SCole Faust do {
4148*c217d954SCole Faust final = stbi__zreceive(a,1);
4149*c217d954SCole Faust type = stbi__zreceive(a,2);
4150*c217d954SCole Faust if (type == 0) {
4151*c217d954SCole Faust if (!stbi__parse_uncompressed_block(a)) return 0;
4152*c217d954SCole Faust } else if (type == 3) {
4153*c217d954SCole Faust return 0;
4154*c217d954SCole Faust } else {
4155*c217d954SCole Faust if (type == 1) {
4156*c217d954SCole Faust // use fixed code lengths
4157*c217d954SCole Faust if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0;
4158*c217d954SCole Faust if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
4159*c217d954SCole Faust } else {
4160*c217d954SCole Faust if (!stbi__compute_huffman_codes(a)) return 0;
4161*c217d954SCole Faust }
4162*c217d954SCole Faust if (!stbi__parse_huffman_block(a)) return 0;
4163*c217d954SCole Faust }
4164*c217d954SCole Faust } while (!final);
4165*c217d954SCole Faust return 1;
4166*c217d954SCole Faust }
4167*c217d954SCole Faust
stbi__do_zlib(stbi__zbuf * a,char * obuf,int olen,int exp,int parse_header)4168*c217d954SCole Faust static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header)
4169*c217d954SCole Faust {
4170*c217d954SCole Faust a->zout_start = obuf;
4171*c217d954SCole Faust a->zout = obuf;
4172*c217d954SCole Faust a->zout_end = obuf + olen;
4173*c217d954SCole Faust a->z_expandable = exp;
4174*c217d954SCole Faust
4175*c217d954SCole Faust return stbi__parse_zlib(a, parse_header);
4176*c217d954SCole Faust }
4177*c217d954SCole Faust
stbi_zlib_decode_malloc_guesssize(const char * buffer,int len,int initial_size,int * outlen)4178*c217d954SCole Faust STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
4179*c217d954SCole Faust {
4180*c217d954SCole Faust stbi__zbuf a;
4181*c217d954SCole Faust char *p = (char *) stbi__malloc(initial_size);
4182*c217d954SCole Faust if (p == NULL) return NULL;
4183*c217d954SCole Faust a.zbuffer = (stbi_uc *) buffer;
4184*c217d954SCole Faust a.zbuffer_end = (stbi_uc *) buffer + len;
4185*c217d954SCole Faust if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
4186*c217d954SCole Faust if (outlen) *outlen = (int) (a.zout - a.zout_start);
4187*c217d954SCole Faust return a.zout_start;
4188*c217d954SCole Faust } else {
4189*c217d954SCole Faust STBI_FREE(a.zout_start);
4190*c217d954SCole Faust return NULL;
4191*c217d954SCole Faust }
4192*c217d954SCole Faust }
4193*c217d954SCole Faust
stbi_zlib_decode_malloc(char const * buffer,int len,int * outlen)4194*c217d954SCole Faust STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
4195*c217d954SCole Faust {
4196*c217d954SCole Faust return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
4197*c217d954SCole Faust }
4198*c217d954SCole Faust
stbi_zlib_decode_malloc_guesssize_headerflag(const char * buffer,int len,int initial_size,int * outlen,int parse_header)4199*c217d954SCole Faust STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
4200*c217d954SCole Faust {
4201*c217d954SCole Faust stbi__zbuf a;
4202*c217d954SCole Faust char *p = (char *) stbi__malloc(initial_size);
4203*c217d954SCole Faust if (p == NULL) return NULL;
4204*c217d954SCole Faust a.zbuffer = (stbi_uc *) buffer;
4205*c217d954SCole Faust a.zbuffer_end = (stbi_uc *) buffer + len;
4206*c217d954SCole Faust if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
4207*c217d954SCole Faust if (outlen) *outlen = (int) (a.zout - a.zout_start);
4208*c217d954SCole Faust return a.zout_start;
4209*c217d954SCole Faust } else {
4210*c217d954SCole Faust STBI_FREE(a.zout_start);
4211*c217d954SCole Faust return NULL;
4212*c217d954SCole Faust }
4213*c217d954SCole Faust }
4214*c217d954SCole Faust
stbi_zlib_decode_buffer(char * obuffer,int olen,char const * ibuffer,int ilen)4215*c217d954SCole Faust STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
4216*c217d954SCole Faust {
4217*c217d954SCole Faust stbi__zbuf a;
4218*c217d954SCole Faust a.zbuffer = (stbi_uc *) ibuffer;
4219*c217d954SCole Faust a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
4220*c217d954SCole Faust if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
4221*c217d954SCole Faust return (int) (a.zout - a.zout_start);
4222*c217d954SCole Faust else
4223*c217d954SCole Faust return -1;
4224*c217d954SCole Faust }
4225*c217d954SCole Faust
stbi_zlib_decode_noheader_malloc(char const * buffer,int len,int * outlen)4226*c217d954SCole Faust STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
4227*c217d954SCole Faust {
4228*c217d954SCole Faust stbi__zbuf a;
4229*c217d954SCole Faust char *p = (char *) stbi__malloc(16384);
4230*c217d954SCole Faust if (p == NULL) return NULL;
4231*c217d954SCole Faust a.zbuffer = (stbi_uc *) buffer;
4232*c217d954SCole Faust a.zbuffer_end = (stbi_uc *) buffer+len;
4233*c217d954SCole Faust if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
4234*c217d954SCole Faust if (outlen) *outlen = (int) (a.zout - a.zout_start);
4235*c217d954SCole Faust return a.zout_start;
4236*c217d954SCole Faust } else {
4237*c217d954SCole Faust STBI_FREE(a.zout_start);
4238*c217d954SCole Faust return NULL;
4239*c217d954SCole Faust }
4240*c217d954SCole Faust }
4241*c217d954SCole Faust
stbi_zlib_decode_noheader_buffer(char * obuffer,int olen,const char * ibuffer,int ilen)4242*c217d954SCole Faust STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
4243*c217d954SCole Faust {
4244*c217d954SCole Faust stbi__zbuf a;
4245*c217d954SCole Faust a.zbuffer = (stbi_uc *) ibuffer;
4246*c217d954SCole Faust a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
4247*c217d954SCole Faust if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
4248*c217d954SCole Faust return (int) (a.zout - a.zout_start);
4249*c217d954SCole Faust else
4250*c217d954SCole Faust return -1;
4251*c217d954SCole Faust }
4252*c217d954SCole Faust #endif
4253*c217d954SCole Faust
4254*c217d954SCole Faust // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18
4255*c217d954SCole Faust // simple implementation
4256*c217d954SCole Faust // - only 8-bit samples
4257*c217d954SCole Faust // - no CRC checking
4258*c217d954SCole Faust // - allocates lots of intermediate memory
4259*c217d954SCole Faust // - avoids problem of streaming data between subsystems
4260*c217d954SCole Faust // - avoids explicit window management
4261*c217d954SCole Faust // performance
4262*c217d954SCole Faust // - uses stb_zlib, a PD zlib implementation with fast huffman decoding
4263*c217d954SCole Faust
4264*c217d954SCole Faust #ifndef STBI_NO_PNG
4265*c217d954SCole Faust typedef struct
4266*c217d954SCole Faust {
4267*c217d954SCole Faust stbi__uint32 length;
4268*c217d954SCole Faust stbi__uint32 type;
4269*c217d954SCole Faust } stbi__pngchunk;
4270*c217d954SCole Faust
stbi__get_chunk_header(stbi__context * s)4271*c217d954SCole Faust static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
4272*c217d954SCole Faust {
4273*c217d954SCole Faust stbi__pngchunk c;
4274*c217d954SCole Faust c.length = stbi__get32be(s);
4275*c217d954SCole Faust c.type = stbi__get32be(s);
4276*c217d954SCole Faust return c;
4277*c217d954SCole Faust }
4278*c217d954SCole Faust
stbi__check_png_header(stbi__context * s)4279*c217d954SCole Faust static int stbi__check_png_header(stbi__context *s)
4280*c217d954SCole Faust {
4281*c217d954SCole Faust static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };
4282*c217d954SCole Faust int i;
4283*c217d954SCole Faust for (i=0; i < 8; ++i)
4284*c217d954SCole Faust if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG");
4285*c217d954SCole Faust return 1;
4286*c217d954SCole Faust }
4287*c217d954SCole Faust
4288*c217d954SCole Faust typedef struct
4289*c217d954SCole Faust {
4290*c217d954SCole Faust stbi__context *s;
4291*c217d954SCole Faust stbi_uc *idata, *expanded, *out;
4292*c217d954SCole Faust int depth;
4293*c217d954SCole Faust } stbi__png;
4294*c217d954SCole Faust
4295*c217d954SCole Faust
4296*c217d954SCole Faust enum {
4297*c217d954SCole Faust STBI__F_none=0,
4298*c217d954SCole Faust STBI__F_sub=1,
4299*c217d954SCole Faust STBI__F_up=2,
4300*c217d954SCole Faust STBI__F_avg=3,
4301*c217d954SCole Faust STBI__F_paeth=4,
4302*c217d954SCole Faust // synthetic filters used for first scanline to avoid needing a dummy row of 0s
4303*c217d954SCole Faust STBI__F_avg_first,
4304*c217d954SCole Faust STBI__F_paeth_first
4305*c217d954SCole Faust };
4306*c217d954SCole Faust
4307*c217d954SCole Faust static stbi_uc first_row_filter[5] =
4308*c217d954SCole Faust {
4309*c217d954SCole Faust STBI__F_none,
4310*c217d954SCole Faust STBI__F_sub,
4311*c217d954SCole Faust STBI__F_none,
4312*c217d954SCole Faust STBI__F_avg_first,
4313*c217d954SCole Faust STBI__F_paeth_first
4314*c217d954SCole Faust };
4315*c217d954SCole Faust
stbi__paeth(int a,int b,int c)4316*c217d954SCole Faust static int stbi__paeth(int a, int b, int c)
4317*c217d954SCole Faust {
4318*c217d954SCole Faust int p = a + b - c;
4319*c217d954SCole Faust int pa = abs(p-a);
4320*c217d954SCole Faust int pb = abs(p-b);
4321*c217d954SCole Faust int pc = abs(p-c);
4322*c217d954SCole Faust if (pa <= pb && pa <= pc) return a;
4323*c217d954SCole Faust if (pb <= pc) return b;
4324*c217d954SCole Faust return c;
4325*c217d954SCole Faust }
4326*c217d954SCole Faust
4327*c217d954SCole Faust static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };
4328*c217d954SCole Faust
4329*c217d954SCole Faust // create the png data from post-deflated data
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)4330*c217d954SCole Faust 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)
4331*c217d954SCole Faust {
4332*c217d954SCole Faust int bytes = (depth == 16? 2 : 1);
4333*c217d954SCole Faust stbi__context *s = a->s;
4334*c217d954SCole Faust stbi__uint32 i,j,stride = x*out_n*bytes;
4335*c217d954SCole Faust stbi__uint32 img_len, img_width_bytes;
4336*c217d954SCole Faust int k;
4337*c217d954SCole Faust int img_n = s->img_n; // copy it into a local for later
4338*c217d954SCole Faust
4339*c217d954SCole Faust int output_bytes = out_n*bytes;
4340*c217d954SCole Faust int filter_bytes = img_n*bytes;
4341*c217d954SCole Faust int width = x;
4342*c217d954SCole Faust
4343*c217d954SCole Faust STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1);
4344*c217d954SCole Faust a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into
4345*c217d954SCole Faust if (!a->out) return stbi__err("outofmem", "Out of memory");
4346*c217d954SCole Faust
4347*c217d954SCole Faust if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG");
4348*c217d954SCole Faust img_width_bytes = (((img_n * x * depth) + 7) >> 3);
4349*c217d954SCole Faust img_len = (img_width_bytes + 1) * y;
4350*c217d954SCole Faust
4351*c217d954SCole Faust // we used to check for exact match between raw_len and img_len on non-interlaced PNGs,
4352*c217d954SCole Faust // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros),
4353*c217d954SCole Faust // so just check for raw_len < img_len always.
4354*c217d954SCole Faust if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");
4355*c217d954SCole Faust
4356*c217d954SCole Faust for (j=0; j < y; ++j) {
4357*c217d954SCole Faust stbi_uc *cur = a->out + stride*j;
4358*c217d954SCole Faust stbi_uc *prior;
4359*c217d954SCole Faust int filter = *raw++;
4360*c217d954SCole Faust
4361*c217d954SCole Faust if (filter > 4)
4362*c217d954SCole Faust return stbi__err("invalid filter","Corrupt PNG");
4363*c217d954SCole Faust
4364*c217d954SCole Faust if (depth < 8) {
4365*c217d954SCole Faust STBI_ASSERT(img_width_bytes <= x);
4366*c217d954SCole Faust cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place
4367*c217d954SCole Faust filter_bytes = 1;
4368*c217d954SCole Faust width = img_width_bytes;
4369*c217d954SCole Faust }
4370*c217d954SCole Faust prior = cur - stride; // bugfix: need to compute this after 'cur +=' computation above
4371*c217d954SCole Faust
4372*c217d954SCole Faust // if first row, use special filter that doesn't sample previous row
4373*c217d954SCole Faust if (j == 0) filter = first_row_filter[filter];
4374*c217d954SCole Faust
4375*c217d954SCole Faust // handle first byte explicitly
4376*c217d954SCole Faust for (k=0; k < filter_bytes; ++k) {
4377*c217d954SCole Faust switch (filter) {
4378*c217d954SCole Faust case STBI__F_none : cur[k] = raw[k]; break;
4379*c217d954SCole Faust case STBI__F_sub : cur[k] = raw[k]; break;
4380*c217d954SCole Faust case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
4381*c217d954SCole Faust case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break;
4382*c217d954SCole Faust case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break;
4383*c217d954SCole Faust case STBI__F_avg_first : cur[k] = raw[k]; break;
4384*c217d954SCole Faust case STBI__F_paeth_first: cur[k] = raw[k]; break;
4385*c217d954SCole Faust }
4386*c217d954SCole Faust }
4387*c217d954SCole Faust
4388*c217d954SCole Faust if (depth == 8) {
4389*c217d954SCole Faust if (img_n != out_n)
4390*c217d954SCole Faust cur[img_n] = 255; // first pixel
4391*c217d954SCole Faust raw += img_n;
4392*c217d954SCole Faust cur += out_n;
4393*c217d954SCole Faust prior += out_n;
4394*c217d954SCole Faust } else if (depth == 16) {
4395*c217d954SCole Faust if (img_n != out_n) {
4396*c217d954SCole Faust cur[filter_bytes] = 255; // first pixel top byte
4397*c217d954SCole Faust cur[filter_bytes+1] = 255; // first pixel bottom byte
4398*c217d954SCole Faust }
4399*c217d954SCole Faust raw += filter_bytes;
4400*c217d954SCole Faust cur += output_bytes;
4401*c217d954SCole Faust prior += output_bytes;
4402*c217d954SCole Faust } else {
4403*c217d954SCole Faust raw += 1;
4404*c217d954SCole Faust cur += 1;
4405*c217d954SCole Faust prior += 1;
4406*c217d954SCole Faust }
4407*c217d954SCole Faust
4408*c217d954SCole Faust // this is a little gross, so that we don't switch per-pixel or per-component
4409*c217d954SCole Faust if (depth < 8 || img_n == out_n) {
4410*c217d954SCole Faust int nk = (width - 1)*filter_bytes;
4411*c217d954SCole Faust #define STBI__CASE(f) \
4412*c217d954SCole Faust case f: \
4413*c217d954SCole Faust for (k=0; k < nk; ++k)
4414*c217d954SCole Faust switch (filter) {
4415*c217d954SCole Faust // "none" filter turns into a memcpy here; make that explicit.
4416*c217d954SCole Faust case STBI__F_none: memcpy(cur, raw, nk); break;
4417*c217d954SCole Faust STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); } break;
4418*c217d954SCole Faust STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
4419*c217d954SCole Faust STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); } break;
4420*c217d954SCole Faust STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); } break;
4421*c217d954SCole Faust STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); } break;
4422*c217d954SCole Faust STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); } break;
4423*c217d954SCole Faust }
4424*c217d954SCole Faust #undef STBI__CASE
4425*c217d954SCole Faust raw += nk;
4426*c217d954SCole Faust } else {
4427*c217d954SCole Faust STBI_ASSERT(img_n+1 == out_n);
4428*c217d954SCole Faust #define STBI__CASE(f) \
4429*c217d954SCole Faust case f: \
4430*c217d954SCole Faust for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) \
4431*c217d954SCole Faust for (k=0; k < filter_bytes; ++k)
4432*c217d954SCole Faust switch (filter) {
4433*c217d954SCole Faust STBI__CASE(STBI__F_none) { cur[k] = raw[k]; } break;
4434*c217d954SCole Faust STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k- output_bytes]); } break;
4435*c217d954SCole Faust STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
4436*c217d954SCole Faust STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); } break;
4437*c217d954SCole Faust STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); } break;
4438*c217d954SCole Faust STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); } break;
4439*c217d954SCole Faust STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); } break;
4440*c217d954SCole Faust }
4441*c217d954SCole Faust #undef STBI__CASE
4442*c217d954SCole Faust
4443*c217d954SCole Faust // the loop above sets the high byte of the pixels' alpha, but for
4444*c217d954SCole Faust // 16 bit png files we also need the low byte set. we'll do that here.
4445*c217d954SCole Faust if (depth == 16) {
4446*c217d954SCole Faust cur = a->out + stride*j; // start at the beginning of the row again
4447*c217d954SCole Faust for (i=0; i < x; ++i,cur+=output_bytes) {
4448*c217d954SCole Faust cur[filter_bytes+1] = 255;
4449*c217d954SCole Faust }
4450*c217d954SCole Faust }
4451*c217d954SCole Faust }
4452*c217d954SCole Faust }
4453*c217d954SCole Faust
4454*c217d954SCole Faust // we make a separate pass to expand bits to pixels; for performance,
4455*c217d954SCole Faust // this could run two scanlines behind the above code, so it won't
4456*c217d954SCole Faust // intefere with filtering but will still be in the cache.
4457*c217d954SCole Faust if (depth < 8) {
4458*c217d954SCole Faust for (j=0; j < y; ++j) {
4459*c217d954SCole Faust stbi_uc *cur = a->out + stride*j;
4460*c217d954SCole Faust stbi_uc *in = a->out + stride*j + x*out_n - img_width_bytes;
4461*c217d954SCole Faust // 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
4462*c217d954SCole Faust // 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
4463*c217d954SCole Faust stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range
4464*c217d954SCole Faust
4465*c217d954SCole Faust // note that the final byte might overshoot and write more data than desired.
4466*c217d954SCole Faust // we can allocate enough data that this never writes out of memory, but it
4467*c217d954SCole Faust // could also overwrite the next scanline. can it overwrite non-empty data
4468*c217d954SCole Faust // on the next scanline? yes, consider 1-pixel-wide scanlines with 1-bit-per-pixel.
4469*c217d954SCole Faust // so we need to explicitly clamp the final ones
4470*c217d954SCole Faust
4471*c217d954SCole Faust if (depth == 4) {
4472*c217d954SCole Faust for (k=x*img_n; k >= 2; k-=2, ++in) {
4473*c217d954SCole Faust *cur++ = scale * ((*in >> 4) );
4474*c217d954SCole Faust *cur++ = scale * ((*in ) & 0x0f);
4475*c217d954SCole Faust }
4476*c217d954SCole Faust if (k > 0) *cur++ = scale * ((*in >> 4) );
4477*c217d954SCole Faust } else if (depth == 2) {
4478*c217d954SCole Faust for (k=x*img_n; k >= 4; k-=4, ++in) {
4479*c217d954SCole Faust *cur++ = scale * ((*in >> 6) );
4480*c217d954SCole Faust *cur++ = scale * ((*in >> 4) & 0x03);
4481*c217d954SCole Faust *cur++ = scale * ((*in >> 2) & 0x03);
4482*c217d954SCole Faust *cur++ = scale * ((*in ) & 0x03);
4483*c217d954SCole Faust }
4484*c217d954SCole Faust if (k > 0) *cur++ = scale * ((*in >> 6) );
4485*c217d954SCole Faust if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03);
4486*c217d954SCole Faust if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03);
4487*c217d954SCole Faust } else if (depth == 1) {
4488*c217d954SCole Faust for (k=x*img_n; k >= 8; k-=8, ++in) {
4489*c217d954SCole Faust *cur++ = scale * ((*in >> 7) );
4490*c217d954SCole Faust *cur++ = scale * ((*in >> 6) & 0x01);
4491*c217d954SCole Faust *cur++ = scale * ((*in >> 5) & 0x01);
4492*c217d954SCole Faust *cur++ = scale * ((*in >> 4) & 0x01);
4493*c217d954SCole Faust *cur++ = scale * ((*in >> 3) & 0x01);
4494*c217d954SCole Faust *cur++ = scale * ((*in >> 2) & 0x01);
4495*c217d954SCole Faust *cur++ = scale * ((*in >> 1) & 0x01);
4496*c217d954SCole Faust *cur++ = scale * ((*in ) & 0x01);
4497*c217d954SCole Faust }
4498*c217d954SCole Faust if (k > 0) *cur++ = scale * ((*in >> 7) );
4499*c217d954SCole Faust if (k > 1) *cur++ = scale * ((*in >> 6) & 0x01);
4500*c217d954SCole Faust if (k > 2) *cur++ = scale * ((*in >> 5) & 0x01);
4501*c217d954SCole Faust if (k > 3) *cur++ = scale * ((*in >> 4) & 0x01);
4502*c217d954SCole Faust if (k > 4) *cur++ = scale * ((*in >> 3) & 0x01);
4503*c217d954SCole Faust if (k > 5) *cur++ = scale * ((*in >> 2) & 0x01);
4504*c217d954SCole Faust if (k > 6) *cur++ = scale * ((*in >> 1) & 0x01);
4505*c217d954SCole Faust }
4506*c217d954SCole Faust if (img_n != out_n) {
4507*c217d954SCole Faust int q;
4508*c217d954SCole Faust // insert alpha = 255
4509*c217d954SCole Faust cur = a->out + stride*j;
4510*c217d954SCole Faust if (img_n == 1) {
4511*c217d954SCole Faust for (q=x-1; q >= 0; --q) {
4512*c217d954SCole Faust cur[q*2+1] = 255;
4513*c217d954SCole Faust cur[q*2+0] = cur[q];
4514*c217d954SCole Faust }
4515*c217d954SCole Faust } else {
4516*c217d954SCole Faust STBI_ASSERT(img_n == 3);
4517*c217d954SCole Faust for (q=x-1; q >= 0; --q) {
4518*c217d954SCole Faust cur[q*4+3] = 255;
4519*c217d954SCole Faust cur[q*4+2] = cur[q*3+2];
4520*c217d954SCole Faust cur[q*4+1] = cur[q*3+1];
4521*c217d954SCole Faust cur[q*4+0] = cur[q*3+0];
4522*c217d954SCole Faust }
4523*c217d954SCole Faust }
4524*c217d954SCole Faust }
4525*c217d954SCole Faust }
4526*c217d954SCole Faust } else if (depth == 16) {
4527*c217d954SCole Faust // force the image data from big-endian to platform-native.
4528*c217d954SCole Faust // this is done in a separate pass due to the decoding relying
4529*c217d954SCole Faust // on the data being untouched, but could probably be done
4530*c217d954SCole Faust // per-line during decode if care is taken.
4531*c217d954SCole Faust stbi_uc *cur = a->out;
4532*c217d954SCole Faust stbi__uint16 *cur16 = (stbi__uint16*)cur;
4533*c217d954SCole Faust
4534*c217d954SCole Faust for(i=0; i < x*y*out_n; ++i,cur16++,cur+=2) {
4535*c217d954SCole Faust *cur16 = (cur[0] << 8) | cur[1];
4536*c217d954SCole Faust }
4537*c217d954SCole Faust }
4538*c217d954SCole Faust
4539*c217d954SCole Faust return 1;
4540*c217d954SCole Faust }
4541*c217d954SCole Faust
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)4542*c217d954SCole Faust 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)
4543*c217d954SCole Faust {
4544*c217d954SCole Faust int bytes = (depth == 16 ? 2 : 1);
4545*c217d954SCole Faust int out_bytes = out_n * bytes;
4546*c217d954SCole Faust stbi_uc *final;
4547*c217d954SCole Faust int p;
4548*c217d954SCole Faust if (!interlaced)
4549*c217d954SCole Faust return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color);
4550*c217d954SCole Faust
4551*c217d954SCole Faust // de-interlacing
4552*c217d954SCole Faust final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0);
4553*c217d954SCole Faust for (p=0; p < 7; ++p) {
4554*c217d954SCole Faust int xorig[] = { 0,4,0,2,0,1,0 };
4555*c217d954SCole Faust int yorig[] = { 0,0,4,0,2,0,1 };
4556*c217d954SCole Faust int xspc[] = { 8,8,4,4,2,2,1 };
4557*c217d954SCole Faust int yspc[] = { 8,8,8,4,4,2,2 };
4558*c217d954SCole Faust int i,j,x,y;
4559*c217d954SCole Faust // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
4560*c217d954SCole Faust x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
4561*c217d954SCole Faust y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
4562*c217d954SCole Faust if (x && y) {
4563*c217d954SCole Faust stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;
4564*c217d954SCole Faust if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {
4565*c217d954SCole Faust STBI_FREE(final);
4566*c217d954SCole Faust return 0;
4567*c217d954SCole Faust }
4568*c217d954SCole Faust for (j=0; j < y; ++j) {
4569*c217d954SCole Faust for (i=0; i < x; ++i) {
4570*c217d954SCole Faust int out_y = j*yspc[p]+yorig[p];
4571*c217d954SCole Faust int out_x = i*xspc[p]+xorig[p];
4572*c217d954SCole Faust memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes,
4573*c217d954SCole Faust a->out + (j*x+i)*out_bytes, out_bytes);
4574*c217d954SCole Faust }
4575*c217d954SCole Faust }
4576*c217d954SCole Faust STBI_FREE(a->out);
4577*c217d954SCole Faust image_data += img_len;
4578*c217d954SCole Faust image_data_len -= img_len;
4579*c217d954SCole Faust }
4580*c217d954SCole Faust }
4581*c217d954SCole Faust a->out = final;
4582*c217d954SCole Faust
4583*c217d954SCole Faust return 1;
4584*c217d954SCole Faust }
4585*c217d954SCole Faust
stbi__compute_transparency(stbi__png * z,stbi_uc tc[3],int out_n)4586*c217d954SCole Faust static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n)
4587*c217d954SCole Faust {
4588*c217d954SCole Faust stbi__context *s = z->s;
4589*c217d954SCole Faust stbi__uint32 i, pixel_count = s->img_x * s->img_y;
4590*c217d954SCole Faust stbi_uc *p = z->out;
4591*c217d954SCole Faust
4592*c217d954SCole Faust // compute color-based transparency, assuming we've
4593*c217d954SCole Faust // already got 255 as the alpha value in the output
4594*c217d954SCole Faust STBI_ASSERT(out_n == 2 || out_n == 4);
4595*c217d954SCole Faust
4596*c217d954SCole Faust if (out_n == 2) {
4597*c217d954SCole Faust for (i=0; i < pixel_count; ++i) {
4598*c217d954SCole Faust p[1] = (p[0] == tc[0] ? 0 : 255);
4599*c217d954SCole Faust p += 2;
4600*c217d954SCole Faust }
4601*c217d954SCole Faust } else {
4602*c217d954SCole Faust for (i=0; i < pixel_count; ++i) {
4603*c217d954SCole Faust if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
4604*c217d954SCole Faust p[3] = 0;
4605*c217d954SCole Faust p += 4;
4606*c217d954SCole Faust }
4607*c217d954SCole Faust }
4608*c217d954SCole Faust return 1;
4609*c217d954SCole Faust }
4610*c217d954SCole Faust
stbi__compute_transparency16(stbi__png * z,stbi__uint16 tc[3],int out_n)4611*c217d954SCole Faust static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n)
4612*c217d954SCole Faust {
4613*c217d954SCole Faust stbi__context *s = z->s;
4614*c217d954SCole Faust stbi__uint32 i, pixel_count = s->img_x * s->img_y;
4615*c217d954SCole Faust stbi__uint16 *p = (stbi__uint16*) z->out;
4616*c217d954SCole Faust
4617*c217d954SCole Faust // compute color-based transparency, assuming we've
4618*c217d954SCole Faust // already got 65535 as the alpha value in the output
4619*c217d954SCole Faust STBI_ASSERT(out_n == 2 || out_n == 4);
4620*c217d954SCole Faust
4621*c217d954SCole Faust if (out_n == 2) {
4622*c217d954SCole Faust for (i = 0; i < pixel_count; ++i) {
4623*c217d954SCole Faust p[1] = (p[0] == tc[0] ? 0 : 65535);
4624*c217d954SCole Faust p += 2;
4625*c217d954SCole Faust }
4626*c217d954SCole Faust } else {
4627*c217d954SCole Faust for (i = 0; i < pixel_count; ++i) {
4628*c217d954SCole Faust if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
4629*c217d954SCole Faust p[3] = 0;
4630*c217d954SCole Faust p += 4;
4631*c217d954SCole Faust }
4632*c217d954SCole Faust }
4633*c217d954SCole Faust return 1;
4634*c217d954SCole Faust }
4635*c217d954SCole Faust
stbi__expand_png_palette(stbi__png * a,stbi_uc * palette,int len,int pal_img_n)4636*c217d954SCole Faust static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n)
4637*c217d954SCole Faust {
4638*c217d954SCole Faust stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
4639*c217d954SCole Faust stbi_uc *p, *temp_out, *orig = a->out;
4640*c217d954SCole Faust
4641*c217d954SCole Faust p = (stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0);
4642*c217d954SCole Faust if (p == NULL) return stbi__err("outofmem", "Out of memory");
4643*c217d954SCole Faust
4644*c217d954SCole Faust // between here and free(out) below, exitting would leak
4645*c217d954SCole Faust temp_out = p;
4646*c217d954SCole Faust
4647*c217d954SCole Faust if (pal_img_n == 3) {
4648*c217d954SCole Faust for (i=0; i < pixel_count; ++i) {
4649*c217d954SCole Faust int n = orig[i]*4;
4650*c217d954SCole Faust p[0] = palette[n ];
4651*c217d954SCole Faust p[1] = palette[n+1];
4652*c217d954SCole Faust p[2] = palette[n+2];
4653*c217d954SCole Faust p += 3;
4654*c217d954SCole Faust }
4655*c217d954SCole Faust } else {
4656*c217d954SCole Faust for (i=0; i < pixel_count; ++i) {
4657*c217d954SCole Faust int n = orig[i]*4;
4658*c217d954SCole Faust p[0] = palette[n ];
4659*c217d954SCole Faust p[1] = palette[n+1];
4660*c217d954SCole Faust p[2] = palette[n+2];
4661*c217d954SCole Faust p[3] = palette[n+3];
4662*c217d954SCole Faust p += 4;
4663*c217d954SCole Faust }
4664*c217d954SCole Faust }
4665*c217d954SCole Faust STBI_FREE(a->out);
4666*c217d954SCole Faust a->out = temp_out;
4667*c217d954SCole Faust
4668*c217d954SCole Faust STBI_NOTUSED(len);
4669*c217d954SCole Faust
4670*c217d954SCole Faust return 1;
4671*c217d954SCole Faust }
4672*c217d954SCole Faust
4673*c217d954SCole Faust static int stbi__unpremultiply_on_load = 0;
4674*c217d954SCole Faust static int stbi__de_iphone_flag = 0;
4675*c217d954SCole Faust
stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)4676*c217d954SCole Faust STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
4677*c217d954SCole Faust {
4678*c217d954SCole Faust stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;
4679*c217d954SCole Faust }
4680*c217d954SCole Faust
stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)4681*c217d954SCole Faust STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
4682*c217d954SCole Faust {
4683*c217d954SCole Faust stbi__de_iphone_flag = flag_true_if_should_convert;
4684*c217d954SCole Faust }
4685*c217d954SCole Faust
stbi__de_iphone(stbi__png * z)4686*c217d954SCole Faust static void stbi__de_iphone(stbi__png *z)
4687*c217d954SCole Faust {
4688*c217d954SCole Faust stbi__context *s = z->s;
4689*c217d954SCole Faust stbi__uint32 i, pixel_count = s->img_x * s->img_y;
4690*c217d954SCole Faust stbi_uc *p = z->out;
4691*c217d954SCole Faust
4692*c217d954SCole Faust if (s->img_out_n == 3) { // convert bgr to rgb
4693*c217d954SCole Faust for (i=0; i < pixel_count; ++i) {
4694*c217d954SCole Faust stbi_uc t = p[0];
4695*c217d954SCole Faust p[0] = p[2];
4696*c217d954SCole Faust p[2] = t;
4697*c217d954SCole Faust p += 3;
4698*c217d954SCole Faust }
4699*c217d954SCole Faust } else {
4700*c217d954SCole Faust STBI_ASSERT(s->img_out_n == 4);
4701*c217d954SCole Faust if (stbi__unpremultiply_on_load) {
4702*c217d954SCole Faust // convert bgr to rgb and unpremultiply
4703*c217d954SCole Faust for (i=0; i < pixel_count; ++i) {
4704*c217d954SCole Faust stbi_uc a = p[3];
4705*c217d954SCole Faust stbi_uc t = p[0];
4706*c217d954SCole Faust if (a) {
4707*c217d954SCole Faust stbi_uc half = a / 2;
4708*c217d954SCole Faust p[0] = (p[2] * 255 + half) / a;
4709*c217d954SCole Faust p[1] = (p[1] * 255 + half) / a;
4710*c217d954SCole Faust p[2] = ( t * 255 + half) / a;
4711*c217d954SCole Faust } else {
4712*c217d954SCole Faust p[0] = p[2];
4713*c217d954SCole Faust p[2] = t;
4714*c217d954SCole Faust }
4715*c217d954SCole Faust p += 4;
4716*c217d954SCole Faust }
4717*c217d954SCole Faust } else {
4718*c217d954SCole Faust // convert bgr to rgb
4719*c217d954SCole Faust for (i=0; i < pixel_count; ++i) {
4720*c217d954SCole Faust stbi_uc t = p[0];
4721*c217d954SCole Faust p[0] = p[2];
4722*c217d954SCole Faust p[2] = t;
4723*c217d954SCole Faust p += 4;
4724*c217d954SCole Faust }
4725*c217d954SCole Faust }
4726*c217d954SCole Faust }
4727*c217d954SCole Faust }
4728*c217d954SCole Faust
4729*c217d954SCole Faust #define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d))
4730*c217d954SCole Faust
stbi__parse_png_file(stbi__png * z,int scan,int req_comp)4731*c217d954SCole Faust static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
4732*c217d954SCole Faust {
4733*c217d954SCole Faust stbi_uc palette[1024], pal_img_n=0;
4734*c217d954SCole Faust stbi_uc has_trans=0, tc[3];
4735*c217d954SCole Faust stbi__uint16 tc16[3];
4736*c217d954SCole Faust stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
4737*c217d954SCole Faust int first=1,k,interlace=0, color=0, is_iphone=0;
4738*c217d954SCole Faust stbi__context *s = z->s;
4739*c217d954SCole Faust
4740*c217d954SCole Faust z->expanded = NULL;
4741*c217d954SCole Faust z->idata = NULL;
4742*c217d954SCole Faust z->out = NULL;
4743*c217d954SCole Faust
4744*c217d954SCole Faust if (!stbi__check_png_header(s)) return 0;
4745*c217d954SCole Faust
4746*c217d954SCole Faust if (scan == STBI__SCAN_type) return 1;
4747*c217d954SCole Faust
4748*c217d954SCole Faust for (;;) {
4749*c217d954SCole Faust stbi__pngchunk c = stbi__get_chunk_header(s);
4750*c217d954SCole Faust switch (c.type) {
4751*c217d954SCole Faust case STBI__PNG_TYPE('C','g','B','I'):
4752*c217d954SCole Faust is_iphone = 1;
4753*c217d954SCole Faust stbi__skip(s, c.length);
4754*c217d954SCole Faust break;
4755*c217d954SCole Faust case STBI__PNG_TYPE('I','H','D','R'): {
4756*c217d954SCole Faust int comp,filter;
4757*c217d954SCole Faust if (!first) return stbi__err("multiple IHDR","Corrupt PNG");
4758*c217d954SCole Faust first = 0;
4759*c217d954SCole Faust if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");
4760*c217d954SCole Faust s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
4761*c217d954SCole Faust s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
4762*c217d954SCole Faust 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");
4763*c217d954SCole Faust color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
4764*c217d954SCole Faust if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG");
4765*c217d954SCole Faust if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
4766*c217d954SCole Faust comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
4767*c217d954SCole Faust filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
4768*c217d954SCole Faust interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
4769*c217d954SCole Faust if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG");
4770*c217d954SCole Faust if (!pal_img_n) {
4771*c217d954SCole Faust s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
4772*c217d954SCole Faust if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
4773*c217d954SCole Faust if (scan == STBI__SCAN_header) return 1;
4774*c217d954SCole Faust } else {
4775*c217d954SCole Faust // if paletted, then pal_n is our final components, and
4776*c217d954SCole Faust // img_n is # components to decompress/filter.
4777*c217d954SCole Faust s->img_n = 1;
4778*c217d954SCole Faust if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
4779*c217d954SCole Faust // if SCAN_header, have to scan to see if we have a tRNS
4780*c217d954SCole Faust }
4781*c217d954SCole Faust break;
4782*c217d954SCole Faust }
4783*c217d954SCole Faust
4784*c217d954SCole Faust case STBI__PNG_TYPE('P','L','T','E'): {
4785*c217d954SCole Faust if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4786*c217d954SCole Faust if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG");
4787*c217d954SCole Faust pal_len = c.length / 3;
4788*c217d954SCole Faust if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG");
4789*c217d954SCole Faust for (i=0; i < pal_len; ++i) {
4790*c217d954SCole Faust palette[i*4+0] = stbi__get8(s);
4791*c217d954SCole Faust palette[i*4+1] = stbi__get8(s);
4792*c217d954SCole Faust palette[i*4+2] = stbi__get8(s);
4793*c217d954SCole Faust palette[i*4+3] = 255;
4794*c217d954SCole Faust }
4795*c217d954SCole Faust break;
4796*c217d954SCole Faust }
4797*c217d954SCole Faust
4798*c217d954SCole Faust case STBI__PNG_TYPE('t','R','N','S'): {
4799*c217d954SCole Faust if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4800*c217d954SCole Faust if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG");
4801*c217d954SCole Faust if (pal_img_n) {
4802*c217d954SCole Faust if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; }
4803*c217d954SCole Faust if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG");
4804*c217d954SCole Faust if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG");
4805*c217d954SCole Faust pal_img_n = 4;
4806*c217d954SCole Faust for (i=0; i < c.length; ++i)
4807*c217d954SCole Faust palette[i*4+3] = stbi__get8(s);
4808*c217d954SCole Faust } else {
4809*c217d954SCole Faust if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG");
4810*c217d954SCole Faust if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG");
4811*c217d954SCole Faust has_trans = 1;
4812*c217d954SCole Faust if (z->depth == 16) {
4813*c217d954SCole Faust for (k = 0; k < s->img_n; ++k) tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is
4814*c217d954SCole Faust } else {
4815*c217d954SCole Faust 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
4816*c217d954SCole Faust }
4817*c217d954SCole Faust }
4818*c217d954SCole Faust break;
4819*c217d954SCole Faust }
4820*c217d954SCole Faust
4821*c217d954SCole Faust case STBI__PNG_TYPE('I','D','A','T'): {
4822*c217d954SCole Faust if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4823*c217d954SCole Faust if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG");
4824*c217d954SCole Faust if (scan == STBI__SCAN_header) { s->img_n = pal_img_n; return 1; }
4825*c217d954SCole Faust if ((int)(ioff + c.length) < (int)ioff) return 0;
4826*c217d954SCole Faust if (ioff + c.length > idata_limit) {
4827*c217d954SCole Faust stbi__uint32 idata_limit_old = idata_limit;
4828*c217d954SCole Faust stbi_uc *p;
4829*c217d954SCole Faust if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
4830*c217d954SCole Faust while (ioff + c.length > idata_limit)
4831*c217d954SCole Faust idata_limit *= 2;
4832*c217d954SCole Faust STBI_NOTUSED(idata_limit_old);
4833*c217d954SCole Faust p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
4834*c217d954SCole Faust z->idata = p;
4835*c217d954SCole Faust }
4836*c217d954SCole Faust if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG");
4837*c217d954SCole Faust ioff += c.length;
4838*c217d954SCole Faust break;
4839*c217d954SCole Faust }
4840*c217d954SCole Faust
4841*c217d954SCole Faust case STBI__PNG_TYPE('I','E','N','D'): {
4842*c217d954SCole Faust stbi__uint32 raw_len, bpl;
4843*c217d954SCole Faust if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4844*c217d954SCole Faust if (scan != STBI__SCAN_load) return 1;
4845*c217d954SCole Faust if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");
4846*c217d954SCole Faust // initial guess for decoded data size to avoid unnecessary reallocs
4847*c217d954SCole Faust bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component
4848*c217d954SCole Faust raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;
4849*c217d954SCole Faust z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);
4850*c217d954SCole Faust if (z->expanded == NULL) return 0; // zlib should set error
4851*c217d954SCole Faust STBI_FREE(z->idata); z->idata = NULL;
4852*c217d954SCole Faust if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
4853*c217d954SCole Faust s->img_out_n = s->img_n+1;
4854*c217d954SCole Faust else
4855*c217d954SCole Faust s->img_out_n = s->img_n;
4856*c217d954SCole Faust if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0;
4857*c217d954SCole Faust if (has_trans) {
4858*c217d954SCole Faust if (z->depth == 16) {
4859*c217d954SCole Faust if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0;
4860*c217d954SCole Faust } else {
4861*c217d954SCole Faust if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
4862*c217d954SCole Faust }
4863*c217d954SCole Faust }
4864*c217d954SCole Faust if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
4865*c217d954SCole Faust stbi__de_iphone(z);
4866*c217d954SCole Faust if (pal_img_n) {
4867*c217d954SCole Faust // pal_img_n == 3 or 4
4868*c217d954SCole Faust s->img_n = pal_img_n; // record the actual colors we had
4869*c217d954SCole Faust s->img_out_n = pal_img_n;
4870*c217d954SCole Faust if (req_comp >= 3) s->img_out_n = req_comp;
4871*c217d954SCole Faust if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
4872*c217d954SCole Faust return 0;
4873*c217d954SCole Faust } else if (has_trans) {
4874*c217d954SCole Faust // non-paletted image with tRNS -> source image has (constant) alpha
4875*c217d954SCole Faust ++s->img_n;
4876*c217d954SCole Faust }
4877*c217d954SCole Faust STBI_FREE(z->expanded); z->expanded = NULL;
4878*c217d954SCole Faust return 1;
4879*c217d954SCole Faust }
4880*c217d954SCole Faust
4881*c217d954SCole Faust default:
4882*c217d954SCole Faust // if critical, fail
4883*c217d954SCole Faust if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4884*c217d954SCole Faust if ((c.type & (1 << 29)) == 0) {
4885*c217d954SCole Faust #ifndef STBI_NO_FAILURE_STRINGS
4886*c217d954SCole Faust // not threadsafe
4887*c217d954SCole Faust static char invalid_chunk[] = "XXXX PNG chunk not known";
4888*c217d954SCole Faust invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
4889*c217d954SCole Faust invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
4890*c217d954SCole Faust invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
4891*c217d954SCole Faust invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
4892*c217d954SCole Faust #endif
4893*c217d954SCole Faust return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type");
4894*c217d954SCole Faust }
4895*c217d954SCole Faust stbi__skip(s, c.length);
4896*c217d954SCole Faust break;
4897*c217d954SCole Faust }
4898*c217d954SCole Faust // end of PNG chunk, read and skip CRC
4899*c217d954SCole Faust stbi__get32be(s);
4900*c217d954SCole Faust }
4901*c217d954SCole Faust }
4902*c217d954SCole Faust
stbi__do_png(stbi__png * p,int * x,int * y,int * n,int req_comp,stbi__result_info * ri)4903*c217d954SCole Faust static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri)
4904*c217d954SCole Faust {
4905*c217d954SCole Faust void *result=NULL;
4906*c217d954SCole Faust if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
4907*c217d954SCole Faust if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
4908*c217d954SCole Faust if (p->depth < 8)
4909*c217d954SCole Faust ri->bits_per_channel = 8;
4910*c217d954SCole Faust else
4911*c217d954SCole Faust ri->bits_per_channel = p->depth;
4912*c217d954SCole Faust result = p->out;
4913*c217d954SCole Faust p->out = NULL;
4914*c217d954SCole Faust if (req_comp && req_comp != p->s->img_out_n) {
4915*c217d954SCole Faust if (ri->bits_per_channel == 8)
4916*c217d954SCole Faust result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
4917*c217d954SCole Faust else
4918*c217d954SCole Faust result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
4919*c217d954SCole Faust p->s->img_out_n = req_comp;
4920*c217d954SCole Faust if (result == NULL) return result;
4921*c217d954SCole Faust }
4922*c217d954SCole Faust *x = p->s->img_x;
4923*c217d954SCole Faust *y = p->s->img_y;
4924*c217d954SCole Faust if (n) *n = p->s->img_n;
4925*c217d954SCole Faust }
4926*c217d954SCole Faust STBI_FREE(p->out); p->out = NULL;
4927*c217d954SCole Faust STBI_FREE(p->expanded); p->expanded = NULL;
4928*c217d954SCole Faust STBI_FREE(p->idata); p->idata = NULL;
4929*c217d954SCole Faust
4930*c217d954SCole Faust return result;
4931*c217d954SCole Faust }
4932*c217d954SCole Faust
stbi__png_load(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri)4933*c217d954SCole Faust static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
4934*c217d954SCole Faust {
4935*c217d954SCole Faust stbi__png p;
4936*c217d954SCole Faust p.s = s;
4937*c217d954SCole Faust return stbi__do_png(&p, x,y,comp,req_comp, ri);
4938*c217d954SCole Faust }
4939*c217d954SCole Faust
stbi__png_test(stbi__context * s)4940*c217d954SCole Faust static int stbi__png_test(stbi__context *s)
4941*c217d954SCole Faust {
4942*c217d954SCole Faust int r;
4943*c217d954SCole Faust r = stbi__check_png_header(s);
4944*c217d954SCole Faust stbi__rewind(s);
4945*c217d954SCole Faust return r;
4946*c217d954SCole Faust }
4947*c217d954SCole Faust
stbi__png_info_raw(stbi__png * p,int * x,int * y,int * comp)4948*c217d954SCole Faust static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp)
4949*c217d954SCole Faust {
4950*c217d954SCole Faust if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) {
4951*c217d954SCole Faust stbi__rewind( p->s );
4952*c217d954SCole Faust return 0;
4953*c217d954SCole Faust }
4954*c217d954SCole Faust if (x) *x = p->s->img_x;
4955*c217d954SCole Faust if (y) *y = p->s->img_y;
4956*c217d954SCole Faust if (comp) *comp = p->s->img_n;
4957*c217d954SCole Faust return 1;
4958*c217d954SCole Faust }
4959*c217d954SCole Faust
stbi__png_info(stbi__context * s,int * x,int * y,int * comp)4960*c217d954SCole Faust static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp)
4961*c217d954SCole Faust {
4962*c217d954SCole Faust stbi__png p;
4963*c217d954SCole Faust p.s = s;
4964*c217d954SCole Faust return stbi__png_info_raw(&p, x, y, comp);
4965*c217d954SCole Faust }
4966*c217d954SCole Faust
stbi__png_is16(stbi__context * s)4967*c217d954SCole Faust static int stbi__png_is16(stbi__context *s)
4968*c217d954SCole Faust {
4969*c217d954SCole Faust stbi__png p;
4970*c217d954SCole Faust p.s = s;
4971*c217d954SCole Faust if (!stbi__png_info_raw(&p, NULL, NULL, NULL))
4972*c217d954SCole Faust return 0;
4973*c217d954SCole Faust if (p.depth != 16) {
4974*c217d954SCole Faust stbi__rewind(p.s);
4975*c217d954SCole Faust return 0;
4976*c217d954SCole Faust }
4977*c217d954SCole Faust return 1;
4978*c217d954SCole Faust }
4979*c217d954SCole Faust #endif
4980*c217d954SCole Faust
4981*c217d954SCole Faust // Microsoft/Windows BMP image
4982*c217d954SCole Faust
4983*c217d954SCole Faust #ifndef STBI_NO_BMP
stbi__bmp_test_raw(stbi__context * s)4984*c217d954SCole Faust static int stbi__bmp_test_raw(stbi__context *s)
4985*c217d954SCole Faust {
4986*c217d954SCole Faust int r;
4987*c217d954SCole Faust int sz;
4988*c217d954SCole Faust if (stbi__get8(s) != 'B') return 0;
4989*c217d954SCole Faust if (stbi__get8(s) != 'M') return 0;
4990*c217d954SCole Faust stbi__get32le(s); // discard filesize
4991*c217d954SCole Faust stbi__get16le(s); // discard reserved
4992*c217d954SCole Faust stbi__get16le(s); // discard reserved
4993*c217d954SCole Faust stbi__get32le(s); // discard data offset
4994*c217d954SCole Faust sz = stbi__get32le(s);
4995*c217d954SCole Faust r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
4996*c217d954SCole Faust return r;
4997*c217d954SCole Faust }
4998*c217d954SCole Faust
stbi__bmp_test(stbi__context * s)4999*c217d954SCole Faust static int stbi__bmp_test(stbi__context *s)
5000*c217d954SCole Faust {
5001*c217d954SCole Faust int r = stbi__bmp_test_raw(s);
5002*c217d954SCole Faust stbi__rewind(s);
5003*c217d954SCole Faust return r;
5004*c217d954SCole Faust }
5005*c217d954SCole Faust
5006*c217d954SCole Faust
5007*c217d954SCole Faust // returns 0..31 for the highest set bit
stbi__high_bit(unsigned int z)5008*c217d954SCole Faust static int stbi__high_bit(unsigned int z)
5009*c217d954SCole Faust {
5010*c217d954SCole Faust int n=0;
5011*c217d954SCole Faust if (z == 0) return -1;
5012*c217d954SCole Faust if (z >= 0x10000) n += 16, z >>= 16;
5013*c217d954SCole Faust if (z >= 0x00100) n += 8, z >>= 8;
5014*c217d954SCole Faust if (z >= 0x00010) n += 4, z >>= 4;
5015*c217d954SCole Faust if (z >= 0x00004) n += 2, z >>= 2;
5016*c217d954SCole Faust if (z >= 0x00002) n += 1, z >>= 1;
5017*c217d954SCole Faust return n;
5018*c217d954SCole Faust }
5019*c217d954SCole Faust
stbi__bitcount(unsigned int a)5020*c217d954SCole Faust static int stbi__bitcount(unsigned int a)
5021*c217d954SCole Faust {
5022*c217d954SCole Faust a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
5023*c217d954SCole Faust a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
5024*c217d954SCole Faust a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
5025*c217d954SCole Faust a = (a + (a >> 8)); // max 16 per 8 bits
5026*c217d954SCole Faust a = (a + (a >> 16)); // max 32 per 8 bits
5027*c217d954SCole Faust return a & 0xff;
5028*c217d954SCole Faust }
5029*c217d954SCole Faust
5030*c217d954SCole Faust // extract an arbitrarily-aligned N-bit value (N=bits)
5031*c217d954SCole Faust // from v, and then make it 8-bits long and fractionally
5032*c217d954SCole Faust // extend it to full full range.
stbi__shiftsigned(int v,int shift,int bits)5033*c217d954SCole Faust static int stbi__shiftsigned(int v, int shift, int bits)
5034*c217d954SCole Faust {
5035*c217d954SCole Faust static unsigned int mul_table[9] = {
5036*c217d954SCole Faust 0,
5037*c217d954SCole Faust 0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/,
5038*c217d954SCole Faust 0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/,
5039*c217d954SCole Faust };
5040*c217d954SCole Faust static unsigned int shift_table[9] = {
5041*c217d954SCole Faust 0, 0,0,1,0,2,4,6,0,
5042*c217d954SCole Faust };
5043*c217d954SCole Faust if (shift < 0)
5044*c217d954SCole Faust v <<= -shift;
5045*c217d954SCole Faust else
5046*c217d954SCole Faust v >>= shift;
5047*c217d954SCole Faust STBI_ASSERT(v >= 0 && v < 256);
5048*c217d954SCole Faust v >>= (8-bits);
5049*c217d954SCole Faust STBI_ASSERT(bits >= 0 && bits <= 8);
5050*c217d954SCole Faust return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits];
5051*c217d954SCole Faust }
5052*c217d954SCole Faust
5053*c217d954SCole Faust typedef struct
5054*c217d954SCole Faust {
5055*c217d954SCole Faust int bpp, offset, hsz;
5056*c217d954SCole Faust unsigned int mr,mg,mb,ma, all_a;
5057*c217d954SCole Faust } stbi__bmp_data;
5058*c217d954SCole Faust
stbi__bmp_parse_header(stbi__context * s,stbi__bmp_data * info)5059*c217d954SCole Faust static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5060*c217d954SCole Faust {
5061*c217d954SCole Faust int hsz;
5062*c217d954SCole Faust if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP");
5063*c217d954SCole Faust stbi__get32le(s); // discard filesize
5064*c217d954SCole Faust stbi__get16le(s); // discard reserved
5065*c217d954SCole Faust stbi__get16le(s); // discard reserved
5066*c217d954SCole Faust info->offset = stbi__get32le(s);
5067*c217d954SCole Faust info->hsz = hsz = stbi__get32le(s);
5068*c217d954SCole Faust info->mr = info->mg = info->mb = info->ma = 0;
5069*c217d954SCole Faust
5070*c217d954SCole Faust if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown");
5071*c217d954SCole Faust if (hsz == 12) {
5072*c217d954SCole Faust s->img_x = stbi__get16le(s);
5073*c217d954SCole Faust s->img_y = stbi__get16le(s);
5074*c217d954SCole Faust } else {
5075*c217d954SCole Faust s->img_x = stbi__get32le(s);
5076*c217d954SCole Faust s->img_y = stbi__get32le(s);
5077*c217d954SCole Faust }
5078*c217d954SCole Faust if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP");
5079*c217d954SCole Faust info->bpp = stbi__get16le(s);
5080*c217d954SCole Faust if (hsz != 12) {
5081*c217d954SCole Faust int compress = stbi__get32le(s);
5082*c217d954SCole Faust if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");
5083*c217d954SCole Faust stbi__get32le(s); // discard sizeof
5084*c217d954SCole Faust stbi__get32le(s); // discard hres
5085*c217d954SCole Faust stbi__get32le(s); // discard vres
5086*c217d954SCole Faust stbi__get32le(s); // discard colorsused
5087*c217d954SCole Faust stbi__get32le(s); // discard max important
5088*c217d954SCole Faust if (hsz == 40 || hsz == 56) {
5089*c217d954SCole Faust if (hsz == 56) {
5090*c217d954SCole Faust stbi__get32le(s);
5091*c217d954SCole Faust stbi__get32le(s);
5092*c217d954SCole Faust stbi__get32le(s);
5093*c217d954SCole Faust stbi__get32le(s);
5094*c217d954SCole Faust }
5095*c217d954SCole Faust if (info->bpp == 16 || info->bpp == 32) {
5096*c217d954SCole Faust if (compress == 0) {
5097*c217d954SCole Faust if (info->bpp == 32) {
5098*c217d954SCole Faust info->mr = 0xffu << 16;
5099*c217d954SCole Faust info->mg = 0xffu << 8;
5100*c217d954SCole Faust info->mb = 0xffu << 0;
5101*c217d954SCole Faust info->ma = 0xffu << 24;
5102*c217d954SCole Faust info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0
5103*c217d954SCole Faust } else {
5104*c217d954SCole Faust info->mr = 31u << 10;
5105*c217d954SCole Faust info->mg = 31u << 5;
5106*c217d954SCole Faust info->mb = 31u << 0;
5107*c217d954SCole Faust }
5108*c217d954SCole Faust } else if (compress == 3) {
5109*c217d954SCole Faust info->mr = stbi__get32le(s);
5110*c217d954SCole Faust info->mg = stbi__get32le(s);
5111*c217d954SCole Faust info->mb = stbi__get32le(s);
5112*c217d954SCole Faust // not documented, but generated by photoshop and handled by mspaint
5113*c217d954SCole Faust if (info->mr == info->mg && info->mg == info->mb) {
5114*c217d954SCole Faust // ?!?!?
5115*c217d954SCole Faust return stbi__errpuc("bad BMP", "bad BMP");
5116*c217d954SCole Faust }
5117*c217d954SCole Faust } else
5118*c217d954SCole Faust return stbi__errpuc("bad BMP", "bad BMP");
5119*c217d954SCole Faust }
5120*c217d954SCole Faust } else {
5121*c217d954SCole Faust int i;
5122*c217d954SCole Faust if (hsz != 108 && hsz != 124)
5123*c217d954SCole Faust return stbi__errpuc("bad BMP", "bad BMP");
5124*c217d954SCole Faust info->mr = stbi__get32le(s);
5125*c217d954SCole Faust info->mg = stbi__get32le(s);
5126*c217d954SCole Faust info->mb = stbi__get32le(s);
5127*c217d954SCole Faust info->ma = stbi__get32le(s);
5128*c217d954SCole Faust stbi__get32le(s); // discard color space
5129*c217d954SCole Faust for (i=0; i < 12; ++i)
5130*c217d954SCole Faust stbi__get32le(s); // discard color space parameters
5131*c217d954SCole Faust if (hsz == 124) {
5132*c217d954SCole Faust stbi__get32le(s); // discard rendering intent
5133*c217d954SCole Faust stbi__get32le(s); // discard offset of profile data
5134*c217d954SCole Faust stbi__get32le(s); // discard size of profile data
5135*c217d954SCole Faust stbi__get32le(s); // discard reserved
5136*c217d954SCole Faust }
5137*c217d954SCole Faust }
5138*c217d954SCole Faust }
5139*c217d954SCole Faust return (void *) 1;
5140*c217d954SCole Faust }
5141*c217d954SCole Faust
5142*c217d954SCole Faust
stbi__bmp_load(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri)5143*c217d954SCole Faust static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
5144*c217d954SCole Faust {
5145*c217d954SCole Faust stbi_uc *out;
5146*c217d954SCole Faust unsigned int mr=0,mg=0,mb=0,ma=0, all_a;
5147*c217d954SCole Faust stbi_uc pal[256][4];
5148*c217d954SCole Faust int psize=0,i,j,width;
5149*c217d954SCole Faust int flip_vertically, pad, target;
5150*c217d954SCole Faust stbi__bmp_data info;
5151*c217d954SCole Faust STBI_NOTUSED(ri);
5152*c217d954SCole Faust
5153*c217d954SCole Faust info.all_a = 255;
5154*c217d954SCole Faust if (stbi__bmp_parse_header(s, &info) == NULL)
5155*c217d954SCole Faust return NULL; // error code already set
5156*c217d954SCole Faust
5157*c217d954SCole Faust flip_vertically = ((int) s->img_y) > 0;
5158*c217d954SCole Faust s->img_y = abs((int) s->img_y);
5159*c217d954SCole Faust
5160*c217d954SCole Faust mr = info.mr;
5161*c217d954SCole Faust mg = info.mg;
5162*c217d954SCole Faust mb = info.mb;
5163*c217d954SCole Faust ma = info.ma;
5164*c217d954SCole Faust all_a = info.all_a;
5165*c217d954SCole Faust
5166*c217d954SCole Faust if (info.hsz == 12) {
5167*c217d954SCole Faust if (info.bpp < 24)
5168*c217d954SCole Faust psize = (info.offset - 14 - 24) / 3;
5169*c217d954SCole Faust } else {
5170*c217d954SCole Faust if (info.bpp < 16)
5171*c217d954SCole Faust psize = (info.offset - 14 - info.hsz) >> 2;
5172*c217d954SCole Faust }
5173*c217d954SCole Faust
5174*c217d954SCole Faust s->img_n = ma ? 4 : 3;
5175*c217d954SCole Faust if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
5176*c217d954SCole Faust target = req_comp;
5177*c217d954SCole Faust else
5178*c217d954SCole Faust target = s->img_n; // if they want monochrome, we'll post-convert
5179*c217d954SCole Faust
5180*c217d954SCole Faust // sanity-check size
5181*c217d954SCole Faust if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0))
5182*c217d954SCole Faust return stbi__errpuc("too large", "Corrupt BMP");
5183*c217d954SCole Faust
5184*c217d954SCole Faust out = (stbi_uc *) stbi__malloc_mad3(target, s->img_x, s->img_y, 0);
5185*c217d954SCole Faust if (!out) return stbi__errpuc("outofmem", "Out of memory");
5186*c217d954SCole Faust if (info.bpp < 16) {
5187*c217d954SCole Faust int z=0;
5188*c217d954SCole Faust if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
5189*c217d954SCole Faust for (i=0; i < psize; ++i) {
5190*c217d954SCole Faust pal[i][2] = stbi__get8(s);
5191*c217d954SCole Faust pal[i][1] = stbi__get8(s);
5192*c217d954SCole Faust pal[i][0] = stbi__get8(s);
5193*c217d954SCole Faust if (info.hsz != 12) stbi__get8(s);
5194*c217d954SCole Faust pal[i][3] = 255;
5195*c217d954SCole Faust }
5196*c217d954SCole Faust stbi__skip(s, info.offset - 14 - info.hsz - psize * (info.hsz == 12 ? 3 : 4));
5197*c217d954SCole Faust if (info.bpp == 1) width = (s->img_x + 7) >> 3;
5198*c217d954SCole Faust else if (info.bpp == 4) width = (s->img_x + 1) >> 1;
5199*c217d954SCole Faust else if (info.bpp == 8) width = s->img_x;
5200*c217d954SCole Faust else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }
5201*c217d954SCole Faust pad = (-width)&3;
5202*c217d954SCole Faust if (info.bpp == 1) {
5203*c217d954SCole Faust for (j=0; j < (int) s->img_y; ++j) {
5204*c217d954SCole Faust int bit_offset = 7, v = stbi__get8(s);
5205*c217d954SCole Faust for (i=0; i < (int) s->img_x; ++i) {
5206*c217d954SCole Faust int color = (v>>bit_offset)&0x1;
5207*c217d954SCole Faust out[z++] = pal[color][0];
5208*c217d954SCole Faust out[z++] = pal[color][1];
5209*c217d954SCole Faust out[z++] = pal[color][2];
5210*c217d954SCole Faust if((--bit_offset) < 0) {
5211*c217d954SCole Faust bit_offset = 7;
5212*c217d954SCole Faust v = stbi__get8(s);
5213*c217d954SCole Faust }
5214*c217d954SCole Faust }
5215*c217d954SCole Faust stbi__skip(s, pad);
5216*c217d954SCole Faust }
5217*c217d954SCole Faust } else {
5218*c217d954SCole Faust for (j=0; j < (int) s->img_y; ++j) {
5219*c217d954SCole Faust for (i=0; i < (int) s->img_x; i += 2) {
5220*c217d954SCole Faust int v=stbi__get8(s),v2=0;
5221*c217d954SCole Faust if (info.bpp == 4) {
5222*c217d954SCole Faust v2 = v & 15;
5223*c217d954SCole Faust v >>= 4;
5224*c217d954SCole Faust }
5225*c217d954SCole Faust out[z++] = pal[v][0];
5226*c217d954SCole Faust out[z++] = pal[v][1];
5227*c217d954SCole Faust out[z++] = pal[v][2];
5228*c217d954SCole Faust if (target == 4) out[z++] = 255;
5229*c217d954SCole Faust if (i+1 == (int) s->img_x) break;
5230*c217d954SCole Faust v = (info.bpp == 8) ? stbi__get8(s) : v2;
5231*c217d954SCole Faust out[z++] = pal[v][0];
5232*c217d954SCole Faust out[z++] = pal[v][1];
5233*c217d954SCole Faust out[z++] = pal[v][2];
5234*c217d954SCole Faust if (target == 4) out[z++] = 255;
5235*c217d954SCole Faust }
5236*c217d954SCole Faust stbi__skip(s, pad);
5237*c217d954SCole Faust }
5238*c217d954SCole Faust }
5239*c217d954SCole Faust } else {
5240*c217d954SCole Faust int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
5241*c217d954SCole Faust int z = 0;
5242*c217d954SCole Faust int easy=0;
5243*c217d954SCole Faust stbi__skip(s, info.offset - 14 - info.hsz);
5244*c217d954SCole Faust if (info.bpp == 24) width = 3 * s->img_x;
5245*c217d954SCole Faust else if (info.bpp == 16) width = 2*s->img_x;
5246*c217d954SCole Faust else /* bpp = 32 and pad = 0 */ width=0;
5247*c217d954SCole Faust pad = (-width) & 3;
5248*c217d954SCole Faust if (info.bpp == 24) {
5249*c217d954SCole Faust easy = 1;
5250*c217d954SCole Faust } else if (info.bpp == 32) {
5251*c217d954SCole Faust if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
5252*c217d954SCole Faust easy = 2;
5253*c217d954SCole Faust }
5254*c217d954SCole Faust if (!easy) {
5255*c217d954SCole Faust if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
5256*c217d954SCole Faust // right shift amt to put high bit in position #7
5257*c217d954SCole Faust rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);
5258*c217d954SCole Faust gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
5259*c217d954SCole Faust bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
5260*c217d954SCole Faust ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
5261*c217d954SCole Faust }
5262*c217d954SCole Faust for (j=0; j < (int) s->img_y; ++j) {
5263*c217d954SCole Faust if (easy) {
5264*c217d954SCole Faust for (i=0; i < (int) s->img_x; ++i) {
5265*c217d954SCole Faust unsigned char a;
5266*c217d954SCole Faust out[z+2] = stbi__get8(s);
5267*c217d954SCole Faust out[z+1] = stbi__get8(s);
5268*c217d954SCole Faust out[z+0] = stbi__get8(s);
5269*c217d954SCole Faust z += 3;
5270*c217d954SCole Faust a = (easy == 2 ? stbi__get8(s) : 255);
5271*c217d954SCole Faust all_a |= a;
5272*c217d954SCole Faust if (target == 4) out[z++] = a;
5273*c217d954SCole Faust }
5274*c217d954SCole Faust } else {
5275*c217d954SCole Faust int bpp = info.bpp;
5276*c217d954SCole Faust for (i=0; i < (int) s->img_x; ++i) {
5277*c217d954SCole Faust stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s));
5278*c217d954SCole Faust unsigned int a;
5279*c217d954SCole Faust out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount));
5280*c217d954SCole Faust out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount));
5281*c217d954SCole Faust out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
5282*c217d954SCole Faust a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
5283*c217d954SCole Faust all_a |= a;
5284*c217d954SCole Faust if (target == 4) out[z++] = STBI__BYTECAST(a);
5285*c217d954SCole Faust }
5286*c217d954SCole Faust }
5287*c217d954SCole Faust stbi__skip(s, pad);
5288*c217d954SCole Faust }
5289*c217d954SCole Faust }
5290*c217d954SCole Faust
5291*c217d954SCole Faust // if alpha channel is all 0s, replace with all 255s
5292*c217d954SCole Faust if (target == 4 && all_a == 0)
5293*c217d954SCole Faust for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4)
5294*c217d954SCole Faust out[i] = 255;
5295*c217d954SCole Faust
5296*c217d954SCole Faust if (flip_vertically) {
5297*c217d954SCole Faust stbi_uc t;
5298*c217d954SCole Faust for (j=0; j < (int) s->img_y>>1; ++j) {
5299*c217d954SCole Faust stbi_uc *p1 = out + j *s->img_x*target;
5300*c217d954SCole Faust stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
5301*c217d954SCole Faust for (i=0; i < (int) s->img_x*target; ++i) {
5302*c217d954SCole Faust t = p1[i], p1[i] = p2[i], p2[i] = t;
5303*c217d954SCole Faust }
5304*c217d954SCole Faust }
5305*c217d954SCole Faust }
5306*c217d954SCole Faust
5307*c217d954SCole Faust if (req_comp && req_comp != target) {
5308*c217d954SCole Faust out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
5309*c217d954SCole Faust if (out == NULL) return out; // stbi__convert_format frees input on failure
5310*c217d954SCole Faust }
5311*c217d954SCole Faust
5312*c217d954SCole Faust *x = s->img_x;
5313*c217d954SCole Faust *y = s->img_y;
5314*c217d954SCole Faust if (comp) *comp = s->img_n;
5315*c217d954SCole Faust return out;
5316*c217d954SCole Faust }
5317*c217d954SCole Faust #endif
5318*c217d954SCole Faust
5319*c217d954SCole Faust // Targa Truevision - TGA
5320*c217d954SCole Faust // by Jonathan Dummer
5321*c217d954SCole Faust #ifndef STBI_NO_TGA
5322*c217d954SCole Faust // returns STBI_rgb or whatever, 0 on error
stbi__tga_get_comp(int bits_per_pixel,int is_grey,int * is_rgb16)5323*c217d954SCole Faust static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16)
5324*c217d954SCole Faust {
5325*c217d954SCole Faust // only RGB or RGBA (incl. 16bit) or grey allowed
5326*c217d954SCole Faust if (is_rgb16) *is_rgb16 = 0;
5327*c217d954SCole Faust switch(bits_per_pixel) {
5328*c217d954SCole Faust case 8: return STBI_grey;
5329*c217d954SCole Faust case 16: if(is_grey) return STBI_grey_alpha;
5330*c217d954SCole Faust // fallthrough
5331*c217d954SCole Faust case 15: if(is_rgb16) *is_rgb16 = 1;
5332*c217d954SCole Faust return STBI_rgb;
5333*c217d954SCole Faust case 24: // fallthrough
5334*c217d954SCole Faust case 32: return bits_per_pixel/8;
5335*c217d954SCole Faust default: return 0;
5336*c217d954SCole Faust }
5337*c217d954SCole Faust }
5338*c217d954SCole Faust
stbi__tga_info(stbi__context * s,int * x,int * y,int * comp)5339*c217d954SCole Faust static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp)
5340*c217d954SCole Faust {
5341*c217d954SCole Faust int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp;
5342*c217d954SCole Faust int sz, tga_colormap_type;
5343*c217d954SCole Faust stbi__get8(s); // discard Offset
5344*c217d954SCole Faust tga_colormap_type = stbi__get8(s); // colormap type
5345*c217d954SCole Faust if( tga_colormap_type > 1 ) {
5346*c217d954SCole Faust stbi__rewind(s);
5347*c217d954SCole Faust return 0; // only RGB or indexed allowed
5348*c217d954SCole Faust }
5349*c217d954SCole Faust tga_image_type = stbi__get8(s); // image type
5350*c217d954SCole Faust if ( tga_colormap_type == 1 ) { // colormapped (paletted) image
5351*c217d954SCole Faust if (tga_image_type != 1 && tga_image_type != 9) {
5352*c217d954SCole Faust stbi__rewind(s);
5353*c217d954SCole Faust return 0;
5354*c217d954SCole Faust }
5355*c217d954SCole Faust stbi__skip(s,4); // skip index of first colormap entry and number of entries
5356*c217d954SCole Faust sz = stbi__get8(s); // check bits per palette color entry
5357*c217d954SCole Faust if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) {
5358*c217d954SCole Faust stbi__rewind(s);
5359*c217d954SCole Faust return 0;
5360*c217d954SCole Faust }
5361*c217d954SCole Faust stbi__skip(s,4); // skip image x and y origin
5362*c217d954SCole Faust tga_colormap_bpp = sz;
5363*c217d954SCole Faust } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE
5364*c217d954SCole Faust if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) {
5365*c217d954SCole Faust stbi__rewind(s);
5366*c217d954SCole Faust return 0; // only RGB or grey allowed, +/- RLE
5367*c217d954SCole Faust }
5368*c217d954SCole Faust stbi__skip(s,9); // skip colormap specification and image x/y origin
5369*c217d954SCole Faust tga_colormap_bpp = 0;
5370*c217d954SCole Faust }
5371*c217d954SCole Faust tga_w = stbi__get16le(s);
5372*c217d954SCole Faust if( tga_w < 1 ) {
5373*c217d954SCole Faust stbi__rewind(s);
5374*c217d954SCole Faust return 0; // test width
5375*c217d954SCole Faust }
5376*c217d954SCole Faust tga_h = stbi__get16le(s);
5377*c217d954SCole Faust if( tga_h < 1 ) {
5378*c217d954SCole Faust stbi__rewind(s);
5379*c217d954SCole Faust return 0; // test height
5380*c217d954SCole Faust }
5381*c217d954SCole Faust tga_bits_per_pixel = stbi__get8(s); // bits per pixel
5382*c217d954SCole Faust stbi__get8(s); // ignore alpha bits
5383*c217d954SCole Faust if (tga_colormap_bpp != 0) {
5384*c217d954SCole Faust if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) {
5385*c217d954SCole Faust // when using a colormap, tga_bits_per_pixel is the size of the indexes
5386*c217d954SCole Faust // I don't think anything but 8 or 16bit indexes makes sense
5387*c217d954SCole Faust stbi__rewind(s);
5388*c217d954SCole Faust return 0;
5389*c217d954SCole Faust }
5390*c217d954SCole Faust tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
5391*c217d954SCole Faust } else {
5392*c217d954SCole Faust tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
5393*c217d954SCole Faust }
5394*c217d954SCole Faust if(!tga_comp) {
5395*c217d954SCole Faust stbi__rewind(s);
5396*c217d954SCole Faust return 0;
5397*c217d954SCole Faust }
5398*c217d954SCole Faust if (x) *x = tga_w;
5399*c217d954SCole Faust if (y) *y = tga_h;
5400*c217d954SCole Faust if (comp) *comp = tga_comp;
5401*c217d954SCole Faust return 1; // seems to have passed everything
5402*c217d954SCole Faust }
5403*c217d954SCole Faust
stbi__tga_test(stbi__context * s)5404*c217d954SCole Faust static int stbi__tga_test(stbi__context *s)
5405*c217d954SCole Faust {
5406*c217d954SCole Faust int res = 0;
5407*c217d954SCole Faust int sz, tga_color_type;
5408*c217d954SCole Faust stbi__get8(s); // discard Offset
5409*c217d954SCole Faust tga_color_type = stbi__get8(s); // color type
5410*c217d954SCole Faust if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed
5411*c217d954SCole Faust sz = stbi__get8(s); // image type
5412*c217d954SCole Faust if ( tga_color_type == 1 ) { // colormapped (paletted) image
5413*c217d954SCole Faust if (sz != 1 && sz != 9) goto errorEnd; // colortype 1 demands image type 1 or 9
5414*c217d954SCole Faust stbi__skip(s,4); // skip index of first colormap entry and number of entries
5415*c217d954SCole Faust sz = stbi__get8(s); // check bits per palette color entry
5416*c217d954SCole Faust if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
5417*c217d954SCole Faust stbi__skip(s,4); // skip image x and y origin
5418*c217d954SCole Faust } else { // "normal" image w/o colormap
5419*c217d954SCole Faust if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE
5420*c217d954SCole Faust stbi__skip(s,9); // skip colormap specification and image x/y origin
5421*c217d954SCole Faust }
5422*c217d954SCole Faust if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width
5423*c217d954SCole Faust if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height
5424*c217d954SCole Faust sz = stbi__get8(s); // bits per pixel
5425*c217d954SCole Faust if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index
5426*c217d954SCole Faust if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
5427*c217d954SCole Faust
5428*c217d954SCole Faust res = 1; // if we got this far, everything's good and we can return 1 instead of 0
5429*c217d954SCole Faust
5430*c217d954SCole Faust errorEnd:
5431*c217d954SCole Faust stbi__rewind(s);
5432*c217d954SCole Faust return res;
5433*c217d954SCole Faust }
5434*c217d954SCole Faust
5435*c217d954SCole Faust // read 16bit value and convert to 24bit RGB
stbi__tga_read_rgb16(stbi__context * s,stbi_uc * out)5436*c217d954SCole Faust static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out)
5437*c217d954SCole Faust {
5438*c217d954SCole Faust stbi__uint16 px = (stbi__uint16)stbi__get16le(s);
5439*c217d954SCole Faust stbi__uint16 fiveBitMask = 31;
5440*c217d954SCole Faust // we have 3 channels with 5bits each
5441*c217d954SCole Faust int r = (px >> 10) & fiveBitMask;
5442*c217d954SCole Faust int g = (px >> 5) & fiveBitMask;
5443*c217d954SCole Faust int b = px & fiveBitMask;
5444*c217d954SCole Faust // Note that this saves the data in RGB(A) order, so it doesn't need to be swapped later
5445*c217d954SCole Faust out[0] = (stbi_uc)((r * 255)/31);
5446*c217d954SCole Faust out[1] = (stbi_uc)((g * 255)/31);
5447*c217d954SCole Faust out[2] = (stbi_uc)((b * 255)/31);
5448*c217d954SCole Faust
5449*c217d954SCole Faust // some people claim that the most significant bit might be used for alpha
5450*c217d954SCole Faust // (possibly if an alpha-bit is set in the "image descriptor byte")
5451*c217d954SCole Faust // but that only made 16bit test images completely translucent..
5452*c217d954SCole Faust // so let's treat all 15 and 16bit TGAs as RGB with no alpha.
5453*c217d954SCole Faust }
5454*c217d954SCole Faust
stbi__tga_load(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri)5455*c217d954SCole Faust static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
5456*c217d954SCole Faust {
5457*c217d954SCole Faust // read in the TGA header stuff
5458*c217d954SCole Faust int tga_offset = stbi__get8(s);
5459*c217d954SCole Faust int tga_indexed = stbi__get8(s);
5460*c217d954SCole Faust int tga_image_type = stbi__get8(s);
5461*c217d954SCole Faust int tga_is_RLE = 0;
5462*c217d954SCole Faust int tga_palette_start = stbi__get16le(s);
5463*c217d954SCole Faust int tga_palette_len = stbi__get16le(s);
5464*c217d954SCole Faust int tga_palette_bits = stbi__get8(s);
5465*c217d954SCole Faust int tga_x_origin = stbi__get16le(s);
5466*c217d954SCole Faust int tga_y_origin = stbi__get16le(s);
5467*c217d954SCole Faust int tga_width = stbi__get16le(s);
5468*c217d954SCole Faust int tga_height = stbi__get16le(s);
5469*c217d954SCole Faust int tga_bits_per_pixel = stbi__get8(s);
5470*c217d954SCole Faust int tga_comp, tga_rgb16=0;
5471*c217d954SCole Faust int tga_inverted = stbi__get8(s);
5472*c217d954SCole Faust // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?)
5473*c217d954SCole Faust // image data
5474*c217d954SCole Faust unsigned char *tga_data;
5475*c217d954SCole Faust unsigned char *tga_palette = NULL;
5476*c217d954SCole Faust int i, j;
5477*c217d954SCole Faust unsigned char raw_data[4] = {0};
5478*c217d954SCole Faust int RLE_count = 0;
5479*c217d954SCole Faust int RLE_repeating = 0;
5480*c217d954SCole Faust int read_next_pixel = 1;
5481*c217d954SCole Faust STBI_NOTUSED(ri);
5482*c217d954SCole Faust
5483*c217d954SCole Faust // do a tiny bit of precessing
5484*c217d954SCole Faust if ( tga_image_type >= 8 )
5485*c217d954SCole Faust {
5486*c217d954SCole Faust tga_image_type -= 8;
5487*c217d954SCole Faust tga_is_RLE = 1;
5488*c217d954SCole Faust }
5489*c217d954SCole Faust tga_inverted = 1 - ((tga_inverted >> 5) & 1);
5490*c217d954SCole Faust
5491*c217d954SCole Faust // If I'm paletted, then I'll use the number of bits from the palette
5492*c217d954SCole Faust if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
5493*c217d954SCole Faust else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16);
5494*c217d954SCole Faust
5495*c217d954SCole Faust if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency
5496*c217d954SCole Faust return stbi__errpuc("bad format", "Can't find out TGA pixelformat");
5497*c217d954SCole Faust
5498*c217d954SCole Faust // tga info
5499*c217d954SCole Faust *x = tga_width;
5500*c217d954SCole Faust *y = tga_height;
5501*c217d954SCole Faust if (comp) *comp = tga_comp;
5502*c217d954SCole Faust
5503*c217d954SCole Faust if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0))
5504*c217d954SCole Faust return stbi__errpuc("too large", "Corrupt TGA");
5505*c217d954SCole Faust
5506*c217d954SCole Faust tga_data = (unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0);
5507*c217d954SCole Faust if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");
5508*c217d954SCole Faust
5509*c217d954SCole Faust // skip to the data's starting position (offset usually = 0)
5510*c217d954SCole Faust stbi__skip(s, tga_offset );
5511*c217d954SCole Faust
5512*c217d954SCole Faust if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) {
5513*c217d954SCole Faust for (i=0; i < tga_height; ++i) {
5514*c217d954SCole Faust int row = tga_inverted ? tga_height -i - 1 : i;
5515*c217d954SCole Faust stbi_uc *tga_row = tga_data + row*tga_width*tga_comp;
5516*c217d954SCole Faust stbi__getn(s, tga_row, tga_width * tga_comp);
5517*c217d954SCole Faust }
5518*c217d954SCole Faust } else {
5519*c217d954SCole Faust // do I need to load a palette?
5520*c217d954SCole Faust if ( tga_indexed)
5521*c217d954SCole Faust {
5522*c217d954SCole Faust // any data to skip? (offset usually = 0)
5523*c217d954SCole Faust stbi__skip(s, tga_palette_start );
5524*c217d954SCole Faust // load the palette
5525*c217d954SCole Faust tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0);
5526*c217d954SCole Faust if (!tga_palette) {
5527*c217d954SCole Faust STBI_FREE(tga_data);
5528*c217d954SCole Faust return stbi__errpuc("outofmem", "Out of memory");
5529*c217d954SCole Faust }
5530*c217d954SCole Faust if (tga_rgb16) {
5531*c217d954SCole Faust stbi_uc *pal_entry = tga_palette;
5532*c217d954SCole Faust STBI_ASSERT(tga_comp == STBI_rgb);
5533*c217d954SCole Faust for (i=0; i < tga_palette_len; ++i) {
5534*c217d954SCole Faust stbi__tga_read_rgb16(s, pal_entry);
5535*c217d954SCole Faust pal_entry += tga_comp;
5536*c217d954SCole Faust }
5537*c217d954SCole Faust } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
5538*c217d954SCole Faust STBI_FREE(tga_data);
5539*c217d954SCole Faust STBI_FREE(tga_palette);
5540*c217d954SCole Faust return stbi__errpuc("bad palette", "Corrupt TGA");
5541*c217d954SCole Faust }
5542*c217d954SCole Faust }
5543*c217d954SCole Faust // load the data
5544*c217d954SCole Faust for (i=0; i < tga_width * tga_height; ++i)
5545*c217d954SCole Faust {
5546*c217d954SCole Faust // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk?
5547*c217d954SCole Faust if ( tga_is_RLE )
5548*c217d954SCole Faust {
5549*c217d954SCole Faust if ( RLE_count == 0 )
5550*c217d954SCole Faust {
5551*c217d954SCole Faust // yep, get the next byte as a RLE command
5552*c217d954SCole Faust int RLE_cmd = stbi__get8(s);
5553*c217d954SCole Faust RLE_count = 1 + (RLE_cmd & 127);
5554*c217d954SCole Faust RLE_repeating = RLE_cmd >> 7;
5555*c217d954SCole Faust read_next_pixel = 1;
5556*c217d954SCole Faust } else if ( !RLE_repeating )
5557*c217d954SCole Faust {
5558*c217d954SCole Faust read_next_pixel = 1;
5559*c217d954SCole Faust }
5560*c217d954SCole Faust } else
5561*c217d954SCole Faust {
5562*c217d954SCole Faust read_next_pixel = 1;
5563*c217d954SCole Faust }
5564*c217d954SCole Faust // OK, if I need to read a pixel, do it now
5565*c217d954SCole Faust if ( read_next_pixel )
5566*c217d954SCole Faust {
5567*c217d954SCole Faust // load however much data we did have
5568*c217d954SCole Faust if ( tga_indexed )
5569*c217d954SCole Faust {
5570*c217d954SCole Faust // read in index, then perform the lookup
5571*c217d954SCole Faust int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s);
5572*c217d954SCole Faust if ( pal_idx >= tga_palette_len ) {
5573*c217d954SCole Faust // invalid index
5574*c217d954SCole Faust pal_idx = 0;
5575*c217d954SCole Faust }
5576*c217d954SCole Faust pal_idx *= tga_comp;
5577*c217d954SCole Faust for (j = 0; j < tga_comp; ++j) {
5578*c217d954SCole Faust raw_data[j] = tga_palette[pal_idx+j];
5579*c217d954SCole Faust }
5580*c217d954SCole Faust } else if(tga_rgb16) {
5581*c217d954SCole Faust STBI_ASSERT(tga_comp == STBI_rgb);
5582*c217d954SCole Faust stbi__tga_read_rgb16(s, raw_data);
5583*c217d954SCole Faust } else {
5584*c217d954SCole Faust // read in the data raw
5585*c217d954SCole Faust for (j = 0; j < tga_comp; ++j) {
5586*c217d954SCole Faust raw_data[j] = stbi__get8(s);
5587*c217d954SCole Faust }
5588*c217d954SCole Faust }
5589*c217d954SCole Faust // clear the reading flag for the next pixel
5590*c217d954SCole Faust read_next_pixel = 0;
5591*c217d954SCole Faust } // end of reading a pixel
5592*c217d954SCole Faust
5593*c217d954SCole Faust // copy data
5594*c217d954SCole Faust for (j = 0; j < tga_comp; ++j)
5595*c217d954SCole Faust tga_data[i*tga_comp+j] = raw_data[j];
5596*c217d954SCole Faust
5597*c217d954SCole Faust // in case we're in RLE mode, keep counting down
5598*c217d954SCole Faust --RLE_count;
5599*c217d954SCole Faust }
5600*c217d954SCole Faust // do I need to invert the image?
5601*c217d954SCole Faust if ( tga_inverted )
5602*c217d954SCole Faust {
5603*c217d954SCole Faust for (j = 0; j*2 < tga_height; ++j)
5604*c217d954SCole Faust {
5605*c217d954SCole Faust int index1 = j * tga_width * tga_comp;
5606*c217d954SCole Faust int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
5607*c217d954SCole Faust for (i = tga_width * tga_comp; i > 0; --i)
5608*c217d954SCole Faust {
5609*c217d954SCole Faust unsigned char temp = tga_data[index1];
5610*c217d954SCole Faust tga_data[index1] = tga_data[index2];
5611*c217d954SCole Faust tga_data[index2] = temp;
5612*c217d954SCole Faust ++index1;
5613*c217d954SCole Faust ++index2;
5614*c217d954SCole Faust }
5615*c217d954SCole Faust }
5616*c217d954SCole Faust }
5617*c217d954SCole Faust // clear my palette, if I had one
5618*c217d954SCole Faust if ( tga_palette != NULL )
5619*c217d954SCole Faust {
5620*c217d954SCole Faust STBI_FREE( tga_palette );
5621*c217d954SCole Faust }
5622*c217d954SCole Faust }
5623*c217d954SCole Faust
5624*c217d954SCole Faust // swap RGB - if the source data was RGB16, it already is in the right order
5625*c217d954SCole Faust if (tga_comp >= 3 && !tga_rgb16)
5626*c217d954SCole Faust {
5627*c217d954SCole Faust unsigned char* tga_pixel = tga_data;
5628*c217d954SCole Faust for (i=0; i < tga_width * tga_height; ++i)
5629*c217d954SCole Faust {
5630*c217d954SCole Faust unsigned char temp = tga_pixel[0];
5631*c217d954SCole Faust tga_pixel[0] = tga_pixel[2];
5632*c217d954SCole Faust tga_pixel[2] = temp;
5633*c217d954SCole Faust tga_pixel += tga_comp;
5634*c217d954SCole Faust }
5635*c217d954SCole Faust }
5636*c217d954SCole Faust
5637*c217d954SCole Faust // convert to target component count
5638*c217d954SCole Faust if (req_comp && req_comp != tga_comp)
5639*c217d954SCole Faust tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
5640*c217d954SCole Faust
5641*c217d954SCole Faust // the things I do to get rid of an error message, and yet keep
5642*c217d954SCole Faust // Microsoft's C compilers happy... [8^(
5643*c217d954SCole Faust tga_palette_start = tga_palette_len = tga_palette_bits =
5644*c217d954SCole Faust tga_x_origin = tga_y_origin = 0;
5645*c217d954SCole Faust // OK, done
5646*c217d954SCole Faust return tga_data;
5647*c217d954SCole Faust }
5648*c217d954SCole Faust #endif
5649*c217d954SCole Faust
5650*c217d954SCole Faust // *************************************************************************************************
5651*c217d954SCole Faust // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
5652*c217d954SCole Faust
5653*c217d954SCole Faust #ifndef STBI_NO_PSD
stbi__psd_test(stbi__context * s)5654*c217d954SCole Faust static int stbi__psd_test(stbi__context *s)
5655*c217d954SCole Faust {
5656*c217d954SCole Faust int r = (stbi__get32be(s) == 0x38425053);
5657*c217d954SCole Faust stbi__rewind(s);
5658*c217d954SCole Faust return r;
5659*c217d954SCole Faust }
5660*c217d954SCole Faust
stbi__psd_decode_rle(stbi__context * s,stbi_uc * p,int pixelCount)5661*c217d954SCole Faust static int stbi__psd_decode_rle(stbi__context *s, stbi_uc *p, int pixelCount)
5662*c217d954SCole Faust {
5663*c217d954SCole Faust int count, nleft, len;
5664*c217d954SCole Faust
5665*c217d954SCole Faust count = 0;
5666*c217d954SCole Faust while ((nleft = pixelCount - count) > 0) {
5667*c217d954SCole Faust len = stbi__get8(s);
5668*c217d954SCole Faust if (len == 128) {
5669*c217d954SCole Faust // No-op.
5670*c217d954SCole Faust } else if (len < 128) {
5671*c217d954SCole Faust // Copy next len+1 bytes literally.
5672*c217d954SCole Faust len++;
5673*c217d954SCole Faust if (len > nleft) return 0; // corrupt data
5674*c217d954SCole Faust count += len;
5675*c217d954SCole Faust while (len) {
5676*c217d954SCole Faust *p = stbi__get8(s);
5677*c217d954SCole Faust p += 4;
5678*c217d954SCole Faust len--;
5679*c217d954SCole Faust }
5680*c217d954SCole Faust } else if (len > 128) {
5681*c217d954SCole Faust stbi_uc val;
5682*c217d954SCole Faust // Next -len+1 bytes in the dest are replicated from next source byte.
5683*c217d954SCole Faust // (Interpret len as a negative 8-bit int.)
5684*c217d954SCole Faust len = 257 - len;
5685*c217d954SCole Faust if (len > nleft) return 0; // corrupt data
5686*c217d954SCole Faust val = stbi__get8(s);
5687*c217d954SCole Faust count += len;
5688*c217d954SCole Faust while (len) {
5689*c217d954SCole Faust *p = val;
5690*c217d954SCole Faust p += 4;
5691*c217d954SCole Faust len--;
5692*c217d954SCole Faust }
5693*c217d954SCole Faust }
5694*c217d954SCole Faust }
5695*c217d954SCole Faust
5696*c217d954SCole Faust return 1;
5697*c217d954SCole Faust }
5698*c217d954SCole Faust
stbi__psd_load(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri,int bpc)5699*c217d954SCole Faust static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
5700*c217d954SCole Faust {
5701*c217d954SCole Faust int pixelCount;
5702*c217d954SCole Faust int channelCount, compression;
5703*c217d954SCole Faust int channel, i;
5704*c217d954SCole Faust int bitdepth;
5705*c217d954SCole Faust int w,h;
5706*c217d954SCole Faust stbi_uc *out;
5707*c217d954SCole Faust STBI_NOTUSED(ri);
5708*c217d954SCole Faust
5709*c217d954SCole Faust // Check identifier
5710*c217d954SCole Faust if (stbi__get32be(s) != 0x38425053) // "8BPS"
5711*c217d954SCole Faust return stbi__errpuc("not PSD", "Corrupt PSD image");
5712*c217d954SCole Faust
5713*c217d954SCole Faust // Check file type version.
5714*c217d954SCole Faust if (stbi__get16be(s) != 1)
5715*c217d954SCole Faust return stbi__errpuc("wrong version", "Unsupported version of PSD image");
5716*c217d954SCole Faust
5717*c217d954SCole Faust // Skip 6 reserved bytes.
5718*c217d954SCole Faust stbi__skip(s, 6 );
5719*c217d954SCole Faust
5720*c217d954SCole Faust // Read the number of channels (R, G, B, A, etc).
5721*c217d954SCole Faust channelCount = stbi__get16be(s);
5722*c217d954SCole Faust if (channelCount < 0 || channelCount > 16)
5723*c217d954SCole Faust return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image");
5724*c217d954SCole Faust
5725*c217d954SCole Faust // Read the rows and columns of the image.
5726*c217d954SCole Faust h = stbi__get32be(s);
5727*c217d954SCole Faust w = stbi__get32be(s);
5728*c217d954SCole Faust
5729*c217d954SCole Faust // Make sure the depth is 8 bits.
5730*c217d954SCole Faust bitdepth = stbi__get16be(s);
5731*c217d954SCole Faust if (bitdepth != 8 && bitdepth != 16)
5732*c217d954SCole Faust return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit");
5733*c217d954SCole Faust
5734*c217d954SCole Faust // Make sure the color mode is RGB.
5735*c217d954SCole Faust // Valid options are:
5736*c217d954SCole Faust // 0: Bitmap
5737*c217d954SCole Faust // 1: Grayscale
5738*c217d954SCole Faust // 2: Indexed color
5739*c217d954SCole Faust // 3: RGB color
5740*c217d954SCole Faust // 4: CMYK color
5741*c217d954SCole Faust // 7: Multichannel
5742*c217d954SCole Faust // 8: Duotone
5743*c217d954SCole Faust // 9: Lab color
5744*c217d954SCole Faust if (stbi__get16be(s) != 3)
5745*c217d954SCole Faust return stbi__errpuc("wrong color format", "PSD is not in RGB color format");
5746*c217d954SCole Faust
5747*c217d954SCole Faust // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.)
5748*c217d954SCole Faust stbi__skip(s,stbi__get32be(s) );
5749*c217d954SCole Faust
5750*c217d954SCole Faust // Skip the image resources. (resolution, pen tool paths, etc)
5751*c217d954SCole Faust stbi__skip(s, stbi__get32be(s) );
5752*c217d954SCole Faust
5753*c217d954SCole Faust // Skip the reserved data.
5754*c217d954SCole Faust stbi__skip(s, stbi__get32be(s) );
5755*c217d954SCole Faust
5756*c217d954SCole Faust // Find out if the data is compressed.
5757*c217d954SCole Faust // Known values:
5758*c217d954SCole Faust // 0: no compression
5759*c217d954SCole Faust // 1: RLE compressed
5760*c217d954SCole Faust compression = stbi__get16be(s);
5761*c217d954SCole Faust if (compression > 1)
5762*c217d954SCole Faust return stbi__errpuc("bad compression", "PSD has an unknown compression format");
5763*c217d954SCole Faust
5764*c217d954SCole Faust // Check size
5765*c217d954SCole Faust if (!stbi__mad3sizes_valid(4, w, h, 0))
5766*c217d954SCole Faust return stbi__errpuc("too large", "Corrupt PSD");
5767*c217d954SCole Faust
5768*c217d954SCole Faust // Create the destination image.
5769*c217d954SCole Faust
5770*c217d954SCole Faust if (!compression && bitdepth == 16 && bpc == 16) {
5771*c217d954SCole Faust out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0);
5772*c217d954SCole Faust ri->bits_per_channel = 16;
5773*c217d954SCole Faust } else
5774*c217d954SCole Faust out = (stbi_uc *) stbi__malloc(4 * w*h);
5775*c217d954SCole Faust
5776*c217d954SCole Faust if (!out) return stbi__errpuc("outofmem", "Out of memory");
5777*c217d954SCole Faust pixelCount = w*h;
5778*c217d954SCole Faust
5779*c217d954SCole Faust // Initialize the data to zero.
5780*c217d954SCole Faust //memset( out, 0, pixelCount * 4 );
5781*c217d954SCole Faust
5782*c217d954SCole Faust // Finally, the image data.
5783*c217d954SCole Faust if (compression) {
5784*c217d954SCole Faust // RLE as used by .PSD and .TIFF
5785*c217d954SCole Faust // Loop until you get the number of unpacked bytes you are expecting:
5786*c217d954SCole Faust // Read the next source byte into n.
5787*c217d954SCole Faust // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
5788*c217d954SCole Faust // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
5789*c217d954SCole Faust // Else if n is 128, noop.
5790*c217d954SCole Faust // Endloop
5791*c217d954SCole Faust
5792*c217d954SCole Faust // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
5793*c217d954SCole Faust // which we're going to just skip.
5794*c217d954SCole Faust stbi__skip(s, h * channelCount * 2 );
5795*c217d954SCole Faust
5796*c217d954SCole Faust // Read the RLE data by channel.
5797*c217d954SCole Faust for (channel = 0; channel < 4; channel++) {
5798*c217d954SCole Faust stbi_uc *p;
5799*c217d954SCole Faust
5800*c217d954SCole Faust p = out+channel;
5801*c217d954SCole Faust if (channel >= channelCount) {
5802*c217d954SCole Faust // Fill this channel with default data.
5803*c217d954SCole Faust for (i = 0; i < pixelCount; i++, p += 4)
5804*c217d954SCole Faust *p = (channel == 3 ? 255 : 0);
5805*c217d954SCole Faust } else {
5806*c217d954SCole Faust // Read the RLE data.
5807*c217d954SCole Faust if (!stbi__psd_decode_rle(s, p, pixelCount)) {
5808*c217d954SCole Faust STBI_FREE(out);
5809*c217d954SCole Faust return stbi__errpuc("corrupt", "bad RLE data");
5810*c217d954SCole Faust }
5811*c217d954SCole Faust }
5812*c217d954SCole Faust }
5813*c217d954SCole Faust
5814*c217d954SCole Faust } else {
5815*c217d954SCole Faust // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
5816*c217d954SCole Faust // where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image.
5817*c217d954SCole Faust
5818*c217d954SCole Faust // Read the data by channel.
5819*c217d954SCole Faust for (channel = 0; channel < 4; channel++) {
5820*c217d954SCole Faust if (channel >= channelCount) {
5821*c217d954SCole Faust // Fill this channel with default data.
5822*c217d954SCole Faust if (bitdepth == 16 && bpc == 16) {
5823*c217d954SCole Faust stbi__uint16 *q = ((stbi__uint16 *) out) + channel;
5824*c217d954SCole Faust stbi__uint16 val = channel == 3 ? 65535 : 0;
5825*c217d954SCole Faust for (i = 0; i < pixelCount; i++, q += 4)
5826*c217d954SCole Faust *q = val;
5827*c217d954SCole Faust } else {
5828*c217d954SCole Faust stbi_uc *p = out+channel;
5829*c217d954SCole Faust stbi_uc val = channel == 3 ? 255 : 0;
5830*c217d954SCole Faust for (i = 0; i < pixelCount; i++, p += 4)
5831*c217d954SCole Faust *p = val;
5832*c217d954SCole Faust }
5833*c217d954SCole Faust } else {
5834*c217d954SCole Faust if (ri->bits_per_channel == 16) { // output bpc
5835*c217d954SCole Faust stbi__uint16 *q = ((stbi__uint16 *) out) + channel;
5836*c217d954SCole Faust for (i = 0; i < pixelCount; i++, q += 4)
5837*c217d954SCole Faust *q = (stbi__uint16) stbi__get16be(s);
5838*c217d954SCole Faust } else {
5839*c217d954SCole Faust stbi_uc *p = out+channel;
5840*c217d954SCole Faust if (bitdepth == 16) { // input bpc
5841*c217d954SCole Faust for (i = 0; i < pixelCount; i++, p += 4)
5842*c217d954SCole Faust *p = (stbi_uc) (stbi__get16be(s) >> 8);
5843*c217d954SCole Faust } else {
5844*c217d954SCole Faust for (i = 0; i < pixelCount; i++, p += 4)
5845*c217d954SCole Faust *p = stbi__get8(s);
5846*c217d954SCole Faust }
5847*c217d954SCole Faust }
5848*c217d954SCole Faust }
5849*c217d954SCole Faust }
5850*c217d954SCole Faust }
5851*c217d954SCole Faust
5852*c217d954SCole Faust // remove weird white matte from PSD
5853*c217d954SCole Faust if (channelCount >= 4) {
5854*c217d954SCole Faust if (ri->bits_per_channel == 16) {
5855*c217d954SCole Faust for (i=0; i < w*h; ++i) {
5856*c217d954SCole Faust stbi__uint16 *pixel = (stbi__uint16 *) out + 4*i;
5857*c217d954SCole Faust if (pixel[3] != 0 && pixel[3] != 65535) {
5858*c217d954SCole Faust float a = pixel[3] / 65535.0f;
5859*c217d954SCole Faust float ra = 1.0f / a;
5860*c217d954SCole Faust float inv_a = 65535.0f * (1 - ra);
5861*c217d954SCole Faust pixel[0] = (stbi__uint16) (pixel[0]*ra + inv_a);
5862*c217d954SCole Faust pixel[1] = (stbi__uint16) (pixel[1]*ra + inv_a);
5863*c217d954SCole Faust pixel[2] = (stbi__uint16) (pixel[2]*ra + inv_a);
5864*c217d954SCole Faust }
5865*c217d954SCole Faust }
5866*c217d954SCole Faust } else {
5867*c217d954SCole Faust for (i=0; i < w*h; ++i) {
5868*c217d954SCole Faust unsigned char *pixel = out + 4*i;
5869*c217d954SCole Faust if (pixel[3] != 0 && pixel[3] != 255) {
5870*c217d954SCole Faust float a = pixel[3] / 255.0f;
5871*c217d954SCole Faust float ra = 1.0f / a;
5872*c217d954SCole Faust float inv_a = 255.0f * (1 - ra);
5873*c217d954SCole Faust pixel[0] = (unsigned char) (pixel[0]*ra + inv_a);
5874*c217d954SCole Faust pixel[1] = (unsigned char) (pixel[1]*ra + inv_a);
5875*c217d954SCole Faust pixel[2] = (unsigned char) (pixel[2]*ra + inv_a);
5876*c217d954SCole Faust }
5877*c217d954SCole Faust }
5878*c217d954SCole Faust }
5879*c217d954SCole Faust }
5880*c217d954SCole Faust
5881*c217d954SCole Faust // convert to desired output format
5882*c217d954SCole Faust if (req_comp && req_comp != 4) {
5883*c217d954SCole Faust if (ri->bits_per_channel == 16)
5884*c217d954SCole Faust out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, 4, req_comp, w, h);
5885*c217d954SCole Faust else
5886*c217d954SCole Faust out = stbi__convert_format(out, 4, req_comp, w, h);
5887*c217d954SCole Faust if (out == NULL) return out; // stbi__convert_format frees input on failure
5888*c217d954SCole Faust }
5889*c217d954SCole Faust
5890*c217d954SCole Faust if (comp) *comp = 4;
5891*c217d954SCole Faust *y = h;
5892*c217d954SCole Faust *x = w;
5893*c217d954SCole Faust
5894*c217d954SCole Faust return out;
5895*c217d954SCole Faust }
5896*c217d954SCole Faust #endif
5897*c217d954SCole Faust
5898*c217d954SCole Faust // *************************************************************************************************
5899*c217d954SCole Faust // Softimage PIC loader
5900*c217d954SCole Faust // by Tom Seddon
5901*c217d954SCole Faust //
5902*c217d954SCole Faust // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
5903*c217d954SCole Faust // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
5904*c217d954SCole Faust
5905*c217d954SCole Faust #ifndef STBI_NO_PIC
stbi__pic_is4(stbi__context * s,const char * str)5906*c217d954SCole Faust static int stbi__pic_is4(stbi__context *s,const char *str)
5907*c217d954SCole Faust {
5908*c217d954SCole Faust int i;
5909*c217d954SCole Faust for (i=0; i<4; ++i)
5910*c217d954SCole Faust if (stbi__get8(s) != (stbi_uc)str[i])
5911*c217d954SCole Faust return 0;
5912*c217d954SCole Faust
5913*c217d954SCole Faust return 1;
5914*c217d954SCole Faust }
5915*c217d954SCole Faust
stbi__pic_test_core(stbi__context * s)5916*c217d954SCole Faust static int stbi__pic_test_core(stbi__context *s)
5917*c217d954SCole Faust {
5918*c217d954SCole Faust int i;
5919*c217d954SCole Faust
5920*c217d954SCole Faust if (!stbi__pic_is4(s,"\x53\x80\xF6\x34"))
5921*c217d954SCole Faust return 0;
5922*c217d954SCole Faust
5923*c217d954SCole Faust for(i=0;i<84;++i)
5924*c217d954SCole Faust stbi__get8(s);
5925*c217d954SCole Faust
5926*c217d954SCole Faust if (!stbi__pic_is4(s,"PICT"))
5927*c217d954SCole Faust return 0;
5928*c217d954SCole Faust
5929*c217d954SCole Faust return 1;
5930*c217d954SCole Faust }
5931*c217d954SCole Faust
5932*c217d954SCole Faust typedef struct
5933*c217d954SCole Faust {
5934*c217d954SCole Faust stbi_uc size,type,channel;
5935*c217d954SCole Faust } stbi__pic_packet;
5936*c217d954SCole Faust
stbi__readval(stbi__context * s,int channel,stbi_uc * dest)5937*c217d954SCole Faust static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest)
5938*c217d954SCole Faust {
5939*c217d954SCole Faust int mask=0x80, i;
5940*c217d954SCole Faust
5941*c217d954SCole Faust for (i=0; i<4; ++i, mask>>=1) {
5942*c217d954SCole Faust if (channel & mask) {
5943*c217d954SCole Faust if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short");
5944*c217d954SCole Faust dest[i]=stbi__get8(s);
5945*c217d954SCole Faust }
5946*c217d954SCole Faust }
5947*c217d954SCole Faust
5948*c217d954SCole Faust return dest;
5949*c217d954SCole Faust }
5950*c217d954SCole Faust
stbi__copyval(int channel,stbi_uc * dest,const stbi_uc * src)5951*c217d954SCole Faust static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src)
5952*c217d954SCole Faust {
5953*c217d954SCole Faust int mask=0x80,i;
5954*c217d954SCole Faust
5955*c217d954SCole Faust for (i=0;i<4; ++i, mask>>=1)
5956*c217d954SCole Faust if (channel&mask)
5957*c217d954SCole Faust dest[i]=src[i];
5958*c217d954SCole Faust }
5959*c217d954SCole Faust
stbi__pic_load_core(stbi__context * s,int width,int height,int * comp,stbi_uc * result)5960*c217d954SCole Faust static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
5961*c217d954SCole Faust {
5962*c217d954SCole Faust int act_comp=0,num_packets=0,y,chained;
5963*c217d954SCole Faust stbi__pic_packet packets[10];
5964*c217d954SCole Faust
5965*c217d954SCole Faust // this will (should...) cater for even some bizarre stuff like having data
5966*c217d954SCole Faust // for the same channel in multiple packets.
5967*c217d954SCole Faust do {
5968*c217d954SCole Faust stbi__pic_packet *packet;
5969*c217d954SCole Faust
5970*c217d954SCole Faust if (num_packets==sizeof(packets)/sizeof(packets[0]))
5971*c217d954SCole Faust return stbi__errpuc("bad format","too many packets");
5972*c217d954SCole Faust
5973*c217d954SCole Faust packet = &packets[num_packets++];
5974*c217d954SCole Faust
5975*c217d954SCole Faust chained = stbi__get8(s);
5976*c217d954SCole Faust packet->size = stbi__get8(s);
5977*c217d954SCole Faust packet->type = stbi__get8(s);
5978*c217d954SCole Faust packet->channel = stbi__get8(s);
5979*c217d954SCole Faust
5980*c217d954SCole Faust act_comp |= packet->channel;
5981*c217d954SCole Faust
5982*c217d954SCole Faust if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)");
5983*c217d954SCole Faust if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp");
5984*c217d954SCole Faust } while (chained);
5985*c217d954SCole Faust
5986*c217d954SCole Faust *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
5987*c217d954SCole Faust
5988*c217d954SCole Faust for(y=0; y<height; ++y) {
5989*c217d954SCole Faust int packet_idx;
5990*c217d954SCole Faust
5991*c217d954SCole Faust for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
5992*c217d954SCole Faust stbi__pic_packet *packet = &packets[packet_idx];
5993*c217d954SCole Faust stbi_uc *dest = result+y*width*4;
5994*c217d954SCole Faust
5995*c217d954SCole Faust switch (packet->type) {
5996*c217d954SCole Faust default:
5997*c217d954SCole Faust return stbi__errpuc("bad format","packet has bad compression type");
5998*c217d954SCole Faust
5999*c217d954SCole Faust case 0: {//uncompressed
6000*c217d954SCole Faust int x;
6001*c217d954SCole Faust
6002*c217d954SCole Faust for(x=0;x<width;++x, dest+=4)
6003*c217d954SCole Faust if (!stbi__readval(s,packet->channel,dest))
6004*c217d954SCole Faust return 0;
6005*c217d954SCole Faust break;
6006*c217d954SCole Faust }
6007*c217d954SCole Faust
6008*c217d954SCole Faust case 1://Pure RLE
6009*c217d954SCole Faust {
6010*c217d954SCole Faust int left=width, i;
6011*c217d954SCole Faust
6012*c217d954SCole Faust while (left>0) {
6013*c217d954SCole Faust stbi_uc count,value[4];
6014*c217d954SCole Faust
6015*c217d954SCole Faust count=stbi__get8(s);
6016*c217d954SCole Faust if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)");
6017*c217d954SCole Faust
6018*c217d954SCole Faust if (count > left)
6019*c217d954SCole Faust count = (stbi_uc) left;
6020*c217d954SCole Faust
6021*c217d954SCole Faust if (!stbi__readval(s,packet->channel,value)) return 0;
6022*c217d954SCole Faust
6023*c217d954SCole Faust for(i=0; i<count; ++i,dest+=4)
6024*c217d954SCole Faust stbi__copyval(packet->channel,dest,value);
6025*c217d954SCole Faust left -= count;
6026*c217d954SCole Faust }
6027*c217d954SCole Faust }
6028*c217d954SCole Faust break;
6029*c217d954SCole Faust
6030*c217d954SCole Faust case 2: {//Mixed RLE
6031*c217d954SCole Faust int left=width;
6032*c217d954SCole Faust while (left>0) {
6033*c217d954SCole Faust int count = stbi__get8(s), i;
6034*c217d954SCole Faust if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)");
6035*c217d954SCole Faust
6036*c217d954SCole Faust if (count >= 128) { // Repeated
6037*c217d954SCole Faust stbi_uc value[4];
6038*c217d954SCole Faust
6039*c217d954SCole Faust if (count==128)
6040*c217d954SCole Faust count = stbi__get16be(s);
6041*c217d954SCole Faust else
6042*c217d954SCole Faust count -= 127;
6043*c217d954SCole Faust if (count > left)
6044*c217d954SCole Faust return stbi__errpuc("bad file","scanline overrun");
6045*c217d954SCole Faust
6046*c217d954SCole Faust if (!stbi__readval(s,packet->channel,value))
6047*c217d954SCole Faust return 0;
6048*c217d954SCole Faust
6049*c217d954SCole Faust for(i=0;i<count;++i, dest += 4)
6050*c217d954SCole Faust stbi__copyval(packet->channel,dest,value);
6051*c217d954SCole Faust } else { // Raw
6052*c217d954SCole Faust ++count;
6053*c217d954SCole Faust if (count>left) return stbi__errpuc("bad file","scanline overrun");
6054*c217d954SCole Faust
6055*c217d954SCole Faust for(i=0;i<count;++i, dest+=4)
6056*c217d954SCole Faust if (!stbi__readval(s,packet->channel,dest))
6057*c217d954SCole Faust return 0;
6058*c217d954SCole Faust }
6059*c217d954SCole Faust left-=count;
6060*c217d954SCole Faust }
6061*c217d954SCole Faust break;
6062*c217d954SCole Faust }
6063*c217d954SCole Faust }
6064*c217d954SCole Faust }
6065*c217d954SCole Faust }
6066*c217d954SCole Faust
6067*c217d954SCole Faust return result;
6068*c217d954SCole Faust }
6069*c217d954SCole Faust
stbi__pic_load(stbi__context * s,int * px,int * py,int * comp,int req_comp,stbi__result_info * ri)6070*c217d954SCole Faust static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri)
6071*c217d954SCole Faust {
6072*c217d954SCole Faust stbi_uc *result;
6073*c217d954SCole Faust int i, x,y, internal_comp;
6074*c217d954SCole Faust STBI_NOTUSED(ri);
6075*c217d954SCole Faust
6076*c217d954SCole Faust if (!comp) comp = &internal_comp;
6077*c217d954SCole Faust
6078*c217d954SCole Faust for (i=0; i<92; ++i)
6079*c217d954SCole Faust stbi__get8(s);
6080*c217d954SCole Faust
6081*c217d954SCole Faust x = stbi__get16be(s);
6082*c217d954SCole Faust y = stbi__get16be(s);
6083*c217d954SCole Faust if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)");
6084*c217d954SCole Faust if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode");
6085*c217d954SCole Faust
6086*c217d954SCole Faust stbi__get32be(s); //skip `ratio'
6087*c217d954SCole Faust stbi__get16be(s); //skip `fields'
6088*c217d954SCole Faust stbi__get16be(s); //skip `pad'
6089*c217d954SCole Faust
6090*c217d954SCole Faust // intermediate buffer is RGBA
6091*c217d954SCole Faust result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0);
6092*c217d954SCole Faust memset(result, 0xff, x*y*4);
6093*c217d954SCole Faust
6094*c217d954SCole Faust if (!stbi__pic_load_core(s,x,y,comp, result)) {
6095*c217d954SCole Faust STBI_FREE(result);
6096*c217d954SCole Faust result=0;
6097*c217d954SCole Faust }
6098*c217d954SCole Faust *px = x;
6099*c217d954SCole Faust *py = y;
6100*c217d954SCole Faust if (req_comp == 0) req_comp = *comp;
6101*c217d954SCole Faust result=stbi__convert_format(result,4,req_comp,x,y);
6102*c217d954SCole Faust
6103*c217d954SCole Faust return result;
6104*c217d954SCole Faust }
6105*c217d954SCole Faust
stbi__pic_test(stbi__context * s)6106*c217d954SCole Faust static int stbi__pic_test(stbi__context *s)
6107*c217d954SCole Faust {
6108*c217d954SCole Faust int r = stbi__pic_test_core(s);
6109*c217d954SCole Faust stbi__rewind(s);
6110*c217d954SCole Faust return r;
6111*c217d954SCole Faust }
6112*c217d954SCole Faust #endif
6113*c217d954SCole Faust
6114*c217d954SCole Faust // *************************************************************************************************
6115*c217d954SCole Faust // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
6116*c217d954SCole Faust
6117*c217d954SCole Faust #ifndef STBI_NO_GIF
6118*c217d954SCole Faust typedef struct
6119*c217d954SCole Faust {
6120*c217d954SCole Faust stbi__int16 prefix;
6121*c217d954SCole Faust stbi_uc first;
6122*c217d954SCole Faust stbi_uc suffix;
6123*c217d954SCole Faust } stbi__gif_lzw;
6124*c217d954SCole Faust
6125*c217d954SCole Faust typedef struct
6126*c217d954SCole Faust {
6127*c217d954SCole Faust int w,h;
6128*c217d954SCole Faust stbi_uc *out; // output buffer (always 4 components)
6129*c217d954SCole Faust stbi_uc *background; // The current "background" as far as a gif is concerned
6130*c217d954SCole Faust stbi_uc *history;
6131*c217d954SCole Faust int flags, bgindex, ratio, transparent, eflags;
6132*c217d954SCole Faust stbi_uc pal[256][4];
6133*c217d954SCole Faust stbi_uc lpal[256][4];
6134*c217d954SCole Faust stbi__gif_lzw codes[8192];
6135*c217d954SCole Faust stbi_uc *color_table;
6136*c217d954SCole Faust int parse, step;
6137*c217d954SCole Faust int lflags;
6138*c217d954SCole Faust int start_x, start_y;
6139*c217d954SCole Faust int max_x, max_y;
6140*c217d954SCole Faust int cur_x, cur_y;
6141*c217d954SCole Faust int line_size;
6142*c217d954SCole Faust int delay;
6143*c217d954SCole Faust } stbi__gif;
6144*c217d954SCole Faust
stbi__gif_test_raw(stbi__context * s)6145*c217d954SCole Faust static int stbi__gif_test_raw(stbi__context *s)
6146*c217d954SCole Faust {
6147*c217d954SCole Faust int sz;
6148*c217d954SCole Faust if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0;
6149*c217d954SCole Faust sz = stbi__get8(s);
6150*c217d954SCole Faust if (sz != '9' && sz != '7') return 0;
6151*c217d954SCole Faust if (stbi__get8(s) != 'a') return 0;
6152*c217d954SCole Faust return 1;
6153*c217d954SCole Faust }
6154*c217d954SCole Faust
stbi__gif_test(stbi__context * s)6155*c217d954SCole Faust static int stbi__gif_test(stbi__context *s)
6156*c217d954SCole Faust {
6157*c217d954SCole Faust int r = stbi__gif_test_raw(s);
6158*c217d954SCole Faust stbi__rewind(s);
6159*c217d954SCole Faust return r;
6160*c217d954SCole Faust }
6161*c217d954SCole Faust
stbi__gif_parse_colortable(stbi__context * s,stbi_uc pal[256][4],int num_entries,int transp)6162*c217d954SCole Faust static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp)
6163*c217d954SCole Faust {
6164*c217d954SCole Faust int i;
6165*c217d954SCole Faust for (i=0; i < num_entries; ++i) {
6166*c217d954SCole Faust pal[i][2] = stbi__get8(s);
6167*c217d954SCole Faust pal[i][1] = stbi__get8(s);
6168*c217d954SCole Faust pal[i][0] = stbi__get8(s);
6169*c217d954SCole Faust pal[i][3] = transp == i ? 0 : 255;
6170*c217d954SCole Faust }
6171*c217d954SCole Faust }
6172*c217d954SCole Faust
stbi__gif_header(stbi__context * s,stbi__gif * g,int * comp,int is_info)6173*c217d954SCole Faust static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info)
6174*c217d954SCole Faust {
6175*c217d954SCole Faust stbi_uc version;
6176*c217d954SCole Faust if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8')
6177*c217d954SCole Faust return stbi__err("not GIF", "Corrupt GIF");
6178*c217d954SCole Faust
6179*c217d954SCole Faust version = stbi__get8(s);
6180*c217d954SCole Faust if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF");
6181*c217d954SCole Faust if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF");
6182*c217d954SCole Faust
6183*c217d954SCole Faust stbi__g_failure_reason = "";
6184*c217d954SCole Faust g->w = stbi__get16le(s);
6185*c217d954SCole Faust g->h = stbi__get16le(s);
6186*c217d954SCole Faust g->flags = stbi__get8(s);
6187*c217d954SCole Faust g->bgindex = stbi__get8(s);
6188*c217d954SCole Faust g->ratio = stbi__get8(s);
6189*c217d954SCole Faust g->transparent = -1;
6190*c217d954SCole Faust
6191*c217d954SCole Faust if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
6192*c217d954SCole Faust
6193*c217d954SCole Faust if (is_info) return 1;
6194*c217d954SCole Faust
6195*c217d954SCole Faust if (g->flags & 0x80)
6196*c217d954SCole Faust stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
6197*c217d954SCole Faust
6198*c217d954SCole Faust return 1;
6199*c217d954SCole Faust }
6200*c217d954SCole Faust
stbi__gif_info_raw(stbi__context * s,int * x,int * y,int * comp)6201*c217d954SCole Faust static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
6202*c217d954SCole Faust {
6203*c217d954SCole Faust stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif));
6204*c217d954SCole Faust if (!stbi__gif_header(s, g, comp, 1)) {
6205*c217d954SCole Faust STBI_FREE(g);
6206*c217d954SCole Faust stbi__rewind( s );
6207*c217d954SCole Faust return 0;
6208*c217d954SCole Faust }
6209*c217d954SCole Faust if (x) *x = g->w;
6210*c217d954SCole Faust if (y) *y = g->h;
6211*c217d954SCole Faust STBI_FREE(g);
6212*c217d954SCole Faust return 1;
6213*c217d954SCole Faust }
6214*c217d954SCole Faust
stbi__out_gif_code(stbi__gif * g,stbi__uint16 code)6215*c217d954SCole Faust static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
6216*c217d954SCole Faust {
6217*c217d954SCole Faust stbi_uc *p, *c;
6218*c217d954SCole Faust int idx;
6219*c217d954SCole Faust
6220*c217d954SCole Faust // recurse to decode the prefixes, since the linked-list is backwards,
6221*c217d954SCole Faust // and working backwards through an interleaved image would be nasty
6222*c217d954SCole Faust if (g->codes[code].prefix >= 0)
6223*c217d954SCole Faust stbi__out_gif_code(g, g->codes[code].prefix);
6224*c217d954SCole Faust
6225*c217d954SCole Faust if (g->cur_y >= g->max_y) return;
6226*c217d954SCole Faust
6227*c217d954SCole Faust idx = g->cur_x + g->cur_y;
6228*c217d954SCole Faust p = &g->out[idx];
6229*c217d954SCole Faust g->history[idx / 4] = 1;
6230*c217d954SCole Faust
6231*c217d954SCole Faust c = &g->color_table[g->codes[code].suffix * 4];
6232*c217d954SCole Faust if (c[3] > 128) { // don't render transparent pixels;
6233*c217d954SCole Faust p[0] = c[2];
6234*c217d954SCole Faust p[1] = c[1];
6235*c217d954SCole Faust p[2] = c[0];
6236*c217d954SCole Faust p[3] = c[3];
6237*c217d954SCole Faust }
6238*c217d954SCole Faust g->cur_x += 4;
6239*c217d954SCole Faust
6240*c217d954SCole Faust if (g->cur_x >= g->max_x) {
6241*c217d954SCole Faust g->cur_x = g->start_x;
6242*c217d954SCole Faust g->cur_y += g->step;
6243*c217d954SCole Faust
6244*c217d954SCole Faust while (g->cur_y >= g->max_y && g->parse > 0) {
6245*c217d954SCole Faust g->step = (1 << g->parse) * g->line_size;
6246*c217d954SCole Faust g->cur_y = g->start_y + (g->step >> 1);
6247*c217d954SCole Faust --g->parse;
6248*c217d954SCole Faust }
6249*c217d954SCole Faust }
6250*c217d954SCole Faust }
6251*c217d954SCole Faust
stbi__process_gif_raster(stbi__context * s,stbi__gif * g)6252*c217d954SCole Faust static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
6253*c217d954SCole Faust {
6254*c217d954SCole Faust stbi_uc lzw_cs;
6255*c217d954SCole Faust stbi__int32 len, init_code;
6256*c217d954SCole Faust stbi__uint32 first;
6257*c217d954SCole Faust stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
6258*c217d954SCole Faust stbi__gif_lzw *p;
6259*c217d954SCole Faust
6260*c217d954SCole Faust lzw_cs = stbi__get8(s);
6261*c217d954SCole Faust if (lzw_cs > 12) return NULL;
6262*c217d954SCole Faust clear = 1 << lzw_cs;
6263*c217d954SCole Faust first = 1;
6264*c217d954SCole Faust codesize = lzw_cs + 1;
6265*c217d954SCole Faust codemask = (1 << codesize) - 1;
6266*c217d954SCole Faust bits = 0;
6267*c217d954SCole Faust valid_bits = 0;
6268*c217d954SCole Faust for (init_code = 0; init_code < clear; init_code++) {
6269*c217d954SCole Faust g->codes[init_code].prefix = -1;
6270*c217d954SCole Faust g->codes[init_code].first = (stbi_uc) init_code;
6271*c217d954SCole Faust g->codes[init_code].suffix = (stbi_uc) init_code;
6272*c217d954SCole Faust }
6273*c217d954SCole Faust
6274*c217d954SCole Faust // support no starting clear code
6275*c217d954SCole Faust avail = clear+2;
6276*c217d954SCole Faust oldcode = -1;
6277*c217d954SCole Faust
6278*c217d954SCole Faust len = 0;
6279*c217d954SCole Faust for(;;) {
6280*c217d954SCole Faust if (valid_bits < codesize) {
6281*c217d954SCole Faust if (len == 0) {
6282*c217d954SCole Faust len = stbi__get8(s); // start new block
6283*c217d954SCole Faust if (len == 0)
6284*c217d954SCole Faust return g->out;
6285*c217d954SCole Faust }
6286*c217d954SCole Faust --len;
6287*c217d954SCole Faust bits |= (stbi__int32) stbi__get8(s) << valid_bits;
6288*c217d954SCole Faust valid_bits += 8;
6289*c217d954SCole Faust } else {
6290*c217d954SCole Faust stbi__int32 code = bits & codemask;
6291*c217d954SCole Faust bits >>= codesize;
6292*c217d954SCole Faust valid_bits -= codesize;
6293*c217d954SCole Faust // @OPTIMIZE: is there some way we can accelerate the non-clear path?
6294*c217d954SCole Faust if (code == clear) { // clear code
6295*c217d954SCole Faust codesize = lzw_cs + 1;
6296*c217d954SCole Faust codemask = (1 << codesize) - 1;
6297*c217d954SCole Faust avail = clear + 2;
6298*c217d954SCole Faust oldcode = -1;
6299*c217d954SCole Faust first = 0;
6300*c217d954SCole Faust } else if (code == clear + 1) { // end of stream code
6301*c217d954SCole Faust stbi__skip(s, len);
6302*c217d954SCole Faust while ((len = stbi__get8(s)) > 0)
6303*c217d954SCole Faust stbi__skip(s,len);
6304*c217d954SCole Faust return g->out;
6305*c217d954SCole Faust } else if (code <= avail) {
6306*c217d954SCole Faust if (first) {
6307*c217d954SCole Faust return stbi__errpuc("no clear code", "Corrupt GIF");
6308*c217d954SCole Faust }
6309*c217d954SCole Faust
6310*c217d954SCole Faust if (oldcode >= 0) {
6311*c217d954SCole Faust p = &g->codes[avail++];
6312*c217d954SCole Faust if (avail > 8192) {
6313*c217d954SCole Faust return stbi__errpuc("too many codes", "Corrupt GIF");
6314*c217d954SCole Faust }
6315*c217d954SCole Faust
6316*c217d954SCole Faust p->prefix = (stbi__int16) oldcode;
6317*c217d954SCole Faust p->first = g->codes[oldcode].first;
6318*c217d954SCole Faust p->suffix = (code == avail) ? p->first : g->codes[code].first;
6319*c217d954SCole Faust } else if (code == avail)
6320*c217d954SCole Faust return stbi__errpuc("illegal code in raster", "Corrupt GIF");
6321*c217d954SCole Faust
6322*c217d954SCole Faust stbi__out_gif_code(g, (stbi__uint16) code);
6323*c217d954SCole Faust
6324*c217d954SCole Faust if ((avail & codemask) == 0 && avail <= 0x0FFF) {
6325*c217d954SCole Faust codesize++;
6326*c217d954SCole Faust codemask = (1 << codesize) - 1;
6327*c217d954SCole Faust }
6328*c217d954SCole Faust
6329*c217d954SCole Faust oldcode = code;
6330*c217d954SCole Faust } else {
6331*c217d954SCole Faust return stbi__errpuc("illegal code in raster", "Corrupt GIF");
6332*c217d954SCole Faust }
6333*c217d954SCole Faust }
6334*c217d954SCole Faust }
6335*c217d954SCole Faust }
6336*c217d954SCole Faust
6337*c217d954SCole Faust // this function is designed to support animated gifs, although stb_image doesn't support it
6338*c217d954SCole Faust // two back is the image from two frames ago, used for a very specific disposal format
stbi__gif_load_next(stbi__context * s,stbi__gif * g,int * comp,int req_comp,stbi_uc * two_back)6339*c217d954SCole Faust static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back)
6340*c217d954SCole Faust {
6341*c217d954SCole Faust int dispose;
6342*c217d954SCole Faust int first_frame;
6343*c217d954SCole Faust int pi;
6344*c217d954SCole Faust int pcount;
6345*c217d954SCole Faust
6346*c217d954SCole Faust // on first frame, any non-written pixels get the background colour (non-transparent)
6347*c217d954SCole Faust first_frame = 0;
6348*c217d954SCole Faust if (g->out == 0) {
6349*c217d954SCole Faust if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
6350*c217d954SCole Faust g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
6351*c217d954SCole Faust g->background = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
6352*c217d954SCole Faust g->history = (stbi_uc *) stbi__malloc(g->w * g->h);
6353*c217d954SCole Faust if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");
6354*c217d954SCole Faust
6355*c217d954SCole Faust // image is treated as "tranparent" at the start - ie, nothing overwrites the current background;
6356*c217d954SCole Faust // background colour is only used for pixels that are not rendered first frame, after that "background"
6357*c217d954SCole Faust // color refers to teh color that was there the previous frame.
6358*c217d954SCole Faust memset( g->out, 0x00, 4 * g->w * g->h );
6359*c217d954SCole Faust memset( g->background, 0x00, 4 * g->w * g->h ); // state of the background (starts transparent)
6360*c217d954SCole Faust memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame
6361*c217d954SCole Faust first_frame = 1;
6362*c217d954SCole Faust } else {
6363*c217d954SCole Faust // second frame - how do we dispoase of the previous one?
6364*c217d954SCole Faust dispose = (g->eflags & 0x1C) >> 2;
6365*c217d954SCole Faust pcount = g->w * g->h;
6366*c217d954SCole Faust
6367*c217d954SCole Faust if ((dispose == 3) && (two_back == 0)) {
6368*c217d954SCole Faust dispose = 2; // if I don't have an image to revert back to, default to the old background
6369*c217d954SCole Faust }
6370*c217d954SCole Faust
6371*c217d954SCole Faust if (dispose == 3) { // use previous graphic
6372*c217d954SCole Faust for (pi = 0; pi < pcount; ++pi) {
6373*c217d954SCole Faust if (g->history[pi]) {
6374*c217d954SCole Faust memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 );
6375*c217d954SCole Faust }
6376*c217d954SCole Faust }
6377*c217d954SCole Faust } else if (dispose == 2) {
6378*c217d954SCole Faust // restore what was changed last frame to background before that frame;
6379*c217d954SCole Faust for (pi = 0; pi < pcount; ++pi) {
6380*c217d954SCole Faust if (g->history[pi]) {
6381*c217d954SCole Faust memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 );
6382*c217d954SCole Faust }
6383*c217d954SCole Faust }
6384*c217d954SCole Faust } else {
6385*c217d954SCole Faust // This is a non-disposal case eithe way, so just
6386*c217d954SCole Faust // leave the pixels as is, and they will become the new background
6387*c217d954SCole Faust // 1: do not dispose
6388*c217d954SCole Faust // 0: not specified.
6389*c217d954SCole Faust }
6390*c217d954SCole Faust
6391*c217d954SCole Faust // background is what out is after the undoing of the previou frame;
6392*c217d954SCole Faust memcpy( g->background, g->out, 4 * g->w * g->h );
6393*c217d954SCole Faust }
6394*c217d954SCole Faust
6395*c217d954SCole Faust // clear my history;
6396*c217d954SCole Faust memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame
6397*c217d954SCole Faust
6398*c217d954SCole Faust for (;;) {
6399*c217d954SCole Faust int tag = stbi__get8(s);
6400*c217d954SCole Faust switch (tag) {
6401*c217d954SCole Faust case 0x2C: /* Image Descriptor */
6402*c217d954SCole Faust {
6403*c217d954SCole Faust stbi__int32 x, y, w, h;
6404*c217d954SCole Faust stbi_uc *o;
6405*c217d954SCole Faust
6406*c217d954SCole Faust x = stbi__get16le(s);
6407*c217d954SCole Faust y = stbi__get16le(s);
6408*c217d954SCole Faust w = stbi__get16le(s);
6409*c217d954SCole Faust h = stbi__get16le(s);
6410*c217d954SCole Faust if (((x + w) > (g->w)) || ((y + h) > (g->h)))
6411*c217d954SCole Faust return stbi__errpuc("bad Image Descriptor", "Corrupt GIF");
6412*c217d954SCole Faust
6413*c217d954SCole Faust g->line_size = g->w * 4;
6414*c217d954SCole Faust g->start_x = x * 4;
6415*c217d954SCole Faust g->start_y = y * g->line_size;
6416*c217d954SCole Faust g->max_x = g->start_x + w * 4;
6417*c217d954SCole Faust g->max_y = g->start_y + h * g->line_size;
6418*c217d954SCole Faust g->cur_x = g->start_x;
6419*c217d954SCole Faust g->cur_y = g->start_y;
6420*c217d954SCole Faust
6421*c217d954SCole Faust g->lflags = stbi__get8(s);
6422*c217d954SCole Faust
6423*c217d954SCole Faust if (g->lflags & 0x40) {
6424*c217d954SCole Faust g->step = 8 * g->line_size; // first interlaced spacing
6425*c217d954SCole Faust g->parse = 3;
6426*c217d954SCole Faust } else {
6427*c217d954SCole Faust g->step = g->line_size;
6428*c217d954SCole Faust g->parse = 0;
6429*c217d954SCole Faust }
6430*c217d954SCole Faust
6431*c217d954SCole Faust if (g->lflags & 0x80) {
6432*c217d954SCole Faust stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
6433*c217d954SCole Faust g->color_table = (stbi_uc *) g->lpal;
6434*c217d954SCole Faust } else if (g->flags & 0x80) {
6435*c217d954SCole Faust g->color_table = (stbi_uc *) g->pal;
6436*c217d954SCole Faust } else
6437*c217d954SCole Faust return stbi__errpuc("missing color table", "Corrupt GIF");
6438*c217d954SCole Faust
6439*c217d954SCole Faust o = stbi__process_gif_raster(s, g);
6440*c217d954SCole Faust if (o == NULL) return NULL;
6441*c217d954SCole Faust
6442*c217d954SCole Faust // if this was the first frame,
6443*c217d954SCole Faust pcount = g->w * g->h;
6444*c217d954SCole Faust if (first_frame && (g->bgindex > 0)) {
6445*c217d954SCole Faust // if first frame, any pixel not drawn to gets the background color
6446*c217d954SCole Faust for (pi = 0; pi < pcount; ++pi) {
6447*c217d954SCole Faust if (g->history[pi] == 0) {
6448*c217d954SCole Faust g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be;
6449*c217d954SCole Faust memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 );
6450*c217d954SCole Faust }
6451*c217d954SCole Faust }
6452*c217d954SCole Faust }
6453*c217d954SCole Faust
6454*c217d954SCole Faust return o;
6455*c217d954SCole Faust }
6456*c217d954SCole Faust
6457*c217d954SCole Faust case 0x21: // Comment Extension.
6458*c217d954SCole Faust {
6459*c217d954SCole Faust int len;
6460*c217d954SCole Faust int ext = stbi__get8(s);
6461*c217d954SCole Faust if (ext == 0xF9) { // Graphic Control Extension.
6462*c217d954SCole Faust len = stbi__get8(s);
6463*c217d954SCole Faust if (len == 4) {
6464*c217d954SCole Faust g->eflags = stbi__get8(s);
6465*c217d954SCole Faust g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths.
6466*c217d954SCole Faust
6467*c217d954SCole Faust // unset old transparent
6468*c217d954SCole Faust if (g->transparent >= 0) {
6469*c217d954SCole Faust g->pal[g->transparent][3] = 255;
6470*c217d954SCole Faust }
6471*c217d954SCole Faust if (g->eflags & 0x01) {
6472*c217d954SCole Faust g->transparent = stbi__get8(s);
6473*c217d954SCole Faust if (g->transparent >= 0) {
6474*c217d954SCole Faust g->pal[g->transparent][3] = 0;
6475*c217d954SCole Faust }
6476*c217d954SCole Faust } else {
6477*c217d954SCole Faust // don't need transparent
6478*c217d954SCole Faust stbi__skip(s, 1);
6479*c217d954SCole Faust g->transparent = -1;
6480*c217d954SCole Faust }
6481*c217d954SCole Faust } else {
6482*c217d954SCole Faust stbi__skip(s, len);
6483*c217d954SCole Faust break;
6484*c217d954SCole Faust }
6485*c217d954SCole Faust }
6486*c217d954SCole Faust while ((len = stbi__get8(s)) != 0) {
6487*c217d954SCole Faust stbi__skip(s, len);
6488*c217d954SCole Faust }
6489*c217d954SCole Faust break;
6490*c217d954SCole Faust }
6491*c217d954SCole Faust
6492*c217d954SCole Faust case 0x3B: // gif stream termination code
6493*c217d954SCole Faust return (stbi_uc *) s; // using '1' causes warning on some compilers
6494*c217d954SCole Faust
6495*c217d954SCole Faust default:
6496*c217d954SCole Faust return stbi__errpuc("unknown code", "Corrupt GIF");
6497*c217d954SCole Faust }
6498*c217d954SCole Faust }
6499*c217d954SCole Faust }
6500*c217d954SCole Faust
stbi__load_gif_main(stbi__context * s,int ** delays,int * x,int * y,int * z,int * comp,int req_comp)6501*c217d954SCole Faust static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
6502*c217d954SCole Faust {
6503*c217d954SCole Faust if (stbi__gif_test(s)) {
6504*c217d954SCole Faust int layers = 0;
6505*c217d954SCole Faust stbi_uc *u = 0;
6506*c217d954SCole Faust stbi_uc *out = 0;
6507*c217d954SCole Faust stbi_uc *two_back = 0;
6508*c217d954SCole Faust stbi__gif g;
6509*c217d954SCole Faust int stride;
6510*c217d954SCole Faust memset(&g, 0, sizeof(g));
6511*c217d954SCole Faust if (delays) {
6512*c217d954SCole Faust *delays = 0;
6513*c217d954SCole Faust }
6514*c217d954SCole Faust
6515*c217d954SCole Faust do {
6516*c217d954SCole Faust u = stbi__gif_load_next(s, &g, comp, req_comp, two_back);
6517*c217d954SCole Faust if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
6518*c217d954SCole Faust
6519*c217d954SCole Faust if (u) {
6520*c217d954SCole Faust *x = g.w;
6521*c217d954SCole Faust *y = g.h;
6522*c217d954SCole Faust ++layers;
6523*c217d954SCole Faust stride = g.w * g.h * 4;
6524*c217d954SCole Faust
6525*c217d954SCole Faust if (out) {
6526*c217d954SCole Faust out = (stbi_uc*) STBI_REALLOC( out, layers * stride );
6527*c217d954SCole Faust if (delays) {
6528*c217d954SCole Faust *delays = (int*) STBI_REALLOC( *delays, sizeof(int) * layers );
6529*c217d954SCole Faust }
6530*c217d954SCole Faust } else {
6531*c217d954SCole Faust out = (stbi_uc*)stbi__malloc( layers * stride );
6532*c217d954SCole Faust if (delays) {
6533*c217d954SCole Faust *delays = (int*) stbi__malloc( layers * sizeof(int) );
6534*c217d954SCole Faust }
6535*c217d954SCole Faust }
6536*c217d954SCole Faust memcpy( out + ((layers - 1) * stride), u, stride );
6537*c217d954SCole Faust if (layers >= 2) {
6538*c217d954SCole Faust two_back = out - 2 * stride;
6539*c217d954SCole Faust }
6540*c217d954SCole Faust
6541*c217d954SCole Faust if (delays) {
6542*c217d954SCole Faust (*delays)[layers - 1U] = g.delay;
6543*c217d954SCole Faust }
6544*c217d954SCole Faust }
6545*c217d954SCole Faust } while (u != 0);
6546*c217d954SCole Faust
6547*c217d954SCole Faust // free temp buffer;
6548*c217d954SCole Faust STBI_FREE(g.out);
6549*c217d954SCole Faust STBI_FREE(g.history);
6550*c217d954SCole Faust STBI_FREE(g.background);
6551*c217d954SCole Faust
6552*c217d954SCole Faust // do the final conversion after loading everything;
6553*c217d954SCole Faust if (req_comp && req_comp != 4)
6554*c217d954SCole Faust out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h);
6555*c217d954SCole Faust
6556*c217d954SCole Faust *z = layers;
6557*c217d954SCole Faust return out;
6558*c217d954SCole Faust } else {
6559*c217d954SCole Faust return stbi__errpuc("not GIF", "Image was not as a gif type.");
6560*c217d954SCole Faust }
6561*c217d954SCole Faust }
6562*c217d954SCole Faust
stbi__gif_load(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri)6563*c217d954SCole Faust static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
6564*c217d954SCole Faust {
6565*c217d954SCole Faust stbi_uc *u = 0;
6566*c217d954SCole Faust stbi__gif g;
6567*c217d954SCole Faust memset(&g, 0, sizeof(g));
6568*c217d954SCole Faust
6569*c217d954SCole Faust u = stbi__gif_load_next(s, &g, comp, req_comp, 0);
6570*c217d954SCole Faust if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
6571*c217d954SCole Faust if (u) {
6572*c217d954SCole Faust *x = g.w;
6573*c217d954SCole Faust *y = g.h;
6574*c217d954SCole Faust
6575*c217d954SCole Faust // moved conversion to after successful load so that the same
6576*c217d954SCole Faust // can be done for multiple frames.
6577*c217d954SCole Faust if (req_comp && req_comp != 4)
6578*c217d954SCole Faust u = stbi__convert_format(u, 4, req_comp, g.w, g.h);
6579*c217d954SCole Faust }
6580*c217d954SCole Faust
6581*c217d954SCole Faust // free buffers needed for multiple frame loading;
6582*c217d954SCole Faust STBI_FREE(g.history);
6583*c217d954SCole Faust STBI_FREE(g.background);
6584*c217d954SCole Faust
6585*c217d954SCole Faust return u;
6586*c217d954SCole Faust }
6587*c217d954SCole Faust
stbi__gif_info(stbi__context * s,int * x,int * y,int * comp)6588*c217d954SCole Faust static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp)
6589*c217d954SCole Faust {
6590*c217d954SCole Faust return stbi__gif_info_raw(s,x,y,comp);
6591*c217d954SCole Faust }
6592*c217d954SCole Faust #endif
6593*c217d954SCole Faust
6594*c217d954SCole Faust // *************************************************************************************************
6595*c217d954SCole Faust // Radiance RGBE HDR loader
6596*c217d954SCole Faust // originally by Nicolas Schulz
6597*c217d954SCole Faust #ifndef STBI_NO_HDR
stbi__hdr_test_core(stbi__context * s,const char * signature)6598*c217d954SCole Faust static int stbi__hdr_test_core(stbi__context *s, const char *signature)
6599*c217d954SCole Faust {
6600*c217d954SCole Faust int i;
6601*c217d954SCole Faust for (i=0; signature[i]; ++i)
6602*c217d954SCole Faust if (stbi__get8(s) != signature[i])
6603*c217d954SCole Faust return 0;
6604*c217d954SCole Faust stbi__rewind(s);
6605*c217d954SCole Faust return 1;
6606*c217d954SCole Faust }
6607*c217d954SCole Faust
stbi__hdr_test(stbi__context * s)6608*c217d954SCole Faust static int stbi__hdr_test(stbi__context* s)
6609*c217d954SCole Faust {
6610*c217d954SCole Faust int r = stbi__hdr_test_core(s, "#?RADIANCE\n");
6611*c217d954SCole Faust stbi__rewind(s);
6612*c217d954SCole Faust if(!r) {
6613*c217d954SCole Faust r = stbi__hdr_test_core(s, "#?RGBE\n");
6614*c217d954SCole Faust stbi__rewind(s);
6615*c217d954SCole Faust }
6616*c217d954SCole Faust return r;
6617*c217d954SCole Faust }
6618*c217d954SCole Faust
6619*c217d954SCole Faust #define STBI__HDR_BUFLEN 1024
stbi__hdr_gettoken(stbi__context * z,char * buffer)6620*c217d954SCole Faust static char *stbi__hdr_gettoken(stbi__context *z, char *buffer)
6621*c217d954SCole Faust {
6622*c217d954SCole Faust int len=0;
6623*c217d954SCole Faust char c = '\0';
6624*c217d954SCole Faust
6625*c217d954SCole Faust c = (char) stbi__get8(z);
6626*c217d954SCole Faust
6627*c217d954SCole Faust while (!stbi__at_eof(z) && c != '\n') {
6628*c217d954SCole Faust buffer[len++] = c;
6629*c217d954SCole Faust if (len == STBI__HDR_BUFLEN-1) {
6630*c217d954SCole Faust // flush to end of line
6631*c217d954SCole Faust while (!stbi__at_eof(z) && stbi__get8(z) != '\n')
6632*c217d954SCole Faust ;
6633*c217d954SCole Faust break;
6634*c217d954SCole Faust }
6635*c217d954SCole Faust c = (char) stbi__get8(z);
6636*c217d954SCole Faust }
6637*c217d954SCole Faust
6638*c217d954SCole Faust buffer[len] = 0;
6639*c217d954SCole Faust return buffer;
6640*c217d954SCole Faust }
6641*c217d954SCole Faust
stbi__hdr_convert(float * output,stbi_uc * input,int req_comp)6642*c217d954SCole Faust static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp)
6643*c217d954SCole Faust {
6644*c217d954SCole Faust if ( input[3] != 0 ) {
6645*c217d954SCole Faust float f1;
6646*c217d954SCole Faust // Exponent
6647*c217d954SCole Faust f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
6648*c217d954SCole Faust if (req_comp <= 2)
6649*c217d954SCole Faust output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
6650*c217d954SCole Faust else {
6651*c217d954SCole Faust output[0] = input[0] * f1;
6652*c217d954SCole Faust output[1] = input[1] * f1;
6653*c217d954SCole Faust output[2] = input[2] * f1;
6654*c217d954SCole Faust }
6655*c217d954SCole Faust if (req_comp == 2) output[1] = 1;
6656*c217d954SCole Faust if (req_comp == 4) output[3] = 1;
6657*c217d954SCole Faust } else {
6658*c217d954SCole Faust switch (req_comp) {
6659*c217d954SCole Faust case 4: output[3] = 1; /* fallthrough */
6660*c217d954SCole Faust case 3: output[0] = output[1] = output[2] = 0;
6661*c217d954SCole Faust break;
6662*c217d954SCole Faust case 2: output[1] = 1; /* fallthrough */
6663*c217d954SCole Faust case 1: output[0] = 0;
6664*c217d954SCole Faust break;
6665*c217d954SCole Faust }
6666*c217d954SCole Faust }
6667*c217d954SCole Faust }
6668*c217d954SCole Faust
stbi__hdr_load(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri)6669*c217d954SCole Faust static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
6670*c217d954SCole Faust {
6671*c217d954SCole Faust char buffer[STBI__HDR_BUFLEN];
6672*c217d954SCole Faust char *token;
6673*c217d954SCole Faust int valid = 0;
6674*c217d954SCole Faust int width, height;
6675*c217d954SCole Faust stbi_uc *scanline;
6676*c217d954SCole Faust float *hdr_data;
6677*c217d954SCole Faust int len;
6678*c217d954SCole Faust unsigned char count, value;
6679*c217d954SCole Faust int i, j, k, c1,c2, z;
6680*c217d954SCole Faust const char *headerToken;
6681*c217d954SCole Faust STBI_NOTUSED(ri);
6682*c217d954SCole Faust
6683*c217d954SCole Faust // Check identifier
6684*c217d954SCole Faust headerToken = stbi__hdr_gettoken(s,buffer);
6685*c217d954SCole Faust if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0)
6686*c217d954SCole Faust return stbi__errpf("not HDR", "Corrupt HDR image");
6687*c217d954SCole Faust
6688*c217d954SCole Faust // Parse header
6689*c217d954SCole Faust for(;;) {
6690*c217d954SCole Faust token = stbi__hdr_gettoken(s,buffer);
6691*c217d954SCole Faust if (token[0] == 0) break;
6692*c217d954SCole Faust if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
6693*c217d954SCole Faust }
6694*c217d954SCole Faust
6695*c217d954SCole Faust if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format");
6696*c217d954SCole Faust
6697*c217d954SCole Faust // Parse width and height
6698*c217d954SCole Faust // can't use sscanf() if we're not using stdio!
6699*c217d954SCole Faust token = stbi__hdr_gettoken(s,buffer);
6700*c217d954SCole Faust if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
6701*c217d954SCole Faust token += 3;
6702*c217d954SCole Faust height = (int) strtol(token, &token, 10);
6703*c217d954SCole Faust while (*token == ' ') ++token;
6704*c217d954SCole Faust if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
6705*c217d954SCole Faust token += 3;
6706*c217d954SCole Faust width = (int) strtol(token, NULL, 10);
6707*c217d954SCole Faust
6708*c217d954SCole Faust *x = width;
6709*c217d954SCole Faust *y = height;
6710*c217d954SCole Faust
6711*c217d954SCole Faust if (comp) *comp = 3;
6712*c217d954SCole Faust if (req_comp == 0) req_comp = 3;
6713*c217d954SCole Faust
6714*c217d954SCole Faust if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0))
6715*c217d954SCole Faust return stbi__errpf("too large", "HDR image is too large");
6716*c217d954SCole Faust
6717*c217d954SCole Faust // Read data
6718*c217d954SCole Faust hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0);
6719*c217d954SCole Faust if (!hdr_data)
6720*c217d954SCole Faust return stbi__errpf("outofmem", "Out of memory");
6721*c217d954SCole Faust
6722*c217d954SCole Faust // Load image data
6723*c217d954SCole Faust // image data is stored as some number of sca
6724*c217d954SCole Faust if ( width < 8 || width >= 32768) {
6725*c217d954SCole Faust // Read flat data
6726*c217d954SCole Faust for (j=0; j < height; ++j) {
6727*c217d954SCole Faust for (i=0; i < width; ++i) {
6728*c217d954SCole Faust stbi_uc rgbe[4];
6729*c217d954SCole Faust main_decode_loop:
6730*c217d954SCole Faust stbi__getn(s, rgbe, 4);
6731*c217d954SCole Faust stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
6732*c217d954SCole Faust }
6733*c217d954SCole Faust }
6734*c217d954SCole Faust } else {
6735*c217d954SCole Faust // Read RLE-encoded data
6736*c217d954SCole Faust scanline = NULL;
6737*c217d954SCole Faust
6738*c217d954SCole Faust for (j = 0; j < height; ++j) {
6739*c217d954SCole Faust c1 = stbi__get8(s);
6740*c217d954SCole Faust c2 = stbi__get8(s);
6741*c217d954SCole Faust len = stbi__get8(s);
6742*c217d954SCole Faust if (c1 != 2 || c2 != 2 || (len & 0x80)) {
6743*c217d954SCole Faust // not run-length encoded, so we have to actually use THIS data as a decoded
6744*c217d954SCole Faust // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
6745*c217d954SCole Faust stbi_uc rgbe[4];
6746*c217d954SCole Faust rgbe[0] = (stbi_uc) c1;
6747*c217d954SCole Faust rgbe[1] = (stbi_uc) c2;
6748*c217d954SCole Faust rgbe[2] = (stbi_uc) len;
6749*c217d954SCole Faust rgbe[3] = (stbi_uc) stbi__get8(s);
6750*c217d954SCole Faust stbi__hdr_convert(hdr_data, rgbe, req_comp);
6751*c217d954SCole Faust i = 1;
6752*c217d954SCole Faust j = 0;
6753*c217d954SCole Faust STBI_FREE(scanline);
6754*c217d954SCole Faust goto main_decode_loop; // yes, this makes no sense
6755*c217d954SCole Faust }
6756*c217d954SCole Faust len <<= 8;
6757*c217d954SCole Faust len |= stbi__get8(s);
6758*c217d954SCole Faust if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
6759*c217d954SCole Faust if (scanline == NULL) {
6760*c217d954SCole Faust scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0);
6761*c217d954SCole Faust if (!scanline) {
6762*c217d954SCole Faust STBI_FREE(hdr_data);
6763*c217d954SCole Faust return stbi__errpf("outofmem", "Out of memory");
6764*c217d954SCole Faust }
6765*c217d954SCole Faust }
6766*c217d954SCole Faust
6767*c217d954SCole Faust for (k = 0; k < 4; ++k) {
6768*c217d954SCole Faust int nleft;
6769*c217d954SCole Faust i = 0;
6770*c217d954SCole Faust while ((nleft = width - i) > 0) {
6771*c217d954SCole Faust count = stbi__get8(s);
6772*c217d954SCole Faust if (count > 128) {
6773*c217d954SCole Faust // Run
6774*c217d954SCole Faust value = stbi__get8(s);
6775*c217d954SCole Faust count -= 128;
6776*c217d954SCole Faust if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
6777*c217d954SCole Faust for (z = 0; z < count; ++z)
6778*c217d954SCole Faust scanline[i++ * 4 + k] = value;
6779*c217d954SCole Faust } else {
6780*c217d954SCole Faust // Dump
6781*c217d954SCole Faust if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
6782*c217d954SCole Faust for (z = 0; z < count; ++z)
6783*c217d954SCole Faust scanline[i++ * 4 + k] = stbi__get8(s);
6784*c217d954SCole Faust }
6785*c217d954SCole Faust }
6786*c217d954SCole Faust }
6787*c217d954SCole Faust for (i=0; i < width; ++i)
6788*c217d954SCole Faust stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
6789*c217d954SCole Faust }
6790*c217d954SCole Faust if (scanline)
6791*c217d954SCole Faust STBI_FREE(scanline);
6792*c217d954SCole Faust }
6793*c217d954SCole Faust
6794*c217d954SCole Faust return hdr_data;
6795*c217d954SCole Faust }
6796*c217d954SCole Faust
stbi__hdr_info(stbi__context * s,int * x,int * y,int * comp)6797*c217d954SCole Faust static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp)
6798*c217d954SCole Faust {
6799*c217d954SCole Faust char buffer[STBI__HDR_BUFLEN];
6800*c217d954SCole Faust char *token;
6801*c217d954SCole Faust int valid = 0;
6802*c217d954SCole Faust int dummy;
6803*c217d954SCole Faust
6804*c217d954SCole Faust if (!x) x = &dummy;
6805*c217d954SCole Faust if (!y) y = &dummy;
6806*c217d954SCole Faust if (!comp) comp = &dummy;
6807*c217d954SCole Faust
6808*c217d954SCole Faust if (stbi__hdr_test(s) == 0) {
6809*c217d954SCole Faust stbi__rewind( s );
6810*c217d954SCole Faust return 0;
6811*c217d954SCole Faust }
6812*c217d954SCole Faust
6813*c217d954SCole Faust for(;;) {
6814*c217d954SCole Faust token = stbi__hdr_gettoken(s,buffer);
6815*c217d954SCole Faust if (token[0] == 0) break;
6816*c217d954SCole Faust if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
6817*c217d954SCole Faust }
6818*c217d954SCole Faust
6819*c217d954SCole Faust if (!valid) {
6820*c217d954SCole Faust stbi__rewind( s );
6821*c217d954SCole Faust return 0;
6822*c217d954SCole Faust }
6823*c217d954SCole Faust token = stbi__hdr_gettoken(s,buffer);
6824*c217d954SCole Faust if (strncmp(token, "-Y ", 3)) {
6825*c217d954SCole Faust stbi__rewind( s );
6826*c217d954SCole Faust return 0;
6827*c217d954SCole Faust }
6828*c217d954SCole Faust token += 3;
6829*c217d954SCole Faust *y = (int) strtol(token, &token, 10);
6830*c217d954SCole Faust while (*token == ' ') ++token;
6831*c217d954SCole Faust if (strncmp(token, "+X ", 3)) {
6832*c217d954SCole Faust stbi__rewind( s );
6833*c217d954SCole Faust return 0;
6834*c217d954SCole Faust }
6835*c217d954SCole Faust token += 3;
6836*c217d954SCole Faust *x = (int) strtol(token, NULL, 10);
6837*c217d954SCole Faust *comp = 3;
6838*c217d954SCole Faust return 1;
6839*c217d954SCole Faust }
6840*c217d954SCole Faust #endif // STBI_NO_HDR
6841*c217d954SCole Faust
6842*c217d954SCole Faust #ifndef STBI_NO_BMP
stbi__bmp_info(stbi__context * s,int * x,int * y,int * comp)6843*c217d954SCole Faust static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
6844*c217d954SCole Faust {
6845*c217d954SCole Faust void *p;
6846*c217d954SCole Faust stbi__bmp_data info;
6847*c217d954SCole Faust
6848*c217d954SCole Faust info.all_a = 255;
6849*c217d954SCole Faust p = stbi__bmp_parse_header(s, &info);
6850*c217d954SCole Faust stbi__rewind( s );
6851*c217d954SCole Faust if (p == NULL)
6852*c217d954SCole Faust return 0;
6853*c217d954SCole Faust if (x) *x = s->img_x;
6854*c217d954SCole Faust if (y) *y = s->img_y;
6855*c217d954SCole Faust if (comp) *comp = info.ma ? 4 : 3;
6856*c217d954SCole Faust return 1;
6857*c217d954SCole Faust }
6858*c217d954SCole Faust #endif
6859*c217d954SCole Faust
6860*c217d954SCole Faust #ifndef STBI_NO_PSD
stbi__psd_info(stbi__context * s,int * x,int * y,int * comp)6861*c217d954SCole Faust static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
6862*c217d954SCole Faust {
6863*c217d954SCole Faust int channelCount, dummy, depth;
6864*c217d954SCole Faust if (!x) x = &dummy;
6865*c217d954SCole Faust if (!y) y = &dummy;
6866*c217d954SCole Faust if (!comp) comp = &dummy;
6867*c217d954SCole Faust if (stbi__get32be(s) != 0x38425053) {
6868*c217d954SCole Faust stbi__rewind( s );
6869*c217d954SCole Faust return 0;
6870*c217d954SCole Faust }
6871*c217d954SCole Faust if (stbi__get16be(s) != 1) {
6872*c217d954SCole Faust stbi__rewind( s );
6873*c217d954SCole Faust return 0;
6874*c217d954SCole Faust }
6875*c217d954SCole Faust stbi__skip(s, 6);
6876*c217d954SCole Faust channelCount = stbi__get16be(s);
6877*c217d954SCole Faust if (channelCount < 0 || channelCount > 16) {
6878*c217d954SCole Faust stbi__rewind( s );
6879*c217d954SCole Faust return 0;
6880*c217d954SCole Faust }
6881*c217d954SCole Faust *y = stbi__get32be(s);
6882*c217d954SCole Faust *x = stbi__get32be(s);
6883*c217d954SCole Faust depth = stbi__get16be(s);
6884*c217d954SCole Faust if (depth != 8 && depth != 16) {
6885*c217d954SCole Faust stbi__rewind( s );
6886*c217d954SCole Faust return 0;
6887*c217d954SCole Faust }
6888*c217d954SCole Faust if (stbi__get16be(s) != 3) {
6889*c217d954SCole Faust stbi__rewind( s );
6890*c217d954SCole Faust return 0;
6891*c217d954SCole Faust }
6892*c217d954SCole Faust *comp = 4;
6893*c217d954SCole Faust return 1;
6894*c217d954SCole Faust }
6895*c217d954SCole Faust
stbi__psd_is16(stbi__context * s)6896*c217d954SCole Faust static int stbi__psd_is16(stbi__context *s)
6897*c217d954SCole Faust {
6898*c217d954SCole Faust int channelCount, depth;
6899*c217d954SCole Faust if (stbi__get32be(s) != 0x38425053) {
6900*c217d954SCole Faust stbi__rewind( s );
6901*c217d954SCole Faust return 0;
6902*c217d954SCole Faust }
6903*c217d954SCole Faust if (stbi__get16be(s) != 1) {
6904*c217d954SCole Faust stbi__rewind( s );
6905*c217d954SCole Faust return 0;
6906*c217d954SCole Faust }
6907*c217d954SCole Faust stbi__skip(s, 6);
6908*c217d954SCole Faust channelCount = stbi__get16be(s);
6909*c217d954SCole Faust if (channelCount < 0 || channelCount > 16) {
6910*c217d954SCole Faust stbi__rewind( s );
6911*c217d954SCole Faust return 0;
6912*c217d954SCole Faust }
6913*c217d954SCole Faust (void) stbi__get32be(s);
6914*c217d954SCole Faust (void) stbi__get32be(s);
6915*c217d954SCole Faust depth = stbi__get16be(s);
6916*c217d954SCole Faust if (depth != 16) {
6917*c217d954SCole Faust stbi__rewind( s );
6918*c217d954SCole Faust return 0;
6919*c217d954SCole Faust }
6920*c217d954SCole Faust return 1;
6921*c217d954SCole Faust }
6922*c217d954SCole Faust #endif
6923*c217d954SCole Faust
6924*c217d954SCole Faust #ifndef STBI_NO_PIC
stbi__pic_info(stbi__context * s,int * x,int * y,int * comp)6925*c217d954SCole Faust static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
6926*c217d954SCole Faust {
6927*c217d954SCole Faust int act_comp=0,num_packets=0,chained,dummy;
6928*c217d954SCole Faust stbi__pic_packet packets[10];
6929*c217d954SCole Faust
6930*c217d954SCole Faust if (!x) x = &dummy;
6931*c217d954SCole Faust if (!y) y = &dummy;
6932*c217d954SCole Faust if (!comp) comp = &dummy;
6933*c217d954SCole Faust
6934*c217d954SCole Faust if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) {
6935*c217d954SCole Faust stbi__rewind(s);
6936*c217d954SCole Faust return 0;
6937*c217d954SCole Faust }
6938*c217d954SCole Faust
6939*c217d954SCole Faust stbi__skip(s, 88);
6940*c217d954SCole Faust
6941*c217d954SCole Faust *x = stbi__get16be(s);
6942*c217d954SCole Faust *y = stbi__get16be(s);
6943*c217d954SCole Faust if (stbi__at_eof(s)) {
6944*c217d954SCole Faust stbi__rewind( s);
6945*c217d954SCole Faust return 0;
6946*c217d954SCole Faust }
6947*c217d954SCole Faust if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
6948*c217d954SCole Faust stbi__rewind( s );
6949*c217d954SCole Faust return 0;
6950*c217d954SCole Faust }
6951*c217d954SCole Faust
6952*c217d954SCole Faust stbi__skip(s, 8);
6953*c217d954SCole Faust
6954*c217d954SCole Faust do {
6955*c217d954SCole Faust stbi__pic_packet *packet;
6956*c217d954SCole Faust
6957*c217d954SCole Faust if (num_packets==sizeof(packets)/sizeof(packets[0]))
6958*c217d954SCole Faust return 0;
6959*c217d954SCole Faust
6960*c217d954SCole Faust packet = &packets[num_packets++];
6961*c217d954SCole Faust chained = stbi__get8(s);
6962*c217d954SCole Faust packet->size = stbi__get8(s);
6963*c217d954SCole Faust packet->type = stbi__get8(s);
6964*c217d954SCole Faust packet->channel = stbi__get8(s);
6965*c217d954SCole Faust act_comp |= packet->channel;
6966*c217d954SCole Faust
6967*c217d954SCole Faust if (stbi__at_eof(s)) {
6968*c217d954SCole Faust stbi__rewind( s );
6969*c217d954SCole Faust return 0;
6970*c217d954SCole Faust }
6971*c217d954SCole Faust if (packet->size != 8) {
6972*c217d954SCole Faust stbi__rewind( s );
6973*c217d954SCole Faust return 0;
6974*c217d954SCole Faust }
6975*c217d954SCole Faust } while (chained);
6976*c217d954SCole Faust
6977*c217d954SCole Faust *comp = (act_comp & 0x10 ? 4 : 3);
6978*c217d954SCole Faust
6979*c217d954SCole Faust return 1;
6980*c217d954SCole Faust }
6981*c217d954SCole Faust #endif
6982*c217d954SCole Faust
6983*c217d954SCole Faust // *************************************************************************************************
6984*c217d954SCole Faust // Portable Gray Map and Portable Pixel Map loader
6985*c217d954SCole Faust // by Ken Miller
6986*c217d954SCole Faust //
6987*c217d954SCole Faust // PGM: http://netpbm.sourceforge.net/doc/pgm.html
6988*c217d954SCole Faust // PPM: http://netpbm.sourceforge.net/doc/ppm.html
6989*c217d954SCole Faust //
6990*c217d954SCole Faust // Known limitations:
6991*c217d954SCole Faust // Does not support comments in the header section
6992*c217d954SCole Faust // Does not support ASCII image data (formats P2 and P3)
6993*c217d954SCole Faust // Does not support 16-bit-per-channel
6994*c217d954SCole Faust
6995*c217d954SCole Faust #ifndef STBI_NO_PNM
6996*c217d954SCole Faust
stbi__pnm_test(stbi__context * s)6997*c217d954SCole Faust static int stbi__pnm_test(stbi__context *s)
6998*c217d954SCole Faust {
6999*c217d954SCole Faust char p, t;
7000*c217d954SCole Faust p = (char) stbi__get8(s);
7001*c217d954SCole Faust t = (char) stbi__get8(s);
7002*c217d954SCole Faust if (p != 'P' || (t != '5' && t != '6')) {
7003*c217d954SCole Faust stbi__rewind( s );
7004*c217d954SCole Faust return 0;
7005*c217d954SCole Faust }
7006*c217d954SCole Faust return 1;
7007*c217d954SCole Faust }
7008*c217d954SCole Faust
stbi__pnm_load(stbi__context * s,int * x,int * y,int * comp,int req_comp,stbi__result_info * ri)7009*c217d954SCole Faust static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
7010*c217d954SCole Faust {
7011*c217d954SCole Faust stbi_uc *out;
7012*c217d954SCole Faust STBI_NOTUSED(ri);
7013*c217d954SCole Faust
7014*c217d954SCole Faust if (!stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n))
7015*c217d954SCole Faust return 0;
7016*c217d954SCole Faust
7017*c217d954SCole Faust *x = s->img_x;
7018*c217d954SCole Faust *y = s->img_y;
7019*c217d954SCole Faust if (comp) *comp = s->img_n;
7020*c217d954SCole Faust
7021*c217d954SCole Faust if (!stbi__mad3sizes_valid(s->img_n, s->img_x, s->img_y, 0))
7022*c217d954SCole Faust return stbi__errpuc("too large", "PNM too large");
7023*c217d954SCole Faust
7024*c217d954SCole Faust out = (stbi_uc *) stbi__malloc_mad3(s->img_n, s->img_x, s->img_y, 0);
7025*c217d954SCole Faust if (!out) return stbi__errpuc("outofmem", "Out of memory");
7026*c217d954SCole Faust stbi__getn(s, out, s->img_n * s->img_x * s->img_y);
7027*c217d954SCole Faust
7028*c217d954SCole Faust if (req_comp && req_comp != s->img_n) {
7029*c217d954SCole Faust out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
7030*c217d954SCole Faust if (out == NULL) return out; // stbi__convert_format frees input on failure
7031*c217d954SCole Faust }
7032*c217d954SCole Faust return out;
7033*c217d954SCole Faust }
7034*c217d954SCole Faust
stbi__pnm_isspace(char c)7035*c217d954SCole Faust static int stbi__pnm_isspace(char c)
7036*c217d954SCole Faust {
7037*c217d954SCole Faust return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
7038*c217d954SCole Faust }
7039*c217d954SCole Faust
stbi__pnm_skip_whitespace(stbi__context * s,char * c)7040*c217d954SCole Faust static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
7041*c217d954SCole Faust {
7042*c217d954SCole Faust for (;;) {
7043*c217d954SCole Faust while (!stbi__at_eof(s) && stbi__pnm_isspace(*c))
7044*c217d954SCole Faust *c = (char) stbi__get8(s);
7045*c217d954SCole Faust
7046*c217d954SCole Faust if (stbi__at_eof(s) || *c != '#')
7047*c217d954SCole Faust break;
7048*c217d954SCole Faust
7049*c217d954SCole Faust while (!stbi__at_eof(s) && *c != '\n' && *c != '\r' )
7050*c217d954SCole Faust *c = (char) stbi__get8(s);
7051*c217d954SCole Faust }
7052*c217d954SCole Faust }
7053*c217d954SCole Faust
stbi__pnm_isdigit(char c)7054*c217d954SCole Faust static int stbi__pnm_isdigit(char c)
7055*c217d954SCole Faust {
7056*c217d954SCole Faust return c >= '0' && c <= '9';
7057*c217d954SCole Faust }
7058*c217d954SCole Faust
stbi__pnm_getinteger(stbi__context * s,char * c)7059*c217d954SCole Faust static int stbi__pnm_getinteger(stbi__context *s, char *c)
7060*c217d954SCole Faust {
7061*c217d954SCole Faust int value = 0;
7062*c217d954SCole Faust
7063*c217d954SCole Faust while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) {
7064*c217d954SCole Faust value = value*10 + (*c - '0');
7065*c217d954SCole Faust *c = (char) stbi__get8(s);
7066*c217d954SCole Faust }
7067*c217d954SCole Faust
7068*c217d954SCole Faust return value;
7069*c217d954SCole Faust }
7070*c217d954SCole Faust
stbi__pnm_info(stbi__context * s,int * x,int * y,int * comp)7071*c217d954SCole Faust static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
7072*c217d954SCole Faust {
7073*c217d954SCole Faust int maxv, dummy;
7074*c217d954SCole Faust char c, p, t;
7075*c217d954SCole Faust
7076*c217d954SCole Faust if (!x) x = &dummy;
7077*c217d954SCole Faust if (!y) y = &dummy;
7078*c217d954SCole Faust if (!comp) comp = &dummy;
7079*c217d954SCole Faust
7080*c217d954SCole Faust stbi__rewind(s);
7081*c217d954SCole Faust
7082*c217d954SCole Faust // Get identifier
7083*c217d954SCole Faust p = (char) stbi__get8(s);
7084*c217d954SCole Faust t = (char) stbi__get8(s);
7085*c217d954SCole Faust if (p != 'P' || (t != '5' && t != '6')) {
7086*c217d954SCole Faust stbi__rewind(s);
7087*c217d954SCole Faust return 0;
7088*c217d954SCole Faust }
7089*c217d954SCole Faust
7090*c217d954SCole Faust *comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm
7091*c217d954SCole Faust
7092*c217d954SCole Faust c = (char) stbi__get8(s);
7093*c217d954SCole Faust stbi__pnm_skip_whitespace(s, &c);
7094*c217d954SCole Faust
7095*c217d954SCole Faust *x = stbi__pnm_getinteger(s, &c); // read width
7096*c217d954SCole Faust stbi__pnm_skip_whitespace(s, &c);
7097*c217d954SCole Faust
7098*c217d954SCole Faust *y = stbi__pnm_getinteger(s, &c); // read height
7099*c217d954SCole Faust stbi__pnm_skip_whitespace(s, &c);
7100*c217d954SCole Faust
7101*c217d954SCole Faust maxv = stbi__pnm_getinteger(s, &c); // read max value
7102*c217d954SCole Faust
7103*c217d954SCole Faust if (maxv > 255)
7104*c217d954SCole Faust return stbi__err("max value > 255", "PPM image not 8-bit");
7105*c217d954SCole Faust else
7106*c217d954SCole Faust return 1;
7107*c217d954SCole Faust }
7108*c217d954SCole Faust #endif
7109*c217d954SCole Faust
stbi__info_main(stbi__context * s,int * x,int * y,int * comp)7110*c217d954SCole Faust static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)
7111*c217d954SCole Faust {
7112*c217d954SCole Faust #ifndef STBI_NO_JPEG
7113*c217d954SCole Faust if (stbi__jpeg_info(s, x, y, comp)) return 1;
7114*c217d954SCole Faust #endif
7115*c217d954SCole Faust
7116*c217d954SCole Faust #ifndef STBI_NO_PNG
7117*c217d954SCole Faust if (stbi__png_info(s, x, y, comp)) return 1;
7118*c217d954SCole Faust #endif
7119*c217d954SCole Faust
7120*c217d954SCole Faust #ifndef STBI_NO_GIF
7121*c217d954SCole Faust if (stbi__gif_info(s, x, y, comp)) return 1;
7122*c217d954SCole Faust #endif
7123*c217d954SCole Faust
7124*c217d954SCole Faust #ifndef STBI_NO_BMP
7125*c217d954SCole Faust if (stbi__bmp_info(s, x, y, comp)) return 1;
7126*c217d954SCole Faust #endif
7127*c217d954SCole Faust
7128*c217d954SCole Faust #ifndef STBI_NO_PSD
7129*c217d954SCole Faust if (stbi__psd_info(s, x, y, comp)) return 1;
7130*c217d954SCole Faust #endif
7131*c217d954SCole Faust
7132*c217d954SCole Faust #ifndef STBI_NO_PIC
7133*c217d954SCole Faust if (stbi__pic_info(s, x, y, comp)) return 1;
7134*c217d954SCole Faust #endif
7135*c217d954SCole Faust
7136*c217d954SCole Faust #ifndef STBI_NO_PNM
7137*c217d954SCole Faust if (stbi__pnm_info(s, x, y, comp)) return 1;
7138*c217d954SCole Faust #endif
7139*c217d954SCole Faust
7140*c217d954SCole Faust #ifndef STBI_NO_HDR
7141*c217d954SCole Faust if (stbi__hdr_info(s, x, y, comp)) return 1;
7142*c217d954SCole Faust #endif
7143*c217d954SCole Faust
7144*c217d954SCole Faust // test tga last because it's a crappy test!
7145*c217d954SCole Faust #ifndef STBI_NO_TGA
7146*c217d954SCole Faust if (stbi__tga_info(s, x, y, comp))
7147*c217d954SCole Faust return 1;
7148*c217d954SCole Faust #endif
7149*c217d954SCole Faust return stbi__err("unknown image type", "Image not of any known type, or corrupt");
7150*c217d954SCole Faust }
7151*c217d954SCole Faust
stbi__is_16_main(stbi__context * s)7152*c217d954SCole Faust static int stbi__is_16_main(stbi__context *s)
7153*c217d954SCole Faust {
7154*c217d954SCole Faust #ifndef STBI_NO_PNG
7155*c217d954SCole Faust if (stbi__png_is16(s)) return 1;
7156*c217d954SCole Faust #endif
7157*c217d954SCole Faust
7158*c217d954SCole Faust #ifndef STBI_NO_PSD
7159*c217d954SCole Faust if (stbi__psd_is16(s)) return 1;
7160*c217d954SCole Faust #endif
7161*c217d954SCole Faust
7162*c217d954SCole Faust return 0;
7163*c217d954SCole Faust }
7164*c217d954SCole Faust
7165*c217d954SCole Faust #ifndef STBI_NO_STDIO
stbi_info(char const * filename,int * x,int * y,int * comp)7166*c217d954SCole Faust STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp)
7167*c217d954SCole Faust {
7168*c217d954SCole Faust FILE *f = stbi__fopen(filename, "rb");
7169*c217d954SCole Faust int result;
7170*c217d954SCole Faust if (!f) return stbi__err("can't fopen", "Unable to open file");
7171*c217d954SCole Faust result = stbi_info_from_file(f, x, y, comp);
7172*c217d954SCole Faust fclose(f);
7173*c217d954SCole Faust return result;
7174*c217d954SCole Faust }
7175*c217d954SCole Faust
stbi_info_from_file(FILE * f,int * x,int * y,int * comp)7176*c217d954SCole Faust STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
7177*c217d954SCole Faust {
7178*c217d954SCole Faust int r;
7179*c217d954SCole Faust stbi__context s;
7180*c217d954SCole Faust long pos = ftell(f);
7181*c217d954SCole Faust stbi__start_file(&s, f);
7182*c217d954SCole Faust r = stbi__info_main(&s,x,y,comp);
7183*c217d954SCole Faust fseek(f,pos,SEEK_SET);
7184*c217d954SCole Faust return r;
7185*c217d954SCole Faust }
7186*c217d954SCole Faust
stbi_is_16_bit(char const * filename)7187*c217d954SCole Faust STBIDEF int stbi_is_16_bit(char const *filename)
7188*c217d954SCole Faust {
7189*c217d954SCole Faust FILE *f = stbi__fopen(filename, "rb");
7190*c217d954SCole Faust int result;
7191*c217d954SCole Faust if (!f) return stbi__err("can't fopen", "Unable to open file");
7192*c217d954SCole Faust result = stbi_is_16_bit_from_file(f);
7193*c217d954SCole Faust fclose(f);
7194*c217d954SCole Faust return result;
7195*c217d954SCole Faust }
7196*c217d954SCole Faust
stbi_is_16_bit_from_file(FILE * f)7197*c217d954SCole Faust STBIDEF int stbi_is_16_bit_from_file(FILE *f)
7198*c217d954SCole Faust {
7199*c217d954SCole Faust int r;
7200*c217d954SCole Faust stbi__context s;
7201*c217d954SCole Faust long pos = ftell(f);
7202*c217d954SCole Faust stbi__start_file(&s, f);
7203*c217d954SCole Faust r = stbi__is_16_main(&s);
7204*c217d954SCole Faust fseek(f,pos,SEEK_SET);
7205*c217d954SCole Faust return r;
7206*c217d954SCole Faust }
7207*c217d954SCole Faust #endif // !STBI_NO_STDIO
7208*c217d954SCole Faust
stbi_info_from_memory(stbi_uc const * buffer,int len,int * x,int * y,int * comp)7209*c217d954SCole Faust STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
7210*c217d954SCole Faust {
7211*c217d954SCole Faust stbi__context s;
7212*c217d954SCole Faust stbi__start_mem(&s,buffer,len);
7213*c217d954SCole Faust return stbi__info_main(&s,x,y,comp);
7214*c217d954SCole Faust }
7215*c217d954SCole Faust
stbi_info_from_callbacks(stbi_io_callbacks const * c,void * user,int * x,int * y,int * comp)7216*c217d954SCole Faust STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
7217*c217d954SCole Faust {
7218*c217d954SCole Faust stbi__context s;
7219*c217d954SCole Faust stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
7220*c217d954SCole Faust return stbi__info_main(&s,x,y,comp);
7221*c217d954SCole Faust }
7222*c217d954SCole Faust
stbi_is_16_bit_from_memory(stbi_uc const * buffer,int len)7223*c217d954SCole Faust STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len)
7224*c217d954SCole Faust {
7225*c217d954SCole Faust stbi__context s;
7226*c217d954SCole Faust stbi__start_mem(&s,buffer,len);
7227*c217d954SCole Faust return stbi__is_16_main(&s);
7228*c217d954SCole Faust }
7229*c217d954SCole Faust
stbi_is_16_bit_from_callbacks(stbi_io_callbacks const * c,void * user)7230*c217d954SCole Faust STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user)
7231*c217d954SCole Faust {
7232*c217d954SCole Faust stbi__context s;
7233*c217d954SCole Faust stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
7234*c217d954SCole Faust return stbi__is_16_main(&s);
7235*c217d954SCole Faust }
7236*c217d954SCole Faust
7237*c217d954SCole Faust #endif // STB_IMAGE_IMPLEMENTATION
7238*c217d954SCole Faust
7239*c217d954SCole Faust /*
7240*c217d954SCole Faust revision history:
7241*c217d954SCole Faust 2.19 (2018-02-11) fix warning
7242*c217d954SCole Faust 2.18 (2018-01-30) fix warnings
7243*c217d954SCole Faust 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug
7244*c217d954SCole Faust 1-bit BMP
7245*c217d954SCole Faust *_is_16_bit api
7246*c217d954SCole Faust avoid warnings
7247*c217d954SCole Faust 2.16 (2017-07-23) all functions have 16-bit variants;
7248*c217d954SCole Faust STBI_NO_STDIO works again;
7249*c217d954SCole Faust compilation fixes;
7250*c217d954SCole Faust fix rounding in unpremultiply;
7251*c217d954SCole Faust optimize vertical flip;
7252*c217d954SCole Faust disable raw_len validation;
7253*c217d954SCole Faust documentation fixes
7254*c217d954SCole Faust 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode;
7255*c217d954SCole Faust warning fixes; disable run-time SSE detection on gcc;
7256*c217d954SCole Faust uniform handling of optional "return" values;
7257*c217d954SCole Faust thread-safe initialization of zlib tables
7258*c217d954SCole Faust 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
7259*c217d954SCole Faust 2.13 (2016-11-29) add 16-bit API, only supported for PNG right now
7260*c217d954SCole Faust 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
7261*c217d954SCole Faust 2.11 (2016-04-02) allocate large structures on the stack
7262*c217d954SCole Faust remove white matting for transparent PSD
7263*c217d954SCole Faust fix reported channel count for PNG & BMP
7264*c217d954SCole Faust re-enable SSE2 in non-gcc 64-bit
7265*c217d954SCole Faust support RGB-formatted JPEG
7266*c217d954SCole Faust read 16-bit PNGs (only as 8-bit)
7267*c217d954SCole Faust 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED
7268*c217d954SCole Faust 2.09 (2016-01-16) allow comments in PNM files
7269*c217d954SCole Faust 16-bit-per-pixel TGA (not bit-per-component)
7270*c217d954SCole Faust info() for TGA could break due to .hdr handling
7271*c217d954SCole Faust info() for BMP to shares code instead of sloppy parse
7272*c217d954SCole Faust can use STBI_REALLOC_SIZED if allocator doesn't support realloc
7273*c217d954SCole Faust code cleanup
7274*c217d954SCole Faust 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA
7275*c217d954SCole Faust 2.07 (2015-09-13) fix compiler warnings
7276*c217d954SCole Faust partial animated GIF support
7277*c217d954SCole Faust limited 16-bpc PSD support
7278*c217d954SCole Faust #ifdef unused functions
7279*c217d954SCole Faust bug with < 92 byte PIC,PNM,HDR,TGA
7280*c217d954SCole Faust 2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value
7281*c217d954SCole Faust 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning
7282*c217d954SCole Faust 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit
7283*c217d954SCole Faust 2.03 (2015-04-12) extra corruption checking (mmozeiko)
7284*c217d954SCole Faust stbi_set_flip_vertically_on_load (nguillemot)
7285*c217d954SCole Faust fix NEON support; fix mingw support
7286*c217d954SCole Faust 2.02 (2015-01-19) fix incorrect assert, fix warning
7287*c217d954SCole Faust 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2
7288*c217d954SCole Faust 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG
7289*c217d954SCole Faust 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg)
7290*c217d954SCole Faust progressive JPEG (stb)
7291*c217d954SCole Faust PGM/PPM support (Ken Miller)
7292*c217d954SCole Faust STBI_MALLOC,STBI_REALLOC,STBI_FREE
7293*c217d954SCole Faust GIF bugfix -- seemingly never worked
7294*c217d954SCole Faust STBI_NO_*, STBI_ONLY_*
7295*c217d954SCole Faust 1.48 (2014-12-14) fix incorrectly-named assert()
7296*c217d954SCole Faust 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb)
7297*c217d954SCole Faust optimize PNG (ryg)
7298*c217d954SCole Faust fix bug in interlaced PNG with user-specified channel count (stb)
7299*c217d954SCole Faust 1.46 (2014-08-26)
7300*c217d954SCole Faust fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG
7301*c217d954SCole Faust 1.45 (2014-08-16)
7302*c217d954SCole Faust fix MSVC-ARM internal compiler error by wrapping malloc
7303*c217d954SCole Faust 1.44 (2014-08-07)
7304*c217d954SCole Faust various warning fixes from Ronny Chevalier
7305*c217d954SCole Faust 1.43 (2014-07-15)
7306*c217d954SCole Faust fix MSVC-only compiler problem in code changed in 1.42
7307*c217d954SCole Faust 1.42 (2014-07-09)
7308*c217d954SCole Faust don't define _CRT_SECURE_NO_WARNINGS (affects user code)
7309*c217d954SCole Faust fixes to stbi__cleanup_jpeg path
7310*c217d954SCole Faust added STBI_ASSERT to avoid requiring assert.h
7311*c217d954SCole Faust 1.41 (2014-06-25)
7312*c217d954SCole Faust fix search&replace from 1.36 that messed up comments/error messages
7313*c217d954SCole Faust 1.40 (2014-06-22)
7314*c217d954SCole Faust fix gcc struct-initialization warning
7315*c217d954SCole Faust 1.39 (2014-06-15)
7316*c217d954SCole Faust fix to TGA optimization when req_comp != number of components in TGA;
7317*c217d954SCole Faust fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite)
7318*c217d954SCole Faust add support for BMP version 5 (more ignored fields)
7319*c217d954SCole Faust 1.38 (2014-06-06)
7320*c217d954SCole Faust suppress MSVC warnings on integer casts truncating values
7321*c217d954SCole Faust fix accidental rename of 'skip' field of I/O
7322*c217d954SCole Faust 1.37 (2014-06-04)
7323*c217d954SCole Faust remove duplicate typedef
7324*c217d954SCole Faust 1.36 (2014-06-03)
7325*c217d954SCole Faust convert to header file single-file library
7326*c217d954SCole Faust if de-iphone isn't set, load iphone images color-swapped instead of returning NULL
7327*c217d954SCole Faust 1.35 (2014-05-27)
7328*c217d954SCole Faust various warnings
7329*c217d954SCole Faust fix broken STBI_SIMD path
7330*c217d954SCole Faust fix bug where stbi_load_from_file no longer left file pointer in correct place
7331*c217d954SCole Faust fix broken non-easy path for 32-bit BMP (possibly never used)
7332*c217d954SCole Faust TGA optimization by Arseny Kapoulkine
7333*c217d954SCole Faust 1.34 (unknown)
7334*c217d954SCole Faust use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case
7335*c217d954SCole Faust 1.33 (2011-07-14)
7336*c217d954SCole Faust make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
7337*c217d954SCole Faust 1.32 (2011-07-13)
7338*c217d954SCole Faust support for "info" function for all supported filetypes (SpartanJ)
7339*c217d954SCole Faust 1.31 (2011-06-20)
7340*c217d954SCole Faust a few more leak fixes, bug in PNG handling (SpartanJ)
7341*c217d954SCole Faust 1.30 (2011-06-11)
7342*c217d954SCole Faust added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
7343*c217d954SCole Faust removed deprecated format-specific test/load functions
7344*c217d954SCole Faust removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
7345*c217d954SCole Faust error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
7346*c217d954SCole Faust fix inefficiency in decoding 32-bit BMP (David Woo)
7347*c217d954SCole Faust 1.29 (2010-08-16)
7348*c217d954SCole Faust various warning fixes from Aurelien Pocheville
7349*c217d954SCole Faust 1.28 (2010-08-01)
7350*c217d954SCole Faust fix bug in GIF palette transparency (SpartanJ)
7351*c217d954SCole Faust 1.27 (2010-08-01)
7352*c217d954SCole Faust cast-to-stbi_uc to fix warnings
7353*c217d954SCole Faust 1.26 (2010-07-24)
7354*c217d954SCole Faust fix bug in file buffering for PNG reported by SpartanJ
7355*c217d954SCole Faust 1.25 (2010-07-17)
7356*c217d954SCole Faust refix trans_data warning (Won Chun)
7357*c217d954SCole Faust 1.24 (2010-07-12)
7358*c217d954SCole Faust perf improvements reading from files on platforms with lock-heavy fgetc()
7359*c217d954SCole Faust minor perf improvements for jpeg
7360*c217d954SCole Faust deprecated type-specific functions so we'll get feedback if they're needed
7361*c217d954SCole Faust attempt to fix trans_data warning (Won Chun)
7362*c217d954SCole Faust 1.23 fixed bug in iPhone support
7363*c217d954SCole Faust 1.22 (2010-07-10)
7364*c217d954SCole Faust removed image *writing* support
7365*c217d954SCole Faust stbi_info support from Jetro Lauha
7366*c217d954SCole Faust GIF support from Jean-Marc Lienher
7367*c217d954SCole Faust iPhone PNG-extensions from James Brown
7368*c217d954SCole Faust warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva)
7369*c217d954SCole Faust 1.21 fix use of 'stbi_uc' in header (reported by jon blow)
7370*c217d954SCole Faust 1.20 added support for Softimage PIC, by Tom Seddon
7371*c217d954SCole Faust 1.19 bug in interlaced PNG corruption check (found by ryg)
7372*c217d954SCole Faust 1.18 (2008-08-02)
7373*c217d954SCole Faust fix a threading bug (local mutable static)
7374*c217d954SCole Faust 1.17 support interlaced PNG
7375*c217d954SCole Faust 1.16 major bugfix - stbi__convert_format converted one too many pixels
7376*c217d954SCole Faust 1.15 initialize some fields for thread safety
7377*c217d954SCole Faust 1.14 fix threadsafe conversion bug
7378*c217d954SCole Faust header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
7379*c217d954SCole Faust 1.13 threadsafe
7380*c217d954SCole Faust 1.12 const qualifiers in the API
7381*c217d954SCole Faust 1.11 Support installable IDCT, colorspace conversion routines
7382*c217d954SCole Faust 1.10 Fixes for 64-bit (don't use "unsigned long")
7383*c217d954SCole Faust optimized upsampling by Fabian "ryg" Giesen
7384*c217d954SCole Faust 1.09 Fix format-conversion for PSD code (bad global variables!)
7385*c217d954SCole Faust 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
7386*c217d954SCole Faust 1.07 attempt to fix C++ warning/errors again
7387*c217d954SCole Faust 1.06 attempt to fix C++ warning/errors again
7388*c217d954SCole Faust 1.05 fix TGA loading to return correct *comp and use good luminance calc
7389*c217d954SCole Faust 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
7390*c217d954SCole Faust 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
7391*c217d954SCole Faust 1.02 support for (subset of) HDR files, float interface for preferred access to them
7392*c217d954SCole Faust 1.01 fix bug: possible bug in handling right-side up bmps... not sure
7393*c217d954SCole Faust fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all
7394*c217d954SCole Faust 1.00 interface to zlib that skips zlib header
7395*c217d954SCole Faust 0.99 correct handling of alpha in palette
7396*c217d954SCole Faust 0.98 TGA loader by lonesock; dynamically add loaders (untested)
7397*c217d954SCole Faust 0.97 jpeg errors on too large a file; also catch another malloc failure
7398*c217d954SCole Faust 0.96 fix detection of invalid v value - particleman@mollyrocket forum
7399*c217d954SCole Faust 0.95 during header scan, seek to markers in case of padding
7400*c217d954SCole Faust 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
7401*c217d954SCole Faust 0.93 handle jpegtran output; verbose errors
7402*c217d954SCole Faust 0.92 read 4,8,16,24,32-bit BMP files of several formats
7403*c217d954SCole Faust 0.91 output 24-bit Windows 3.0 BMP files
7404*c217d954SCole Faust 0.90 fix a few more warnings; bump version number to approach 1.0
7405*c217d954SCole Faust 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
7406*c217d954SCole Faust 0.60 fix compiling as c++
7407*c217d954SCole Faust 0.59 fix warnings: merge Dave Moore's -Wall fixes
7408*c217d954SCole Faust 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
7409*c217d954SCole Faust 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
7410*c217d954SCole Faust 0.56 fix bug: zlib uncompressed mode len vs. nlen
7411*c217d954SCole Faust 0.55 fix bug: restart_interval not initialized to 0
7412*c217d954SCole Faust 0.54 allow NULL for 'int *comp'
7413*c217d954SCole Faust 0.53 fix bug in png 3->4; speedup png decoding
7414*c217d954SCole Faust 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
7415*c217d954SCole Faust 0.51 obey req_comp requests, 1-component jpegs return as 1-component,
7416*c217d954SCole Faust on 'test' only check type, not whether we support this variant
7417*c217d954SCole Faust 0.50 (2006-11-19)
7418*c217d954SCole Faust first released version
7419*c217d954SCole Faust */
7420*c217d954SCole Faust
7421*c217d954SCole Faust
7422*c217d954SCole Faust /*
7423*c217d954SCole Faust ------------------------------------------------------------------------------
7424*c217d954SCole Faust This software is available under 2 licenses -- choose whichever you prefer.
7425*c217d954SCole Faust ------------------------------------------------------------------------------
7426*c217d954SCole Faust ALTERNATIVE A - MIT License
7427*c217d954SCole Faust Copyright (c) 2017 Sean Barrett
7428*c217d954SCole Faust Permission is hereby granted, free of charge, to any person obtaining a copy of
7429*c217d954SCole Faust this software and associated documentation files (the "Software"), to deal in
7430*c217d954SCole Faust the Software without restriction, including without limitation the rights to
7431*c217d954SCole Faust use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7432*c217d954SCole Faust of the Software, and to permit persons to whom the Software is furnished to do
7433*c217d954SCole Faust so, subject to the following conditions:
7434*c217d954SCole Faust The above copyright notice and this permission notice shall be included in all
7435*c217d954SCole Faust copies or substantial portions of the Software.
7436*c217d954SCole Faust THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7437*c217d954SCole Faust IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7438*c217d954SCole Faust FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7439*c217d954SCole Faust AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7440*c217d954SCole Faust LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
7441*c217d954SCole Faust OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
7442*c217d954SCole Faust SOFTWARE.
7443*c217d954SCole Faust ------------------------------------------------------------------------------
7444*c217d954SCole Faust ALTERNATIVE B - Public Domain (www.unlicense.org)
7445*c217d954SCole Faust This is free and unencumbered software released into the public domain.
7446*c217d954SCole Faust Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
7447*c217d954SCole Faust software, either in source code form or as a compiled binary, for any purpose,
7448*c217d954SCole Faust commercial or non-commercial, and by any means.
7449*c217d954SCole Faust In jurisdictions that recognize copyright laws, the author or authors of this
7450*c217d954SCole Faust software dedicate any and all copyright interest in the software to the public
7451*c217d954SCole Faust domain. We make this dedication for the benefit of the public at large and to
7452*c217d954SCole Faust the detriment of our heirs and successors. We intend this dedication to be an
7453*c217d954SCole Faust overt act of relinquishment in perpetuity of all present and future rights to
7454*c217d954SCole Faust this software under copyright law.
7455*c217d954SCole Faust THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7456*c217d954SCole Faust IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7457*c217d954SCole Faust FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7458*c217d954SCole Faust AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
7459*c217d954SCole Faust ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
7460*c217d954SCole Faust WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7461*c217d954SCole Faust ------------------------------------------------------------------------------
7462*c217d954SCole Faust */
7463