stb_image.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. // SPDX-FileCopyrightText: stb http://nothings.org/stb
  2. // SPDX-License-Identifier: MIT
  3. /* stb_image - v2.28 - public domain image loader - http://nothings.org/stb
  4. no warranty implied; use at your own risk
  5. Do this:
  6. #define STB_IMAGE_IMPLEMENTATION
  7. before you include this file in *one* C or C++ file to create the implementation.
  8. // i.e. it should look like this:
  9. #include ...
  10. #include ...
  11. #include ...
  12. #define STB_IMAGE_IMPLEMENTATION
  13. #include "stb_image.h"
  14. You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.
  15. And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
  16. QUICK NOTES:
  17. Primarily of interest to game developers and other people who can
  18. avoid problematic images and only need the trivial interface
  19. JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
  20. PNG 1/2/4/8/16-bit-per-channel
  21. TGA (not sure what subset, if a subset)
  22. BMP non-1bpp, non-RLE
  23. PSD (composited view only, no extra channels, 8/16 bit-per-channel)
  24. GIF (*comp always reports as 4-channel)
  25. HDR (radiance rgbE format)
  26. PIC (Softimage PIC)
  27. PNM (PPM and PGM binary only)
  28. Animated GIF still needs a proper API, but here's one way to do it:
  29. http://gist.github.com/urraka/685d9a6340b26b830d49
  30. - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
  31. - decode from arbitrary I/O callbacks
  32. - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)
  33. Full documentation under "DOCUMENTATION" below.
  34. LICENSE
  35. See end of file for license information.
  36. RECENT REVISION HISTORY:
  37. 2.28 (2023-01-29) many error fixes, security errors, just tons of stuff
  38. 2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes
  39. 2.26 (2020-07-13) many minor fixes
  40. 2.25 (2020-02-02) fix warnings
  41. 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically
  42. 2.23 (2019-08-11) fix clang static analysis warning
  43. 2.22 (2019-03-04) gif fixes, fix warnings
  44. 2.21 (2019-02-25) fix typo in comment
  45. 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
  46. 2.19 (2018-02-11) fix warning
  47. 2.18 (2018-01-30) fix warnings
  48. 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
  49. 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes
  50. 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC
  51. 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
  52. 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
  53. 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
  54. 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
  55. RGB-format JPEG; remove white matting in PSD;
  56. allocate large structures on the stack;
  57. correct channel count for PNG & BMP
  58. 2.10 (2016-01-22) avoid warning introduced in 2.09
  59. 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
  60. See end of file for full revision history.
  61. ============================ Contributors =========================
  62. Image formats Extensions, features
  63. Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info)
  64. Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info)
  65. Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG)
  66. Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks)
  67. Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)
  68. Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)
  69. Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)
  70. github:urraka (animated gif) Junggon Kim (PNM comments)
  71. Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA)
  72. socks-the-fox (16-bit PNG)
  73. Jeremy Sawicki (handle all ImageNet JPGs)
  74. Optimizations & bugfixes Mikhail Morozov (1-bit BMP)
  75. Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
  76. Arseny Kapoulkine Simon Breuss (16-bit PNM)
  77. John-Mark Allen
  78. Carmelo J Fdez-Aguera
  79. Bug & warning fixes
  80. Marc LeBlanc David Woo Guillaume George Martins Mozeiko
  81. Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski
  82. Phil Jordan Dave Moore Roy Eltham
  83. Hayaki Saito Nathan Reed Won Chun
  84. Luke Graham Johan Duparc Nick Verigakis the Horde3D community
  85. Thomas Ruf Ronny Chevalier github:rlyeh
  86. Janez Zemva John Bartholomew Michal Cichon github:romigrou
  87. Jonathan Blow Ken Hamada Tero Hanninen github:svdijk
  88. Eugene Golushkov Laurent Gomila Cort Stratton github:snagar
  89. Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex
  90. Cass Everitt Ryamond Barbiero github:grim210
  91. Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw
  92. Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus
  93. Josh Tobin Neil Bickford Matthew Gregan github:poppolopoppo
  94. Julian Raschke Gregory Mullen Christian Floisand github:darealshinji
  95. Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007
  96. Brad Weinberger Matvey Cherevko github:mosra
  97. Luca Sas Alexander Veselov Zack Middleton [reserved]
  98. Ryan C. Gordon [reserved] [reserved]
  99. DO NOT ADD YOUR NAME HERE
  100. Jacko Dirks
  101. To add your name to the credits, pick a random blank space in the middle and fill it.
  102. 80% of merge conflicts on stb PRs are due to people adding their name at the end
  103. of the credits.
  104. */
  105. #ifndef STBI_INCLUDE_STB_IMAGE_H
  106. #define STBI_INCLUDE_STB_IMAGE_H
  107. // DOCUMENTATION
  108. //
  109. // Limitations:
  110. // - no 12-bit-per-channel JPEG
  111. // - no JPEGs with arithmetic coding
  112. // - GIF always returns *comp=4
  113. //
  114. // Basic usage (see HDR discussion below for HDR usage):
  115. // int x,y,n;
  116. // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
  117. // // ... process data if not NULL ...
  118. // // ... x = width, y = height, n = # 8-bit components per pixel ...
  119. // // ... replace '0' with '1'..'4' to force that many components per pixel
  120. // // ... but 'n' will always be the number that it would have been if you said 0
  121. // stbi_image_free(data);
  122. //
  123. // Standard parameters:
  124. // int *x -- outputs image width in pixels
  125. // int *y -- outputs image height in pixels
  126. // int *channels_in_file -- outputs # of image components in image file
  127. // int desired_channels -- if non-zero, # of image components requested in result
  128. //
  129. // The return value from an image loader is an 'unsigned char *' which points
  130. // to the pixel data, or NULL on an allocation failure or if the image is
  131. // corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
  132. // with each pixel consisting of N interleaved 8-bit components; the first
  133. // pixel pointed to is top-left-most in the image. There is no padding between
  134. // image scanlines or between pixels, regardless of format. The number of
  135. // components N is 'desired_channels' if desired_channels is non-zero, or
  136. // *channels_in_file otherwise. If desired_channels is non-zero,
  137. // *channels_in_file has the number of components that _would_ have been
  138. // output otherwise. E.g. if you set desired_channels to 4, you will always
  139. // get RGBA output, but you can check *channels_in_file to see if it's trivially
  140. // opaque because e.g. there were only 3 channels in the source image.
  141. //
  142. // An output image with N components has the following components interleaved
  143. // in this order in each pixel:
  144. //
  145. // N=#comp components
  146. // 1 grey
  147. // 2 grey, alpha
  148. // 3 red, green, blue
  149. // 4 red, green, blue, alpha
  150. //
  151. // If image loading fails for any reason, the return value will be NULL,
  152. // and *x, *y, *channels_in_file will be unchanged. The function
  153. // stbi_failure_reason() can be queried for an extremely brief, end-user
  154. // unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS
  155. // to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
  156. // more user-friendly ones.
  157. //
  158. // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
  159. //
  160. // To query the width, height and component count of an image without having to
  161. // decode the full file, you can use the stbi_info family of functions:
  162. //
  163. // int x,y,n,ok;
  164. // ok = stbi_info(filename, &x, &y, &n);
  165. // // returns ok=1 and sets x, y, n if image is a supported format,
  166. // // 0 otherwise.
  167. //
  168. // Note that stb_image pervasively uses ints in its public API for sizes,
  169. // including sizes of memory buffers. This is now part of the API and thus
  170. // hard to change without causing breakage. As a result, the various image
  171. // loaders all have certain limits on image size; these differ somewhat
  172. // by format but generally boil down to either just under 2GB or just under
  173. // 1GB. When the decoded image would be larger than this, stb_image decoding
  174. // will fail.
  175. //
  176. // Additionally, stb_image will reject image files that have any of their
  177. // dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS,
  178. // which defaults to 2**24 = 16777216 pixels. Due to the above memory limit,
  179. // the only way to have an image with such dimensions load correctly
  180. // is for it to have a rather extreme aspect ratio. Either way, the
  181. // assumption here is that such larger images are likely to be malformed
  182. // or malicious. If you do need to load an image with individual dimensions
  183. // larger than that, and it still fits in the overall size limit, you can
  184. // #define STBI_MAX_DIMENSIONS on your own to be something larger.
  185. //
  186. // ===========================================================================
  187. //
  188. // UNICODE:
  189. //
  190. // If compiling for Windows and you wish to use Unicode filenames, compile
  191. // with
  192. // #define STBI_WINDOWS_UTF8
  193. // and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert
  194. // Windows wchar_t filenames to utf8.
  195. //
  196. // ===========================================================================
  197. //
  198. // Philosophy
  199. //
  200. // stb libraries are designed with the following priorities:
  201. //
  202. // 1. easy to use
  203. // 2. easy to maintain
  204. // 3. good performance
  205. //
  206. // Sometimes I let "good performance" creep up in priority over "easy to maintain",
  207. // and for best performance I may provide less-easy-to-use APIs that give higher
  208. // performance, in addition to the easy-to-use ones. Nevertheless, it's important
  209. // to keep in mind that from the standpoint of you, a client of this library,
  210. // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.
  211. //
  212. // Some secondary priorities arise directly from the first two, some of which
  213. // provide more explicit reasons why performance can't be emphasized.
  214. //
  215. // - Portable ("ease of use")
  216. // - Small source code footprint ("easy to maintain")
  217. // - No dependencies ("ease of use")
  218. //
  219. // ===========================================================================
  220. //
  221. // I/O callbacks
  222. //
  223. // I/O callbacks allow you to read from arbitrary sources, like packaged
  224. // files or some other source. Data read from callbacks are processed
  225. // through a small internal buffer (currently 128 bytes) to try to reduce
  226. // overhead.
  227. //
  228. // The three functions you must define are "read" (reads some bytes of data),
  229. // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
  230. //
  231. // ===========================================================================
  232. //
  233. // SIMD support
  234. //
  235. // The JPEG decoder will try to automatically use SIMD kernels on x86 when
  236. // supported by the compiler. For ARM Neon support, you must explicitly
  237. // request it.
  238. //
  239. // (The old do-it-yourself SIMD API is no longer supported in the current
  240. // code.)
  241. //
  242. // On x86, SSE2 will automatically be used when available based on a run-time
  243. // test; if not, the generic C versions are used as a fall-back. On ARM targets,
  244. // the typical path is to have separate builds for NEON and non-NEON devices
  245. // (at least this is true for iOS and Android). Therefore, the NEON support is
  246. // toggled by a build flag: define STBI_NEON to get NEON loops.
  247. //
  248. // If for some reason you do not want to use any of SIMD code, or if
  249. // you have issues compiling it, you can disable it entirely by
  250. // defining STBI_NO_SIMD.
  251. //
  252. // ===========================================================================
  253. //
  254. // HDR image support (disable by defining STBI_NO_HDR)
  255. //
  256. // stb_image supports loading HDR images in general, and currently the Radiance
  257. // .HDR file format specifically. You can still load any file through the existing
  258. // interface; if you attempt to load an HDR file, it will be automatically remapped
  259. // to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
  260. // both of these constants can be reconfigured through this interface:
  261. //
  262. // stbi_hdr_to_ldr_gamma(2.2f);
  263. // stbi_hdr_to_ldr_scale(1.0f);
  264. //
  265. // (note, do not use _inverse_ constants; stbi_image will invert them
  266. // appropriately).
  267. //
  268. // Additionally, there is a new, parallel interface for loading files as
  269. // (linear) floats to preserve the full dynamic range:
  270. //
  271. // float *data = stbi_loadf(filename, &x, &y, &n, 0);
  272. //
  273. // If you load LDR images through this interface, those images will
  274. // be promoted to floating point values, run through the inverse of
  275. // constants corresponding to the above:
  276. //
  277. // stbi_ldr_to_hdr_scale(1.0f);
  278. // stbi_ldr_to_hdr_gamma(2.2f);
  279. //
  280. // Finally, given a filename (or an open file or memory block--see header
  281. // file for details) containing image data, you can query for the "most
  282. // appropriate" interface to use (that is, whether the image is HDR or
  283. // not), using:
  284. //
  285. // stbi_is_hdr(char *filename);
  286. //
  287. // ===========================================================================
  288. //
  289. // iPhone PNG support:
  290. //
  291. // We optionally support converting iPhone-formatted PNGs (which store
  292. // premultiplied BGRA) back to RGB, even though they're internally encoded
  293. // differently. To enable this conversion, call
  294. // stbi_convert_iphone_png_to_rgb(1).
  295. //
  296. // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
  297. // pixel to remove any premultiplied alpha *only* if the image file explicitly
  298. // says there's premultiplied data (currently only happens in iPhone images,
  299. // and only if iPhone convert-to-rgb processing is on).
  300. //
  301. // ===========================================================================
  302. //
  303. // ADDITIONAL CONFIGURATION
  304. //
  305. // - You can suppress implementation of any of the decoders to reduce
  306. // your code footprint by #defining one or more of the following
  307. // symbols before creating the implementation.
  308. //
  309. // STBI_NO_JPEG
  310. // STBI_NO_PNG
  311. // STBI_NO_BMP
  312. // STBI_NO_PSD
  313. // STBI_NO_TGA
  314. // STBI_NO_GIF
  315. // STBI_NO_HDR
  316. // STBI_NO_PIC
  317. // STBI_NO_PNM (.ppm and .pgm)
  318. //
  319. // - You can request *only* certain decoders and suppress all other ones
  320. // (this will be more forward-compatible, as addition of new decoders
  321. // doesn't require you to disable them explicitly):
  322. //
  323. // STBI_ONLY_JPEG
  324. // STBI_ONLY_PNG
  325. // STBI_ONLY_BMP
  326. // STBI_ONLY_PSD
  327. // STBI_ONLY_TGA
  328. // STBI_ONLY_GIF
  329. // STBI_ONLY_HDR
  330. // STBI_ONLY_PIC
  331. // STBI_ONLY_PNM (.ppm and .pgm)
  332. //
  333. // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
  334. // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
  335. //
  336. // - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater
  337. // than that size (in either width or height) without further processing.
  338. // This is to let programs in the wild set an upper bound to prevent
  339. // denial-of-service attacks on untrusted data, as one could generate a
  340. // valid image of gigantic dimensions and force stb_image to allocate a
  341. // huge block of memory and spend disproportionate time decoding it. By
  342. // default this is set to (1 << 24), which is 16777216, but that's still
  343. // very big.
  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 desired_channels
  351. STBI_grey = 1,
  352. STBI_grey_alpha = 2,
  353. STBI_rgb = 3,
  354. STBI_rgb_alpha = 4
  355. };
  356. #include <stdlib.h>
  357. typedef unsigned char stbi_uc;
  358. typedef unsigned short stbi_us;
  359. #ifdef __cplusplus
  360. extern "C" {
  361. #endif
  362. #ifndef STBIDEF
  363. #ifdef STB_IMAGE_STATIC
  364. #define STBIDEF static
  365. #else
  366. #define STBIDEF extern
  367. #endif
  368. #endif
  369. //////////////////////////////////////////////////////////////////////////////
  370. //
  371. // PRIMARY API - works on images of any type
  372. //
  373. //
  374. // load image by filename, open file, or memory buffer
  375. //
  376. typedef struct
  377. {
  378. int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
  379. void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
  380. int (*eof) (void *user); // returns nonzero if we are at end of file/data
  381. } stbi_io_callbacks;
  382. ////////////////////////////////////
  383. //
  384. // 8-bits-per-channel interface
  385. //
  386. STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
  387. 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);
  388. #ifndef STBI_NO_STDIO
  389. STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
  390. STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
  391. // for stbi_load_from_file, file pointer is left pointing immediately after image
  392. #endif
  393. #ifndef STBI_NO_GIF
  394. STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
  395. #endif
  396. #ifdef STBI_WINDOWS_UTF8
  397. STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
  398. #endif
  399. ////////////////////////////////////
  400. //
  401. // 16-bits-per-channel interface
  402. //
  403. STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
  404. STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
  405. #ifndef STBI_NO_STDIO
  406. STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
  407. STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
  408. #endif
  409. ////////////////////////////////////
  410. //
  411. // float-per-channel interface
  412. //
  413. #ifndef STBI_NO_LINEAR
  414. STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
  415. STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
  416. #ifndef STBI_NO_STDIO
  417. STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
  418. STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
  419. #endif
  420. #endif
  421. #ifndef STBI_NO_HDR
  422. STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);
  423. STBIDEF void stbi_hdr_to_ldr_scale(float scale);
  424. #endif // STBI_NO_HDR
  425. #ifndef STBI_NO_LINEAR
  426. STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);
  427. STBIDEF void stbi_ldr_to_hdr_scale(float scale);
  428. #endif // STBI_NO_LINEAR
  429. // stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR
  430. STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
  431. STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
  432. #ifndef STBI_NO_STDIO
  433. STBIDEF int stbi_is_hdr (char const *filename);
  434. STBIDEF int stbi_is_hdr_from_file(FILE *f);
  435. #endif // STBI_NO_STDIO
  436. // get a VERY brief reason for failure
  437. // on most compilers (and ALL modern mainstream compilers) this is threadsafe
  438. STBIDEF const char *stbi_failure_reason (void);
  439. // free the loaded image -- this is just free()
  440. STBIDEF void stbi_image_free (void *retval_from_stbi_load);
  441. // get image dimensions & components without fully decoding
  442. STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  443. STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
  444. STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);
  445. STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user);
  446. #ifndef STBI_NO_STDIO
  447. STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);
  448. STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
  449. STBIDEF int stbi_is_16_bit (char const *filename);
  450. STBIDEF int stbi_is_16_bit_from_file(FILE *f);
  451. #endif
  452. // for image formats that explicitly notate that they have premultiplied alpha,
  453. // we just return the colors as stored in the file. set this flag to force
  454. // unpremultiplication. results are undefined if the unpremultiply overflow.
  455. STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
  456. // indicate whether we should process iphone images back to canonical format,
  457. // or just pass them through "as-is"
  458. STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
  459. // flip the image vertically, so the first pixel in the output array is the bottom left
  460. STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);
  461. // as above, but only applies to images loaded on the thread that calls the function
  462. // this function is only available if your compiler supports thread-local variables;
  463. // calling it will fail to link if your compiler doesn't
  464. STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply);
  465. STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert);
  466. STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);
  467. // ZLIB client - used by PNG, available for other purposes
  468. STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
  469. STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
  470. STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
  471. STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  472. STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
  473. STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  474. #ifdef __cplusplus
  475. }
  476. #endif
  477. //
  478. //
  479. //// end header file /////////////////////////////////////////////////////
  480. #endif // STBI_INCLUDE_STB_IMAGE_H
  481. /*
  482. revision history:
  483. 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
  484. 2.19 (2018-02-11) fix warning
  485. 2.18 (2018-01-30) fix warnings
  486. 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug
  487. 1-bit BMP
  488. *_is_16_bit api
  489. avoid warnings
  490. 2.16 (2017-07-23) all functions have 16-bit variants;
  491. STBI_NO_STDIO works again;
  492. compilation fixes;
  493. fix rounding in unpremultiply;
  494. optimize vertical flip;
  495. disable raw_len validation;
  496. documentation fixes
  497. 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode;
  498. warning fixes; disable run-time SSE detection on gcc;
  499. uniform handling of optional "return" values;
  500. thread-safe initialization of zlib tables
  501. 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
  502. 2.13 (2016-11-29) add 16-bit API, only supported for PNG right now
  503. 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
  504. 2.11 (2016-04-02) allocate large structures on the stack
  505. remove white matting for transparent PSD
  506. fix reported channel count for PNG & BMP
  507. re-enable SSE2 in non-gcc 64-bit
  508. support RGB-formatted JPEG
  509. read 16-bit PNGs (only as 8-bit)
  510. 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED
  511. 2.09 (2016-01-16) allow comments in PNM files
  512. 16-bit-per-pixel TGA (not bit-per-component)
  513. info() for TGA could break due to .hdr handling
  514. info() for BMP to shares code instead of sloppy parse
  515. can use STBI_REALLOC_SIZED if allocator doesn't support realloc
  516. code cleanup
  517. 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA
  518. 2.07 (2015-09-13) fix compiler warnings
  519. partial animated GIF support
  520. limited 16-bpc PSD support
  521. #ifdef unused functions
  522. bug with < 92 byte PIC,PNM,HDR,TGA
  523. 2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value
  524. 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning
  525. 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit
  526. 2.03 (2015-04-12) extra corruption checking (mmozeiko)
  527. stbi_set_flip_vertically_on_load (nguillemot)
  528. fix NEON support; fix mingw support
  529. 2.02 (2015-01-19) fix incorrect assert, fix warning
  530. 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2
  531. 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG
  532. 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg)
  533. progressive JPEG (stb)
  534. PGM/PPM support (Ken Miller)
  535. STBI_MALLOC,STBI_REALLOC,STBI_FREE
  536. GIF bugfix -- seemingly never worked
  537. STBI_NO_*, STBI_ONLY_*
  538. 1.48 (2014-12-14) fix incorrectly-named assert()
  539. 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb)
  540. optimize PNG (ryg)
  541. fix bug in interlaced PNG with user-specified channel count (stb)
  542. 1.46 (2014-08-26)
  543. fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG
  544. 1.45 (2014-08-16)
  545. fix MSVC-ARM internal compiler error by wrapping malloc
  546. 1.44 (2014-08-07)
  547. various warning fixes from Ronny Chevalier
  548. 1.43 (2014-07-15)
  549. fix MSVC-only compiler problem in code changed in 1.42
  550. 1.42 (2014-07-09)
  551. don't define _CRT_SECURE_NO_WARNINGS (affects user code)
  552. fixes to stbi__cleanup_jpeg path
  553. added STBI_ASSERT to avoid requiring assert.h
  554. 1.41 (2014-06-25)
  555. fix search&replace from 1.36 that messed up comments/error messages
  556. 1.40 (2014-06-22)
  557. fix gcc struct-initialization warning
  558. 1.39 (2014-06-15)
  559. fix to TGA optimization when req_comp != number of components in TGA;
  560. fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite)
  561. add support for BMP version 5 (more ignored fields)
  562. 1.38 (2014-06-06)
  563. suppress MSVC warnings on integer casts truncating values
  564. fix accidental rename of 'skip' field of I/O
  565. 1.37 (2014-06-04)
  566. remove duplicate typedef
  567. 1.36 (2014-06-03)
  568. convert to header file single-file library
  569. if de-iphone isn't set, load iphone images color-swapped instead of returning NULL
  570. 1.35 (2014-05-27)
  571. various warnings
  572. fix broken STBI_SIMD path
  573. fix bug where stbi_load_from_file no longer left file pointer in correct place
  574. fix broken non-easy path for 32-bit BMP (possibly never used)
  575. TGA optimization by Arseny Kapoulkine
  576. 1.34 (unknown)
  577. use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case
  578. 1.33 (2011-07-14)
  579. make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
  580. 1.32 (2011-07-13)
  581. support for "info" function for all supported filetypes (SpartanJ)
  582. 1.31 (2011-06-20)
  583. a few more leak fixes, bug in PNG handling (SpartanJ)
  584. 1.30 (2011-06-11)
  585. added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
  586. removed deprecated format-specific test/load functions
  587. removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
  588. error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
  589. fix inefficiency in decoding 32-bit BMP (David Woo)
  590. 1.29 (2010-08-16)
  591. various warning fixes from Aurelien Pocheville
  592. 1.28 (2010-08-01)
  593. fix bug in GIF palette transparency (SpartanJ)
  594. 1.27 (2010-08-01)
  595. cast-to-stbi_uc to fix warnings
  596. 1.26 (2010-07-24)
  597. fix bug in file buffering for PNG reported by SpartanJ
  598. 1.25 (2010-07-17)
  599. refix trans_data warning (Won Chun)
  600. 1.24 (2010-07-12)
  601. perf improvements reading from files on platforms with lock-heavy fgetc()
  602. minor perf improvements for jpeg
  603. deprecated type-specific functions so we'll get feedback if they're needed
  604. attempt to fix trans_data warning (Won Chun)
  605. 1.23 fixed bug in iPhone support
  606. 1.22 (2010-07-10)
  607. removed image *writing* support
  608. stbi_info support from Jetro Lauha
  609. GIF support from Jean-Marc Lienher
  610. iPhone PNG-extensions from James Brown
  611. warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva)
  612. 1.21 fix use of 'stbi_uc' in header (reported by jon blow)
  613. 1.20 added support for Softimage PIC, by Tom Seddon
  614. 1.19 bug in interlaced PNG corruption check (found by ryg)
  615. 1.18 (2008-08-02)
  616. fix a threading bug (local mutable static)
  617. 1.17 support interlaced PNG
  618. 1.16 major bugfix - stbi__convert_format converted one too many pixels
  619. 1.15 initialize some fields for thread safety
  620. 1.14 fix threadsafe conversion bug
  621. header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
  622. 1.13 threadsafe
  623. 1.12 const qualifiers in the API
  624. 1.11 Support installable IDCT, colorspace conversion routines
  625. 1.10 Fixes for 64-bit (don't use "unsigned long")
  626. optimized upsampling by Fabian "ryg" Giesen
  627. 1.09 Fix format-conversion for PSD code (bad global variables!)
  628. 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
  629. 1.07 attempt to fix C++ warning/errors again
  630. 1.06 attempt to fix C++ warning/errors again
  631. 1.05 fix TGA loading to return correct *comp and use good luminance calc
  632. 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
  633. 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
  634. 1.02 support for (subset of) HDR files, float interface for preferred access to them
  635. 1.01 fix bug: possible bug in handling right-side up bmps... not sure
  636. fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all
  637. 1.00 interface to zlib that skips zlib header
  638. 0.99 correct handling of alpha in palette
  639. 0.98 TGA loader by lonesock; dynamically add loaders (untested)
  640. 0.97 jpeg errors on too large a file; also catch another malloc failure
  641. 0.96 fix detection of invalid v value - particleman@mollyrocket forum
  642. 0.95 during header scan, seek to markers in case of padding
  643. 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
  644. 0.93 handle jpegtran output; verbose errors
  645. 0.92 read 4,8,16,24,32-bit BMP files of several formats
  646. 0.91 output 24-bit Windows 3.0 BMP files
  647. 0.90 fix a few more warnings; bump version number to approach 1.0
  648. 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
  649. 0.60 fix compiling as c++
  650. 0.59 fix warnings: merge Dave Moore's -Wall fixes
  651. 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
  652. 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
  653. 0.56 fix bug: zlib uncompressed mode len vs. nlen
  654. 0.55 fix bug: restart_interval not initialized to 0
  655. 0.54 allow NULL for 'int *comp'
  656. 0.53 fix bug in png 3->4; speedup png decoding
  657. 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
  658. 0.51 obey req_comp requests, 1-component jpegs return as 1-component,
  659. on 'test' only check type, not whether we support this variant
  660. 0.50 (2006-11-19)
  661. first released version
  662. */
  663. /*
  664. ------------------------------------------------------------------------------
  665. This software is available under 2 licenses -- choose whichever you prefer.
  666. ------------------------------------------------------------------------------
  667. ALTERNATIVE A - MIT License
  668. Copyright (c) 2017 Sean Barrett
  669. Permission is hereby granted, free of charge, to any person obtaining a copy of
  670. this software and associated documentation files (the "Software"), to deal in
  671. the Software without restriction, including without limitation the rights to
  672. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  673. of the Software, and to permit persons to whom the Software is furnished to do
  674. so, subject to the following conditions:
  675. The above copyright notice and this permission notice shall be included in all
  676. copies or substantial portions of the Software.
  677. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  678. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  679. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  680. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  681. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  682. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  683. SOFTWARE.
  684. ------------------------------------------------------------------------------
  685. ALTERNATIVE B - Public Domain (www.unlicense.org)
  686. This is free and unencumbered software released into the public domain.
  687. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  688. software, either in source code form or as a compiled binary, for any purpose,
  689. commercial or non-commercial, and by any means.
  690. In jurisdictions that recognize copyright laws, the author or authors of this
  691. software dedicate any and all copyright interest in the software to the public
  692. domain. We make this dedication for the benefit of the public at large and to
  693. the detriment of our heirs and successors. We intend this dedication to be an
  694. overt act of relinquishment in perpetuity of all present and future rights to
  695. this software under copyright law.
  696. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  697. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  698. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  699. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  700. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  701. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  702. ------------------------------------------------------------------------------
  703. */