StbImage.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5/* stbi-1.33 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
6 when you control the images you're loading
7 no warranty implied; use at your own risk
8
9 QUICK NOTES:
10 Primarily of interest to game developers and other people who can
11 avoid problematic images and only need the trivial interface
12
13 JPEG baseline (no JPEG progressive)
14 PNG 8-bit only
15
16 TGA (not sure what subset, if a subset)
17 BMP non-1bpp, non-RLE
18 PSD (composited view only, no extra channels)
19
20 GIF (*comp always reports as 4-channel)
21 HDR (radiance rgbE format)
22 PIC (Softimage PIC)
23
24 - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
25 - decode from arbitrary I/O callbacks
26 - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define
27 STBI_SIMD)
28
29 Latest revisions:
30 1.33 (2011-07-14) minor fixes suggested by Dave Moore
31 1.32 (2011-07-13) info support for all filetypes (SpartanJ)
32 1.31 (2011-06-19) a few more leak fixes, bug in PNG handling (SpartanJ)
33 1.30 (2011-06-11) added ability to load files via io callbacks (Ben
34 Wenger) 1.29 (2010-08-16) various warning fixes from Aurelien Pocheville 1.28
35 (2010-08-01) fix bug in GIF palette transparency (SpartanJ) 1.27 (2010-08-01)
36 cast-to-uint8 to fix warnings (Laurent Gomila) allow trailing 0s at end of
37 image data (Laurent Gomila) 1.26 (2010-07-24) fix bug in file buffering for PNG
38 reported by SpartanJ
39
40 See end of file for full revision history.
41
42 TODO:
43 stbi_info support for BMP,PSD,HDR,PIC
44
45
46 ============================ Contributors =========================
47
48 Image formats Optimizations & bugfixes
49 Sean Barrett (jpeg, png, bmp) Fabian "ryg" Giesen
50 Nicolas Schulz (hdr, psd)
51 Jonathan Dummer (tga) Bug fixes & warning fixes
52 Jean-Marc Lienher (gif) Marc LeBlanc
53 Tom Seddon (pic) Christpher Lloyd
54 Thatcher Ulrich (psd) Dave Moore
55 Won Chun
56 the Horde3D community
57 Extensions, features Janez Zemva
58 Jetro Lauha (stbi_info) Jonathan Blow
59 James "moose2000" Brown (iPhone PNG) Laurent Gomila
60 Ben "Disch" Wenger (io callbacks) Aruelien Pocheville
61 Martin "SpartanJ" Golini Ryamond Barbiero
62 David Woo
63
64
65 If your name should be here but isn't, let Sean know.
66
67*/
68
69#ifndef STBI_INCLUDE_STB_IMAGE_H
70#define STBI_INCLUDE_STB_IMAGE_H
71
72// To get a header file for this, either cut and paste the header,
73// or create stb_image.h, #define STBI_HEADER_FILE_ONLY, and
74// then include stb_image.c from it.
75
76//// begin header file ////////////////////////////////////////////////////
77//
78// Limitations:
79// - no jpeg progressive support
80// - non-HDR formats support 8-bit samples only (jpeg, png)
81// - no delayed line count (jpeg) -- IJG doesn't support either
82// - no 1-bit BMP
83// - GIF always returns *comp=4
84//
85// Basic usage (see HDR discussion below):
86// int x,y,n;
87// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
88// // ... process data if not NULL ...
89// // ... x = width, y = height, n = # 8-bit components per pixel ...
90// // ... replace '0' with '1'..'4' to force that many components per pixel
91// // ... but 'n' will always be the number that it would have been if you
92// said 0 stbi_image_free(data)
93//
94// Standard parameters:
95// int *x -- outputs image width in pixels
96// int *y -- outputs image height in pixels
97// int *comp -- outputs # of image components in image file
98// int req_comp -- if non-zero, # of image components requested in result
99//
100// The return value from an image loader is an 'unsigned char *' which points
101// to the pixel data. The pixel data consists of *y scanlines of *x pixels,
102// with each pixel consisting of N interleaved 8-bit components; the first
103// pixel pointed to is top-left-most in the image. There is no padding between
104// image scanlines or between pixels, regardless of format. The number of
105// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
106// If req_comp is non-zero, *comp has the number of components that _would_
107// have been output otherwise. E.g. if you set req_comp to 4, you will always
108// get RGBA output, but you can check *comp to easily see if it's opaque.
109//
110// An output image with N components has the following components interleaved
111// in this order in each pixel:
112//
113// N=#comp components
114// 1 grey
115// 2 grey, alpha
116// 3 red, green, blue
117// 4 red, green, blue, alpha
118//
119// If image loading fails for any reason, the return value will be NULL,
120// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
121// can be queried for an extremely brief, end-user unfriendly explanation
122// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
123// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
124// more user-friendly ones.
125//
126// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
127//
128// ===========================================================================
129//
130// iPhone PNG support:
131//
132// By default we convert iphone-formatted PNGs back to RGB; nominally they
133// would silently load as BGR, except the existing code should have just
134// failed on such iPhone PNGs. But you can disable this conversion by
135// by calling stbi_convert_iphone_png_to_rgb(0), in which case
136// you will always just get the native iphone "format" through.
137//
138// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
139// pixel to remove any premultiplied alpha *only* if the image file explicitly
140// says there's premultiplied data (currently only happens in iPhone images,
141// and only if iPhone convert-to-rgb processing is on).
142//
143// ===========================================================================
144//
145// HDR image support (disable by defining STBI_NO_HDR)
146//
147// stb_image now supports loading HDR images in general, and currently
148// the Radiance .HDR file format, although the support is provided
149// generically. You can still load any file through the existing interface;
150// if you attempt to load an HDR file, it will be automatically remapped to
151// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
152// both of these constants can be reconfigured through this interface:
153//
154// stbi_hdr_to_ldr_gamma(2.2f);
155// stbi_hdr_to_ldr_scale(1.0f);
156//
157// (note, do not use _inverse_ constants; stbi_image will invert them
158// appropriately).
159//
160// Additionally, there is a new, parallel interface for loading files as
161// (linear) floats to preserve the full dynamic range:
162//
163// float *data = stbi_loadf(filename, &x, &y, &n, 0);
164//
165// If you load LDR images through this interface, those images will
166// be promoted to floating point values, run through the inverse of
167// constants corresponding to the above:
168//
169// stbi_ldr_to_hdr_scale(1.0f);
170// stbi_ldr_to_hdr_gamma(2.2f);
171//
172// Finally, given a filename (or an open file or memory block--see header
173// file for details) containing image data, you can query for the "most
174// appropriate" interface to use (that is, whether the image is HDR or
175// not), using:
176//
177// stbi_is_hdr(char *filename);
178//
179// ===========================================================================
180//
181// I/O callbacks
182//
183// I/O callbacks allow you to read from arbitrary sources, like packaged
184// files or some other source. Data read from callbacks are processed
185// through a small internal buffer (currently 128 bytes) to try to reduce
186// overhead.
187//
188// The three functions you must define are "read" (reads some bytes of data),
189// "skip" (skips some bytes of data), "eof" (reports if the stream is at the
190// end).
191
192#ifndef STBI_NO_STDIO
193
194 #if defined(_MSC_VER) && _MSC_VER >= 0x1400
195 #define _CRT_SECURE_NO_WARNINGS // suppress bogus warnings about fopen()
196 #endif
197
198 #include <stdio.h>
199#endif
200
201#define STBI_VERSION 1
202
203enum {
204 STBI_default = 0, // only used for req_comp
205
206 STBI_grey = 1,
207 STBI_grey_alpha = 2,
208 STBI_rgb = 3,
209 STBI_rgb_alpha = 4
210};
211
212typedef unsigned char stbi_uc;
213
214#ifdef __cplusplus
215extern "C" {
216#endif
217
218//////////////////////////////////////////////////////////////////////////////
219//
220// PRIMARY API - works on images of any type
221//
222
223//
224// load image by filename, open file, or memory buffer
225//
226
227extern stbi_uc* stbi_load_from_memory(stbi_uc const* buffer,
228 int len,
229 int* x,
230 int* y,
231 int* comp,
232 int req_comp);
233
234#ifndef STBI_NO_STDIO
235extern stbi_uc* stbi_load(char const* filename,
236 int* x,
237 int* y,
238 int* comp,
239 int req_comp);
240extern stbi_uc* stbi_load_from_file(FILE* f,
241 int* x,
242 int* y,
243 int* comp,
244 int req_comp);
245// for stbi_load_from_file, file pointer is left pointing immediately after
246// image
247#endif
248
249typedef struct {
250 int (*read)(void* user,
251 char* data,
252 int size); // fill 'data' with 'size' bytes. return number of
253 // bytes actually read
254 void (*skip)(void* user, unsigned n); // skip the next 'n' bytes
255 int (*eof)(void* user); // returns nonzero if we are at end of file/data
256} stbi_io_callbacks;
257
258extern stbi_uc* stbi_load_from_callbacks(stbi_io_callbacks const* clbk,
259 void* user,
260 int* x,
261 int* y,
262 int* comp,
263 int req_comp);
264
265#ifndef STBI_NO_HDR
266extern float* stbi_loadf_from_memory(stbi_uc const* buffer,
267 int len,
268 int* x,
269 int* y,
270 int* comp,
271 int req_comp);
272
273 #ifndef STBI_NO_STDIO
274extern float* stbi_loadf(char const* filename,
275 int* x,
276 int* y,
277 int* comp,
278 int req_comp);
279extern float* stbi_loadf_from_file(FILE* f,
280 int* x,
281 int* y,
282 int* comp,
283 int req_comp);
284 #endif
285
286extern float* stbi_loadf_from_callbacks(stbi_io_callbacks const* clbk,
287 void* user,
288 int* x,
289 int* y,
290 int* comp,
291 int req_comp);
292
293extern void stbi_hdr_to_ldr_gamma(float gamma);
294extern void stbi_hdr_to_ldr_scale(float scale);
295
296extern void stbi_ldr_to_hdr_gamma(float gamma);
297extern void stbi_ldr_to_hdr_scale(float scale);
298#endif // STBI_NO_HDR
299
300// stbi_is_hdr is always defined
301extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks const* clbk,
302 void* user);
303extern int stbi_is_hdr_from_memory(stbi_uc const* buffer, int len);
304#ifndef STBI_NO_STDIO
305extern int stbi_is_hdr(char const* filename);
306extern int stbi_is_hdr_from_file(FILE* f);
307#endif // STBI_NO_STDIO
308
309// get a VERY brief reason for failure
310// NOT THREADSAFE
311extern const char* stbi_failure_reason(void);
312
313// free the loaded image -- this is just free()
314extern void stbi_image_free(void* retval_from_stbi_load);
315
316// get image dimensions & components without fully decoding
317extern int stbi_info_from_memory(stbi_uc const* buffer,
318 int len,
319 int* x,
320 int* y,
321 int* comp);
322extern int stbi_info_from_callbacks(stbi_io_callbacks const* clbk,
323 void* user,
324 int* x,
325 int* y,
326 int* comp);
327
328#ifndef STBI_NO_STDIO
329extern int stbi_info(char const* filename, int* x, int* y, int* comp);
330extern int stbi_info_from_file(FILE* f, int* x, int* y, int* comp);
331
332#endif
333
334// for image formats that explicitly notate that they have premultiplied alpha,
335// we just return the colors as stored in the file. set this flag to force
336// unpremultiplication. results are undefined if the unpremultiply overflow.
337extern void stbi_set_unpremultiply_on_load(
338 int flag_true_if_should_unpremultiply);
339
340// indicate whether we should process iphone images back to canonical format,
341// or just pass them through "as-is"
342extern void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
343
344// ZLIB client - used by PNG, available for other purposes
345
346extern char* stbi_zlib_decode_malloc_guesssize(const char* buffer,
347 int len,
348 int initial_size,
349 int* outlen);
350extern char* stbi_zlib_decode_malloc(const char* buffer, int len, int* outlen);
351extern int stbi_zlib_decode_buffer(char* obuffer,
352 int olen,
353 const char* ibuffer,
354 int ilen);
355
356extern char* stbi_zlib_decode_noheader_malloc(const char* buffer,
357 int len,
358 int* outlen);
359extern int stbi_zlib_decode_noheader_buffer(char* obuffer,
360 int olen,
361 const char* ibuffer,
362 int ilen);
363
364// define faster low-level operations (typically SIMD support)
365#ifdef STBI_SIMD
366typedef void (*stbi_idct_8x8)(stbi_uc* out,
367 int out_stride,
368 short data[64],
369 unsigned short* dequantize);
370// compute an integer IDCT on "input"
371// input[x] = data[x] * dequantize[x]
372// write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
373// CLAMP results to 0..255
374typedef void (*stbi_YCbCr_to_RGB_run)(stbi_uc* output,
375 stbi_uc const* y,
376 stbi_uc const* cb,
377 stbi_uc const* cr,
378 int count,
379 int step);
380// compute a conversion from YCbCr to RGB
381// 'count' pixels
382// write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if
383// 4, write '255' as 4th), order R,G,B y: Y input channel cb: Cb input
384// channel; scale/biased to be 0..255 cr: Cr input channel; scale/biased to
385// be 0..255
386
387extern void stbi_install_idct(stbi_idct_8x8 func);
388extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
389#endif // STBI_SIMD
390
391#ifdef __cplusplus
392}
393#endif
394
395//
396//
397//// end header file /////////////////////////////////////////////////////
398#endif // STBI_INCLUDE_STB_IMAGE_H