stb_image.h 22 KB

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