stb_image.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* stbi-1.29 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
  2. when you control the images you're loading
  3. no warranty implied; use at your own risk
  4. QUICK NOTES:
  5. Primarily of interest to game developers and other people who can
  6. avoid problematic images and only need the trivial interface
  7. JPEG baseline (no JPEG progressive)
  8. PNG 8-bit only
  9. TGA (not sure what subset, if a subset)
  10. BMP non-1bpp, non-RLE
  11. PSD (composited view only, no extra channels)
  12. GIF (*comp always reports as 4-channel)
  13. HDR (radiance rgbE format)
  14. PIC (Softimage PIC)
  15. - decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)
  16. - supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
  17. Latest revisions:
  18. 1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
  19. 1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
  20. 1.27 (2010-08-01) cast-to-uint8 to fix warnings (Laurent Gomila)
  21. allow trailing 0s at end of image data (Laurent Gomila)
  22. 1.26 (2010-07-24) fix bug in file buffering for PNG reported by SpartanJ
  23. 1.25 (2010-07-17) refix trans_data warning (Won Chun)
  24. 1.24 (2010-07-12) perf improvements reading from files
  25. minor perf improvements for jpeg
  26. deprecated type-specific functions in hope of feedback
  27. attempt to fix trans_data warning (Won Chun)
  28. 1.23 fixed bug in iPhone support
  29. 1.22 (2010-07-10) removed image *writing* support to stb_image_write.h
  30. stbi_info support from Jetro Lauha
  31. GIF support from Jean-Marc Lienher
  32. iPhone PNG-extensions from James Brown
  33. warning-fixes from Nicolas Schulz and Janez Zemva
  34. 1.21 fix use of 'uint8' in header (reported by jon blow)
  35. 1.20 added support for Softimage PIC, by Tom Seddon
  36. See end of file for full revision history.
  37. TODO:
  38. stbi_info support for BMP,PSD,HDR,PIC
  39. rewrite stbi_info and load_file variations to share file handling code
  40. (current system allows individual functions to be called directly,
  41. since each does all the work, but I doubt anyone uses this in practice)
  42. ============================ Contributors =========================
  43. Image formats Optimizations & bugfixes
  44. Sean Barrett (jpeg, png, bmp) Fabian "ryg" Giesen
  45. Nicolas Schulz (hdr, psd)
  46. Jonathan Dummer (tga) Bug fixes & warning fixes
  47. Jean-Marc Lienher (gif) Marc LeBlanc
  48. Tom Seddon (pic) Christpher Lloyd
  49. Thatcher Ulrich (psd) Dave Moore
  50. Won Chun
  51. the Horde3D community
  52. Extensions, features Janez Zemva
  53. Jetro Lauha (stbi_info) Jonathan Blow
  54. James "moose2000" Brown (iPhone PNG) Laurent Gomila
  55. Aruelien Pocheville
  56. If your name should be here but isn't, let Sean know.
  57. */
  58. #ifndef STBI_INCLUDE_STB_IMAGE_H
  59. #define STBI_INCLUDE_STB_IMAGE_H
  60. // To get a header file for this, either cut and paste the header,
  61. // or create stb_image.h, #define STBI_HEADER_FILE_ONLY, and
  62. // then include stb_image.c from it.
  63. //// begin header file ////////////////////////////////////////////////////
  64. //
  65. // Limitations:
  66. // - no jpeg progressive support
  67. // - non-HDR formats support 8-bit samples only (jpeg, png)
  68. // - no delayed line count (jpeg) -- IJG doesn't support either
  69. // - no 1-bit BMP
  70. // - GIF always returns *comp=4
  71. //
  72. // Basic usage (see HDR discussion below):
  73. // int x,y,n;
  74. // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
  75. // // ... process data if not NULL ...
  76. // // ... x = width, y = height, n = # 8-bit components per pixel ...
  77. // // ... replace '0' with '1'..'4' to force that many components per pixel
  78. // stbi_image_free(data)
  79. //
  80. // Standard parameters:
  81. // int *x -- outputs image width in pixels
  82. // int *y -- outputs image height in pixels
  83. // int *comp -- outputs # of image components in image file
  84. // int req_comp -- if non-zero, # of image components requested in result
  85. //
  86. // The return value from an image loader is an 'unsigned char *' which points
  87. // to the pixel data. The pixel data consists of *y scanlines of *x pixels,
  88. // with each pixel consisting of N interleaved 8-bit components; the first
  89. // pixel pointed to is top-left-most in the image. There is no padding between
  90. // image scanlines or between pixels, regardless of format. The number of
  91. // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
  92. // If req_comp is non-zero, *comp has the number of components that _would_
  93. // have been output otherwise. E.g. if you set req_comp to 4, you will always
  94. // get RGBA output, but you can check *comp to easily see if it's opaque.
  95. //
  96. // An output image with N components has the following components interleaved
  97. // in this order in each pixel:
  98. //
  99. // N=#comp components
  100. // 1 grey
  101. // 2 grey, alpha
  102. // 3 red, green, blue
  103. // 4 red, green, blue, alpha
  104. //
  105. // If image loading fails for any reason, the return value will be NULL,
  106. // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
  107. // can be queried for an extremely brief, end-user unfriendly explanation
  108. // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
  109. // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
  110. // more user-friendly ones.
  111. //
  112. // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
  113. //
  114. // ===========================================================================
  115. //
  116. // iPhone PNG support:
  117. //
  118. // By default we convert iphone-formatted PNGs back to RGB; nominally they
  119. // would silently load as BGR, except the existing code should have just
  120. // failed on such iPhone PNGs. But you can disable this conversion by
  121. // by calling stbi_convert_iphone_png_to_rgb(0), in which case
  122. // you will always just get the native iphone "format" through.
  123. //
  124. // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
  125. // pixel to remove any premultiplied alpha *only* if the image file explicitly
  126. // says there's premultiplied data (currently only happens in iPhone images,
  127. // and only if iPhone convert-to-rgb processing is on).
  128. //
  129. // ===========================================================================
  130. //
  131. // HDR image support (disable by defining STBI_NO_HDR)
  132. //
  133. // stb_image now supports loading HDR images in general, and currently
  134. // the Radiance .HDR file format, although the support is provided
  135. // generically. You can still load any file through the existing interface;
  136. // if you attempt to load an HDR file, it will be automatically remapped to
  137. // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
  138. // both of these constants can be reconfigured through this interface:
  139. //
  140. // stbi_hdr_to_ldr_gamma(2.2f);
  141. // stbi_hdr_to_ldr_scale(1.0f);
  142. //
  143. // (note, do not use _inverse_ constants; stbi_image will invert them
  144. // appropriately).
  145. //
  146. // Additionally, there is a new, parallel interface for loading files as
  147. // (linear) floats to preserve the full dynamic range:
  148. //
  149. // float *data = stbi_loadf(filename, &x, &y, &n, 0);
  150. //
  151. // If you load LDR images through this interface, those images will
  152. // be promoted to floating point values, run through the inverse of
  153. // constants corresponding to the above:
  154. //
  155. // stbi_ldr_to_hdr_scale(1.0f);
  156. // stbi_ldr_to_hdr_gamma(2.2f);
  157. //
  158. // Finally, given a filename (or an open file or memory block--see header
  159. // file for details) containing image data, you can query for the "most
  160. // appropriate" interface to use (that is, whether the image is HDR or
  161. // not), using:
  162. //
  163. // stbi_is_hdr(char *filename);
  164. #ifndef STBI_NO_STDIO
  165. #include <stdio.h>
  166. #endif
  167. #define STBI_VERSION 1
  168. enum
  169. {
  170. STBI_default = 0, // only used for req_comp
  171. STBI_grey = 1,
  172. STBI_grey_alpha = 2,
  173. STBI_rgb = 3,
  174. STBI_rgb_alpha = 4
  175. };
  176. typedef unsigned char stbi_uc;
  177. #ifdef __cplusplus
  178. extern "C" {
  179. #endif
  180. // PRIMARY API - works on images of any type
  181. // load image by filename, open file, or memory buffer
  182. extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  183. #ifndef STBI_NO_STDIO
  184. extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  185. extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  186. // for stbi_load_from_file, file pointer is left pointing immediately after image
  187. #endif
  188. #ifndef STBI_NO_HDR
  189. extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  190. #ifndef STBI_NO_STDIO
  191. extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
  192. extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  193. #endif
  194. extern void stbi_hdr_to_ldr_gamma(float gamma);
  195. extern void stbi_hdr_to_ldr_scale(float scale);
  196. extern void stbi_ldr_to_hdr_gamma(float gamma);
  197. extern void stbi_ldr_to_hdr_scale(float scale);
  198. #endif // STBI_NO_HDR
  199. // get a VERY brief reason for failure
  200. // NOT THREADSAFE
  201. extern const char *stbi_failure_reason (void);
  202. // free the loaded image -- this is just free()
  203. extern void stbi_image_free (void *retval_from_stbi_load);
  204. // get image dimensions & components without fully decoding
  205. extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  206. extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
  207. #ifndef STBI_NO_STDIO
  208. extern int stbi_info (char const *filename, int *x, int *y, int *comp);
  209. extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
  210. extern int stbi_is_hdr (char const *filename);
  211. extern int stbi_is_hdr_from_file(FILE *f);
  212. #endif
  213. // for image formats that explicitly notate that they have premultiplied alpha,
  214. // we just return the colors as stored in the file. set this flag to force
  215. // unpremultiplication. results are undefined if the unpremultiply overflow.
  216. extern void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
  217. // indicate whether we should process iphone images back to canonical format,
  218. // or just pass them through "as-is"
  219. extern void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
  220. // ZLIB client - used by PNG, available for other purposes
  221. extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
  222. extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
  223. extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  224. extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
  225. extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  226. // define new loaders
  227. typedef struct
  228. {
  229. int (*test_memory)(stbi_uc const *buffer, int len);
  230. stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  231. #ifndef STBI_NO_STDIO
  232. int (*test_file)(FILE *f);
  233. stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);
  234. #endif
  235. } stbi_loader;
  236. // register a loader by filling out the above structure (you must define ALL functions)
  237. // returns 1 if added or already added, 0 if not added (too many loaders)
  238. // NOT THREADSAFE
  239. extern int stbi_register_loader(stbi_loader *loader);
  240. // define faster low-level operations (typically SIMD support)
  241. #ifdef STBI_SIMD
  242. typedef void (*stbi_idct_8x8)(stbi_uc *out, int out_stride, short data[64], unsigned short *dequantize);
  243. // compute an integer IDCT on "input"
  244. // input[x] = data[x] * dequantize[x]
  245. // write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
  246. // CLAMP results to 0..255
  247. typedef void (*stbi_YCbCr_to_RGB_run)(stbi_uc *output, stbi_uc const *y, stbi_uc const *cb, stbi_uc const *cr, int count, int step);
  248. // compute a conversion from YCbCr to RGB
  249. // 'count' pixels
  250. // write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
  251. // y: Y input channel
  252. // cb: Cb input channel; scale/biased to be 0..255
  253. // cr: Cr input channel; scale/biased to be 0..255
  254. extern void stbi_install_idct(stbi_idct_8x8 func);
  255. extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
  256. #endif // STBI_SIMD
  257. // TYPE-SPECIFIC ACCESS
  258. #ifdef STBI_TYPE_SPECIFIC_FUNCTIONS
  259. // is it a jpeg?
  260. extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len);
  261. extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  262. extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  263. #ifndef STBI_NO_STDIO
  264. extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  265. extern int stbi_jpeg_test_file (FILE *f);
  266. extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  267. extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
  268. extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
  269. #endif
  270. // is it a png?
  271. extern int stbi_png_test_memory (stbi_uc const *buffer, int len);
  272. extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  273. extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  274. #ifndef STBI_NO_STDIO
  275. extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  276. extern int stbi_png_info (char const *filename, int *x, int *y, int *comp);
  277. extern int stbi_png_test_file (FILE *f);
  278. extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  279. extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp);
  280. #endif
  281. // is it a bmp?
  282. extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len);
  283. extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  284. extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  285. #ifndef STBI_NO_STDIO
  286. extern int stbi_bmp_test_file (FILE *f);
  287. extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  288. #endif
  289. // is it a tga?
  290. extern int stbi_tga_test_memory (stbi_uc const *buffer, int len);
  291. extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  292. extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  293. #ifndef STBI_NO_STDIO
  294. extern int stbi_tga_test_file (FILE *f);
  295. extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  296. #endif
  297. // is it a psd?
  298. extern int stbi_psd_test_memory (stbi_uc const *buffer, int len);
  299. extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  300. extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  301. #ifndef STBI_NO_STDIO
  302. extern int stbi_psd_test_file (FILE *f);
  303. extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  304. #endif
  305. // is it an hdr?
  306. extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len);
  307. extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  308. extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  309. #ifndef STBI_NO_STDIO
  310. extern int stbi_hdr_test_file (FILE *f);
  311. extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  312. #endif
  313. // is it a pic?
  314. extern int stbi_pic_test_memory (stbi_uc const *buffer, int len);
  315. extern stbi_uc *stbi_pic_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  316. extern stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  317. #ifndef STBI_NO_STDIO
  318. extern int stbi_pic_test_file (FILE *f);
  319. extern stbi_uc *stbi_pic_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  320. #endif
  321. // is it a gif?
  322. extern int stbi_gif_test_memory (stbi_uc const *buffer, int len);
  323. extern stbi_uc *stbi_gif_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  324. extern stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  325. extern int stbi_gif_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  326. #ifndef STBI_NO_STDIO
  327. extern int stbi_gif_test_file (FILE *f);
  328. extern stbi_uc *stbi_gif_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  329. extern int stbi_gif_info (char const *filename, int *x, int *y, int *comp);
  330. extern int stbi_gif_info_from_file (FILE *f, int *x, int *y, int *comp);
  331. #endif
  332. #endif//STBI_TYPE_SPECIFIC_FUNCTIONS
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. //
  337. //
  338. //// end header file /////////////////////////////////////////////////////
  339. #endif // STBI_INCLUDE_STB_IMAGE_H