SCV  4.2.1
Simple Components for Visual
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
stb_image.h
Go to the documentation of this file.
1 /* stbi-1.33 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
2  when you control the images you're loading
3  no warranty implied; use at your own risk
4 
5  QUICK NOTES:
6  Primarily of interest to game developers and other people who can
7  avoid problematic images and only need the trivial interface
8 
9  JPEG baseline (no JPEG progressive)
10  PNG 8-bit only
11 
12  TGA (not sure what subset, if a subset)
13  BMP non-1bpp, non-RLE
14  PSD (composited view only, no extra channels)
15 
16  GIF (*comp always reports as 4-channel)
17  HDR (radiance rgbE format)
18  PIC (Softimage PIC)
19 
20  - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
21  - decode from arbitrary I/O callbacks
22  - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
23 
24  Latest revisions:
25  1.33 (2011-07-14) minor fixes suggested by Dave Moore
26  1.32 (2011-07-13) info support for all filetypes (SpartanJ)
27  1.31 (2011-06-19) a few more leak fixes, bug in PNG handling (SpartanJ)
28  1.30 (2011-06-11) added ability to load files via io callbacks (Ben Wenger)
29  1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
30  1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
31  1.27 (2010-08-01) cast-to-uint8 to fix warnings (Laurent Gomila)
32  allow trailing 0s at end of image data (Laurent Gomila)
33  1.26 (2010-07-24) fix bug in file buffering for PNG reported by SpartanJ
34 
35  See end of file for full revision history.
36 
37  TODO:
38  stbi_info support for BMP,PSD,HDR,PIC
39 
40 
41  ============================ Contributors =========================
42 
43  Image formats Optimizations & bugfixes
44  Sean Barrett (jpeg, png, bmp) Fabian "ryg" Giesen
45  Nicolas Schulz (hdr, psd)
46  Jonathan Dummer (tga) Bug fixes & warning fixes
47  Jean-Marc Lienher (gif) Marc LeBlanc
48  Tom Seddon (pic) Christpher Lloyd
49  Thatcher Ulrich (psd) Dave Moore
50  Won Chun
51  the Horde3D community
52  Extensions, features Janez Zemva
53  Jetro Lauha (stbi_info) Jonathan Blow
54  James "moose2000" Brown (iPhone PNG) Laurent Gomila
55  Ben "Disch" Wenger (io callbacks) Aruelien Pocheville
56  Martin "SpartanJ" Golini Ryamond Barbiero
57  David Woo
58 
59 
60  If your name should be here but isn't, let Sean know.
61 
62 */
63 
64 #ifndef STBI_INCLUDE_STB_IMAGE_H
65 #define STBI_INCLUDE_STB_IMAGE_H
66 
67 // To get a header file for this, either cut and paste the header,
68 // or create stb_image.h, #define STBI_HEADER_FILE_ONLY, and
69 // then include stb_image.c from it.
70 
72 //
73 // Limitations:
74 // - no jpeg progressive support
75 // - non-HDR formats support 8-bit samples only (jpeg, png)
76 // - no delayed line count (jpeg) -- IJG doesn't support either
77 // - no 1-bit BMP
78 // - GIF always returns *comp=4
79 //
80 // Basic usage (see HDR discussion below):
81 // int x,y,n;
82 // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
83 // // ... process data if not NULL ...
84 // // ... x = width, y = height, n = # 8-bit components per pixel ...
85 // // ... replace '0' with '1'..'4' to force that many components per pixel
86 // // ... but 'n' will always be the number that it would have been if you said 0
87 // stbi_image_free(data)
88 //
89 // Standard parameters:
90 // int *x -- outputs image width in pixels
91 // int *y -- outputs image height in pixels
92 // int *comp -- outputs # of image components in image file
93 // int req_comp -- if non-zero, # of image components requested in result
94 //
95 // The return value from an image loader is an 'unsigned char *' which points
96 // to the pixel data. The pixel data consists of *y scanlines of *x pixels,
97 // with each pixel consisting of N interleaved 8-bit components; the first
98 // pixel pointed to is top-left-most in the image. There is no padding between
99 // image scanlines or between pixels, regardless of format. The number of
100 // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
101 // If req_comp is non-zero, *comp has the number of components that _would_
102 // have been output otherwise. E.g. if you set req_comp to 4, you will always
103 // get RGBA output, but you can check *comp to easily see if it's opaque.
104 //
105 // An output image with N components has the following components interleaved
106 // in this order in each pixel:
107 //
108 // N=#comp components
109 // 1 grey
110 // 2 grey, alpha
111 // 3 red, green, blue
112 // 4 red, green, blue, alpha
113 //
114 // If image loading fails for any reason, the return value will be NULL,
115 // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
116 // can be queried for an extremely brief, end-user unfriendly explanation
117 // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
118 // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
119 // more user-friendly ones.
120 //
121 // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
122 //
123 // ===========================================================================
124 //
125 // iPhone PNG support:
126 //
127 // By default we convert iphone-formatted PNGs back to RGB; nominally they
128 // would silently load as BGR, except the existing code should have just
129 // failed on such iPhone PNGs. But you can disable this conversion by
130 // by calling stbi_convert_iphone_png_to_rgb(0), in which case
131 // you will always just get the native iphone "format" through.
132 //
133 // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
134 // pixel to remove any premultiplied alpha *only* if the image file explicitly
135 // says there's premultiplied data (currently only happens in iPhone images,
136 // and only if iPhone convert-to-rgb processing is on).
137 //
138 // ===========================================================================
139 //
140 // HDR image support (disable by defining STBI_NO_HDR)
141 //
142 // stb_image now supports loading HDR images in general, and currently
143 // the Radiance .HDR file format, although the support is provided
144 // generically. You can still load any file through the existing interface;
145 // if you attempt to load an HDR file, it will be automatically remapped to
146 // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
147 // both of these constants can be reconfigured through this interface:
148 //
149 // stbi_hdr_to_ldr_gamma(2.2f);
150 // stbi_hdr_to_ldr_scale(1.0f);
151 //
152 // (note, do not use _inverse_ constants; stbi_image will invert them
153 // appropriately).
154 //
155 // Additionally, there is a new, parallel interface for loading files as
156 // (linear) floats to preserve the full dynamic range:
157 //
158 // float *data = stbi_loadf(filename, &x, &y, &n, 0);
159 //
160 // If you load LDR images through this interface, those images will
161 // be promoted to floating point values, run through the inverse of
162 // constants corresponding to the above:
163 //
164 // stbi_ldr_to_hdr_scale(1.0f);
165 // stbi_ldr_to_hdr_gamma(2.2f);
166 //
167 // Finally, given a filename (or an open file or memory block--see header
168 // file for details) containing image data, you can query for the "most
169 // appropriate" interface to use (that is, whether the image is HDR or
170 // not), using:
171 //
172 // stbi_is_hdr(char *filename);
173 //
174 // ===========================================================================
175 //
176 // I/O callbacks
177 //
178 // I/O callbacks allow you to read from arbitrary sources, like packaged
179 // files or some other source. Data read from callbacks are processed
180 // through a small internal buffer (currently 128 bytes) to try to reduce
181 // overhead.
182 //
183 // The three functions you must define are "read" (reads some bytes of data),
184 // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
185 
186 
187 #ifndef STBI_NO_STDIO
188 
189 #if defined(_MSC_VER) && _MSC_VER >= 0x1400
190 #define _CRT_SECURE_NO_WARNINGS // suppress bogus warnings about fopen()
191 #endif
192 
193 #include <stdio.h>
194 #endif
195 
196 #define STBI_VERSION 1
197 
198 enum
199 {
200  STBI_default = 0, // only used for req_comp
201 
204  STBI_rgb = 3,
206 };
207 
208 typedef unsigned char stbi_uc;
209 
210 #ifdef __cplusplus
211 extern "C" {
212 #endif
213 
215 //
216 // PRIMARY API - works on images of any type
217 //
218 
219 //
220 // load image by filename, open file, or memory buffer
221 //
222 
223 extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
224 
225 #ifndef STBI_NO_STDIO
226 extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
227 extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
228 // for stbi_load_from_file, file pointer is left pointing immediately after image
229 #endif
230 
231 typedef struct
232 {
233  int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
234  void (*skip) (void *user,unsigned n); // skip the next 'n' bytes
235  int (*eof) (void *user); // returns nonzero if we are at end of file/data
237 
238 extern stbi_uc *stbi_load_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
239 
240 #ifndef STBI_NO_HDR
241  extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
242 
243  #ifndef STBI_NO_STDIO
244  extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
245  extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
246  #endif
247 
248  extern float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
249 
250  extern void stbi_hdr_to_ldr_gamma(float gamma);
251  extern void stbi_hdr_to_ldr_scale(float scale);
252 
253  extern void stbi_ldr_to_hdr_gamma(float gamma);
254  extern void stbi_ldr_to_hdr_scale(float scale);
255 #endif // STBI_NO_HDR
256 
257 // stbi_is_hdr is always defined
258 extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
259 extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
260 #ifndef STBI_NO_STDIO
261 extern int stbi_is_hdr (char const *filename);
262 extern int stbi_is_hdr_from_file(FILE *f);
263 #endif // STBI_NO_STDIO
264 
265 
266 // get a VERY brief reason for failure
267 // NOT THREADSAFE
268 extern const char *stbi_failure_reason (void);
269 
270 // free the loaded image -- this is just free()
271 extern void stbi_image_free (void *retval_from_stbi_load);
272 
273 // get image dimensions & components without fully decoding
274 extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
275 extern int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
276 
277 #ifndef STBI_NO_STDIO
278 extern int stbi_info (char const *filename, int *x, int *y, int *comp);
279 extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
280 
281 #endif
282 
283 
284 
285 // for image formats that explicitly notate that they have premultiplied alpha,
286 // we just return the colors as stored in the file. set this flag to force
287 // unpremultiplication. results are undefined if the unpremultiply overflow.
288 extern void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
289 
290 // indicate whether we should process iphone images back to canonical format,
291 // or just pass them through "as-is"
292 extern void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
293 
294 
295 // ZLIB client - used by PNG, available for other purposes
296 
297 extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
298 extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
299 extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
300 
301 extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
302 extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
303 
304 
305 // define faster low-level operations (typically SIMD support)
306 #ifdef STBI_SIMD
307 typedef void (*stbi_idct_8x8)(stbi_uc *out, int out_stride, short data[64], unsigned short *dequantize);
308 // compute an integer IDCT on "input"
309 // input[x] = data[x] * dequantize[x]
310 // write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
311 // CLAMP results to 0..255
312 typedef void (*stbi_YCbCr_to_RGB_run)(stbi_uc *output, stbi_uc const *y, stbi_uc const *cb, stbi_uc const *cr, int count, int step);
313 // compute a conversion from YCbCr to RGB
314 // 'count' pixels
315 // write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
316 // y: Y input channel
317 // cb: Cb input channel; scale/biased to be 0..255
318 // cr: Cr input channel; scale/biased to be 0..255
319 
320 extern void stbi_install_idct(stbi_idct_8x8 func);
322 #endif // STBI_SIMD
323 
324 
325 #ifdef __cplusplus
326 }
327 #endif
328 
329 //
330 //
332 #endif // STBI_INCLUDE_STB_IMAGE_H
333 
334 /*
335  revision history:
336  1.33 (2011-07-14)
337  make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
338  1.32 (2011-07-13)
339  support for "info" function for all supported filetypes (SpartanJ)
340  1.31 (2011-06-20)
341  a few more leak fixes, bug in PNG handling (SpartanJ)
342  1.30 (2011-06-11)
343  added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
344  removed deprecated format-specific test/load functions
345  removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
346  error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
347  fix inefficiency in decoding 32-bit BMP (David Woo)
348  1.29 (2010-08-16)
349  various warning fixes from Aurelien Pocheville
350  1.28 (2010-08-01)
351  fix bug in GIF palette transparency (SpartanJ)
352  1.27 (2010-08-01)
353  cast-to-uint8 to fix warnings
354  1.26 (2010-07-24)
355  fix bug in file buffering for PNG reported by SpartanJ
356  1.25 (2010-07-17)
357  refix trans_data warning (Won Chun)
358  1.24 (2010-07-12)
359  perf improvements reading from files on platforms with lock-heavy fgetc()
360  minor perf improvements for jpeg
361  deprecated type-specific functions so we'll get feedback if they're needed
362  attempt to fix trans_data warning (Won Chun)
363  1.23 fixed bug in iPhone support
364  1.22 (2010-07-10)
365  removed image *writing* support
366  stbi_info support from Jetro Lauha
367  GIF support from Jean-Marc Lienher
368  iPhone PNG-extensions from James Brown
369  warning-fixes from Nicolas Schulz and Janez Zemva (i.e. Janez (U+017D)emva)
370  1.21 fix use of 'uint8' in header (reported by jon blow)
371  1.20 added support for Softimage PIC, by Tom Seddon
372  1.19 bug in interlaced PNG corruption check (found by ryg)
373  1.18 2008-08-02
374  fix a threading bug (local mutable static)
375  1.17 support interlaced PNG
376  1.16 major bugfix - convert_format converted one too many pixels
377  1.15 initialize some fields for thread safety
378  1.14 fix threadsafe conversion bug
379  header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
380  1.13 threadsafe
381  1.12 const qualifiers in the API
382  1.11 Support installable IDCT, colorspace conversion routines
383  1.10 Fixes for 64-bit (don't use "unsigned long")
384  optimized upsampling by Fabian "ryg" Giesen
385  1.09 Fix format-conversion for PSD code (bad global variables!)
386  1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
387  1.07 attempt to fix C++ warning/errors again
388  1.06 attempt to fix C++ warning/errors again
389  1.05 fix TGA loading to return correct *comp and use good luminance calc
390  1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
391  1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
392  1.02 support for (subset of) HDR files, float interface for preferred access to them
393  1.01 fix bug: possible bug in handling right-side up bmps... not sure
394  fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
395  1.00 interface to zlib that skips zlib header
396  0.99 correct handling of alpha in palette
397  0.98 TGA loader by lonesock; dynamically add loaders (untested)
398  0.97 jpeg errors on too large a file; also catch another malloc failure
399  0.96 fix detection of invalid v value - particleman@mollyrocket forum
400  0.95 during header scan, seek to markers in case of padding
401  0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
402  0.93 handle jpegtran output; verbose errors
403  0.92 read 4,8,16,24,32-bit BMP files of several formats
404  0.91 output 24-bit Windows 3.0 BMP files
405  0.90 fix a few more warnings; bump version number to approach 1.0
406  0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
407  0.60 fix compiling as c++
408  0.59 fix warnings: merge Dave Moore's -Wall fixes
409  0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
410  0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
411  0.56 fix bug: zlib uncompressed mode len vs. nlen
412  0.55 fix bug: restart_interval not initialized to 0
413  0.54 allow NULL for 'int *comp'
414  0.53 fix bug in png 3->4; speedup png decoding
415  0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
416  0.51 obey req_comp requests, 1-component jpegs return as 1-component,
417  on 'test' only check type, not whether we support this variant
418  0.50 first released version
419 */