stb_image.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* stb_image - v2.14 - public domain image loader - http://nothings.org/stb_image.h
  2. no warranty implied; use at your own risk
  3. Do this:
  4. #define STB_IMAGE_IMPLEMENTATION
  5. before you include this file in *one* C or C++ file to create the implementation.
  6. // i.e. it should look like this:
  7. #include ...
  8. #include ...
  9. #include ...
  10. #define STB_IMAGE_IMPLEMENTATION
  11. #include "stb_image.h"
  12. You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.
  13. And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
  14. QUICK NOTES:
  15. Primarily of interest to game developers and other people who can
  16. avoid problematic images and only need the trivial interface
  17. JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
  18. PNG 1/2/4/8/16-bit-per-channel
  19. TGA (not sure what subset, if a subset)
  20. BMP non-1bpp, non-RLE
  21. PSD (composited view only, no extra channels, 8/16 bit-per-channel)
  22. GIF (*comp always reports as 4-channel)
  23. HDR (radiance rgbE format)
  24. PIC (Softimage PIC)
  25. PNM (PPM and PGM binary only)
  26. Animated GIF still needs a proper API, but here's one way to do it:
  27. http://gist.github.com/urraka/685d9a6340b26b830d49
  28. - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
  29. - decode from arbitrary I/O callbacks
  30. - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)
  31. Full documentation under "DOCUMENTATION" below.
  32. LICENSE
  33. See end of file for license information.
  34. RECENT REVISION HISTORY:
  35. 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
  36. 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
  37. 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
  38. 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
  39. RGB-format JPEG; remove white matting in PSD;
  40. allocate large structures on the stack;
  41. correct channel count for PNG & BMP
  42. 2.10 (2016-01-22) avoid warning introduced in 2.09
  43. 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
  44. 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA
  45. 2.07 (2015-09-13) partial animated GIF support
  46. limited 16-bit PSD support
  47. minor bugs, code cleanup, and compiler warnings
  48. See end of file for full revision history.
  49. ============================ Contributors =========================
  50. Image formats Extensions, features
  51. Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info)
  52. Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info)
  53. Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG)
  54. Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks)
  55. Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)
  56. Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)
  57. Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)
  58. github:urraka (animated gif) Junggon Kim (PNM comments)
  59. Daniel Gibson (16-bit TGA)
  60. socks-the-fox (16-bit TGA)
  61. Jeremy Sawicki (handle all ImageNet JPGs)
  62. Optimizations & bugfixes
  63. Fabian "ryg" Giesen
  64. Arseny Kapoulkine
  65. Bug & warning fixes
  66. Marc LeBlanc David Woo Guillaume George Martins Mozeiko
  67. Christpher Lloyd Martin Golini Jerry Jansson Joseph Thomson
  68. Dave Moore Roy Eltham Hayaki Saito Phil Jordan
  69. Won Chun Luke Graham Johan Duparc Nathan Reed
  70. the Horde3D community Thomas Ruf Ronny Chevalier Nick Verigakis
  71. Janez Zemva John Bartholomew Michal Cichon github:svdijk
  72. Jonathan Blow Ken Hamada Tero Hanninen Baldur Karlsson
  73. Laurent Gomila Cort Stratton Sergio Gonzalez github:romigrou
  74. Aruelien Pocheville Thibault Reuille Cass Everitt Matthew Gregan
  75. Ryamond Barbiero Paul Du Bois Engin Manap github:snagar
  76. Michaelangel007@github Oriol Ferrer Mesia Dale Weiler github:Zelex
  77. Philipp Wiesemann Josh Tobin github:rlyeh github:grim210@github
  78. Blazej Dariusz Roszkowski github:sammyhw
  79. */
  80. #ifndef STBI_INCLUDE_STB_IMAGE_H
  81. #define STBI_INCLUDE_STB_IMAGE_H
  82. // DOCUMENTATION
  83. //
  84. // Limitations:
  85. // - no 16-bit-per-channel PNG
  86. // - no 12-bit-per-channel JPEG
  87. // - no JPEGs with arithmetic coding
  88. // - no 1-bit BMP
  89. // - GIF always returns *comp=4
  90. //
  91. // Basic usage (see HDR discussion below for HDR usage):
  92. // int x,y,n;
  93. // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
  94. // // ... process data if not NULL ...
  95. // // ... x = width, y = height, n = # 8-bit components per pixel ...
  96. // // ... replace '0' with '1'..'4' to force that many components per pixel
  97. // // ... but 'n' will always be the number that it would have been if you said 0
  98. // stbi_image_free(data)
  99. //
  100. // Standard parameters:
  101. // int *x -- outputs image width in pixels
  102. // int *y -- outputs image height in pixels
  103. // int *channels_in_file -- outputs # of image components in image file
  104. // int desired_channels -- if non-zero, # of image components requested in result
  105. //
  106. // The return value from an image loader is an 'unsigned char *' which points
  107. // to the pixel data, or NULL on an allocation failure or if the image is
  108. // corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
  109. // with each pixel consisting of N interleaved 8-bit components; the first
  110. // pixel pointed to is top-left-most in the image. There is no padding between
  111. // image scanlines or between pixels, regardless of format. The number of
  112. // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
  113. // If req_comp is non-zero, *comp has the number of components that _would_
  114. // have been output otherwise. E.g. if you set req_comp to 4, you will always
  115. // get RGBA output, but you can check *comp to see if it's trivially opaque
  116. // because e.g. there were only 3 channels in the source image.
  117. //
  118. // An output image with N components has the following components interleaved
  119. // in this order in each pixel:
  120. //
  121. // N=#comp components
  122. // 1 grey
  123. // 2 grey, alpha
  124. // 3 red, green, blue
  125. // 4 red, green, blue, alpha
  126. //
  127. // If image loading fails for any reason, the return value will be NULL,
  128. // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
  129. // can be queried for an extremely brief, end-user unfriendly explanation
  130. // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
  131. // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
  132. // more user-friendly ones.
  133. //
  134. // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
  135. //
  136. // ===========================================================================
  137. //
  138. // Philosophy
  139. //
  140. // stb libraries are designed with the following priorities:
  141. //
  142. // 1. easy to use
  143. // 2. easy to maintain
  144. // 3. good performance
  145. //
  146. // Sometimes I let "good performance" creep up in priority over "easy to maintain",
  147. // and for best performance I may provide less-easy-to-use APIs that give higher
  148. // performance, in addition to the easy to use ones. Nevertheless, it's important
  149. // to keep in mind that from the standpoint of you, a client of this library,
  150. // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.
  151. //
  152. // Some secondary priorities arise directly from the first two, some of which
  153. // make more explicit reasons why performance can't be emphasized.
  154. //
  155. // - Portable ("ease of use")
  156. // - Small source code footprint ("easy to maintain")
  157. // - No dependencies ("ease of use")
  158. //
  159. // ===========================================================================
  160. //
  161. // I/O callbacks
  162. //
  163. // I/O callbacks allow you to read from arbitrary sources, like packaged
  164. // files or some other source. Data read from callbacks are processed
  165. // through a small internal buffer (currently 128 bytes) to try to reduce
  166. // overhead.
  167. //
  168. // The three functions you must define are "read" (reads some bytes of data),
  169. // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
  170. //
  171. // ===========================================================================
  172. //
  173. // SIMD support
  174. //
  175. // The JPEG decoder will try to automatically use SIMD kernels on x86 when
  176. // supported by the compiler. For ARM Neon support, you must explicitly
  177. // request it.
  178. //
  179. // (The old do-it-yourself SIMD API is no longer supported in the current
  180. // code.)
  181. //
  182. // On x86, SSE2 will automatically be used when available based on a run-time
  183. // test; if not, the generic C versions are used as a fall-back. On ARM targets,
  184. // the typical path is to have separate builds for NEON and non-NEON devices
  185. // (at least this is true for iOS and Android). Therefore, the NEON support is
  186. // toggled by a build flag: define STBI_NEON to get NEON loops.
  187. //
  188. // If for some reason you do not want to use any of SIMD code, or if
  189. // you have issues compiling it, you can disable it entirely by
  190. // defining STBI_NO_SIMD.
  191. //
  192. // ===========================================================================
  193. //
  194. // HDR image support (disable by defining STBI_NO_HDR)
  195. //
  196. // stb_image now supports loading HDR images in general, and currently
  197. // the Radiance .HDR file format, although the support is provided
  198. // generically. You can still load any file through the existing interface;
  199. // if you attempt to load an HDR file, it will be automatically remapped to
  200. // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
  201. // both of these constants can be reconfigured through this interface:
  202. //
  203. // stbi_hdr_to_ldr_gamma(2.2f);
  204. // stbi_hdr_to_ldr_scale(1.0f);
  205. //
  206. // (note, do not use _inverse_ constants; stbi_image will invert them
  207. // appropriately).
  208. //
  209. // Additionally, there is a new, parallel interface for loading files as
  210. // (linear) floats to preserve the full dynamic range:
  211. //
  212. // float *data = stbi_loadf(filename, &x, &y, &n, 0);
  213. //
  214. // If you load LDR images through this interface, those images will
  215. // be promoted to floating point values, run through the inverse of
  216. // constants corresponding to the above:
  217. //
  218. // stbi_ldr_to_hdr_scale(1.0f);
  219. // stbi_ldr_to_hdr_gamma(2.2f);
  220. //
  221. // Finally, given a filename (or an open file or memory block--see header
  222. // file for details) containing image data, you can query for the "most
  223. // appropriate" interface to use (that is, whether the image is HDR or
  224. // not), using:
  225. //
  226. // stbi_is_hdr(char *filename);
  227. //
  228. // ===========================================================================
  229. //
  230. // iPhone PNG support:
  231. //
  232. // By default we convert iphone-formatted PNGs back to RGB, even though
  233. // they are internally encoded differently. You can disable this conversion
  234. // by by calling stbi_convert_iphone_png_to_rgb(0), in which case
  235. // you will always just get the native iphone "format" through (which
  236. // is BGR stored in RGB).
  237. //
  238. // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
  239. // pixel to remove any premultiplied alpha *only* if the image file explicitly
  240. // says there's premultiplied data (currently only happens in iPhone images,
  241. // and only if iPhone convert-to-rgb processing is on).
  242. //
  243. // ===========================================================================
  244. //
  245. // ADDITIONAL CONFIGURATION
  246. //
  247. // - You can suppress implementation of any of the decoders to reduce
  248. // your code footprint by #defining one or more of the following
  249. // symbols before creating the implementation.
  250. //
  251. // STBI_NO_JPEG
  252. // STBI_NO_PNG
  253. // STBI_NO_BMP
  254. // STBI_NO_PSD
  255. // STBI_NO_TGA
  256. // STBI_NO_GIF
  257. // STBI_NO_HDR
  258. // STBI_NO_PIC
  259. // STBI_NO_PNM (.ppm and .pgm)
  260. //
  261. // - You can request *only* certain decoders and suppress all other ones
  262. // (this will be more forward-compatible, as addition of new decoders
  263. // doesn't require you to disable them explicitly):
  264. //
  265. // STBI_ONLY_JPEG
  266. // STBI_ONLY_PNG
  267. // STBI_ONLY_BMP
  268. // STBI_ONLY_PSD
  269. // STBI_ONLY_TGA
  270. // STBI_ONLY_GIF
  271. // STBI_ONLY_HDR
  272. // STBI_ONLY_PIC
  273. // STBI_ONLY_PNM (.ppm and .pgm)
  274. //
  275. // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
  276. // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
  277. //
  278. #ifndef STBI_NO_STDIO
  279. #include <stdio.h>
  280. #endif // STBI_NO_STDIO
  281. #define STBI_VERSION 1
  282. enum
  283. {
  284. STBI_default = 0, // only used for req_comp
  285. STBI_grey = 1,
  286. STBI_grey_alpha = 2,
  287. STBI_rgb = 3,
  288. STBI_rgb_alpha = 4
  289. };
  290. typedef unsigned char stbi_uc;
  291. typedef unsigned short stbi_us;
  292. #ifdef __cplusplus
  293. extern "C" {
  294. #endif
  295. #ifdef STB_IMAGE_STATIC
  296. #define STBIDEF static
  297. #else
  298. #define STBIDEF extern
  299. #endif
  300. //////////////////////////////////////////////////////////////////////////////
  301. //
  302. // PRIMARY API - works on images of any type
  303. //
  304. //
  305. // load image by filename, open file, or memory buffer
  306. //
  307. typedef struct
  308. {
  309. int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
  310. void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
  311. int (*eof) (void *user); // returns nonzero if we are at end of file/data
  312. } stbi_io_callbacks;
  313. ////////////////////////////////////
  314. //
  315. // 8-bits-per-channel interface
  316. //
  317. STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
  318. STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
  319. 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);
  320. #ifndef STBI_NO_STDIO
  321. STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
  322. // for stbi_load_from_file, file pointer is left pointing immediately after image
  323. #endif
  324. ////////////////////////////////////
  325. //
  326. // 16-bits-per-channel interface
  327. //
  328. STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
  329. #ifndef STBI_NO_STDIO
  330. STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
  331. #endif
  332. // @TODO the other variants
  333. ////////////////////////////////////
  334. //
  335. // float-per-channel interface
  336. //
  337. #ifndef STBI_NO_LINEAR
  338. STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
  339. STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
  340. STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
  341. #ifndef STBI_NO_STDIO
  342. STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
  343. #endif
  344. #endif
  345. #ifndef STBI_NO_HDR
  346. STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);
  347. STBIDEF void stbi_hdr_to_ldr_scale(float scale);
  348. #endif // STBI_NO_HDR
  349. #ifndef STBI_NO_LINEAR
  350. STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);
  351. STBIDEF void stbi_ldr_to_hdr_scale(float scale);
  352. #endif // STBI_NO_LINEAR
  353. // stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR
  354. STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
  355. STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
  356. #ifndef STBI_NO_STDIO
  357. STBIDEF int stbi_is_hdr (char const *filename);
  358. STBIDEF int stbi_is_hdr_from_file(FILE *f);
  359. #endif // STBI_NO_STDIO
  360. // get a VERY brief reason for failure
  361. // NOT THREADSAFE
  362. STBIDEF const char *stbi_failure_reason (void);
  363. // free the loaded image -- this is just free()
  364. STBIDEF void stbi_image_free (void *retval_from_stbi_load);
  365. // get image dimensions & components without fully decoding
  366. STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  367. STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
  368. #ifndef STBI_NO_STDIO
  369. STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);
  370. STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
  371. #endif
  372. // for image formats that explicitly notate that they have premultiplied alpha,
  373. // we just return the colors as stored in the file. set this flag to force
  374. // unpremultiplication. results are undefined if the unpremultiply overflow.
  375. STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
  376. // indicate whether we should process iphone images back to canonical format,
  377. // or just pass them through "as-is"
  378. STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
  379. // flip the image vertically, so the first pixel in the output array is the bottom left
  380. STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);
  381. // ZLIB client - used by PNG, available for other purposes
  382. STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
  383. STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
  384. STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
  385. STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  386. STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
  387. STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  388. #ifdef __cplusplus
  389. }
  390. #endif
  391. //
  392. //
  393. //// end header file /////////////////////////////////////////////////////
  394. #endif // STBI_INCLUDE_STB_IMAGE_H