Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4649 lines
152KB

  1. /* stb_image - v1.39 - 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. Do this:
  5. #define STB_IMAGE_IMPLEMENTATION
  6. before you include this file in *one* C or C++ file to create the implementation.
  7. QUICK NOTES:
  8. Primarily of interest to game developers and other people who can
  9. avoid problematic images and only need the trivial interface
  10. JPEG baseline (no JPEG progressive)
  11. PNG 8-bit-per-channel only
  12. TGA (not sure what subset, if a subset)
  13. BMP non-1bpp, non-RLE
  14. PSD (composited view only, no extra channels)
  15. GIF (*comp always reports as 4-channel)
  16. HDR (radiance rgbE format)
  17. PIC (Softimage PIC)
  18. - stbi__jpeg_huff_decode from memory or through FILE (define STBI_NO_STDIO to remove code)
  19. - stbi__jpeg_huff_decode from arbitrary I/O callbacks
  20. - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
  21. Latest revisions:
  22. 1.39 (2014-06-15) TGA optimization fix, multiple BMP fixes
  23. 1.38 (2014-06-06) suppress MSVC run-time warnings, fix accidental rename of 'skip'
  24. 1.37 (2014-06-04) remove duplicate typedef
  25. 1.36 (2014-06-03) converted to header file, allow reading incorrect iphoned-images without iphone flag
  26. 1.35 (2014-05-27) warnings, bugfixes, TGA optimization, etc
  27. 1.34 (unknown ) warning fix
  28. 1.33 (2011-07-14) minor fixes suggested by Dave Moore
  29. See end of file for full revision history.
  30. TODO:
  31. stbi_info support for BMP,PSD,HDR,PIC
  32. ============================ Contributors =========================
  33. Image formats Bug fixes & warning fixes
  34. Sean Barrett (jpeg, png, bmp) Marc LeBlanc
  35. Nicolas Schulz (hdr, psd) Christpher Lloyd
  36. Jonathan Dummer (tga) Dave Moore
  37. Jean-Marc Lienher (gif) Won Chun
  38. Tom Seddon (pic) the Horde3D community
  39. Thatcher Ulrich (psd) Janez Zemva
  40. Jonathan Blow
  41. Laurent Gomila
  42. Extensions, features Aruelien Pocheville
  43. Jetro Lauha (stbi_info) Ryamond Barbiero
  44. James "moose2000" Brown (iPhone PNG) David Woo
  45. Ben "Disch" Wenger (io callbacks) Roy Eltham
  46. Martin "SpartanJ" Golini Luke Graham
  47. Thomas Ruf
  48. John Bartholomew
  49. Optimizations & bugfixes Ken Hamada
  50. Fabian "ryg" Giesen Cort Stratton
  51. Arseny Kapoulkine Blazej Dariusz Roszkowski
  52. Thibault Reuille
  53. If your name should be here but Paul Du Bois
  54. isn't, let Sean know. Guillaume George
  55. Jerry Jansson
  56. Hayaki Saito
  57. */
  58. #ifndef STBI_INCLUDE_STB_IMAGE_H
  59. #define STBI_INCLUDE_STB_IMAGE_H
  60. // Limitations:
  61. // - no jpeg progressive support
  62. // - non-HDR formats support 8-bit samples only (jpeg, png)
  63. // - no delayed line count (jpeg) -- IJG doesn't support either
  64. // - no 1-bit BMP
  65. // - GIF always returns *comp=4
  66. //
  67. // Basic usage (see HDR discussion below):
  68. // int x,y,n;
  69. // unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
  70. // // ... process data if not NULL ...
  71. // // ... x = width, y = height, n = # 8-bit components per pixel ...
  72. // // ... replace '0' with '1'..'4' to force that many components per pixel
  73. // // ... but 'n' will always be the number that it would have been if you said 0
  74. // stbi_image_free(data)
  75. //
  76. // Standard parameters:
  77. // int *x -- outputs image width in pixels
  78. // int *y -- outputs image height in pixels
  79. // int *comp -- outputs # of image components in image file
  80. // int req_comp -- if non-zero, # of image components requested in result
  81. //
  82. // The return value from an image loader is an 'unsigned char *' which points
  83. // to the pixel data. The pixel data consists of *y scanlines of *x pixels,
  84. // with each pixel consisting of N interleaved 8-bit components; the first
  85. // pixel pointed to is top-left-most in the image. There is no padding between
  86. // image scanlines or between pixels, regardless of format. The number of
  87. // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
  88. // If req_comp is non-zero, *comp has the number of components that _would_
  89. // have been output otherwise. E.g. if you set req_comp to 4, you will always
  90. // get RGBA output, but you can check *comp to easily see if it's opaque.
  91. //
  92. // An output image with N components has the following components interleaved
  93. // in this order in each pixel:
  94. //
  95. // N=#comp components
  96. // 1 grey
  97. // 2 grey, alpha
  98. // 3 red, green, blue
  99. // 4 red, green, blue, alpha
  100. //
  101. // If image loading fails for any reason, the return value will be NULL,
  102. // and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
  103. // can be queried for an extremely brief, end-user unfriendly explanation
  104. // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
  105. // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
  106. // more user-friendly ones.
  107. //
  108. // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
  109. //
  110. // ===========================================================================
  111. //
  112. // iPhone PNG support:
  113. //
  114. // By default we convert iphone-formatted PNGs back to RGB; nominally they
  115. // would silently load as BGR, except the existing code should have just
  116. // failed on such iPhone PNGs. But you can disable this conversion by
  117. // by calling stbi_convert_iphone_png_to_rgb(0), in which case
  118. // you will always just get the native iphone "format" through.
  119. //
  120. // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
  121. // pixel to remove any premultiplied alpha *only* if the image file explicitly
  122. // says there's premultiplied data (currently only happens in iPhone images,
  123. // and only if iPhone convert-to-rgb processing is on).
  124. //
  125. // ===========================================================================
  126. //
  127. // HDR image support (disable by defining STBI_NO_HDR)
  128. //
  129. // stb_image now supports loading HDR images in general, and currently
  130. // the Radiance .HDR file format, although the support is provided
  131. // generically. You can still load any file through the existing interface;
  132. // if you attempt to load an HDR file, it will be automatically remapped to
  133. // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
  134. // both of these constants can be reconfigured through this interface:
  135. //
  136. // stbi_hdr_to_ldr_gamma(2.2f);
  137. // stbi_hdr_to_ldr_scale(1.0f);
  138. //
  139. // (note, do not use _inverse_ constants; stbi_image will invert them
  140. // appropriately).
  141. //
  142. // Additionally, there is a new, parallel interface for loading files as
  143. // (linear) floats to preserve the full dynamic range:
  144. //
  145. // float *data = stbi_loadf(filename, &x, &y, &n, 0);
  146. //
  147. // If you load LDR images through this interface, those images will
  148. // be promoted to floating point values, run through the inverse of
  149. // constants corresponding to the above:
  150. //
  151. // stbi_ldr_to_hdr_scale(1.0f);
  152. // stbi_ldr_to_hdr_gamma(2.2f);
  153. //
  154. // Finally, given a filename (or an open file or memory block--see header
  155. // file for details) containing image data, you can query for the "most
  156. // appropriate" interface to use (that is, whether the image is HDR or
  157. // not), using:
  158. //
  159. // stbi_is_hdr(char *filename);
  160. //
  161. // ===========================================================================
  162. //
  163. // I/O callbacks
  164. //
  165. // I/O callbacks allow you to read from arbitrary sources, like packaged
  166. // files or some other source. Data read from callbacks are processed
  167. // through a small internal buffer (currently 128 bytes) to try to reduce
  168. // overhead.
  169. //
  170. // The three functions you must define are "read" (reads some bytes of data),
  171. // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
  172. #ifndef STBI_NO_STDIO
  173. #if defined(_MSC_VER) && _MSC_VER >= 1400
  174. #define _CRT_SECURE_NO_WARNINGS // suppress warnings about fopen()
  175. #pragma warning(push)
  176. #pragma warning(disable:4996) // suppress even more warnings about fopen()
  177. #endif
  178. #include <stdio.h>
  179. #endif // STBI_NO_STDIO
  180. #define STBI_VERSION 1
  181. enum
  182. {
  183. STBI_default = 0, // only used for req_comp
  184. STBI_grey = 1,
  185. STBI_grey_alpha = 2,
  186. STBI_rgb = 3,
  187. STBI_rgb_alpha = 4
  188. };
  189. typedef unsigned char stbi_uc;
  190. #ifdef __cplusplus
  191. extern "C" {
  192. #endif
  193. #ifdef STB_IMAGE_STATIC
  194. #define STBIDEF static
  195. #else
  196. #define STBIDEF extern
  197. #endif
  198. //////////////////////////////////////////////////////////////////////////////
  199. //
  200. // PRIMARY API - works on images of any type
  201. //
  202. //
  203. // load image by filename, open file, or memory buffer
  204. //
  205. STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  206. #ifndef STBI_NO_STDIO
  207. STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  208. STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  209. // for stbi_load_from_file, file pointer is left pointing immediately after image
  210. #endif
  211. typedef struct
  212. {
  213. int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
  214. void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
  215. int (*eof) (void *user); // returns nonzero if we are at end of file/data
  216. } stbi_io_callbacks;
  217. STBIDEF stbi_uc *stbi_load_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
  218. #ifndef STBI_NO_HDR
  219. STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  220. #ifndef STBI_NO_STDIO
  221. STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
  222. STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  223. #endif
  224. STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
  225. STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);
  226. STBIDEF void stbi_hdr_to_ldr_scale(float scale);
  227. STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);
  228. STBIDEF void stbi_ldr_to_hdr_scale(float scale);
  229. #endif // STBI_NO_HDR
  230. // stbi_is_hdr is always defined
  231. STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
  232. STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
  233. #ifndef STBI_NO_STDIO
  234. STBIDEF int stbi_is_hdr (char const *filename);
  235. STBIDEF int stbi_is_hdr_from_file(FILE *f);
  236. #endif // STBI_NO_STDIO
  237. // get a VERY brief reason for failure
  238. // NOT THREADSAFE
  239. STBIDEF const char *stbi_failure_reason (void);
  240. // free the loaded image -- this is just free()
  241. STBIDEF void stbi_image_free (void *retval_from_stbi_load);
  242. // get image dimensions & components without fully decoding
  243. STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  244. STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
  245. #ifndef STBI_NO_STDIO
  246. STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);
  247. STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
  248. #endif
  249. // for image formats that explicitly notate that they have premultiplied alpha,
  250. // we just return the colors as stored in the file. set this flag to force
  251. // unpremultiplication. results are undefined if the unpremultiply overflow.
  252. STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
  253. // indicate whether we should process iphone images back to canonical format,
  254. // or just pass them through "as-is"
  255. STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
  256. // ZLIB client - used by PNG, available for other purposes
  257. STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
  258. STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
  259. STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
  260. STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  261. STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
  262. STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  263. // define faster low-level operations (typically SIMD support)
  264. #ifdef STBI_SIMD
  265. typedef void (*stbi_idct_8x8)(stbi_uc *out, int out_stride, short data[64], unsigned short *dequantize);
  266. // compute an integer IDCT on "input"
  267. // input[x] = data[x] * dequantize[x]
  268. // write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
  269. // CLAMP results to 0..255
  270. 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);
  271. // compute a conversion from YCbCr to RGB
  272. // 'count' pixels
  273. // write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
  274. // y: Y input channel
  275. // cb: Cb input channel; scale/biased to be 0..255
  276. // cr: Cr input channel; scale/biased to be 0..255
  277. STBIDEF void stbi_install_idct(stbi_idct_8x8 func);
  278. STBIDEF void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
  279. #endif // STBI_SIMD
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. //
  284. //
  285. //// end header file /////////////////////////////////////////////////////
  286. #endif // STBI_INCLUDE_STB_IMAGE_H
  287. #ifdef STB_IMAGE_IMPLEMENTATION
  288. #ifndef STBI_NO_HDR
  289. #include <math.h> // ldexp
  290. #include <string.h> // strcmp, strtok
  291. #endif
  292. #ifndef STBI_NO_STDIO
  293. #include <stdio.h>
  294. #endif
  295. #include <stdlib.h>
  296. #include <memory.h>
  297. #include <assert.h>
  298. #include <stdarg.h>
  299. #include <stddef.h> // ptrdiff_t on osx
  300. #ifndef _MSC_VER
  301. #ifdef __cplusplus
  302. #define stbi_inline inline
  303. #else
  304. #define stbi_inline
  305. #endif
  306. #else
  307. #define stbi_inline __forceinline
  308. #endif
  309. #ifdef _MSC_VER
  310. typedef unsigned short stbi__uint16;
  311. typedef signed short stbi__int16;
  312. typedef unsigned int stbi__uint32;
  313. typedef signed int stbi__int32;
  314. #else
  315. #include <stdint.h>
  316. typedef uint16_t stbi__uint16;
  317. typedef int16_t stbi__int16;
  318. typedef uint32_t stbi__uint32;
  319. typedef int32_t stbi__int32;
  320. #endif
  321. // should produce compiler error if size is wrong
  322. typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
  323. #ifdef _MSC_VER
  324. #define STBI_NOTUSED(v) (void)(v)
  325. #else
  326. #define STBI_NOTUSED(v) (void)sizeof(v)
  327. #endif
  328. #ifdef _MSC_VER
  329. #define STBI_HAS_LROTL
  330. #endif
  331. #ifdef STBI_HAS_LROTL
  332. #define stbi_lrot(x,y) _lrotl(x,y)
  333. #else
  334. #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
  335. #endif
  336. ///////////////////////////////////////////////
  337. //
  338. // stbi__context struct and start_xxx functions
  339. // stbi__context structure is our basic context used by all images, so it
  340. // contains all the IO context, plus some basic image information
  341. typedef struct
  342. {
  343. stbi__uint32 img_x, img_y;
  344. int img_n, img_out_n;
  345. stbi_io_callbacks io;
  346. void *io_user_data;
  347. int read_from_callbacks;
  348. int buflen;
  349. stbi_uc buffer_start[128];
  350. stbi_uc *img_buffer, *img_buffer_end;
  351. stbi_uc *img_buffer_original;
  352. } stbi__context;
  353. static void stbi__refill_buffer(stbi__context *s);
  354. // initialize a memory-stbi__jpeg_huff_decode context
  355. static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)
  356. {
  357. s->io.read = NULL;
  358. s->read_from_callbacks = 0;
  359. s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;
  360. s->img_buffer_end = (stbi_uc *) buffer+len;
  361. }
  362. // initialize a callback-based context
  363. static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user)
  364. {
  365. s->io = *c;
  366. s->io_user_data = user;
  367. s->buflen = sizeof(s->buffer_start);
  368. s->read_from_callbacks = 1;
  369. s->img_buffer_original = s->buffer_start;
  370. stbi__refill_buffer(s);
  371. }
  372. #ifndef STBI_NO_STDIO
  373. static int stbi__stdio_read(void *user, char *data, int size)
  374. {
  375. return (int) fread(data,1,size,(FILE*) user);
  376. }
  377. static void stbi__stdio_skip(void *user, int n)
  378. {
  379. fseek((FILE*) user, n, SEEK_CUR);
  380. }
  381. static int stbi__stdio_eof(void *user)
  382. {
  383. return feof((FILE*) user);
  384. }
  385. static stbi_io_callbacks stbi__stdio_callbacks =
  386. {
  387. stbi__stdio_read,
  388. stbi__stdio_skip,
  389. stbi__stdio_eof,
  390. };
  391. static void stbi__start_file(stbi__context *s, FILE *f)
  392. {
  393. stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);
  394. }
  395. //static void stop_file(stbi__context *s) { }
  396. #endif // !STBI_NO_STDIO
  397. static void stbi__rewind(stbi__context *s)
  398. {
  399. // conceptually rewind SHOULD rewind to the beginning of the stream,
  400. // but we just rewind to the beginning of the initial buffer, because
  401. // we only use it after doing 'test', which only ever looks at at most 92 bytes
  402. s->img_buffer = s->img_buffer_original;
  403. }
  404. static int stbi__jpeg_test(stbi__context *s);
  405. static stbi_uc *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  406. static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp);
  407. static int stbi__png_test(stbi__context *s);
  408. static stbi_uc *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  409. static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);
  410. static int stbi__bmp_test(stbi__context *s);
  411. static stbi_uc *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  412. static int stbi__tga_test(stbi__context *s);
  413. static stbi_uc *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  414. static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp);
  415. static int stbi__psd_test(stbi__context *s);
  416. static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  417. #ifndef STBI_NO_HDR
  418. static int stbi__hdr_test(stbi__context *s);
  419. static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  420. #endif
  421. static int stbi__pic_test(stbi__context *s);
  422. static stbi_uc *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  423. static int stbi__gif_test(stbi__context *s);
  424. static stbi_uc *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  425. static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);
  426. // this is not threadsafe
  427. static const char *stbi__g_failure_reason;
  428. STBIDEF const char *stbi_failure_reason(void)
  429. {
  430. return stbi__g_failure_reason;
  431. }
  432. static int stbi__err(const char *str)
  433. {
  434. stbi__g_failure_reason = str;
  435. return 0;
  436. }
  437. // stbi__err - error
  438. // stbi__errpf - error returning pointer to float
  439. // stbi__errpuc - error returning pointer to unsigned char
  440. #ifdef STBI_NO_FAILURE_STRINGS
  441. #define stbi__err(x,y) 0
  442. #elif defined(STBI_FAILURE_USERMSG)
  443. #define stbi__err(x,y) stbi__err(y)
  444. #else
  445. #define stbi__err(x,y) stbi__err(x)
  446. #endif
  447. #define stbi__errpf(x,y) ((float *) (stbi__err(x,y)?NULL:NULL))
  448. #define stbi__errpuc(x,y) ((unsigned char *) (stbi__err(x,y)?NULL:NULL))
  449. STBIDEF void stbi_image_free(void *retval_from_stbi_load)
  450. {
  451. free(retval_from_stbi_load);
  452. }
  453. #ifndef STBI_NO_HDR
  454. static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
  455. static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);
  456. #endif
  457. static unsigned char *stbi_load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  458. {
  459. if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp);
  460. if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp);
  461. if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp);
  462. if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp);
  463. if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp);
  464. if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp);
  465. #ifndef STBI_NO_HDR
  466. if (stbi__hdr_test(s)) {
  467. float *hdr = stbi__hdr_load(s, x,y,comp,req_comp);
  468. return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
  469. }
  470. #endif
  471. // test tga last because it's a crappy test!
  472. if (stbi__tga_test(s))
  473. return stbi__tga_load(s,x,y,comp,req_comp);
  474. return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt");
  475. }
  476. #ifndef STBI_NO_STDIO
  477. STBIDEF unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  478. {
  479. FILE *f = fopen(filename, "rb");
  480. unsigned char *result;
  481. if (!f) return stbi__errpuc("can't fopen", "Unable to open file");
  482. result = stbi_load_from_file(f,x,y,comp,req_comp);
  483. fclose(f);
  484. return result;
  485. }
  486. STBIDEF unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  487. {
  488. unsigned char *result;
  489. stbi__context s;
  490. stbi__start_file(&s,f);
  491. result = stbi_load_main(&s,x,y,comp,req_comp);
  492. if (result) {
  493. // need to 'unget' all the characters in the IO buffer
  494. fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
  495. }
  496. return result;
  497. }
  498. #endif //!STBI_NO_STDIO
  499. STBIDEF unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  500. {
  501. stbi__context s;
  502. stbi__start_mem(&s,buffer,len);
  503. return stbi_load_main(&s,x,y,comp,req_comp);
  504. }
  505. unsigned char *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
  506. {
  507. stbi__context s;
  508. stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
  509. return stbi_load_main(&s,x,y,comp,req_comp);
  510. }
  511. #ifndef STBI_NO_HDR
  512. float *stbi_loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  513. {
  514. unsigned char *data;
  515. #ifndef STBI_NO_HDR
  516. if (stbi__hdr_test(s))
  517. return stbi__hdr_load(s,x,y,comp,req_comp);
  518. #endif
  519. data = stbi_load_main(s, x, y, comp, req_comp);
  520. if (data)
  521. return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
  522. return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");
  523. }
  524. float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  525. {
  526. stbi__context s;
  527. stbi__start_mem(&s,buffer,len);
  528. return stbi_loadf_main(&s,x,y,comp,req_comp);
  529. }
  530. float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
  531. {
  532. stbi__context s;
  533. stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
  534. return stbi_loadf_main(&s,x,y,comp,req_comp);
  535. }
  536. #ifndef STBI_NO_STDIO
  537. float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
  538. {
  539. FILE *f = fopen(filename, "rb");
  540. float *result;
  541. if (!f) return stbi__errpf("can't fopen", "Unable to open file");
  542. result = stbi_loadf_from_file(f,x,y,comp,req_comp);
  543. fclose(f);
  544. return result;
  545. }
  546. float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  547. {
  548. stbi__context s;
  549. stbi__start_file(&s,f);
  550. return stbi_loadf_main(&s,x,y,comp,req_comp);
  551. }
  552. #endif // !STBI_NO_STDIO
  553. #endif // !STBI_NO_HDR
  554. // these is-hdr-or-not is defined independent of whether STBI_NO_HDR is
  555. // defined, for API simplicity; if STBI_NO_HDR is defined, it always
  556. // reports false!
  557. int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
  558. {
  559. #ifndef STBI_NO_HDR
  560. stbi__context s;
  561. stbi__start_mem(&s,buffer,len);
  562. return stbi__hdr_test(&s);
  563. #else
  564. STBI_NOTUSED(buffer);
  565. STBI_NOTUSED(len);
  566. return 0;
  567. #endif
  568. }
  569. #ifndef STBI_NO_STDIO
  570. STBIDEF int stbi_is_hdr (char const *filename)
  571. {
  572. FILE *f = fopen(filename, "rb");
  573. int result=0;
  574. if (f) {
  575. result = stbi_is_hdr_from_file(f);
  576. fclose(f);
  577. }
  578. return result;
  579. }
  580. STBIDEF int stbi_is_hdr_from_file(FILE *f)
  581. {
  582. #ifndef STBI_NO_HDR
  583. stbi__context s;
  584. stbi__start_file(&s,f);
  585. return stbi__hdr_test(&s);
  586. #else
  587. return 0;
  588. #endif
  589. }
  590. #endif // !STBI_NO_STDIO
  591. STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
  592. {
  593. #ifndef STBI_NO_HDR
  594. stbi__context s;
  595. stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
  596. return stbi__hdr_test(&s);
  597. #else
  598. return 0;
  599. #endif
  600. }
  601. #ifndef STBI_NO_HDR
  602. static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;
  603. static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;
  604. void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; }
  605. void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; }
  606. void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; }
  607. void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; }
  608. #endif
  609. //////////////////////////////////////////////////////////////////////////////
  610. //
  611. // Common code used by all image loaders
  612. //
  613. enum
  614. {
  615. SCAN_load=0,
  616. SCAN_type,
  617. SCAN_header
  618. };
  619. static void stbi__refill_buffer(stbi__context *s)
  620. {
  621. int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
  622. if (n == 0) {
  623. // at end of file, treat same as if from memory, but need to handle case
  624. // where s->img_buffer isn't pointing to safe memory, stbi__err.g. 0-byte file
  625. s->read_from_callbacks = 0;
  626. s->img_buffer = s->buffer_start;
  627. s->img_buffer_end = s->buffer_start+1;
  628. *s->img_buffer = 0;
  629. } else {
  630. s->img_buffer = s->buffer_start;
  631. s->img_buffer_end = s->buffer_start + n;
  632. }
  633. }
  634. stbi_inline static stbi_uc stbi__get8(stbi__context *s)
  635. {
  636. if (s->img_buffer < s->img_buffer_end)
  637. return *s->img_buffer++;
  638. if (s->read_from_callbacks) {
  639. stbi__refill_buffer(s);
  640. return *s->img_buffer++;
  641. }
  642. return 0;
  643. }
  644. stbi_inline static int stbi__at_eof(stbi__context *s)
  645. {
  646. if (s->io.read) {
  647. if (!(s->io.eof)(s->io_user_data)) return 0;
  648. // if feof() is true, check if buffer = end
  649. // special case: we've only got the special 0 character at the end
  650. if (s->read_from_callbacks == 0) return 1;
  651. }
  652. return s->img_buffer >= s->img_buffer_end;
  653. }
  654. static void stbi__skip(stbi__context *s, int n)
  655. {
  656. if (s->io.read) {
  657. int blen = (int) (s->img_buffer_end - s->img_buffer);
  658. if (blen < n) {
  659. s->img_buffer = s->img_buffer_end;
  660. (s->io.skip)(s->io_user_data, n - blen);
  661. return;
  662. }
  663. }
  664. s->img_buffer += n;
  665. }
  666. static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
  667. {
  668. if (s->io.read) {
  669. int blen = (int) (s->img_buffer_end - s->img_buffer);
  670. if (blen < n) {
  671. int res, count;
  672. memcpy(buffer, s->img_buffer, blen);
  673. count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);
  674. res = (count == (n-blen));
  675. s->img_buffer = s->img_buffer_end;
  676. return res;
  677. }
  678. }
  679. if (s->img_buffer+n <= s->img_buffer_end) {
  680. memcpy(buffer, s->img_buffer, n);
  681. s->img_buffer += n;
  682. return 1;
  683. } else
  684. return 0;
  685. }
  686. static int stbi__get16be(stbi__context *s)
  687. {
  688. int z = stbi__get8(s);
  689. return (z << 8) + stbi__get8(s);
  690. }
  691. static stbi__uint32 stbi__get32be(stbi__context *s)
  692. {
  693. stbi__uint32 z = stbi__get16be(s);
  694. return (z << 16) + stbi__get16be(s);
  695. }
  696. static int stbi__get16le(stbi__context *s)
  697. {
  698. int z = stbi__get8(s);
  699. return z + (stbi__get8(s) << 8);
  700. }
  701. static stbi__uint32 stbi__get32le(stbi__context *s)
  702. {
  703. stbi__uint32 z = stbi__get16le(s);
  704. return z + (stbi__get16le(s) << 16);
  705. }
  706. //////////////////////////////////////////////////////////////////////////////
  707. //
  708. // generic converter from built-in img_n to req_comp
  709. // individual types do this automatically as much as possible (stbi__err.g. jpeg
  710. // does all cases internally since it needs to colorspace convert anyway,
  711. // and it never has alpha, so very few cases ). png can automatically
  712. // interleave an alpha=255 channel, but falls back to this for other cases
  713. //
  714. // assume data buffer is malloced, so malloc a new one and free that one
  715. // only failure mode is malloc failing
  716. static stbi_uc stbi__compute_y(int r, int g, int b)
  717. {
  718. return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);
  719. }
  720. static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
  721. {
  722. int i,j;
  723. unsigned char *good;
  724. if (req_comp == img_n) return data;
  725. assert(req_comp >= 1 && req_comp <= 4);
  726. good = (unsigned char *) malloc(req_comp * x * y);
  727. if (good == NULL) {
  728. free(data);
  729. return stbi__errpuc("outofmem", "Out of memory");
  730. }
  731. for (j=0; j < (int) y; ++j) {
  732. unsigned char *src = data + j * x * img_n ;
  733. unsigned char *dest = good + j * x * req_comp;
  734. #define COMBO(a,b) ((a)*8+(b))
  735. #define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
  736. // convert source image with img_n components to one with req_comp components;
  737. // avoid switch per pixel, so use switch per scanline and massive macros
  738. switch (COMBO(img_n, req_comp)) {
  739. CASE(1,2) dest[0]=src[0], dest[1]=255; break;
  740. CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;
  741. CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;
  742. CASE(2,1) dest[0]=src[0]; break;
  743. CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;
  744. CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;
  745. CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;
  746. CASE(3,1) dest[0]=stbi__compute_y(src[0],src[1],src[2]); break;
  747. CASE(3,2) dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
  748. CASE(4,1) dest[0]=stbi__compute_y(src[0],src[1],src[2]); break;
  749. CASE(4,2) dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;
  750. CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;
  751. default: assert(0);
  752. }
  753. #undef CASE
  754. }
  755. free(data);
  756. return good;
  757. }
  758. #ifndef STBI_NO_HDR
  759. static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
  760. {
  761. int i,k,n;
  762. float *output = (float *) malloc(x * y * comp * sizeof(float));
  763. if (output == NULL) { free(data); return stbi__errpf("outofmem", "Out of memory"); }
  764. // compute number of non-alpha components
  765. if (comp & 1) n = comp; else n = comp-1;
  766. for (i=0; i < x*y; ++i) {
  767. for (k=0; k < n; ++k) {
  768. output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
  769. }
  770. if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
  771. }
  772. free(data);
  773. return output;
  774. }
  775. #define stbi__float2int(x) ((int) (x))
  776. static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)
  777. {
  778. int i,k,n;
  779. stbi_uc *output = (stbi_uc *) malloc(x * y * comp);
  780. if (output == NULL) { free(data); return stbi__errpuc("outofmem", "Out of memory"); }
  781. // compute number of non-alpha components
  782. if (comp & 1) n = comp; else n = comp-1;
  783. for (i=0; i < x*y; ++i) {
  784. for (k=0; k < n; ++k) {
  785. float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;
  786. if (z < 0) z = 0;
  787. if (z > 255) z = 255;
  788. output[i*comp + k] = (stbi_uc) stbi__float2int(z);
  789. }
  790. if (k < comp) {
  791. float z = data[i*comp+k] * 255 + 0.5f;
  792. if (z < 0) z = 0;
  793. if (z > 255) z = 255;
  794. output[i*comp + k] = (stbi_uc) stbi__float2int(z);
  795. }
  796. }
  797. free(data);
  798. return output;
  799. }
  800. #endif
  801. //////////////////////////////////////////////////////////////////////////////
  802. //
  803. // "baseline" JPEG/JFIF decoder (not actually fully baseline implementation)
  804. //
  805. // simple implementation
  806. // - channel subsampling of at most 2 in each dimension
  807. // - doesn't support delayed output of y-dimension
  808. // - simple interface (only one output format: 8-bit interleaved RGB)
  809. // - doesn't try to recover corrupt jpegs
  810. // - doesn't allow partial loading, loading multiple at once
  811. // - still fast on x86 (copying globals into locals doesn't help x86)
  812. // - allocates lots of intermediate memory (full size of all components)
  813. // - non-interleaved case requires this anyway
  814. // - allows good upsampling (see next)
  815. // high-quality
  816. // - upsampled channels are bilinearly interpolated, even across blocks
  817. // - quality integer IDCT derived from IJG's 'slow'
  818. // performance
  819. // - fast huffman; reasonable integer IDCT
  820. // - uses a lot of intermediate memory, could cache poorly
  821. // - load http://nothings.org/remote/anemones.jpg 3 times on 2.8Ghz P4
  822. // stb_jpeg: 1.34 seconds (MSVC6, default release build)
  823. // stb_jpeg: 1.06 seconds (MSVC6, processor = Pentium Pro)
  824. // IJL11.dll: 1.08 seconds (compiled by intel)
  825. // IJG 1998: 0.98 seconds (MSVC6, makefile provided by IJG)
  826. // IJG 1998: 0.95 seconds (MSVC6, makefile + proc=PPro)
  827. // huffman decoding acceleration
  828. #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
  829. typedef struct
  830. {
  831. stbi_uc fast[1 << FAST_BITS];
  832. // weirdly, repacking this into AoS is a 10% speed loss, instead of a win
  833. stbi__uint16 code[256];
  834. stbi_uc values[256];
  835. stbi_uc size[257];
  836. unsigned int maxcode[18];
  837. int delta[17]; // old 'firstsymbol' - old 'firstcode'
  838. } stbi__huffman;
  839. typedef struct
  840. {
  841. #ifdef STBI_SIMD
  842. unsigned short dequant2[4][64];
  843. #endif
  844. stbi__context *s;
  845. stbi__huffman huff_dc[4];
  846. stbi__huffman huff_ac[4];
  847. stbi_uc dequant[4][64];
  848. // sizes for components, interleaved MCUs
  849. int img_h_max, img_v_max;
  850. int img_mcu_x, img_mcu_y;
  851. int img_mcu_w, img_mcu_h;
  852. // definition of jpeg image component
  853. struct
  854. {
  855. int id;
  856. int h,v;
  857. int tq;
  858. int hd,ha;
  859. int dc_pred;
  860. int x,y,w2,h2;
  861. stbi_uc *data;
  862. void *raw_data;
  863. stbi_uc *linebuf;
  864. } img_comp[4];
  865. stbi__uint32 code_buffer; // jpeg entropy-coded buffer
  866. int code_bits; // number of valid bits
  867. unsigned char marker; // marker seen while filling entropy buffer
  868. int nomore; // flag if we saw a marker so must stop
  869. int scan_n, order[4];
  870. int restart_interval, todo;
  871. } stbi__jpeg;
  872. static int stbi__build_huffman(stbi__huffman *h, int *count)
  873. {
  874. int i,j,k=0,code;
  875. // build size list for each symbol (from JPEG spec)
  876. for (i=0; i < 16; ++i)
  877. for (j=0; j < count[i]; ++j)
  878. h->size[k++] = (stbi_uc) (i+1);
  879. h->size[k] = 0;
  880. // compute actual symbols (from jpeg spec)
  881. code = 0;
  882. k = 0;
  883. for(j=1; j <= 16; ++j) {
  884. // compute delta to add to code to compute symbol id
  885. h->delta[j] = k - code;
  886. if (h->size[k] == j) {
  887. while (h->size[k] == j)
  888. h->code[k++] = (stbi__uint16) (code++);
  889. if (code-1 >= (1 << j)) return stbi__err("bad code lengths","Corrupt JPEG");
  890. }
  891. // compute largest code + 1 for this size, preshifted as needed later
  892. h->maxcode[j] = code << (16-j);
  893. code <<= 1;
  894. }
  895. h->maxcode[j] = 0xffffffff;
  896. // build non-spec acceleration table; 255 is flag for not-accelerated
  897. memset(h->fast, 255, 1 << FAST_BITS);
  898. for (i=0; i < k; ++i) {
  899. int s = h->size[i];
  900. if (s <= FAST_BITS) {
  901. int c = h->code[i] << (FAST_BITS-s);
  902. int m = 1 << (FAST_BITS-s);
  903. for (j=0; j < m; ++j) {
  904. h->fast[c+j] = (stbi_uc) i;
  905. }
  906. }
  907. }
  908. return 1;
  909. }
  910. static void stbi__grow_buffer_unsafe(stbi__jpeg *j)
  911. {
  912. do {
  913. int b = j->nomore ? 0 : stbi__get8(j->s);
  914. if (b == 0xff) {
  915. int c = stbi__get8(j->s);
  916. if (c != 0) {
  917. j->marker = (unsigned char) c;
  918. j->nomore = 1;
  919. return;
  920. }
  921. }
  922. j->code_buffer |= b << (24 - j->code_bits);
  923. j->code_bits += 8;
  924. } while (j->code_bits <= 24);
  925. }
  926. // (1 << n) - 1
  927. static stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
  928. // stbi__jpeg_huff_decode a jpeg huffman value from the bitstream
  929. stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
  930. {
  931. unsigned int temp;
  932. int c,k;
  933. if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
  934. // look at the top FAST_BITS and determine what symbol ID it is,
  935. // if the code is <= FAST_BITS
  936. c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
  937. k = h->fast[c];
  938. if (k < 255) {
  939. int s = h->size[k];
  940. if (s > j->code_bits)
  941. return -1;
  942. j->code_buffer <<= s;
  943. j->code_bits -= s;
  944. return h->values[k];
  945. }
  946. // naive test is to shift the code_buffer down so k bits are
  947. // valid, then test against maxcode. To speed this up, we've
  948. // preshifted maxcode left so that it has (16-k) 0s at the
  949. // end; in other words, regardless of the number of bits, it
  950. // wants to be compared against something shifted to have 16;
  951. // that way we don't need to shift inside the loop.
  952. temp = j->code_buffer >> 16;
  953. for (k=FAST_BITS+1 ; ; ++k)
  954. if (temp < h->maxcode[k])
  955. break;
  956. if (k == 17) {
  957. // error! code not found
  958. j->code_bits -= 16;
  959. return -1;
  960. }
  961. if (k > j->code_bits)
  962. return -1;
  963. // convert the huffman code to the symbol id
  964. c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
  965. assert((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
  966. // convert the id to a symbol
  967. j->code_bits -= k;
  968. j->code_buffer <<= k;
  969. return h->values[c];
  970. }
  971. // combined JPEG 'receive' and JPEG 'extend', since baseline
  972. // always extends everything it receives.
  973. stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)
  974. {
  975. unsigned int m = 1 << (n-1);
  976. unsigned int k;
  977. if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
  978. #if 1
  979. k = stbi_lrot(j->code_buffer, n);
  980. j->code_buffer = k & ~stbi__bmask[n];
  981. k &= stbi__bmask[n];
  982. j->code_bits -= n;
  983. #else
  984. k = (j->code_buffer >> (32 - n)) & stbi__bmask[n];
  985. j->code_bits -= n;
  986. j->code_buffer <<= n;
  987. #endif
  988. // the following test is probably a random branch that won't
  989. // predict well. I tried to table accelerate it but failed.
  990. // maybe it's compiling as a conditional move?
  991. if (k < m)
  992. return (-1 << n) + k + 1;
  993. else
  994. return k;
  995. }
  996. // given a value that's at position X in the zigzag stream,
  997. // where does it appear in the 8x8 matrix coded as row-major?
  998. static stbi_uc stbi__jpeg_dezigzag[64+15] =
  999. {
  1000. 0, 1, 8, 16, 9, 2, 3, 10,
  1001. 17, 24, 32, 25, 18, 11, 4, 5,
  1002. 12, 19, 26, 33, 40, 48, 41, 34,
  1003. 27, 20, 13, 6, 7, 14, 21, 28,
  1004. 35, 42, 49, 56, 57, 50, 43, 36,
  1005. 29, 22, 15, 23, 30, 37, 44, 51,
  1006. 58, 59, 52, 45, 38, 31, 39, 46,
  1007. 53, 60, 61, 54, 47, 55, 62, 63,
  1008. // let corrupt input sample past end
  1009. 63, 63, 63, 63, 63, 63, 63, 63,
  1010. 63, 63, 63, 63, 63, 63, 63
  1011. };
  1012. // stbi__jpeg_huff_decode one 64-entry block--
  1013. static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, int b)
  1014. {
  1015. int diff,dc,k;
  1016. int t = stbi__jpeg_huff_decode(j, hdc);
  1017. if (t < 0) return stbi__err("bad huffman code","Corrupt JPEG");
  1018. // 0 all the ac values now so we can do it 32-bits at a time
  1019. memset(data,0,64*sizeof(data[0]));
  1020. diff = t ? stbi__extend_receive(j, t) : 0;
  1021. dc = j->img_comp[b].dc_pred + diff;
  1022. j->img_comp[b].dc_pred = dc;
  1023. data[0] = (short) dc;
  1024. // stbi__jpeg_huff_decode AC components, see JPEG spec
  1025. k = 1;
  1026. do {
  1027. int r,s;
  1028. int rs = stbi__jpeg_huff_decode(j, hac);
  1029. if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
  1030. s = rs & 15;
  1031. r = rs >> 4;
  1032. if (s == 0) {
  1033. if (rs != 0xf0) break; // end block
  1034. k += 16;
  1035. } else {
  1036. k += r;
  1037. // stbi__jpeg_huff_decode into unzigzag'd location
  1038. data[stbi__jpeg_dezigzag[k++]] = (short) stbi__extend_receive(j,s);
  1039. }
  1040. } while (k < 64);
  1041. return 1;
  1042. }
  1043. // take a -128..127 value and stbi__clamp it and convert to 0..255
  1044. stbi_inline static stbi_uc stbi__clamp(int x)
  1045. {
  1046. // trick to use a single test to catch both cases
  1047. if ((unsigned int) x > 255) {
  1048. if (x < 0) return 0;
  1049. if (x > 255) return 255;
  1050. }
  1051. return (stbi_uc) x;
  1052. }
  1053. #define stbi__f2f(x) (int) (((x) * 4096 + 0.5))
  1054. #define stbi__fsh(x) ((x) << 12)
  1055. // derived from jidctint -- DCT_ISLOW
  1056. #define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
  1057. int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
  1058. p2 = s2; \
  1059. p3 = s6; \
  1060. p1 = (p2+p3) * stbi__f2f(0.5411961f); \
  1061. t2 = p1 + p3*stbi__f2f(-1.847759065f); \
  1062. t3 = p1 + p2*stbi__f2f( 0.765366865f); \
  1063. p2 = s0; \
  1064. p3 = s4; \
  1065. t0 = stbi__fsh(p2+p3); \
  1066. t1 = stbi__fsh(p2-p3); \
  1067. x0 = t0+t3; \
  1068. x3 = t0-t3; \
  1069. x1 = t1+t2; \
  1070. x2 = t1-t2; \
  1071. t0 = s7; \
  1072. t1 = s5; \
  1073. t2 = s3; \
  1074. t3 = s1; \
  1075. p3 = t0+t2; \
  1076. p4 = t1+t3; \
  1077. p1 = t0+t3; \
  1078. p2 = t1+t2; \
  1079. p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
  1080. t0 = t0*stbi__f2f( 0.298631336f); \
  1081. t1 = t1*stbi__f2f( 2.053119869f); \
  1082. t2 = t2*stbi__f2f( 3.072711026f); \
  1083. t3 = t3*stbi__f2f( 1.501321110f); \
  1084. p1 = p5 + p1*stbi__f2f(-0.899976223f); \
  1085. p2 = p5 + p2*stbi__f2f(-2.562915447f); \
  1086. p3 = p3*stbi__f2f(-1.961570560f); \
  1087. p4 = p4*stbi__f2f(-0.390180644f); \
  1088. t3 += p1+p4; \
  1089. t2 += p2+p3; \
  1090. t1 += p2+p4; \
  1091. t0 += p1+p3;
  1092. #ifdef STBI_SIMD
  1093. typedef unsigned short stbi_dequantize_t;
  1094. #else
  1095. typedef stbi_uc stbi_dequantize_t;
  1096. #endif
  1097. // .344 seconds on 3*anemones.jpg
  1098. static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64], stbi_dequantize_t *dequantize)
  1099. {
  1100. int i,val[64],*v=val;
  1101. stbi_dequantize_t *dq = dequantize;
  1102. stbi_uc *o;
  1103. short *d = data;
  1104. // columns
  1105. for (i=0; i < 8; ++i,++d,++dq, ++v) {
  1106. // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
  1107. if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
  1108. && d[40]==0 && d[48]==0 && d[56]==0) {
  1109. // no shortcut 0 seconds
  1110. // (1|2|3|4|5|6|7)==0 0 seconds
  1111. // all separate -0.047 seconds
  1112. // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds
  1113. int dcterm = d[0] * dq[0] << 2;
  1114. v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
  1115. } else {
  1116. STBI__IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],
  1117. d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])
  1118. // constants scaled things up by 1<<12; let's bring them back
  1119. // down, but keep 2 extra bits of precision
  1120. x0 += 512; x1 += 512; x2 += 512; x3 += 512;
  1121. v[ 0] = (x0+t3) >> 10;
  1122. v[56] = (x0-t3) >> 10;
  1123. v[ 8] = (x1+t2) >> 10;
  1124. v[48] = (x1-t2) >> 10;
  1125. v[16] = (x2+t1) >> 10;
  1126. v[40] = (x2-t1) >> 10;
  1127. v[24] = (x3+t0) >> 10;
  1128. v[32] = (x3-t0) >> 10;
  1129. }
  1130. }
  1131. for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
  1132. // no fast case since the first 1D IDCT spread components out
  1133. STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
  1134. // constants scaled things up by 1<<12, plus we had 1<<2 from first
  1135. // loop, plus horizontal and vertical each scale by sqrt(8) so together
  1136. // we've got an extra 1<<3, so 1<<17 total we need to remove.
  1137. // so we want to round that, which means adding 0.5 * 1<<17,
  1138. // aka 65536. Also, we'll end up with -128 to 127 that we want
  1139. // to encode as 0..255 by adding 128, so we'll add that before the shift
  1140. x0 += 65536 + (128<<17);
  1141. x1 += 65536 + (128<<17);
  1142. x2 += 65536 + (128<<17);
  1143. x3 += 65536 + (128<<17);
  1144. // tried computing the shifts into temps, or'ing the temps to see
  1145. // if any were out of range, but that was slower
  1146. o[0] = stbi__clamp((x0+t3) >> 17);
  1147. o[7] = stbi__clamp((x0-t3) >> 17);
  1148. o[1] = stbi__clamp((x1+t2) >> 17);
  1149. o[6] = stbi__clamp((x1-t2) >> 17);
  1150. o[2] = stbi__clamp((x2+t1) >> 17);
  1151. o[5] = stbi__clamp((x2-t1) >> 17);
  1152. o[3] = stbi__clamp((x3+t0) >> 17);
  1153. o[4] = stbi__clamp((x3-t0) >> 17);
  1154. }
  1155. }
  1156. #ifdef STBI_SIMD
  1157. static stbi_idct_8x8 stbi__idct_installed = stbi__idct_block;
  1158. STBIDEF void stbi_install_idct(stbi_idct_8x8 func)
  1159. {
  1160. stbi__idct_installed = func;
  1161. }
  1162. #endif
  1163. #define STBI__MARKER_none 0xff
  1164. // if there's a pending marker from the entropy stream, return that
  1165. // otherwise, fetch from the stream and get a marker. if there's no
  1166. // marker, return 0xff, which is never a valid marker value
  1167. static stbi_uc stbi__get_marker(stbi__jpeg *j)
  1168. {
  1169. stbi_uc x;
  1170. if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; }
  1171. x = stbi__get8(j->s);
  1172. if (x != 0xff) return STBI__MARKER_none;
  1173. while (x == 0xff)
  1174. x = stbi__get8(j->s);
  1175. return x;
  1176. }
  1177. // in each scan, we'll have scan_n components, and the order
  1178. // of the components is specified by order[]
  1179. #define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
  1180. // after a restart interval, stbi__jpeg_reset the entropy decoder and
  1181. // the dc prediction
  1182. static void stbi__jpeg_reset(stbi__jpeg *j)
  1183. {
  1184. j->code_bits = 0;
  1185. j->code_buffer = 0;
  1186. j->nomore = 0;
  1187. j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
  1188. j->marker = STBI__MARKER_none;
  1189. j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
  1190. // no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
  1191. // since we don't even allow 1<<30 pixels
  1192. }
  1193. static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
  1194. {
  1195. stbi__jpeg_reset(z);
  1196. if (z->scan_n == 1) {
  1197. int i,j;
  1198. #ifdef STBI_SIMD
  1199. __declspec(align(16))
  1200. #endif
  1201. short data[64];
  1202. int n = z->order[0];
  1203. // non-interleaved data, we just need to process one block at a time,
  1204. // in trivial scanline order
  1205. // number of blocks to do just depends on how many actual "pixels" this
  1206. // component has, independent of interleaved MCU blocking and such
  1207. int w = (z->img_comp[n].x+7) >> 3;
  1208. int h = (z->img_comp[n].y+7) >> 3;
  1209. for (j=0; j < h; ++j) {
  1210. for (i=0; i < w; ++i) {
  1211. if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
  1212. #ifdef STBI_SIMD
  1213. stbi__idct_installed(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
  1214. #else
  1215. stbi__idct_block(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
  1216. #endif
  1217. // every data block is an MCU, so countdown the restart interval
  1218. if (--z->todo <= 0) {
  1219. if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
  1220. // if it's NOT a restart, then just bail, so we get corrupt data
  1221. // rather than no data
  1222. if (!STBI__RESTART(z->marker)) return 1;
  1223. stbi__jpeg_reset(z);
  1224. }
  1225. }
  1226. }
  1227. } else { // interleaved!
  1228. int i,j,k,x,y;
  1229. short data[64];
  1230. for (j=0; j < z->img_mcu_y; ++j) {
  1231. for (i=0; i < z->img_mcu_x; ++i) {
  1232. // scan an interleaved mcu... process scan_n components in order
  1233. for (k=0; k < z->scan_n; ++k) {
  1234. int n = z->order[k];
  1235. // scan out an mcu's worth of this component; that's just determined
  1236. // by the basic H and V specified for the component
  1237. for (y=0; y < z->img_comp[n].v; ++y) {
  1238. for (x=0; x < z->img_comp[n].h; ++x) {
  1239. int x2 = (i*z->img_comp[n].h + x)*8;
  1240. int y2 = (j*z->img_comp[n].v + y)*8;
  1241. if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
  1242. #ifdef STBI_SIMD
  1243. stbi__idct_installed(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
  1244. #else
  1245. stbi__idct_block(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
  1246. #endif
  1247. }
  1248. }
  1249. }
  1250. // after all interleaved components, that's an interleaved MCU,
  1251. // so now count down the restart interval
  1252. if (--z->todo <= 0) {
  1253. if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
  1254. // if it's NOT a restart, then just bail, so we get corrupt data
  1255. // rather than no data
  1256. if (!STBI__RESTART(z->marker)) return 1;
  1257. stbi__jpeg_reset(z);
  1258. }
  1259. }
  1260. }
  1261. }
  1262. return 1;
  1263. }
  1264. static int stbi__process_marker(stbi__jpeg *z, int m)
  1265. {
  1266. int L;
  1267. switch (m) {
  1268. case STBI__MARKER_none: // no marker found
  1269. return stbi__err("expected marker","Corrupt JPEG");
  1270. case 0xC2: // stbi__SOF - progressive
  1271. return stbi__err("progressive jpeg","JPEG format not supported (progressive)");
  1272. case 0xDD: // DRI - specify restart interval
  1273. if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG");
  1274. z->restart_interval = stbi__get16be(z->s);
  1275. return 1;
  1276. case 0xDB: // DQT - define quantization table
  1277. L = stbi__get16be(z->s)-2;
  1278. while (L > 0) {
  1279. int q = stbi__get8(z->s);
  1280. int p = q >> 4;
  1281. int t = q & 15,i;
  1282. if (p != 0) return stbi__err("bad DQT type","Corrupt JPEG");
  1283. if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG");
  1284. for (i=0; i < 64; ++i)
  1285. z->dequant[t][stbi__jpeg_dezigzag[i]] = stbi__get8(z->s);
  1286. #ifdef STBI_SIMD
  1287. for (i=0; i < 64; ++i)
  1288. z->dequant2[t][i] = z->dequant[t][i];
  1289. #endif
  1290. L -= 65;
  1291. }
  1292. return L==0;
  1293. case 0xC4: // DHT - define huffman table
  1294. L = stbi__get16be(z->s)-2;
  1295. while (L > 0) {
  1296. stbi_uc *v;
  1297. int sizes[16],i,n=0;
  1298. int q = stbi__get8(z->s);
  1299. int tc = q >> 4;
  1300. int th = q & 15;
  1301. if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG");
  1302. for (i=0; i < 16; ++i) {
  1303. sizes[i] = stbi__get8(z->s);
  1304. n += sizes[i];
  1305. }
  1306. L -= 17;
  1307. if (tc == 0) {
  1308. if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0;
  1309. v = z->huff_dc[th].values;
  1310. } else {
  1311. if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0;
  1312. v = z->huff_ac[th].values;
  1313. }
  1314. for (i=0; i < n; ++i)
  1315. v[i] = stbi__get8(z->s);
  1316. L -= n;
  1317. }
  1318. return L==0;
  1319. }
  1320. // check for comment block or APP blocks
  1321. if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
  1322. stbi__skip(z->s, stbi__get16be(z->s)-2);
  1323. return 1;
  1324. }
  1325. return 0;
  1326. }
  1327. // after we see stbi__SOS
  1328. static int stbi__process_scan_header(stbi__jpeg *z)
  1329. {
  1330. int i;
  1331. int Ls = stbi__get16be(z->s);
  1332. z->scan_n = stbi__get8(z->s);
  1333. if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad stbi__SOS component count","Corrupt JPEG");
  1334. if (Ls != 6+2*z->scan_n) return stbi__err("bad stbi__SOS len","Corrupt JPEG");
  1335. for (i=0; i < z->scan_n; ++i) {
  1336. int id = stbi__get8(z->s), which;
  1337. int q = stbi__get8(z->s);
  1338. for (which = 0; which < z->s->img_n; ++which)
  1339. if (z->img_comp[which].id == id)
  1340. break;
  1341. if (which == z->s->img_n) return 0;
  1342. z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG");
  1343. z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG");
  1344. z->order[i] = which;
  1345. }
  1346. if (stbi__get8(z->s) != 0) return stbi__err("bad stbi__SOS","Corrupt JPEG");
  1347. stbi__get8(z->s); // should be 63, but might be 0
  1348. if (stbi__get8(z->s) != 0) return stbi__err("bad stbi__SOS","Corrupt JPEG");
  1349. return 1;
  1350. }
  1351. static int stbi__process_frame_header(stbi__jpeg *z, int scan)
  1352. {
  1353. stbi__context *s = z->s;
  1354. int Lf,p,i,q, h_max=1,v_max=1,c;
  1355. Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad stbi__SOF len","Corrupt JPEG"); // JPEG
  1356. p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
  1357. s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
  1358. s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires
  1359. c = stbi__get8(s);
  1360. if (c != 3 && c != 1) return stbi__err("bad component count","Corrupt JPEG"); // JFIF requires
  1361. s->img_n = c;
  1362. for (i=0; i < c; ++i) {
  1363. z->img_comp[i].data = NULL;
  1364. z->img_comp[i].linebuf = NULL;
  1365. }
  1366. if (Lf != 8+3*s->img_n) return stbi__err("bad stbi__SOF len","Corrupt JPEG");
  1367. for (i=0; i < s->img_n; ++i) {
  1368. z->img_comp[i].id = stbi__get8(s);
  1369. if (z->img_comp[i].id != i+1) // JFIF requires
  1370. if (z->img_comp[i].id != i) // some version of jpegtran outputs non-JFIF-compliant files!
  1371. return stbi__err("bad component ID","Corrupt JPEG");
  1372. q = stbi__get8(s);
  1373. z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG");
  1374. z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG");
  1375. z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG");
  1376. }
  1377. if (scan != SCAN_load) return 1;
  1378. if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to stbi__jpeg_huff_decode");
  1379. for (i=0; i < s->img_n; ++i) {
  1380. if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
  1381. if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
  1382. }
  1383. // compute interleaved mcu info
  1384. z->img_h_max = h_max;
  1385. z->img_v_max = v_max;
  1386. z->img_mcu_w = h_max * 8;
  1387. z->img_mcu_h = v_max * 8;
  1388. z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
  1389. z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
  1390. for (i=0; i < s->img_n; ++i) {
  1391. // number of effective pixels (stbi__err.g. for non-interleaved MCU)
  1392. z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
  1393. z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
  1394. // to simplify generation, we'll allocate enough memory to stbi__jpeg_huff_decode
  1395. // the bogus oversized data from using interleaved MCUs and their
  1396. // big blocks (stbi__err.g. a 16x16 iMCU on an image of width 33); we won't
  1397. // discard the extra data until colorspace conversion
  1398. z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
  1399. z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
  1400. z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
  1401. if (z->img_comp[i].raw_data == NULL) {
  1402. for(--i; i >= 0; --i) {
  1403. free(z->img_comp[i].raw_data);
  1404. z->img_comp[i].data = NULL;
  1405. }
  1406. return stbi__err("outofmem", "Out of memory");
  1407. }
  1408. // align blocks for installable-idct using mmx/sse
  1409. z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
  1410. z->img_comp[i].linebuf = NULL;
  1411. }
  1412. return 1;
  1413. }
  1414. // use comparisons since in some cases we handle more than one case (stbi__err.g. stbi__SOF)
  1415. #define stbi__DNL(x) ((x) == 0xdc)
  1416. #define stbi__SOI(x) ((x) == 0xd8)
  1417. #define stbi__EOI(x) ((x) == 0xd9)
  1418. #define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1)
  1419. #define stbi__SOS(x) ((x) == 0xda)
  1420. static int decode_jpeg_header(stbi__jpeg *z, int scan)
  1421. {
  1422. int m;
  1423. z->marker = STBI__MARKER_none; // initialize cached marker to empty
  1424. m = stbi__get_marker(z);
  1425. if (!stbi__SOI(m)) return stbi__err("no stbi__SOI","Corrupt JPEG");
  1426. if (scan == SCAN_type) return 1;
  1427. m = stbi__get_marker(z);
  1428. while (!stbi__SOF(m)) {
  1429. if (!stbi__process_marker(z,m)) return 0;
  1430. m = stbi__get_marker(z);
  1431. while (m == STBI__MARKER_none) {
  1432. // some files have extra padding after their blocks, so ok, we'll scan
  1433. if (stbi__at_eof(z->s)) return stbi__err("no stbi__SOF", "Corrupt JPEG");
  1434. m = stbi__get_marker(z);
  1435. }
  1436. }
  1437. if (!stbi__process_frame_header(z, scan)) return 0;
  1438. return 1;
  1439. }
  1440. static int decode_jpeg_image(stbi__jpeg *j)
  1441. {
  1442. int m;
  1443. j->restart_interval = 0;
  1444. if (!decode_jpeg_header(j, SCAN_load)) return 0;
  1445. m = stbi__get_marker(j);
  1446. while (!stbi__EOI(m)) {
  1447. if (stbi__SOS(m)) {
  1448. if (!stbi__process_scan_header(j)) return 0;
  1449. if (!stbi__parse_entropy_coded_data(j)) return 0;
  1450. if (j->marker == STBI__MARKER_none ) {
  1451. // handle 0s at the end of image data from IP Kamera 9060
  1452. while (!stbi__at_eof(j->s)) {
  1453. int x = stbi__get8(j->s);
  1454. if (x == 255) {
  1455. j->marker = stbi__get8(j->s);
  1456. break;
  1457. } else if (x != 0) {
  1458. return 0;
  1459. }
  1460. }
  1461. // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0
  1462. }
  1463. } else {
  1464. if (!stbi__process_marker(j, m)) return 0;
  1465. }
  1466. m = stbi__get_marker(j);
  1467. }
  1468. return 1;
  1469. }
  1470. // static jfif-centered resampling (across block boundaries)
  1471. typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1,
  1472. int w, int hs);
  1473. #define stbi__div4(x) ((stbi_uc) ((x) >> 2))
  1474. static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  1475. {
  1476. STBI_NOTUSED(out);
  1477. STBI_NOTUSED(in_far);
  1478. STBI_NOTUSED(w);
  1479. STBI_NOTUSED(hs);
  1480. return in_near;
  1481. }
  1482. static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  1483. {
  1484. // need to generate two samples vertically for every one in input
  1485. int i;
  1486. STBI_NOTUSED(hs);
  1487. for (i=0; i < w; ++i)
  1488. out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2);
  1489. return out;
  1490. }
  1491. static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  1492. {
  1493. // need to generate two samples horizontally for every one in input
  1494. int i;
  1495. stbi_uc *input = in_near;
  1496. if (w == 1) {
  1497. // if only one sample, can't do any interpolation
  1498. out[0] = out[1] = input[0];
  1499. return out;
  1500. }
  1501. out[0] = input[0];
  1502. out[1] = stbi__div4(input[0]*3 + input[1] + 2);
  1503. for (i=1; i < w-1; ++i) {
  1504. int n = 3*input[i]+2;
  1505. out[i*2+0] = stbi__div4(n+input[i-1]);
  1506. out[i*2+1] = stbi__div4(n+input[i+1]);
  1507. }
  1508. out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2);
  1509. out[i*2+1] = input[w-1];
  1510. STBI_NOTUSED(in_far);
  1511. STBI_NOTUSED(hs);
  1512. return out;
  1513. }
  1514. #define stbi__div16(x) ((stbi_uc) ((x) >> 4))
  1515. static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  1516. {
  1517. // need to generate 2x2 samples for every one in input
  1518. int i,t0,t1;
  1519. if (w == 1) {
  1520. out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
  1521. return out;
  1522. }
  1523. t1 = 3*in_near[0] + in_far[0];
  1524. out[0] = stbi__div4(t1+2);
  1525. for (i=1; i < w; ++i) {
  1526. t0 = t1;
  1527. t1 = 3*in_near[i]+in_far[i];
  1528. out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
  1529. out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
  1530. }
  1531. out[w*2-1] = stbi__div4(t1+2);
  1532. STBI_NOTUSED(hs);
  1533. return out;
  1534. }
  1535. static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  1536. {
  1537. // resample with nearest-neighbor
  1538. int i,j;
  1539. STBI_NOTUSED(in_far);
  1540. for (i=0; i < w; ++i)
  1541. for (j=0; j < hs; ++j)
  1542. out[i*hs+j] = in_near[i];
  1543. return out;
  1544. }
  1545. #define float2fixed(x) ((int) ((x) * 65536 + 0.5))
  1546. // 0.38 seconds on 3*anemones.jpg (0.25 with processor = Pro)
  1547. // VC6 without processor=Pro is generating multiple LEAs per multiply!
  1548. static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step)
  1549. {
  1550. int i;
  1551. for (i=0; i < count; ++i) {
  1552. int y_fixed = (y[i] << 16) + 32768; // rounding
  1553. int r,g,b;
  1554. int cr = pcr[i] - 128;
  1555. int cb = pcb[i] - 128;
  1556. r = y_fixed + cr*float2fixed(1.40200f);
  1557. g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);
  1558. b = y_fixed + cb*float2fixed(1.77200f);
  1559. r >>= 16;
  1560. g >>= 16;
  1561. b >>= 16;
  1562. if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
  1563. if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
  1564. if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
  1565. out[0] = (stbi_uc)r;
  1566. out[1] = (stbi_uc)g;
  1567. out[2] = (stbi_uc)b;
  1568. out[3] = 255;
  1569. out += step;
  1570. }
  1571. }
  1572. #ifdef STBI_SIMD
  1573. static stbi_YCbCr_to_RGB_run stbi__YCbCr_installed = stbi__YCbCr_to_RGB_row;
  1574. STBIDEF void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)
  1575. {
  1576. stbi__YCbCr_installed = func;
  1577. }
  1578. #endif
  1579. // clean up the temporary component buffers
  1580. static void stbi__cleanup_jpeg(stbi__jpeg *j)
  1581. {
  1582. int i;
  1583. for (i=0; i < j->s->img_n; ++i) {
  1584. if (j->img_comp[i].data) {
  1585. free(j->img_comp[i].raw_data);
  1586. j->img_comp[i].data = NULL;
  1587. }
  1588. if (j->img_comp[i].linebuf) {
  1589. free(j->img_comp[i].linebuf);
  1590. j->img_comp[i].linebuf = NULL;
  1591. }
  1592. }
  1593. }
  1594. typedef struct
  1595. {
  1596. resample_row_func resample;
  1597. stbi_uc *line0,*line1;
  1598. int hs,vs; // expansion factor in each axis
  1599. int w_lores; // horizontal pixels pre-expansion
  1600. int ystep; // how far through vertical expansion we are
  1601. int ypos; // which pre-expansion row we're on
  1602. } stbi__resample;
  1603. static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
  1604. {
  1605. int n, decode_n;
  1606. // validate req_comp
  1607. if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
  1608. z->s->img_n = 0;
  1609. // load a jpeg image from whichever source
  1610. if (!decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; }
  1611. // determine actual number of components to generate
  1612. n = req_comp ? req_comp : z->s->img_n;
  1613. if (z->s->img_n == 3 && n < 3)
  1614. decode_n = 1;
  1615. else
  1616. decode_n = z->s->img_n;
  1617. // resample and color-convert
  1618. {
  1619. int k;
  1620. unsigned int i,j;
  1621. stbi_uc *output;
  1622. stbi_uc *coutput[4];
  1623. stbi__resample res_comp[4];
  1624. for (k=0; k < decode_n; ++k) {
  1625. stbi__resample *r = &res_comp[k];
  1626. // allocate line buffer big enough for upsampling off the edges
  1627. // with upsample factor of 4
  1628. z->img_comp[k].linebuf = (stbi_uc *) malloc(z->s->img_x + 3);
  1629. if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
  1630. r->hs = z->img_h_max / z->img_comp[k].h;
  1631. r->vs = z->img_v_max / z->img_comp[k].v;
  1632. r->ystep = r->vs >> 1;
  1633. r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
  1634. r->ypos = 0;
  1635. r->line0 = r->line1 = z->img_comp[k].data;
  1636. if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
  1637. else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2;
  1638. else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2;
  1639. else if (r->hs == 2 && r->vs == 2) r->resample = stbi__resample_row_hv_2;
  1640. else r->resample = stbi__resample_row_generic;
  1641. }
  1642. // can't error after this so, this is safe
  1643. output = (stbi_uc *) malloc(n * z->s->img_x * z->s->img_y + 1);
  1644. if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
  1645. // now go ahead and resample
  1646. for (j=0; j < z->s->img_y; ++j) {
  1647. stbi_uc *out = output + n * z->s->img_x * j;
  1648. for (k=0; k < decode_n; ++k) {
  1649. stbi__resample *r = &res_comp[k];
  1650. int y_bot = r->ystep >= (r->vs >> 1);
  1651. coutput[k] = r->resample(z->img_comp[k].linebuf,
  1652. y_bot ? r->line1 : r->line0,
  1653. y_bot ? r->line0 : r->line1,
  1654. r->w_lores, r->hs);
  1655. if (++r->ystep >= r->vs) {
  1656. r->ystep = 0;
  1657. r->line0 = r->line1;
  1658. if (++r->ypos < z->img_comp[k].y)
  1659. r->line1 += z->img_comp[k].w2;
  1660. }
  1661. }
  1662. if (n >= 3) {
  1663. stbi_uc *y = coutput[0];
  1664. if (z->s->img_n == 3) {
  1665. #ifdef STBI_SIMD
  1666. stbi__YCbCr_installed(out, y, coutput[1], coutput[2], z->s->img_x, n);
  1667. #else
  1668. stbi__YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s->img_x, n);
  1669. #endif
  1670. } else
  1671. for (i=0; i < z->s->img_x; ++i) {
  1672. out[0] = out[1] = out[2] = y[i];
  1673. out[3] = 255; // not used if n==3
  1674. out += n;
  1675. }
  1676. } else {
  1677. stbi_uc *y = coutput[0];
  1678. if (n == 1)
  1679. for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
  1680. else
  1681. for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
  1682. }
  1683. }
  1684. stbi__cleanup_jpeg(z);
  1685. *out_x = z->s->img_x;
  1686. *out_y = z->s->img_y;
  1687. if (comp) *comp = z->s->img_n; // report original components, not output
  1688. return output;
  1689. }
  1690. }
  1691. static unsigned char *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  1692. {
  1693. stbi__jpeg j;
  1694. j.s = s;
  1695. return load_jpeg_image(&j, x,y,comp,req_comp);
  1696. }
  1697. static int stbi__jpeg_test(stbi__context *s)
  1698. {
  1699. int r;
  1700. stbi__jpeg j;
  1701. j.s = s;
  1702. r = decode_jpeg_header(&j, SCAN_type);
  1703. stbi__rewind(s);
  1704. return r;
  1705. }
  1706. static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp)
  1707. {
  1708. if (!decode_jpeg_header(j, SCAN_header)) {
  1709. stbi__rewind( j->s );
  1710. return 0;
  1711. }
  1712. if (x) *x = j->s->img_x;
  1713. if (y) *y = j->s->img_y;
  1714. if (comp) *comp = j->s->img_n;
  1715. return 1;
  1716. }
  1717. static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp)
  1718. {
  1719. stbi__jpeg j;
  1720. j.s = s;
  1721. return stbi__jpeg_info_raw(&j, x, y, comp);
  1722. }
  1723. // public domain zlib stbi__jpeg_huff_decode v0.2 Sean Barrett 2006-11-18
  1724. // simple implementation
  1725. // - all input must be provided in an upfront buffer
  1726. // - all output is written to a single output buffer (can malloc/realloc)
  1727. // performance
  1728. // - fast huffman
  1729. // fast-way is faster to check than jpeg huffman, but slow way is slower
  1730. #define STBI__ZFAST_BITS 9 // accelerate all cases in default tables
  1731. #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
  1732. // zlib-style huffman encoding
  1733. // (jpegs packs from left, zlib from right, so can't share code)
  1734. typedef struct
  1735. {
  1736. stbi__uint16 fast[1 << STBI__ZFAST_BITS];
  1737. stbi__uint16 firstcode[16];
  1738. int maxcode[17];
  1739. stbi__uint16 firstsymbol[16];
  1740. stbi_uc size[288];
  1741. stbi__uint16 value[288];
  1742. } stbi__zhuffman;
  1743. stbi_inline static int stbi__bitreverse16(int n)
  1744. {
  1745. n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
  1746. n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
  1747. n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
  1748. n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
  1749. return n;
  1750. }
  1751. stbi_inline static int stbi__bit_reverse(int v, int bits)
  1752. {
  1753. assert(bits <= 16);
  1754. // to bit reverse n bits, reverse 16 and shift
  1755. // stbi__err.g. 11 bits, bit reverse and shift away 5
  1756. return stbi__bitreverse16(v) >> (16-bits);
  1757. }
  1758. static int stbi__zbuild_huffman(stbi__zhuffman *z, stbi_uc *sizelist, int num)
  1759. {
  1760. int i,k=0;
  1761. int code, next_code[16], sizes[17];
  1762. // DEFLATE spec for generating codes
  1763. memset(sizes, 0, sizeof(sizes));
  1764. memset(z->fast, 255, sizeof(z->fast));
  1765. for (i=0; i < num; ++i)
  1766. ++sizes[sizelist[i]];
  1767. sizes[0] = 0;
  1768. for (i=1; i < 16; ++i)
  1769. assert(sizes[i] <= (1 << i));
  1770. code = 0;
  1771. for (i=1; i < 16; ++i) {
  1772. next_code[i] = code;
  1773. z->firstcode[i] = (stbi__uint16) code;
  1774. z->firstsymbol[i] = (stbi__uint16) k;
  1775. code = (code + sizes[i]);
  1776. if (sizes[i])
  1777. if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt JPEG");
  1778. z->maxcode[i] = code << (16-i); // preshift for inner loop
  1779. code <<= 1;
  1780. k += sizes[i];
  1781. }
  1782. z->maxcode[16] = 0x10000; // sentinel
  1783. for (i=0; i < num; ++i) {
  1784. int s = sizelist[i];
  1785. if (s) {
  1786. int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
  1787. z->size [c] = (stbi_uc ) s;
  1788. z->value[c] = (stbi__uint16) i;
  1789. if (s <= STBI__ZFAST_BITS) {
  1790. int k = stbi__bit_reverse(next_code[s],s);
  1791. while (k < (1 << STBI__ZFAST_BITS)) {
  1792. z->fast[k] = (stbi__uint16) c;
  1793. k += (1 << s);
  1794. }
  1795. }
  1796. ++next_code[s];
  1797. }
  1798. }
  1799. return 1;
  1800. }
  1801. // zlib-from-memory implementation for PNG reading
  1802. // because PNG allows splitting the zlib stream arbitrarily,
  1803. // and it's annoying structurally to have PNG call ZLIB call PNG,
  1804. // we require PNG read all the IDATs and combine them into a single
  1805. // memory buffer
  1806. typedef struct
  1807. {
  1808. stbi_uc *zbuffer, *zbuffer_end;
  1809. int num_bits;
  1810. stbi__uint32 code_buffer;
  1811. char *zout;
  1812. char *zout_start;
  1813. char *zout_end;
  1814. int z_expandable;
  1815. stbi__zhuffman z_length, z_distance;
  1816. } stbi__zbuf;
  1817. stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z)
  1818. {
  1819. if (z->zbuffer >= z->zbuffer_end) return 0;
  1820. return *z->zbuffer++;
  1821. }
  1822. static void stbi__fill_bits(stbi__zbuf *z)
  1823. {
  1824. do {
  1825. assert(z->code_buffer < (1U << z->num_bits));
  1826. z->code_buffer |= stbi__zget8(z) << z->num_bits;
  1827. z->num_bits += 8;
  1828. } while (z->num_bits <= 24);
  1829. }
  1830. stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n)
  1831. {
  1832. unsigned int k;
  1833. if (z->num_bits < n) stbi__fill_bits(z);
  1834. k = z->code_buffer & ((1 << n) - 1);
  1835. z->code_buffer >>= n;
  1836. z->num_bits -= n;
  1837. return k;
  1838. }
  1839. stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
  1840. {
  1841. int b,s,k;
  1842. if (a->num_bits < 16) stbi__fill_bits(a);
  1843. b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
  1844. if (b < 0xffff) {
  1845. s = z->size[b];
  1846. a->code_buffer >>= s;
  1847. a->num_bits -= s;
  1848. return z->value[b];
  1849. }
  1850. // not resolved by fast table, so compute it the slow way
  1851. // use jpeg approach, which requires MSbits at top
  1852. k = stbi__bit_reverse(a->code_buffer, 16);
  1853. for (s=STBI__ZFAST_BITS+1; ; ++s)
  1854. if (k < z->maxcode[s])
  1855. break;
  1856. if (s == 16) return -1; // invalid code!
  1857. // code size is s, so:
  1858. b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
  1859. assert(z->size[b] == s);
  1860. a->code_buffer >>= s;
  1861. a->num_bits -= s;
  1862. return z->value[b];
  1863. }
  1864. static int stbi__zexpand(stbi__zbuf *z, int n) // need to make room for n bytes
  1865. {
  1866. char *q;
  1867. int cur, limit;
  1868. if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
  1869. cur = (int) (z->zout - z->zout_start);
  1870. limit = (int) (z->zout_end - z->zout_start);
  1871. while (cur + n > limit)
  1872. limit *= 2;
  1873. q = (char *) realloc(z->zout_start, limit);
  1874. if (q == NULL) return stbi__err("outofmem", "Out of memory");
  1875. z->zout_start = q;
  1876. z->zout = q + cur;
  1877. z->zout_end = q + limit;
  1878. return 1;
  1879. }
  1880. static int stbi__zlength_base[31] = {
  1881. 3,4,5,6,7,8,9,10,11,13,
  1882. 15,17,19,23,27,31,35,43,51,59,
  1883. 67,83,99,115,131,163,195,227,258,0,0 };
  1884. static int stbi__zlength_extra[31]=
  1885. { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
  1886. static int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
  1887. 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
  1888. static int stbi__zdist_extra[32] =
  1889. { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
  1890. static int stbi__parse_huffman_block(stbi__zbuf *a)
  1891. {
  1892. for(;;) {
  1893. int z = stbi__zhuffman_decode(a, &a->z_length);
  1894. if (z < 256) {
  1895. if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes
  1896. if (a->zout >= a->zout_end) if (!stbi__zexpand(a, 1)) return 0;
  1897. *a->zout++ = (char) z;
  1898. } else {
  1899. stbi_uc *p;
  1900. int len,dist;
  1901. if (z == 256) return 1;
  1902. z -= 257;
  1903. len = stbi__zlength_base[z];
  1904. if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
  1905. z = stbi__zhuffman_decode(a, &a->z_distance);
  1906. if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");
  1907. dist = stbi__zdist_base[z];
  1908. if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
  1909. if (a->zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");
  1910. if (a->zout + len > a->zout_end) if (!stbi__zexpand(a, len)) return 0;
  1911. p = (stbi_uc *) (a->zout - dist);
  1912. while (len--)
  1913. *a->zout++ = *p++;
  1914. }
  1915. }
  1916. }
  1917. static int stbi__compute_huffman_codes(stbi__zbuf *a)
  1918. {
  1919. static stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
  1920. stbi__zhuffman z_codelength;
  1921. stbi_uc lencodes[286+32+137];//padding for maximum single op
  1922. stbi_uc codelength_sizes[19];
  1923. int i,n;
  1924. int hlit = stbi__zreceive(a,5) + 257;
  1925. int hdist = stbi__zreceive(a,5) + 1;
  1926. int hclen = stbi__zreceive(a,4) + 4;
  1927. memset(codelength_sizes, 0, sizeof(codelength_sizes));
  1928. for (i=0; i < hclen; ++i) {
  1929. int s = stbi__zreceive(a,3);
  1930. codelength_sizes[length_dezigzag[i]] = (stbi_uc) s;
  1931. }
  1932. if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
  1933. n = 0;
  1934. while (n < hlit + hdist) {
  1935. int c = stbi__zhuffman_decode(a, &z_codelength);
  1936. assert(c >= 0 && c < 19);
  1937. if (c < 16)
  1938. lencodes[n++] = (stbi_uc) c;
  1939. else if (c == 16) {
  1940. c = stbi__zreceive(a,2)+3;
  1941. memset(lencodes+n, lencodes[n-1], c);
  1942. n += c;
  1943. } else if (c == 17) {
  1944. c = stbi__zreceive(a,3)+3;
  1945. memset(lencodes+n, 0, c);
  1946. n += c;
  1947. } else {
  1948. assert(c == 18);
  1949. c = stbi__zreceive(a,7)+11;
  1950. memset(lencodes+n, 0, c);
  1951. n += c;
  1952. }
  1953. }
  1954. if (n != hlit+hdist) return stbi__err("bad codelengths","Corrupt PNG");
  1955. if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
  1956. if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
  1957. return 1;
  1958. }
  1959. static int stbi__parse_uncomperssed_block(stbi__zbuf *a)
  1960. {
  1961. stbi_uc header[4];
  1962. int len,nlen,k;
  1963. if (a->num_bits & 7)
  1964. stbi__zreceive(a, a->num_bits & 7); // discard
  1965. // drain the bit-packed data into header
  1966. k = 0;
  1967. while (a->num_bits > 0) {
  1968. header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check
  1969. a->code_buffer >>= 8;
  1970. a->num_bits -= 8;
  1971. }
  1972. assert(a->num_bits == 0);
  1973. // now fill header the normal way
  1974. while (k < 4)
  1975. header[k++] = stbi__zget8(a);
  1976. len = header[1] * 256 + header[0];
  1977. nlen = header[3] * 256 + header[2];
  1978. if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG");
  1979. if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG");
  1980. if (a->zout + len > a->zout_end)
  1981. if (!stbi__zexpand(a, len)) return 0;
  1982. memcpy(a->zout, a->zbuffer, len);
  1983. a->zbuffer += len;
  1984. a->zout += len;
  1985. return 1;
  1986. }
  1987. static int stbi__parse_zlib_header(stbi__zbuf *a)
  1988. {
  1989. int cmf = stbi__zget8(a);
  1990. int cm = cmf & 15;
  1991. /* int cinfo = cmf >> 4; */
  1992. int flg = stbi__zget8(a);
  1993. if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec
  1994. if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
  1995. if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png
  1996. // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
  1997. return 1;
  1998. }
  1999. // @TODO: should statically initialize these for optimal thread safety
  2000. static stbi_uc stbi__zdefault_length[288], stbi__zdefault_distance[32];
  2001. static void stbi__init_zdefaults(void)
  2002. {
  2003. int i; // use <= to match clearly with spec
  2004. for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8;
  2005. for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9;
  2006. for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7;
  2007. for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8;
  2008. for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5;
  2009. }
  2010. static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
  2011. {
  2012. int final_, type;
  2013. if (parse_header)
  2014. if (!stbi__parse_zlib_header(a)) return 0;
  2015. a->num_bits = 0;
  2016. a->code_buffer = 0;
  2017. do {
  2018. final_ = stbi__zreceive(a,1);
  2019. type = stbi__zreceive(a,2);
  2020. if (type == 0) {
  2021. if (!stbi__parse_uncomperssed_block(a)) return 0;
  2022. } else if (type == 3) {
  2023. return 0;
  2024. } else {
  2025. if (type == 1) {
  2026. // use fixed code lengths
  2027. if (!stbi__zdefault_distance[31]) stbi__init_zdefaults();
  2028. if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0;
  2029. if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
  2030. } else {
  2031. if (!stbi__compute_huffman_codes(a)) return 0;
  2032. }
  2033. if (!stbi__parse_huffman_block(a)) return 0;
  2034. }
  2035. } while (!final_);
  2036. return 1;
  2037. }
  2038. static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header)
  2039. {
  2040. a->zout_start = obuf;
  2041. a->zout = obuf;
  2042. a->zout_end = obuf + olen;
  2043. a->z_expandable = exp;
  2044. return stbi__parse_zlib(a, parse_header);
  2045. }
  2046. STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
  2047. {
  2048. stbi__zbuf a;
  2049. char *p = (char *) malloc(initial_size);
  2050. if (p == NULL) return NULL;
  2051. a.zbuffer = (stbi_uc *) buffer;
  2052. a.zbuffer_end = (stbi_uc *) buffer + len;
  2053. if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
  2054. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2055. return a.zout_start;
  2056. } else {
  2057. free(a.zout_start);
  2058. return NULL;
  2059. }
  2060. }
  2061. STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
  2062. {
  2063. return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
  2064. }
  2065. STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
  2066. {
  2067. stbi__zbuf a;
  2068. char *p = (char *) malloc(initial_size);
  2069. if (p == NULL) return NULL;
  2070. a.zbuffer = (stbi_uc *) buffer;
  2071. a.zbuffer_end = (stbi_uc *) buffer + len;
  2072. if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
  2073. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2074. return a.zout_start;
  2075. } else {
  2076. free(a.zout_start);
  2077. return NULL;
  2078. }
  2079. }
  2080. STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
  2081. {
  2082. stbi__zbuf a;
  2083. a.zbuffer = (stbi_uc *) ibuffer;
  2084. a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
  2085. if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
  2086. return (int) (a.zout - a.zout_start);
  2087. else
  2088. return -1;
  2089. }
  2090. STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
  2091. {
  2092. stbi__zbuf a;
  2093. char *p = (char *) malloc(16384);
  2094. if (p == NULL) return NULL;
  2095. a.zbuffer = (stbi_uc *) buffer;
  2096. a.zbuffer_end = (stbi_uc *) buffer+len;
  2097. if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
  2098. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2099. return a.zout_start;
  2100. } else {
  2101. free(a.zout_start);
  2102. return NULL;
  2103. }
  2104. }
  2105. STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
  2106. {
  2107. stbi__zbuf a;
  2108. a.zbuffer = (stbi_uc *) ibuffer;
  2109. a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
  2110. if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
  2111. return (int) (a.zout - a.zout_start);
  2112. else
  2113. return -1;
  2114. }
  2115. // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18
  2116. // simple implementation
  2117. // - only 8-bit samples
  2118. // - no CRC checking
  2119. // - allocates lots of intermediate memory
  2120. // - avoids problem of streaming data between subsystems
  2121. // - avoids explicit window management
  2122. // performance
  2123. // - uses stb_zlib, a PD zlib implementation with fast huffman decoding
  2124. typedef struct
  2125. {
  2126. stbi__uint32 length;
  2127. stbi__uint32 type;
  2128. } stbi__pngchunk;
  2129. #define PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
  2130. static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
  2131. {
  2132. stbi__pngchunk c;
  2133. c.length = stbi__get32be(s);
  2134. c.type = stbi__get32be(s);
  2135. return c;
  2136. }
  2137. static int stbi__check_png_header(stbi__context *s)
  2138. {
  2139. static stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };
  2140. int i;
  2141. for (i=0; i < 8; ++i)
  2142. if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG");
  2143. return 1;
  2144. }
  2145. typedef struct
  2146. {
  2147. stbi__context *s;
  2148. stbi_uc *idata, *expanded, *out;
  2149. } stbi__png;
  2150. enum {
  2151. STBI__F_none=0, STBI__F_sub=1, STBI__F_up=2, STBI__F_avg=3, STBI__F_paeth=4,
  2152. STBI__F_avg_first, STBI__F_paeth_first
  2153. };
  2154. static stbi_uc first_row_filter[5] =
  2155. {
  2156. STBI__F_none, STBI__F_sub, STBI__F_none, STBI__F_avg_first, STBI__F_paeth_first
  2157. };
  2158. static int stbi__paeth(int a, int b, int c)
  2159. {
  2160. int p = a + b - c;
  2161. int pa = abs(p-a);
  2162. int pb = abs(p-b);
  2163. int pc = abs(p-c);
  2164. if (pa <= pb && pa <= pc) return a;
  2165. if (pb <= pc) return b;
  2166. return c;
  2167. }
  2168. #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
  2169. // create the png data from post-deflated data
  2170. static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y)
  2171. {
  2172. stbi__context *s = a->s;
  2173. stbi__uint32 i,j,stride = x*out_n;
  2174. int k;
  2175. int img_n = s->img_n; // copy it into a local for later
  2176. assert(out_n == s->img_n || out_n == s->img_n+1);
  2177. a->out = (stbi_uc *) malloc(x * y * out_n);
  2178. if (!a->out) return stbi__err("outofmem", "Out of memory");
  2179. if (s->img_x == x && s->img_y == y) {
  2180. if (raw_len != (img_n * x + 1) * y) return stbi__err("not enough pixels","Corrupt PNG");
  2181. } else { // interlaced:
  2182. if (raw_len < (img_n * x + 1) * y) return stbi__err("not enough pixels","Corrupt PNG");
  2183. }
  2184. for (j=0; j < y; ++j) {
  2185. stbi_uc *cur = a->out + stride*j;
  2186. stbi_uc *prior = cur - stride;
  2187. int filter = *raw++;
  2188. if (filter > 4) return stbi__err("invalid filter","Corrupt PNG");
  2189. // if first row, use special filter that doesn't sample previous row
  2190. if (j == 0) filter = first_row_filter[filter];
  2191. // handle first pixel explicitly
  2192. for (k=0; k < img_n; ++k) {
  2193. switch (filter) {
  2194. case STBI__F_none : cur[k] = raw[k]; break;
  2195. case STBI__F_sub : cur[k] = raw[k]; break;
  2196. case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
  2197. case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break;
  2198. case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break;
  2199. case STBI__F_avg_first : cur[k] = raw[k]; break;
  2200. case STBI__F_paeth_first: cur[k] = raw[k]; break;
  2201. }
  2202. }
  2203. if (img_n != out_n) cur[img_n] = 255;
  2204. raw += img_n;
  2205. cur += out_n;
  2206. prior += out_n;
  2207. // this is a little gross, so that we don't switch per-pixel or per-component
  2208. if (img_n == out_n) {
  2209. #define CASE(f) \
  2210. case f: \
  2211. for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \
  2212. for (k=0; k < img_n; ++k)
  2213. switch (filter) {
  2214. CASE(STBI__F_none) cur[k] = raw[k]; break;
  2215. CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-img_n]); break;
  2216. CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
  2217. CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-img_n])>>1)); break;
  2218. CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;
  2219. CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-img_n] >> 1)); break;
  2220. CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-img_n],0,0)); break;
  2221. }
  2222. #undef CASE
  2223. } else {
  2224. assert(img_n+1 == out_n);
  2225. #define CASE(f) \
  2226. case f: \
  2227. for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
  2228. for (k=0; k < img_n; ++k)
  2229. switch (filter) {
  2230. CASE(STBI__F_none) cur[k] = raw[k]; break;
  2231. CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-out_n]); break;
  2232. CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
  2233. CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-out_n])>>1)); break;
  2234. CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
  2235. CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-out_n] >> 1)); break;
  2236. CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],0,0)); break;
  2237. }
  2238. #undef CASE
  2239. }
  2240. }
  2241. return 1;
  2242. }
  2243. static int stbi__create_png_image(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, int interlaced)
  2244. {
  2245. stbi_uc *final_;
  2246. int p;
  2247. if (!interlaced)
  2248. return stbi__create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y);
  2249. // de-interlacing
  2250. final_ = (stbi_uc *) malloc(a->s->img_x * a->s->img_y * out_n);
  2251. for (p=0; p < 7; ++p) {
  2252. int xorig[] = { 0,4,0,2,0,1,0 };
  2253. int yorig[] = { 0,0,4,0,2,0,1 };
  2254. int xspc[] = { 8,8,4,4,2,2,1 };
  2255. int yspc[] = { 8,8,8,4,4,2,2 };
  2256. int i,j,x,y;
  2257. // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
  2258. x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
  2259. y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
  2260. if (x && y) {
  2261. if (!stbi__create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
  2262. free(final_);
  2263. return 0;
  2264. }
  2265. for (j=0; j < y; ++j)
  2266. for (i=0; i < x; ++i)
  2267. memcpy(final_ + (j*yspc[p]+yorig[p])*a->s->img_x*out_n + (i*xspc[p]+xorig[p])*out_n,
  2268. a->out + (j*x+i)*out_n, out_n);
  2269. free(a->out);
  2270. raw += (x*out_n+1)*y;
  2271. raw_len -= (x*out_n+1)*y;
  2272. }
  2273. }
  2274. a->out = final_;
  2275. return 1;
  2276. }
  2277. static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n)
  2278. {
  2279. stbi__context *s = z->s;
  2280. stbi__uint32 i, pixel_count = s->img_x * s->img_y;
  2281. stbi_uc *p = z->out;
  2282. // compute color-based transparency, assuming we've
  2283. // already got 255 as the alpha value in the output
  2284. assert(out_n == 2 || out_n == 4);
  2285. if (out_n == 2) {
  2286. for (i=0; i < pixel_count; ++i) {
  2287. p[1] = (p[0] == tc[0] ? 0 : 255);
  2288. p += 2;
  2289. }
  2290. } else {
  2291. for (i=0; i < pixel_count; ++i) {
  2292. if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
  2293. p[3] = 0;
  2294. p += 4;
  2295. }
  2296. }
  2297. return 1;
  2298. }
  2299. static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n)
  2300. {
  2301. stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
  2302. stbi_uc *p, *temp_out, *orig = a->out;
  2303. p = (stbi_uc *) malloc(pixel_count * pal_img_n);
  2304. if (p == NULL) return stbi__err("outofmem", "Out of memory");
  2305. // between here and free(out) below, exitting would leak
  2306. temp_out = p;
  2307. if (pal_img_n == 3) {
  2308. for (i=0; i < pixel_count; ++i) {
  2309. int n = orig[i]*4;
  2310. p[0] = palette[n ];
  2311. p[1] = palette[n+1];
  2312. p[2] = palette[n+2];
  2313. p += 3;
  2314. }
  2315. } else {
  2316. for (i=0; i < pixel_count; ++i) {
  2317. int n = orig[i]*4;
  2318. p[0] = palette[n ];
  2319. p[1] = palette[n+1];
  2320. p[2] = palette[n+2];
  2321. p[3] = palette[n+3];
  2322. p += 4;
  2323. }
  2324. }
  2325. free(a->out);
  2326. a->out = temp_out;
  2327. STBI_NOTUSED(len);
  2328. return 1;
  2329. }
  2330. static int stbi__unpremultiply_on_load = 0;
  2331. static int stbi__de_iphone_flag = 0;
  2332. STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
  2333. {
  2334. stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;
  2335. }
  2336. STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
  2337. {
  2338. stbi__de_iphone_flag = flag_true_if_should_convert;
  2339. }
  2340. static void stbi__de_iphone(stbi__png *z)
  2341. {
  2342. stbi__context *s = z->s;
  2343. stbi__uint32 i, pixel_count = s->img_x * s->img_y;
  2344. stbi_uc *p = z->out;
  2345. if (s->img_out_n == 3) { // convert bgr to rgb
  2346. for (i=0; i < pixel_count; ++i) {
  2347. stbi_uc t = p[0];
  2348. p[0] = p[2];
  2349. p[2] = t;
  2350. p += 3;
  2351. }
  2352. } else {
  2353. assert(s->img_out_n == 4);
  2354. if (stbi__unpremultiply_on_load) {
  2355. // convert bgr to rgb and unpremultiply
  2356. for (i=0; i < pixel_count; ++i) {
  2357. stbi_uc a = p[3];
  2358. stbi_uc t = p[0];
  2359. if (a) {
  2360. p[0] = p[2] * 255 / a;
  2361. p[1] = p[1] * 255 / a;
  2362. p[2] = t * 255 / a;
  2363. } else {
  2364. p[0] = p[2];
  2365. p[2] = t;
  2366. }
  2367. p += 4;
  2368. }
  2369. } else {
  2370. // convert bgr to rgb
  2371. for (i=0; i < pixel_count; ++i) {
  2372. stbi_uc t = p[0];
  2373. p[0] = p[2];
  2374. p[2] = t;
  2375. p += 4;
  2376. }
  2377. }
  2378. }
  2379. }
  2380. static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
  2381. {
  2382. stbi_uc palette[1024], pal_img_n=0;
  2383. stbi_uc has_trans=0, tc[3];
  2384. stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
  2385. int first=1,k,interlace=0, is_iphone=0;
  2386. stbi__context *s = z->s;
  2387. z->expanded = NULL;
  2388. z->idata = NULL;
  2389. z->out = NULL;
  2390. if (!stbi__check_png_header(s)) return 0;
  2391. if (scan == SCAN_type) return 1;
  2392. for (;;) {
  2393. stbi__pngchunk c = stbi__get_chunk_header(s);
  2394. switch (c.type) {
  2395. case PNG_TYPE('C','g','B','I'):
  2396. is_iphone = 1;
  2397. stbi__skip(s, c.length);
  2398. break;
  2399. case PNG_TYPE('I','H','D','R'): {
  2400. int depth,color,comp,filter;
  2401. if (!first) return stbi__err("multiple IHDR","Corrupt PNG");
  2402. first = 0;
  2403. if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");
  2404. s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
  2405. s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
  2406. depth = stbi__get8(s); if (depth != 8) return stbi__err("8bit only","PNG not supported: 8-bit only");
  2407. color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
  2408. if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
  2409. comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
  2410. filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
  2411. interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
  2412. if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG");
  2413. if (!pal_img_n) {
  2414. s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
  2415. if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to stbi__jpeg_huff_decode");
  2416. if (scan == SCAN_header) return 1;
  2417. } else {
  2418. // if paletted, then pal_n is our final components, and
  2419. // img_n is # components to decompress/filter.
  2420. s->img_n = 1;
  2421. if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
  2422. // if SCAN_header, have to scan to see if we have a tRNS
  2423. }
  2424. break;
  2425. }
  2426. case PNG_TYPE('P','L','T','E'): {
  2427. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  2428. if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG");
  2429. pal_len = c.length / 3;
  2430. if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG");
  2431. for (i=0; i < pal_len; ++i) {
  2432. palette[i*4+0] = stbi__get8(s);
  2433. palette[i*4+1] = stbi__get8(s);
  2434. palette[i*4+2] = stbi__get8(s);
  2435. palette[i*4+3] = 255;
  2436. }
  2437. break;
  2438. }
  2439. case PNG_TYPE('t','R','N','S'): {
  2440. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  2441. if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG");
  2442. if (pal_img_n) {
  2443. if (scan == SCAN_header) { s->img_n = 4; return 1; }
  2444. if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG");
  2445. if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG");
  2446. pal_img_n = 4;
  2447. for (i=0; i < c.length; ++i)
  2448. palette[i*4+3] = stbi__get8(s);
  2449. } else {
  2450. if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG");
  2451. if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG");
  2452. has_trans = 1;
  2453. for (k=0; k < s->img_n; ++k)
  2454. tc[k] = (stbi_uc) (stbi__get16be(s) & 255); // non 8-bit images will be larger
  2455. }
  2456. break;
  2457. }
  2458. case PNG_TYPE('I','D','A','T'): {
  2459. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  2460. if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG");
  2461. if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; }
  2462. if (ioff + c.length > idata_limit) {
  2463. stbi_uc *p;
  2464. if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
  2465. while (ioff + c.length > idata_limit)
  2466. idata_limit *= 2;
  2467. p = (stbi_uc *) realloc(z->idata, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
  2468. z->idata = p;
  2469. }
  2470. if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG");
  2471. ioff += c.length;
  2472. break;
  2473. }
  2474. case PNG_TYPE('I','E','N','D'): {
  2475. stbi__uint32 raw_len;
  2476. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  2477. if (scan != SCAN_load) return 1;
  2478. if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");
  2479. z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !is_iphone);
  2480. if (z->expanded == NULL) return 0; // zlib should set error
  2481. free(z->idata); z->idata = NULL;
  2482. if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
  2483. s->img_out_n = s->img_n+1;
  2484. else
  2485. s->img_out_n = s->img_n;
  2486. if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0;
  2487. if (has_trans)
  2488. if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
  2489. if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
  2490. stbi__de_iphone(z);
  2491. if (pal_img_n) {
  2492. // pal_img_n == 3 or 4
  2493. s->img_n = pal_img_n; // record the actual colors we had
  2494. s->img_out_n = pal_img_n;
  2495. if (req_comp >= 3) s->img_out_n = req_comp;
  2496. if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
  2497. return 0;
  2498. }
  2499. free(z->expanded); z->expanded = NULL;
  2500. return 1;
  2501. }
  2502. default:
  2503. // if critical, fail
  2504. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  2505. if ((c.type & (1 << 29)) == 0) {
  2506. #ifndef STBI_NO_FAILURE_STRINGS
  2507. // not threadsafe
  2508. static char invalid_chunk[] = "XXXX PNG chunk not known";
  2509. invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
  2510. invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
  2511. invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
  2512. invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
  2513. #endif
  2514. return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type");
  2515. }
  2516. stbi__skip(s, c.length);
  2517. break;
  2518. }
  2519. // end of PNG chunk, read and skip CRC
  2520. stbi__get32be(s);
  2521. }
  2522. }
  2523. static unsigned char *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp)
  2524. {
  2525. unsigned char *result=NULL;
  2526. if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
  2527. if (stbi__parse_png_file(p, SCAN_load, req_comp)) {
  2528. result = p->out;
  2529. p->out = NULL;
  2530. if (req_comp && req_comp != p->s->img_out_n) {
  2531. result = stbi__convert_format(result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
  2532. p->s->img_out_n = req_comp;
  2533. if (result == NULL) return result;
  2534. }
  2535. *x = p->s->img_x;
  2536. *y = p->s->img_y;
  2537. if (n) *n = p->s->img_n;
  2538. }
  2539. free(p->out); p->out = NULL;
  2540. free(p->expanded); p->expanded = NULL;
  2541. free(p->idata); p->idata = NULL;
  2542. return result;
  2543. }
  2544. static unsigned char *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  2545. {
  2546. stbi__png p;
  2547. p.s = s;
  2548. return stbi__do_png(&p, x,y,comp,req_comp);
  2549. }
  2550. static int stbi__png_test(stbi__context *s)
  2551. {
  2552. int r;
  2553. r = stbi__check_png_header(s);
  2554. stbi__rewind(s);
  2555. return r;
  2556. }
  2557. static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp)
  2558. {
  2559. if (!stbi__parse_png_file(p, SCAN_header, 0)) {
  2560. stbi__rewind( p->s );
  2561. return 0;
  2562. }
  2563. if (x) *x = p->s->img_x;
  2564. if (y) *y = p->s->img_y;
  2565. if (comp) *comp = p->s->img_n;
  2566. return 1;
  2567. }
  2568. static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp)
  2569. {
  2570. stbi__png p;
  2571. p.s = s;
  2572. return stbi__png_info_raw(&p, x, y, comp);
  2573. }
  2574. // Microsoft/Windows BMP image
  2575. static int stbi__bmp_test_raw(stbi__context *s)
  2576. {
  2577. int r;
  2578. int sz;
  2579. if (stbi__get8(s) != 'B') return 0;
  2580. if (stbi__get8(s) != 'M') return 0;
  2581. stbi__get32le(s); // discard filesize
  2582. stbi__get16le(s); // discard reserved
  2583. stbi__get16le(s); // discard reserved
  2584. stbi__get32le(s); // discard data offset
  2585. sz = stbi__get32le(s);
  2586. r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
  2587. return r;
  2588. }
  2589. static int stbi__bmp_test(stbi__context *s)
  2590. {
  2591. int r = stbi__bmp_test_raw(s);
  2592. stbi__rewind(s);
  2593. return r;
  2594. }
  2595. // returns 0..31 for the highest set bit
  2596. static int stbi__high_bit(unsigned int z)
  2597. {
  2598. int n=0;
  2599. if (z == 0) return -1;
  2600. if (z >= 0x10000) n += 16, z >>= 16;
  2601. if (z >= 0x00100) n += 8, z >>= 8;
  2602. if (z >= 0x00010) n += 4, z >>= 4;
  2603. if (z >= 0x00004) n += 2, z >>= 2;
  2604. if (z >= 0x00002) n += 1, z >>= 1;
  2605. return n;
  2606. }
  2607. static int stbi__bitcount(unsigned int a)
  2608. {
  2609. a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
  2610. a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
  2611. a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
  2612. a = (a + (a >> 8)); // max 16 per 8 bits
  2613. a = (a + (a >> 16)); // max 32 per 8 bits
  2614. return a & 0xff;
  2615. }
  2616. static int stbi__shiftsigned(int v, int shift, int bits)
  2617. {
  2618. int result;
  2619. int z=0;
  2620. if (shift < 0) v <<= -shift;
  2621. else v >>= shift;
  2622. result = v;
  2623. z = bits;
  2624. while (z < 8) {
  2625. result += v >> z;
  2626. z += bits;
  2627. }
  2628. return result;
  2629. }
  2630. static stbi_uc *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  2631. {
  2632. stbi_uc *out;
  2633. unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
  2634. stbi_uc pal[256][4];
  2635. int psize=0,i,j,compress=0,width;
  2636. int bpp, flip_vertically, pad, target, offset, hsz;
  2637. if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP");
  2638. stbi__get32le(s); // discard filesize
  2639. stbi__get16le(s); // discard reserved
  2640. stbi__get16le(s); // discard reserved
  2641. offset = stbi__get32le(s);
  2642. hsz = stbi__get32le(s);
  2643. if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown");
  2644. if (hsz == 12) {
  2645. s->img_x = stbi__get16le(s);
  2646. s->img_y = stbi__get16le(s);
  2647. } else {
  2648. s->img_x = stbi__get32le(s);
  2649. s->img_y = stbi__get32le(s);
  2650. }
  2651. if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP");
  2652. bpp = stbi__get16le(s);
  2653. if (bpp == 1) return stbi__errpuc("monochrome", "BMP type not supported: 1-bit");
  2654. flip_vertically = ((int) s->img_y) > 0;
  2655. s->img_y = abs((int) s->img_y);
  2656. if (hsz == 12) {
  2657. if (bpp < 24)
  2658. psize = (offset - 14 - 24) / 3;
  2659. } else {
  2660. compress = stbi__get32le(s);
  2661. if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");
  2662. stbi__get32le(s); // discard sizeof
  2663. stbi__get32le(s); // discard hres
  2664. stbi__get32le(s); // discard vres
  2665. stbi__get32le(s); // discard colorsused
  2666. stbi__get32le(s); // discard max important
  2667. if (hsz == 40 || hsz == 56) {
  2668. if (hsz == 56) {
  2669. stbi__get32le(s);
  2670. stbi__get32le(s);
  2671. stbi__get32le(s);
  2672. stbi__get32le(s);
  2673. }
  2674. if (bpp == 16 || bpp == 32) {
  2675. mr = mg = mb = 0;
  2676. if (compress == 0) {
  2677. if (bpp == 32) {
  2678. mr = 0xffu << 16;
  2679. mg = 0xffu << 8;
  2680. mb = 0xffu << 0;
  2681. ma = 0xffu << 24;
  2682. fake_a = 1; // @TODO: check for cases like alpha value is all 0 and switch it to 255
  2683. STBI_NOTUSED(fake_a);
  2684. } else {
  2685. mr = 31u << 10;
  2686. mg = 31u << 5;
  2687. mb = 31u << 0;
  2688. }
  2689. } else if (compress == 3) {
  2690. mr = stbi__get32le(s);
  2691. mg = stbi__get32le(s);
  2692. mb = stbi__get32le(s);
  2693. // not documented, but generated by photoshop and handled by mspaint
  2694. if (mr == mg && mg == mb) {
  2695. // ?!?!?
  2696. return stbi__errpuc("bad BMP", "bad BMP");
  2697. }
  2698. } else
  2699. return stbi__errpuc("bad BMP", "bad BMP");
  2700. }
  2701. } else {
  2702. assert(hsz == 108 || hsz == 124);
  2703. mr = stbi__get32le(s);
  2704. mg = stbi__get32le(s);
  2705. mb = stbi__get32le(s);
  2706. ma = stbi__get32le(s);
  2707. stbi__get32le(s); // discard color space
  2708. for (i=0; i < 12; ++i)
  2709. stbi__get32le(s); // discard color space parameters
  2710. if (hsz == 124) {
  2711. stbi__get32le(s); // discard rendering intent
  2712. stbi__get32le(s); // discard offset of profile data
  2713. stbi__get32le(s); // discard size of profile data
  2714. stbi__get32le(s); // discard reserved
  2715. }
  2716. }
  2717. if (bpp < 16)
  2718. psize = (offset - 14 - hsz) >> 2;
  2719. }
  2720. s->img_n = ma ? 4 : 3;
  2721. if (req_comp && req_comp >= 3) // we can directly stbi__jpeg_huff_decode 3 or 4
  2722. target = req_comp;
  2723. else
  2724. target = s->img_n; // if they want monochrome, we'll post-convert
  2725. out = (stbi_uc *) malloc(target * s->img_x * s->img_y);
  2726. if (!out) return stbi__errpuc("outofmem", "Out of memory");
  2727. if (bpp < 16) {
  2728. int z=0;
  2729. if (psize == 0 || psize > 256) { free(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
  2730. for (i=0; i < psize; ++i) {
  2731. pal[i][2] = stbi__get8(s);
  2732. pal[i][1] = stbi__get8(s);
  2733. pal[i][0] = stbi__get8(s);
  2734. if (hsz != 12) stbi__get8(s);
  2735. pal[i][3] = 255;
  2736. }
  2737. stbi__skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
  2738. if (bpp == 4) width = (s->img_x + 1) >> 1;
  2739. else if (bpp == 8) width = s->img_x;
  2740. else { free(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }
  2741. pad = (-width)&3;
  2742. for (j=0; j < (int) s->img_y; ++j) {
  2743. for (i=0; i < (int) s->img_x; i += 2) {
  2744. int v=stbi__get8(s),v2=0;
  2745. if (bpp == 4) {
  2746. v2 = v & 15;
  2747. v >>= 4;
  2748. }
  2749. out[z++] = pal[v][0];
  2750. out[z++] = pal[v][1];
  2751. out[z++] = pal[v][2];
  2752. if (target == 4) out[z++] = 255;
  2753. if (i+1 == (int) s->img_x) break;
  2754. v = (bpp == 8) ? stbi__get8(s) : v2;
  2755. out[z++] = pal[v][0];
  2756. out[z++] = pal[v][1];
  2757. out[z++] = pal[v][2];
  2758. if (target == 4) out[z++] = 255;
  2759. }
  2760. stbi__skip(s, pad);
  2761. }
  2762. } else {
  2763. int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
  2764. int z = 0;
  2765. int easy=0;
  2766. stbi__skip(s, offset - 14 - hsz);
  2767. if (bpp == 24) width = 3 * s->img_x;
  2768. else if (bpp == 16) width = 2*s->img_x;
  2769. else /* bpp = 32 and pad = 0 */ width=0;
  2770. pad = (-width) & 3;
  2771. if (bpp == 24) {
  2772. easy = 1;
  2773. } else if (bpp == 32) {
  2774. if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
  2775. easy = 2;
  2776. }
  2777. if (!easy) {
  2778. if (!mr || !mg || !mb) { free(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
  2779. // right shift amt to put high bit in position #7
  2780. rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);
  2781. gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
  2782. bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
  2783. ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
  2784. }
  2785. for (j=0; j < (int) s->img_y; ++j) {
  2786. if (easy) {
  2787. for (i=0; i < (int) s->img_x; ++i) {
  2788. unsigned char a;
  2789. out[z+2] = stbi__get8(s);
  2790. out[z+1] = stbi__get8(s);
  2791. out[z+0] = stbi__get8(s);
  2792. z += 3;
  2793. a = (easy == 2 ? stbi__get8(s) : 255);
  2794. if (target == 4) out[z++] = a;
  2795. }
  2796. } else {
  2797. for (i=0; i < (int) s->img_x; ++i) {
  2798. stbi__uint32 v = (stbi__uint32) (bpp == 16 ? stbi__get16le(s) : stbi__get32le(s));
  2799. int a;
  2800. out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount));
  2801. out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount));
  2802. out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
  2803. a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
  2804. if (target == 4) out[z++] = STBI__BYTECAST(a);
  2805. }
  2806. }
  2807. stbi__skip(s, pad);
  2808. }
  2809. }
  2810. if (flip_vertically) {
  2811. stbi_uc t;
  2812. for (j=0; j < (int) s->img_y>>1; ++j) {
  2813. stbi_uc *p1 = out + j *s->img_x*target;
  2814. stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
  2815. for (i=0; i < (int) s->img_x*target; ++i) {
  2816. t = p1[i], p1[i] = p2[i], p2[i] = t;
  2817. }
  2818. }
  2819. }
  2820. if (req_comp && req_comp != target) {
  2821. out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
  2822. if (out == NULL) return out; // stbi__convert_format frees input on failure
  2823. }
  2824. *x = s->img_x;
  2825. *y = s->img_y;
  2826. if (comp) *comp = s->img_n;
  2827. return out;
  2828. }
  2829. // Targa Truevision - TGA
  2830. // by Jonathan Dummer
  2831. static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp)
  2832. {
  2833. int tga_w, tga_h, tga_comp;
  2834. int sz;
  2835. stbi__get8(s); // discard Offset
  2836. sz = stbi__get8(s); // color type
  2837. if( sz > 1 ) {
  2838. stbi__rewind(s);
  2839. return 0; // only RGB or indexed allowed
  2840. }
  2841. sz = stbi__get8(s); // image type
  2842. // only RGB or grey allowed, +/- RLE
  2843. if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11)) return 0;
  2844. stbi__skip(s,9);
  2845. tga_w = stbi__get16le(s);
  2846. if( tga_w < 1 ) {
  2847. stbi__rewind(s);
  2848. return 0; // test width
  2849. }
  2850. tga_h = stbi__get16le(s);
  2851. if( tga_h < 1 ) {
  2852. stbi__rewind(s);
  2853. return 0; // test height
  2854. }
  2855. sz = stbi__get8(s); // bits per pixel
  2856. // only RGB or RGBA or grey allowed
  2857. if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) {
  2858. stbi__rewind(s);
  2859. return 0;
  2860. }
  2861. tga_comp = sz;
  2862. if (x) *x = tga_w;
  2863. if (y) *y = tga_h;
  2864. if (comp) *comp = tga_comp / 8;
  2865. return 1; // seems to have passed everything
  2866. }
  2867. static int stbi__tga_test(stbi__context *s)
  2868. {
  2869. int res;
  2870. int sz;
  2871. stbi__get8(s); // discard Offset
  2872. sz = stbi__get8(s); // color type
  2873. if ( sz > 1 ) return 0; // only RGB or indexed allowed
  2874. sz = stbi__get8(s); // image type
  2875. if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0; // only RGB or grey allowed, +/- RLE
  2876. stbi__get16be(s); // discard palette start
  2877. stbi__get16be(s); // discard palette length
  2878. stbi__get8(s); // discard bits per palette color entry
  2879. stbi__get16be(s); // discard x origin
  2880. stbi__get16be(s); // discard y origin
  2881. if ( stbi__get16be(s) < 1 ) return 0; // test width
  2882. if ( stbi__get16be(s) < 1 ) return 0; // test height
  2883. sz = stbi__get8(s); // bits per pixel
  2884. if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) )
  2885. res = 0;
  2886. else
  2887. res = 1;
  2888. stbi__rewind(s);
  2889. return res;
  2890. }
  2891. static stbi_uc *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  2892. {
  2893. // read in the TGA header stuff
  2894. int tga_offset = stbi__get8(s);
  2895. int tga_indexed = stbi__get8(s);
  2896. int tga_image_type = stbi__get8(s);
  2897. int tga_is_RLE = 0;
  2898. int tga_palette_start = stbi__get16le(s);
  2899. int tga_palette_len = stbi__get16le(s);
  2900. int tga_palette_bits = stbi__get8(s);
  2901. int tga_x_origin = stbi__get16le(s);
  2902. int tga_y_origin = stbi__get16le(s);
  2903. int tga_width = stbi__get16le(s);
  2904. int tga_height = stbi__get16le(s);
  2905. int tga_bits_per_pixel = stbi__get8(s);
  2906. int tga_comp = tga_bits_per_pixel / 8;
  2907. int tga_inverted = stbi__get8(s);
  2908. // image data
  2909. unsigned char *tga_data;
  2910. unsigned char *tga_palette = NULL;
  2911. int i, j;
  2912. unsigned char raw_data[4];
  2913. int RLE_count = 0;
  2914. int RLE_repeating = 0;
  2915. int read_next_pixel = 1;
  2916. // do a tiny bit of precessing
  2917. if ( tga_image_type >= 8 )
  2918. {
  2919. tga_image_type -= 8;
  2920. tga_is_RLE = 1;
  2921. }
  2922. /* int tga_alpha_bits = tga_inverted & 15; */
  2923. tga_inverted = 1 - ((tga_inverted >> 5) & 1);
  2924. // error check
  2925. if ( //(tga_indexed) ||
  2926. (tga_width < 1) || (tga_height < 1) ||
  2927. (tga_image_type < 1) || (tga_image_type > 3) ||
  2928. ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
  2929. (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
  2930. )
  2931. {
  2932. return NULL; // we don't report this as a bad TGA because we don't even know if it's TGA
  2933. }
  2934. // If I'm paletted, then I'll use the number of bits from the palette
  2935. if ( tga_indexed )
  2936. {
  2937. tga_comp = tga_palette_bits / 8;
  2938. }
  2939. // tga info
  2940. *x = tga_width;
  2941. *y = tga_height;
  2942. if (comp) *comp = tga_comp;
  2943. tga_data = (unsigned char*)malloc( tga_width * tga_height * tga_comp );
  2944. if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");
  2945. // skip to the data's starting position (offset usually = 0)
  2946. stbi__skip(s, tga_offset );
  2947. if ( !tga_indexed && !tga_is_RLE) {
  2948. for (i=0; i < tga_height; ++i) {
  2949. int y = tga_inverted ? tga_height -i - 1 : i;
  2950. stbi_uc *tga_row = tga_data + y*tga_width*tga_comp;
  2951. stbi__getn(s, tga_row, tga_width * tga_comp);
  2952. }
  2953. } else {
  2954. // do I need to load a palette?
  2955. if ( tga_indexed)
  2956. {
  2957. // any data to skip? (offset usually = 0)
  2958. stbi__skip(s, tga_palette_start );
  2959. // load the palette
  2960. tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
  2961. if (!tga_palette) {
  2962. free(tga_data);
  2963. return stbi__errpuc("outofmem", "Out of memory");
  2964. }
  2965. if (!stbi__getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {
  2966. free(tga_data);
  2967. free(tga_palette);
  2968. return stbi__errpuc("bad palette", "Corrupt TGA");
  2969. }
  2970. }
  2971. // load the data
  2972. for (i=0; i < tga_width * tga_height; ++i)
  2973. {
  2974. // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk?
  2975. if ( tga_is_RLE )
  2976. {
  2977. if ( RLE_count == 0 )
  2978. {
  2979. // yep, get the next byte as a RLE command
  2980. int RLE_cmd = stbi__get8(s);
  2981. RLE_count = 1 + (RLE_cmd & 127);
  2982. RLE_repeating = RLE_cmd >> 7;
  2983. read_next_pixel = 1;
  2984. } else if ( !RLE_repeating )
  2985. {
  2986. read_next_pixel = 1;
  2987. }
  2988. } else
  2989. {
  2990. read_next_pixel = 1;
  2991. }
  2992. // OK, if I need to read a pixel, do it now
  2993. if ( read_next_pixel )
  2994. {
  2995. // load however much data we did have
  2996. if ( tga_indexed )
  2997. {
  2998. // read in 1 byte, then perform the lookup
  2999. int pal_idx = stbi__get8(s);
  3000. if ( pal_idx >= tga_palette_len )
  3001. {
  3002. // invalid index
  3003. pal_idx = 0;
  3004. }
  3005. pal_idx *= tga_bits_per_pixel / 8;
  3006. for (j = 0; j*8 < tga_bits_per_pixel; ++j)
  3007. {
  3008. raw_data[j] = tga_palette[pal_idx+j];
  3009. }
  3010. } else
  3011. {
  3012. // read in the data raw
  3013. for (j = 0; j*8 < tga_bits_per_pixel; ++j)
  3014. {
  3015. raw_data[j] = stbi__get8(s);
  3016. }
  3017. }
  3018. // clear the reading flag for the next pixel
  3019. read_next_pixel = 0;
  3020. } // end of reading a pixel
  3021. // copy data
  3022. for (j = 0; j < tga_comp; ++j)
  3023. tga_data[i*tga_comp+j] = raw_data[j];
  3024. // in case we're in RLE mode, keep counting down
  3025. --RLE_count;
  3026. }
  3027. // do I need to invert the image?
  3028. if ( tga_inverted )
  3029. {
  3030. for (j = 0; j*2 < tga_height; ++j)
  3031. {
  3032. int index1 = j * tga_width * tga_comp;
  3033. int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
  3034. for (i = tga_width * tga_comp; i > 0; --i)
  3035. {
  3036. unsigned char temp = tga_data[index1];
  3037. tga_data[index1] = tga_data[index2];
  3038. tga_data[index2] = temp;
  3039. ++index1;
  3040. ++index2;
  3041. }
  3042. }
  3043. }
  3044. // clear my palette, if I had one
  3045. if ( tga_palette != NULL )
  3046. {
  3047. free( tga_palette );
  3048. }
  3049. }
  3050. // swap RGB
  3051. if (tga_comp >= 3)
  3052. {
  3053. unsigned char* tga_pixel = tga_data;
  3054. for (i=0; i < tga_width * tga_height; ++i)
  3055. {
  3056. unsigned char temp = tga_pixel[0];
  3057. tga_pixel[0] = tga_pixel[2];
  3058. tga_pixel[2] = temp;
  3059. tga_pixel += tga_comp;
  3060. }
  3061. }
  3062. // convert to target component count
  3063. if (req_comp && req_comp != tga_comp)
  3064. tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
  3065. // the things I do to get rid of an error message, and yet keep
  3066. // Microsoft's C compilers happy... [8^(
  3067. tga_palette_start = tga_palette_len = tga_palette_bits =
  3068. tga_x_origin = tga_y_origin = 0;
  3069. // OK, done
  3070. return tga_data;
  3071. }
  3072. // *************************************************************************************************
  3073. // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
  3074. static int stbi__psd_test(stbi__context *s)
  3075. {
  3076. int r = (stbi__get32be(s) == 0x38425053);
  3077. stbi__rewind(s);
  3078. return r;
  3079. }
  3080. static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  3081. {
  3082. int pixelCount;
  3083. int channelCount, compression;
  3084. int channel, i, count, len;
  3085. int w,h;
  3086. stbi_uc *out;
  3087. // Check identifier
  3088. if (stbi__get32be(s) != 0x38425053) // "8BPS"
  3089. return stbi__errpuc("not PSD", "Corrupt PSD image");
  3090. // Check file type version.
  3091. if (stbi__get16be(s) != 1)
  3092. return stbi__errpuc("wrong version", "Unsupported version of PSD image");
  3093. // Skip 6 reserved bytes.
  3094. stbi__skip(s, 6 );
  3095. // Read the number of channels (R, G, B, A, etc).
  3096. channelCount = stbi__get16be(s);
  3097. if (channelCount < 0 || channelCount > 16)
  3098. return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image");
  3099. // Read the rows and columns of the image.
  3100. h = stbi__get32be(s);
  3101. w = stbi__get32be(s);
  3102. // Make sure the depth is 8 bits.
  3103. if (stbi__get16be(s) != 8)
  3104. return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 bit");
  3105. // Make sure the color mode is RGB.
  3106. // Valid options are:
  3107. // 0: Bitmap
  3108. // 1: Grayscale
  3109. // 2: Indexed color
  3110. // 3: RGB color
  3111. // 4: CMYK color
  3112. // 7: Multichannel
  3113. // 8: Duotone
  3114. // 9: Lab color
  3115. if (stbi__get16be(s) != 3)
  3116. return stbi__errpuc("wrong color format", "PSD is not in RGB color format");
  3117. // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.)
  3118. stbi__skip(s,stbi__get32be(s) );
  3119. // Skip the image resources. (resolution, pen tool paths, etc)
  3120. stbi__skip(s, stbi__get32be(s) );
  3121. // Skip the reserved data.
  3122. stbi__skip(s, stbi__get32be(s) );
  3123. // Find out if the data is compressed.
  3124. // Known values:
  3125. // 0: no compression
  3126. // 1: RLE compressed
  3127. compression = stbi__get16be(s);
  3128. if (compression > 1)
  3129. return stbi__errpuc("bad compression", "PSD has an unknown compression format");
  3130. // Create the destination image.
  3131. out = (stbi_uc *) malloc(4 * w*h);
  3132. if (!out) return stbi__errpuc("outofmem", "Out of memory");
  3133. pixelCount = w*h;
  3134. // Initialize the data to zero.
  3135. //memset( out, 0, pixelCount * 4 );
  3136. // Finally, the image data.
  3137. if (compression) {
  3138. // RLE as used by .PSD and .TIFF
  3139. // Loop until you get the number of unpacked bytes you are expecting:
  3140. // Read the next source byte into n.
  3141. // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
  3142. // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
  3143. // Else if n is 128, noop.
  3144. // Endloop
  3145. // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
  3146. // which we're going to just skip.
  3147. stbi__skip(s, h * channelCount * 2 );
  3148. // Read the RLE data by channel.
  3149. for (channel = 0; channel < 4; channel++) {
  3150. stbi_uc *p;
  3151. p = out+channel;
  3152. if (channel >= channelCount) {
  3153. // Fill this channel with default data.
  3154. for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
  3155. } else {
  3156. // Read the RLE data.
  3157. count = 0;
  3158. while (count < pixelCount) {
  3159. len = stbi__get8(s);
  3160. if (len == 128) {
  3161. // No-op.
  3162. } else if (len < 128) {
  3163. // Copy next len+1 bytes literally.
  3164. len++;
  3165. count += len;
  3166. while (len) {
  3167. *p = stbi__get8(s);
  3168. p += 4;
  3169. len--;
  3170. }
  3171. } else if (len > 128) {
  3172. stbi_uc val;
  3173. // Next -len+1 bytes in the dest are replicated from next source byte.
  3174. // (Interpret len as a negative 8-bit int.)
  3175. len ^= 0x0FF;
  3176. len += 2;
  3177. val = stbi__get8(s);
  3178. count += len;
  3179. while (len) {
  3180. *p = val;
  3181. p += 4;
  3182. len--;
  3183. }
  3184. }
  3185. }
  3186. }
  3187. }
  3188. } else {
  3189. // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
  3190. // where each channel consists of an 8-bit value for each pixel in the image.
  3191. // Read the data by channel.
  3192. for (channel = 0; channel < 4; channel++) {
  3193. stbi_uc *p;
  3194. p = out + channel;
  3195. if (channel > channelCount) {
  3196. // Fill this channel with default data.
  3197. for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
  3198. } else {
  3199. // Read the data.
  3200. for (i = 0; i < pixelCount; i++)
  3201. *p = stbi__get8(s), p += 4;
  3202. }
  3203. }
  3204. }
  3205. if (req_comp && req_comp != 4) {
  3206. out = stbi__convert_format(out, 4, req_comp, w, h);
  3207. if (out == NULL) return out; // stbi__convert_format frees input on failure
  3208. }
  3209. if (comp) *comp = channelCount;
  3210. *y = h;
  3211. *x = w;
  3212. return out;
  3213. }
  3214. // *************************************************************************************************
  3215. // Softimage PIC loader
  3216. // by Tom Seddon
  3217. //
  3218. // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
  3219. // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
  3220. static int stbi__pic_is4(stbi__context *s,const char *str)
  3221. {
  3222. int i;
  3223. for (i=0; i<4; ++i)
  3224. if (stbi__get8(s) != (stbi_uc)str[i])
  3225. return 0;
  3226. return 1;
  3227. }
  3228. static int stbi__pic_test_core(stbi__context *s)
  3229. {
  3230. int i;
  3231. if (!stbi__pic_is4(s,"\x53\x80\xF6\x34"))
  3232. return 0;
  3233. for(i=0;i<84;++i)
  3234. stbi__get8(s);
  3235. if (!stbi__pic_is4(s,"PICT"))
  3236. return 0;
  3237. return 1;
  3238. }
  3239. typedef struct
  3240. {
  3241. stbi_uc size,type,channel;
  3242. } stbi__pic_packet;
  3243. static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest)
  3244. {
  3245. int mask=0x80, i;
  3246. for (i=0; i<4; ++i, mask>>=1) {
  3247. if (channel & mask) {
  3248. if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short");
  3249. dest[i]=stbi__get8(s);
  3250. }
  3251. }
  3252. return dest;
  3253. }
  3254. static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src)
  3255. {
  3256. int mask=0x80,i;
  3257. for (i=0;i<4; ++i, mask>>=1)
  3258. if (channel&mask)
  3259. dest[i]=src[i];
  3260. }
  3261. static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
  3262. {
  3263. int act_comp=0,num_packets=0,y,chained;
  3264. stbi__pic_packet packets[10];
  3265. // this will (should...) cater for even some bizarre stuff like having data
  3266. // for the same channel in multiple packets.
  3267. do {
  3268. stbi__pic_packet *packet;
  3269. if (num_packets==sizeof(packets)/sizeof(packets[0]))
  3270. return stbi__errpuc("bad format","too many packets");
  3271. packet = &packets[num_packets++];
  3272. chained = stbi__get8(s);
  3273. packet->size = stbi__get8(s);
  3274. packet->type = stbi__get8(s);
  3275. packet->channel = stbi__get8(s);
  3276. act_comp |= packet->channel;
  3277. if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)");
  3278. if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp");
  3279. } while (chained);
  3280. *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
  3281. for(y=0; y<height; ++y) {
  3282. int packet_idx;
  3283. for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
  3284. stbi__pic_packet *packet = &packets[packet_idx];
  3285. stbi_uc *dest = result+y*width*4;
  3286. switch (packet->type) {
  3287. default:
  3288. return stbi__errpuc("bad format","packet has bad compression type");
  3289. case 0: {//uncompressed
  3290. int x;
  3291. for(x=0;x<width;++x, dest+=4)
  3292. if (!stbi__readval(s,packet->channel,dest))
  3293. return 0;
  3294. break;
  3295. }
  3296. case 1://Pure RLE
  3297. {
  3298. int left=width, i;
  3299. while (left>0) {
  3300. stbi_uc count,value[4];
  3301. count=stbi__get8(s);
  3302. if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)");
  3303. if (count > left)
  3304. count = (stbi_uc) left;
  3305. if (!stbi__readval(s,packet->channel,value)) return 0;
  3306. for(i=0; i<count; ++i,dest+=4)
  3307. stbi__copyval(packet->channel,dest,value);
  3308. left -= count;
  3309. }
  3310. }
  3311. break;
  3312. case 2: {//Mixed RLE
  3313. int left=width;
  3314. while (left>0) {
  3315. int count = stbi__get8(s), i;
  3316. if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)");
  3317. if (count >= 128) { // Repeated
  3318. stbi_uc value[4];
  3319. int i;
  3320. if (count==128)
  3321. count = stbi__get16be(s);
  3322. else
  3323. count -= 127;
  3324. if (count > left)
  3325. return stbi__errpuc("bad file","scanline overrun");
  3326. if (!stbi__readval(s,packet->channel,value))
  3327. return 0;
  3328. for(i=0;i<count;++i, dest += 4)
  3329. stbi__copyval(packet->channel,dest,value);
  3330. } else { // Raw
  3331. ++count;
  3332. if (count>left) return stbi__errpuc("bad file","scanline overrun");
  3333. for(i=0;i<count;++i, dest+=4)
  3334. if (!stbi__readval(s,packet->channel,dest))
  3335. return 0;
  3336. }
  3337. left-=count;
  3338. }
  3339. break;
  3340. }
  3341. }
  3342. }
  3343. }
  3344. return result;
  3345. }
  3346. static stbi_uc *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp)
  3347. {
  3348. stbi_uc *result;
  3349. int i, x,y;
  3350. for (i=0; i<92; ++i)
  3351. stbi__get8(s);
  3352. x = stbi__get16be(s);
  3353. y = stbi__get16be(s);
  3354. if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)");
  3355. if ((1 << 28) / x < y) return stbi__errpuc("too large", "Image too large to stbi__jpeg_huff_decode");
  3356. stbi__get32be(s); //skip `ratio'
  3357. stbi__get16be(s); //skip `fields'
  3358. stbi__get16be(s); //skip `pad'
  3359. // intermediate buffer is RGBA
  3360. result = (stbi_uc *) malloc(x*y*4);
  3361. memset(result, 0xff, x*y*4);
  3362. if (!stbi__pic_load_core(s,x,y,comp, result)) {
  3363. free(result);
  3364. result=0;
  3365. }
  3366. *px = x;
  3367. *py = y;
  3368. if (req_comp == 0) req_comp = *comp;
  3369. result=stbi__convert_format(result,4,req_comp,x,y);
  3370. return result;
  3371. }
  3372. static int stbi__pic_test(stbi__context *s)
  3373. {
  3374. int r = stbi__pic_test_core(s);
  3375. stbi__rewind(s);
  3376. return r;
  3377. }
  3378. // *************************************************************************************************
  3379. // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
  3380. typedef struct
  3381. {
  3382. stbi__int16 prefix;
  3383. stbi_uc first;
  3384. stbi_uc suffix;
  3385. } stbi__gif_lzw;
  3386. typedef struct
  3387. {
  3388. int w,h;
  3389. stbi_uc *out; // output buffer (always 4 components)
  3390. int flags, bgindex, ratio, transparent, eflags;
  3391. stbi_uc pal[256][4];
  3392. stbi_uc lpal[256][4];
  3393. stbi__gif_lzw codes[4096];
  3394. stbi_uc *color_table;
  3395. int parse, step;
  3396. int lflags;
  3397. int start_x, start_y;
  3398. int max_x, max_y;
  3399. int cur_x, cur_y;
  3400. int line_size;
  3401. } stbi__gif;
  3402. static int stbi__gif_test_raw(stbi__context *s)
  3403. {
  3404. int sz;
  3405. if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0;
  3406. sz = stbi__get8(s);
  3407. if (sz != '9' && sz != '7') return 0;
  3408. if (stbi__get8(s) != 'a') return 0;
  3409. return 1;
  3410. }
  3411. static int stbi__gif_test(stbi__context *s)
  3412. {
  3413. int r = stbi__gif_test_raw(s);
  3414. stbi__rewind(s);
  3415. return r;
  3416. }
  3417. static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp)
  3418. {
  3419. int i;
  3420. for (i=0; i < num_entries; ++i) {
  3421. pal[i][2] = stbi__get8(s);
  3422. pal[i][1] = stbi__get8(s);
  3423. pal[i][0] = stbi__get8(s);
  3424. pal[i][3] = transp ? 0 : 255;
  3425. }
  3426. }
  3427. static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info)
  3428. {
  3429. stbi_uc version;
  3430. if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8')
  3431. return stbi__err("not GIF", "Corrupt GIF");
  3432. version = stbi__get8(s);
  3433. if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF");
  3434. if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF");
  3435. stbi__g_failure_reason = "";
  3436. g->w = stbi__get16le(s);
  3437. g->h = stbi__get16le(s);
  3438. g->flags = stbi__get8(s);
  3439. g->bgindex = stbi__get8(s);
  3440. g->ratio = stbi__get8(s);
  3441. g->transparent = -1;
  3442. if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
  3443. if (is_info) return 1;
  3444. if (g->flags & 0x80)
  3445. stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
  3446. return 1;
  3447. }
  3448. static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
  3449. {
  3450. stbi__gif g;
  3451. if (!stbi__gif_header(s, &g, comp, 1)) {
  3452. stbi__rewind( s );
  3453. return 0;
  3454. }
  3455. if (x) *x = g.w;
  3456. if (y) *y = g.h;
  3457. return 1;
  3458. }
  3459. static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
  3460. {
  3461. stbi_uc *p, *c;
  3462. // recurse to stbi__jpeg_huff_decode the prefixes, since the linked-list is backwards,
  3463. // and working backwards through an interleaved image would be nasty
  3464. if (g->codes[code].prefix >= 0)
  3465. stbi__out_gif_code(g, g->codes[code].prefix);
  3466. if (g->cur_y >= g->max_y) return;
  3467. p = &g->out[g->cur_x + g->cur_y];
  3468. c = &g->color_table[g->codes[code].suffix * 4];
  3469. if (c[3] >= 128) {
  3470. p[0] = c[2];
  3471. p[1] = c[1];
  3472. p[2] = c[0];
  3473. p[3] = c[3];
  3474. }
  3475. g->cur_x += 4;
  3476. if (g->cur_x >= g->max_x) {
  3477. g->cur_x = g->start_x;
  3478. g->cur_y += g->step;
  3479. while (g->cur_y >= g->max_y && g->parse > 0) {
  3480. g->step = (1 << g->parse) * g->line_size;
  3481. g->cur_y = g->start_y + (g->step >> 1);
  3482. --g->parse;
  3483. }
  3484. }
  3485. }
  3486. static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
  3487. {
  3488. stbi_uc lzw_cs;
  3489. stbi__int32 len, code;
  3490. stbi__uint32 first;
  3491. stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
  3492. stbi__gif_lzw *p;
  3493. lzw_cs = stbi__get8(s);
  3494. clear = 1 << lzw_cs;
  3495. first = 1;
  3496. codesize = lzw_cs + 1;
  3497. codemask = (1 << codesize) - 1;
  3498. bits = 0;
  3499. valid_bits = 0;
  3500. for (code = 0; code < clear; code++) {
  3501. g->codes[code].prefix = -1;
  3502. g->codes[code].first = (stbi_uc) code;
  3503. g->codes[code].suffix = (stbi_uc) code;
  3504. }
  3505. // support no starting clear code
  3506. avail = clear+2;
  3507. oldcode = -1;
  3508. len = 0;
  3509. for(;;) {
  3510. if (valid_bits < codesize) {
  3511. if (len == 0) {
  3512. len = stbi__get8(s); // start new block
  3513. if (len == 0)
  3514. return g->out;
  3515. }
  3516. --len;
  3517. bits |= (stbi__int32) stbi__get8(s) << valid_bits;
  3518. valid_bits += 8;
  3519. } else {
  3520. stbi__int32 code = bits & codemask;
  3521. bits >>= codesize;
  3522. valid_bits -= codesize;
  3523. // @OPTIMIZE: is there some way we can accelerate the non-clear path?
  3524. if (code == clear) { // clear code
  3525. codesize = lzw_cs + 1;
  3526. codemask = (1 << codesize) - 1;
  3527. avail = clear + 2;
  3528. oldcode = -1;
  3529. first = 0;
  3530. } else if (code == clear + 1) { // end of stream code
  3531. stbi__skip(s, len);
  3532. while ((len = stbi__get8(s)) > 0)
  3533. stbi__skip(s,len);
  3534. return g->out;
  3535. } else if (code <= avail) {
  3536. if (first) return stbi__errpuc("no clear code", "Corrupt GIF");
  3537. if (oldcode >= 0) {
  3538. p = &g->codes[avail++];
  3539. if (avail > 4096) return stbi__errpuc("too many codes", "Corrupt GIF");
  3540. p->prefix = (stbi__int16) oldcode;
  3541. p->first = g->codes[oldcode].first;
  3542. p->suffix = (code == avail) ? p->first : g->codes[code].first;
  3543. } else if (code == avail)
  3544. return stbi__errpuc("illegal code in raster", "Corrupt GIF");
  3545. stbi__out_gif_code(g, (stbi__uint16) code);
  3546. if ((avail & codemask) == 0 && avail <= 0x0FFF) {
  3547. codesize++;
  3548. codemask = (1 << codesize) - 1;
  3549. }
  3550. oldcode = code;
  3551. } else {
  3552. return stbi__errpuc("illegal code in raster", "Corrupt GIF");
  3553. }
  3554. }
  3555. }
  3556. }
  3557. static void stbi__fill_gif_background(stbi__gif *g)
  3558. {
  3559. int i;
  3560. stbi_uc *c = g->pal[g->bgindex];
  3561. // @OPTIMIZE: write a dword at a time
  3562. for (i = 0; i < g->w * g->h * 4; i += 4) {
  3563. stbi_uc *p = &g->out[i];
  3564. p[0] = c[2];
  3565. p[1] = c[1];
  3566. p[2] = c[0];
  3567. p[3] = c[3];
  3568. }
  3569. }
  3570. // this function is designed to support animated gifs, although stb_image doesn't support it
  3571. static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp)
  3572. {
  3573. int i;
  3574. stbi_uc *old_out = 0;
  3575. if (g->out == 0) {
  3576. if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
  3577. g->out = (stbi_uc *) malloc(4 * g->w * g->h);
  3578. if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");
  3579. stbi__fill_gif_background(g);
  3580. } else {
  3581. // animated-gif-only path
  3582. if (((g->eflags & 0x1C) >> 2) == 3) {
  3583. old_out = g->out;
  3584. g->out = (stbi_uc *) malloc(4 * g->w * g->h);
  3585. if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");
  3586. memcpy(g->out, old_out, g->w*g->h*4);
  3587. }
  3588. }
  3589. for (;;) {
  3590. switch (stbi__get8(s)) {
  3591. case 0x2C: /* Image Descriptor */
  3592. {
  3593. stbi__int32 x, y, w, h;
  3594. stbi_uc *o;
  3595. x = stbi__get16le(s);
  3596. y = stbi__get16le(s);
  3597. w = stbi__get16le(s);
  3598. h = stbi__get16le(s);
  3599. if (((x + w) > (g->w)) || ((y + h) > (g->h)))
  3600. return stbi__errpuc("bad Image Descriptor", "Corrupt GIF");
  3601. g->line_size = g->w * 4;
  3602. g->start_x = x * 4;
  3603. g->start_y = y * g->line_size;
  3604. g->max_x = g->start_x + w * 4;
  3605. g->max_y = g->start_y + h * g->line_size;
  3606. g->cur_x = g->start_x;
  3607. g->cur_y = g->start_y;
  3608. g->lflags = stbi__get8(s);
  3609. if (g->lflags & 0x40) {
  3610. g->step = 8 * g->line_size; // first interlaced spacing
  3611. g->parse = 3;
  3612. } else {
  3613. g->step = g->line_size;
  3614. g->parse = 0;
  3615. }
  3616. if (g->lflags & 0x80) {
  3617. stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
  3618. g->color_table = (stbi_uc *) g->lpal;
  3619. } else if (g->flags & 0x80) {
  3620. for (i=0; i < 256; ++i) // @OPTIMIZE: stbi__jpeg_reset only the previous transparent
  3621. g->pal[i][3] = 255;
  3622. if (g->transparent >= 0 && (g->eflags & 0x01))
  3623. g->pal[g->transparent][3] = 0;
  3624. g->color_table = (stbi_uc *) g->pal;
  3625. } else
  3626. return stbi__errpuc("missing color table", "Corrupt GIF");
  3627. o = stbi__process_gif_raster(s, g);
  3628. if (o == NULL) return NULL;
  3629. if (req_comp && req_comp != 4)
  3630. o = stbi__convert_format(o, 4, req_comp, g->w, g->h);
  3631. return o;
  3632. }
  3633. case 0x21: // Comment Extension.
  3634. {
  3635. int len;
  3636. if (stbi__get8(s) == 0xF9) { // Graphic Control Extension.
  3637. len = stbi__get8(s);
  3638. if (len == 4) {
  3639. g->eflags = stbi__get8(s);
  3640. stbi__get16le(s); // delay
  3641. g->transparent = stbi__get8(s);
  3642. } else {
  3643. stbi__skip(s, len);
  3644. break;
  3645. }
  3646. }
  3647. while ((len = stbi__get8(s)) != 0)
  3648. stbi__skip(s, len);
  3649. break;
  3650. }
  3651. case 0x3B: // gif stream termination code
  3652. return (stbi_uc *) 1;
  3653. default:
  3654. return stbi__errpuc("unknown code", "Corrupt GIF");
  3655. }
  3656. }
  3657. }
  3658. static stbi_uc *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  3659. {
  3660. stbi_uc *u = 0;
  3661. stbi__gif g;
  3662. memset(&g, 0, sizeof(stbi__gif));
  3663. u = stbi__gif_load_next(s, &g, comp, req_comp);
  3664. if (u == (void *) 1) u = 0; // end of animated gif marker
  3665. if (u) {
  3666. *x = g.w;
  3667. *y = g.h;
  3668. }
  3669. return u;
  3670. }
  3671. static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp)
  3672. {
  3673. return stbi__gif_info_raw(s,x,y,comp);
  3674. }
  3675. // *************************************************************************************************
  3676. // Radiance RGBE HDR loader
  3677. // originally by Nicolas Schulz
  3678. #ifndef STBI_NO_HDR
  3679. static int stbi__hdr_test_core(stbi__context *s)
  3680. {
  3681. const char *signature = "#?RADIANCE\n";
  3682. int i;
  3683. for (i=0; signature[i]; ++i)
  3684. if (stbi__get8(s) != signature[i])
  3685. return 0;
  3686. return 1;
  3687. }
  3688. static int stbi__hdr_test(stbi__context* s)
  3689. {
  3690. int r = stbi__hdr_test_core(s);
  3691. stbi__rewind(s);
  3692. return r;
  3693. }
  3694. #define STBI__HDR_BUFLEN 1024
  3695. static char *stbi__hdr_gettoken(stbi__context *z, char *buffer)
  3696. {
  3697. int len=0;
  3698. char c = '\0';
  3699. c = (char) stbi__get8(z);
  3700. while (!stbi__at_eof(z) && c != '\n') {
  3701. buffer[len++] = c;
  3702. if (len == STBI__HDR_BUFLEN-1) {
  3703. // flush to end of line
  3704. while (!stbi__at_eof(z) && stbi__get8(z) != '\n')
  3705. ;
  3706. break;
  3707. }
  3708. c = (char) stbi__get8(z);
  3709. }
  3710. buffer[len] = 0;
  3711. return buffer;
  3712. }
  3713. static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp)
  3714. {
  3715. if ( input[3] != 0 ) {
  3716. float f1;
  3717. // Exponent
  3718. f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
  3719. if (req_comp <= 2)
  3720. output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
  3721. else {
  3722. output[0] = input[0] * f1;
  3723. output[1] = input[1] * f1;
  3724. output[2] = input[2] * f1;
  3725. }
  3726. if (req_comp == 2) output[1] = 1;
  3727. if (req_comp == 4) output[3] = 1;
  3728. } else {
  3729. switch (req_comp) {
  3730. case 4: output[3] = 1; /* fallthrough */
  3731. case 3: output[0] = output[1] = output[2] = 0;
  3732. break;
  3733. case 2: output[1] = 1; /* fallthrough */
  3734. case 1: output[0] = 0;
  3735. break;
  3736. }
  3737. }
  3738. }
  3739. static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  3740. {
  3741. char buffer[STBI__HDR_BUFLEN];
  3742. char *token;
  3743. int valid = 0;
  3744. int width, height;
  3745. stbi_uc *scanline;
  3746. float *hdr_data;
  3747. int len;
  3748. unsigned char count, value;
  3749. int i, j, k, c1,c2, z;
  3750. // Check identifier
  3751. if (strcmp(stbi__hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
  3752. return stbi__errpf("not HDR", "Corrupt HDR image");
  3753. // Parse header
  3754. for(;;) {
  3755. token = stbi__hdr_gettoken(s,buffer);
  3756. if (token[0] == 0) break;
  3757. if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
  3758. }
  3759. if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format");
  3760. // Parse width and height
  3761. // can't use sscanf() if we're not using stdio!
  3762. token = stbi__hdr_gettoken(s,buffer);
  3763. if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
  3764. token += 3;
  3765. height = (int) strtol(token, &token, 10);
  3766. while (*token == ' ') ++token;
  3767. if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
  3768. token += 3;
  3769. width = (int) strtol(token, NULL, 10);
  3770. *x = width;
  3771. *y = height;
  3772. if (comp) *comp = 3;
  3773. if (req_comp == 0) req_comp = 3;
  3774. // Read data
  3775. hdr_data = (float *) malloc(height * width * req_comp * sizeof(float));
  3776. // Load image data
  3777. // image data is stored as some number of sca
  3778. if ( width < 8 || width >= 32768) {
  3779. // Read flat data
  3780. for (j=0; j < height; ++j) {
  3781. for (i=0; i < width; ++i) {
  3782. stbi_uc rgbe[4];
  3783. main_decode_loop:
  3784. stbi__getn(s, rgbe, 4);
  3785. stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
  3786. }
  3787. }
  3788. } else {
  3789. // Read RLE-encoded data
  3790. scanline = NULL;
  3791. for (j = 0; j < height; ++j) {
  3792. c1 = stbi__get8(s);
  3793. c2 = stbi__get8(s);
  3794. len = stbi__get8(s);
  3795. if (c1 != 2 || c2 != 2 || (len & 0x80)) {
  3796. // not run-length encoded, so we have to actually use THIS data as a decoded
  3797. // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
  3798. stbi_uc rgbe[4];
  3799. rgbe[0] = (stbi_uc) c1;
  3800. rgbe[1] = (stbi_uc) c2;
  3801. rgbe[2] = (stbi_uc) len;
  3802. rgbe[3] = (stbi_uc) stbi__get8(s);
  3803. stbi__hdr_convert(hdr_data, rgbe, req_comp);
  3804. i = 1;
  3805. j = 0;
  3806. free(scanline);
  3807. goto main_decode_loop; // yes, this makes no sense
  3808. }
  3809. len <<= 8;
  3810. len |= stbi__get8(s);
  3811. if (len != width) { free(hdr_data); free(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
  3812. if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4);
  3813. for (k = 0; k < 4; ++k) {
  3814. i = 0;
  3815. while (i < width) {
  3816. count = stbi__get8(s);
  3817. if (count > 128) {
  3818. // Run
  3819. value = stbi__get8(s);
  3820. count -= 128;
  3821. for (z = 0; z < count; ++z)
  3822. scanline[i++ * 4 + k] = value;
  3823. } else {
  3824. // Dump
  3825. for (z = 0; z < count; ++z)
  3826. scanline[i++ * 4 + k] = stbi__get8(s);
  3827. }
  3828. }
  3829. }
  3830. for (i=0; i < width; ++i)
  3831. stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
  3832. }
  3833. free(scanline);
  3834. }
  3835. return hdr_data;
  3836. }
  3837. static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp)
  3838. {
  3839. char buffer[STBI__HDR_BUFLEN];
  3840. char *token;
  3841. int valid = 0;
  3842. if (strcmp(stbi__hdr_gettoken(s,buffer), "#?RADIANCE") != 0) {
  3843. stbi__rewind( s );
  3844. return 0;
  3845. }
  3846. for(;;) {
  3847. token = stbi__hdr_gettoken(s,buffer);
  3848. if (token[0] == 0) break;
  3849. if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
  3850. }
  3851. if (!valid) {
  3852. stbi__rewind( s );
  3853. return 0;
  3854. }
  3855. token = stbi__hdr_gettoken(s,buffer);
  3856. if (strncmp(token, "-Y ", 3)) {
  3857. stbi__rewind( s );
  3858. return 0;
  3859. }
  3860. token += 3;
  3861. *y = (int) strtol(token, &token, 10);
  3862. while (*token == ' ') ++token;
  3863. if (strncmp(token, "+X ", 3)) {
  3864. stbi__rewind( s );
  3865. return 0;
  3866. }
  3867. token += 3;
  3868. *x = (int) strtol(token, NULL, 10);
  3869. *comp = 3;
  3870. return 1;
  3871. }
  3872. #endif // STBI_NO_HDR
  3873. static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
  3874. {
  3875. int hsz;
  3876. if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') {
  3877. stbi__rewind( s );
  3878. return 0;
  3879. }
  3880. stbi__skip(s,12);
  3881. hsz = stbi__get32le(s);
  3882. if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) {
  3883. stbi__rewind( s );
  3884. return 0;
  3885. }
  3886. if (hsz == 12) {
  3887. *x = stbi__get16le(s);
  3888. *y = stbi__get16le(s);
  3889. } else {
  3890. *x = stbi__get32le(s);
  3891. *y = stbi__get32le(s);
  3892. }
  3893. if (stbi__get16le(s) != 1) {
  3894. stbi__rewind( s );
  3895. return 0;
  3896. }
  3897. *comp = stbi__get16le(s) / 8;
  3898. return 1;
  3899. }
  3900. static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
  3901. {
  3902. int channelCount;
  3903. if (stbi__get32be(s) != 0x38425053) {
  3904. stbi__rewind( s );
  3905. return 0;
  3906. }
  3907. if (stbi__get16be(s) != 1) {
  3908. stbi__rewind( s );
  3909. return 0;
  3910. }
  3911. stbi__skip(s, 6);
  3912. channelCount = stbi__get16be(s);
  3913. if (channelCount < 0 || channelCount > 16) {
  3914. stbi__rewind( s );
  3915. return 0;
  3916. }
  3917. *y = stbi__get32be(s);
  3918. *x = stbi__get32be(s);
  3919. if (stbi__get16be(s) != 8) {
  3920. stbi__rewind( s );
  3921. return 0;
  3922. }
  3923. if (stbi__get16be(s) != 3) {
  3924. stbi__rewind( s );
  3925. return 0;
  3926. }
  3927. *comp = 4;
  3928. return 1;
  3929. }
  3930. static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
  3931. {
  3932. int act_comp=0,num_packets=0,chained;
  3933. stbi__pic_packet packets[10];
  3934. stbi__skip(s, 92);
  3935. *x = stbi__get16be(s);
  3936. *y = stbi__get16be(s);
  3937. if (stbi__at_eof(s)) return 0;
  3938. if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
  3939. stbi__rewind( s );
  3940. return 0;
  3941. }
  3942. stbi__skip(s, 8);
  3943. do {
  3944. stbi__pic_packet *packet;
  3945. if (num_packets==sizeof(packets)/sizeof(packets[0]))
  3946. return 0;
  3947. packet = &packets[num_packets++];
  3948. chained = stbi__get8(s);
  3949. packet->size = stbi__get8(s);
  3950. packet->type = stbi__get8(s);
  3951. packet->channel = stbi__get8(s);
  3952. act_comp |= packet->channel;
  3953. if (stbi__at_eof(s)) {
  3954. stbi__rewind( s );
  3955. return 0;
  3956. }
  3957. if (packet->size != 8) {
  3958. stbi__rewind( s );
  3959. return 0;
  3960. }
  3961. } while (chained);
  3962. *comp = (act_comp & 0x10 ? 4 : 3);
  3963. return 1;
  3964. }
  3965. static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)
  3966. {
  3967. if (stbi__jpeg_info(s, x, y, comp))
  3968. return 1;
  3969. if (stbi__png_info(s, x, y, comp))
  3970. return 1;
  3971. if (stbi__gif_info(s, x, y, comp))
  3972. return 1;
  3973. if (stbi__bmp_info(s, x, y, comp))
  3974. return 1;
  3975. if (stbi__psd_info(s, x, y, comp))
  3976. return 1;
  3977. if (stbi__pic_info(s, x, y, comp))
  3978. return 1;
  3979. #ifndef STBI_NO_HDR
  3980. if (stbi__hdr_info(s, x, y, comp))
  3981. return 1;
  3982. #endif
  3983. // test tga last because it's a crappy test!
  3984. if (stbi__tga_info(s, x, y, comp))
  3985. return 1;
  3986. return stbi__err("unknown image type", "Image not of any known type, or corrupt");
  3987. }
  3988. #ifndef STBI_NO_STDIO
  3989. STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp)
  3990. {
  3991. FILE *f = fopen(filename, "rb");
  3992. int result;
  3993. if (!f) return stbi__err("can't fopen", "Unable to open file");
  3994. result = stbi_info_from_file(f, x, y, comp);
  3995. fclose(f);
  3996. return result;
  3997. }
  3998. STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
  3999. {
  4000. int r;
  4001. stbi__context s;
  4002. long pos = ftell(f);
  4003. stbi__start_file(&s, f);
  4004. r = stbi__info_main(&s,x,y,comp);
  4005. fseek(f,pos,SEEK_SET);
  4006. return r;
  4007. }
  4008. #endif // !STBI_NO_STDIO
  4009. STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  4010. {
  4011. stbi__context s;
  4012. stbi__start_mem(&s,buffer,len);
  4013. return stbi__info_main(&s,x,y,comp);
  4014. }
  4015. STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
  4016. {
  4017. stbi__context s;
  4018. stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
  4019. return stbi__info_main(&s,x,y,comp);
  4020. }
  4021. #endif // STB_IMAGE_IMPLEMENTATION
  4022. #if !defined(STBI_NO_STDIO) && defined(_MSC_VER) && _MSC_VER >= 1400
  4023. #pragma warning(pop)
  4024. #endif
  4025. /*
  4026. revision history:
  4027. 1.39 (2014-06-15)
  4028. fix to TGA optimization when req_comp != number of components in TGA;
  4029. fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite)
  4030. add support for BMP version 5 (more ignored fields)
  4031. 1.38 (2014-06-06)
  4032. suppress MSVC warnings on integer casts truncating values
  4033. fix accidental rename of 'skip' field of I/O
  4034. 1.37 (2014-06-04)
  4035. remove duplicate typedef
  4036. 1.36 (2014-06-03)
  4037. convert to header file single-file library
  4038. if de-iphone isn't set, load iphone images color-swapped instead of returning NULL
  4039. 1.35 (2014-05-27)
  4040. various warnings
  4041. fix broken STBI_SIMD path
  4042. fix bug where stbi_load_from_file no longer left file pointer in correct place
  4043. fix broken non-easy path for 32-bit BMP (possibly never used)
  4044. TGA optimization by Arseny Kapoulkine
  4045. 1.34 (unknown)
  4046. use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case
  4047. 1.33 (2011-07-14)
  4048. make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
  4049. 1.32 (2011-07-13)
  4050. support for "info" function for all supported filetypes (SpartanJ)
  4051. 1.31 (2011-06-20)
  4052. a few more leak fixes, bug in PNG handling (SpartanJ)
  4053. 1.30 (2011-06-11)
  4054. added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
  4055. removed deprecated format-specific test/load functions
  4056. removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
  4057. error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
  4058. fix inefficiency in decoding 32-bit BMP (David Woo)
  4059. 1.29 (2010-08-16)
  4060. various warning fixes from Aurelien Pocheville
  4061. 1.28 (2010-08-01)
  4062. fix bug in GIF palette transparency (SpartanJ)
  4063. 1.27 (2010-08-01)
  4064. cast-to-stbi_uc to fix warnings
  4065. 1.26 (2010-07-24)
  4066. fix bug in file buffering for PNG reported by SpartanJ
  4067. 1.25 (2010-07-17)
  4068. refix trans_data warning (Won Chun)
  4069. 1.24 (2010-07-12)
  4070. perf improvements reading from files on platforms with lock-heavy fgetc()
  4071. minor perf improvements for jpeg
  4072. deprecated type-specific functions so we'll get feedback if they're needed
  4073. attempt to fix trans_data warning (Won Chun)
  4074. 1.23 fixed bug in iPhone support
  4075. 1.22 (2010-07-10)
  4076. removed image *writing* support
  4077. stbi_info support from Jetro Lauha
  4078. GIF support from Jean-Marc Lienher
  4079. iPhone PNG-extensions from James Brown
  4080. warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva)
  4081. 1.21 fix use of 'stbi_uc' in header (reported by jon blow)
  4082. 1.20 added support for Softimage PIC, by Tom Seddon
  4083. 1.19 bug in interlaced PNG corruption check (found by ryg)
  4084. 1.18 2008-08-02
  4085. fix a threading bug (local mutable static)
  4086. 1.17 support interlaced PNG
  4087. 1.16 major bugfix - stbi__convert_format converted one too many pixels
  4088. 1.15 initialize some fields for thread safety
  4089. 1.14 fix threadsafe conversion bug
  4090. header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
  4091. 1.13 threadsafe
  4092. 1.12 const qualifiers in the API
  4093. 1.11 Support installable IDCT, colorspace conversion routines
  4094. 1.10 Fixes for 64-bit (don't use "unsigned long")
  4095. optimized upsampling by Fabian "ryg" Giesen
  4096. 1.09 Fix format-conversion for PSD code (bad global variables!)
  4097. 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
  4098. 1.07 attempt to fix C++ warning/errors again
  4099. 1.06 attempt to fix C++ warning/errors again
  4100. 1.05 fix TGA loading to return correct *comp and use good luminance calc
  4101. 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
  4102. 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
  4103. 1.02 support for (subset of) HDR files, float interface for preferred access to them
  4104. 1.01 fix bug: possible bug in handling right-side up bmps... not sure
  4105. fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all
  4106. 1.00 interface to zlib that skips zlib header
  4107. 0.99 correct handling of alpha in palette
  4108. 0.98 TGA loader by lonesock; dynamically add loaders (untested)
  4109. 0.97 jpeg errors on too large a file; also catch another malloc failure
  4110. 0.96 fix detection of invalid v value - particleman@mollyrocket forum
  4111. 0.95 during header scan, seek to markers in case of padding
  4112. 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
  4113. 0.93 handle jpegtran output; verbose errors
  4114. 0.92 read 4,8,16,24,32-bit BMP files of several formats
  4115. 0.91 output 24-bit Windows 3.0 BMP files
  4116. 0.90 fix a few more warnings; bump version number to approach 1.0
  4117. 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
  4118. 0.60 fix compiling as c++
  4119. 0.59 fix warnings: merge Dave Moore's -Wall fixes
  4120. 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
  4121. 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
  4122. 0.56 fix bug: zlib uncompressed mode len vs. nlen
  4123. 0.55 fix bug: restart_interval not initialized to 0
  4124. 0.54 allow NULL for 'int *comp'
  4125. 0.53 fix bug in png 3->4; speedup png decoding
  4126. 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
  4127. 0.51 obey req_comp requests, 1-component jpegs return as 1-component,
  4128. on 'test' only check type, not whether we support this variant
  4129. 0.50 first released version
  4130. */