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