/xbmc/visualizations/XBMCProjectM/libprojectM/stb_image_aug.h

http://github.com/xbmc/xbmc · C++ Header · 313 lines · 112 code · 42 blank · 159 comment · 1 complexity · b7c8f2c132f8e4bb55daccab59a9cb19 MD5 · raw file

  1. /* stbi-1.03 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
  2. when you control the images you're loading
  3. QUICK NOTES:
  4. Primarily of interest to game developers and other people who can
  5. avoid problematic images and only need the trivial interface
  6. JPEG baseline (no JPEG progressive, no oddball channel decimations)
  7. PNG non-interlaced
  8. BMP non-1bpp, non-RLE
  9. TGA (not sure what subset, if a subset)
  10. HDR (radiance rgbE format)
  11. writes BMP,TGA (define STBI_NO_WRITE to remove code)
  12. decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)
  13. TODO:
  14. stbi_info_*
  15. PSD loader
  16. history:
  17. 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
  18. 1.02 support for (subset of) HDR files, float interface for preferred access to them
  19. 1.01 fix bug: possible bug in handling right-side up bmps... not sure
  20. fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
  21. 1.00 interface to zlib that skips zlib header
  22. 0.99 correct handling of alpha in palette
  23. 0.98 TGA loader by lonesock; dynamically add loaders (untested)
  24. 0.97 jpeg errors on too large a file; also catch another malloc failure
  25. 0.96 fix detection of invalid v value - particleman@mollyrocket forum
  26. 0.95 during header scan, seek to markers in case of padding
  27. 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
  28. 0.93 handle jpegtran output; verbose errors
  29. 0.92 read 4,8,16,24,32-bit BMP files of several formats
  30. 0.91 output 24-bit Windows 3.0 BMP files
  31. 0.90 fix a few more warnings; bump version number to approach 1.0
  32. 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
  33. 0.60 fix compiling as c++
  34. 0.59 fix warnings: merge Dave Moore's -Wall fixes
  35. 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
  36. 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less
  37. than 16 available
  38. 0.56 fix bug: zlib uncompressed mode len vs. nlen
  39. 0.55 fix bug: restart_interval not initialized to 0
  40. 0.54 allow NULL for 'int *comp'
  41. 0.53 fix bug in png 3->4; speedup png decoding
  42. 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
  43. 0.51 obey req_comp requests, 1-component jpegs return as 1-component,
  44. on 'test' only check type, not whether we support this variant
  45. */
  46. #ifndef HEADER_STB_IMAGE_AUGMENTED
  47. #define HEADER_STB_IMAGE_AUGMENTED
  48. //// begin header file ////////////////////////////////////////////////////
  49. //
  50. // Limitations:
  51. // - no progressive/interlaced support (jpeg, png)
  52. // - 8-bit samples only (jpeg, png)
  53. // - not threadsafe
  54. // - channel subsampling of at most 2 in each dimension (jpeg)
  55. // - no delayed line count (jpeg) -- IJG doesn't support either
  56. //
  57. // Basic usage (see HDR discussion below):
  58. // int x,y,n;
  59. // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
  60. // // ... process data if not NULL ...
  61. // // ... x = width, y = height, n = # 8-bit components per pixel ...
  62. // // ... replace '0' with '1'..'4' to force that many components per pixel
  63. // stbi_image_free(data)
  64. //
  65. // Standard parameters:
  66. // int *x -- outputs image width in pixels
  67. // int *y -- outputs image height in pixels
  68. // int *comp -- outputs # of image components in image file
  69. // int req_comp -- if non-zero, # of image components requested in result
  70. //
  71. // The return value from an image loader is an 'unsigned char *' which points
  72. // to the pixel data. The pixel data consists of *y scanlines of *x pixels,
  73. // with each pixel consisting of N interleaved 8-bit components; the first
  74. // pixel pointed to is top-left-most in the image. There is no padding between
  75. // image scanlines or between pixels, regardless of format. The number of
  76. // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
  77. // If req_comp is non-zero, *comp has the number of components that _would_
  78. // have been output otherwise. E.g. if you set req_comp to 4, you will always
  79. // get RGBA output, but you can check *comp to easily see if it's opaque.
  80. //
  81. // An output image with N components has the following components interleaved
  82. // in this order in each pixel:
  83. //
  84. // N=#comp components
  85. // 1 grey
  86. // 2 grey, alpha
  87. // 3 red, green, blue
  88. // 4 red, green, blue, alpha
  89. //
  90. // If image loading fails for any reason, the return value will be NULL,
  91. // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
  92. // can be queried for an extremely brief, end-user unfriendly explanation
  93. // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
  94. // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
  95. // more user-friendly ones.
  96. //
  97. // Paletted PNG and BMP images are automatically depalettized.
  98. //
  99. //
  100. // ===========================================================================
  101. //
  102. // HDR image support (disable by defining STBI_NO_HDR)
  103. //
  104. // stb_image now supports loading HDR images in general, and currently
  105. // the Radiance .HDR file format, although the support is provided
  106. // generically. You can still load any file through the existing interface;
  107. // if you attempt to load an HDR file, it will be automatically remapped to
  108. // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
  109. // both of these constants can be reconfigured through this interface:
  110. //
  111. // stbi_hdr_to_ldr_gamma(2.2f);
  112. // stbi_hdr_to_ldr_scale(1.0f);
  113. //
  114. // (note, do not use _inverse_ constants; stbi_image will invert them
  115. // appropriately).
  116. //
  117. // Additionally, there is a new, parallel interface for loading files as
  118. // (linear) floats to preserve the full dynamic range:
  119. //
  120. // float *data = stbi_loadf(filename, &x, &y, &n, 0);
  121. //
  122. // If you load LDR images through this interface, those images will
  123. // be promoted to floating point values, run through the inverse of
  124. // constants corresponding to the above:
  125. //
  126. // stbi_ldr_to_hdr_scale(1.0f);
  127. // stbi_ldr_to_hdr_gamma(2.2f);
  128. //
  129. // Finally, given a filename (or an open file or memory block--see header
  130. // file for details) containing image data, you can query for the "most
  131. // appropriate" interface to use (that is, whether the image is HDR or
  132. // not), using:
  133. //
  134. // stbi_is_hdr(char *filename);
  135. #ifndef STBI_NO_STDIO
  136. #include <stdio.h>
  137. #endif
  138. #ifndef STBI_NO_HDR
  139. #include <math.h> // ldexp
  140. #include <string.h> // strcmp
  141. #endif
  142. enum
  143. {
  144. STBI_default = 0, // only used for req_comp
  145. STBI_grey = 1,
  146. STBI_grey_alpha = 2,
  147. STBI_rgb = 3,
  148. STBI_rgb_alpha = 4,
  149. };
  150. typedef unsigned char stbi_uc;
  151. #ifdef __cplusplus
  152. extern "C" {
  153. #endif
  154. // WRITING API
  155. #if !defined(STBI_NO_WRITE) && !defined(STBI_NO_STDIO)
  156. // write a BMP/TGA file given tightly packed 'comp' channels (no padding, nor bmp-stride-padding)
  157. // (you must include the appropriate extension in the filename).
  158. // returns TRUE on success, FALSE if couldn't open file, error writing file
  159. extern int stbi_write_bmp (char *filename, int x, int y, int comp, void *data);
  160. extern int stbi_write_tga (char *filename, int x, int y, int comp, void *data);
  161. #endif
  162. // PRIMARY API - works on images of any type
  163. // load image by filename, open file, or memory buffer
  164. #ifndef STBI_NO_STDIO
  165. extern stbi_uc *stbi_load (char *filename, int *x, int *y, int *comp, int req_comp);
  166. extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  167. extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
  168. #endif
  169. extern stbi_uc *stbi_load_from_memory(stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp);
  170. // for stbi_load_from_file, file pointer is left pointing immediately after image
  171. #ifndef STBI_NO_HDR
  172. #ifndef STBI_NO_STDIO
  173. extern float *stbi_loadf (char *filename, int *x, int *y, int *comp, int req_comp);
  174. extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  175. #endif
  176. extern float *stbi_loadf_from_memory(stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp);
  177. extern void stbi_hdr_to_ldr_gamma(float gamma);
  178. extern void stbi_hdr_to_ldr_scale(float scale);
  179. extern void stbi_ldr_to_hdr_gamma(float gamma);
  180. extern void stbi_ldr_to_hdr_scale(float scale);
  181. #endif // STBI_NO_HDR
  182. // get a VERY brief reason for failure
  183. extern char *stbi_failure_reason (void);
  184. // free the loaded image -- this is just free()
  185. extern void stbi_image_free (stbi_uc *retval_from_stbi_load);
  186. // get image dimensions & components without fully decoding
  187. extern int stbi_info_from_memory(stbi_uc *buffer, int len, int *x, int *y, int *comp);
  188. extern int stbi_is_hdr_from_memory(stbi_uc *buffer, int len);
  189. #ifndef STBI_NO_STDIO
  190. extern int stbi_info (char *filename, int *x, int *y, int *comp);
  191. extern int stbi_is_hdr (char *filename);
  192. extern int stbi_is_hdr_from_file(FILE *f);
  193. #endif
  194. // ZLIB client - used by PNG, available for other purposes
  195. extern char *stbi_zlib_decode_malloc_guesssize(int initial_size, int *outlen);
  196. extern char *stbi_zlib_decode_malloc(char *buffer, int len, int *outlen);
  197. extern int stbi_zlib_decode_buffer(char *obuffer, int olen, char *ibuffer, int ilen);
  198. extern char *stbi_zlib_decode_noheader_malloc(char *buffer, int len, int *outlen);
  199. extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, char *ibuffer, int ilen);
  200. // TYPE-SPECIFIC ACCESS
  201. // is it a jpeg?
  202. extern int stbi_jpeg_test_memory (stbi_uc *buffer, int len);
  203. extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp);
  204. extern int stbi_jpeg_info_from_memory(stbi_uc *buffer, int len, int *x, int *y, int *comp);
  205. #ifndef STBI_NO_STDIO
  206. extern stbi_uc *stbi_jpeg_load (char *filename, int *x, int *y, int *comp, int req_comp);
  207. extern int stbi_jpeg_test_file (FILE *f);
  208. extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  209. extern int stbi_jpeg_info (char *filename, int *x, int *y, int *comp);
  210. extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
  211. #endif
  212. extern int stbi_jpeg_dc_only; // only decode DC component
  213. // is it a png?
  214. extern int stbi_png_test_memory (stbi_uc *buffer, int len);
  215. extern stbi_uc *stbi_png_load_from_memory (stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp);
  216. extern int stbi_png_info_from_memory (stbi_uc *buffer, int len, int *x, int *y, int *comp);
  217. #ifndef STBI_NO_STDIO
  218. extern stbi_uc *stbi_png_load (char *filename, int *x, int *y, int *comp, int req_comp);
  219. extern int stbi_png_info (char *filename, int *x, int *y, int *comp);
  220. extern int stbi_png_test_file (FILE *f);
  221. extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  222. extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp);
  223. #endif
  224. // is it a bmp?
  225. extern int stbi_bmp_test_memory (stbi_uc *buffer, int len);
  226. extern stbi_uc *stbi_bmp_load (char *filename, int *x, int *y, int *comp, int req_comp);
  227. extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp);
  228. #ifndef STBI_NO_STDIO
  229. extern int stbi_bmp_test_file (FILE *f);
  230. extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  231. #endif
  232. // is it a tga?
  233. extern int stbi_tga_test_memory (stbi_uc *buffer, int len);
  234. extern stbi_uc *stbi_tga_load (char *filename, int *x, int *y, int *comp, int req_comp);
  235. extern stbi_uc *stbi_tga_load_from_memory (stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp);
  236. #ifndef STBI_NO_STDIO
  237. extern int stbi_tga_test_file (FILE *f);
  238. extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  239. #endif
  240. // is it an hdr?
  241. extern int stbi_hdr_test_memory (stbi_uc *buffer, int len);
  242. extern float * stbi_hdr_load (char *filename, int *x, int *y, int *comp, int req_comp);
  243. extern float * stbi_hdr_load_from_memory (stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp);
  244. #ifndef STBI_NO_STDIO
  245. extern int stbi_hdr_test_file (FILE *f);
  246. extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  247. #endif
  248. // define new loaders
  249. typedef struct
  250. {
  251. int (*test_memory)(stbi_uc *buffer, int len);
  252. stbi_uc * (*load_from_memory)(stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp);
  253. #ifndef STBI_NO_STDIO
  254. int (*test_file)(FILE *f);
  255. stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);
  256. #endif
  257. } stbi_loader;
  258. // register a loader by filling out the above structure (you must defined ALL functions)
  259. // returns 1 if added or already added, 0 if not added (too many loaders)
  260. extern int stbi_register_loader(stbi_loader *loader);
  261. #ifdef __cplusplus
  262. }
  263. #endif
  264. //
  265. //
  266. //// end header file /////////////////////////////////////////////////////
  267. #endif