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