1#ifndef STBI_INCLUDE_STB_IMAGE_H
2#define STBI_INCLUDE_STB_IMAGE_H
3
4#ifndef STBI_NO_STDIO
5#include <stdio.h>
6#endif
7
8#define STBI_VERSION 1
9
10enum
11{
12 STBI_default = 0,
13
14 STBI_grey = 1,
15 STBI_grey_alpha = 2,
16 STBI_rgb = 3,
17 STBI_rgb_alpha = 4
18};
19
20#include <stdlib.h>
21typedef unsigned char stbi_uc;
22typedef unsigned short stbi_us;
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#ifndef STBIDEF
29#ifdef STB_IMAGE_STATIC
30#define STBIDEF static
31#else
32#define STBIDEF extern
33#endif
34#endif
35
36typedef struct
37{
38 int (*read) (void *user,char *data,int size);
39 void (*skip) (void *user,int n);
40 int (*eof) (void *user);
41} stbi_io_callbacks;
42
43STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
44STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);
45
46#ifndef STBI_NO_STDIO
47STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
48STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
49
50#endif
51
52#ifndef STBI_NO_GIF
53STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
54#endif
55
56#ifdef STBI_WINDOWS_UTF8
57STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
58#endif
59
60STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
61STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
62
63#ifndef STBI_NO_STDIO
64STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
65STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
66#endif
67
68#ifndef STBI_NO_LINEAR
69 STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
70 STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
71
72 #ifndef STBI_NO_STDIO
73 STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
74 STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
75 #endif
76#endif
77
78#ifndef STBI_NO_HDR
79 STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);
80 STBIDEF void stbi_hdr_to_ldr_scale(float scale);
81#endif
82
83#ifndef STBI_NO_LINEAR
84 STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);
85 STBIDEF void stbi_ldr_to_hdr_scale(float scale);
86#endif
87
88STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
89STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
90#ifndef STBI_NO_STDIO
91STBIDEF int stbi_is_hdr (char const *filename);
92STBIDEF int stbi_is_hdr_from_file(FILE *f);
93#endif
94
95STBIDEF const char *stbi_failure_reason (void);
96
97STBIDEF void stbi_image_free (void *retval_from_stbi_load);
98
99STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
100STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
101STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);
102STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user);
103
104#ifndef STBI_NO_STDIO
105STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);
106STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
107STBIDEF int stbi_is_16_bit (char const *filename);
108STBIDEF int stbi_is_16_bit_from_file(FILE *f);
109#endif
110
111STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
112
113STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
114
115STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);
116
117STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply);
118STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert);
119STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);
120
121STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
122STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
123STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
124STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
125
126STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
127STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
128
129#ifdef __cplusplus
130}
131#endif
132
133#endif
134
135#ifdef STB_IMAGE_IMPLEMENTATION
136
137#if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \
138 || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \
139 || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \
140 || defined(STBI_ONLY_ZLIB)
141 #ifndef STBI_ONLY_JPEG
142 #define STBI_NO_JPEG
143 #endif
144 #ifndef STBI_ONLY_PNG
145 #define STBI_NO_PNG
146 #endif
147 #ifndef STBI_ONLY_BMP
148 #define STBI_NO_BMP
149 #endif
150 #ifndef STBI_ONLY_PSD
151 #define STBI_NO_PSD
152 #endif
153 #ifndef STBI_ONLY_TGA
154 #define STBI_NO_TGA
155 #endif
156 #ifndef STBI_ONLY_GIF
157 #define STBI_NO_GIF
158 #endif
159 #ifndef STBI_ONLY_HDR
160 #define STBI_NO_HDR
161 #endif
162 #ifndef STBI_ONLY_PIC
163 #define STBI_NO_PIC
164 #endif
165 #ifndef STBI_ONLY_PNM
166 #define STBI_NO_PNM
167 #endif
168#endif
169
170#if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB)
171#define STBI_NO_ZLIB
172#endif
173
174#include <stdarg.h>
175#include <stddef.h>
176#include <stdlib.h>
177#include <string.h>
178#include <limits.h>
179
180#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
181#include <math.h>
182#endif
183
184#ifndef STBI_NO_STDIO
185#include <stdio.h>
186#endif
187
188#ifndef STBI_ASSERT
189#include <assert.h>
190#define STBI_ASSERT(x) assert(x)
191#endif
192
193#ifdef __cplusplus
194#define STBI_EXTERN extern "C"
195#else
196#define STBI_EXTERN extern
197#endif
198
199#ifndef _MSC_VER
200 #ifdef __cplusplus
201 #define stbi_inline inline
202 #else
203 #define stbi_inline
204 #endif
205#else
206 #define stbi_inline __forceinline
207#endif
208
209#ifndef STBI_NO_THREAD_LOCALS
210 #if defined(__cplusplus) && __cplusplus >= 201103L
211 #define STBI_THREAD_LOCAL thread_local
212 #elif defined(__GNUC__) && __GNUC__ < 5
213 #define STBI_THREAD_LOCAL __thread
214 #elif defined(_MSC_VER)
215 #define STBI_THREAD_LOCAL __declspec(thread)
216 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
217 #define STBI_THREAD_LOCAL _Thread_local
218 #endif
219
220 #ifndef STBI_THREAD_LOCAL
221 #if defined(__GNUC__)
222 #define STBI_THREAD_LOCAL __thread
223 #endif
224 #endif
225#endif
226
227#if defined(_MSC_VER) || defined(__SYMBIAN32__)
228typedef unsigned short stbi__uint16;
229typedef signed short stbi__int16;
230typedef unsigned int stbi__uint32;
231typedef signed int stbi__int32;
232#else
233#include <stdint.h>
234typedef uint16_t stbi__uint16;
235typedef int16_t stbi__int16;
236typedef uint32_t stbi__uint32;
237typedef int32_t stbi__int32;
238#endif
239
240typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
241
242#ifdef _MSC_VER
243#define STBI_NOTUSED(v) (void)(v)
244#else
245#define STBI_NOTUSED(v) (void)sizeof(v)
246#endif
247
248#ifdef _MSC_VER
249#define STBI_HAS_LROTL
250#endif
251
252#ifdef STBI_HAS_LROTL
253 #define stbi_lrot(x,y) _lrotl(x,y)
254#else
255 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31)))
256#endif
257
258#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))
259
260#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED)
261
262#else
263#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)."
264#endif
265
266#ifndef STBI_MALLOC
267#define STBI_MALLOC(sz) malloc(sz)
268#define STBI_REALLOC(p,newsz) realloc(p,newsz)
269#define STBI_FREE(p) free(p)
270#endif
271
272#ifndef STBI_REALLOC_SIZED
273#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)
274#endif
275
276#if defined(__x86_64__) || defined(_M_X64)
277#define STBI__X64_TARGET
278#elif defined(__i386) || defined(_M_IX86)
279#define STBI__X86_TARGET
280#endif
281
282#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD)
283
284#define STBI_NO_SIMD
285#endif
286
287#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)
288
289#define STBI_NO_SIMD
290#endif
291
292#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))
293#define STBI_SSE2
294#include <emmintrin.h>
295
296#ifdef _MSC_VER
297
298#if _MSC_VER >= 1400
299#include <intrin.h>
300static int stbi__cpuid3(void)
301{
302 int info[4];
303 __cpuid(info,1);
304 return info[3];
305}
306#else
307static int stbi__cpuid3(void)
308{
309 int res;
310 __asm {
311 mov eax,1
312 cpuid
313 mov res,edx
314 }
315 return res;
316}
317#endif
318
319#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
320
321#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
322static int stbi__sse2_available(void)
323{
324 int info3 = stbi__cpuid3();
325 return ((info3 >> 26) & 1) != 0;
326}
327#endif
328
329#else
330#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
331
332#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
333static int stbi__sse2_available(void)
334{
335
336 return 1;
337}
338#endif
339
340#endif
341#endif
342
343#if defined(STBI_NO_SIMD) && defined(STBI_NEON)
344#undef STBI_NEON
345#endif
346
347#ifdef STBI_NEON
348#include <arm_neon.h>
349#ifdef _MSC_VER
350#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
351#else
352#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
353#endif
354#endif
355
356#ifndef STBI_SIMD_ALIGN
357#define STBI_SIMD_ALIGN(type, name) type name
358#endif
359
360#ifndef STBI_MAX_DIMENSIONS
361#define STBI_MAX_DIMENSIONS (1 << 24)
362#endif
363
364typedef struct
365{
366 stbi__uint32 img_x, img_y;
367 int img_n, img_out_n;
368
369 stbi_io_callbacks io;
370 void *io_user_data;
371
372 int read_from_callbacks;
373 int buflen;
374 stbi_uc buffer_start[128];
375 int callback_already_read;
376
377 stbi_uc *img_buffer, *img_buffer_end;
378 stbi_uc *img_buffer_original, *img_buffer_original_end;
379} stbi__context;
380
381static void stbi__refill_buffer(stbi__context *s);
382
383static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)
384{
385 s->io.read = NULL;
386 s->read_from_callbacks = 0;
387 s->callback_already_read = 0;
388 s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;
389 s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len;
390}
391
392static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user)
393{
394 s->io = *c;
395 s->io_user_data = user;
396 s->buflen = sizeof(s->buffer_start);
397 s->read_from_callbacks = 1;
398 s->callback_already_read = 0;
399 s->img_buffer = s->img_buffer_original = s->buffer_start;
400 stbi__refill_buffer(s);
401 s->img_buffer_original_end = s->img_buffer_end;
402}
403
404#ifndef STBI_NO_STDIO
405
406static int stbi__stdio_read(void *user, char *data, int size)
407{
408 return (int) fread(data,1,size,(FILE*) user);
409}
410
411static void stbi__stdio_skip(void *user, int n)
412{
413 int ch;
414 fseek((FILE*) user, n, SEEK_CUR);
415 ch = fgetc((FILE*) user);
416 if (ch != EOF) {
417 ungetc(ch, (FILE *) user);
418 }
419}
420
421static int stbi__stdio_eof(void *user)
422{
423 return feof((FILE*) user) || ferror((FILE *) user);
424}
425
426static stbi_io_callbacks stbi__stdio_callbacks =
427{
428 stbi__stdio_read,
429 stbi__stdio_skip,
430 stbi__stdio_eof,
431};
432
433static void stbi__start_file(stbi__context *s, FILE *f)
434{
435 stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);
436}
437
438#endif
439
440static void stbi__rewind(stbi__context *s)
441{
442
443 s->img_buffer = s->img_buffer_original;
444 s->img_buffer_end = s->img_buffer_original_end;
445}
446
447enum
448{
449 STBI_ORDER_RGB,
450 STBI_ORDER_BGR
451};
452
453typedef struct
454{
455 int bits_per_channel;
456 int num_channels;
457 int channel_order;
458} stbi__result_info;
459
460#ifndef STBI_NO_JPEG
461static int stbi__jpeg_test(stbi__context *s);
462static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
463static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp);
464#endif
465
466#ifndef STBI_NO_PNG
467static int stbi__png_test(stbi__context *s);
468static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
469static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);
470static int stbi__png_is16(stbi__context *s);
471#endif
472
473#ifndef STBI_NO_BMP
474static int stbi__bmp_test(stbi__context *s);
475static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
476static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp);
477#endif
478
479#ifndef STBI_NO_TGA
480static int stbi__tga_test(stbi__context *s);
481static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
482static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp);
483#endif
484
485#ifndef STBI_NO_PSD
486static int stbi__psd_test(stbi__context *s);
487static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);
488static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);
489static int stbi__psd_is16(stbi__context *s);
490#endif
491
492#ifndef STBI_NO_HDR
493static int stbi__hdr_test(stbi__context *s);
494static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
495static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp);
496#endif
497
498#ifndef STBI_NO_PIC
499static int stbi__pic_test(stbi__context *s);
500static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
501static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp);
502#endif
503
504#ifndef STBI_NO_GIF
505static int stbi__gif_test(stbi__context *s);
506static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
507static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
508static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);
509#endif
510
511#ifndef STBI_NO_PNM
512static int stbi__pnm_test(stbi__context *s);
513static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
514static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);
515static int stbi__pnm_is16(stbi__context *s);
516#endif
517
518static
519#ifdef STBI_THREAD_LOCAL
520STBI_THREAD_LOCAL
521#endif
522const char *stbi__g_failure_reason;
523
524STBIDEF const char *stbi_failure_reason(void)
525{
526 return stbi__g_failure_reason;
527}
528
529#ifndef STBI_NO_FAILURE_STRINGS
530static int stbi__err(const char *str)
531{
532 stbi__g_failure_reason = str;
533 return 0;
534}
535#endif
536
537static void *stbi__malloc(size_t size)
538{
539 return STBI_MALLOC(size);
540}
541
542static int stbi__addsizes_valid(int a, int b)
543{
544 if (b < 0) return 0;
545
546 return a <= INT_MAX - b;
547}
548
549static int stbi__mul2sizes_valid(int a, int b)
550{
551 if (a < 0 || b < 0) return 0;
552 if (b == 0) return 1;
553
554 return a <= INT_MAX/b;
555}
556
557#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
558
559static int stbi__mad2sizes_valid(int a, int b, int add)
560{
561 return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add);
562}
563#endif
564
565static int stbi__mad3sizes_valid(int a, int b, int c, int add)
566{
567 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
568 stbi__addsizes_valid(a*b*c, add);
569}
570
571#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
572static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
573{
574 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
575 stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add);
576}
577#endif
578
579#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
580
581static void *stbi__malloc_mad2(int a, int b, int add)
582{
583 if (!stbi__mad2sizes_valid(a, b, add)) return NULL;
584 return stbi__malloc(a*b + add);
585}
586#endif
587
588static void *stbi__malloc_mad3(int a, int b, int c, int add)
589{
590 if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL;
591 return stbi__malloc(a*b*c + add);
592}
593
594#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
595static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
596{
597 if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;
598 return stbi__malloc(a*b*c*d + add);
599}
600#endif
601
602static int stbi__addints_valid(int a, int b)
603{
604 if ((a >= 0) != (b >= 0)) return 1;
605 if (a < 0 && b < 0) return a >= INT_MIN - b;
606 return a <= INT_MAX - b;
607}
608
609static int stbi__mul2shorts_valid(int a, int b)
610{
611 if (b == 0 || b == -1) return 1;
612 if ((a >= 0) == (b >= 0)) return a <= SHRT_MAX/b;
613 if (b < 0) return a <= SHRT_MIN / b;
614 return a >= SHRT_MIN / b;
615}
616
617#ifdef STBI_NO_FAILURE_STRINGS
618 #define stbi__err(x,y) 0
619#elif defined(STBI_FAILURE_USERMSG)
620 #define stbi__err(x,y) stbi__err(y)
621#else
622 #define stbi__err(x,y) stbi__err(x)
623#endif
624
625#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL))
626#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL))
627
628STBIDEF void stbi_image_free(void *retval_from_stbi_load)
629{
630 STBI_FREE(retval_from_stbi_load);
631}
632
633#ifndef STBI_NO_LINEAR
634static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
635#endif
636
637#ifndef STBI_NO_HDR
638static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);
639#endif
640
641static int stbi__vertically_flip_on_load_global = 0;
642
643STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
644{
645 stbi__vertically_flip_on_load_global = flag_true_if_should_flip;
646}
647
648#ifndef STBI_THREAD_LOCAL
649#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global
650#else
651static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set;
652
653STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip)
654{
655 stbi__vertically_flip_on_load_local = flag_true_if_should_flip;
656 stbi__vertically_flip_on_load_set = 1;
657}
658
659#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \
660 ? stbi__vertically_flip_on_load_local \
661 : stbi__vertically_flip_on_load_global)
662#endif
663
664static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
665{
666 memset(ri, 0, sizeof(*ri));
667 ri->bits_per_channel = 8;
668 ri->channel_order = STBI_ORDER_RGB;
669 ri->num_channels = 0;
670
671 #ifndef STBI_NO_PNG
672 if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri);
673 #endif
674 #ifndef STBI_NO_BMP
675 if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri);
676 #endif
677 #ifndef STBI_NO_GIF
678 if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri);
679 #endif
680 #ifndef STBI_NO_PSD
681 if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc);
682 #else
683 STBI_NOTUSED(bpc);
684 #endif
685 #ifndef STBI_NO_PIC
686 if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);
687 #endif
688
689 #ifndef STBI_NO_JPEG
690 if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri);
691 #endif
692 #ifndef STBI_NO_PNM
693 if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri);
694 #endif
695
696 #ifndef STBI_NO_HDR
697 if (stbi__hdr_test(s)) {
698 float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri);
699 return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
700 }
701 #endif
702
703 #ifndef STBI_NO_TGA
704
705 if (stbi__tga_test(s))
706 return stbi__tga_load(s,x,y,comp,req_comp, ri);
707 #endif
708
709 return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt");
710}
711
712static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels)
713{
714 int i;
715 int img_len = w * h * channels;
716 stbi_uc *reduced;
717
718 reduced = (stbi_uc *) stbi__malloc(img_len);
719 if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory");
720
721 for (i = 0; i < img_len; ++i)
722 reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF);
723
724 STBI_FREE(orig);
725 return reduced;
726}
727
728static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels)
729{
730 int i;
731 int img_len = w * h * channels;
732 stbi__uint16 *enlarged;
733
734 enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);
735 if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
736
737 for (i = 0; i < img_len; ++i)
738 enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]);
739
740 STBI_FREE(orig);
741 return enlarged;
742}
743
744static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
745{
746 int row;
747 size_t bytes_per_row = (size_t)w * bytes_per_pixel;
748 stbi_uc temp[2048];
749 stbi_uc *bytes = (stbi_uc *)image;
750
751 for (row = 0; row < (h>>1); row++) {
752 stbi_uc *row0 = bytes + row*bytes_per_row;
753 stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row;
754
755 size_t bytes_left = bytes_per_row;
756 while (bytes_left) {
757 size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp);
758 memcpy(temp, row0, bytes_copy);
759 memcpy(row0, row1, bytes_copy);
760 memcpy(row1, temp, bytes_copy);
761 row0 += bytes_copy;
762 row1 += bytes_copy;
763 bytes_left -= bytes_copy;
764 }
765 }
766}
767
768#ifndef STBI_NO_GIF
769static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)
770{
771 int slice;
772 int slice_size = w * h * bytes_per_pixel;
773
774 stbi_uc *bytes = (stbi_uc *)image;
775 for (slice = 0; slice < z; ++slice) {
776 stbi__vertical_flip(bytes, w, h, bytes_per_pixel);
777 bytes += slice_size;
778 }
779}
780#endif
781
782static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
783{
784 stbi__result_info ri;
785 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);
786
787 if (result == NULL)
788 return NULL;
789
790 STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16);
791
792 if (ri.bits_per_channel != 8) {
793 result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
794 ri.bits_per_channel = 8;
795 }
796
797 if (stbi__vertically_flip_on_load) {
798 int channels = req_comp ? req_comp : *comp;
799 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc));
800 }
801
802 return (unsigned char *) result;
803}
804
805static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
806{
807 stbi__result_info ri;
808 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16);
809
810 if (result == NULL)
811 return NULL;
812
813 STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16);
814
815 if (ri.bits_per_channel != 16) {
816 result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
817 ri.bits_per_channel = 16;
818 }
819
820 if (stbi__vertically_flip_on_load) {
821 int channels = req_comp ? req_comp : *comp;
822 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16));
823 }
824
825 return (stbi__uint16 *) result;
826}
827
828#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR)
829static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)
830{
831 if (stbi__vertically_flip_on_load && result != NULL) {
832 int channels = req_comp ? req_comp : *comp;
833 stbi__vertical_flip(result, *x, *y, channels * sizeof(float));
834 }
835}
836#endif
837
838#ifndef STBI_NO_STDIO
839
840#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
841STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
842STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
843#endif
844
845#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
846STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)
847{
848 return WideCharToMultiByte(65001 , 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
849}
850#endif
851
852static FILE *stbi__fopen(char const *filename, char const *mode)
853{
854 FILE *f;
855#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
856 wchar_t wMode[64];
857 wchar_t wFilename[1024];
858 if (0 == MultiByteToWideChar(65001 , 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename)))
859 return 0;
860
861 if (0 == MultiByteToWideChar(65001 , 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode)))
862 return 0;
863
864#if defined(_MSC_VER) && _MSC_VER >= 1400
865 if (0 != _wfopen_s(&f, wFilename, wMode))
866 f = 0;
867#else
868 f = _wfopen(wFilename, wMode);
869#endif
870
871#elif defined(_MSC_VER) && _MSC_VER >= 1400
872 if (0 != fopen_s(&f, filename, mode))
873 f=0;
874#else
875 f = fopen(filename, mode);
876#endif
877 return f;
878}
879
880STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
881{
882 FILE *f = stbi__fopen(filename, "rb");
883 unsigned char *result;
884 if (!f) return stbi__errpuc("can't fopen", "Unable to open file");
885 result = stbi_load_from_file(f,x,y,comp,req_comp);
886 fclose(f);
887 return result;
888}
889
890STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
891{
892 unsigned char *result;
893 stbi__context s;
894 stbi__start_file(&s,f);
895 result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
896 if (result) {
897
898 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
899 }
900 return result;
901}
902
903STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp)
904{
905 stbi__uint16 *result;
906 stbi__context s;
907 stbi__start_file(&s,f);
908 result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp);
909 if (result) {
910
911 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
912 }
913 return result;
914}
915
916STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp)
917{
918 FILE *f = stbi__fopen(filename, "rb");
919 stbi__uint16 *result;
920 if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file");
921 result = stbi_load_from_file_16(f,x,y,comp,req_comp);
922 fclose(f);
923 return result;
924}
925
926#endif
927
928STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)
929{
930 stbi__context s;
931 stbi__start_mem(&s,buffer,len);
932 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
933}
934
935STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)
936{
937 stbi__context s;
938 stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user);
939 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
940}
941
942STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
943{
944 stbi__context s;
945 stbi__start_mem(&s,buffer,len);
946 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
947}
948
949STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
950{
951 stbi__context s;
952 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
953 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
954}
955
956#ifndef STBI_NO_GIF
957STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
958{
959 unsigned char *result;
960 stbi__context s;
961 stbi__start_mem(&s,buffer,len);
962
963 result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
964 if (stbi__vertically_flip_on_load) {
965 stbi__vertical_flip_slices( result, *x, *y, *z, *comp );
966 }
967
968 return result;
969}
970#endif
971
972#ifndef STBI_NO_LINEAR
973static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)
974{
975 unsigned char *data;
976 #ifndef STBI_NO_HDR
977 if (stbi__hdr_test(s)) {
978 stbi__result_info ri;
979 float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri);
980 if (hdr_data)
981 stbi__float_postprocess(hdr_data,x,y,comp,req_comp);
982 return hdr_data;
983 }
984 #endif
985 data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
986 if (data)
987 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
988 return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");
989}
990
991STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
992{
993 stbi__context s;
994 stbi__start_mem(&s,buffer,len);
995 return stbi__loadf_main(&s,x,y,comp,req_comp);
996}
997
998STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
999{
1000 stbi__context s;
1001 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1002 return stbi__loadf_main(&s,x,y,comp,req_comp);
1003}
1004
1005#ifndef STBI_NO_STDIO
1006STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
1007{
1008 float *result;
1009 FILE *f = stbi__fopen(filename, "rb");
1010 if (!f) return stbi__errpf("can't fopen", "Unable to open file");
1011 result = stbi_loadf_from_file(f,x,y,comp,req_comp);
1012 fclose(f);
1013 return result;
1014}
1015
1016STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
1017{
1018 stbi__context s;
1019 stbi__start_file(&s,f);
1020 return stbi__loadf_main(&s,x,y,comp,req_comp);
1021}
1022#endif
1023
1024#endif
1025
1026STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
1027{
1028 #ifndef STBI_NO_HDR
1029 stbi__context s;
1030 stbi__start_mem(&s,buffer,len);
1031 return stbi__hdr_test(&s);
1032 #else
1033 STBI_NOTUSED(buffer);
1034 STBI_NOTUSED(len);
1035 return 0;
1036 #endif
1037}
1038
1039#ifndef STBI_NO_STDIO
1040STBIDEF int stbi_is_hdr (char const *filename)
1041{
1042 FILE *f = stbi__fopen(filename, "rb");
1043 int result=0;
1044 if (f) {
1045 result = stbi_is_hdr_from_file(f);
1046 fclose(f);
1047 }
1048 return result;
1049}
1050
1051STBIDEF int stbi_is_hdr_from_file(FILE *f)
1052{
1053 #ifndef STBI_NO_HDR
1054 long pos = ftell(f);
1055 int res;
1056 stbi__context s;
1057 stbi__start_file(&s,f);
1058 res = stbi__hdr_test(&s);
1059 fseek(f, pos, SEEK_SET);
1060 return res;
1061 #else
1062 STBI_NOTUSED(f);
1063 return 0;
1064 #endif
1065}
1066#endif
1067
1068STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
1069{
1070 #ifndef STBI_NO_HDR
1071 stbi__context s;
1072 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1073 return stbi__hdr_test(&s);
1074 #else
1075 STBI_NOTUSED(clbk);
1076 STBI_NOTUSED(user);
1077 return 0;
1078 #endif
1079}
1080
1081#ifndef STBI_NO_LINEAR
1082static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;
1083
1084STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; }
1085STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; }
1086#endif
1087
1088static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;
1089
1090STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; }
1091STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; }
1092
1093enum
1094{
1095 STBI__SCAN_load=0,
1096 STBI__SCAN_type,
1097 STBI__SCAN_header
1098};
1099
1100static void stbi__refill_buffer(stbi__context *s)
1101{
1102 int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
1103 s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original);
1104 if (n == 0) {
1105
1106 s->read_from_callbacks = 0;
1107 s->img_buffer = s->buffer_start;
1108 s->img_buffer_end = s->buffer_start+1;
1109 *s->img_buffer = 0;
1110 } else {
1111 s->img_buffer = s->buffer_start;
1112 s->img_buffer_end = s->buffer_start + n;
1113 }
1114}
1115
1116stbi_inline static stbi_uc stbi__get8(stbi__context *s)
1117{
1118 if (s->img_buffer < s->img_buffer_end)
1119 return *s->img_buffer++;
1120 if (s->read_from_callbacks) {
1121 stbi__refill_buffer(s);
1122 return *s->img_buffer++;
1123 }
1124 return 0;
1125}
1126
1127#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1128
1129#else
1130stbi_inline static int stbi__at_eof(stbi__context *s)
1131{
1132 if (s->io.read) {
1133 if (!(s->io.eof)(s->io_user_data)) return 0;
1134
1135 if (s->read_from_callbacks == 0) return 1;
1136 }
1137
1138 return s->img_buffer >= s->img_buffer_end;
1139}
1140#endif
1141
1142#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC)
1143
1144#else
1145static void stbi__skip(stbi__context *s, int n)
1146{
1147 if (n == 0) return;
1148 if (n < 0) {
1149 s->img_buffer = s->img_buffer_end;
1150 return;
1151 }
1152 if (s->io.read) {
1153 int blen = (int) (s->img_buffer_end - s->img_buffer);
1154 if (blen < n) {
1155 s->img_buffer = s->img_buffer_end;
1156 (s->io.skip)(s->io_user_data, n - blen);
1157 return;
1158 }
1159 }
1160 s->img_buffer += n;
1161}
1162#endif
1163
1164#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM)
1165
1166#else
1167static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1168{
1169 if (s->io.read) {
1170 int blen = (int) (s->img_buffer_end - s->img_buffer);
1171 if (blen < n) {
1172 int res, count;
1173
1174 memcpy(buffer, s->img_buffer, blen);
1175
1176 count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);
1177 res = (count == (n-blen));
1178 s->img_buffer = s->img_buffer_end;
1179 return res;
1180 }
1181 }
1182
1183 if (s->img_buffer+n <= s->img_buffer_end) {
1184 memcpy(buffer, s->img_buffer, n);
1185 s->img_buffer += n;
1186 return 1;
1187 } else
1188 return 0;
1189}
1190#endif
1191
1192#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
1193
1194#else
1195static int stbi__get16be(stbi__context *s)
1196{
1197 int z = stbi__get8(s);
1198 return (z << 8) + stbi__get8(s);
1199}
1200#endif
1201
1202#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
1203
1204#else
1205static stbi__uint32 stbi__get32be(stbi__context *s)
1206{
1207 stbi__uint32 z = stbi__get16be(s);
1208 return (z << 16) + stbi__get16be(s);
1209}
1210#endif
1211
1212#if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)
1213
1214#else
1215static int stbi__get16le(stbi__context *s)
1216{
1217 int z = stbi__get8(s);
1218 return z + (stbi__get8(s) << 8);
1219}
1220#endif
1221
1222#ifndef STBI_NO_BMP
1223static stbi__uint32 stbi__get32le(stbi__context *s)
1224{
1225 stbi__uint32 z = stbi__get16le(s);
1226 z += (stbi__uint32)stbi__get16le(s) << 16;
1227 return z;
1228}
1229#endif
1230
1231#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255))
1232
1233#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1234
1235#else
1236
1237static stbi_uc stbi__compute_y(int r, int g, int b)
1238{
1239 return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);
1240}
1241#endif
1242
1243#if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1244
1245#else
1246static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
1247{
1248 int i,j;
1249 unsigned char *good;
1250
1251 if (req_comp == img_n) return data;
1252 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1253
1254 good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);
1255 if (good == NULL) {
1256 STBI_FREE(data);
1257 return stbi__errpuc("outofmem", "Out of memory");
1258 }
1259
1260 for (j=0; j < (int) y; ++j) {
1261 unsigned char *src = data + j * x * img_n ;
1262 unsigned char *dest = good + j * x * req_comp;
1263
1264 #define STBI__COMBO(a,b) ((a)*8+(b))
1265 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1266
1267 switch (STBI__COMBO(img_n, req_comp)) {
1268 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break;
1269 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1270 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break;
1271 STBI__CASE(2,1) { dest[0]=src[0]; } break;
1272 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1273 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;
1274 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break;
1275 STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
1276 STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break;
1277 STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
1278 STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break;
1279 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;
1280 default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported", "Unsupported format conversion");
1281 }
1282 #undef STBI__CASE
1283 }
1284
1285 STBI_FREE(data);
1286 return good;
1287}
1288#endif
1289
1290#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1291
1292#else
1293static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
1294{
1295 return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);
1296}
1297#endif
1298
1299#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1300
1301#else
1302static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
1303{
1304 int i,j;
1305 stbi__uint16 *good;
1306
1307 if (req_comp == img_n) return data;
1308 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1309
1310 good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
1311 if (good == NULL) {
1312 STBI_FREE(data);
1313 return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
1314 }
1315
1316 for (j=0; j < (int) y; ++j) {
1317 stbi__uint16 *src = data + j * x * img_n ;
1318 stbi__uint16 *dest = good + j * x * req_comp;
1319
1320 #define STBI__COMBO(a,b) ((a)*8+(b))
1321 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1322
1323 switch (STBI__COMBO(img_n, req_comp)) {
1324 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break;
1325 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1326 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break;
1327 STBI__CASE(2,1) { dest[0]=src[0]; } break;
1328 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1329 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;
1330 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break;
1331 STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
1332 STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break;
1333 STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
1334 STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break;
1335 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;
1336 default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported", "Unsupported format conversion");
1337 }
1338 #undef STBI__CASE
1339 }
1340
1341 STBI_FREE(data);
1342 return good;
1343}
1344#endif
1345
1346#ifndef STBI_NO_LINEAR
1347static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
1348{
1349 int i,k,n;
1350 float *output;
1351 if (!data) return NULL;
1352 output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0);
1353 if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); }
1354
1355 if (comp & 1) n = comp; else n = comp-1;
1356 for (i=0; i < x*y; ++i) {
1357 for (k=0; k < n; ++k) {
1358 output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
1359 }
1360 }
1361 if (n < comp) {
1362 for (i=0; i < x*y; ++i) {
1363 output[i*comp + n] = data[i*comp + n]/255.0f;
1364 }
1365 }
1366 STBI_FREE(data);
1367 return output;
1368}
1369#endif
1370
1371#ifndef STBI_NO_HDR
1372#define stbi__float2int(x) ((int) (x))
1373static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)
1374{
1375 int i,k,n;
1376 stbi_uc *output;
1377 if (!data) return NULL;
1378 output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0);
1379 if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); }
1380
1381 if (comp & 1) n = comp; else n = comp-1;
1382 for (i=0; i < x*y; ++i) {
1383 for (k=0; k < n; ++k) {
1384 float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;
1385 if (z < 0) z = 0;
1386 if (z > 255) z = 255;
1387 output[i*comp + k] = (stbi_uc) stbi__float2int(z);
1388 }
1389 if (k < comp) {
1390 float z = data[i*comp+k] * 255 + 0.5f;
1391 if (z < 0) z = 0;
1392 if (z > 255) z = 255;
1393 output[i*comp + k] = (stbi_uc) stbi__float2int(z);
1394 }
1395 }
1396 STBI_FREE(data);
1397 return output;
1398}
1399#endif
1400
1401#ifndef STBI_NO_JPEG
1402
1403#define FAST_BITS 9
1404
1405typedef struct
1406{
1407 stbi_uc fast[1 << FAST_BITS];
1408
1409 stbi__uint16 code[256];
1410 stbi_uc values[256];
1411 stbi_uc size[257];
1412 unsigned int maxcode[18];
1413 int delta[17];
1414} stbi__huffman;
1415
1416typedef struct
1417{
1418 stbi__context *s;
1419 stbi__huffman huff_dc[4];
1420 stbi__huffman huff_ac[4];
1421 stbi__uint16 dequant[4][64];
1422 stbi__int16 fast_ac[4][1 << FAST_BITS];
1423
1424 int img_h_max, img_v_max;
1425 int img_mcu_x, img_mcu_y;
1426 int img_mcu_w, img_mcu_h;
1427
1428 struct
1429 {
1430 int id;
1431 int h,v;
1432 int tq;
1433 int hd,ha;
1434 int dc_pred;
1435
1436 int x,y,w2,h2;
1437 stbi_uc *data;
1438 void *raw_data, *raw_coeff;
1439 stbi_uc *linebuf;
1440 short *coeff;
1441 int coeff_w, coeff_h;
1442 } img_comp[4];
1443
1444 stbi__uint32 code_buffer;
1445 int code_bits;
1446 unsigned char marker;
1447 int nomore;
1448
1449 int progressive;
1450 int spec_start;
1451 int spec_end;
1452 int succ_high;
1453 int succ_low;
1454 int eob_run;
1455 int jfif;
1456 int app14_color_transform;
1457 int rgb;
1458
1459 int scan_n, order[4];
1460 int restart_interval, todo;
1461
1462 void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]);
1463 void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step);
1464 stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs);
1465} stbi__jpeg;
1466
1467static int stbi__build_huffman(stbi__huffman *h, int *count)
1468{
1469 int i,j,k=0;
1470 unsigned int code;
1471
1472 for (i=0; i < 16; ++i) {
1473 for (j=0; j < count[i]; ++j) {
1474 h->size[k++] = (stbi_uc) (i+1);
1475 if(k >= 257) return stbi__err("bad size list","Corrupt JPEG");
1476 }
1477 }
1478 h->size[k] = 0;
1479
1480 code = 0;
1481 k = 0;
1482 for(j=1; j <= 16; ++j) {
1483
1484 h->delta[j] = k - code;
1485 if (h->size[k] == j) {
1486 while (h->size[k] == j)
1487 h->code[k++] = (stbi__uint16) (code++);
1488 if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG");
1489 }
1490
1491 h->maxcode[j] = code << (16-j);
1492 code <<= 1;
1493 }
1494 h->maxcode[j] = 0xffffffff;
1495
1496 memset(h->fast, 255, 1 << FAST_BITS);
1497 for (i=0; i < k; ++i) {
1498 int s = h->size[i];
1499 if (s <= FAST_BITS) {
1500 int c = h->code[i] << (FAST_BITS-s);
1501 int m = 1 << (FAST_BITS-s);
1502 for (j=0; j < m; ++j) {
1503 h->fast[c+j] = (stbi_uc) i;
1504 }
1505 }
1506 }
1507 return 1;
1508}
1509
1510static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h)
1511{
1512 int i;
1513 for (i=0; i < (1 << FAST_BITS); ++i) {
1514 stbi_uc fast = h->fast[i];
1515 fast_ac[i] = 0;
1516 if (fast < 255) {
1517 int rs = h->values[fast];
1518 int run = (rs >> 4) & 15;
1519 int magbits = rs & 15;
1520 int len = h->size[fast];
1521
1522 if (magbits && len + magbits <= FAST_BITS) {
1523
1524 int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits);
1525 int m = 1 << (magbits - 1);
1526 if (k < m) k += (~0U << magbits) + 1;
1527
1528 if (k >= -128 && k <= 127)
1529 fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits));
1530 }
1531 }
1532 }
1533}
1534
1535static void stbi__grow_buffer_unsafe(stbi__jpeg *j)
1536{
1537 do {
1538 unsigned int b = j->nomore ? 0 : stbi__get8(j->s);
1539 if (b == 0xff) {
1540 int c = stbi__get8(j->s);
1541 while (c == 0xff) c = stbi__get8(j->s);
1542 if (c != 0) {
1543 j->marker = (unsigned char) c;
1544 j->nomore = 1;
1545 return;
1546 }
1547 }
1548 j->code_buffer |= b << (24 - j->code_bits);
1549 j->code_bits += 8;
1550 } while (j->code_bits <= 24);
1551}
1552
1553static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
1554
1555stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
1556{
1557 unsigned int temp;
1558 int c,k;
1559
1560 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1561
1562 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
1563 k = h->fast[c];
1564 if (k < 255) {
1565 int s = h->size[k];
1566 if (s > j->code_bits)
1567 return -1;
1568 j->code_buffer <<= s;
1569 j->code_bits -= s;
1570 return h->values[k];
1571 }
1572
1573 temp = j->code_buffer >> 16;
1574 for (k=FAST_BITS+1 ; ; ++k)
1575 if (temp < h->maxcode[k])
1576 break;
1577 if (k == 17) {
1578
1579 j->code_bits -= 16;
1580 return -1;
1581 }
1582
1583 if (k > j->code_bits)
1584 return -1;
1585
1586 c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
1587 if(c < 0 || c >= 256)
1588 return -1;
1589 STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
1590
1591 j->code_bits -= k;
1592 j->code_buffer <<= k;
1593 return h->values[c];
1594}
1595
1596static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};
1597
1598stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)
1599{
1600 unsigned int k;
1601 int sgn;
1602 if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
1603 if (j->code_bits < n) return 0;
1604
1605 sgn = j->code_buffer >> 31;
1606 k = stbi_lrot(j->code_buffer, n);
1607 j->code_buffer = k & ~stbi__bmask[n];
1608 k &= stbi__bmask[n];
1609 j->code_bits -= n;
1610 return k + (stbi__jbias[n] & (sgn - 1));
1611}
1612
1613stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n)
1614{
1615 unsigned int k;
1616 if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
1617 if (j->code_bits < n) return 0;
1618 k = stbi_lrot(j->code_buffer, n);
1619 j->code_buffer = k & ~stbi__bmask[n];
1620 k &= stbi__bmask[n];
1621 j->code_bits -= n;
1622 return k;
1623}
1624
1625stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j)
1626{
1627 unsigned int k;
1628 if (j->code_bits < 1) stbi__grow_buffer_unsafe(j);
1629 if (j->code_bits < 1) return 0;
1630 k = j->code_buffer;
1631 j->code_buffer <<= 1;
1632 --j->code_bits;
1633 return k & 0x80000000;
1634}
1635
1636static const stbi_uc stbi__jpeg_dezigzag[64+15] =
1637{
1638 0, 1, 8, 16, 9, 2, 3, 10,
1639 17, 24, 32, 25, 18, 11, 4, 5,
1640 12, 19, 26, 33, 40, 48, 41, 34,
1641 27, 20, 13, 6, 7, 14, 21, 28,
1642 35, 42, 49, 56, 57, 50, 43, 36,
1643 29, 22, 15, 23, 30, 37, 44, 51,
1644 58, 59, 52, 45, 38, 31, 39, 46,
1645 53, 60, 61, 54, 47, 55, 62, 63,
1646
1647 63, 63, 63, 63, 63, 63, 63, 63,
1648 63, 63, 63, 63, 63, 63, 63
1649};
1650
1651static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, stbi__int16 *fac, int b, stbi__uint16 *dequant)
1652{
1653 int diff,dc,k;
1654 int t;
1655
1656 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1657 t = stbi__jpeg_huff_decode(j, hdc);
1658 if (t < 0 || t > 15) return stbi__err("bad huffman code","Corrupt JPEG");
1659
1660 memset(data,0,64*sizeof(data[0]));
1661
1662 diff = t ? stbi__extend_receive(j, t) : 0;
1663 if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta","Corrupt JPEG");
1664 dc = j->img_comp[b].dc_pred + diff;
1665 j->img_comp[b].dc_pred = dc;
1666 if (!stbi__mul2shorts_valid(dc, dequant[0])) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
1667 data[0] = (short) (dc * dequant[0]);
1668
1669 k = 1;
1670 do {
1671 unsigned int zig;
1672 int c,r,s;
1673 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1674 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
1675 r = fac[c];
1676 if (r) {
1677 k += (r >> 4) & 15;
1678 s = r & 15;
1679 if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available");
1680 j->code_buffer <<= s;
1681 j->code_bits -= s;
1682
1683 zig = stbi__jpeg_dezigzag[k++];
1684 data[zig] = (short) ((r >> 8) * dequant[zig]);
1685 } else {
1686 int rs = stbi__jpeg_huff_decode(j, hac);
1687 if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
1688 s = rs & 15;
1689 r = rs >> 4;
1690 if (s == 0) {
1691 if (rs != 0xf0) break;
1692 k += 16;
1693 } else {
1694 k += r;
1695
1696 zig = stbi__jpeg_dezigzag[k++];
1697 data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]);
1698 }
1699 }
1700 } while (k < 64);
1701 return 1;
1702}
1703
1704static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b)
1705{
1706 int diff,dc;
1707 int t;
1708 if (j->spec_end != 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
1709
1710 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1711
1712 if (j->succ_high == 0) {
1713
1714 memset(data,0,64*sizeof(data[0]));
1715 t = stbi__jpeg_huff_decode(j, hdc);
1716 if (t < 0 || t > 15) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
1717 diff = t ? stbi__extend_receive(j, t) : 0;
1718
1719 if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta", "Corrupt JPEG");
1720 dc = j->img_comp[b].dc_pred + diff;
1721 j->img_comp[b].dc_pred = dc;
1722 if (!stbi__mul2shorts_valid(dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
1723 data[0] = (short) (dc * (1 << j->succ_low));
1724 } else {
1725
1726 if (stbi__jpeg_get_bit(j))
1727 data[0] += (short) (1 << j->succ_low);
1728 }
1729 return 1;
1730}
1731
1732static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac)
1733{
1734 int k;
1735 if (j->spec_start == 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
1736
1737 if (j->succ_high == 0) {
1738 int shift = j->succ_low;
1739
1740 if (j->eob_run) {
1741 --j->eob_run;
1742 return 1;
1743 }
1744
1745 k = j->spec_start;
1746 do {
1747 unsigned int zig;
1748 int c,r,s;
1749 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1750 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
1751 r = fac[c];
1752 if (r) {
1753 k += (r >> 4) & 15;
1754 s = r & 15;
1755 if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available");
1756 j->code_buffer <<= s;
1757 j->code_bits -= s;
1758 zig = stbi__jpeg_dezigzag[k++];
1759 data[zig] = (short) ((r >> 8) * (1 << shift));
1760 } else {
1761 int rs = stbi__jpeg_huff_decode(j, hac);
1762 if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
1763 s = rs & 15;
1764 r = rs >> 4;
1765 if (s == 0) {
1766 if (r < 15) {
1767 j->eob_run = (1 << r);
1768 if (r)
1769 j->eob_run += stbi__jpeg_get_bits(j, r);
1770 --j->eob_run;
1771 break;
1772 }
1773 k += 16;
1774 } else {
1775 k += r;
1776 zig = stbi__jpeg_dezigzag[k++];
1777 data[zig] = (short) (stbi__extend_receive(j,s) * (1 << shift));
1778 }
1779 }
1780 } while (k <= j->spec_end);
1781 } else {
1782
1783 short bit = (short) (1 << j->succ_low);
1784
1785 if (j->eob_run) {
1786 --j->eob_run;
1787 for (k = j->spec_start; k <= j->spec_end; ++k) {
1788 short *p = &data[stbi__jpeg_dezigzag[k]];
1789 if (*p != 0)
1790 if (stbi__jpeg_get_bit(j))
1791 if ((*p & bit)==0) {
1792 if (*p > 0)
1793 *p += bit;
1794 else
1795 *p -= bit;
1796 }
1797 }
1798 } else {
1799 k = j->spec_start;
1800 do {
1801 int r,s;
1802 int rs = stbi__jpeg_huff_decode(j, hac);
1803 if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
1804 s = rs & 15;
1805 r = rs >> 4;
1806 if (s == 0) {
1807 if (r < 15) {
1808 j->eob_run = (1 << r) - 1;
1809 if (r)
1810 j->eob_run += stbi__jpeg_get_bits(j, r);
1811 r = 64;
1812 } else {
1813
1814 }
1815 } else {
1816 if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG");
1817
1818 if (stbi__jpeg_get_bit(j))
1819 s = bit;
1820 else
1821 s = -bit;
1822 }
1823
1824 while (k <= j->spec_end) {
1825 short *p = &data[stbi__jpeg_dezigzag[k++]];
1826 if (*p != 0) {
1827 if (stbi__jpeg_get_bit(j))
1828 if ((*p & bit)==0) {
1829 if (*p > 0)
1830 *p += bit;
1831 else
1832 *p -= bit;
1833 }
1834 } else {
1835 if (r == 0) {
1836 *p = (short) s;
1837 break;
1838 }
1839 --r;
1840 }
1841 }
1842 } while (k <= j->spec_end);
1843 }
1844 }
1845 return 1;
1846}
1847
1848stbi_inline static stbi_uc stbi__clamp(int x)
1849{
1850
1851 if ((unsigned int) x > 255) {
1852 if (x < 0) return 0;
1853 if (x > 255) return 255;
1854 }
1855 return (stbi_uc) x;
1856}
1857
1858#define stbi__f2f(x) ((int) (((x) * 4096 + 0.5)))
1859#define stbi__fsh(x) ((x) * 4096)
1860
1861#define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
1862 int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
1863 p2 = s2; \
1864 p3 = s6; \
1865 p1 = (p2+p3) * stbi__f2f(0.5411961f); \
1866 t2 = p1 + p3*stbi__f2f(-1.847759065f); \
1867 t3 = p1 + p2*stbi__f2f( 0.765366865f); \
1868 p2 = s0; \
1869 p3 = s4; \
1870 t0 = stbi__fsh(p2+p3); \
1871 t1 = stbi__fsh(p2-p3); \
1872 x0 = t0+t3; \
1873 x3 = t0-t3; \
1874 x1 = t1+t2; \
1875 x2 = t1-t2; \
1876 t0 = s7; \
1877 t1 = s5; \
1878 t2 = s3; \
1879 t3 = s1; \
1880 p3 = t0+t2; \
1881 p4 = t1+t3; \
1882 p1 = t0+t3; \
1883 p2 = t1+t2; \
1884 p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
1885 t0 = t0*stbi__f2f( 0.298631336f); \
1886 t1 = t1*stbi__f2f( 2.053119869f); \
1887 t2 = t2*stbi__f2f( 3.072711026f); \
1888 t3 = t3*stbi__f2f( 1.501321110f); \
1889 p1 = p5 + p1*stbi__f2f(-0.899976223f); \
1890 p2 = p5 + p2*stbi__f2f(-2.562915447f); \
1891 p3 = p3*stbi__f2f(-1.961570560f); \
1892 p4 = p4*stbi__f2f(-0.390180644f); \
1893 t3 += p1+p4; \
1894 t2 += p2+p3; \
1895 t1 += p2+p4; \
1896 t0 += p1+p3;
1897
1898static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64])
1899{
1900 int i,val[64],*v=val;
1901 stbi_uc *o;
1902 short *d = data;
1903
1904 for (i=0; i < 8; ++i,++d, ++v) {
1905
1906 if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
1907 && d[40]==0 && d[48]==0 && d[56]==0) {
1908
1909 int dcterm = d[0]*4;
1910 v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
1911 } else {
1912 STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56])
1913
1914 x0 += 512; x1 += 512; x2 += 512; x3 += 512;
1915 v[ 0] = (x0+t3) >> 10;
1916 v[56] = (x0-t3) >> 10;
1917 v[ 8] = (x1+t2) >> 10;
1918 v[48] = (x1-t2) >> 10;
1919 v[16] = (x2+t1) >> 10;
1920 v[40] = (x2-t1) >> 10;
1921 v[24] = (x3+t0) >> 10;
1922 v[32] = (x3-t0) >> 10;
1923 }
1924 }
1925
1926 for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
1927
1928 STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
1929
1930 x0 += 65536 + (128<<17);
1931 x1 += 65536 + (128<<17);
1932 x2 += 65536 + (128<<17);
1933 x3 += 65536 + (128<<17);
1934
1935 o[0] = stbi__clamp((x0+t3) >> 17);
1936 o[7] = stbi__clamp((x0-t3) >> 17);
1937 o[1] = stbi__clamp((x1+t2) >> 17);
1938 o[6] = stbi__clamp((x1-t2) >> 17);
1939 o[2] = stbi__clamp((x2+t1) >> 17);
1940 o[5] = stbi__clamp((x2-t1) >> 17);
1941 o[3] = stbi__clamp((x3+t0) >> 17);
1942 o[4] = stbi__clamp((x3-t0) >> 17);
1943 }
1944}
1945
1946#ifdef STBI_SSE2
1947
1948static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
1949{
1950
1951 __m128i row0, row1, row2, row3, row4, row5, row6, row7;
1952 __m128i tmp;
1953
1954 #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y))
1955
1956 #define dct_rot(out0,out1, x,y,c0,c1) \
1957 __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \
1958 __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \
1959 __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \
1960 __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \
1961 __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \
1962 __m128i out1##_h = _mm_madd_epi16(c0##hi, c1)
1963
1964 #define dct_widen(out, in) \
1965 __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \
1966 __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4)
1967
1968 #define dct_wadd(out, a, b) \
1969 __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \
1970 __m128i out##_h = _mm_add_epi32(a##_h, b##_h)
1971
1972 #define dct_wsub(out, a, b) \
1973 __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \
1974 __m128i out##_h = _mm_sub_epi32(a##_h, b##_h)
1975
1976 #define dct_bfly32o(out0, out1, a,b,bias,s) \
1977 { \
1978 __m128i abiased_l = _mm_add_epi32(a##_l, bias); \
1979 __m128i abiased_h = _mm_add_epi32(a##_h, bias); \
1980 dct_wadd(sum, abiased, b); \
1981 dct_wsub(dif, abiased, b); \
1982 out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \
1983 out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \
1984 }
1985
1986 #define dct_interleave8(a, b) \
1987 tmp = a; \
1988 a = _mm_unpacklo_epi8(a, b); \
1989 b = _mm_unpackhi_epi8(tmp, b)
1990
1991 #define dct_interleave16(a, b) \
1992 tmp = a; \
1993 a = _mm_unpacklo_epi16(a, b); \
1994 b = _mm_unpackhi_epi16(tmp, b)
1995
1996 #define dct_pass(bias,shift) \
1997 { \
1998 \
1999 dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \
2000 __m128i sum04 = _mm_add_epi16(row0, row4); \
2001 __m128i dif04 = _mm_sub_epi16(row0, row4); \
2002 dct_widen(t0e, sum04); \
2003 dct_widen(t1e, dif04); \
2004 dct_wadd(x0, t0e, t3e); \
2005 dct_wsub(x3, t0e, t3e); \
2006 dct_wadd(x1, t1e, t2e); \
2007 dct_wsub(x2, t1e, t2e); \
2008 \
2009 dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \
2010 dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \
2011 __m128i sum17 = _mm_add_epi16(row1, row7); \
2012 __m128i sum35 = _mm_add_epi16(row3, row5); \
2013 dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \
2014 dct_wadd(x4, y0o, y4o); \
2015 dct_wadd(x5, y1o, y5o); \
2016 dct_wadd(x6, y2o, y5o); \
2017 dct_wadd(x7, y3o, y4o); \
2018 dct_bfly32o(row0,row7, x0,x7,bias,shift); \
2019 dct_bfly32o(row1,row6, x1,x6,bias,shift); \
2020 dct_bfly32o(row2,row5, x2,x5,bias,shift); \
2021 dct_bfly32o(row3,row4, x3,x4,bias,shift); \
2022 }
2023
2024 __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f));
2025 __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f));
2026 __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f));
2027 __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f));
2028 __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f));
2029 __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f));
2030 __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f));
2031 __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f));
2032
2033 __m128i bias_0 = _mm_set1_epi32(512);
2034 __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17));
2035
2036 row0 = _mm_load_si128((const __m128i *) (data + 0*8));
2037 row1 = _mm_load_si128((const __m128i *) (data + 1*8));
2038 row2 = _mm_load_si128((const __m128i *) (data + 2*8));
2039 row3 = _mm_load_si128((const __m128i *) (data + 3*8));
2040 row4 = _mm_load_si128((const __m128i *) (data + 4*8));
2041 row5 = _mm_load_si128((const __m128i *) (data + 5*8));
2042 row6 = _mm_load_si128((const __m128i *) (data + 6*8));
2043 row7 = _mm_load_si128((const __m128i *) (data + 7*8));
2044
2045 dct_pass(bias_0, 10);
2046
2047 {
2048
2049 dct_interleave16(row0, row4);
2050 dct_interleave16(row1, row5);
2051 dct_interleave16(row2, row6);
2052 dct_interleave16(row3, row7);
2053
2054 dct_interleave16(row0, row2);
2055 dct_interleave16(row1, row3);
2056 dct_interleave16(row4, row6);
2057 dct_interleave16(row5, row7);
2058
2059 dct_interleave16(row0, row1);
2060 dct_interleave16(row2, row3);
2061 dct_interleave16(row4, row5);
2062 dct_interleave16(row6, row7);
2063 }
2064
2065 dct_pass(bias_1, 17);
2066
2067 {
2068
2069 __m128i p0 = _mm_packus_epi16(row0, row1);
2070 __m128i p1 = _mm_packus_epi16(row2, row3);
2071 __m128i p2 = _mm_packus_epi16(row4, row5);
2072 __m128i p3 = _mm_packus_epi16(row6, row7);
2073
2074 dct_interleave8(p0, p2);
2075 dct_interleave8(p1, p3);
2076
2077 dct_interleave8(p0, p1);
2078 dct_interleave8(p2, p3);
2079
2080 dct_interleave8(p0, p2);
2081 dct_interleave8(p1, p3);
2082
2083 _mm_storel_epi64((__m128i *) out, p0); out += out_stride;
2084 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride;
2085 _mm_storel_epi64((__m128i *) out, p2); out += out_stride;
2086 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride;
2087 _mm_storel_epi64((__m128i *) out, p1); out += out_stride;
2088 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride;
2089 _mm_storel_epi64((__m128i *) out, p3); out += out_stride;
2090 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e));
2091 }
2092
2093#undef dct_const
2094#undef dct_rot
2095#undef dct_widen
2096#undef dct_wadd
2097#undef dct_wsub
2098#undef dct_bfly32o
2099#undef dct_interleave8
2100#undef dct_interleave16
2101#undef dct_pass
2102}
2103
2104#endif
2105
2106#ifdef STBI_NEON
2107
2108static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
2109{
2110 int16x8_t row0, row1, row2, row3, row4, row5, row6, row7;
2111
2112 int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f));
2113 int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f));
2114 int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f));
2115 int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f));
2116 int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f));
2117 int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f));
2118 int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f));
2119 int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f));
2120 int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f));
2121 int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f));
2122 int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f));
2123 int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f));
2124
2125#define dct_long_mul(out, inq, coeff) \
2126 int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \
2127 int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff)
2128
2129#define dct_long_mac(out, acc, inq, coeff) \
2130 int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \
2131 int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff)
2132
2133#define dct_widen(out, inq) \
2134 int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \
2135 int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12)
2136
2137#define dct_wadd(out, a, b) \
2138 int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \
2139 int32x4_t out##_h = vaddq_s32(a##_h, b##_h)
2140
2141#define dct_wsub(out, a, b) \
2142 int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \
2143 int32x4_t out##_h = vsubq_s32(a##_h, b##_h)
2144
2145#define dct_bfly32o(out0,out1, a,b,shiftop,s) \
2146 { \
2147 dct_wadd(sum, a, b); \
2148 dct_wsub(dif, a, b); \
2149 out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \
2150 out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \
2151 }
2152
2153#define dct_pass(shiftop, shift) \
2154 { \
2155 \
2156 int16x8_t sum26 = vaddq_s16(row2, row6); \
2157 dct_long_mul(p1e, sum26, rot0_0); \
2158 dct_long_mac(t2e, p1e, row6, rot0_1); \
2159 dct_long_mac(t3e, p1e, row2, rot0_2); \
2160 int16x8_t sum04 = vaddq_s16(row0, row4); \
2161 int16x8_t dif04 = vsubq_s16(row0, row4); \
2162 dct_widen(t0e, sum04); \
2163 dct_widen(t1e, dif04); \
2164 dct_wadd(x0, t0e, t3e); \
2165 dct_wsub(x3, t0e, t3e); \
2166 dct_wadd(x1, t1e, t2e); \
2167 dct_wsub(x2, t1e, t2e); \
2168 \
2169 int16x8_t sum15 = vaddq_s16(row1, row5); \
2170 int16x8_t sum17 = vaddq_s16(row1, row7); \
2171 int16x8_t sum35 = vaddq_s16(row3, row5); \
2172 int16x8_t sum37 = vaddq_s16(row3, row7); \
2173 int16x8_t sumodd = vaddq_s16(sum17, sum35); \
2174 dct_long_mul(p5o, sumodd, rot1_0); \
2175 dct_long_mac(p1o, p5o, sum17, rot1_1); \
2176 dct_long_mac(p2o, p5o, sum35, rot1_2); \
2177 dct_long_mul(p3o, sum37, rot2_0); \
2178 dct_long_mul(p4o, sum15, rot2_1); \
2179 dct_wadd(sump13o, p1o, p3o); \
2180 dct_wadd(sump24o, p2o, p4o); \
2181 dct_wadd(sump23o, p2o, p3o); \
2182 dct_wadd(sump14o, p1o, p4o); \
2183 dct_long_mac(x4, sump13o, row7, rot3_0); \
2184 dct_long_mac(x5, sump24o, row5, rot3_1); \
2185 dct_long_mac(x6, sump23o, row3, rot3_2); \
2186 dct_long_mac(x7, sump14o, row1, rot3_3); \
2187 dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \
2188 dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \
2189 dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \
2190 dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \
2191 }
2192
2193 row0 = vld1q_s16(data + 0*8);
2194 row1 = vld1q_s16(data + 1*8);
2195 row2 = vld1q_s16(data + 2*8);
2196 row3 = vld1q_s16(data + 3*8);
2197 row4 = vld1q_s16(data + 4*8);
2198 row5 = vld1q_s16(data + 5*8);
2199 row6 = vld1q_s16(data + 6*8);
2200 row7 = vld1q_s16(data + 7*8);
2201
2202 row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0));
2203
2204 dct_pass(vrshrn_n_s32, 10);
2205
2206 {
2207
2208#define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; }
2209#define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); }
2210#define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); }
2211
2212 dct_trn16(row0, row1);
2213 dct_trn16(row2, row3);
2214 dct_trn16(row4, row5);
2215 dct_trn16(row6, row7);
2216
2217 dct_trn32(row0, row2);
2218 dct_trn32(row1, row3);
2219 dct_trn32(row4, row6);
2220 dct_trn32(row5, row7);
2221
2222 dct_trn64(row0, row4);
2223 dct_trn64(row1, row5);
2224 dct_trn64(row2, row6);
2225 dct_trn64(row3, row7);
2226
2227#undef dct_trn16
2228#undef dct_trn32
2229#undef dct_trn64
2230 }
2231
2232 dct_pass(vshrn_n_s32, 16);
2233
2234 {
2235
2236 uint8x8_t p0 = vqrshrun_n_s16(row0, 1);
2237 uint8x8_t p1 = vqrshrun_n_s16(row1, 1);
2238 uint8x8_t p2 = vqrshrun_n_s16(row2, 1);
2239 uint8x8_t p3 = vqrshrun_n_s16(row3, 1);
2240 uint8x8_t p4 = vqrshrun_n_s16(row4, 1);
2241 uint8x8_t p5 = vqrshrun_n_s16(row5, 1);
2242 uint8x8_t p6 = vqrshrun_n_s16(row6, 1);
2243 uint8x8_t p7 = vqrshrun_n_s16(row7, 1);
2244
2245#define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; }
2246#define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); }
2247#define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); }
2248
2249 dct_trn8_8(p0, p1);
2250 dct_trn8_8(p2, p3);
2251 dct_trn8_8(p4, p5);
2252 dct_trn8_8(p6, p7);
2253
2254 dct_trn8_16(p0, p2);
2255 dct_trn8_16(p1, p3);
2256 dct_trn8_16(p4, p6);
2257 dct_trn8_16(p5, p7);
2258
2259 dct_trn8_32(p0, p4);
2260 dct_trn8_32(p1, p5);
2261 dct_trn8_32(p2, p6);
2262 dct_trn8_32(p3, p7);
2263
2264 vst1_u8(out, p0); out += out_stride;
2265 vst1_u8(out, p1); out += out_stride;
2266 vst1_u8(out, p2); out += out_stride;
2267 vst1_u8(out, p3); out += out_stride;
2268 vst1_u8(out, p4); out += out_stride;
2269 vst1_u8(out, p5); out += out_stride;
2270 vst1_u8(out, p6); out += out_stride;
2271 vst1_u8(out, p7);
2272
2273#undef dct_trn8_8
2274#undef dct_trn8_16
2275#undef dct_trn8_32
2276 }
2277
2278#undef dct_long_mul
2279#undef dct_long_mac
2280#undef dct_widen
2281#undef dct_wadd
2282#undef dct_wsub
2283#undef dct_bfly32o
2284#undef dct_pass
2285}
2286
2287#endif
2288
2289#define STBI__MARKER_none 0xff
2290
2291static stbi_uc stbi__get_marker(stbi__jpeg *j)
2292{
2293 stbi_uc x;
2294 if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; }
2295 x = stbi__get8(j->s);
2296 if (x != 0xff) return STBI__MARKER_none;
2297 while (x == 0xff)
2298 x = stbi__get8(j->s);
2299 return x;
2300}
2301
2302#define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
2303
2304static void stbi__jpeg_reset(stbi__jpeg *j)
2305{
2306 j->code_bits = 0;
2307 j->code_buffer = 0;
2308 j->nomore = 0;
2309 j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = j->img_comp[3].dc_pred = 0;
2310 j->marker = STBI__MARKER_none;
2311 j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
2312 j->eob_run = 0;
2313
2314}
2315
2316static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
2317{
2318 stbi__jpeg_reset(z);
2319 if (!z->progressive) {
2320 if (z->scan_n == 1) {
2321 int i,j;
2322 STBI_SIMD_ALIGN(short, data[64]);
2323 int n = z->order[0];
2324
2325 int w = (z->img_comp[n].x+7) >> 3;
2326 int h = (z->img_comp[n].y+7) >> 3;
2327 for (j=0; j < h; ++j) {
2328 for (i=0; i < w; ++i) {
2329 int ha = z->img_comp[n].ha;
2330 if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0;
2331 z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data);
2332
2333 if (--z->todo <= 0) {
2334 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
2335
2336 if (!STBI__RESTART(z->marker)) return 1;
2337 stbi__jpeg_reset(z);
2338 }
2339 }
2340 }
2341 return 1;
2342 } else {
2343 int i,j,k,x,y;
2344 STBI_SIMD_ALIGN(short, data[64]);
2345 for (j=0; j < z->img_mcu_y; ++j) {
2346 for (i=0; i < z->img_mcu_x; ++i) {
2347
2348 for (k=0; k < z->scan_n; ++k) {
2349 int n = z->order[k];
2350
2351 for (y=0; y < z->img_comp[n].v; ++y) {
2352 for (x=0; x < z->img_comp[n].h; ++x) {
2353 int x2 = (i*z->img_comp[n].h + x)*8;
2354 int y2 = (j*z->img_comp[n].v + y)*8;
2355 int ha = z->img_comp[n].ha;
2356 if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0;
2357 z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data);
2358 }
2359 }
2360 }
2361
2362 if (--z->todo <= 0) {
2363 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
2364 if (!STBI__RESTART(z->marker)) return 1;
2365 stbi__jpeg_reset(z);
2366 }
2367 }
2368 }
2369 return 1;
2370 }
2371 } else {
2372 if (z->scan_n == 1) {
2373 int i,j;
2374 int n = z->order[0];
2375
2376 int w = (z->img_comp[n].x+7) >> 3;
2377 int h = (z->img_comp[n].y+7) >> 3;
2378 for (j=0; j < h; ++j) {
2379 for (i=0; i < w; ++i) {
2380 short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
2381 if (z->spec_start == 0) {
2382 if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
2383 return 0;
2384 } else {
2385 int ha = z->img_comp[n].ha;
2386 if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha]))
2387 return 0;
2388 }
2389
2390 if (--z->todo <= 0) {
2391 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
2392 if (!STBI__RESTART(z->marker)) return 1;
2393 stbi__jpeg_reset(z);
2394 }
2395 }
2396 }
2397 return 1;
2398 } else {
2399 int i,j,k,x,y;
2400 for (j=0; j < z->img_mcu_y; ++j) {
2401 for (i=0; i < z->img_mcu_x; ++i) {
2402
2403 for (k=0; k < z->scan_n; ++k) {
2404 int n = z->order[k];
2405
2406 for (y=0; y < z->img_comp[n].v; ++y) {
2407 for (x=0; x < z->img_comp[n].h; ++x) {
2408 int x2 = (i*z->img_comp[n].h + x);
2409 int y2 = (j*z->img_comp[n].v + y);
2410 short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w);
2411 if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
2412 return 0;
2413 }
2414 }
2415 }
2416
2417 if (--z->todo <= 0) {
2418 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
2419 if (!STBI__RESTART(z->marker)) return 1;
2420 stbi__jpeg_reset(z);
2421 }
2422 }
2423 }
2424 return 1;
2425 }
2426 }
2427}
2428
2429static void stbi__jpeg_dequantize(short *data, stbi__uint16 *dequant)
2430{
2431 int i;
2432 for (i=0; i < 64; ++i)
2433 data[i] *= dequant[i];
2434}
2435
2436static void stbi__jpeg_finish(stbi__jpeg *z)
2437{
2438 if (z->progressive) {
2439
2440 int i,j,n;
2441 for (n=0; n < z->s->img_n; ++n) {
2442 int w = (z->img_comp[n].x+7) >> 3;
2443 int h = (z->img_comp[n].y+7) >> 3;
2444 for (j=0; j < h; ++j) {
2445 for (i=0; i < w; ++i) {
2446 short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
2447 stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]);
2448 z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data);
2449 }
2450 }
2451 }
2452 }
2453}
2454
2455static int stbi__process_marker(stbi__jpeg *z, int m)
2456{
2457 int L;
2458 switch (m) {
2459 case STBI__MARKER_none:
2460 return stbi__err("expected marker","Corrupt JPEG");
2461
2462 case 0xDD:
2463 if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG");
2464 z->restart_interval = stbi__get16be(z->s);
2465 return 1;
2466
2467 case 0xDB:
2468 L = stbi__get16be(z->s)-2;
2469 while (L > 0) {
2470 int q = stbi__get8(z->s);
2471 int p = q >> 4, sixteen = (p != 0);
2472 int t = q & 15,i;
2473 if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG");
2474 if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG");
2475
2476 for (i=0; i < 64; ++i)
2477 z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s));
2478 L -= (sixteen ? 129 : 65);
2479 }
2480 return L==0;
2481
2482 case 0xC4:
2483 L = stbi__get16be(z->s)-2;
2484 while (L > 0) {
2485 stbi_uc *v;
2486 int sizes[16],i,n=0;
2487 int q = stbi__get8(z->s);
2488 int tc = q >> 4;
2489 int th = q & 15;
2490 if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG");
2491 for (i=0; i < 16; ++i) {
2492 sizes[i] = stbi__get8(z->s);
2493 n += sizes[i];
2494 }
2495 if(n > 256) return stbi__err("bad DHT header","Corrupt JPEG");
2496 L -= 17;
2497 if (tc == 0) {
2498 if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0;
2499 v = z->huff_dc[th].values;
2500 } else {
2501 if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0;
2502 v = z->huff_ac[th].values;
2503 }
2504 for (i=0; i < n; ++i)
2505 v[i] = stbi__get8(z->s);
2506 if (tc != 0)
2507 stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th);
2508 L -= n;
2509 }
2510 return L==0;
2511 }
2512
2513 if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
2514 L = stbi__get16be(z->s);
2515 if (L < 2) {
2516 if (m == 0xFE)
2517 return stbi__err("bad COM len","Corrupt JPEG");
2518 else
2519 return stbi__err("bad APP len","Corrupt JPEG");
2520 }
2521 L -= 2;
2522
2523 if (m == 0xE0 && L >= 5) {
2524 static const unsigned char tag[5] = {'J','F','I','F','\0'};
2525 int ok = 1;
2526 int i;
2527 for (i=0; i < 5; ++i)
2528 if (stbi__get8(z->s) != tag[i])
2529 ok = 0;
2530 L -= 5;
2531 if (ok)
2532 z->jfif = 1;
2533 } else if (m == 0xEE && L >= 12) {
2534 static const unsigned char tag[6] = {'A','d','o','b','e','\0'};
2535 int ok = 1;
2536 int i;
2537 for (i=0; i < 6; ++i)
2538 if (stbi__get8(z->s) != tag[i])
2539 ok = 0;
2540 L -= 6;
2541 if (ok) {
2542 stbi__get8(z->s);
2543 stbi__get16be(z->s);
2544 stbi__get16be(z->s);
2545 z->app14_color_transform = stbi__get8(z->s);
2546 L -= 6;
2547 }
2548 }
2549
2550 stbi__skip(z->s, L);
2551 return 1;
2552 }
2553
2554 return stbi__err("unknown marker","Corrupt JPEG");
2555}
2556
2557static int stbi__process_scan_header(stbi__jpeg *z)
2558{
2559 int i;
2560 int Ls = stbi__get16be(z->s);
2561 z->scan_n = stbi__get8(z->s);
2562 if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad SOS component count","Corrupt JPEG");
2563 if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len","Corrupt JPEG");
2564 for (i=0; i < z->scan_n; ++i) {
2565 int id = stbi__get8(z->s), which;
2566 int q = stbi__get8(z->s);
2567 for (which = 0; which < z->s->img_n; ++which)
2568 if (z->img_comp[which].id == id)
2569 break;
2570 if (which == z->s->img_n) return 0;
2571 z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG");
2572 z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG");
2573 z->order[i] = which;
2574 }
2575
2576 {
2577 int aa;
2578 z->spec_start = stbi__get8(z->s);
2579 z->spec_end = stbi__get8(z->s);
2580 aa = stbi__get8(z->s);
2581 z->succ_high = (aa >> 4);
2582 z->succ_low = (aa & 15);
2583 if (z->progressive) {
2584 if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13)
2585 return stbi__err("bad SOS", "Corrupt JPEG");
2586 } else {
2587 if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG");
2588 if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG");
2589 z->spec_end = 63;
2590 }
2591 }
2592
2593 return 1;
2594}
2595
2596static int stbi__free_jpeg_components(stbi__jpeg *z, int ncomp, int why)
2597{
2598 int i;
2599 for (i=0; i < ncomp; ++i) {
2600 if (z->img_comp[i].raw_data) {
2601 STBI_FREE(z->img_comp[i].raw_data);
2602 z->img_comp[i].raw_data = NULL;
2603 z->img_comp[i].data = NULL;
2604 }
2605 if (z->img_comp[i].raw_coeff) {
2606 STBI_FREE(z->img_comp[i].raw_coeff);
2607 z->img_comp[i].raw_coeff = 0;
2608 z->img_comp[i].coeff = 0;
2609 }
2610 if (z->img_comp[i].linebuf) {
2611 STBI_FREE(z->img_comp[i].linebuf);
2612 z->img_comp[i].linebuf = NULL;
2613 }
2614 }
2615 return why;
2616}
2617
2618static int stbi__process_frame_header(stbi__jpeg *z, int scan)
2619{
2620 stbi__context *s = z->s;
2621 int Lf,p,i,q, h_max=1,v_max=1,c;
2622 Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG");
2623 p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only");
2624 s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height");
2625 s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG");
2626 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
2627 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
2628 c = stbi__get8(s);
2629 if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG");
2630 s->img_n = c;
2631 for (i=0; i < c; ++i) {
2632 z->img_comp[i].data = NULL;
2633 z->img_comp[i].linebuf = NULL;
2634 }
2635
2636 if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG");
2637
2638 z->rgb = 0;
2639 for (i=0; i < s->img_n; ++i) {
2640 static const unsigned char rgb[3] = { 'R', 'G', 'B' };
2641 z->img_comp[i].id = stbi__get8(s);
2642 if (s->img_n == 3 && z->img_comp[i].id == rgb[i])
2643 ++z->rgb;
2644 q = stbi__get8(s);
2645 z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG");
2646 z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG");
2647 z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG");
2648 }
2649
2650 if (scan != STBI__SCAN_load) return 1;
2651
2652 if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode");
2653
2654 for (i=0; i < s->img_n; ++i) {
2655 if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
2656 if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
2657 }
2658
2659 for (i=0; i < s->img_n; ++i) {
2660 if (h_max % z->img_comp[i].h != 0) return stbi__err("bad H","Corrupt JPEG");
2661 if (v_max % z->img_comp[i].v != 0) return stbi__err("bad V","Corrupt JPEG");
2662 }
2663
2664 z->img_h_max = h_max;
2665 z->img_v_max = v_max;
2666 z->img_mcu_w = h_max * 8;
2667 z->img_mcu_h = v_max * 8;
2668
2669 z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
2670 z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
2671
2672 for (i=0; i < s->img_n; ++i) {
2673
2674 z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
2675 z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
2676
2677 z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
2678 z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
2679 z->img_comp[i].coeff = 0;
2680 z->img_comp[i].raw_coeff = 0;
2681 z->img_comp[i].linebuf = NULL;
2682 z->img_comp[i].raw_data = stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15);
2683 if (z->img_comp[i].raw_data == NULL)
2684 return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory"));
2685
2686 z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
2687 if (z->progressive) {
2688
2689 z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8;
2690 z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8;
2691 z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15);
2692 if (z->img_comp[i].raw_coeff == NULL)
2693 return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory"));
2694 z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15);
2695 }
2696 }
2697
2698 return 1;
2699}
2700
2701#define stbi__DNL(x) ((x) == 0xdc)
2702#define stbi__SOI(x) ((x) == 0xd8)
2703#define stbi__EOI(x) ((x) == 0xd9)
2704#define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2)
2705#define stbi__SOS(x) ((x) == 0xda)
2706
2707#define stbi__SOF_progressive(x) ((x) == 0xc2)
2708
2709static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan)
2710{
2711 int m;
2712 z->jfif = 0;
2713 z->app14_color_transform = -1;
2714 z->marker = STBI__MARKER_none;
2715 m = stbi__get_marker(z);
2716 if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG");
2717 if (scan == STBI__SCAN_type) return 1;
2718 m = stbi__get_marker(z);
2719 while (!stbi__SOF(m)) {
2720 if (!stbi__process_marker(z,m)) return 0;
2721 m = stbi__get_marker(z);
2722 while (m == STBI__MARKER_none) {
2723
2724 if (stbi__at_eof(z->s)) return stbi__err("no SOF", "Corrupt JPEG");
2725 m = stbi__get_marker(z);
2726 }
2727 }
2728 z->progressive = stbi__SOF_progressive(m);
2729 if (!stbi__process_frame_header(z, scan)) return 0;
2730 return 1;
2731}
2732
2733static stbi_uc stbi__skip_jpeg_junk_at_end(stbi__jpeg *j)
2734{
2735
2736 while (!stbi__at_eof(j->s)) {
2737 stbi_uc x = stbi__get8(j->s);
2738 while (x == 0xff) {
2739 if (stbi__at_eof(j->s)) return STBI__MARKER_none;
2740 x = stbi__get8(j->s);
2741 if (x != 0x00 && x != 0xff) {
2742
2743 return x;
2744 }
2745
2746 }
2747 }
2748 return STBI__MARKER_none;
2749}
2750
2751static int stbi__decode_jpeg_image(stbi__jpeg *j)
2752{
2753 int m;
2754 for (m = 0; m < 4; m++) {
2755 j->img_comp[m].raw_data = NULL;
2756 j->img_comp[m].raw_coeff = NULL;
2757 }
2758 j->restart_interval = 0;
2759 if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0;
2760 m = stbi__get_marker(j);
2761 while (!stbi__EOI(m)) {
2762 if (stbi__SOS(m)) {
2763 if (!stbi__process_scan_header(j)) return 0;
2764 if (!stbi__parse_entropy_coded_data(j)) return 0;
2765 if (j->marker == STBI__MARKER_none ) {
2766 j->marker = stbi__skip_jpeg_junk_at_end(j);
2767
2768 }
2769 m = stbi__get_marker(j);
2770 if (STBI__RESTART(m))
2771 m = stbi__get_marker(j);
2772 } else if (stbi__DNL(m)) {
2773 int Ld = stbi__get16be(j->s);
2774 stbi__uint32 NL = stbi__get16be(j->s);
2775 if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG");
2776 if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG");
2777 m = stbi__get_marker(j);
2778 } else {
2779 if (!stbi__process_marker(j, m)) return 1;
2780 m = stbi__get_marker(j);
2781 }
2782 }
2783 if (j->progressive)
2784 stbi__jpeg_finish(j);
2785 return 1;
2786}
2787
2788typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1,
2789 int w, int hs);
2790
2791#define stbi__div4(x) ((stbi_uc) ((x) >> 2))
2792
2793static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
2794{
2795 STBI_NOTUSED(out);
2796 STBI_NOTUSED(in_far);
2797 STBI_NOTUSED(w);
2798 STBI_NOTUSED(hs);
2799 return in_near;
2800}
2801
2802static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
2803{
2804
2805 int i;
2806 STBI_NOTUSED(hs);
2807 for (i=0; i < w; ++i)
2808 out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2);
2809 return out;
2810}
2811
2812static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
2813{
2814
2815 int i;
2816 stbi_uc *input = in_near;
2817
2818 if (w == 1) {
2819
2820 out[0] = out[1] = input[0];
2821 return out;
2822 }
2823
2824 out[0] = input[0];
2825 out[1] = stbi__div4(input[0]*3 + input[1] + 2);
2826 for (i=1; i < w-1; ++i) {
2827 int n = 3*input[i]+2;
2828 out[i*2+0] = stbi__div4(n+input[i-1]);
2829 out[i*2+1] = stbi__div4(n+input[i+1]);
2830 }
2831 out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2);
2832 out[i*2+1] = input[w-1];
2833
2834 STBI_NOTUSED(in_far);
2835 STBI_NOTUSED(hs);
2836
2837 return out;
2838}
2839
2840#define stbi__div16(x) ((stbi_uc) ((x) >> 4))
2841
2842static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
2843{
2844
2845 int i,t0,t1;
2846 if (w == 1) {
2847 out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
2848 return out;
2849 }
2850
2851 t1 = 3*in_near[0] + in_far[0];
2852 out[0] = stbi__div4(t1+2);
2853 for (i=1; i < w; ++i) {
2854 t0 = t1;
2855 t1 = 3*in_near[i]+in_far[i];
2856 out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
2857 out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
2858 }
2859 out[w*2-1] = stbi__div4(t1+2);
2860
2861 STBI_NOTUSED(hs);
2862
2863 return out;
2864}
2865
2866#if defined(STBI_SSE2) || defined(STBI_NEON)
2867static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
2868{
2869
2870 int i=0,t0,t1;
2871
2872 if (w == 1) {
2873 out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
2874 return out;
2875 }
2876
2877 t1 = 3*in_near[0] + in_far[0];
2878
2879 for (; i < ((w-1) & ~7); i += 8) {
2880#if defined(STBI_SSE2)
2881
2882 __m128i zero = _mm_setzero_si128();
2883 __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i));
2884 __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i));
2885 __m128i farw = _mm_unpacklo_epi8(farb, zero);
2886 __m128i nearw = _mm_unpacklo_epi8(nearb, zero);
2887 __m128i diff = _mm_sub_epi16(farw, nearw);
2888 __m128i nears = _mm_slli_epi16(nearw, 2);
2889 __m128i curr = _mm_add_epi16(nears, diff);
2890
2891 __m128i prv0 = _mm_slli_si128(curr, 2);
2892 __m128i nxt0 = _mm_srli_si128(curr, 2);
2893 __m128i prev = _mm_insert_epi16(prv0, t1, 0);
2894 __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7);
2895
2896 __m128i bias = _mm_set1_epi16(8);
2897 __m128i curs = _mm_slli_epi16(curr, 2);
2898 __m128i prvd = _mm_sub_epi16(prev, curr);
2899 __m128i nxtd = _mm_sub_epi16(next, curr);
2900 __m128i curb = _mm_add_epi16(curs, bias);
2901 __m128i even = _mm_add_epi16(prvd, curb);
2902 __m128i odd = _mm_add_epi16(nxtd, curb);
2903
2904 __m128i int0 = _mm_unpacklo_epi16(even, odd);
2905 __m128i int1 = _mm_unpackhi_epi16(even, odd);
2906 __m128i de0 = _mm_srli_epi16(int0, 4);
2907 __m128i de1 = _mm_srli_epi16(int1, 4);
2908
2909 __m128i outv = _mm_packus_epi16(de0, de1);
2910 _mm_storeu_si128((__m128i *) (out + i*2), outv);
2911#elif defined(STBI_NEON)
2912
2913 uint8x8_t farb = vld1_u8(in_far + i);
2914 uint8x8_t nearb = vld1_u8(in_near + i);
2915 int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb));
2916 int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2));
2917 int16x8_t curr = vaddq_s16(nears, diff);
2918
2919 int16x8_t prv0 = vextq_s16(curr, curr, 7);
2920 int16x8_t nxt0 = vextq_s16(curr, curr, 1);
2921 int16x8_t prev = vsetq_lane_s16(t1, prv0, 0);
2922 int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7);
2923
2924 int16x8_t curs = vshlq_n_s16(curr, 2);
2925 int16x8_t prvd = vsubq_s16(prev, curr);
2926 int16x8_t nxtd = vsubq_s16(next, curr);
2927 int16x8_t even = vaddq_s16(curs, prvd);
2928 int16x8_t odd = vaddq_s16(curs, nxtd);
2929
2930 uint8x8x2_t o;
2931 o.val[0] = vqrshrun_n_s16(even, 4);
2932 o.val[1] = vqrshrun_n_s16(odd, 4);
2933 vst2_u8(out + i*2, o);
2934#endif
2935
2936 t1 = 3*in_near[i+7] + in_far[i+7];
2937 }
2938
2939 t0 = t1;
2940 t1 = 3*in_near[i] + in_far[i];
2941 out[i*2] = stbi__div16(3*t1 + t0 + 8);
2942
2943 for (++i; i < w; ++i) {
2944 t0 = t1;
2945 t1 = 3*in_near[i]+in_far[i];
2946 out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
2947 out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
2948 }
2949 out[w*2-1] = stbi__div4(t1+2);
2950
2951 STBI_NOTUSED(hs);
2952
2953 return out;
2954}
2955#endif
2956
2957static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
2958{
2959
2960 int i,j;
2961 STBI_NOTUSED(in_far);
2962 for (i=0; i < w; ++i)
2963 for (j=0; j < hs; ++j)
2964 out[i*hs+j] = in_near[i];
2965 return out;
2966}
2967
2968#define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8)
2969static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step)
2970{
2971 int i;
2972 for (i=0; i < count; ++i) {
2973 int y_fixed = (y[i] << 20) + (1<<19);
2974 int r,g,b;
2975 int cr = pcr[i] - 128;
2976 int cb = pcb[i] - 128;
2977 r = y_fixed + cr* stbi__float2fixed(1.40200f);
2978 g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000);
2979 b = y_fixed + cb* stbi__float2fixed(1.77200f);
2980 r >>= 20;
2981 g >>= 20;
2982 b >>= 20;
2983 if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
2984 if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
2985 if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
2986 out[0] = (stbi_uc)r;
2987 out[1] = (stbi_uc)g;
2988 out[2] = (stbi_uc)b;
2989 out[3] = 255;
2990 out += step;
2991 }
2992}
2993
2994#if defined(STBI_SSE2) || defined(STBI_NEON)
2995static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step)
2996{
2997 int i = 0;
2998
2999#ifdef STBI_SSE2
3000
3001 if (step == 4) {
3002
3003 __m128i signflip = _mm_set1_epi8(-0x80);
3004 __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f));
3005 __m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f));
3006 __m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f));
3007 __m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f));
3008 __m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128);
3009 __m128i xw = _mm_set1_epi16(255);
3010
3011 for (; i+7 < count; i += 8) {
3012
3013 __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i));
3014 __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i));
3015 __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i));
3016 __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip);
3017 __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip);
3018
3019 __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes);
3020 __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased);
3021 __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased);
3022
3023 __m128i yws = _mm_srli_epi16(yw, 4);
3024 __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw);
3025 __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw);
3026 __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1);
3027 __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1);
3028 __m128i rws = _mm_add_epi16(cr0, yws);
3029 __m128i gwt = _mm_add_epi16(cb0, yws);
3030 __m128i bws = _mm_add_epi16(yws, cb1);
3031 __m128i gws = _mm_add_epi16(gwt, cr1);
3032
3033 __m128i rw = _mm_srai_epi16(rws, 4);
3034 __m128i bw = _mm_srai_epi16(bws, 4);
3035 __m128i gw = _mm_srai_epi16(gws, 4);
3036
3037 __m128i brb = _mm_packus_epi16(rw, bw);
3038 __m128i gxb = _mm_packus_epi16(gw, xw);
3039
3040 __m128i t0 = _mm_unpacklo_epi8(brb, gxb);
3041 __m128i t1 = _mm_unpackhi_epi8(brb, gxb);
3042 __m128i o0 = _mm_unpacklo_epi16(t0, t1);
3043 __m128i o1 = _mm_unpackhi_epi16(t0, t1);
3044
3045 _mm_storeu_si128((__m128i *) (out + 0), o0);
3046 _mm_storeu_si128((__m128i *) (out + 16), o1);
3047 out += 32;
3048 }
3049 }
3050#endif
3051
3052#ifdef STBI_NEON
3053
3054 if (step == 4) {
3055
3056 uint8x8_t signflip = vdup_n_u8(0x80);
3057 int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f));
3058 int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f));
3059 int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f));
3060 int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f));
3061
3062 for (; i+7 < count; i += 8) {
3063
3064 uint8x8_t y_bytes = vld1_u8(y + i);
3065 uint8x8_t cr_bytes = vld1_u8(pcr + i);
3066 uint8x8_t cb_bytes = vld1_u8(pcb + i);
3067 int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip));
3068 int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip));
3069
3070 int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4));
3071 int16x8_t crw = vshll_n_s8(cr_biased, 7);
3072 int16x8_t cbw = vshll_n_s8(cb_biased, 7);
3073
3074 int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0);
3075 int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0);
3076 int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1);
3077 int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1);
3078 int16x8_t rws = vaddq_s16(yws, cr0);
3079 int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1);
3080 int16x8_t bws = vaddq_s16(yws, cb1);
3081
3082 uint8x8x4_t o;
3083 o.val[0] = vqrshrun_n_s16(rws, 4);
3084 o.val[1] = vqrshrun_n_s16(gws, 4);
3085 o.val[2] = vqrshrun_n_s16(bws, 4);
3086 o.val[3] = vdup_n_u8(255);
3087
3088 vst4_u8(out, o);
3089 out += 8*4;
3090 }
3091 }
3092#endif
3093
3094 for (; i < count; ++i) {
3095 int y_fixed = (y[i] << 20) + (1<<19);
3096 int r,g,b;
3097 int cr = pcr[i] - 128;
3098 int cb = pcb[i] - 128;
3099 r = y_fixed + cr* stbi__float2fixed(1.40200f);
3100 g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000);
3101 b = y_fixed + cb* stbi__float2fixed(1.77200f);
3102 r >>= 20;
3103 g >>= 20;
3104 b >>= 20;
3105 if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
3106 if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
3107 if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
3108 out[0] = (stbi_uc)r;
3109 out[1] = (stbi_uc)g;
3110 out[2] = (stbi_uc)b;
3111 out[3] = 255;
3112 out += step;
3113 }
3114}
3115#endif
3116
3117static void stbi__setup_jpeg(stbi__jpeg *j)
3118{
3119 j->idct_block_kernel = stbi__idct_block;
3120 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row;
3121 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2;
3122
3123#ifdef STBI_SSE2
3124 if (stbi__sse2_available()) {
3125 j->idct_block_kernel = stbi__idct_simd;
3126 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
3127 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
3128 }
3129#endif
3130
3131#ifdef STBI_NEON
3132 j->idct_block_kernel = stbi__idct_simd;
3133 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
3134 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
3135#endif
3136}
3137
3138static void stbi__cleanup_jpeg(stbi__jpeg *j)
3139{
3140 stbi__free_jpeg_components(j, j->s->img_n, 0);
3141}
3142
3143typedef struct
3144{
3145 resample_row_func resample;
3146 stbi_uc *line0,*line1;
3147 int hs,vs;
3148 int w_lores;
3149 int ystep;
3150 int ypos;
3151} stbi__resample;
3152
3153static stbi_uc stbi__blinn_8x8(stbi_uc x, stbi_uc y)
3154{
3155 unsigned int t = x*y + 128;
3156 return (stbi_uc) ((t + (t >>8)) >> 8);
3157}
3158
3159static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
3160{
3161 int n, decode_n, is_rgb;
3162 z->s->img_n = 0;
3163
3164 if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
3165
3166 if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; }
3167
3168 n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1;
3169
3170 is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif));
3171
3172 if (z->s->img_n == 3 && n < 3 && !is_rgb)
3173 decode_n = 1;
3174 else
3175 decode_n = z->s->img_n;
3176
3177 if (decode_n <= 0) { stbi__cleanup_jpeg(z); return NULL; }
3178
3179 {
3180 int k;
3181 unsigned int i,j;
3182 stbi_uc *output;
3183 stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL };
3184
3185 stbi__resample res_comp[4];
3186
3187 for (k=0; k < decode_n; ++k) {
3188 stbi__resample *r = &res_comp[k];
3189
3190 z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3);
3191 if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
3192
3193 r->hs = z->img_h_max / z->img_comp[k].h;
3194 r->vs = z->img_v_max / z->img_comp[k].v;
3195 r->ystep = r->vs >> 1;
3196 r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
3197 r->ypos = 0;
3198 r->line0 = r->line1 = z->img_comp[k].data;
3199
3200 if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
3201 else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2;
3202 else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2;
3203 else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel;
3204 else r->resample = stbi__resample_row_generic;
3205 }
3206
3207 output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1);
3208 if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
3209
3210 for (j=0; j < z->s->img_y; ++j) {
3211 stbi_uc *out = output + n * z->s->img_x * j;
3212 for (k=0; k < decode_n; ++k) {
3213 stbi__resample *r = &res_comp[k];
3214 int y_bot = r->ystep >= (r->vs >> 1);
3215 coutput[k] = r->resample(z->img_comp[k].linebuf,
3216 y_bot ? r->line1 : r->line0,
3217 y_bot ? r->line0 : r->line1,
3218 r->w_lores, r->hs);
3219 if (++r->ystep >= r->vs) {
3220 r->ystep = 0;
3221 r->line0 = r->line1;
3222 if (++r->ypos < z->img_comp[k].y)
3223 r->line1 += z->img_comp[k].w2;
3224 }
3225 }
3226 if (n >= 3) {
3227 stbi_uc *y = coutput[0];
3228 if (z->s->img_n == 3) {
3229 if (is_rgb) {
3230 for (i=0; i < z->s->img_x; ++i) {
3231 out[0] = y[i];
3232 out[1] = coutput[1][i];
3233 out[2] = coutput[2][i];
3234 out[3] = 255;
3235 out += n;
3236 }
3237 } else {
3238 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
3239 }
3240 } else if (z->s->img_n == 4) {
3241 if (z->app14_color_transform == 0) {
3242 for (i=0; i < z->s->img_x; ++i) {
3243 stbi_uc m = coutput[3][i];
3244 out[0] = stbi__blinn_8x8(coutput[0][i], m);
3245 out[1] = stbi__blinn_8x8(coutput[1][i], m);
3246 out[2] = stbi__blinn_8x8(coutput[2][i], m);
3247 out[3] = 255;
3248 out += n;
3249 }
3250 } else if (z->app14_color_transform == 2) {
3251 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
3252 for (i=0; i < z->s->img_x; ++i) {
3253 stbi_uc m = coutput[3][i];
3254 out[0] = stbi__blinn_8x8(255 - out[0], m);
3255 out[1] = stbi__blinn_8x8(255 - out[1], m);
3256 out[2] = stbi__blinn_8x8(255 - out[2], m);
3257 out += n;
3258 }
3259 } else {
3260 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
3261 }
3262 } else
3263 for (i=0; i < z->s->img_x; ++i) {
3264 out[0] = out[1] = out[2] = y[i];
3265 out[3] = 255;
3266 out += n;
3267 }
3268 } else {
3269 if (is_rgb) {
3270 if (n == 1)
3271 for (i=0; i < z->s->img_x; ++i)
3272 *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);
3273 else {
3274 for (i=0; i < z->s->img_x; ++i, out += 2) {
3275 out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);
3276 out[1] = 255;
3277 }
3278 }
3279 } else if (z->s->img_n == 4 && z->app14_color_transform == 0) {
3280 for (i=0; i < z->s->img_x; ++i) {
3281 stbi_uc m = coutput[3][i];
3282 stbi_uc r = stbi__blinn_8x8(coutput[0][i], m);
3283 stbi_uc g = stbi__blinn_8x8(coutput[1][i], m);
3284 stbi_uc b = stbi__blinn_8x8(coutput[2][i], m);
3285 out[0] = stbi__compute_y(r, g, b);
3286 out[1] = 255;
3287 out += n;
3288 }
3289 } else if (z->s->img_n == 4 && z->app14_color_transform == 2) {
3290 for (i=0; i < z->s->img_x; ++i) {
3291 out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]);
3292 out[1] = 255;
3293 out += n;
3294 }
3295 } else {
3296 stbi_uc *y = coutput[0];
3297 if (n == 1)
3298 for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
3299 else
3300 for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; }
3301 }
3302 }
3303 }
3304 stbi__cleanup_jpeg(z);
3305 *out_x = z->s->img_x;
3306 *out_y = z->s->img_y;
3307 if (comp) *comp = z->s->img_n >= 3 ? 3 : 1;
3308 return output;
3309 }
3310}
3311
3312static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
3313{
3314 unsigned char* result;
3315 stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg));
3316 if (!j) return stbi__errpuc("outofmem", "Out of memory");
3317 memset(j, 0, sizeof(stbi__jpeg));
3318 STBI_NOTUSED(ri);
3319 j->s = s;
3320 stbi__setup_jpeg(j);
3321 result = load_jpeg_image(j, x,y,comp,req_comp);
3322 STBI_FREE(j);
3323 return result;
3324}
3325
3326static int stbi__jpeg_test(stbi__context *s)
3327{
3328 int r;
3329 stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg));
3330 if (!j) return stbi__err("outofmem", "Out of memory");
3331 memset(j, 0, sizeof(stbi__jpeg));
3332 j->s = s;
3333 stbi__setup_jpeg(j);
3334 r = stbi__decode_jpeg_header(j, STBI__SCAN_type);
3335 stbi__rewind(s);
3336 STBI_FREE(j);
3337 return r;
3338}
3339
3340static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp)
3341{
3342 if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) {
3343 stbi__rewind( j->s );
3344 return 0;
3345 }
3346 if (x) *x = j->s->img_x;
3347 if (y) *y = j->s->img_y;
3348 if (comp) *comp = j->s->img_n >= 3 ? 3 : 1;
3349 return 1;
3350}
3351
3352static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp)
3353{
3354 int result;
3355 stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg)));
3356 if (!j) return stbi__err("outofmem", "Out of memory");
3357 memset(j, 0, sizeof(stbi__jpeg));
3358 j->s = s;
3359 result = stbi__jpeg_info_raw(j, x, y, comp);
3360 STBI_FREE(j);
3361 return result;
3362}
3363#endif
3364
3365#ifndef STBI_NO_ZLIB
3366
3367#define STBI__ZFAST_BITS 9
3368#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
3369#define STBI__ZNSYMS 288
3370
3371typedef struct
3372{
3373 stbi__uint16 fast[1 << STBI__ZFAST_BITS];
3374 stbi__uint16 firstcode[16];
3375 int maxcode[17];
3376 stbi__uint16 firstsymbol[16];
3377 stbi_uc size[STBI__ZNSYMS];
3378 stbi__uint16 value[STBI__ZNSYMS];
3379} stbi__zhuffman;
3380
3381stbi_inline static int stbi__bitreverse16(int n)
3382{
3383 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
3384 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
3385 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
3386 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
3387 return n;
3388}
3389
3390stbi_inline static int stbi__bit_reverse(int v, int bits)
3391{
3392 STBI_ASSERT(bits <= 16);
3393
3394 return stbi__bitreverse16(v) >> (16-bits);
3395}
3396
3397static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num)
3398{
3399 int i,k=0;
3400 int code, next_code[16], sizes[17];
3401
3402 memset(sizes, 0, sizeof(sizes));
3403 memset(z->fast, 0, sizeof(z->fast));
3404 for (i=0; i < num; ++i)
3405 ++sizes[sizelist[i]];
3406 sizes[0] = 0;
3407 for (i=1; i < 16; ++i)
3408 if (sizes[i] > (1 << i))
3409 return stbi__err("bad sizes", "Corrupt PNG");
3410 code = 0;
3411 for (i=1; i < 16; ++i) {
3412 next_code[i] = code;
3413 z->firstcode[i] = (stbi__uint16) code;
3414 z->firstsymbol[i] = (stbi__uint16) k;
3415 code = (code + sizes[i]);
3416 if (sizes[i])
3417 if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG");
3418 z->maxcode[i] = code << (16-i);
3419 code <<= 1;
3420 k += sizes[i];
3421 }
3422 z->maxcode[16] = 0x10000;
3423 for (i=0; i < num; ++i) {
3424 int s = sizelist[i];
3425 if (s) {
3426 int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
3427 stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i);
3428 z->size [c] = (stbi_uc ) s;
3429 z->value[c] = (stbi__uint16) i;
3430 if (s <= STBI__ZFAST_BITS) {
3431 int j = stbi__bit_reverse(next_code[s],s);
3432 while (j < (1 << STBI__ZFAST_BITS)) {
3433 z->fast[j] = fastv;
3434 j += (1 << s);
3435 }
3436 }
3437 ++next_code[s];
3438 }
3439 }
3440 return 1;
3441}
3442
3443typedef struct
3444{
3445 stbi_uc *zbuffer, *zbuffer_end;
3446 int num_bits;
3447 int hit_zeof_once;
3448 stbi__uint32 code_buffer;
3449
3450 char *zout;
3451 char *zout_start;
3452 char *zout_end;
3453 int z_expandable;
3454
3455 stbi__zhuffman z_length, z_distance;
3456} stbi__zbuf;
3457
3458stbi_inline static int stbi__zeof(stbi__zbuf *z)
3459{
3460 return (z->zbuffer >= z->zbuffer_end);
3461}
3462
3463stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z)
3464{
3465 return stbi__zeof(z) ? 0 : *z->zbuffer++;
3466}
3467
3468static void stbi__fill_bits(stbi__zbuf *z)
3469{
3470 do {
3471 if (z->code_buffer >= (1U << z->num_bits)) {
3472 z->zbuffer = z->zbuffer_end;
3473 return;
3474 }
3475 z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits;
3476 z->num_bits += 8;
3477 } while (z->num_bits <= 24);
3478}
3479
3480stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n)
3481{
3482 unsigned int k;
3483 if (z->num_bits < n) stbi__fill_bits(z);
3484 k = z->code_buffer & ((1 << n) - 1);
3485 z->code_buffer >>= n;
3486 z->num_bits -= n;
3487 return k;
3488}
3489
3490static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
3491{
3492 int b,s,k;
3493
3494 k = stbi__bit_reverse(a->code_buffer, 16);
3495 for (s=STBI__ZFAST_BITS+1; ; ++s)
3496 if (k < z->maxcode[s])
3497 break;
3498 if (s >= 16) return -1;
3499
3500 b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
3501 if (b >= STBI__ZNSYMS) return -1;
3502 if (z->size[b] != s) return -1;
3503 a->code_buffer >>= s;
3504 a->num_bits -= s;
3505 return z->value[b];
3506}
3507
3508stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
3509{
3510 int b,s;
3511 if (a->num_bits < 16) {
3512 if (stbi__zeof(a)) {
3513 if (!a->hit_zeof_once) {
3514
3515 a->hit_zeof_once = 1;
3516 a->num_bits += 16;
3517 } else {
3518
3519 return -1;
3520 }
3521 } else {
3522 stbi__fill_bits(a);
3523 }
3524 }
3525 b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
3526 if (b) {
3527 s = b >> 9;
3528 a->code_buffer >>= s;
3529 a->num_bits -= s;
3530 return b & 511;
3531 }
3532 return stbi__zhuffman_decode_slowpath(a, z);
3533}
3534
3535static int stbi__zexpand(stbi__zbuf *z, char *zout, int n)
3536{
3537 char *q;
3538 unsigned int cur, limit, old_limit;
3539 z->zout = zout;
3540 if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
3541 cur = (unsigned int) (z->zout - z->zout_start);
3542 limit = old_limit = (unsigned) (z->zout_end - z->zout_start);
3543 if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem", "Out of memory");
3544 while (cur + n > limit) {
3545 if(limit > UINT_MAX / 2) return stbi__err("outofmem", "Out of memory");
3546 limit *= 2;
3547 }
3548 q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit);
3549 STBI_NOTUSED(old_limit);
3550 if (q == NULL) return stbi__err("outofmem", "Out of memory");
3551 z->zout_start = q;
3552 z->zout = q + cur;
3553 z->zout_end = q + limit;
3554 return 1;
3555}
3556
3557static const int stbi__zlength_base[31] = {
3558 3,4,5,6,7,8,9,10,11,13,
3559 15,17,19,23,27,31,35,43,51,59,
3560 67,83,99,115,131,163,195,227,258,0,0 };
3561
3562static const int stbi__zlength_extra[31]=
3563{ 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 };
3564
3565static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
3566257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
3567
3568static const int stbi__zdist_extra[32] =
3569{ 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};
3570
3571static int stbi__parse_huffman_block(stbi__zbuf *a)
3572{
3573 char *zout = a->zout;
3574 for(;;) {
3575 int z = stbi__zhuffman_decode(a, &a->z_length);
3576 if (z < 256) {
3577 if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");
3578 if (zout >= a->zout_end) {
3579 if (!stbi__zexpand(a, zout, 1)) return 0;
3580 zout = a->zout;
3581 }
3582 *zout++ = (char) z;
3583 } else {
3584 stbi_uc *p;
3585 int len,dist;
3586 if (z == 256) {
3587 a->zout = zout;
3588 if (a->hit_zeof_once && a->num_bits < 16) {
3589
3590 return stbi__err("unexpected end","Corrupt PNG");
3591 }
3592 return 1;
3593 }
3594 if (z >= 286) return stbi__err("bad huffman code","Corrupt PNG");
3595 z -= 257;
3596 len = stbi__zlength_base[z];
3597 if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
3598 z = stbi__zhuffman_decode(a, &a->z_distance);
3599 if (z < 0 || z >= 30) return stbi__err("bad huffman code","Corrupt PNG");
3600 dist = stbi__zdist_base[z];
3601 if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
3602 if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");
3603 if (len > a->zout_end - zout) {
3604 if (!stbi__zexpand(a, zout, len)) return 0;
3605 zout = a->zout;
3606 }
3607 p = (stbi_uc *) (zout - dist);
3608 if (dist == 1) {
3609 stbi_uc v = *p;
3610 if (len) { do *zout++ = v; while (--len); }
3611 } else {
3612 if (len) { do *zout++ = *p++; while (--len); }
3613 }
3614 }
3615 }
3616}
3617
3618static int stbi__compute_huffman_codes(stbi__zbuf *a)
3619{
3620 static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
3621 stbi__zhuffman z_codelength;
3622 stbi_uc lencodes[286+32+137];
3623 stbi_uc codelength_sizes[19];
3624 int i,n;
3625
3626 int hlit = stbi__zreceive(a,5) + 257;
3627 int hdist = stbi__zreceive(a,5) + 1;
3628 int hclen = stbi__zreceive(a,4) + 4;
3629 int ntot = hlit + hdist;
3630
3631 memset(codelength_sizes, 0, sizeof(codelength_sizes));
3632 for (i=0; i < hclen; ++i) {
3633 int s = stbi__zreceive(a,3);
3634 codelength_sizes[length_dezigzag[i]] = (stbi_uc) s;
3635 }
3636 if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
3637
3638 n = 0;
3639 while (n < ntot) {
3640 int c = stbi__zhuffman_decode(a, &z_codelength);
3641 if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG");
3642 if (c < 16)
3643 lencodes[n++] = (stbi_uc) c;
3644 else {
3645 stbi_uc fill = 0;
3646 if (c == 16) {
3647 c = stbi__zreceive(a,2)+3;
3648 if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG");
3649 fill = lencodes[n-1];
3650 } else if (c == 17) {
3651 c = stbi__zreceive(a,3)+3;
3652 } else if (c == 18) {
3653 c = stbi__zreceive(a,7)+11;
3654 } else {
3655 return stbi__err("bad codelengths", "Corrupt PNG");
3656 }
3657 if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG");
3658 memset(lencodes+n, fill, c);
3659 n += c;
3660 }
3661 }
3662 if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG");
3663 if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
3664 if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
3665 return 1;
3666}
3667
3668static int stbi__parse_uncompressed_block(stbi__zbuf *a)
3669{
3670 stbi_uc header[4];
3671 int len,nlen,k;
3672 if (a->num_bits & 7)
3673 stbi__zreceive(a, a->num_bits & 7);
3674
3675 k = 0;
3676 while (a->num_bits > 0) {
3677 header[k++] = (stbi_uc) (a->code_buffer & 255);
3678 a->code_buffer >>= 8;
3679 a->num_bits -= 8;
3680 }
3681 if (a->num_bits < 0) return stbi__err("zlib corrupt","Corrupt PNG");
3682
3683 while (k < 4)
3684 header[k++] = stbi__zget8(a);
3685 len = header[1] * 256 + header[0];
3686 nlen = header[3] * 256 + header[2];
3687 if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG");
3688 if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG");
3689 if (a->zout + len > a->zout_end)
3690 if (!stbi__zexpand(a, a->zout, len)) return 0;
3691 memcpy(a->zout, a->zbuffer, len);
3692 a->zbuffer += len;
3693 a->zout += len;
3694 return 1;
3695}
3696
3697static int stbi__parse_zlib_header(stbi__zbuf *a)
3698{
3699 int cmf = stbi__zget8(a);
3700 int cm = cmf & 15;
3701
3702 int flg = stbi__zget8(a);
3703 if (stbi__zeof(a)) return stbi__err("bad zlib header","Corrupt PNG");
3704 if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG");
3705 if (flg & 32) return stbi__err("no preset dict","Corrupt PNG");
3706 if (cm != 8) return stbi__err("bad compression","Corrupt PNG");
3707
3708 return 1;
3709}
3710
3711static const stbi_uc stbi__zdefault_length[STBI__ZNSYMS] =
3712{
3713 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
3714 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
3715 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
3716 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
3717 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
3718 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
3719 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
3720 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
3721 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8
3722};
3723static const stbi_uc stbi__zdefault_distance[32] =
3724{
3725 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
3726};
3727
3728static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
3729{
3730 int final, type;
3731 if (parse_header)
3732 if (!stbi__parse_zlib_header(a)) return 0;
3733 a->num_bits = 0;
3734 a->code_buffer = 0;
3735 a->hit_zeof_once = 0;
3736 do {
3737 final = stbi__zreceive(a,1);
3738 type = stbi__zreceive(a,2);
3739 if (type == 0) {
3740 if (!stbi__parse_uncompressed_block(a)) return 0;
3741 } else if (type == 3) {
3742 return 0;
3743 } else {
3744 if (type == 1) {
3745
3746 if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , STBI__ZNSYMS)) return 0;
3747 if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
3748 } else {
3749 if (!stbi__compute_huffman_codes(a)) return 0;
3750 }
3751 if (!stbi__parse_huffman_block(a)) return 0;
3752 }
3753 } while (!final);
3754 return 1;
3755}
3756
3757static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header)
3758{
3759 a->zout_start = obuf;
3760 a->zout = obuf;
3761 a->zout_end = obuf + olen;
3762 a->z_expandable = exp;
3763
3764 return stbi__parse_zlib(a, parse_header);
3765}
3766
3767STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
3768{
3769 stbi__zbuf a;
3770 char *p = (char *) stbi__malloc(initial_size);
3771 if (p == NULL) return NULL;
3772 a.zbuffer = (stbi_uc *) buffer;
3773 a.zbuffer_end = (stbi_uc *) buffer + len;
3774 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
3775 if (outlen) *outlen = (int) (a.zout - a.zout_start);
3776 return a.zout_start;
3777 } else {
3778 STBI_FREE(a.zout_start);
3779 return NULL;
3780 }
3781}
3782
3783STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
3784{
3785 return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
3786}
3787
3788STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
3789{
3790 stbi__zbuf a;
3791 char *p = (char *) stbi__malloc(initial_size);
3792 if (p == NULL) return NULL;
3793 a.zbuffer = (stbi_uc *) buffer;
3794 a.zbuffer_end = (stbi_uc *) buffer + len;
3795 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
3796 if (outlen) *outlen = (int) (a.zout - a.zout_start);
3797 return a.zout_start;
3798 } else {
3799 STBI_FREE(a.zout_start);
3800 return NULL;
3801 }
3802}
3803
3804STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
3805{
3806 stbi__zbuf a;
3807 a.zbuffer = (stbi_uc *) ibuffer;
3808 a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
3809 if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
3810 return (int) (a.zout - a.zout_start);
3811 else
3812 return -1;
3813}
3814
3815STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
3816{
3817 stbi__zbuf a;
3818 char *p = (char *) stbi__malloc(16384);
3819 if (p == NULL) return NULL;
3820 a.zbuffer = (stbi_uc *) buffer;
3821 a.zbuffer_end = (stbi_uc *) buffer+len;
3822 if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
3823 if (outlen) *outlen = (int) (a.zout - a.zout_start);
3824 return a.zout_start;
3825 } else {
3826 STBI_FREE(a.zout_start);
3827 return NULL;
3828 }
3829}
3830
3831STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
3832{
3833 stbi__zbuf a;
3834 a.zbuffer = (stbi_uc *) ibuffer;
3835 a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
3836 if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
3837 return (int) (a.zout - a.zout_start);
3838 else
3839 return -1;
3840}
3841#endif
3842
3843#ifndef STBI_NO_PNG
3844typedef struct
3845{
3846 stbi__uint32 length;
3847 stbi__uint32 type;
3848} stbi__pngchunk;
3849
3850static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
3851{
3852 stbi__pngchunk c;
3853 c.length = stbi__get32be(s);
3854 c.type = stbi__get32be(s);
3855 return c;
3856}
3857
3858static int stbi__check_png_header(stbi__context *s)
3859{
3860 static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };
3861 int i;
3862 for (i=0; i < 8; ++i)
3863 if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG");
3864 return 1;
3865}
3866
3867typedef struct
3868{
3869 stbi__context *s;
3870 stbi_uc *idata, *expanded, *out;
3871 int depth;
3872} stbi__png;
3873
3874enum {
3875 STBI__F_none=0,
3876 STBI__F_sub=1,
3877 STBI__F_up=2,
3878 STBI__F_avg=3,
3879 STBI__F_paeth=4,
3880
3881 STBI__F_avg_first
3882};
3883
3884static stbi_uc first_row_filter[5] =
3885{
3886 STBI__F_none,
3887 STBI__F_sub,
3888 STBI__F_none,
3889 STBI__F_avg_first,
3890 STBI__F_sub
3891};
3892
3893static int stbi__paeth(int a, int b, int c)
3894{
3895
3896 int thresh = c*3 - (a + b);
3897 int lo = a < b ? a : b;
3898 int hi = a < b ? b : a;
3899 int t0 = (hi <= thresh) ? lo : c;
3900 int t1 = (thresh <= lo) ? hi : t0;
3901 return t1;
3902}
3903
3904static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };
3905
3906static void stbi__create_png_alpha_expand8(stbi_uc *dest, stbi_uc *src, stbi__uint32 x, int img_n)
3907{
3908 int i;
3909
3910 if (img_n == 1) {
3911 for (i=x-1; i >= 0; --i) {
3912 dest[i*2+1] = 255;
3913 dest[i*2+0] = src[i];
3914 }
3915 } else {
3916 STBI_ASSERT(img_n == 3);
3917 for (i=x-1; i >= 0; --i) {
3918 dest[i*4+3] = 255;
3919 dest[i*4+2] = src[i*3+2];
3920 dest[i*4+1] = src[i*3+1];
3921 dest[i*4+0] = src[i*3+0];
3922 }
3923 }
3924}
3925
3926static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color)
3927{
3928 int bytes = (depth == 16 ? 2 : 1);
3929 stbi__context *s = a->s;
3930 stbi__uint32 i,j,stride = x*out_n*bytes;
3931 stbi__uint32 img_len, img_width_bytes;
3932 stbi_uc *filter_buf;
3933 int all_ok = 1;
3934 int k;
3935 int img_n = s->img_n;
3936
3937 int output_bytes = out_n*bytes;
3938 int filter_bytes = img_n*bytes;
3939 int width = x;
3940
3941 STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1);
3942 a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0);
3943 if (!a->out) return stbi__err("outofmem", "Out of memory");
3944
3945 if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG");
3946 img_width_bytes = (((img_n * x * depth) + 7) >> 3);
3947 if (!stbi__mad2sizes_valid(img_width_bytes, y, img_width_bytes)) return stbi__err("too large", "Corrupt PNG");
3948 img_len = (img_width_bytes + 1) * y;
3949
3950 if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");
3951
3952 filter_buf = (stbi_uc *) stbi__malloc_mad2(img_width_bytes, 2, 0);
3953 if (!filter_buf) return stbi__err("outofmem", "Out of memory");
3954
3955 if (depth < 8) {
3956 filter_bytes = 1;
3957 width = img_width_bytes;
3958 }
3959
3960 for (j=0; j < y; ++j) {
3961
3962 stbi_uc *cur = filter_buf + (j & 1)*img_width_bytes;
3963 stbi_uc *prior = filter_buf + (~j & 1)*img_width_bytes;
3964 stbi_uc *dest = a->out + stride*j;
3965 int nk = width * filter_bytes;
3966 int filter = *raw++;
3967
3968 if (filter > 4) {
3969 all_ok = stbi__err("invalid filter","Corrupt PNG");
3970 break;
3971 }
3972
3973 if (j == 0) filter = first_row_filter[filter];
3974
3975 switch (filter) {
3976 case STBI__F_none:
3977 memcpy(cur, raw, nk);
3978 break;
3979 case STBI__F_sub:
3980 memcpy(cur, raw, filter_bytes);
3981 for (k = filter_bytes; k < nk; ++k)
3982 cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]);
3983 break;
3984 case STBI__F_up:
3985 for (k = 0; k < nk; ++k)
3986 cur[k] = STBI__BYTECAST(raw[k] + prior[k]);
3987 break;
3988 case STBI__F_avg:
3989 for (k = 0; k < filter_bytes; ++k)
3990 cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1));
3991 for (k = filter_bytes; k < nk; ++k)
3992 cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1));
3993 break;
3994 case STBI__F_paeth:
3995 for (k = 0; k < filter_bytes; ++k)
3996 cur[k] = STBI__BYTECAST(raw[k] + prior[k]);
3997 for (k = filter_bytes; k < nk; ++k)
3998 cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes], prior[k], prior[k-filter_bytes]));
3999 break;
4000 case STBI__F_avg_first:
4001 memcpy(cur, raw, filter_bytes);
4002 for (k = filter_bytes; k < nk; ++k)
4003 cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1));
4004 break;
4005 }
4006
4007 raw += nk;
4008
4009 if (depth < 8) {
4010 stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1;
4011 stbi_uc *in = cur;
4012 stbi_uc *out = dest;
4013 stbi_uc inb = 0;
4014 stbi__uint32 nsmp = x*img_n;
4015
4016 if (depth == 4) {
4017 for (i=0; i < nsmp; ++i) {
4018 if ((i & 1) == 0) inb = *in++;
4019 *out++ = scale * (inb >> 4);
4020 inb <<= 4;
4021 }
4022 } else if (depth == 2) {
4023 for (i=0; i < nsmp; ++i) {
4024 if ((i & 3) == 0) inb = *in++;
4025 *out++ = scale * (inb >> 6);
4026 inb <<= 2;
4027 }
4028 } else {
4029 STBI_ASSERT(depth == 1);
4030 for (i=0; i < nsmp; ++i) {
4031 if ((i & 7) == 0) inb = *in++;
4032 *out++ = scale * (inb >> 7);
4033 inb <<= 1;
4034 }
4035 }
4036
4037 if (img_n != out_n)
4038 stbi__create_png_alpha_expand8(dest, dest, x, img_n);
4039 } else if (depth == 8) {
4040 if (img_n == out_n)
4041 memcpy(dest, cur, x*img_n);
4042 else
4043 stbi__create_png_alpha_expand8(dest, cur, x, img_n);
4044 } else if (depth == 16) {
4045
4046 stbi__uint16 *dest16 = (stbi__uint16*)dest;
4047 stbi__uint32 nsmp = x*img_n;
4048
4049 if (img_n == out_n) {
4050 for (i = 0; i < nsmp; ++i, ++dest16, cur += 2)
4051 *dest16 = (cur[0] << 8) | cur[1];
4052 } else {
4053 STBI_ASSERT(img_n+1 == out_n);
4054 if (img_n == 1) {
4055 for (i = 0; i < x; ++i, dest16 += 2, cur += 2) {
4056 dest16[0] = (cur[0] << 8) | cur[1];
4057 dest16[1] = 0xffff;
4058 }
4059 } else {
4060 STBI_ASSERT(img_n == 3);
4061 for (i = 0; i < x; ++i, dest16 += 4, cur += 6) {
4062 dest16[0] = (cur[0] << 8) | cur[1];
4063 dest16[1] = (cur[2] << 8) | cur[3];
4064 dest16[2] = (cur[4] << 8) | cur[5];
4065 dest16[3] = 0xffff;
4066 }
4067 }
4068 }
4069 }
4070 }
4071
4072 STBI_FREE(filter_buf);
4073 if (!all_ok) return 0;
4074
4075 return 1;
4076}
4077
4078static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced)
4079{
4080 int bytes = (depth == 16 ? 2 : 1);
4081 int out_bytes = out_n * bytes;
4082 stbi_uc *final;
4083 int p;
4084 if (!interlaced)
4085 return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color);
4086
4087 final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0);
4088 if (!final) return stbi__err("outofmem", "Out of memory");
4089 for (p=0; p < 7; ++p) {
4090 int xorig[] = { 0,4,0,2,0,1,0 };
4091 int yorig[] = { 0,0,4,0,2,0,1 };
4092 int xspc[] = { 8,8,4,4,2,2,1 };
4093 int yspc[] = { 8,8,8,4,4,2,2 };
4094 int i,j,x,y;
4095
4096 x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
4097 y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
4098 if (x && y) {
4099 stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;
4100 if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {
4101 STBI_FREE(final);
4102 return 0;
4103 }
4104 for (j=0; j < y; ++j) {
4105 for (i=0; i < x; ++i) {
4106 int out_y = j*yspc[p]+yorig[p];
4107 int out_x = i*xspc[p]+xorig[p];
4108 memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes,
4109 a->out + (j*x+i)*out_bytes, out_bytes);
4110 }
4111 }
4112 STBI_FREE(a->out);
4113 image_data += img_len;
4114 image_data_len -= img_len;
4115 }
4116 }
4117 a->out = final;
4118
4119 return 1;
4120}
4121
4122static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n)
4123{
4124 stbi__context *s = z->s;
4125 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
4126 stbi_uc *p = z->out;
4127
4128 STBI_ASSERT(out_n == 2 || out_n == 4);
4129
4130 if (out_n == 2) {
4131 for (i=0; i < pixel_count; ++i) {
4132 p[1] = (p[0] == tc[0] ? 0 : 255);
4133 p += 2;
4134 }
4135 } else {
4136 for (i=0; i < pixel_count; ++i) {
4137 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
4138 p[3] = 0;
4139 p += 4;
4140 }
4141 }
4142 return 1;
4143}
4144
4145static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n)
4146{
4147 stbi__context *s = z->s;
4148 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
4149 stbi__uint16 *p = (stbi__uint16*) z->out;
4150
4151 STBI_ASSERT(out_n == 2 || out_n == 4);
4152
4153 if (out_n == 2) {
4154 for (i = 0; i < pixel_count; ++i) {
4155 p[1] = (p[0] == tc[0] ? 0 : 65535);
4156 p += 2;
4157 }
4158 } else {
4159 for (i = 0; i < pixel_count; ++i) {
4160 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
4161 p[3] = 0;
4162 p += 4;
4163 }
4164 }
4165 return 1;
4166}
4167
4168static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n)
4169{
4170 stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
4171 stbi_uc *p, *temp_out, *orig = a->out;
4172
4173 p = (stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0);
4174 if (p == NULL) return stbi__err("outofmem", "Out of memory");
4175
4176 temp_out = p;
4177
4178 if (pal_img_n == 3) {
4179 for (i=0; i < pixel_count; ++i) {
4180 int n = orig[i]*4;
4181 p[0] = palette[n ];
4182 p[1] = palette[n+1];
4183 p[2] = palette[n+2];
4184 p += 3;
4185 }
4186 } else {
4187 for (i=0; i < pixel_count; ++i) {
4188 int n = orig[i]*4;
4189 p[0] = palette[n ];
4190 p[1] = palette[n+1];
4191 p[2] = palette[n+2];
4192 p[3] = palette[n+3];
4193 p += 4;
4194 }
4195 }
4196 STBI_FREE(a->out);
4197 a->out = temp_out;
4198
4199 STBI_NOTUSED(len);
4200
4201 return 1;
4202}
4203
4204static int stbi__unpremultiply_on_load_global = 0;
4205static int stbi__de_iphone_flag_global = 0;
4206
4207STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
4208{
4209 stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply;
4210}
4211
4212STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
4213{
4214 stbi__de_iphone_flag_global = flag_true_if_should_convert;
4215}
4216
4217#ifndef STBI_THREAD_LOCAL
4218#define stbi__unpremultiply_on_load stbi__unpremultiply_on_load_global
4219#define stbi__de_iphone_flag stbi__de_iphone_flag_global
4220#else
4221static STBI_THREAD_LOCAL int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set;
4222static STBI_THREAD_LOCAL int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set;
4223
4224STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply)
4225{
4226 stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply;
4227 stbi__unpremultiply_on_load_set = 1;
4228}
4229
4230STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert)
4231{
4232 stbi__de_iphone_flag_local = flag_true_if_should_convert;
4233 stbi__de_iphone_flag_set = 1;
4234}
4235
4236#define stbi__unpremultiply_on_load (stbi__unpremultiply_on_load_set \
4237 ? stbi__unpremultiply_on_load_local \
4238 : stbi__unpremultiply_on_load_global)
4239#define stbi__de_iphone_flag (stbi__de_iphone_flag_set \
4240 ? stbi__de_iphone_flag_local \
4241 : stbi__de_iphone_flag_global)
4242#endif
4243
4244static void stbi__de_iphone(stbi__png *z)
4245{
4246 stbi__context *s = z->s;
4247 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
4248 stbi_uc *p = z->out;
4249
4250 if (s->img_out_n == 3) {
4251 for (i=0; i < pixel_count; ++i) {
4252 stbi_uc t = p[0];
4253 p[0] = p[2];
4254 p[2] = t;
4255 p += 3;
4256 }
4257 } else {
4258 STBI_ASSERT(s->img_out_n == 4);
4259 if (stbi__unpremultiply_on_load) {
4260
4261 for (i=0; i < pixel_count; ++i) {
4262 stbi_uc a = p[3];
4263 stbi_uc t = p[0];
4264 if (a) {
4265 stbi_uc half = a / 2;
4266 p[0] = (p[2] * 255 + half) / a;
4267 p[1] = (p[1] * 255 + half) / a;
4268 p[2] = ( t * 255 + half) / a;
4269 } else {
4270 p[0] = p[2];
4271 p[2] = t;
4272 }
4273 p += 4;
4274 }
4275 } else {
4276
4277 for (i=0; i < pixel_count; ++i) {
4278 stbi_uc t = p[0];
4279 p[0] = p[2];
4280 p[2] = t;
4281 p += 4;
4282 }
4283 }
4284 }
4285}
4286
4287#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d))
4288
4289static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
4290{
4291 stbi_uc palette[1024], pal_img_n=0;
4292 stbi_uc has_trans=0, tc[3]={0};
4293 stbi__uint16 tc16[3];
4294 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
4295 int first=1,k,interlace=0, color=0, is_iphone=0;
4296 stbi__context *s = z->s;
4297
4298 z->expanded = NULL;
4299 z->idata = NULL;
4300 z->out = NULL;
4301
4302 if (!stbi__check_png_header(s)) return 0;
4303
4304 if (scan == STBI__SCAN_type) return 1;
4305
4306 for (;;) {
4307 stbi__pngchunk c = stbi__get_chunk_header(s);
4308 switch (c.type) {
4309 case STBI__PNG_TYPE('C','g','B','I'):
4310 is_iphone = 1;
4311 stbi__skip(s, c.length);
4312 break;
4313 case STBI__PNG_TYPE('I','H','D','R'): {
4314 int comp,filter;
4315 if (!first) return stbi__err("multiple IHDR","Corrupt PNG");
4316 first = 0;
4317 if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");
4318 s->img_x = stbi__get32be(s);
4319 s->img_y = stbi__get32be(s);
4320 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
4321 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
4322 z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only");
4323 color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
4324 if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG");
4325 if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
4326 comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
4327 filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
4328 interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
4329 if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG");
4330 if (!pal_img_n) {
4331 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
4332 if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
4333 } else {
4334
4335 s->img_n = 1;
4336 if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
4337 }
4338
4339 break;
4340 }
4341
4342 case STBI__PNG_TYPE('P','L','T','E'): {
4343 if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4344 if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG");
4345 pal_len = c.length / 3;
4346 if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG");
4347 for (i=0; i < pal_len; ++i) {
4348 palette[i*4+0] = stbi__get8(s);
4349 palette[i*4+1] = stbi__get8(s);
4350 palette[i*4+2] = stbi__get8(s);
4351 palette[i*4+3] = 255;
4352 }
4353 break;
4354 }
4355
4356 case STBI__PNG_TYPE('t','R','N','S'): {
4357 if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4358 if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG");
4359 if (pal_img_n) {
4360 if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; }
4361 if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG");
4362 if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG");
4363 pal_img_n = 4;
4364 for (i=0; i < c.length; ++i)
4365 palette[i*4+3] = stbi__get8(s);
4366 } else {
4367 if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG");
4368 if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG");
4369 has_trans = 1;
4370
4371 if (scan == STBI__SCAN_header) { ++s->img_n; return 1; }
4372 if (z->depth == 16) {
4373 for (k = 0; k < s->img_n && k < 3; ++k)
4374 tc16[k] = (stbi__uint16)stbi__get16be(s);
4375 } else {
4376 for (k = 0; k < s->img_n && k < 3; ++k)
4377 tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth];
4378 }
4379 }
4380 break;
4381 }
4382
4383 case STBI__PNG_TYPE('I','D','A','T'): {
4384 if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4385 if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG");
4386 if (scan == STBI__SCAN_header) {
4387
4388 if (pal_img_n)
4389 s->img_n = pal_img_n;
4390 return 1;
4391 }
4392 if (c.length > (1u << 30)) return stbi__err("IDAT size limit", "IDAT section larger than 2^30 bytes");
4393 if ((int)(ioff + c.length) < (int)ioff) return 0;
4394 if (ioff + c.length > idata_limit) {
4395 stbi__uint32 idata_limit_old = idata_limit;
4396 stbi_uc *p;
4397 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
4398 while (ioff + c.length > idata_limit)
4399 idata_limit *= 2;
4400 STBI_NOTUSED(idata_limit_old);
4401 p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
4402 z->idata = p;
4403 }
4404 if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG");
4405 ioff += c.length;
4406 break;
4407 }
4408
4409 case STBI__PNG_TYPE('I','E','N','D'): {
4410 stbi__uint32 raw_len, bpl;
4411 if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4412 if (scan != STBI__SCAN_load) return 1;
4413 if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");
4414
4415 bpl = (s->img_x * z->depth + 7) / 8;
4416 raw_len = bpl * s->img_y * s->img_n + s->img_y ;
4417 z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);
4418 if (z->expanded == NULL) return 0;
4419 STBI_FREE(z->idata); z->idata = NULL;
4420 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
4421 s->img_out_n = s->img_n+1;
4422 else
4423 s->img_out_n = s->img_n;
4424 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0;
4425 if (has_trans) {
4426 if (z->depth == 16) {
4427 if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0;
4428 } else {
4429 if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
4430 }
4431 }
4432 if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
4433 stbi__de_iphone(z);
4434 if (pal_img_n) {
4435
4436 s->img_n = pal_img_n;
4437 s->img_out_n = pal_img_n;
4438 if (req_comp >= 3) s->img_out_n = req_comp;
4439 if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
4440 return 0;
4441 } else if (has_trans) {
4442
4443 ++s->img_n;
4444 }
4445 STBI_FREE(z->expanded); z->expanded = NULL;
4446
4447 stbi__get32be(s);
4448 return 1;
4449 }
4450
4451 default:
4452
4453 if (first) return stbi__err("first not IHDR", "Corrupt PNG");
4454 if ((c.type & (1 << 29)) == 0) {
4455 #ifndef STBI_NO_FAILURE_STRINGS
4456
4457 static char invalid_chunk[] = "XXXX PNG chunk not known";
4458 invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
4459 invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
4460 invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
4461 invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
4462 #endif
4463 return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type");
4464 }
4465 stbi__skip(s, c.length);
4466 break;
4467 }
4468
4469 stbi__get32be(s);
4470 }
4471}
4472
4473static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri)
4474{
4475 void *result=NULL;
4476 if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
4477 if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
4478 if (p->depth <= 8)
4479 ri->bits_per_channel = 8;
4480 else if (p->depth == 16)
4481 ri->bits_per_channel = 16;
4482 else
4483 return stbi__errpuc("bad bits_per_channel", "PNG not supported: unsupported color depth");
4484 result = p->out;
4485 p->out = NULL;
4486 if (req_comp && req_comp != p->s->img_out_n) {
4487 if (ri->bits_per_channel == 8)
4488 result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
4489 else
4490 result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
4491 p->s->img_out_n = req_comp;
4492 if (result == NULL) return result;
4493 }
4494 *x = p->s->img_x;
4495 *y = p->s->img_y;
4496 if (n) *n = p->s->img_n;
4497 }
4498 STBI_FREE(p->out); p->out = NULL;
4499 STBI_FREE(p->expanded); p->expanded = NULL;
4500 STBI_FREE(p->idata); p->idata = NULL;
4501
4502 return result;
4503}
4504
4505static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
4506{
4507 stbi__png p;
4508 p.s = s;
4509 return stbi__do_png(&p, x,y,comp,req_comp, ri);
4510}
4511
4512static int stbi__png_test(stbi__context *s)
4513{
4514 int r;
4515 r = stbi__check_png_header(s);
4516 stbi__rewind(s);
4517 return r;
4518}
4519
4520static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp)
4521{
4522 if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) {
4523 stbi__rewind( p->s );
4524 return 0;
4525 }
4526 if (x) *x = p->s->img_x;
4527 if (y) *y = p->s->img_y;
4528 if (comp) *comp = p->s->img_n;
4529 return 1;
4530}
4531
4532static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp)
4533{
4534 stbi__png p;
4535 p.s = s;
4536 return stbi__png_info_raw(&p, x, y, comp);
4537}
4538
4539static int stbi__png_is16(stbi__context *s)
4540{
4541 stbi__png p;
4542 p.s = s;
4543 if (!stbi__png_info_raw(&p, NULL, NULL, NULL))
4544 return 0;
4545 if (p.depth != 16) {
4546 stbi__rewind(p.s);
4547 return 0;
4548 }
4549 return 1;
4550}
4551#endif
4552
4553#ifndef STBI_NO_BMP
4554static int stbi__bmp_test_raw(stbi__context *s)
4555{
4556 int r;
4557 int sz;
4558 if (stbi__get8(s) != 'B') return 0;
4559 if (stbi__get8(s) != 'M') return 0;
4560 stbi__get32le(s);
4561 stbi__get16le(s);
4562 stbi__get16le(s);
4563 stbi__get32le(s);
4564 sz = stbi__get32le(s);
4565 r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
4566 return r;
4567}
4568
4569static int stbi__bmp_test(stbi__context *s)
4570{
4571 int r = stbi__bmp_test_raw(s);
4572 stbi__rewind(s);
4573 return r;
4574}
4575
4576static int stbi__high_bit(unsigned int z)
4577{
4578 int n=0;
4579 if (z == 0) return -1;
4580 if (z >= 0x10000) { n += 16; z >>= 16; }
4581 if (z >= 0x00100) { n += 8; z >>= 8; }
4582 if (z >= 0x00010) { n += 4; z >>= 4; }
4583 if (z >= 0x00004) { n += 2; z >>= 2; }
4584 if (z >= 0x00002) { n += 1; }
4585 return n;
4586}
4587
4588static int stbi__bitcount(unsigned int a)
4589{
4590 a = (a & 0x55555555) + ((a >> 1) & 0x55555555);
4591 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
4592 a = (a + (a >> 4)) & 0x0f0f0f0f;
4593 a = (a + (a >> 8));
4594 a = (a + (a >> 16));
4595 return a & 0xff;
4596}
4597
4598static int stbi__shiftsigned(unsigned int v, int shift, int bits)
4599{
4600 static unsigned int mul_table[9] = {
4601 0,
4602 0xff, 0x55, 0x49, 0x11,
4603 0x21, 0x41, 0x81, 0x01,
4604 };
4605 static unsigned int shift_table[9] = {
4606 0, 0,0,1,0,2,4,6,0,
4607 };
4608 if (shift < 0)
4609 v <<= -shift;
4610 else
4611 v >>= shift;
4612 STBI_ASSERT(v < 256);
4613 v >>= (8-bits);
4614 STBI_ASSERT(bits >= 0 && bits <= 8);
4615 return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits];
4616}
4617
4618typedef struct
4619{
4620 int bpp, offset, hsz;
4621 unsigned int mr,mg,mb,ma, all_a;
4622 int extra_read;
4623} stbi__bmp_data;
4624
4625static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress)
4626{
4627
4628 if (compress == 3)
4629 return 1;
4630
4631 if (compress == 0) {
4632 if (info->bpp == 16) {
4633 info->mr = 31u << 10;
4634 info->mg = 31u << 5;
4635 info->mb = 31u << 0;
4636 } else if (info->bpp == 32) {
4637 info->mr = 0xffu << 16;
4638 info->mg = 0xffu << 8;
4639 info->mb = 0xffu << 0;
4640 info->ma = 0xffu << 24;
4641 info->all_a = 0;
4642 } else {
4643
4644 info->mr = info->mg = info->mb = info->ma = 0;
4645 }
4646 return 1;
4647 }
4648 return 0;
4649}
4650
4651static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
4652{
4653 int hsz;
4654 if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP");
4655 stbi__get32le(s);
4656 stbi__get16le(s);
4657 stbi__get16le(s);
4658 info->offset = stbi__get32le(s);
4659 info->hsz = hsz = stbi__get32le(s);
4660 info->mr = info->mg = info->mb = info->ma = 0;
4661 info->extra_read = 14;
4662
4663 if (info->offset < 0) return stbi__errpuc("bad BMP", "bad BMP");
4664
4665 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown");
4666 if (hsz == 12) {
4667 s->img_x = stbi__get16le(s);
4668 s->img_y = stbi__get16le(s);
4669 } else {
4670 s->img_x = stbi__get32le(s);
4671 s->img_y = stbi__get32le(s);
4672 }
4673 if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP");
4674 info->bpp = stbi__get16le(s);
4675 if (hsz != 12) {
4676 int compress = stbi__get32le(s);
4677 if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");
4678 if (compress >= 4) return stbi__errpuc("BMP JPEG/PNG", "BMP type not supported: unsupported compression");
4679 if (compress == 3 && info->bpp != 16 && info->bpp != 32) return stbi__errpuc("bad BMP", "bad BMP");
4680 stbi__get32le(s);
4681 stbi__get32le(s);
4682 stbi__get32le(s);
4683 stbi__get32le(s);
4684 stbi__get32le(s);
4685 if (hsz == 40 || hsz == 56) {
4686 if (hsz == 56) {
4687 stbi__get32le(s);
4688 stbi__get32le(s);
4689 stbi__get32le(s);
4690 stbi__get32le(s);
4691 }
4692 if (info->bpp == 16 || info->bpp == 32) {
4693 if (compress == 0) {
4694 stbi__bmp_set_mask_defaults(info, compress);
4695 } else if (compress == 3) {
4696 info->mr = stbi__get32le(s);
4697 info->mg = stbi__get32le(s);
4698 info->mb = stbi__get32le(s);
4699 info->extra_read += 12;
4700
4701 if (info->mr == info->mg && info->mg == info->mb) {
4702
4703 return stbi__errpuc("bad BMP", "bad BMP");
4704 }
4705 } else
4706 return stbi__errpuc("bad BMP", "bad BMP");
4707 }
4708 } else {
4709
4710 int i;
4711 if (hsz != 108 && hsz != 124)
4712 return stbi__errpuc("bad BMP", "bad BMP");
4713 info->mr = stbi__get32le(s);
4714 info->mg = stbi__get32le(s);
4715 info->mb = stbi__get32le(s);
4716 info->ma = stbi__get32le(s);
4717 if (compress != 3)
4718 stbi__bmp_set_mask_defaults(info, compress);
4719 stbi__get32le(s);
4720 for (i=0; i < 12; ++i)
4721 stbi__get32le(s);
4722 if (hsz == 124) {
4723 stbi__get32le(s);
4724 stbi__get32le(s);
4725 stbi__get32le(s);
4726 stbi__get32le(s);
4727 }
4728 }
4729 }
4730 return (void *) 1;
4731}
4732
4733static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
4734{
4735 stbi_uc *out;
4736 unsigned int mr=0,mg=0,mb=0,ma=0, all_a;
4737 stbi_uc pal[256][4];
4738 int psize=0,i,j,width;
4739 int flip_vertically, pad, target;
4740 stbi__bmp_data info;
4741 STBI_NOTUSED(ri);
4742
4743 info.all_a = 255;
4744 if (stbi__bmp_parse_header(s, &info) == NULL)
4745 return NULL;
4746
4747 flip_vertically = ((int) s->img_y) > 0;
4748 s->img_y = abs((int) s->img_y);
4749
4750 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
4751 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
4752
4753 mr = info.mr;
4754 mg = info.mg;
4755 mb = info.mb;
4756 ma = info.ma;
4757 all_a = info.all_a;
4758
4759 if (info.hsz == 12) {
4760 if (info.bpp < 24)
4761 psize = (info.offset - info.extra_read - 24) / 3;
4762 } else {
4763 if (info.bpp < 16)
4764 psize = (info.offset - info.extra_read - info.hsz) >> 2;
4765 }
4766 if (psize == 0) {
4767
4768 int bytes_read_so_far = s->callback_already_read + (int)(s->img_buffer - s->img_buffer_original);
4769 int header_limit = 1024;
4770 int extra_data_limit = 256*4;
4771 if (bytes_read_so_far <= 0 || bytes_read_so_far > header_limit) {
4772 return stbi__errpuc("bad header", "Corrupt BMP");
4773 }
4774
4775 if (info.offset < bytes_read_so_far || info.offset - bytes_read_so_far > extra_data_limit) {
4776 return stbi__errpuc("bad offset", "Corrupt BMP");
4777 } else {
4778 stbi__skip(s, info.offset - bytes_read_so_far);
4779 }
4780 }
4781
4782 if (info.bpp == 24 && ma == 0xff000000)
4783 s->img_n = 3;
4784 else
4785 s->img_n = ma ? 4 : 3;
4786 if (req_comp && req_comp >= 3)
4787 target = req_comp;
4788 else
4789 target = s->img_n;
4790
4791 if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0))
4792 return stbi__errpuc("too large", "Corrupt BMP");
4793
4794 out = (stbi_uc *) stbi__malloc_mad3(target, s->img_x, s->img_y, 0);
4795 if (!out) return stbi__errpuc("outofmem", "Out of memory");
4796 if (info.bpp < 16) {
4797 int z=0;
4798 if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
4799 for (i=0; i < psize; ++i) {
4800 pal[i][2] = stbi__get8(s);
4801 pal[i][1] = stbi__get8(s);
4802 pal[i][0] = stbi__get8(s);
4803 if (info.hsz != 12) stbi__get8(s);
4804 pal[i][3] = 255;
4805 }
4806 stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4));
4807 if (info.bpp == 1) width = (s->img_x + 7) >> 3;
4808 else if (info.bpp == 4) width = (s->img_x + 1) >> 1;
4809 else if (info.bpp == 8) width = s->img_x;
4810 else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }
4811 pad = (-width)&3;
4812 if (info.bpp == 1) {
4813 for (j=0; j < (int) s->img_y; ++j) {
4814 int bit_offset = 7, v = stbi__get8(s);
4815 for (i=0; i < (int) s->img_x; ++i) {
4816 int color = (v>>bit_offset)&0x1;
4817 out[z++] = pal[color][0];
4818 out[z++] = pal[color][1];
4819 out[z++] = pal[color][2];
4820 if (target == 4) out[z++] = 255;
4821 if (i+1 == (int) s->img_x) break;
4822 if((--bit_offset) < 0) {
4823 bit_offset = 7;
4824 v = stbi__get8(s);
4825 }
4826 }
4827 stbi__skip(s, pad);
4828 }
4829 } else {
4830 for (j=0; j < (int) s->img_y; ++j) {
4831 for (i=0; i < (int) s->img_x; i += 2) {
4832 int v=stbi__get8(s),v2=0;
4833 if (info.bpp == 4) {
4834 v2 = v & 15;
4835 v >>= 4;
4836 }
4837 out[z++] = pal[v][0];
4838 out[z++] = pal[v][1];
4839 out[z++] = pal[v][2];
4840 if (target == 4) out[z++] = 255;
4841 if (i+1 == (int) s->img_x) break;
4842 v = (info.bpp == 8) ? stbi__get8(s) : v2;
4843 out[z++] = pal[v][0];
4844 out[z++] = pal[v][1];
4845 out[z++] = pal[v][2];
4846 if (target == 4) out[z++] = 255;
4847 }
4848 stbi__skip(s, pad);
4849 }
4850 }
4851 } else {
4852 int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
4853 int z = 0;
4854 int easy=0;
4855 stbi__skip(s, info.offset - info.extra_read - info.hsz);
4856 if (info.bpp == 24) width = 3 * s->img_x;
4857 else if (info.bpp == 16) width = 2*s->img_x;
4858 else width=0;
4859 pad = (-width) & 3;
4860 if (info.bpp == 24) {
4861 easy = 1;
4862 } else if (info.bpp == 32) {
4863 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
4864 easy = 2;
4865 }
4866 if (!easy) {
4867 if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
4868
4869 rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);
4870 gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
4871 bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
4872 ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
4873 if (rcount > 8 || gcount > 8 || bcount > 8 || acount > 8) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
4874 }
4875 for (j=0; j < (int) s->img_y; ++j) {
4876 if (easy) {
4877 for (i=0; i < (int) s->img_x; ++i) {
4878 unsigned char a;
4879 out[z+2] = stbi__get8(s);
4880 out[z+1] = stbi__get8(s);
4881 out[z+0] = stbi__get8(s);
4882 z += 3;
4883 a = (easy == 2 ? stbi__get8(s) : 255);
4884 all_a |= a;
4885 if (target == 4) out[z++] = a;
4886 }
4887 } else {
4888 int bpp = info.bpp;
4889 for (i=0; i < (int) s->img_x; ++i) {
4890 stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s));
4891 unsigned int a;
4892 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount));
4893 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount));
4894 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
4895 a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
4896 all_a |= a;
4897 if (target == 4) out[z++] = STBI__BYTECAST(a);
4898 }
4899 }
4900 stbi__skip(s, pad);
4901 }
4902 }
4903
4904 if (target == 4 && all_a == 0)
4905 for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4)
4906 out[i] = 255;
4907
4908 if (flip_vertically) {
4909 stbi_uc t;
4910 for (j=0; j < (int) s->img_y>>1; ++j) {
4911 stbi_uc *p1 = out + j *s->img_x*target;
4912 stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
4913 for (i=0; i < (int) s->img_x*target; ++i) {
4914 t = p1[i]; p1[i] = p2[i]; p2[i] = t;
4915 }
4916 }
4917 }
4918
4919 if (req_comp && req_comp != target) {
4920 out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
4921 if (out == NULL) return out;
4922 }
4923
4924 *x = s->img_x;
4925 *y = s->img_y;
4926 if (comp) *comp = s->img_n;
4927 return out;
4928}
4929#endif
4930
4931#ifndef STBI_NO_TGA
4932
4933static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16)
4934{
4935
4936 if (is_rgb16) *is_rgb16 = 0;
4937 switch(bits_per_pixel) {
4938 case 8: return STBI_grey;
4939 case 16: if(is_grey) return STBI_grey_alpha;
4940
4941 case 15: if(is_rgb16) *is_rgb16 = 1;
4942 return STBI_rgb;
4943 case 24:
4944 case 32: return bits_per_pixel/8;
4945 default: return 0;
4946 }
4947}
4948
4949static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp)
4950{
4951 int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp;
4952 int sz, tga_colormap_type;
4953 stbi__get8(s);
4954 tga_colormap_type = stbi__get8(s);
4955 if( tga_colormap_type > 1 ) {
4956 stbi__rewind(s);
4957 return 0;
4958 }
4959 tga_image_type = stbi__get8(s);
4960 if ( tga_colormap_type == 1 ) {
4961 if (tga_image_type != 1 && tga_image_type != 9) {
4962 stbi__rewind(s);
4963 return 0;
4964 }
4965 stbi__skip(s,4);
4966 sz = stbi__get8(s);
4967 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) {
4968 stbi__rewind(s);
4969 return 0;
4970 }
4971 stbi__skip(s,4);
4972 tga_colormap_bpp = sz;
4973 } else {
4974 if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) {
4975 stbi__rewind(s);
4976 return 0;
4977 }
4978 stbi__skip(s,9);
4979 tga_colormap_bpp = 0;
4980 }
4981 tga_w = stbi__get16le(s);
4982 if( tga_w < 1 ) {
4983 stbi__rewind(s);
4984 return 0;
4985 }
4986 tga_h = stbi__get16le(s);
4987 if( tga_h < 1 ) {
4988 stbi__rewind(s);
4989 return 0;
4990 }
4991 tga_bits_per_pixel = stbi__get8(s);
4992 stbi__get8(s);
4993 if (tga_colormap_bpp != 0) {
4994 if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) {
4995
4996 stbi__rewind(s);
4997 return 0;
4998 }
4999 tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
5000 } else {
5001 tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
5002 }
5003 if(!tga_comp) {
5004 stbi__rewind(s);
5005 return 0;
5006 }
5007 if (x) *x = tga_w;
5008 if (y) *y = tga_h;
5009 if (comp) *comp = tga_comp;
5010 return 1;
5011}
5012
5013static int stbi__tga_test(stbi__context *s)
5014{
5015 int res = 0;
5016 int sz, tga_color_type;
5017 stbi__get8(s);
5018 tga_color_type = stbi__get8(s);
5019 if ( tga_color_type > 1 ) goto errorEnd;
5020 sz = stbi__get8(s);
5021 if ( tga_color_type == 1 ) {
5022 if (sz != 1 && sz != 9) goto errorEnd;
5023 stbi__skip(s,4);
5024 sz = stbi__get8(s);
5025 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
5026 stbi__skip(s,4);
5027 } else {
5028 if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd;
5029 stbi__skip(s,9);
5030 }
5031 if ( stbi__get16le(s) < 1 ) goto errorEnd;
5032 if ( stbi__get16le(s) < 1 ) goto errorEnd;
5033 sz = stbi__get8(s);
5034 if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd;
5035 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
5036
5037 res = 1;
5038
5039errorEnd:
5040 stbi__rewind(s);
5041 return res;
5042}
5043
5044static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out)
5045{
5046 stbi__uint16 px = (stbi__uint16)stbi__get16le(s);
5047 stbi__uint16 fiveBitMask = 31;
5048
5049 int r = (px >> 10) & fiveBitMask;
5050 int g = (px >> 5) & fiveBitMask;
5051 int b = px & fiveBitMask;
5052
5053 out[0] = (stbi_uc)((r * 255)/31);
5054 out[1] = (stbi_uc)((g * 255)/31);
5055 out[2] = (stbi_uc)((b * 255)/31);
5056
5057}
5058
5059static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
5060{
5061
5062 int tga_offset = stbi__get8(s);
5063 int tga_indexed = stbi__get8(s);
5064 int tga_image_type = stbi__get8(s);
5065 int tga_is_RLE = 0;
5066 int tga_palette_start = stbi__get16le(s);
5067 int tga_palette_len = stbi__get16le(s);
5068 int tga_palette_bits = stbi__get8(s);
5069 int tga_x_origin = stbi__get16le(s);
5070 int tga_y_origin = stbi__get16le(s);
5071 int tga_width = stbi__get16le(s);
5072 int tga_height = stbi__get16le(s);
5073 int tga_bits_per_pixel = stbi__get8(s);
5074 int tga_comp, tga_rgb16=0;
5075 int tga_inverted = stbi__get8(s);
5076
5077 unsigned char *tga_data;
5078 unsigned char *tga_palette = NULL;
5079 int i, j;
5080 unsigned char raw_data[4] = {0};
5081 int RLE_count = 0;
5082 int RLE_repeating = 0;
5083 int read_next_pixel = 1;
5084 STBI_NOTUSED(ri);
5085 STBI_NOTUSED(tga_x_origin);
5086 STBI_NOTUSED(tga_y_origin);
5087
5088 if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5089 if (tga_width > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5090
5091 if ( tga_image_type >= 8 )
5092 {
5093 tga_image_type -= 8;
5094 tga_is_RLE = 1;
5095 }
5096 tga_inverted = 1 - ((tga_inverted >> 5) & 1);
5097
5098 if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
5099 else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16);
5100
5101 if(!tga_comp)
5102 return stbi__errpuc("bad format", "Can't find out TGA pixelformat");
5103
5104 *x = tga_width;
5105 *y = tga_height;
5106 if (comp) *comp = tga_comp;
5107
5108 if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0))
5109 return stbi__errpuc("too large", "Corrupt TGA");
5110
5111 tga_data = (unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0);
5112 if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");
5113
5114 stbi__skip(s, tga_offset );
5115
5116 if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) {
5117 for (i=0; i < tga_height; ++i) {
5118 int row = tga_inverted ? tga_height -i - 1 : i;
5119 stbi_uc *tga_row = tga_data + row*tga_width*tga_comp;
5120 stbi__getn(s, tga_row, tga_width * tga_comp);
5121 }
5122 } else {
5123
5124 if ( tga_indexed)
5125 {
5126 if (tga_palette_len == 0) {
5127 STBI_FREE(tga_data);
5128 return stbi__errpuc("bad palette", "Corrupt TGA");
5129 }
5130
5131 stbi__skip(s, tga_palette_start );
5132
5133 tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0);
5134 if (!tga_palette) {
5135 STBI_FREE(tga_data);
5136 return stbi__errpuc("outofmem", "Out of memory");
5137 }
5138 if (tga_rgb16) {
5139 stbi_uc *pal_entry = tga_palette;
5140 STBI_ASSERT(tga_comp == STBI_rgb);
5141 for (i=0; i < tga_palette_len; ++i) {
5142 stbi__tga_read_rgb16(s, pal_entry);
5143 pal_entry += tga_comp;
5144 }
5145 } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
5146 STBI_FREE(tga_data);
5147 STBI_FREE(tga_palette);
5148 return stbi__errpuc("bad palette", "Corrupt TGA");
5149 }
5150 }
5151
5152 for (i=0; i < tga_width * tga_height; ++i)
5153 {
5154
5155 if ( tga_is_RLE )
5156 {
5157 if ( RLE_count == 0 )
5158 {
5159
5160 int RLE_cmd = stbi__get8(s);
5161 RLE_count = 1 + (RLE_cmd & 127);
5162 RLE_repeating = RLE_cmd >> 7;
5163 read_next_pixel = 1;
5164 } else if ( !RLE_repeating )
5165 {
5166 read_next_pixel = 1;
5167 }
5168 } else
5169 {
5170 read_next_pixel = 1;
5171 }
5172
5173 if ( read_next_pixel )
5174 {
5175
5176 if ( tga_indexed )
5177 {
5178
5179 int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s);
5180 if ( pal_idx >= tga_palette_len ) {
5181
5182 pal_idx = 0;
5183 }
5184 pal_idx *= tga_comp;
5185 for (j = 0; j < tga_comp; ++j) {
5186 raw_data[j] = tga_palette[pal_idx+j];
5187 }
5188 } else if(tga_rgb16) {
5189 STBI_ASSERT(tga_comp == STBI_rgb);
5190 stbi__tga_read_rgb16(s, raw_data);
5191 } else {
5192
5193 for (j = 0; j < tga_comp; ++j) {
5194 raw_data[j] = stbi__get8(s);
5195 }
5196 }
5197
5198 read_next_pixel = 0;
5199 }
5200
5201 for (j = 0; j < tga_comp; ++j)
5202 tga_data[i*tga_comp+j] = raw_data[j];
5203
5204 --RLE_count;
5205 }
5206
5207 if ( tga_inverted )
5208 {
5209 for (j = 0; j*2 < tga_height; ++j)
5210 {
5211 int index1 = j * tga_width * tga_comp;
5212 int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
5213 for (i = tga_width * tga_comp; i > 0; --i)
5214 {
5215 unsigned char temp = tga_data[index1];
5216 tga_data[index1] = tga_data[index2];
5217 tga_data[index2] = temp;
5218 ++index1;
5219 ++index2;
5220 }
5221 }
5222 }
5223
5224 if ( tga_palette != NULL )
5225 {
5226 STBI_FREE( tga_palette );
5227 }
5228 }
5229
5230 if (tga_comp >= 3 && !tga_rgb16)
5231 {
5232 unsigned char* tga_pixel = tga_data;
5233 for (i=0; i < tga_width * tga_height; ++i)
5234 {
5235 unsigned char temp = tga_pixel[0];
5236 tga_pixel[0] = tga_pixel[2];
5237 tga_pixel[2] = temp;
5238 tga_pixel += tga_comp;
5239 }
5240 }
5241
5242 if (req_comp && req_comp != tga_comp)
5243 tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
5244
5245 tga_palette_start = tga_palette_len = tga_palette_bits =
5246 tga_x_origin = tga_y_origin = 0;
5247 STBI_NOTUSED(tga_palette_start);
5248
5249 return tga_data;
5250}
5251#endif
5252
5253#ifndef STBI_NO_PSD
5254static int stbi__psd_test(stbi__context *s)
5255{
5256 int r = (stbi__get32be(s) == 0x38425053);
5257 stbi__rewind(s);
5258 return r;
5259}
5260
5261static int stbi__psd_decode_rle(stbi__context *s, stbi_uc *p, int pixelCount)
5262{
5263 int count, nleft, len;
5264
5265 count = 0;
5266 while ((nleft = pixelCount - count) > 0) {
5267 len = stbi__get8(s);
5268 if (len == 128) {
5269
5270 } else if (len < 128) {
5271
5272 len++;
5273 if (len > nleft) return 0;
5274 count += len;
5275 while (len) {
5276 *p = stbi__get8(s);
5277 p += 4;
5278 len--;
5279 }
5280 } else if (len > 128) {
5281 stbi_uc val;
5282
5283 len = 257 - len;
5284 if (len > nleft) return 0;
5285 val = stbi__get8(s);
5286 count += len;
5287 while (len) {
5288 *p = val;
5289 p += 4;
5290 len--;
5291 }
5292 }
5293 }
5294
5295 return 1;
5296}
5297
5298static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
5299{
5300 int pixelCount;
5301 int channelCount, compression;
5302 int channel, i;
5303 int bitdepth;
5304 int w,h;
5305 stbi_uc *out;
5306 STBI_NOTUSED(ri);
5307
5308 if (stbi__get32be(s) != 0x38425053)
5309 return stbi__errpuc("not PSD", "Corrupt PSD image");
5310
5311 if (stbi__get16be(s) != 1)
5312 return stbi__errpuc("wrong version", "Unsupported version of PSD image");
5313
5314 stbi__skip(s, 6 );
5315
5316 channelCount = stbi__get16be(s);
5317 if (channelCount < 0 || channelCount > 16)
5318 return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image");
5319
5320 h = stbi__get32be(s);
5321 w = stbi__get32be(s);
5322
5323 if (h > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5324 if (w > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5325
5326 bitdepth = stbi__get16be(s);
5327 if (bitdepth != 8 && bitdepth != 16)
5328 return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit");
5329
5330 if (stbi__get16be(s) != 3)
5331 return stbi__errpuc("wrong color format", "PSD is not in RGB color format");
5332
5333 stbi__skip(s,stbi__get32be(s) );
5334
5335 stbi__skip(s, stbi__get32be(s) );
5336
5337 stbi__skip(s, stbi__get32be(s) );
5338
5339 compression = stbi__get16be(s);
5340 if (compression > 1)
5341 return stbi__errpuc("bad compression", "PSD has an unknown compression format");
5342
5343 if (!stbi__mad3sizes_valid(4, w, h, 0))
5344 return stbi__errpuc("too large", "Corrupt PSD");
5345
5346 if (!compression && bitdepth == 16 && bpc == 16) {
5347 out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0);
5348 ri->bits_per_channel = 16;
5349 } else
5350 out = (stbi_uc *) stbi__malloc(4 * w*h);
5351
5352 if (!out) return stbi__errpuc("outofmem", "Out of memory");
5353 pixelCount = w*h;
5354
5355 if (compression) {
5356
5357 stbi__skip(s, h * channelCount * 2 );
5358
5359 for (channel = 0; channel < 4; channel++) {
5360 stbi_uc *p;
5361
5362 p = out+channel;
5363 if (channel >= channelCount) {
5364
5365 for (i = 0; i < pixelCount; i++, p += 4)
5366 *p = (channel == 3 ? 255 : 0);
5367 } else {
5368
5369 if (!stbi__psd_decode_rle(s, p, pixelCount)) {
5370 STBI_FREE(out);
5371 return stbi__errpuc("corrupt", "bad RLE data");
5372 }
5373 }
5374 }
5375
5376 } else {
5377
5378 for (channel = 0; channel < 4; channel++) {
5379 if (channel >= channelCount) {
5380
5381 if (bitdepth == 16 && bpc == 16) {
5382 stbi__uint16 *q = ((stbi__uint16 *) out) + channel;
5383 stbi__uint16 val = channel == 3 ? 65535 : 0;
5384 for (i = 0; i < pixelCount; i++, q += 4)
5385 *q = val;
5386 } else {
5387 stbi_uc *p = out+channel;
5388 stbi_uc val = channel == 3 ? 255 : 0;
5389 for (i = 0; i < pixelCount; i++, p += 4)
5390 *p = val;
5391 }
5392 } else {
5393 if (ri->bits_per_channel == 16) {
5394 stbi__uint16 *q = ((stbi__uint16 *) out) + channel;
5395 for (i = 0; i < pixelCount; i++, q += 4)
5396 *q = (stbi__uint16) stbi__get16be(s);
5397 } else {
5398 stbi_uc *p = out+channel;
5399 if (bitdepth == 16) {
5400 for (i = 0; i < pixelCount; i++, p += 4)
5401 *p = (stbi_uc) (stbi__get16be(s) >> 8);
5402 } else {
5403 for (i = 0; i < pixelCount; i++, p += 4)
5404 *p = stbi__get8(s);
5405 }
5406 }
5407 }
5408 }
5409 }
5410
5411 if (channelCount >= 4) {
5412 if (ri->bits_per_channel == 16) {
5413 for (i=0; i < w*h; ++i) {
5414 stbi__uint16 *pixel = (stbi__uint16 *) out + 4*i;
5415 if (pixel[3] != 0 && pixel[3] != 65535) {
5416 float a = pixel[3] / 65535.0f;
5417 float ra = 1.0f / a;
5418 float inv_a = 65535.0f * (1 - ra);
5419 pixel[0] = (stbi__uint16) (pixel[0]*ra + inv_a);
5420 pixel[1] = (stbi__uint16) (pixel[1]*ra + inv_a);
5421 pixel[2] = (stbi__uint16) (pixel[2]*ra + inv_a);
5422 }
5423 }
5424 } else {
5425 for (i=0; i < w*h; ++i) {
5426 unsigned char *pixel = out + 4*i;
5427 if (pixel[3] != 0 && pixel[3] != 255) {
5428 float a = pixel[3] / 255.0f;
5429 float ra = 1.0f / a;
5430 float inv_a = 255.0f * (1 - ra);
5431 pixel[0] = (unsigned char) (pixel[0]*ra + inv_a);
5432 pixel[1] = (unsigned char) (pixel[1]*ra + inv_a);
5433 pixel[2] = (unsigned char) (pixel[2]*ra + inv_a);
5434 }
5435 }
5436 }
5437 }
5438
5439 if (req_comp && req_comp != 4) {
5440 if (ri->bits_per_channel == 16)
5441 out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, 4, req_comp, w, h);
5442 else
5443 out = stbi__convert_format(out, 4, req_comp, w, h);
5444 if (out == NULL) return out;
5445 }
5446
5447 if (comp) *comp = 4;
5448 *y = h;
5449 *x = w;
5450
5451 return out;
5452}
5453#endif
5454
5455#ifndef STBI_NO_PIC
5456static int stbi__pic_is4(stbi__context *s,const char *str)
5457{
5458 int i;
5459 for (i=0; i<4; ++i)
5460 if (stbi__get8(s) != (stbi_uc)str[i])
5461 return 0;
5462
5463 return 1;
5464}
5465
5466static int stbi__pic_test_core(stbi__context *s)
5467{
5468 int i;
5469
5470 if (!stbi__pic_is4(s,"\x53\x80\xF6\x34"))
5471 return 0;
5472
5473 for(i=0;i<84;++i)
5474 stbi__get8(s);
5475
5476 if (!stbi__pic_is4(s,"PICT"))
5477 return 0;
5478
5479 return 1;
5480}
5481
5482typedef struct
5483{
5484 stbi_uc size,type,channel;
5485} stbi__pic_packet;
5486
5487static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest)
5488{
5489 int mask=0x80, i;
5490
5491 for (i=0; i<4; ++i, mask>>=1) {
5492 if (channel & mask) {
5493 if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short");
5494 dest[i]=stbi__get8(s);
5495 }
5496 }
5497
5498 return dest;
5499}
5500
5501static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src)
5502{
5503 int mask=0x80,i;
5504
5505 for (i=0;i<4; ++i, mask>>=1)
5506 if (channel&mask)
5507 dest[i]=src[i];
5508}
5509
5510static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
5511{
5512 int act_comp=0,num_packets=0,y,chained;
5513 stbi__pic_packet packets[10];
5514
5515 do {
5516 stbi__pic_packet *packet;
5517
5518 if (num_packets==sizeof(packets)/sizeof(packets[0]))
5519 return stbi__errpuc("bad format","too many packets");
5520
5521 packet = &packets[num_packets++];
5522
5523 chained = stbi__get8(s);
5524 packet->size = stbi__get8(s);
5525 packet->type = stbi__get8(s);
5526 packet->channel = stbi__get8(s);
5527
5528 act_comp |= packet->channel;
5529
5530 if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)");
5531 if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp");
5532 } while (chained);
5533
5534 *comp = (act_comp & 0x10 ? 4 : 3);
5535
5536 for(y=0; y<height; ++y) {
5537 int packet_idx;
5538
5539 for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
5540 stbi__pic_packet *packet = &packets[packet_idx];
5541 stbi_uc *dest = result+y*width*4;
5542
5543 switch (packet->type) {
5544 default:
5545 return stbi__errpuc("bad format","packet has bad compression type");
5546
5547 case 0: {
5548 int x;
5549
5550 for(x=0;x<width;++x, dest+=4)
5551 if (!stbi__readval(s,packet->channel,dest))
5552 return 0;
5553 break;
5554 }
5555
5556 case 1:
5557 {
5558 int left=width, i;
5559
5560 while (left>0) {
5561 stbi_uc count,value[4];
5562
5563 count=stbi__get8(s);
5564 if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)");
5565
5566 if (count > left)
5567 count = (stbi_uc) left;
5568
5569 if (!stbi__readval(s,packet->channel,value)) return 0;
5570
5571 for(i=0; i<count; ++i,dest+=4)
5572 stbi__copyval(packet->channel,dest,value);
5573 left -= count;
5574 }
5575 }
5576 break;
5577
5578 case 2: {
5579 int left=width;
5580 while (left>0) {
5581 int count = stbi__get8(s), i;
5582 if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)");
5583
5584 if (count >= 128) {
5585 stbi_uc value[4];
5586
5587 if (count==128)
5588 count = stbi__get16be(s);
5589 else
5590 count -= 127;
5591 if (count > left)
5592 return stbi__errpuc("bad file","scanline overrun");
5593
5594 if (!stbi__readval(s,packet->channel,value))
5595 return 0;
5596
5597 for(i=0;i<count;++i, dest += 4)
5598 stbi__copyval(packet->channel,dest,value);
5599 } else {
5600 ++count;
5601 if (count>left) return stbi__errpuc("bad file","scanline overrun");
5602
5603 for(i=0;i<count;++i, dest+=4)
5604 if (!stbi__readval(s,packet->channel,dest))
5605 return 0;
5606 }
5607 left-=count;
5608 }
5609 break;
5610 }
5611 }
5612 }
5613 }
5614
5615 return result;
5616}
5617
5618static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri)
5619{
5620 stbi_uc *result;
5621 int i, x,y, internal_comp;
5622 STBI_NOTUSED(ri);
5623
5624 if (!comp) comp = &internal_comp;
5625
5626 for (i=0; i<92; ++i)
5627 stbi__get8(s);
5628
5629 x = stbi__get16be(s);
5630 y = stbi__get16be(s);
5631
5632 if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5633 if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5634
5635 if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)");
5636 if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode");
5637
5638 stbi__get32be(s);
5639 stbi__get16be(s);
5640 stbi__get16be(s);
5641
5642 result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0);
5643 if (!result) return stbi__errpuc("outofmem", "Out of memory");
5644 memset(result, 0xff, x*y*4);
5645
5646 if (!stbi__pic_load_core(s,x,y,comp, result)) {
5647 STBI_FREE(result);
5648 result=0;
5649 }
5650 *px = x;
5651 *py = y;
5652 if (req_comp == 0) req_comp = *comp;
5653 result=stbi__convert_format(result,4,req_comp,x,y);
5654
5655 return result;
5656}
5657
5658static int stbi__pic_test(stbi__context *s)
5659{
5660 int r = stbi__pic_test_core(s);
5661 stbi__rewind(s);
5662 return r;
5663}
5664#endif
5665
5666#ifndef STBI_NO_GIF
5667typedef struct
5668{
5669 stbi__int16 prefix;
5670 stbi_uc first;
5671 stbi_uc suffix;
5672} stbi__gif_lzw;
5673
5674typedef struct
5675{
5676 int w,h;
5677 stbi_uc *out;
5678 stbi_uc *background;
5679 stbi_uc *history;
5680 int flags, bgindex, ratio, transparent, eflags;
5681 stbi_uc pal[256][4];
5682 stbi_uc lpal[256][4];
5683 stbi__gif_lzw codes[8192];
5684 stbi_uc *color_table;
5685 int parse, step;
5686 int lflags;
5687 int start_x, start_y;
5688 int max_x, max_y;
5689 int cur_x, cur_y;
5690 int line_size;
5691 int delay;
5692} stbi__gif;
5693
5694static int stbi__gif_test_raw(stbi__context *s)
5695{
5696 int sz;
5697 if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0;
5698 sz = stbi__get8(s);
5699 if (sz != '9' && sz != '7') return 0;
5700 if (stbi__get8(s) != 'a') return 0;
5701 return 1;
5702}
5703
5704static int stbi__gif_test(stbi__context *s)
5705{
5706 int r = stbi__gif_test_raw(s);
5707 stbi__rewind(s);
5708 return r;
5709}
5710
5711static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp)
5712{
5713 int i;
5714 for (i=0; i < num_entries; ++i) {
5715 pal[i][2] = stbi__get8(s);
5716 pal[i][1] = stbi__get8(s);
5717 pal[i][0] = stbi__get8(s);
5718 pal[i][3] = transp == i ? 0 : 255;
5719 }
5720}
5721
5722static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info)
5723{
5724 stbi_uc version;
5725 if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8')
5726 return stbi__err("not GIF", "Corrupt GIF");
5727
5728 version = stbi__get8(s);
5729 if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF");
5730 if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF");
5731
5732 stbi__g_failure_reason = "";
5733 g->w = stbi__get16le(s);
5734 g->h = stbi__get16le(s);
5735 g->flags = stbi__get8(s);
5736 g->bgindex = stbi__get8(s);
5737 g->ratio = stbi__get8(s);
5738 g->transparent = -1;
5739
5740 if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
5741 if (g->h > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
5742
5743 if (comp != 0) *comp = 4;
5744
5745 if (is_info) return 1;
5746
5747 if (g->flags & 0x80)
5748 stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
5749
5750 return 1;
5751}
5752
5753static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
5754{
5755 stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif));
5756 if (!g) return stbi__err("outofmem", "Out of memory");
5757 if (!stbi__gif_header(s, g, comp, 1)) {
5758 STBI_FREE(g);
5759 stbi__rewind( s );
5760 return 0;
5761 }
5762 if (x) *x = g->w;
5763 if (y) *y = g->h;
5764 STBI_FREE(g);
5765 return 1;
5766}
5767
5768static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
5769{
5770 stbi_uc *p, *c;
5771 int idx;
5772
5773 if (g->codes[code].prefix >= 0)
5774 stbi__out_gif_code(g, g->codes[code].prefix);
5775
5776 if (g->cur_y >= g->max_y) return;
5777
5778 idx = g->cur_x + g->cur_y;
5779 p = &g->out[idx];
5780 g->history[idx / 4] = 1;
5781
5782 c = &g->color_table[g->codes[code].suffix * 4];
5783 if (c[3] > 128) {
5784 p[0] = c[2];
5785 p[1] = c[1];
5786 p[2] = c[0];
5787 p[3] = c[3];
5788 }
5789 g->cur_x += 4;
5790
5791 if (g->cur_x >= g->max_x) {
5792 g->cur_x = g->start_x;
5793 g->cur_y += g->step;
5794
5795 while (g->cur_y >= g->max_y && g->parse > 0) {
5796 g->step = (1 << g->parse) * g->line_size;
5797 g->cur_y = g->start_y + (g->step >> 1);
5798 --g->parse;
5799 }
5800 }
5801}
5802
5803static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
5804{
5805 stbi_uc lzw_cs;
5806 stbi__int32 len, init_code;
5807 stbi__uint32 first;
5808 stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
5809 stbi__gif_lzw *p;
5810
5811 lzw_cs = stbi__get8(s);
5812 if (lzw_cs > 12) return NULL;
5813 clear = 1 << lzw_cs;
5814 first = 1;
5815 codesize = lzw_cs + 1;
5816 codemask = (1 << codesize) - 1;
5817 bits = 0;
5818 valid_bits = 0;
5819 for (init_code = 0; init_code < clear; init_code++) {
5820 g->codes[init_code].prefix = -1;
5821 g->codes[init_code].first = (stbi_uc) init_code;
5822 g->codes[init_code].suffix = (stbi_uc) init_code;
5823 }
5824
5825 avail = clear+2;
5826 oldcode = -1;
5827
5828 len = 0;
5829 for(;;) {
5830 if (valid_bits < codesize) {
5831 if (len == 0) {
5832 len = stbi__get8(s);
5833 if (len == 0)
5834 return g->out;
5835 }
5836 --len;
5837 bits |= (stbi__int32) stbi__get8(s) << valid_bits;
5838 valid_bits += 8;
5839 } else {
5840 stbi__int32 code = bits & codemask;
5841 bits >>= codesize;
5842 valid_bits -= codesize;
5843
5844 if (code == clear) {
5845 codesize = lzw_cs + 1;
5846 codemask = (1 << codesize) - 1;
5847 avail = clear + 2;
5848 oldcode = -1;
5849 first = 0;
5850 } else if (code == clear + 1) {
5851 stbi__skip(s, len);
5852 while ((len = stbi__get8(s)) > 0)
5853 stbi__skip(s,len);
5854 return g->out;
5855 } else if (code <= avail) {
5856 if (first) {
5857 return stbi__errpuc("no clear code", "Corrupt GIF");
5858 }
5859
5860 if (oldcode >= 0) {
5861 p = &g->codes[avail++];
5862 if (avail > 8192) {
5863 return stbi__errpuc("too many codes", "Corrupt GIF");
5864 }
5865
5866 p->prefix = (stbi__int16) oldcode;
5867 p->first = g->codes[oldcode].first;
5868 p->suffix = (code == avail) ? p->first : g->codes[code].first;
5869 } else if (code == avail)
5870 return stbi__errpuc("illegal code in raster", "Corrupt GIF");
5871
5872 stbi__out_gif_code(g, (stbi__uint16) code);
5873
5874 if ((avail & codemask) == 0 && avail <= 0x0FFF) {
5875 codesize++;
5876 codemask = (1 << codesize) - 1;
5877 }
5878
5879 oldcode = code;
5880 } else {
5881 return stbi__errpuc("illegal code in raster", "Corrupt GIF");
5882 }
5883 }
5884 }
5885}
5886
5887static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back)
5888{
5889 int dispose;
5890 int first_frame;
5891 int pi;
5892 int pcount;
5893 STBI_NOTUSED(req_comp);
5894
5895 first_frame = 0;
5896 if (g->out == 0) {
5897 if (!stbi__gif_header(s, g, comp,0)) return 0;
5898 if (!stbi__mad3sizes_valid(4, g->w, g->h, 0))
5899 return stbi__errpuc("too large", "GIF image is too large");
5900 pcount = g->w * g->h;
5901 g->out = (stbi_uc *) stbi__malloc(4 * pcount);
5902 g->background = (stbi_uc *) stbi__malloc(4 * pcount);
5903 g->history = (stbi_uc *) stbi__malloc(pcount);
5904 if (!g->out || !g->background || !g->history)
5905 return stbi__errpuc("outofmem", "Out of memory");
5906
5907 memset(g->out, 0x00, 4 * pcount);
5908 memset(g->background, 0x00, 4 * pcount);
5909 memset(g->history, 0x00, pcount);
5910 first_frame = 1;
5911 } else {
5912
5913 dispose = (g->eflags & 0x1C) >> 2;
5914 pcount = g->w * g->h;
5915
5916 if ((dispose == 3) && (two_back == 0)) {
5917 dispose = 2;
5918 }
5919
5920 if (dispose == 3) {
5921 for (pi = 0; pi < pcount; ++pi) {
5922 if (g->history[pi]) {
5923 memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 );
5924 }
5925 }
5926 } else if (dispose == 2) {
5927
5928 for (pi = 0; pi < pcount; ++pi) {
5929 if (g->history[pi]) {
5930 memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 );
5931 }
5932 }
5933 } else {
5934
5935 }
5936
5937 memcpy( g->background, g->out, 4 * g->w * g->h );
5938 }
5939
5940 memset( g->history, 0x00, g->w * g->h );
5941
5942 for (;;) {
5943 int tag = stbi__get8(s);
5944 switch (tag) {
5945 case 0x2C:
5946 {
5947 stbi__int32 x, y, w, h;
5948 stbi_uc *o;
5949
5950 x = stbi__get16le(s);
5951 y = stbi__get16le(s);
5952 w = stbi__get16le(s);
5953 h = stbi__get16le(s);
5954 if (((x + w) > (g->w)) || ((y + h) > (g->h)))
5955 return stbi__errpuc("bad Image Descriptor", "Corrupt GIF");
5956
5957 g->line_size = g->w * 4;
5958 g->start_x = x * 4;
5959 g->start_y = y * g->line_size;
5960 g->max_x = g->start_x + w * 4;
5961 g->max_y = g->start_y + h * g->line_size;
5962 g->cur_x = g->start_x;
5963 g->cur_y = g->start_y;
5964
5965 if (w == 0)
5966 g->cur_y = g->max_y;
5967
5968 g->lflags = stbi__get8(s);
5969
5970 if (g->lflags & 0x40) {
5971 g->step = 8 * g->line_size;
5972 g->parse = 3;
5973 } else {
5974 g->step = g->line_size;
5975 g->parse = 0;
5976 }
5977
5978 if (g->lflags & 0x80) {
5979 stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
5980 g->color_table = (stbi_uc *) g->lpal;
5981 } else if (g->flags & 0x80) {
5982 g->color_table = (stbi_uc *) g->pal;
5983 } else
5984 return stbi__errpuc("missing color table", "Corrupt GIF");
5985
5986 o = stbi__process_gif_raster(s, g);
5987 if (!o) return NULL;
5988
5989 pcount = g->w * g->h;
5990 if (first_frame && (g->bgindex > 0)) {
5991
5992 for (pi = 0; pi < pcount; ++pi) {
5993 if (g->history[pi] == 0) {
5994 g->pal[g->bgindex][3] = 255;
5995 memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 );
5996 }
5997 }
5998 }
5999
6000 return o;
6001 }
6002
6003 case 0x21:
6004 {
6005 int len;
6006 int ext = stbi__get8(s);
6007 if (ext == 0xF9) {
6008 len = stbi__get8(s);
6009 if (len == 4) {
6010 g->eflags = stbi__get8(s);
6011 g->delay = 10 * stbi__get16le(s);
6012
6013 if (g->transparent >= 0) {
6014 g->pal[g->transparent][3] = 255;
6015 }
6016 if (g->eflags & 0x01) {
6017 g->transparent = stbi__get8(s);
6018 if (g->transparent >= 0) {
6019 g->pal[g->transparent][3] = 0;
6020 }
6021 } else {
6022
6023 stbi__skip(s, 1);
6024 g->transparent = -1;
6025 }
6026 } else {
6027 stbi__skip(s, len);
6028 break;
6029 }
6030 }
6031 while ((len = stbi__get8(s)) != 0) {
6032 stbi__skip(s, len);
6033 }
6034 break;
6035 }
6036
6037 case 0x3B:
6038 return (stbi_uc *) s;
6039
6040 default:
6041 return stbi__errpuc("unknown code", "Corrupt GIF");
6042 }
6043 }
6044}
6045
6046static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **delays)
6047{
6048 STBI_FREE(g->out);
6049 STBI_FREE(g->history);
6050 STBI_FREE(g->background);
6051
6052 if (out) STBI_FREE(out);
6053 if (delays && *delays) STBI_FREE(*delays);
6054 return stbi__errpuc("outofmem", "Out of memory");
6055}
6056
6057static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
6058{
6059 if (stbi__gif_test(s)) {
6060 int layers = 0;
6061 stbi_uc *u = 0;
6062 stbi_uc *out = 0;
6063 stbi_uc *two_back = 0;
6064 stbi__gif g;
6065 int stride;
6066 int out_size = 0;
6067 int delays_size = 0;
6068
6069 STBI_NOTUSED(out_size);
6070 STBI_NOTUSED(delays_size);
6071
6072 memset(&g, 0, sizeof(g));
6073 if (delays) {
6074 *delays = 0;
6075 }
6076
6077 do {
6078 u = stbi__gif_load_next(s, &g, comp, req_comp, two_back);
6079 if (u == (stbi_uc *) s) u = 0;
6080
6081 if (u) {
6082 *x = g.w;
6083 *y = g.h;
6084 ++layers;
6085 stride = g.w * g.h * 4;
6086
6087 if (out) {
6088 void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
6089 if (!tmp)
6090 return stbi__load_gif_main_outofmem(&g, out, delays);
6091 else {
6092 out = (stbi_uc*) tmp;
6093 out_size = layers * stride;
6094 }
6095
6096 if (delays) {
6097 int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
6098 if (!new_delays)
6099 return stbi__load_gif_main_outofmem(&g, out, delays);
6100 *delays = new_delays;
6101 delays_size = layers * sizeof(int);
6102 }
6103 } else {
6104 out = (stbi_uc*)stbi__malloc( layers * stride );
6105 if (!out)
6106 return stbi__load_gif_main_outofmem(&g, out, delays);
6107 out_size = layers * stride;
6108 if (delays) {
6109 *delays = (int*) stbi__malloc( layers * sizeof(int) );
6110 if (!*delays)
6111 return stbi__load_gif_main_outofmem(&g, out, delays);
6112 delays_size = layers * sizeof(int);
6113 }
6114 }
6115 memcpy( out + ((layers - 1) * stride), u, stride );
6116 if (layers >= 2) {
6117 two_back = out - 2 * stride;
6118 }
6119
6120 if (delays) {
6121 (*delays)[layers - 1U] = g.delay;
6122 }
6123 }
6124 } while (u != 0);
6125
6126 STBI_FREE(g.out);
6127 STBI_FREE(g.history);
6128 STBI_FREE(g.background);
6129
6130 if (req_comp && req_comp != 4)
6131 out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h);
6132
6133 *z = layers;
6134 return out;
6135 } else {
6136 return stbi__errpuc("not GIF", "Image was not as a gif type.");
6137 }
6138}
6139
6140static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
6141{
6142 stbi_uc *u = 0;
6143 stbi__gif g;
6144 memset(&g, 0, sizeof(g));
6145 STBI_NOTUSED(ri);
6146
6147 u = stbi__gif_load_next(s, &g, comp, req_comp, 0);
6148 if (u == (stbi_uc *) s) u = 0;
6149 if (u) {
6150 *x = g.w;
6151 *y = g.h;
6152
6153 if (req_comp && req_comp != 4)
6154 u = stbi__convert_format(u, 4, req_comp, g.w, g.h);
6155 } else if (g.out) {
6156
6157 STBI_FREE(g.out);
6158 }
6159
6160 STBI_FREE(g.history);
6161 STBI_FREE(g.background);
6162
6163 return u;
6164}
6165
6166static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp)
6167{
6168 return stbi__gif_info_raw(s,x,y,comp);
6169}
6170#endif
6171
6172#ifndef STBI_NO_HDR
6173static int stbi__hdr_test_core(stbi__context *s, const char *signature)
6174{
6175 int i;
6176 for (i=0; signature[i]; ++i)
6177 if (stbi__get8(s) != signature[i])
6178 return 0;
6179 stbi__rewind(s);
6180 return 1;
6181}
6182
6183static int stbi__hdr_test(stbi__context* s)
6184{
6185 int r = stbi__hdr_test_core(s, "#?RADIANCE\n");
6186 stbi__rewind(s);
6187 if(!r) {
6188 r = stbi__hdr_test_core(s, "#?RGBE\n");
6189 stbi__rewind(s);
6190 }
6191 return r;
6192}
6193
6194#define STBI__HDR_BUFLEN 1024
6195static char *stbi__hdr_gettoken(stbi__context *z, char *buffer)
6196{
6197 int len=0;
6198 char c = '\0';
6199
6200 c = (char) stbi__get8(z);
6201
6202 while (!stbi__at_eof(z) && c != '\n') {
6203 buffer[len++] = c;
6204 if (len == STBI__HDR_BUFLEN-1) {
6205
6206 while (!stbi__at_eof(z) && stbi__get8(z) != '\n')
6207 ;
6208 break;
6209 }
6210 c = (char) stbi__get8(z);
6211 }
6212
6213 buffer[len] = 0;
6214 return buffer;
6215}
6216
6217static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp)
6218{
6219 if ( input[3] != 0 ) {
6220 float f1;
6221
6222 f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
6223 if (req_comp <= 2)
6224 output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
6225 else {
6226 output[0] = input[0] * f1;
6227 output[1] = input[1] * f1;
6228 output[2] = input[2] * f1;
6229 }
6230 if (req_comp == 2) output[1] = 1;
6231 if (req_comp == 4) output[3] = 1;
6232 } else {
6233 switch (req_comp) {
6234 case 4: output[3] = 1;
6235 case 3: output[0] = output[1] = output[2] = 0;
6236 break;
6237 case 2: output[1] = 1;
6238 case 1: output[0] = 0;
6239 break;
6240 }
6241 }
6242}
6243
6244static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
6245{
6246 char buffer[STBI__HDR_BUFLEN];
6247 char *token;
6248 int valid = 0;
6249 int width, height;
6250 stbi_uc *scanline;
6251 float *hdr_data;
6252 int len;
6253 unsigned char count, value;
6254 int i, j, k, c1,c2, z;
6255 const char *headerToken;
6256 STBI_NOTUSED(ri);
6257
6258 headerToken = stbi__hdr_gettoken(s,buffer);
6259 if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0)
6260 return stbi__errpf("not HDR", "Corrupt HDR image");
6261
6262 for(;;) {
6263 token = stbi__hdr_gettoken(s,buffer);
6264 if (token[0] == 0) break;
6265 if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
6266 }
6267
6268 if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format");
6269
6270 token = stbi__hdr_gettoken(s,buffer);
6271 if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
6272 token += 3;
6273 height = (int) strtol(token, &token, 10);
6274 while (*token == ' ') ++token;
6275 if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
6276 token += 3;
6277 width = (int) strtol(token, NULL, 10);
6278
6279 if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)");
6280 if (width > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)");
6281
6282 *x = width;
6283 *y = height;
6284
6285 if (comp) *comp = 3;
6286 if (req_comp == 0) req_comp = 3;
6287
6288 if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0))
6289 return stbi__errpf("too large", "HDR image is too large");
6290
6291 hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0);
6292 if (!hdr_data)
6293 return stbi__errpf("outofmem", "Out of memory");
6294
6295 if ( width < 8 || width >= 32768) {
6296
6297 for (j=0; j < height; ++j) {
6298 for (i=0; i < width; ++i) {
6299 stbi_uc rgbe[4];
6300 main_decode_loop:
6301 stbi__getn(s, rgbe, 4);
6302 stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
6303 }
6304 }
6305 } else {
6306
6307 scanline = NULL;
6308
6309 for (j = 0; j < height; ++j) {
6310 c1 = stbi__get8(s);
6311 c2 = stbi__get8(s);
6312 len = stbi__get8(s);
6313 if (c1 != 2 || c2 != 2 || (len & 0x80)) {
6314
6315 stbi_uc rgbe[4];
6316 rgbe[0] = (stbi_uc) c1;
6317 rgbe[1] = (stbi_uc) c2;
6318 rgbe[2] = (stbi_uc) len;
6319 rgbe[3] = (stbi_uc) stbi__get8(s);
6320 stbi__hdr_convert(hdr_data, rgbe, req_comp);
6321 i = 1;
6322 j = 0;
6323 STBI_FREE(scanline);
6324 goto main_decode_loop;
6325 }
6326 len <<= 8;
6327 len |= stbi__get8(s);
6328 if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
6329 if (scanline == NULL) {
6330 scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0);
6331 if (!scanline) {
6332 STBI_FREE(hdr_data);
6333 return stbi__errpf("outofmem", "Out of memory");
6334 }
6335 }
6336
6337 for (k = 0; k < 4; ++k) {
6338 int nleft;
6339 i = 0;
6340 while ((nleft = width - i) > 0) {
6341 count = stbi__get8(s);
6342 if (count > 128) {
6343
6344 value = stbi__get8(s);
6345 count -= 128;
6346 if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
6347 for (z = 0; z < count; ++z)
6348 scanline[i++ * 4 + k] = value;
6349 } else {
6350
6351 if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
6352 for (z = 0; z < count; ++z)
6353 scanline[i++ * 4 + k] = stbi__get8(s);
6354 }
6355 }
6356 }
6357 for (i=0; i < width; ++i)
6358 stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
6359 }
6360 if (scanline)
6361 STBI_FREE(scanline);
6362 }
6363
6364 return hdr_data;
6365}
6366
6367static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp)
6368{
6369 char buffer[STBI__HDR_BUFLEN];
6370 char *token;
6371 int valid = 0;
6372 int dummy;
6373
6374 if (!x) x = &dummy;
6375 if (!y) y = &dummy;
6376 if (!comp) comp = &dummy;
6377
6378 if (stbi__hdr_test(s) == 0) {
6379 stbi__rewind( s );
6380 return 0;
6381 }
6382
6383 for(;;) {
6384 token = stbi__hdr_gettoken(s,buffer);
6385 if (token[0] == 0) break;
6386 if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
6387 }
6388
6389 if (!valid) {
6390 stbi__rewind( s );
6391 return 0;
6392 }
6393 token = stbi__hdr_gettoken(s,buffer);
6394 if (strncmp(token, "-Y ", 3)) {
6395 stbi__rewind( s );
6396 return 0;
6397 }
6398 token += 3;
6399 *y = (int) strtol(token, &token, 10);
6400 while (*token == ' ') ++token;
6401 if (strncmp(token, "+X ", 3)) {
6402 stbi__rewind( s );
6403 return 0;
6404 }
6405 token += 3;
6406 *x = (int) strtol(token, NULL, 10);
6407 *comp = 3;
6408 return 1;
6409}
6410#endif
6411
6412#ifndef STBI_NO_BMP
6413static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
6414{
6415 void *p;
6416 stbi__bmp_data info;
6417
6418 info.all_a = 255;
6419 p = stbi__bmp_parse_header(s, &info);
6420 if (p == NULL) {
6421 stbi__rewind( s );
6422 return 0;
6423 }
6424 if (x) *x = s->img_x;
6425 if (y) *y = s->img_y;
6426 if (comp) {
6427 if (info.bpp == 24 && info.ma == 0xff000000)
6428 *comp = 3;
6429 else
6430 *comp = info.ma ? 4 : 3;
6431 }
6432 return 1;
6433}
6434#endif
6435
6436#ifndef STBI_NO_PSD
6437static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
6438{
6439 int channelCount, dummy, depth;
6440 if (!x) x = &dummy;
6441 if (!y) y = &dummy;
6442 if (!comp) comp = &dummy;
6443 if (stbi__get32be(s) != 0x38425053) {
6444 stbi__rewind( s );
6445 return 0;
6446 }
6447 if (stbi__get16be(s) != 1) {
6448 stbi__rewind( s );
6449 return 0;
6450 }
6451 stbi__skip(s, 6);
6452 channelCount = stbi__get16be(s);
6453 if (channelCount < 0 || channelCount > 16) {
6454 stbi__rewind( s );
6455 return 0;
6456 }
6457 *y = stbi__get32be(s);
6458 *x = stbi__get32be(s);
6459 depth = stbi__get16be(s);
6460 if (depth != 8 && depth != 16) {
6461 stbi__rewind( s );
6462 return 0;
6463 }
6464 if (stbi__get16be(s) != 3) {
6465 stbi__rewind( s );
6466 return 0;
6467 }
6468 *comp = 4;
6469 return 1;
6470}
6471
6472static int stbi__psd_is16(stbi__context *s)
6473{
6474 int channelCount, depth;
6475 if (stbi__get32be(s) != 0x38425053) {
6476 stbi__rewind( s );
6477 return 0;
6478 }
6479 if (stbi__get16be(s) != 1) {
6480 stbi__rewind( s );
6481 return 0;
6482 }
6483 stbi__skip(s, 6);
6484 channelCount = stbi__get16be(s);
6485 if (channelCount < 0 || channelCount > 16) {
6486 stbi__rewind( s );
6487 return 0;
6488 }
6489 STBI_NOTUSED(stbi__get32be(s));
6490 STBI_NOTUSED(stbi__get32be(s));
6491 depth = stbi__get16be(s);
6492 if (depth != 16) {
6493 stbi__rewind( s );
6494 return 0;
6495 }
6496 return 1;
6497}
6498#endif
6499
6500#ifndef STBI_NO_PIC
6501static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
6502{
6503 int act_comp=0,num_packets=0,chained,dummy;
6504 stbi__pic_packet packets[10];
6505
6506 if (!x) x = &dummy;
6507 if (!y) y = &dummy;
6508 if (!comp) comp = &dummy;
6509
6510 if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) {
6511 stbi__rewind(s);
6512 return 0;
6513 }
6514
6515 stbi__skip(s, 88);
6516
6517 *x = stbi__get16be(s);
6518 *y = stbi__get16be(s);
6519 if (stbi__at_eof(s)) {
6520 stbi__rewind( s);
6521 return 0;
6522 }
6523 if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
6524 stbi__rewind( s );
6525 return 0;
6526 }
6527
6528 stbi__skip(s, 8);
6529
6530 do {
6531 stbi__pic_packet *packet;
6532
6533 if (num_packets==sizeof(packets)/sizeof(packets[0]))
6534 return 0;
6535
6536 packet = &packets[num_packets++];
6537 chained = stbi__get8(s);
6538 packet->size = stbi__get8(s);
6539 packet->type = stbi__get8(s);
6540 packet->channel = stbi__get8(s);
6541 act_comp |= packet->channel;
6542
6543 if (stbi__at_eof(s)) {
6544 stbi__rewind( s );
6545 return 0;
6546 }
6547 if (packet->size != 8) {
6548 stbi__rewind( s );
6549 return 0;
6550 }
6551 } while (chained);
6552
6553 *comp = (act_comp & 0x10 ? 4 : 3);
6554
6555 return 1;
6556}
6557#endif
6558
6559#ifndef STBI_NO_PNM
6560
6561static int stbi__pnm_test(stbi__context *s)
6562{
6563 char p, t;
6564 p = (char) stbi__get8(s);
6565 t = (char) stbi__get8(s);
6566 if (p != 'P' || (t != '5' && t != '6')) {
6567 stbi__rewind( s );
6568 return 0;
6569 }
6570 return 1;
6571}
6572
6573static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
6574{
6575 stbi_uc *out;
6576 STBI_NOTUSED(ri);
6577
6578 ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n);
6579 if (ri->bits_per_channel == 0)
6580 return 0;
6581
6582 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
6583 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
6584
6585 *x = s->img_x;
6586 *y = s->img_y;
6587 if (comp) *comp = s->img_n;
6588
6589 if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0))
6590 return stbi__errpuc("too large", "PNM too large");
6591
6592 out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0);
6593 if (!out) return stbi__errpuc("outofmem", "Out of memory");
6594 if (!stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8))) {
6595 STBI_FREE(out);
6596 return stbi__errpuc("bad PNM", "PNM file truncated");
6597 }
6598
6599 if (req_comp && req_comp != s->img_n) {
6600 if (ri->bits_per_channel == 16) {
6601 out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, s->img_n, req_comp, s->img_x, s->img_y);
6602 } else {
6603 out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
6604 }
6605 if (out == NULL) return out;
6606 }
6607 return out;
6608}
6609
6610static int stbi__pnm_isspace(char c)
6611{
6612 return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
6613}
6614
6615static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
6616{
6617 for (;;) {
6618 while (!stbi__at_eof(s) && stbi__pnm_isspace(*c))
6619 *c = (char) stbi__get8(s);
6620
6621 if (stbi__at_eof(s) || *c != '#')
6622 break;
6623
6624 while (!stbi__at_eof(s) && *c != '\n' && *c != '\r' )
6625 *c = (char) stbi__get8(s);
6626 }
6627}
6628
6629static int stbi__pnm_isdigit(char c)
6630{
6631 return c >= '0' && c <= '9';
6632}
6633
6634static int stbi__pnm_getinteger(stbi__context *s, char *c)
6635{
6636 int value = 0;
6637
6638 while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) {
6639 value = value*10 + (*c - '0');
6640 *c = (char) stbi__get8(s);
6641 if((value > 214748364) || (value == 214748364 && *c > '7'))
6642 return stbi__err("integer parse overflow", "Parsing an integer in the PPM header overflowed a 32-bit int");
6643 }
6644
6645 return value;
6646}
6647
6648static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
6649{
6650 int maxv, dummy;
6651 char c, p, t;
6652
6653 if (!x) x = &dummy;
6654 if (!y) y = &dummy;
6655 if (!comp) comp = &dummy;
6656
6657 stbi__rewind(s);
6658
6659 p = (char) stbi__get8(s);
6660 t = (char) stbi__get8(s);
6661 if (p != 'P' || (t != '5' && t != '6')) {
6662 stbi__rewind(s);
6663 return 0;
6664 }
6665
6666 *comp = (t == '6') ? 3 : 1;
6667
6668 c = (char) stbi__get8(s);
6669 stbi__pnm_skip_whitespace(s, &c);
6670
6671 *x = stbi__pnm_getinteger(s, &c);
6672 if(*x == 0)
6673 return stbi__err("invalid width", "PPM image header had zero or overflowing width");
6674 stbi__pnm_skip_whitespace(s, &c);
6675
6676 *y = stbi__pnm_getinteger(s, &c);
6677 if (*y == 0)
6678 return stbi__err("invalid width", "PPM image header had zero or overflowing width");
6679 stbi__pnm_skip_whitespace(s, &c);
6680
6681 maxv = stbi__pnm_getinteger(s, &c);
6682 if (maxv > 65535)
6683 return stbi__err("max value > 65535", "PPM image supports only 8-bit and 16-bit images");
6684 else if (maxv > 255)
6685 return 16;
6686 else
6687 return 8;
6688}
6689
6690static int stbi__pnm_is16(stbi__context *s)
6691{
6692 if (stbi__pnm_info(s, NULL, NULL, NULL) == 16)
6693 return 1;
6694 return 0;
6695}
6696#endif
6697
6698static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)
6699{
6700 #ifndef STBI_NO_JPEG
6701 if (stbi__jpeg_info(s, x, y, comp)) return 1;
6702 #endif
6703
6704 #ifndef STBI_NO_PNG
6705 if (stbi__png_info(s, x, y, comp)) return 1;
6706 #endif
6707
6708 #ifndef STBI_NO_GIF
6709 if (stbi__gif_info(s, x, y, comp)) return 1;
6710 #endif
6711
6712 #ifndef STBI_NO_BMP
6713 if (stbi__bmp_info(s, x, y, comp)) return 1;
6714 #endif
6715
6716 #ifndef STBI_NO_PSD
6717 if (stbi__psd_info(s, x, y, comp)) return 1;
6718 #endif
6719
6720 #ifndef STBI_NO_PIC
6721 if (stbi__pic_info(s, x, y, comp)) return 1;
6722 #endif
6723
6724 #ifndef STBI_NO_PNM
6725 if (stbi__pnm_info(s, x, y, comp)) return 1;
6726 #endif
6727
6728 #ifndef STBI_NO_HDR
6729 if (stbi__hdr_info(s, x, y, comp)) return 1;
6730 #endif
6731
6732 #ifndef STBI_NO_TGA
6733 if (stbi__tga_info(s, x, y, comp))
6734 return 1;
6735 #endif
6736 return stbi__err("unknown image type", "Image not of any known type, or corrupt");
6737}
6738
6739static int stbi__is_16_main(stbi__context *s)
6740{
6741 #ifndef STBI_NO_PNG
6742 if (stbi__png_is16(s)) return 1;
6743 #endif
6744
6745 #ifndef STBI_NO_PSD
6746 if (stbi__psd_is16(s)) return 1;
6747 #endif
6748
6749 #ifndef STBI_NO_PNM
6750 if (stbi__pnm_is16(s)) return 1;
6751 #endif
6752 return 0;
6753}
6754
6755#ifndef STBI_NO_STDIO
6756STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp)
6757{
6758 FILE *f = stbi__fopen(filename, "rb");
6759 int result;
6760 if (!f) return stbi__err("can't fopen", "Unable to open file");
6761 result = stbi_info_from_file(f, x, y, comp);
6762 fclose(f);
6763 return result;
6764}
6765
6766STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
6767{
6768 int r;
6769 stbi__context s;
6770 long pos = ftell(f);
6771 stbi__start_file(&s, f);
6772 r = stbi__info_main(&s,x,y,comp);
6773 fseek(f,pos,SEEK_SET);
6774 return r;
6775}
6776
6777STBIDEF int stbi_is_16_bit(char const *filename)
6778{
6779 FILE *f = stbi__fopen(filename, "rb");
6780 int result;
6781 if (!f) return stbi__err("can't fopen", "Unable to open file");
6782 result = stbi_is_16_bit_from_file(f);
6783 fclose(f);
6784 return result;
6785}
6786
6787STBIDEF int stbi_is_16_bit_from_file(FILE *f)
6788{
6789 int r;
6790 stbi__context s;
6791 long pos = ftell(f);
6792 stbi__start_file(&s, f);
6793 r = stbi__is_16_main(&s);
6794 fseek(f,pos,SEEK_SET);
6795 return r;
6796}
6797#endif
6798
6799STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
6800{
6801 stbi__context s;
6802 stbi__start_mem(&s,buffer,len);
6803 return stbi__info_main(&s,x,y,comp);
6804}
6805
6806STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
6807{
6808 stbi__context s;
6809 stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
6810 return stbi__info_main(&s,x,y,comp);
6811}
6812
6813STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len)
6814{
6815 stbi__context s;
6816 stbi__start_mem(&s,buffer,len);
6817 return stbi__is_16_main(&s);
6818}
6819
6820STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user)
6821{
6822 stbi__context s;
6823 stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
6824 return stbi__is_16_main(&s);
6825}
6826
6827#endif