duilib自学教程
stb_image.h
1 #define STBI_NO_STDIO
2 #define STBI_NO_WRITE
3 #define STBI_NO_HDR
4 /* stb_image - v2.05 - public domain image loader - http://nothings.org/stb_image.h
5  no warranty implied; use at your own risk
6 
7  Do this:
8  #define STB_IMAGE_IMPLEMENTATION
9  before you include this file in *one* C or C++ file to create the implementation.
10 
11  // i.e. it should look like this:
12  #include ...
13  #include ...
14  #include ...
15  #define STB_IMAGE_IMPLEMENTATION
16  #include "stb_image.h"
17 
18  You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.
19  And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
20 
21 
22  QUICK NOTES:
23  Primarily of interest to game developers and other people who can
24  avoid problematic images and only need the trivial interface
25 
26  JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
27  PNG 1/2/4/8-bit-per-channel (16 bpc not supported)
28 
29  TGA (not sure what subset, if a subset)
30  BMP non-1bpp, non-RLE
31  PSD (composited view only, no extra channels)
32 
33  GIF (*comp always reports as 4-channel)
34  HDR (radiance rgbE format)
35  PIC (Softimage PIC)
36  PNM (PPM and PGM binary only)
37 
38  - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
39  - decode from arbitrary I/O callbacks
40  - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)
41 
42  Full documentation under "DOCUMENTATION" below.
43 
44 
45  Revision 2.00 release notes:
46 
47  - Progressive JPEG is now supported.
48 
49  - PPM and PGM binary formats are now supported, thanks to Ken Miller.
50 
51  - x86 platforms now make use of SSE2 SIMD instructions for
52  JPEG decoding, and ARM platforms can use NEON SIMD if requested.
53  This work was done by Fabian "ryg" Giesen. SSE2 is used by
54  default, but NEON must be enabled explicitly; see docs.
55 
56  With other JPEG optimizations included in this version, we see
57  2x speedup on a JPEG on an x86 machine, and a 1.5x speedup
58  on a JPEG on an ARM machine, relative to previous versions of this
59  library. The same results will not obtain for all JPGs and for all
60  x86/ARM machines. (Note that progressive JPEGs are significantly
61  slower to decode than regular JPEGs.) This doesn't mean that this
62  is the fastest JPEG decoder in the land; rather, it brings it
63  closer to parity with standard libraries. If you want the fastest
64  decode, look elsewhere. (See "Philosophy" section of docs below.)
65 
66  See final bullet items below for more info on SIMD.
67 
68  - Added STBI_MALLOC, STBI_REALLOC, and STBI_FREE macros for replacing
69  the memory allocator. Unlike other STBI libraries, these macros don't
70  support a context parameter, so if you need to pass a context in to
71  the allocator, you'll have to store it in a global or a thread-local
72  variable.
73 
74  - Split existing STBI_NO_HDR flag into two flags, STBI_NO_HDR and
75  STBI_NO_LINEAR.
76  STBI_NO_HDR: suppress implementation of .hdr reader format
77  STBI_NO_LINEAR: suppress high-dynamic-range light-linear float API
78 
79  - You can suppress implementation of any of the decoders to reduce
80  your code footprint by #defining one or more of the following
81  symbols before creating the implementation.
82 
83  STBI_NO_JPEG
84  STBI_NO_PNG
85  STBI_NO_BMP
86  STBI_NO_PSD
87  STBI_NO_TGA
88  STBI_NO_GIF
89  STBI_NO_HDR
90  STBI_NO_PIC
91  STBI_NO_PNM (.ppm and .pgm)
92 
93  - You can request *only* certain decoders and suppress all other ones
94  (this will be more forward-compatible, as addition of new decoders
95  doesn't require you to disable them explicitly):
96 
97  STBI_ONLY_JPEG
98  STBI_ONLY_PNG
99  STBI_ONLY_BMP
100  STBI_ONLY_PSD
101  STBI_ONLY_TGA
102  STBI_ONLY_GIF
103  STBI_ONLY_HDR
104  STBI_ONLY_PIC
105  STBI_ONLY_PNM (.ppm and .pgm)
106 
107  Note that you can define multiples of these, and you will get all
108  of them ("only x" and "only y" is interpreted to mean "only x&y").
109 
110  - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
111  want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
112 
113  - Compilation of all SIMD code can be suppressed with
114  #define STBI_NO_SIMD
115  It should not be necessary to disable SIMD unless you have issues
116  compiling (e.g. using an x86 compiler which doesn't support SSE
117  intrinsics or that doesn't support the method used to detect
118  SSE2 support at run-time), and even those can be reported as
119  bugs so I can refine the built-in compile-time checking to be
120  smarter.
121 
122  - The old STBI_SIMD system which allowed installing a user-defined
123  IDCT etc. has been removed. If you need this, don't upgrade. My
124  assumption is that almost nobody was doing this, and those who
125  were will find the built-in SIMD more satisfactory anyway.
126 
127  - RGB values computed for JPEG images are slightly different from
128  previous versions of stb_image. (This is due to using less
129  integer precision in SIMD.) The C code has been adjusted so
130  that the same RGB values will be computed regardless of whether
131  SIMD support is available, so your app should always produce
132  consistent results. But these results are slightly different from
133  previous versions. (Specifically, about 3% of available YCbCr values
134  will compute different RGB results from pre-1.49 versions by +-1;
135  most of the deviating values are one smaller in the G channel.)
136 
137  - If you must produce consistent results with previous versions of
138  stb_image, #define STBI_JPEG_OLD and you will get the same results
139  you used to; however, you will not get the SIMD speedups for
140  the YCbCr-to-RGB conversion step (although you should still see
141  significant JPEG speedup from the other changes).
142 
143  Please note that STBI_JPEG_OLD is a temporary feature; it will be
144  removed in future versions of the library. It is only intended for
145  near-term back-compatibility use.
146 
147 
148  Latest revision history:
149  2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning
150  2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit
151  2.03 (2015-04-12) additional corruption checking
152  stbi_set_flip_vertically_on_load
153  fix NEON support; fix mingw support
154  2.02 (2015-01-19) fix incorrect assert, fix warning
155  2.01 (2015-01-17) fix various warnings
156  2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG
157  2.00 (2014-12-25) optimize JPEG, including x86 SSE2 & ARM NEON SIMD
158  progressive JPEG
159  PGM/PPM support
160  STBI_MALLOC,STBI_REALLOC,STBI_FREE
161  STBI_NO_*, STBI_ONLY_*
162  GIF bugfix
163  1.48 (2014-12-14) fix incorrectly-named assert()
164  1.47 (2014-12-14) 1/2/4-bit PNG support (both grayscale and paletted)
165  optimize PNG
166  fix bug in interlaced PNG with user-specified channel count
167 
168  See end of file for full revision history.
169 
170 
171  ============================ Contributors =========================
172 
173  Image formats Bug fixes & warning fixes
174  Sean Barrett (jpeg, png, bmp) Marc LeBlanc
175  Nicolas Schulz (hdr, psd) Christpher Lloyd
176  Jonathan Dummer (tga) Dave Moore
177  Jean-Marc Lienher (gif) Won Chun
178  Tom Seddon (pic) the Horde3D community
179  Thatcher Ulrich (psd) Janez Zemva
180  Ken Miller (pgm, ppm) Jonathan Blow
181  Laurent Gomila
182  Aruelien Pocheville
183  Extensions, features Ryamond Barbiero
184  Jetro Lauha (stbi_info) David Woo
185  Martin "SpartanJ" Golini (stbi_info) Martin Golini
186  James "moose2000" Brown (iPhone PNG) Roy Eltham
187  Ben "Disch" Wenger (io callbacks) Luke Graham
188  Omar Cornut (1/2/4-bit PNG) Thomas Ruf
189  Nicolas Guillemot (vertical flip) John Bartholomew
190  Ken Hamada
191  Optimizations & bugfixes Cort Stratton
192  Fabian "ryg" Giesen Blazej Dariusz Roszkowski
193  Arseny Kapoulkine Thibault Reuille
194  Paul Du Bois
195  Guillaume George
196  If your name should be here but Jerry Jansson
197  isn't, let Sean know. Hayaki Saito
198  Johan Duparc
199  Ronny Chevalier
200  Michal Cichon
201  Tero Hanninen
202  Sergio Gonzalez
203  Cass Everitt
204  Engin Manap
205  Martins Mozeiko
206  Joseph Thomson
207  Phil Jordan
208 
209 License:
210  This software is in the public domain. Where that dedication is not
211  recognized, you are granted a perpetual, irrevocable license to copy
212  and modify this file however you want.
213 
214 */
215 
216 #ifndef STBI_INCLUDE_STB_IMAGE_H
217 #define STBI_INCLUDE_STB_IMAGE_H
218 
219 // DOCUMENTATION
220 //
221 // Limitations:
222 // - no 16-bit-per-channel PNG
223 // - no 12-bit-per-channel JPEG
224 // - no JPEGs with arithmetic coding
225 // - no 1-bit BMP
226 // - GIF always returns *comp=4
227 //
228 // Basic usage (see HDR discussion below for HDR usage):
229 // int x,y,n;
230 // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
231 // // ... process data if not NULL ...
232 // // ... x = width, y = height, n = # 8-bit components per pixel ...
233 // // ... replace '0' with '1'..'4' to force that many components per pixel
234 // // ... but 'n' will always be the number that it would have been if you said 0
235 // stbi_image_free(data)
236 //
237 // Standard parameters:
238 // int *x -- outputs image width in pixels
239 // int *y -- outputs image height in pixels
240 // int *comp -- outputs # of image components in image file
241 // int req_comp -- if non-zero, # of image components requested in result
242 //
243 // The return value from an image loader is an 'unsigned char *' which points
244 // to the pixel data, or NULL on an allocation failure or if the image is
245 // corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
246 // with each pixel consisting of N interleaved 8-bit components; the first
247 // pixel pointed to is top-left-most in the image. There is no padding between
248 // image scanlines or between pixels, regardless of format. The number of
249 // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
250 // If req_comp is non-zero, *comp has the number of components that _would_
251 // have been output otherwise. E.g. if you set req_comp to 4, you will always
252 // get RGBA output, but you can check *comp to see if it's trivially opaque
253 // because e.g. there were only 3 channels in the source image.
254 //
255 // An output image with N components has the following components interleaved
256 // in this order in each pixel:
257 //
258 // N=#comp components
259 // 1 grey
260 // 2 grey, alpha
261 // 3 red, green, blue
262 // 4 red, green, blue, alpha
263 //
264 // If image loading fails for any reason, the return value will be NULL,
265 // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
266 // can be queried for an extremely brief, end-user unfriendly explanation
267 // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
268 // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
269 // more user-friendly ones.
270 //
271 // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
272 //
273 // ===========================================================================
274 //
275 // Philosophy
276 //
277 // stb libraries are designed with the following priorities:
278 //
279 // 1. easy to use
280 // 2. easy to maintain
281 // 3. good performance
282 //
283 // Sometimes I let "good performance" creep up in priority over "easy to maintain",
284 // and for best performance I may provide less-easy-to-use APIs that give higher
285 // performance, in addition to the easy to use ones. Nevertheless, it's important
286 // to keep in mind that from the standpoint of you, a client of this library,
287 // all you care about is #1 and #3, and stb libraries do not emphasize #3 above all.
288 //
289 // Some secondary priorities arise directly from the first two, some of which
290 // make more explicit reasons why performance can't be emphasized.
291 //
292 // - Portable ("ease of use")
293 // - Small footprint ("easy to maintain")
294 // - No dependencies ("ease of use")
295 //
296 // ===========================================================================
297 //
298 // I/O callbacks
299 //
300 // I/O callbacks allow you to read from arbitrary sources, like packaged
301 // files or some other source. Data read from callbacks are processed
302 // through a small internal buffer (currently 128 bytes) to try to reduce
303 // overhead.
304 //
305 // The three functions you must define are "read" (reads some bytes of data),
306 // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
307 //
308 // ===========================================================================
309 //
310 // SIMD support
311 //
312 // The JPEG decoder will try to automatically use SIMD kernels on x86 when
313 // supported by the compiler. For ARM Neon support, you must explicitly
314 // request it.
315 //
316 // (The old do-it-yourself SIMD API is no longer supported in the current
317 // code.)
318 //
319 // On x86, SSE2 will automatically be used when available based on a run-time
320 // test; if not, the generic C versions are used as a fall-back. On ARM targets,
321 // the typical path is to have separate builds for NEON and non-NEON devices
322 // (at least this is true for iOS and Android). Therefore, the NEON support is
323 // toggled by a build flag: define STBI_NEON to get NEON loops.
324 //
325 // The output of the JPEG decoder is slightly different from versions where
326 // SIMD support was introduced (that is, for versions before 1.49). The
327 // difference is only +-1 in the 8-bit RGB channels, and only on a small
328 // fraction of pixels. You can force the pre-1.49 behavior by defining
329 // STBI_JPEG_OLD, but this will disable some of the SIMD decoding path
330 // and hence cost some performance.
331 //
332 // If for some reason you do not want to use any of SIMD code, or if
333 // you have issues compiling it, you can disable it entirely by
334 // defining STBI_NO_SIMD.
335 //
336 // ===========================================================================
337 //
338 // HDR image support (disable by defining STBI_NO_HDR)
339 //
340 // stb_image now supports loading HDR images in general, and currently
341 // the Radiance .HDR file format, although the support is provided
342 // generically. You can still load any file through the existing interface;
343 // if you attempt to load an HDR file, it will be automatically remapped to
344 // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
345 // both of these constants can be reconfigured through this interface:
346 //
347 // stbi_hdr_to_ldr_gamma(2.2f);
348 // stbi_hdr_to_ldr_scale(1.0f);
349 //
350 // (note, do not use _inverse_ constants; stbi_image will invert them
351 // appropriately).
352 //
353 // Additionally, there is a new, parallel interface for loading files as
354 // (linear) floats to preserve the full dynamic range:
355 //
356 // float *data = stbi_loadf(filename, &x, &y, &n, 0);
357 //
358 // If you load LDR images through this interface, those images will
359 // be promoted to floating point values, run through the inverse of
360 // constants corresponding to the above:
361 //
362 // stbi_ldr_to_hdr_scale(1.0f);
363 // stbi_ldr_to_hdr_gamma(2.2f);
364 //
365 // Finally, given a filename (or an open file or memory block--see header
366 // file for details) containing image data, you can query for the "most
367 // appropriate" interface to use (that is, whether the image is HDR or
368 // not), using:
369 //
370 // stbi_is_hdr(char *filename);
371 //
372 // ===========================================================================
373 //
374 // iPhone PNG support:
375 //
376 // By default we convert iphone-formatted PNGs back to RGB, even though
377 // they are internally encoded differently. You can disable this conversion
378 // by by calling stbi_convert_iphone_png_to_rgb(0), in which case
379 // you will always just get the native iphone "format" through (which
380 // is BGR stored in RGB).
381 //
382 // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
383 // pixel to remove any premultiplied alpha *only* if the image file explicitly
384 // says there's premultiplied data (currently only happens in iPhone images,
385 // and only if iPhone convert-to-rgb processing is on).
386 //
387 
388 
389 #ifndef STBI_NO_STDIO
390 #include <stdio.h>
391 #endif // STBI_NO_STDIO
392 
393 #define STBI_VERSION 1
394 
395 enum
396 {
397  STBI_default = 0, // only used for req_comp
398 
399  STBI_grey = 1,
400  STBI_grey_alpha = 2,
401  STBI_rgb = 3,
402  STBI_rgb_alpha = 4
403 };
404 
405 typedef unsigned char stbi_uc;
406 
407 #ifdef __cplusplus
408 extern "C" {
409 #endif
410 
411 #ifdef STB_IMAGE_STATIC
412 #define STBIDEF static
413 #else
414 #define STBIDEF extern
415 #endif
416 
418 //
419 // PRIMARY API - works on images of any type
420 //
421 
422 //
423 // load image by filename, open file, or memory buffer
424 //
425 
426 typedef struct
427 {
428  int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
429  void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
430  int (*eof) (void *user); // returns nonzero if we are at end of file/data
432 
433 STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
434 STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *comp, int req_comp);
435 STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *comp, int req_comp);
436 
437 #ifndef STBI_NO_STDIO
438 STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
439 // for stbi_load_from_file, file pointer is left pointing immediately after image
440 #endif
441 
442 #ifndef STBI_NO_LINEAR
443  STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
444  STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
445  STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
446 
447  #ifndef STBI_NO_STDIO
448  STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
449  #endif
450 #endif
451 
452 #ifndef STBI_NO_HDR
453  STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);
454  STBIDEF void stbi_hdr_to_ldr_scale(float scale);
455 #endif
456 
457 #ifndef STBI_NO_LINEAR
458  STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);
459  STBIDEF void stbi_ldr_to_hdr_scale(float scale);
460 #endif // STBI_NO_HDR
461 
462 // stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR
463 STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
464 STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
465 #ifndef STBI_NO_STDIO
466 STBIDEF int stbi_is_hdr (char const *filename);
467 STBIDEF int stbi_is_hdr_from_file(FILE *f);
468 #endif // STBI_NO_STDIO
469 
470 
471 // get a VERY brief reason for failure
472 // NOT THREADSAFE
473 STBIDEF const char *stbi_failure_reason (void);
474 
475 // free the loaded image -- this is just free()
476 STBIDEF void stbi_image_free (void *retval_from_stbi_load);
477 
478 // get image dimensions & components without fully decoding
479 STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
480 STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
481 
482 #ifndef STBI_NO_STDIO
483 STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);
484 STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
485 
486 #endif
487 
488 
489 
490 // for image formats that explicitly notate that they have premultiplied alpha,
491 // we just return the colors as stored in the file. set this flag to force
492 // unpremultiplication. results are undefined if the unpremultiply overflow.
493 STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
494 
495 // indicate whether we should process iphone images back to canonical format,
496 // or just pass them through "as-is"
497 STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
498 
499 // flip the image vertically, so the first pixel in the output array is the bottom left
500 STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);
501 
502 // ZLIB client - used by PNG, available for other purposes
503 
504 STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
505 STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
506 STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
507 STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
508 
509 STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
510 STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
511 
512 
513 #ifdef __cplusplus
514 }
515 #endif
516 
517 //
518 //
520 #endif // STBI_INCLUDE_STB_IMAGE_H
stbi_io_callbacks
Definition: stb_image.h:427