stb_image.h 22 KB

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