76 #ifndef STBI_INCLUDE_STB_IMAGE_H
77 #define STBI_INCLUDE_STB_IMAGE_H
193 #ifndef STBI_NO_STDIO
195 #endif // STBI_NO_STDIO
197 #define STBI_VERSION 1
215 #ifdef STB_IMAGE_STATIC
216 #define STBIDEF static
218 #define STBIDEF extern
232 #ifndef STBI_NO_STDIO
250 #ifndef STBI_NO_STDIO
262 #endif // STBI_NO_HDR
267 #ifndef STBI_NO_STDIO
270 #endif // STBI_NO_STDIO
284 #ifndef STBI_NO_STDIO
315 typedef void (*stbi_idct_8x8)(
stbi_uc *out,
int out_stride,
short data[64],
unsigned short *dequantize);
328 STBIDEF void stbi_install_idct(stbi_idct_8x8 func);
329 STBIDEF void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
340 #endif // STBI_INCLUDE_STB_IMAGE_H
342 #ifdef STB_IMAGE_IMPLEMENTATION
349 #ifndef STBI_NO_STDIO
356 #define STBI_ASSERT(x) assert(x)
363 #define stbi_inline inline
368 #define stbi_inline __forceinline
373 typedef unsigned short stbi__uint16;
374 typedef signed short stbi__int16;
375 typedef unsigned int stbi__uint32;
376 typedef signed int stbi__int32;
379 typedef uint16_t stbi__uint16;
380 typedef int16_t stbi__int16;
381 typedef uint32_t stbi__uint32;
382 typedef int32_t stbi__int32;
386 typedef unsigned char validate_uint32[
sizeof(stbi__uint32)==4 ? 1 : -1];
389 #define STBI_NOTUSED(v) (void)(v)
391 #define STBI_NOTUSED(v) (void)sizeof(v)
395 #define STBI_HAS_LROTL
398 #ifdef STBI_HAS_LROTL
399 #define stbi_lrot(x,y) _lrotl(x,y)
401 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
412 stbi__uint32 img_x, img_y;
413 int img_n, img_out_n;
418 int read_from_callbacks;
422 stbi_uc *img_buffer, *img_buffer_end;
427 static void stbi__refill_buffer(stbi__context *s);
430 static void stbi__start_mem(stbi__context *s,
stbi_uc const *
buffer,
int len)
433 s->read_from_callbacks = 0;
434 s->img_buffer = s->img_buffer_original = (
stbi_uc *) buffer;
435 s->img_buffer_end = (
stbi_uc *) buffer+len;
439 static void stbi__start_callbacks(stbi__context *s,
stbi_io_callbacks *c,
void *user)
442 s->io_user_data = user;
443 s->buflen =
sizeof(s->buffer_start);
444 s->read_from_callbacks = 1;
445 s->img_buffer_original = s->buffer_start;
446 stbi__refill_buffer(s);
449 #ifndef STBI_NO_STDIO
451 static int stbi__stdio_read(
void *user,
char *
data,
int size)
453 return (
int) fread(data,1,size,(FILE*) user);
456 static void stbi__stdio_skip(
void *user,
int n)
458 fseek((FILE*) user, n, SEEK_CUR);
461 static int stbi__stdio_eof(
void *user)
463 return feof((FILE*) user);
473 static void stbi__start_file(stbi__context *s, FILE *
f)
475 stbi__start_callbacks(s, &stbi__stdio_callbacks, (
void *) f);
480 #endif // !STBI_NO_STDIO
482 static void stbi__rewind(stbi__context *s)
487 s->img_buffer = s->img_buffer_original;
490 static int stbi__jpeg_test(stbi__context *s);
491 static stbi_uc *stbi__jpeg_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
492 static int stbi__jpeg_info(stbi__context *s,
int *
x,
int *
y,
int *comp);
493 static int stbi__png_test(stbi__context *s);
494 static stbi_uc *stbi__png_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
495 static int stbi__png_info(stbi__context *s,
int *
x,
int *
y,
int *comp);
496 static int stbi__bmp_test(stbi__context *s);
497 static stbi_uc *stbi__bmp_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
498 static int stbi__tga_test(stbi__context *s);
499 static stbi_uc *stbi__tga_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
500 static int stbi__tga_info(stbi__context *s,
int *
x,
int *
y,
int *comp);
501 static int stbi__psd_test(stbi__context *s);
502 static stbi_uc *stbi__psd_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
504 static int stbi__hdr_test(stbi__context *s);
505 static float *stbi__hdr_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
507 static int stbi__pic_test(stbi__context *s);
508 static stbi_uc *stbi__pic_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
509 static int stbi__gif_test(stbi__context *s);
510 static stbi_uc *stbi__gif_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
511 static int stbi__gif_info(stbi__context *s,
int *
x,
int *
y,
int *comp);
515 static const char *stbi__g_failure_reason;
519 return stbi__g_failure_reason;
522 static int stbi__err(
const char *str)
524 stbi__g_failure_reason = str;
532 #ifdef STBI_NO_FAILURE_STRINGS
533 #define stbi__err(x,y) 0
534 #elif defined(STBI_FAILURE_USERMSG)
535 #define stbi__err(x,y) stbi__err(y)
537 #define stbi__err(x,y) stbi__err(x)
540 #define stbi__errpf(x,y) ((float *) (stbi__err(x,y)?NULL:NULL))
541 #define stbi__errpuc(x,y) ((unsigned char *) (stbi__err(x,y)?NULL:NULL))
545 free(retval_from_stbi_load);
549 static float *stbi__ldr_to_hdr(
stbi_uc *data,
int x,
int y,
int comp);
550 static stbi_uc *stbi__hdr_to_ldr(
float *data,
int x,
int y,
int comp);
553 static unsigned char *stbi_load_main(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp)
555 if (stbi__jpeg_test(s))
return stbi__jpeg_load(s,x,y,comp,req_comp);
556 if (stbi__png_test(s))
return stbi__png_load(s,x,y,comp,req_comp);
557 if (stbi__bmp_test(s))
return stbi__bmp_load(s,x,y,comp,req_comp);
558 if (stbi__gif_test(s))
return stbi__gif_load(s,x,y,comp,req_comp);
559 if (stbi__psd_test(s))
return stbi__psd_load(s,x,y,comp,req_comp);
560 if (stbi__pic_test(s))
return stbi__pic_load(s,x,y,comp,req_comp);
563 if (stbi__hdr_test(s)) {
564 float *hdr = stbi__hdr_load(s, x,y,comp,req_comp);
565 return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
570 if (stbi__tga_test(s))
571 return stbi__tga_load(s,x,y,comp,req_comp);
572 return stbi__errpuc(
"unknown image type",
"Image not of any known type, or corrupt");
575 #ifndef STBI_NO_STDIO
577 FILE *stbi__fopen(
char const *filename,
char const *
mode)
581 if (0 != fopen_s(&f, filename,
"rb"))
584 f = fopen(filename,
"rb");
590 STBIDEF unsigned char *
stbi_load(
char const *filename,
int *x,
int *y,
int *comp,
int req_comp)
592 FILE *f = stbi__fopen(filename,
"rb");
593 unsigned char *result;
594 if (!f)
return stbi__errpuc(
"can't fopen",
"Unable to open file");
602 unsigned char *result;
604 stbi__start_file(&s,f);
605 result = stbi_load_main(&s,x,y,comp,req_comp);
608 fseek(f, - (
int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
617 stbi__start_mem(&s,buffer,len);
618 return stbi_load_main(&s,x,y,comp,req_comp);
625 return stbi_load_main(&s,x,y,comp,req_comp);
630 float *stbi_loadf_main(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
634 if (stbi__hdr_test(s))
635 return stbi__hdr_load(s,x,y,comp,req_comp);
637 data = stbi_load_main(s, x, y, comp, req_comp);
639 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
640 return stbi__errpf(
"unknown image type",
"Image not of any known type, or corrupt");
646 stbi__start_mem(&s,buffer,len);
647 return stbi_loadf_main(&s,x,y,comp,req_comp);
654 return stbi_loadf_main(&s,x,y,comp,req_comp);
657 #ifndef STBI_NO_STDIO
658 float *
stbi_loadf(
char const *filename,
int *x,
int *y,
int *comp,
int req_comp)
661 FILE *f = stbi__fopen(filename,
"rb");
662 if (!f)
return stbi__errpf(
"can't fopen",
"Unable to open file");
671 stbi__start_file(&s,f);
672 return stbi_loadf_main(&s,x,y,comp,req_comp);
674 #endif // !STBI_NO_STDIO
676 #endif // !STBI_NO_HDR
686 stbi__start_mem(&s,buffer,len);
687 return stbi__hdr_test(&s);
689 STBI_NOTUSED(buffer);
695 #ifndef STBI_NO_STDIO
698 FILE *f = stbi__fopen(filename,
"rb");
711 stbi__start_file(&s,f);
712 return stbi__hdr_test(&s);
717 #endif // !STBI_NO_STDIO
724 return stbi__hdr_test(&s);
731 static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;
732 static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;
754 static void stbi__refill_buffer(stbi__context *s)
756 int n = (s->io.read)(s->io_user_data,(
char*)s->buffer_start,s->buflen);
760 s->read_from_callbacks = 0;
761 s->img_buffer = s->buffer_start;
762 s->img_buffer_end = s->buffer_start+1;
765 s->img_buffer = s->buffer_start;
766 s->img_buffer_end = s->buffer_start +
n;
770 stbi_inline
static stbi_uc stbi__get8(stbi__context *s)
772 if (s->img_buffer < s->img_buffer_end)
773 return *s->img_buffer++;
774 if (s->read_from_callbacks) {
775 stbi__refill_buffer(s);
776 return *s->img_buffer++;
781 stbi_inline
static int stbi__at_eof(stbi__context *s)
784 if (!(s->io.eof)(s->io_user_data))
return 0;
787 if (s->read_from_callbacks == 0)
return 1;
790 return s->img_buffer >= s->img_buffer_end;
793 static void stbi__skip(stbi__context *s,
int n)
796 int blen = (
int) (s->img_buffer_end - s->img_buffer);
798 s->img_buffer = s->img_buffer_end;
799 (s->io.skip)(s->io_user_data, n - blen);
806 static int stbi__getn(stbi__context *s,
stbi_uc *
buffer,
int n)
809 int blen = (
int) (s->img_buffer_end - s->img_buffer);
813 memcpy(buffer, s->img_buffer, blen);
815 count = (s->io.read)(s->io_user_data, (
char*) buffer + blen, n - blen);
816 res = (count == (n-blen));
817 s->img_buffer = s->img_buffer_end;
822 if (s->img_buffer+
n <= s->img_buffer_end) {
823 memcpy(buffer, s->img_buffer, n);
830 static int stbi__get16be(stbi__context *s)
832 int z = stbi__get8(s);
833 return (z << 8) + stbi__get8(s);
836 static stbi__uint32 stbi__get32be(stbi__context *s)
838 stbi__uint32 z = stbi__get16be(s);
839 return (z << 16) + stbi__get16be(s);
842 static int stbi__get16le(stbi__context *s)
844 int z = stbi__get8(s);
845 return z + (stbi__get8(s) << 8);
848 static stbi__uint32 stbi__get32le(stbi__context *s)
850 stbi__uint32 z = stbi__get16le(s);
851 return z + (stbi__get16le(s) << 16);
865 static stbi_uc stbi__compute_y(
int r,
int g,
int b)
867 return (
stbi_uc) (((r*77) + (g*150) + (29*
b)) >> 8);
870 static unsigned char *stbi__convert_format(
unsigned char *data,
int img_n,
int req_comp,
unsigned int x,
unsigned int y)
875 if (req_comp == img_n)
return data;
876 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
878 good = (
unsigned char *) malloc(req_comp * x * y);
881 return stbi__errpuc(
"outofmem",
"Out of memory");
884 for (j=0; j < (
int) y; ++j) {
885 unsigned char *
src = data + j * x * img_n ;
886 unsigned char *dest = good + j * x * req_comp;
888 #define COMBO(a,b) ((a)*8+(b))
889 #define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
892 switch (COMBO(img_n, req_comp)) {
893 CASE(1,2) dest[0]=src[0], dest[1]=255; break;
894 CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;
895 CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;
896 CASE(2,1) dest[0]=src[0]; break;
897 CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;
898 CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;
899 CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;
900 CASE(3,1) dest[0]=stbi__compute_y(src[0],src[1],src[2]); break;
901 CASE(3,2) dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
902 CASE(4,1) dest[0]=stbi__compute_y(src[0],src[1],src[2]); break;
903 CASE(4,2) dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;
904 CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;
905 default: STBI_ASSERT(0);
915 static float *stbi__ldr_to_hdr(
stbi_uc *data,
int x,
int y,
int comp)
918 float *output = (
float *) malloc(x * y * comp *
sizeof(
float));
919 if (output == NULL) { free(data);
return stbi__errpf(
"outofmem",
"Out of memory"); }
921 if (comp & 1) n = comp;
else n = comp-1;
922 for (i=0; i < x*
y; ++i) {
923 for (k=0; k <
n; ++k) {
924 output[i*comp + k] = (
float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
926 if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
932 #define stbi__float2int(x) ((int) (x))
933 static stbi_uc *stbi__hdr_to_ldr(
float *data,
int x,
int y,
int comp)
937 if (output == NULL) { free(data);
return stbi__errpuc(
"outofmem",
"Out of memory"); }
939 if (comp & 1) n = comp;
else n = comp-1;
940 for (i=0; i < x*
y; ++i) {
941 for (k=0; k <
n; ++k) {
942 float z = (
float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;
944 if (z > 255) z = 255;
945 output[i*comp + k] = (
stbi_uc) stbi__float2int(z);
948 float z = data[i*comp+k] * 255 + 0.5f;
950 if (z > 255) z = 255;
951 output[i*comp + k] = (
stbi_uc) stbi__float2int(z);
987 #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
993 stbi__uint16 code[256];
996 unsigned int maxcode[18];
1003 unsigned short dequant2[4][64];
1006 stbi__huffman huff_dc[4];
1007 stbi__huffman huff_ac[4];
1011 int img_h_max, img_v_max;
1012 int img_mcu_x, img_mcu_y;
1013 int img_mcu_w, img_mcu_h;
1030 stbi__uint32 code_buffer;
1035 int scan_n, order[4];
1036 int restart_interval, todo;
1039 static int stbi__build_huffman(stbi__huffman *
h,
int *
count)
1043 for (i=0; i < 16; ++i)
1044 for (j=0; j < count[i]; ++j)
1045 h->size[k++] = (
stbi_uc) (i+1);
1051 for(j=1; j <= 16; ++j) {
1053 h->delta[j] = k - code;
1054 if (h->size[k] == j) {
1055 while (h->size[k] == j)
1056 h->code[k++] = (stbi__uint16) (code++);
1057 if (code-1 >= (1 << j))
return stbi__err(
"bad code lengths",
"Corrupt JPEG");
1060 h->maxcode[j] = code << (16-j);
1063 h->maxcode[j] = 0xffffffff;
1066 memset(h->fast, 255, 1 << FAST_BITS);
1067 for (i=0; i < k; ++i) {
1069 if (s <= FAST_BITS) {
1070 int c = h->code[i] << (FAST_BITS-s);
1071 int m = 1 << (FAST_BITS-s);
1072 for (j=0; j <
m; ++j) {
1080 static void stbi__grow_buffer_unsafe(stbi__jpeg *j)
1083 int b = j->nomore ? 0 : stbi__get8(j->s);
1085 int c = stbi__get8(j->s);
1087 j->marker = (
unsigned char) c;
1092 j->code_buffer |= b << (24 - j->code_bits);
1094 }
while (j->code_bits <= 24);
1098 static stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
1101 stbi_inline
static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
1106 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1110 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
1114 if (s > j->code_bits)
1116 j->code_buffer <<= s;
1118 return h->values[k];
1127 temp = j->code_buffer >> 16;
1128 for (k=FAST_BITS+1 ; ; ++k)
1129 if (temp < h->maxcode[k])
1137 if (k > j->code_bits)
1141 c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
1142 STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
1146 j->code_buffer <<= k;
1147 return h->values[c];
1152 stbi_inline
static int stbi__extend_receive(stbi__jpeg *j,
int n)
1154 unsigned int m = 1 << (n-1);
1156 if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
1159 k = stbi_lrot(j->code_buffer, n);
1160 j->code_buffer = k & ~stbi__bmask[
n];
1161 k &= stbi__bmask[
n];
1164 k = (j->code_buffer >> (32 -
n)) & stbi__bmask[
n];
1166 j->code_buffer <<=
n;
1172 return (-1 << n) + k + 1;
1179 static stbi_uc stbi__jpeg_dezigzag[64+15] =
1181 0, 1, 8, 16, 9, 2, 3, 10,
1182 17, 24, 32, 25, 18, 11, 4, 5,
1183 12, 19, 26, 33, 40, 48, 41, 34,
1184 27, 20, 13, 6, 7, 14, 21, 28,
1185 35, 42, 49, 56, 57, 50, 43, 36,
1186 29, 22, 15, 23, 30, 37, 44, 51,
1187 58, 59, 52, 45, 38, 31, 39, 46,
1188 53, 60, 61, 54, 47, 55, 62, 63,
1190 63, 63, 63, 63, 63, 63, 63, 63,
1191 63, 63, 63, 63, 63, 63, 63
1195 static int stbi__jpeg_decode_block(stbi__jpeg *j,
short data[64], stbi__huffman *hdc, stbi__huffman *hac,
int b)
1198 int t = stbi__jpeg_huff_decode(j, hdc);
1199 if (t < 0)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
1202 memset(data,0,64*
sizeof(data[0]));
1204 diff = t ? stbi__extend_receive(j, t) : 0;
1205 dc = j->img_comp[
b].dc_pred + diff;
1206 j->img_comp[
b].dc_pred = dc;
1207 data[0] = (short) dc;
1213 int rs = stbi__jpeg_huff_decode(j, hac);
1214 if (rs < 0)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
1218 if (rs != 0xf0)
break;
1223 data[stbi__jpeg_dezigzag[k++]] = (short) stbi__extend_receive(j,s);
1230 stbi_inline
static stbi_uc stbi__clamp(
int x)
1233 if ((
unsigned int) x > 255) {
1234 if (x < 0)
return 0;
1235 if (x > 255)
return 255;
1240 #define stbi__f2f(x) (int) (((x) * 4096 + 0.5))
1241 #define stbi__fsh(x) ((x) << 12)
1244 #define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
1245 int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
1248 p1 = (p2+p3) * stbi__f2f(0.5411961f); \
1249 t2 = p1 + p3*stbi__f2f(-1.847759065f); \
1250 t3 = p1 + p2*stbi__f2f( 0.765366865f); \
1253 t0 = stbi__fsh(p2+p3); \
1254 t1 = stbi__fsh(p2-p3); \
1267 p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
1268 t0 = t0*stbi__f2f( 0.298631336f); \
1269 t1 = t1*stbi__f2f( 2.053119869f); \
1270 t2 = t2*stbi__f2f( 3.072711026f); \
1271 t3 = t3*stbi__f2f( 1.501321110f); \
1272 p1 = p5 + p1*stbi__f2f(-0.899976223f); \
1273 p2 = p5 + p2*stbi__f2f(-2.562915447f); \
1274 p3 = p3*stbi__f2f(-1.961570560f); \
1275 p4 = p4*stbi__f2f(-0.390180644f); \
1282 typedef unsigned short stbi_dequantize_t;
1284 typedef stbi_uc stbi_dequantize_t;
1288 static void stbi__idct_block(
stbi_uc *out,
int out_stride,
short data[64], stbi_dequantize_t *dequantize)
1290 int i,val[64],*
v=val;
1291 stbi_dequantize_t *dq = dequantize;
1296 for (i=0; i < 8; ++i,++d,++dq, ++
v) {
1298 if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
1299 && d[40]==0 && d[48]==0 && d[56]==0) {
1304 int dcterm = d[0] * dq[0] << 2;
1305 v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
1307 STBI__IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],
1308 d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])
1311 x0 += 512; x1 += 512; x2 += 512; x3 += 512;
1312 v[ 0] = (x0+t3) >> 10;
1313 v[56] = (x0-t3) >> 10;
1314 v[ 8] = (x1+t2) >> 10;
1315 v[48] = (x1-t2) >> 10;
1316 v[16] = (x2+t1) >> 10;
1317 v[40] = (x2-t1) >> 10;
1318 v[24] = (x3+t0) >> 10;
1319 v[32] = (x3-t0) >> 10;
1323 for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
1325 STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
1332 x0 += 65536 + (128<<17);
1333 x1 += 65536 + (128<<17);
1334 x2 += 65536 + (128<<17);
1335 x3 += 65536 + (128<<17);
1338 o[0] = stbi__clamp((x0+t3) >> 17);
1339 o[7] = stbi__clamp((x0-t3) >> 17);
1340 o[1] = stbi__clamp((x1+t2) >> 17);
1341 o[6] = stbi__clamp((x1-t2) >> 17);
1342 o[2] = stbi__clamp((x2+t1) >> 17);
1343 o[5] = stbi__clamp((x2-t1) >> 17);
1344 o[3] = stbi__clamp((x3+t0) >> 17);
1345 o[4] = stbi__clamp((x3-t0) >> 17);
1350 static stbi_idct_8x8 stbi__idct_installed = stbi__idct_block;
1352 STBIDEF void stbi_install_idct(stbi_idct_8x8 func)
1354 stbi__idct_installed = func;
1358 #define STBI__MARKER_none 0xff
1362 static stbi_uc stbi__get_marker(stbi__jpeg *j)
1365 if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none;
return x; }
1366 x = stbi__get8(j->s);
1367 if (x != 0xff)
return STBI__MARKER_none;
1369 x = stbi__get8(j->s);
1375 #define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
1379 static void stbi__jpeg_reset(stbi__jpeg *j)
1384 j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
1385 j->marker = STBI__MARKER_none;
1386 j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
1391 static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
1393 stbi__jpeg_reset(z);
1394 if (z->scan_n == 1) {
1397 __declspec(align(16))
1400 int n = z->order[0];
1405 int w = (z->img_comp[
n].x+7) >> 3;
1406 int h = (z->img_comp[
n].y+7) >> 3;
1407 for (j=0; j <
h; ++j) {
1408 for (i=0; i <
w; ++i) {
1409 if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n))
return 0;
1411 stbi__idct_installed(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
1413 stbi__idct_block(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
1416 if (--z->todo <= 0) {
1417 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
1420 if (!STBI__RESTART(z->marker))
return 1;
1421 stbi__jpeg_reset(z);
1428 for (j=0; j < z->img_mcu_y; ++j) {
1429 for (i=0; i < z->img_mcu_x; ++i) {
1431 for (k=0; k < z->scan_n; ++k) {
1432 int n = z->order[k];
1435 for (y=0; y < z->img_comp[
n].v; ++
y) {
1436 for (x=0; x < z->img_comp[
n].h; ++
x) {
1437 int x2 = (i*z->img_comp[
n].h +
x)*8;
1438 int y2 = (j*z->img_comp[
n].v +
y)*8;
1439 if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n))
return 0;
1441 stbi__idct_installed(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
1443 stbi__idct_block(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
1450 if (--z->todo <= 0) {
1451 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
1454 if (!STBI__RESTART(z->marker))
return 1;
1455 stbi__jpeg_reset(z);
1463 static int stbi__process_marker(stbi__jpeg *z,
int m)
1467 case STBI__MARKER_none:
1468 return stbi__err(
"expected marker",
"Corrupt JPEG");
1471 return stbi__err(
"progressive jpeg",
"JPEG format not supported (progressive)");
1474 if (stbi__get16be(z->s) != 4)
return stbi__err(
"bad DRI len",
"Corrupt JPEG");
1475 z->restart_interval = stbi__get16be(z->s);
1479 L = stbi__get16be(z->s)-2;
1481 int q = stbi__get8(z->s);
1484 if (p != 0)
return stbi__err(
"bad DQT type",
"Corrupt JPEG");
1485 if (t > 3)
return stbi__err(
"bad DQT table",
"Corrupt JPEG");
1486 for (i=0; i < 64; ++i)
1487 z->dequant[t][stbi__jpeg_dezigzag[i]] = stbi__get8(z->s);
1489 for (i=0; i < 64; ++i)
1490 z->dequant2[t][i] = z->dequant[t][i];
1497 L = stbi__get16be(z->s)-2;
1500 int sizes[16],i,n=0;
1501 int q = stbi__get8(z->s);
1504 if (tc > 1 || th > 3)
return stbi__err(
"bad DHT header",
"Corrupt JPEG");
1505 for (i=0; i < 16; ++i) {
1506 sizes[i] = stbi__get8(z->s);
1511 if (!stbi__build_huffman(z->huff_dc+th, sizes))
return 0;
1512 v = z->huff_dc[th].values;
1514 if (!stbi__build_huffman(z->huff_ac+th, sizes))
return 0;
1515 v = z->huff_ac[th].values;
1517 for (i=0; i <
n; ++i)
1518 v[i] = stbi__get8(z->s);
1524 if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
1525 stbi__skip(z->s, stbi__get16be(z->s)-2);
1532 static int stbi__process_scan_header(stbi__jpeg *z)
1535 int Ls = stbi__get16be(z->s);
1536 z->scan_n = stbi__get8(z->s);
1537 if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (
int) z->s->img_n)
return stbi__err(
"bad stbi__SOS component count",
"Corrupt JPEG");
1538 if (Ls != 6+2*z->scan_n)
return stbi__err(
"bad stbi__SOS len",
"Corrupt JPEG");
1539 for (i=0; i < z->scan_n; ++i) {
1540 int id = stbi__get8(z->s), which;
1541 int q = stbi__get8(z->s);
1542 for (which = 0; which < z->s->img_n; ++which)
1543 if (z->img_comp[which].id ==
id)
1545 if (which == z->s->img_n)
return 0;
1546 z->img_comp[which].hd = q >> 4;
if (z->img_comp[which].hd > 3)
return stbi__err(
"bad DC huff",
"Corrupt JPEG");
1547 z->img_comp[which].ha = q & 15;
if (z->img_comp[which].ha > 3)
return stbi__err(
"bad AC huff",
"Corrupt JPEG");
1548 z->order[i] = which;
1550 if (stbi__get8(z->s) != 0)
return stbi__err(
"bad stbi__SOS",
"Corrupt JPEG");
1552 if (stbi__get8(z->s) != 0)
return stbi__err(
"bad stbi__SOS",
"Corrupt JPEG");
1557 static int stbi__process_frame_header(stbi__jpeg *z,
int scan)
1559 stbi__context *s = z->s;
1560 int Lf,p,i,q, h_max=1,v_max=1,c;
1561 Lf = stbi__get16be(s);
if (Lf < 11)
return stbi__err(
"bad stbi__SOF len",
"Corrupt JPEG");
1562 p = stbi__get8(s);
if (p != 8)
return stbi__err(
"only 8-bit",
"JPEG format not supported: 8-bit only");
1563 s->img_y = stbi__get16be(s);
if (s->img_y == 0)
return stbi__err(
"no header height",
"JPEG format not supported: delayed height");
1564 s->img_x = stbi__get16be(s);
if (s->img_x == 0)
return stbi__err(
"0 width",
"Corrupt JPEG");
1566 if (c != 3 && c != 1)
return stbi__err(
"bad component count",
"Corrupt JPEG");
1568 for (i=0; i < c; ++i) {
1569 z->img_comp[i].data = NULL;
1570 z->img_comp[i].linebuf = NULL;
1573 if (Lf != 8+3*s->img_n)
return stbi__err(
"bad stbi__SOF len",
"Corrupt JPEG");
1575 for (i=0; i < s->img_n; ++i) {
1576 z->img_comp[i].id = stbi__get8(s);
1577 if (z->img_comp[i].id != i+1)
1578 if (z->img_comp[i].id != i)
1579 return stbi__err(
"bad component ID",
"Corrupt JPEG");
1581 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");
1582 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");
1583 z->img_comp[i].tq = stbi__get8(s);
if (z->img_comp[i].tq > 3)
return stbi__err(
"bad TQ",
"Corrupt JPEG");
1586 if (scan != SCAN_load)
return 1;
1588 if ((1 << 30) / s->img_x / s->img_n < s->img_y)
return stbi__err(
"too large",
"Image too large to decode");
1590 for (i=0; i < s->img_n; ++i) {
1591 if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
1592 if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
1596 z->img_h_max = h_max;
1597 z->img_v_max = v_max;
1598 z->img_mcu_w = h_max * 8;
1599 z->img_mcu_h = v_max * 8;
1600 z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
1601 z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
1603 for (i=0; i < s->img_n; ++i) {
1605 z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
1606 z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
1611 z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
1612 z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
1613 z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
1614 if (z->img_comp[i].raw_data == NULL) {
1615 for(--i; i >= 0; --i) {
1616 free(z->img_comp[i].raw_data);
1617 z->img_comp[i].data = NULL;
1619 return stbi__err(
"outofmem",
"Out of memory");
1622 z->img_comp[i].data = (
stbi_uc*) (((
size_t) z->img_comp[i].raw_data + 15) & ~15);
1623 z->img_comp[i].linebuf = NULL;
1630 #define stbi__DNL(x) ((x) == 0xdc)
1631 #define stbi__SOI(x) ((x) == 0xd8)
1632 #define stbi__EOI(x) ((x) == 0xd9)
1633 #define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1)
1634 #define stbi__SOS(x) ((x) == 0xda)
1636 static int decode_jpeg_header(stbi__jpeg *z,
int scan)
1639 z->marker = STBI__MARKER_none;
1640 m = stbi__get_marker(z);
1641 if (!stbi__SOI(m))
return stbi__err(
"no stbi__SOI",
"Corrupt JPEG");
1642 if (scan == SCAN_type)
return 1;
1643 m = stbi__get_marker(z);
1644 while (!stbi__SOF(m)) {
1645 if (!stbi__process_marker(z,m))
return 0;
1646 m = stbi__get_marker(z);
1647 while (m == STBI__MARKER_none) {
1649 if (stbi__at_eof(z->s))
return stbi__err(
"no stbi__SOF",
"Corrupt JPEG");
1650 m = stbi__get_marker(z);
1653 if (!stbi__process_frame_header(z, scan))
return 0;
1657 static int decode_jpeg_image(stbi__jpeg *j)
1660 j->restart_interval = 0;
1661 if (!decode_jpeg_header(j, SCAN_load))
return 0;
1662 m = stbi__get_marker(j);
1663 while (!stbi__EOI(m)) {
1665 if (!stbi__process_scan_header(j))
return 0;
1666 if (!stbi__parse_entropy_coded_data(j))
return 0;
1667 if (j->marker == STBI__MARKER_none ) {
1669 while (!stbi__at_eof(j->s)) {
1670 int x = stbi__get8(j->s);
1672 j->marker = stbi__get8(j->s);
1674 }
else if (x != 0) {
1681 if (!stbi__process_marker(j, m))
return 0;
1683 m = stbi__get_marker(j);
1693 #define stbi__div4(x) ((stbi_uc) ((x) >> 2))
1698 STBI_NOTUSED(in_far);
1709 for (i=0; i <
w; ++i)
1710 out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2);
1722 out[0] = out[1] = input[0];
1727 out[1] = stbi__div4(input[0]*3 + input[1] + 2);
1728 for (i=1; i < w-1; ++i) {
1729 int n = 3*input[i]+2;
1730 out[i*2+0] = stbi__div4(n+input[i-1]);
1731 out[i*2+1] = stbi__div4(n+input[i+1]);
1733 out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2);
1734 out[i*2+1] = input[w-1];
1736 STBI_NOTUSED(in_far);
1742 #define stbi__div16(x) ((stbi_uc) ((x) >> 4))
1749 out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
1753 t1 = 3*in_near[0] + in_far[0];
1754 out[0] = stbi__div4(t1+2);
1755 for (i=1; i <
w; ++i) {
1757 t1 = 3*in_near[i]+in_far[i];
1758 out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
1759 out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
1761 out[w*2-1] = stbi__div4(t1+2);
1772 STBI_NOTUSED(in_far);
1773 for (i=0; i <
w; ++i)
1774 for (j=0; j < hs; ++j)
1775 out[i*hs+j] = in_near[i];
1779 #define float2fixed(x) ((int) ((x) * 65536 + 0.5))
1786 for (i=0; i <
count; ++i) {
1787 int y_fixed = (y[i] << 16) + 32768;
1789 int cr = pcr[i] - 128;
1790 int cb = pcb[i] - 128;
1791 r = y_fixed + cr*float2fixed(1.40200f);
1792 g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);
1793 b = y_fixed + cb*float2fixed(1.77200f);
1797 if ((
unsigned) r > 255) {
if (r < 0) r = 0;
else r = 255; }
1798 if ((
unsigned) g > 255) {
if (g < 0) g = 0;
else g = 255; }
1799 if ((
unsigned) b > 255) {
if (b < 0) b = 0;
else b = 255; }
1809 static stbi_YCbCr_to_RGB_run stbi__YCbCr_installed = stbi__YCbCr_to_RGB_row;
1811 STBIDEF void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)
1813 stbi__YCbCr_installed = func;
1819 static void stbi__cleanup_jpeg(stbi__jpeg *j)
1822 for (i=0; i < j->s->img_n; ++i) {
1823 if (j->img_comp[i].raw_data) {
1824 free(j->img_comp[i].raw_data);
1825 j->img_comp[i].raw_data = NULL;
1826 j->img_comp[i].data = NULL;
1828 if (j->img_comp[i].linebuf) {
1829 free(j->img_comp[i].linebuf);
1830 j->img_comp[i].linebuf = NULL;
1837 resample_row_func resample;
1845 static stbi_uc *load_jpeg_image(stbi__jpeg *z,
int *out_x,
int *out_y,
int *comp,
int req_comp)
1851 if (req_comp < 0 || req_comp > 4)
return stbi__errpuc(
"bad req_comp",
"Internal error");
1854 if (!decode_jpeg_image(z)) { stbi__cleanup_jpeg(z);
return NULL; }
1857 n = req_comp ? req_comp : z->s->img_n;
1859 if (z->s->img_n == 3 && n < 3)
1862 decode_n = z->s->img_n;
1871 stbi__resample res_comp[4];
1873 for (k=0; k < decode_n; ++k) {
1874 stbi__resample *r = &res_comp[k];
1878 z->img_comp[k].linebuf = (
stbi_uc *) malloc(z->s->img_x + 3);
1879 if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z);
return stbi__errpuc(
"outofmem",
"Out of memory"); }
1881 r->hs = z->img_h_max / z->img_comp[k].h;
1882 r->vs = z->img_v_max / z->img_comp[k].v;
1883 r->ystep = r->vs >> 1;
1884 r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
1886 r->line0 = r->line1 = z->img_comp[k].data;
1888 if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
1889 else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2;
1890 else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2;
1891 else if (r->hs == 2 && r->vs == 2) r->resample = stbi__resample_row_hv_2;
1892 else r->resample = stbi__resample_row_generic;
1896 output = (
stbi_uc *) malloc(n * z->s->img_x * z->s->img_y + 1);
1897 if (!output) { stbi__cleanup_jpeg(z);
return stbi__errpuc(
"outofmem",
"Out of memory"); }
1900 for (j=0; j < z->s->img_y; ++j) {
1901 stbi_uc *out = output + n * z->s->img_x * j;
1902 for (k=0; k < decode_n; ++k) {
1903 stbi__resample *r = &res_comp[k];
1904 int y_bot = r->ystep >= (r->vs >> 1);
1905 coutput[k] = r->resample(z->img_comp[k].linebuf,
1906 y_bot ? r->line1 : r->line0,
1907 y_bot ? r->line0 : r->line1,
1909 if (++r->ystep >= r->vs) {
1911 r->line0 = r->line1;
1912 if (++r->ypos < z->img_comp[k].y)
1913 r->line1 += z->img_comp[k].w2;
1918 if (z->s->img_n == 3) {
1920 stbi__YCbCr_installed(out, y, coutput[1], coutput[2], z->s->img_x, n);
1922 stbi__YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s->img_x, n);
1925 for (i=0; i < z->s->img_x; ++i) {
1926 out[0] = out[1] = out[2] = y[i];
1933 for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
1935 for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
1938 stbi__cleanup_jpeg(z);
1939 *out_x = z->s->img_x;
1940 *out_y = z->s->img_y;
1941 if (comp) *comp = z->s->img_n;
1946 static unsigned char *stbi__jpeg_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
1950 return load_jpeg_image(&j, x,y,comp,req_comp);
1953 static int stbi__jpeg_test(stbi__context *s)
1958 r = decode_jpeg_header(&j, SCAN_type);
1963 static int stbi__jpeg_info_raw(stbi__jpeg *j,
int *x,
int *y,
int *comp)
1965 if (!decode_jpeg_header(j, SCAN_header)) {
1966 stbi__rewind( j->s );
1969 if (x) *x = j->s->img_x;
1970 if (y) *y = j->s->img_y;
1971 if (comp) *comp = j->s->img_n;
1975 static int stbi__jpeg_info(stbi__context *s,
int *x,
int *y,
int *comp)
1979 return stbi__jpeg_info_raw(&j, x, y, comp);
1990 #define STBI__ZFAST_BITS 9 // accelerate all cases in default tables
1991 #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
1997 stbi__uint16 fast[1 << STBI__ZFAST_BITS];
1998 stbi__uint16 firstcode[16];
2000 stbi__uint16 firstsymbol[16];
2002 stbi__uint16
value[288];
2005 stbi_inline
static int stbi__bitreverse16(
int n)
2007 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
2008 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
2009 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
2010 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
2014 stbi_inline
static int stbi__bit_reverse(
int v,
int bits)
2016 STBI_ASSERT(bits <= 16);
2019 return stbi__bitreverse16(v) >> (16-bits);
2022 static int stbi__zbuild_huffman(stbi__zhuffman *z,
stbi_uc *sizelist,
int num)
2025 int code, next_code[16], sizes[17];
2028 memset(sizes, 0,
sizeof(sizes));
2029 memset(z->fast, 255,
sizeof(z->fast));
2030 for (i=0; i < num; ++i)
2031 ++sizes[sizelist[i]];
2033 for (i=1; i < 16; ++i)
2034 STBI_ASSERT(sizes[i] <= (1 << i));
2036 for (i=1; i < 16; ++i) {
2037 next_code[i] = code;
2038 z->firstcode[i] = (stbi__uint16) code;
2039 z->firstsymbol[i] = (stbi__uint16) k;
2040 code = (code + sizes[i]);
2042 if (code-1 >= (1 << i))
return stbi__err(
"bad codelengths",
"Corrupt JPEG");
2043 z->maxcode[i] = code << (16-i);
2047 z->maxcode[16] = 0x10000;
2048 for (i=0; i < num; ++i) {
2049 int s = sizelist[i];
2051 int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
2053 z->value[c] = (stbi__uint16) i;
2054 if (s <= STBI__ZFAST_BITS) {
2055 int k = stbi__bit_reverse(next_code[s],s);
2056 while (k < (1 << STBI__ZFAST_BITS)) {
2057 z->fast[k] = (stbi__uint16) c;
2075 stbi_uc *zbuffer, *zbuffer_end;
2077 stbi__uint32 code_buffer;
2084 stbi__zhuffman z_length, z_distance;
2087 stbi_inline
static stbi_uc stbi__zget8(stbi__zbuf *z)
2089 if (z->zbuffer >= z->zbuffer_end)
return 0;
2090 return *z->zbuffer++;
2093 static void stbi__fill_bits(stbi__zbuf *z)
2096 STBI_ASSERT(z->code_buffer < (1U << z->num_bits));
2097 z->code_buffer |= stbi__zget8(z) << z->num_bits;
2099 }
while (z->num_bits <= 24);
2102 stbi_inline
static unsigned int stbi__zreceive(stbi__zbuf *z,
int n)
2105 if (z->num_bits < n) stbi__fill_bits(z);
2106 k = z->code_buffer & ((1 <<
n) - 1);
2107 z->code_buffer >>=
n;
2112 stbi_inline
static int stbi__zhuffman_decode(stbi__zbuf *
a, stbi__zhuffman *z)
2115 if (a->num_bits < 16) stbi__fill_bits(a);
2116 b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
2119 a->code_buffer >>= s;
2126 k = stbi__bit_reverse(a->code_buffer, 16);
2127 for (s=STBI__ZFAST_BITS+1; ; ++s)
2128 if (k < z->maxcode[s])
2130 if (s == 16)
return -1;
2132 b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
2133 STBI_ASSERT(z->size[b] == s);
2134 a->code_buffer >>= s;
2139 static int stbi__zexpand(stbi__zbuf *z,
int n)
2143 if (!z->z_expandable)
return stbi__err(
"output buffer limit",
"Corrupt PNG");
2144 cur = (
int) (z->zout - z->zout_start);
2145 limit = (
int) (z->zout_end - z->zout_start);
2146 while (cur + n > limit)
2148 q = (
char *) realloc(z->zout_start, limit);
2149 if (q == NULL)
return stbi__err(
"outofmem",
"Out of memory");
2152 z->zout_end = q + limit;
2156 static int stbi__zlength_base[31] = {
2157 3,4,5,6,7,8,9,10,11,13,
2158 15,17,19,23,27,31,35,43,51,59,
2159 67,83,99,115,131,163,195,227,258,0,0 };
2161 static int stbi__zlength_extra[31]=
2162 { 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 };
2164 static int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
2165 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
2167 static int stbi__zdist_extra[32] =
2168 { 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};
2170 static int stbi__parse_huffman_block(stbi__zbuf *a)
2173 int z = stbi__zhuffman_decode(a, &a->z_length);
2175 if (z < 0)
return stbi__err(
"bad huffman code",
"Corrupt PNG");
2176 if (a->zout >= a->zout_end)
if (!stbi__zexpand(a, 1))
return 0;
2177 *a->zout++ = (char) z;
2181 if (z == 256)
return 1;
2183 len = stbi__zlength_base[z];
2184 if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
2185 z = stbi__zhuffman_decode(a, &a->z_distance);
2186 if (z < 0)
return stbi__err(
"bad huffman code",
"Corrupt PNG");
2187 dist = stbi__zdist_base[z];
2188 if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
2189 if (a->zout - a->zout_start < dist)
return stbi__err(
"bad dist",
"Corrupt PNG");
2190 if (a->zout + len > a->zout_end)
if (!stbi__zexpand(a, len))
return 0;
2191 p = (
stbi_uc *) (a->zout - dist);
2198 static int stbi__compute_huffman_codes(stbi__zbuf *a)
2200 static stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
2201 stbi__zhuffman z_codelength;
2206 int hlit = stbi__zreceive(a,5) + 257;
2207 int hdist = stbi__zreceive(a,5) + 1;
2208 int hclen = stbi__zreceive(a,4) + 4;
2210 memset(codelength_sizes, 0,
sizeof(codelength_sizes));
2211 for (i=0; i < hclen; ++i) {
2212 int s = stbi__zreceive(a,3);
2213 codelength_sizes[length_dezigzag[i]] = (
stbi_uc) s;
2215 if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19))
return 0;
2218 while (n < hlit + hdist) {
2219 int c = stbi__zhuffman_decode(a, &z_codelength);
2220 STBI_ASSERT(c >= 0 && c < 19);
2224 c = stbi__zreceive(a,2)+3;
2225 memset(lencodes+n, lencodes[n-1], c);
2227 }
else if (c == 17) {
2228 c = stbi__zreceive(a,3)+3;
2229 memset(lencodes+n, 0, c);
2232 STBI_ASSERT(c == 18);
2233 c = stbi__zreceive(a,7)+11;
2234 memset(lencodes+n, 0, c);
2238 if (n != hlit+hdist)
return stbi__err(
"bad codelengths",
"Corrupt PNG");
2239 if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit))
return 0;
2240 if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist))
return 0;
2244 static int stbi__parse_uncomperssed_block(stbi__zbuf *a)
2248 if (a->num_bits & 7)
2249 stbi__zreceive(a, a->num_bits & 7);
2252 while (a->num_bits > 0) {
2253 header[k++] = (
stbi_uc) (a->code_buffer & 255);
2254 a->code_buffer >>= 8;
2257 STBI_ASSERT(a->num_bits == 0);
2260 header[k++] = stbi__zget8(a);
2261 len = header[1] * 256 + header[0];
2262 nlen = header[3] * 256 + header[2];
2263 if (nlen != (len ^ 0xffff))
return stbi__err(
"zlib corrupt",
"Corrupt PNG");
2264 if (a->zbuffer + len > a->zbuffer_end)
return stbi__err(
"read past buffer",
"Corrupt PNG");
2265 if (a->zout + len > a->zout_end)
2266 if (!stbi__zexpand(a, len))
return 0;
2267 memcpy(a->zout, a->zbuffer, len);
2273 static int stbi__parse_zlib_header(stbi__zbuf *a)
2275 int cmf = stbi__zget8(a);
2278 int flg = stbi__zget8(a);
2279 if ((cmf*256+flg) % 31 != 0)
return stbi__err(
"bad zlib header",
"Corrupt PNG");
2280 if (flg & 32)
return stbi__err(
"no preset dict",
"Corrupt PNG");
2281 if (cm != 8)
return stbi__err(
"bad compression",
"Corrupt PNG");
2287 static stbi_uc stbi__zdefault_length[288], stbi__zdefault_distance[32];
2288 static void stbi__init_zdefaults(
void)
2291 for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8;
2292 for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9;
2293 for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7;
2294 for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8;
2296 for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5;
2299 static int stbi__parse_zlib(stbi__zbuf *a,
int parse_header)
2303 if (!stbi__parse_zlib_header(a))
return 0;
2307 final = stbi__zreceive(a,1);
2308 type = stbi__zreceive(a,2);
2310 if (!stbi__parse_uncomperssed_block(a))
return 0;
2311 }
else if (type == 3) {
2316 if (!stbi__zdefault_distance[31]) stbi__init_zdefaults();
2317 if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288))
return 0;
2318 if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32))
return 0;
2320 if (!stbi__compute_huffman_codes(a))
return 0;
2322 if (!stbi__parse_huffman_block(a))
return 0;
2328 static int stbi__do_zlib(stbi__zbuf *a,
char *obuf,
int olen,
int exp,
int parse_header)
2330 a->zout_start = obuf;
2332 a->zout_end = obuf + olen;
2333 a->z_expandable = exp;
2335 return stbi__parse_zlib(a, parse_header);
2341 char *p = (
char *) malloc(initial_size);
2342 if (p == NULL)
return NULL;
2343 a.zbuffer = (
stbi_uc *) buffer;
2344 a.zbuffer_end = (
stbi_uc *) buffer + len;
2345 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
2346 if (outlen) *outlen = (
int) (a.zout - a.zout_start);
2347 return a.zout_start;
2362 char *p = (
char *) malloc(initial_size);
2363 if (p == NULL)
return NULL;
2364 a.zbuffer = (
stbi_uc *) buffer;
2365 a.zbuffer_end = (
stbi_uc *) buffer + len;
2366 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
2367 if (outlen) *outlen = (
int) (a.zout - a.zout_start);
2368 return a.zout_start;
2378 a.zbuffer = (
stbi_uc *) ibuffer;
2379 a.zbuffer_end = (
stbi_uc *) ibuffer + ilen;
2380 if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
2381 return (
int) (a.zout - a.zout_start);
2389 char *p = (
char *) malloc(16384);
2390 if (p == NULL)
return NULL;
2391 a.zbuffer = (
stbi_uc *) buffer;
2392 a.zbuffer_end = (
stbi_uc *) buffer+len;
2393 if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
2394 if (outlen) *outlen = (
int) (a.zout - a.zout_start);
2395 return a.zout_start;
2405 a.zbuffer = (
stbi_uc *) ibuffer;
2406 a.zbuffer_end = (
stbi_uc *) ibuffer + ilen;
2407 if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
2408 return (
int) (a.zout - a.zout_start);
2430 #define PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
2432 static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
2435 c.length = stbi__get32be(s);
2436 c.type = stbi__get32be(s);
2440 static int stbi__check_png_header(stbi__context *s)
2442 static stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };
2444 for (i=0; i < 8; ++i)
2445 if (stbi__get8(s) != png_sig[i])
return stbi__err(
"bad png sig",
"Not a PNG");
2452 stbi_uc *idata, *expanded, *out;
2457 STBI__F_none=0, STBI__F_sub=1, STBI__F_up=2, STBI__F_avg=3, STBI__F_paeth=4,
2458 STBI__F_avg_first, STBI__F_paeth_first
2461 static stbi_uc first_row_filter[5] =
2463 STBI__F_none, STBI__F_sub, STBI__F_none, STBI__F_avg_first, STBI__F_paeth_first
2466 static int stbi__paeth(
int a,
int b,
int c)
2472 if (pa <= pb && pa <= pc)
return a;
2473 if (pb <= pc)
return b;
2477 #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
2480 static int stbi__create_png_image_raw(stbi__png *a,
stbi_uc *raw, stbi__uint32 raw_len,
int out_n, stbi__uint32 x, stbi__uint32 y)
2482 stbi__context *s = a->s;
2483 stbi__uint32 i,j,
stride = x*out_n;
2485 int img_n = s->img_n;
2486 STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1);
2487 a->out = (
stbi_uc *) malloc(x * y * out_n);
2488 if (!a->out)
return stbi__err(
"outofmem",
"Out of memory");
2489 if (s->img_x == x && s->img_y == y) {
2490 if (raw_len != (img_n * x + 1) * y)
return stbi__err(
"not enough pixels",
"Corrupt PNG");
2492 if (raw_len < (img_n * x + 1) * y)
return stbi__err(
"not enough pixels",
"Corrupt PNG");
2494 for (j=0; j <
y; ++j) {
2495 stbi_uc *cur = a->out + stride*j;
2498 if (filter > 4)
return stbi__err(
"invalid filter",
"Corrupt PNG");
2500 if (j == 0) filter = first_row_filter[
filter];
2502 for (k=0; k < img_n; ++k) {
2504 case STBI__F_none : cur[k] = raw[k];
break;
2505 case STBI__F_sub : cur[k] = raw[k];
break;
2506 case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]);
break;
2507 case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1));
break;
2508 case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0));
break;
2509 case STBI__F_avg_first : cur[k] = raw[k];
break;
2510 case STBI__F_paeth_first: cur[k] = raw[k];
break;
2513 if (img_n != out_n) cur[img_n] = 255;
2518 if (img_n == out_n) {
2521 for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \
2522 for (k=0; k < img_n; ++k)
2524 CASE(STBI__F_none) cur[k] = raw[k]; break;
2525 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-img_n]); break;
2526 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
2527 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-img_n])>>1)); break;
2528 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;
2529 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-img_n] >> 1)); break;
2530 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-img_n],0,0)); break;
2534 STBI_ASSERT(img_n+1 == out_n);
2537 for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
2538 for (k=0; k < img_n; ++k)
2540 CASE(STBI__F_none) cur[k] = raw[k]; break;
2541 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-out_n]); break;
2542 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
2543 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-out_n])>>1)); break;
2544 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
2545 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-out_n] >> 1)); break;
2546 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],0,0)); break;
2554 static int stbi__create_png_image(stbi__png *a,
stbi_uc *raw, stbi__uint32 raw_len,
int out_n,
int interlaced)
2559 return stbi__create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y);
2562 final = (
stbi_uc *) malloc(a->s->img_x * a->s->img_y * out_n);
2563 for (p=0; p < 7; ++p) {
2564 int xorig[] = { 0,4,0,2,0,1,0 };
2565 int yorig[] = { 0,0,4,0,2,0,1 };
2566 int xspc[] = { 8,8,4,4,2,2,1 };
2567 int yspc[] = { 8,8,8,4,4,2,2 };
2570 x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
2571 y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
2573 if (!stbi__create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
2577 for (j=0; j <
y; ++j)
2578 for (i=0; i <
x; ++i)
2579 memcpy(
final + (j*yspc[p]+yorig[p])*a->s->img_x*out_n + (i*xspc[p]+xorig[p])*out_n,
2580 a->out + (j*x+i)*out_n, out_n);
2582 raw += (x*out_n+1)*y;
2583 raw_len -= (x*out_n+1)*y;
2591 static int stbi__compute_transparency(stbi__png *z,
stbi_uc tc[3],
int out_n)
2593 stbi__context *s = z->s;
2594 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
2599 STBI_ASSERT(out_n == 2 || out_n == 4);
2602 for (i=0; i < pixel_count; ++i) {
2603 p[1] = (p[0] == tc[0] ? 0 : 255);
2607 for (i=0; i < pixel_count; ++i) {
2608 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
2616 static int stbi__expand_png_palette(stbi__png *a,
stbi_uc *palette,
int len,
int pal_img_n)
2618 stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
2619 stbi_uc *p, *temp_out, *orig = a->out;
2621 p = (
stbi_uc *) malloc(pixel_count * pal_img_n);
2622 if (p == NULL)
return stbi__err(
"outofmem",
"Out of memory");
2627 if (pal_img_n == 3) {
2628 for (i=0; i < pixel_count; ++i) {
2631 p[1] = palette[n+1];
2632 p[2] = palette[n+2];
2636 for (i=0; i < pixel_count; ++i) {
2639 p[1] = palette[n+1];
2640 p[2] = palette[n+2];
2641 p[3] = palette[n+3];
2653 static int stbi__unpremultiply_on_load = 0;
2654 static int stbi__de_iphone_flag = 0;
2658 stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;
2663 stbi__de_iphone_flag = flag_true_if_should_convert;
2666 static void stbi__de_iphone(stbi__png *z)
2668 stbi__context *s = z->s;
2669 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
2672 if (s->img_out_n == 3) {
2673 for (i=0; i < pixel_count; ++i) {
2680 STBI_ASSERT(s->img_out_n == 4);
2681 if (stbi__unpremultiply_on_load) {
2683 for (i=0; i < pixel_count; ++i) {
2687 p[0] = p[2] * 255 /
a;
2688 p[1] = p[1] * 255 /
a;
2698 for (i=0; i < pixel_count; ++i) {
2708 static int stbi__parse_png_file(stbi__png *z,
int scan,
int req_comp)
2710 stbi_uc palette[1024], pal_img_n=0;
2712 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
2713 int first=1,k,interlace=0, is_iphone=0;
2714 stbi__context *s = z->s;
2720 if (!stbi__check_png_header(s))
return 0;
2722 if (scan == SCAN_type)
return 1;
2725 stbi__pngchunk c = stbi__get_chunk_header(s);
2727 case PNG_TYPE(
'C',
'g',
'B',
'I'):
2729 stbi__skip(s, c.length);
2731 case PNG_TYPE(
'I',
'H',
'D',
'R'): {
2733 if (!first)
return stbi__err(
"multiple IHDR",
"Corrupt PNG");
2735 if (c.length != 13)
return stbi__err(
"bad IHDR len",
"Corrupt PNG");
2736 s->img_x = stbi__get32be(s);
if (s->img_x > (1 << 24))
return stbi__err(
"too large",
"Very large image (corrupt?)");
2737 s->img_y = stbi__get32be(s);
if (s->img_y > (1 << 24))
return stbi__err(
"too large",
"Very large image (corrupt?)");
2738 depth = stbi__get8(s);
if (depth != 8)
return stbi__err(
"8bit only",
"PNG not supported: 8-bit only");
2739 color = stbi__get8(s);
if (color > 6)
return stbi__err(
"bad ctype",
"Corrupt PNG");
2740 if (color == 3) pal_img_n = 3;
else if (color & 1)
return stbi__err(
"bad ctype",
"Corrupt PNG");
2741 comp = stbi__get8(s);
if (comp)
return stbi__err(
"bad comp method",
"Corrupt PNG");
2742 filter= stbi__get8(s);
if (filter)
return stbi__err(
"bad filter method",
"Corrupt PNG");
2743 interlace = stbi__get8(s);
if (interlace>1)
return stbi__err(
"bad interlace method",
"Corrupt PNG");
2744 if (!s->img_x || !s->img_y)
return stbi__err(
"0-pixel image",
"Corrupt PNG");
2746 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
2747 if ((1 << 30) / s->img_x / s->img_n < s->img_y)
return stbi__err(
"too large",
"Image too large to decode");
2748 if (scan == SCAN_header)
return 1;
2753 if ((1 << 30) / s->img_x / 4 < s->img_y)
return stbi__err(
"too large",
"Corrupt PNG");
2759 case PNG_TYPE(
'P',
'L',
'T',
'E'): {
2760 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2761 if (c.length > 256*3)
return stbi__err(
"invalid PLTE",
"Corrupt PNG");
2762 pal_len = c.length / 3;
2763 if (pal_len * 3 != c.length)
return stbi__err(
"invalid PLTE",
"Corrupt PNG");
2764 for (i=0; i < pal_len; ++i) {
2765 palette[i*4+0] = stbi__get8(s);
2766 palette[i*4+1] = stbi__get8(s);
2767 palette[i*4+2] = stbi__get8(s);
2768 palette[i*4+3] = 255;
2773 case PNG_TYPE(
't',
'R',
'N',
'S'): {
2774 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2775 if (z->idata)
return stbi__err(
"tRNS after IDAT",
"Corrupt PNG");
2777 if (scan == SCAN_header) { s->img_n = 4;
return 1; }
2778 if (pal_len == 0)
return stbi__err(
"tRNS before PLTE",
"Corrupt PNG");
2779 if (c.length > pal_len)
return stbi__err(
"bad tRNS len",
"Corrupt PNG");
2781 for (i=0; i < c.length; ++i)
2782 palette[i*4+3] = stbi__get8(s);
2784 if (!(s->img_n & 1))
return stbi__err(
"tRNS with alpha",
"Corrupt PNG");
2785 if (c.length != (stbi__uint32) s->img_n*2)
return stbi__err(
"bad tRNS len",
"Corrupt PNG");
2787 for (k=0; k < s->img_n; ++k)
2788 tc[k] = (
stbi_uc) (stbi__get16be(s) & 255);
2793 case PNG_TYPE(
'I',
'D',
'A',
'T'): {
2794 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2795 if (pal_img_n && !pal_len)
return stbi__err(
"no PLTE",
"Corrupt PNG");
2796 if (scan == SCAN_header) { s->img_n = pal_img_n;
return 1; }
2797 if (ioff + c.length > idata_limit) {
2799 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
2800 while (ioff + c.length > idata_limit)
2802 p = (
stbi_uc *) realloc(z->idata, idata_limit);
if (p == NULL)
return stbi__err(
"outofmem",
"Out of memory");
2805 if (!stbi__getn(s, z->idata+ioff,c.length))
return stbi__err(
"outofdata",
"Corrupt PNG");
2810 case PNG_TYPE(
'I',
'E',
'N',
'D'): {
2811 stbi__uint32 raw_len;
2812 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2813 if (scan != SCAN_load)
return 1;
2814 if (z->idata == NULL)
return stbi__err(
"no IDAT",
"Corrupt PNG");
2816 if (z->expanded == NULL)
return 0;
2817 free(z->idata); z->idata = NULL;
2818 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
2819 s->img_out_n = s->img_n+1;
2821 s->img_out_n = s->img_n;
2822 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace))
return 0;
2824 if (!stbi__compute_transparency(z, tc, s->img_out_n))
return 0;
2825 if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
2829 s->img_n = pal_img_n;
2830 s->img_out_n = pal_img_n;
2831 if (req_comp >= 3) s->img_out_n = req_comp;
2832 if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
2835 free(z->expanded); z->expanded = NULL;
2841 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2842 if ((c.type & (1 << 29)) == 0) {
2843 #ifndef STBI_NO_FAILURE_STRINGS
2845 static char invalid_chunk[] =
"XXXX PNG chunk not known";
2846 invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
2847 invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
2848 invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
2849 invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
2851 return stbi__err(invalid_chunk,
"PNG not supported: unknown PNG chunk type");
2853 stbi__skip(s, c.length);
2861 static unsigned char *stbi__do_png(stbi__png *p,
int *x,
int *y,
int *n,
int req_comp)
2863 unsigned char *result=NULL;
2864 if (req_comp < 0 || req_comp > 4)
return stbi__errpuc(
"bad req_comp",
"Internal error");
2865 if (stbi__parse_png_file(p, SCAN_load, req_comp)) {
2868 if (req_comp && req_comp != p->s->img_out_n) {
2869 result = stbi__convert_format(result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
2870 p->s->img_out_n = req_comp;
2871 if (result == NULL)
return result;
2875 if (n) *n = p->s->img_n;
2877 free(p->out); p->out = NULL;
2878 free(p->expanded); p->expanded = NULL;
2879 free(p->idata); p->idata = NULL;
2884 static unsigned char *stbi__png_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
2888 return stbi__do_png(&p, x,y,comp,req_comp);
2891 static int stbi__png_test(stbi__context *s)
2894 r = stbi__check_png_header(s);
2899 static int stbi__png_info_raw(stbi__png *p,
int *x,
int *y,
int *comp)
2901 if (!stbi__parse_png_file(p, SCAN_header, 0)) {
2902 stbi__rewind( p->s );
2905 if (x) *x = p->s->img_x;
2906 if (y) *y = p->s->img_y;
2907 if (comp) *comp = p->s->img_n;
2911 static int stbi__png_info(stbi__context *s,
int *x,
int *y,
int *comp)
2915 return stbi__png_info_raw(&p, x, y, comp);
2919 static int stbi__bmp_test_raw(stbi__context *s)
2923 if (stbi__get8(s) !=
'B')
return 0;
2924 if (stbi__get8(s) !=
'M')
return 0;
2929 sz = stbi__get32le(s);
2930 r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
2934 static int stbi__bmp_test(stbi__context *s)
2936 int r = stbi__bmp_test_raw(s);
2943 static int stbi__high_bit(
unsigned int z)
2946 if (z == 0)
return -1;
2947 if (z >= 0x10000) n += 16, z >>= 16;
2948 if (z >= 0x00100) n += 8, z >>= 8;
2949 if (z >= 0x00010) n += 4, z >>= 4;
2950 if (z >= 0x00004) n += 2, z >>= 2;
2951 if (z >= 0x00002) n += 1, z >>= 1;
2955 static int stbi__bitcount(
unsigned int a)
2957 a = (a & 0x55555555) + ((a >> 1) & 0x55555555);
2958 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
2959 a = (a + (a >> 4)) & 0x0f0f0f0f;
2961 a = (a + (a >> 16));
2965 static int stbi__shiftsigned(
int v,
int shift,
int bits)
2970 if (shift < 0) v <<= -shift;
2982 static stbi_uc *stbi__bmp_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
2985 unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
2987 int psize=0,i,j,compress=0,
width;
2989 if (stbi__get8(s) !=
'B' || stbi__get8(s) !=
'M')
return stbi__errpuc(
"not BMP",
"Corrupt BMP");
2993 offset = stbi__get32le(s);
2994 hsz = stbi__get32le(s);
2995 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124)
return stbi__errpuc(
"unknown BMP",
"BMP type not supported: unknown");
2997 s->img_x = stbi__get16le(s);
2998 s->img_y = stbi__get16le(s);
3000 s->img_x = stbi__get32le(s);
3001 s->img_y = stbi__get32le(s);
3003 if (stbi__get16le(s) != 1)
return stbi__errpuc(
"bad BMP",
"bad BMP");
3004 bpp = stbi__get16le(s);
3005 if (bpp == 1)
return stbi__errpuc(
"monochrome",
"BMP type not supported: 1-bit");
3006 flip_vertically = ((
int) s->img_y) > 0;
3007 s->img_y = abs((
int) s->img_y);
3010 psize = (offset - 14 - 24) / 3;
3012 compress = stbi__get32le(s);
3013 if (compress == 1 || compress == 2)
return stbi__errpuc(
"BMP RLE",
"BMP type not supported: RLE");
3019 if (hsz == 40 || hsz == 56) {
3026 if (bpp == 16 || bpp == 32) {
3028 if (compress == 0) {
3035 STBI_NOTUSED(fake_a);
3041 }
else if (compress == 3) {
3042 mr = stbi__get32le(s);
3043 mg = stbi__get32le(s);
3044 mb = stbi__get32le(s);
3046 if (mr == mg && mg == mb) {
3048 return stbi__errpuc(
"bad BMP",
"bad BMP");
3051 return stbi__errpuc(
"bad BMP",
"bad BMP");
3054 STBI_ASSERT(hsz == 108 || hsz == 124);
3055 mr = stbi__get32le(s);
3056 mg = stbi__get32le(s);
3057 mb = stbi__get32le(s);
3058 ma = stbi__get32le(s);
3060 for (i=0; i < 12; ++i)
3070 psize = (offset - 14 - hsz) >> 2;
3072 s->img_n = ma ? 4 : 3;
3073 if (req_comp && req_comp >= 3)
3077 out = (
stbi_uc *) malloc(target * s->img_x * s->img_y);
3078 if (!out)
return stbi__errpuc(
"outofmem",
"Out of memory");
3081 if (psize == 0 || psize > 256) { free(out);
return stbi__errpuc(
"invalid",
"Corrupt BMP"); }
3082 for (i=0; i < psize; ++i) {
3083 pal[i][2] = stbi__get8(s);
3084 pal[i][1] = stbi__get8(s);
3085 pal[i][0] = stbi__get8(s);
3086 if (hsz != 12) stbi__get8(s);
3089 stbi__skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
3090 if (bpp == 4)
width = (s->img_x + 1) >> 1;
3091 else if (bpp == 8)
width = s->img_x;
3092 else { free(out);
return stbi__errpuc(
"bad bpp",
"Corrupt BMP"); }
3094 for (j=0; j < (
int) s->img_y; ++j) {
3095 for (i=0; i < (
int) s->img_x; i += 2) {
3096 int v=stbi__get8(s),
v2=0;
3101 out[z++] = pal[
v][0];
3102 out[z++] = pal[
v][1];
3103 out[z++] = pal[
v][2];
3104 if (target == 4) out[z++] = 255;
3105 if (i+1 == (
int) s->img_x)
break;
3106 v = (bpp == 8) ? stbi__get8(s) :
v2;
3107 out[z++] = pal[
v][0];
3108 out[z++] = pal[
v][1];
3109 out[z++] = pal[
v][2];
3110 if (target == 4) out[z++] = 255;
3115 int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
3118 stbi__skip(s, offset - 14 - hsz);
3119 if (bpp == 24)
width = 3 * s->img_x;
3120 else if (bpp == 16)
width = 2*s->img_x;
3125 }
else if (bpp == 32) {
3126 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
3130 if (!mr || !mg || !mb) { free(out);
return stbi__errpuc(
"bad masks",
"Corrupt BMP"); }
3132 rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);
3133 gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
3134 bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
3135 ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
3137 for (j=0; j < (
int) s->img_y; ++j) {
3139 for (i=0; i < (
int) s->img_x; ++i) {
3141 out[z+2] = stbi__get8(s);
3142 out[z+1] = stbi__get8(s);
3143 out[z+0] = stbi__get8(s);
3145 a = (easy == 2 ? stbi__get8(s) : 255);
3146 if (target == 4) out[z++] =
a;
3149 for (i=0; i < (
int) s->img_x; ++i) {
3150 stbi__uint32 v = (stbi__uint32) (bpp == 16 ? stbi__get16le(s) : stbi__get32le(s));
3152 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount));
3153 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount));
3154 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
3155 a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
3156 if (target == 4) out[z++] = STBI__BYTECAST(a);
3162 if (flip_vertically) {
3164 for (j=0; j < (
int) s->img_y>>1; ++j) {
3166 stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
3167 for (i=0; i < (
int) s->img_x*target; ++i) {
3168 t = p1[i], p1[i] = p2[i], p2[i] = t;
3173 if (req_comp && req_comp != target) {
3174 out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
3175 if (out == NULL)
return out;
3180 if (comp) *comp = s->img_n;
3187 static int stbi__tga_info(stbi__context *s,
int *x,
int *y,
int *comp)
3189 int tga_w, tga_h, tga_comp;
3199 if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11))
return 0;
3201 tga_w = stbi__get16le(s);
3206 tga_h = stbi__get16le(s);
3213 if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) {
3220 if (comp) *comp = tga_comp / 8;
3224 static int stbi__tga_test(stbi__context *s)
3230 if ( sz > 1 )
return 0;
3232 if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) )
return 0;
3238 if ( stbi__get16be(s) < 1 )
return 0;
3239 if ( stbi__get16be(s) < 1 )
return 0;
3241 if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) )
3249 static stbi_uc *stbi__tga_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
3252 int tga_offset = stbi__get8(s);
3253 int tga_indexed = stbi__get8(s);
3254 int tga_image_type = stbi__get8(s);
3256 int tga_palette_start = stbi__get16le(s);
3257 int tga_palette_len = stbi__get16le(s);
3258 int tga_palette_bits = stbi__get8(s);
3259 int tga_x_origin = stbi__get16le(s);
3260 int tga_y_origin = stbi__get16le(s);
3261 int tga_width = stbi__get16le(s);
3262 int tga_height = stbi__get16le(s);
3263 int tga_bits_per_pixel = stbi__get8(s);
3264 int tga_comp = tga_bits_per_pixel / 8;
3265 int tga_inverted = stbi__get8(s);
3267 unsigned char *tga_data;
3268 unsigned char *tga_palette = NULL;
3270 unsigned char raw_data[4];
3272 int RLE_repeating = 0;
3273 int read_next_pixel = 1;
3276 if ( tga_image_type >= 8 )
3278 tga_image_type -= 8;
3282 tga_inverted = 1 - ((tga_inverted >> 5) & 1);
3286 (tga_width < 1) || (tga_height < 1) ||
3287 (tga_image_type < 1) || (tga_image_type > 3) ||
3288 ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
3289 (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
3298 tga_comp = tga_palette_bits / 8;
3304 if (comp) *comp = tga_comp;
3306 tga_data = (
unsigned char*)malloc( tga_width * tga_height * tga_comp );
3307 if (!tga_data)
return stbi__errpuc(
"outofmem",
"Out of memory");
3310 stbi__skip(s, tga_offset );
3312 if ( !tga_indexed && !tga_is_RLE) {
3313 for (i=0; i < tga_height; ++i) {
3314 int y = tga_inverted ? tga_height -i - 1 : i;
3315 stbi_uc *tga_row = tga_data + y*tga_width*tga_comp;
3316 stbi__getn(s, tga_row, tga_width * tga_comp);
3323 stbi__skip(s, tga_palette_start );
3325 tga_palette = (
unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
3328 return stbi__errpuc(
"outofmem",
"Out of memory");
3330 if (!stbi__getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {
3333 return stbi__errpuc(
"bad palette",
"Corrupt TGA");
3337 for (i=0; i < tga_width * tga_height; ++i)
3342 if ( RLE_count == 0 )
3345 int RLE_cmd = stbi__get8(s);
3346 RLE_count = 1 + (RLE_cmd & 127);
3347 RLE_repeating = RLE_cmd >> 7;
3348 read_next_pixel = 1;
3349 }
else if ( !RLE_repeating )
3351 read_next_pixel = 1;
3355 read_next_pixel = 1;
3358 if ( read_next_pixel )
3364 int pal_idx = stbi__get8(s);
3365 if ( pal_idx >= tga_palette_len )
3370 pal_idx *= tga_bits_per_pixel / 8;
3371 for (j = 0; j*8 < tga_bits_per_pixel; ++j)
3373 raw_data[j] = tga_palette[pal_idx+j];
3378 for (j = 0; j*8 < tga_bits_per_pixel; ++j)
3380 raw_data[j] = stbi__get8(s);
3384 read_next_pixel = 0;
3388 for (j = 0; j < tga_comp; ++j)
3389 tga_data[i*tga_comp+j] = raw_data[j];
3397 for (j = 0; j*2 < tga_height; ++j)
3399 int index1 = j * tga_width * tga_comp;
3400 int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
3401 for (i = tga_width * tga_comp; i > 0; --i)
3403 unsigned char temp = tga_data[index1];
3404 tga_data[index1] = tga_data[index2];
3405 tga_data[index2] = temp;
3412 if ( tga_palette != NULL )
3414 free( tga_palette );
3421 unsigned char* tga_pixel = tga_data;
3422 for (i=0; i < tga_width * tga_height; ++i)
3424 unsigned char temp = tga_pixel[0];
3425 tga_pixel[0] = tga_pixel[2];
3426 tga_pixel[2] = temp;
3427 tga_pixel += tga_comp;
3432 if (req_comp && req_comp != tga_comp)
3433 tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
3437 tga_palette_start = tga_palette_len = tga_palette_bits =
3438 tga_x_origin = tga_y_origin = 0;
3446 static int stbi__psd_test(stbi__context *s)
3448 int r = (stbi__get32be(s) == 0x38425053);
3453 static stbi_uc *stbi__psd_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
3456 int channelCount, compression;
3457 int channel, i,
count, len;
3462 if (stbi__get32be(s) != 0x38425053)
3463 return stbi__errpuc(
"not PSD",
"Corrupt PSD image");
3466 if (stbi__get16be(s) != 1)
3467 return stbi__errpuc(
"wrong version",
"Unsupported version of PSD image");
3473 channelCount = stbi__get16be(s);
3474 if (channelCount < 0 || channelCount > 16)
3475 return stbi__errpuc(
"wrong channel count",
"Unsupported number of channels in PSD image");
3478 h = stbi__get32be(s);
3479 w = stbi__get32be(s);
3482 if (stbi__get16be(s) != 8)
3483 return stbi__errpuc(
"unsupported bit depth",
"PSD bit depth is not 8 bit");
3495 if (stbi__get16be(s) != 3)
3496 return stbi__errpuc(
"wrong color format",
"PSD is not in RGB color format");
3499 stbi__skip(s,stbi__get32be(s) );
3502 stbi__skip(s, stbi__get32be(s) );
3505 stbi__skip(s, stbi__get32be(s) );
3511 compression = stbi__get16be(s);
3512 if (compression > 1)
3513 return stbi__errpuc(
"bad compression",
"PSD has an unknown compression format");
3516 out = (
stbi_uc *) malloc(4 * w*h);
3517 if (!out)
return stbi__errpuc(
"outofmem",
"Out of memory");
3535 stbi__skip(s, h * channelCount * 2 );
3538 for (channel = 0; channel < 4; channel++) {
3542 if (channel >= channelCount) {
3544 for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
3548 while (count < pixelCount) {
3549 len = stbi__get8(s);
3552 }
else if (len < 128) {
3561 }
else if (len > 128) {
3567 val = stbi__get8(s);
3584 for (channel = 0; channel < 4; channel++) {
3588 if (channel > channelCount) {
3590 for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
3593 for (i = 0; i < pixelCount; i++)
3594 *p = stbi__get8(s), p += 4;
3599 if (req_comp && req_comp != 4) {
3600 out = stbi__convert_format(out, 4, req_comp, w, h);
3601 if (out == NULL)
return out;
3604 if (comp) *comp = channelCount;
3618 static int stbi__pic_is4(stbi__context *s,
const char *str)
3622 if (stbi__get8(s) != (
stbi_uc)str[i])
3628 static int stbi__pic_test_core(stbi__context *s)
3632 if (!stbi__pic_is4(s,
"\x53\x80\xF6\x34"))
3638 if (!stbi__pic_is4(s,
"PICT"))
3649 static stbi_uc *stbi__readval(stbi__context *s,
int channel,
stbi_uc *dest)
3653 for (i=0; i<4; ++i, mask>>=1) {
3654 if (channel & mask) {
3655 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"PIC file too short");
3656 dest[i]=stbi__get8(s);
3663 static void stbi__copyval(
int channel,
stbi_uc *dest,
const stbi_uc *src)
3667 for (i=0;i<4; ++i, mask>>=1)
3674 int act_comp=0,num_packets=0,
y,chained;
3675 stbi__pic_packet packets[10];
3680 stbi__pic_packet *packet;
3682 if (num_packets==
sizeof(packets)/
sizeof(packets[0]))
3683 return stbi__errpuc(
"bad format",
"too many packets");
3685 packet = &packets[num_packets++];
3687 chained = stbi__get8(s);
3688 packet->size = stbi__get8(s);
3689 packet->type = stbi__get8(s);
3690 packet->channel = stbi__get8(s);
3692 act_comp |= packet->channel;
3694 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (reading packets)");
3695 if (packet->size != 8)
return stbi__errpuc(
"bad format",
"packet isn't 8bpp");
3698 *comp = (act_comp & 0x10 ? 4 : 3);
3703 for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
3704 stbi__pic_packet *packet = &packets[packet_idx];
3705 stbi_uc *dest = result+y*width*4;
3707 switch (packet->type) {
3709 return stbi__errpuc(
"bad format",
"packet has bad compression type");
3714 for(x=0;x<
width;++
x, dest+=4)
3715 if (!stbi__readval(s,packet->channel,dest))
3727 count=stbi__get8(s);
3728 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (pure read count)");
3733 if (!stbi__readval(s,packet->channel,value))
return 0;
3735 for(i=0; i<
count; ++i,dest+=4)
3736 stbi__copyval(packet->channel,dest,value);
3745 int count = stbi__get8(s), i;
3746 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (mixed read count)");
3753 count = stbi__get16be(s);
3757 return stbi__errpuc(
"bad file",
"scanline overrun");
3759 if (!stbi__readval(s,packet->channel,value))
3762 for(i=0;i<
count;++i, dest += 4)
3763 stbi__copyval(packet->channel,dest,value);
3766 if (count>left)
return stbi__errpuc(
"bad file",
"scanline overrun");
3768 for(i=0;i<
count;++i, dest+=4)
3769 if (!stbi__readval(s,packet->channel,dest))
3783 static stbi_uc *stbi__pic_load(stbi__context *s,
int *px,
int *py,
int *comp,
int req_comp)
3788 for (i=0; i<92; ++i)
3791 x = stbi__get16be(s);
3792 y = stbi__get16be(s);
3793 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (pic header)");
3794 if ((1 << 28) / x < y)
return stbi__errpuc(
"too large",
"Image too large to decode");
3801 result = (
stbi_uc *) malloc(x*y*4);
3802 memset(result, 0xff, x*y*4);
3804 if (!stbi__pic_load_core(s,x,y,comp, result)) {
3810 if (req_comp == 0) req_comp = *comp;
3811 result=stbi__convert_format(result,4,req_comp,x,y);
3816 static int stbi__pic_test(stbi__context *s)
3818 int r = stbi__pic_test_core(s);
3836 int flags, bgindex, ratio, transparent, eflags;
3839 stbi__gif_lzw codes[4096];
3843 int start_x, start_y;
3849 static int stbi__gif_test_raw(stbi__context *s)
3852 if (stbi__get8(s) !=
'G' || stbi__get8(s) !=
'I' || stbi__get8(s) !=
'F' || stbi__get8(s) !=
'8')
return 0;
3854 if (sz !=
'9' && sz !=
'7')
return 0;
3855 if (stbi__get8(s) !=
'a')
return 0;
3859 static int stbi__gif_test(stbi__context *s)
3861 int r = stbi__gif_test_raw(s);
3866 static void stbi__gif_parse_colortable(stbi__context *s,
stbi_uc pal[256][4],
int num_entries,
int transp)
3869 for (i=0; i < num_entries; ++i) {
3870 pal[i][2] = stbi__get8(s);
3871 pal[i][1] = stbi__get8(s);
3872 pal[i][0] = stbi__get8(s);
3873 pal[i][3] = transp ? 0 : 255;
3877 static int stbi__gif_header(stbi__context *s, stbi__gif *g,
int *comp,
int is_info)
3880 if (stbi__get8(s) !=
'G' || stbi__get8(s) !=
'I' || stbi__get8(s) !=
'F' || stbi__get8(s) !=
'8')
3881 return stbi__err(
"not GIF",
"Corrupt GIF");
3883 version = stbi__get8(s);
3884 if (version !=
'7' && version !=
'9')
return stbi__err(
"not GIF",
"Corrupt GIF");
3885 if (stbi__get8(s) !=
'a')
return stbi__err(
"not GIF",
"Corrupt GIF");
3887 stbi__g_failure_reason =
"";
3888 g->w = stbi__get16le(s);
3889 g->h = stbi__get16le(s);
3890 g->flags = stbi__get8(s);
3891 g->bgindex = stbi__get8(s);
3892 g->ratio = stbi__get8(s);
3893 g->transparent = -1;
3895 if (comp != 0) *comp = 4;
3897 if (is_info)
return 1;
3899 if (g->flags & 0x80)
3900 stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
3905 static int stbi__gif_info_raw(stbi__context *s,
int *x,
int *y,
int *comp)
3908 if (!stbi__gif_header(s, &g, comp, 1)) {
3917 static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
3923 if (g->codes[code].prefix >= 0)
3924 stbi__out_gif_code(g, g->codes[code].prefix);
3926 if (g->cur_y >= g->max_y)
return;
3928 p = &g->out[g->cur_x + g->cur_y];
3929 c = &g->color_table[g->codes[code].suffix * 4];
3939 if (g->cur_x >= g->max_x) {
3940 g->cur_x = g->start_x;
3941 g->cur_y += g->step;
3943 while (g->cur_y >= g->max_y && g->parse > 0) {
3944 g->step = (1 << g->parse) * g->line_size;
3945 g->cur_y = g->start_y + (g->step >> 1);
3951 static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
3954 stbi__int32 len, code;
3956 stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
3959 lzw_cs = stbi__get8(s);
3960 clear = 1 << lzw_cs;
3962 codesize = lzw_cs + 1;
3963 codemask = (1 << codesize) - 1;
3966 for (code = 0; code < clear; code++) {
3967 g->codes[code].prefix = -1;
3968 g->codes[code].first = (
stbi_uc) code;
3969 g->codes[code].suffix = (
stbi_uc) code;
3978 if (valid_bits < codesize) {
3980 len = stbi__get8(s);
3985 bits |= (stbi__int32) stbi__get8(s) << valid_bits;
3988 stbi__int32 code = bits & codemask;
3990 valid_bits -= codesize;
3992 if (code == clear) {
3993 codesize = lzw_cs + 1;
3994 codemask = (1 << codesize) - 1;
3998 }
else if (code == clear + 1) {
4000 while ((len = stbi__get8(s)) > 0)
4003 }
else if (code <= avail) {
4004 if (first)
return stbi__errpuc(
"no clear code",
"Corrupt GIF");
4007 p = &g->codes[avail++];
4008 if (avail > 4096)
return stbi__errpuc(
"too many codes",
"Corrupt GIF");
4009 p->prefix = (stbi__int16) oldcode;
4010 p->first = g->codes[oldcode].first;
4011 p->suffix = (code == avail) ? p->first : g->codes[code].first;
4012 }
else if (code == avail)
4013 return stbi__errpuc(
"illegal code in raster",
"Corrupt GIF");
4015 stbi__out_gif_code(g, (stbi__uint16) code);
4017 if ((avail & codemask) == 0 && avail <= 0x0FFF) {
4019 codemask = (1 << codesize) - 1;
4024 return stbi__errpuc(
"illegal code in raster",
"Corrupt GIF");
4030 static void stbi__fill_gif_background(stbi__gif *g)
4033 stbi_uc *c = g->pal[g->bgindex];
4035 for (i = 0; i < g->w * g->h * 4; i += 4) {
4045 static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g,
int *comp,
int req_comp)
4051 if (!stbi__gif_header(s, g, comp,0))
return 0;
4052 g->out = (
stbi_uc *) malloc(4 * g->w * g->h);
4053 if (g->out == 0)
return stbi__errpuc(
"outofmem",
"Out of memory");
4054 stbi__fill_gif_background(g);
4057 if (((g->eflags & 0x1C) >> 2) == 3) {
4059 g->out = (
stbi_uc *) malloc(4 * g->w * g->h);
4060 if (g->out == 0)
return stbi__errpuc(
"outofmem",
"Out of memory");
4061 memcpy(g->out, old_out, g->w*g->h*4);
4066 switch (stbi__get8(s)) {
4069 stbi__int32
x,
y,
w,
h;
4072 x = stbi__get16le(s);
4073 y = stbi__get16le(s);
4074 w = stbi__get16le(s);
4075 h = stbi__get16le(s);
4076 if (((x + w) > (g->w)) || ((y + h) > (g->h)))
4077 return stbi__errpuc(
"bad Image Descriptor",
"Corrupt GIF");
4079 g->line_size = g->w * 4;
4081 g->start_y = y * g->line_size;
4082 g->max_x = g->start_x + w * 4;
4083 g->max_y = g->start_y + h * g->line_size;
4084 g->cur_x = g->start_x;
4085 g->cur_y = g->start_y;
4087 g->lflags = stbi__get8(s);
4089 if (g->lflags & 0x40) {
4090 g->step = 8 * g->line_size;
4093 g->step = g->line_size;
4097 if (g->lflags & 0x80) {
4098 stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
4099 g->color_table = (
stbi_uc *) g->lpal;
4100 }
else if (g->flags & 0x80) {
4101 for (i=0; i < 256; ++i)
4103 if (g->transparent >= 0 && (g->eflags & 0x01))
4104 g->pal[g->transparent][3] = 0;
4105 g->color_table = (
stbi_uc *) g->pal;
4107 return stbi__errpuc(
"missing color table",
"Corrupt GIF");
4109 o = stbi__process_gif_raster(s, g);
4110 if (o == NULL)
return NULL;
4112 if (req_comp && req_comp != 4)
4113 o = stbi__convert_format(o, 4, req_comp, g->w, g->h);
4120 if (stbi__get8(s) == 0xF9) {
4121 len = stbi__get8(s);
4123 g->eflags = stbi__get8(s);
4125 g->transparent = stbi__get8(s);
4131 while ((len = stbi__get8(s)) != 0)
4140 return stbi__errpuc(
"unknown code",
"Corrupt GIF");
4145 static stbi_uc *stbi__gif_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
4149 memset(&g, 0,
sizeof(g));
4151 u = stbi__gif_load_next(s, &g, comp, req_comp);
4152 if (u == (
stbi_uc *) s) u = 0;
4161 static int stbi__gif_info(stbi__context *s,
int *x,
int *y,
int *comp)
4163 return stbi__gif_info_raw(s,x,y,comp);
4171 static int stbi__hdr_test_core(stbi__context *s)
4173 const char *signature =
"#?RADIANCE\n";
4175 for (i=0; signature[i]; ++i)
4176 if (stbi__get8(s) != signature[i])
4181 static int stbi__hdr_test(stbi__context* s)
4183 int r = stbi__hdr_test_core(s);
4188 #define STBI__HDR_BUFLEN 1024
4189 static char *stbi__hdr_gettoken(stbi__context *z,
char *buffer)
4194 c = (char) stbi__get8(z);
4196 while (!stbi__at_eof(z) && c !=
'\n') {
4198 if (len == STBI__HDR_BUFLEN-1) {
4200 while (!stbi__at_eof(z) && stbi__get8(z) !=
'\n')
4204 c = (char) stbi__get8(z);
4211 static void stbi__hdr_convert(
float *output,
stbi_uc *input,
int req_comp)
4213 if ( input[3] != 0 ) {
4216 f1 = (
float) ldexp(1.0f, input[3] - (
int)(128 + 8));
4218 output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
4220 output[0] = input[0] * f1;
4221 output[1] = input[1] * f1;
4222 output[2] = input[2] * f1;
4224 if (req_comp == 2) output[1] = 1;
4225 if (req_comp == 4) output[3] = 1;
4228 case 4: output[3] = 1;
4229 case 3: output[0] = output[1] = output[2] = 0;
4231 case 2: output[1] = 1;
4232 case 1: output[0] = 0;
4238 static float *stbi__hdr_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
4240 char buffer[STBI__HDR_BUFLEN];
4248 int i, j, k, c1,c2, z;
4252 if (strcmp(stbi__hdr_gettoken(s,buffer),
"#?RADIANCE") != 0)
4253 return stbi__errpf(
"not HDR",
"Corrupt HDR image");
4257 token = stbi__hdr_gettoken(s,buffer);
4258 if (token[0] == 0)
break;
4259 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
4262 if (!valid)
return stbi__errpf(
"unsupported format",
"Unsupported HDR format");
4266 token = stbi__hdr_gettoken(s,buffer);
4267 if (strncmp(token,
"-Y ", 3))
return stbi__errpf(
"unsupported data layout",
"Unsupported HDR format");
4269 height = (
int) strtol(token, &token, 10);
4270 while (*token ==
' ') ++token;
4271 if (strncmp(token,
"+X ", 3))
return stbi__errpf(
"unsupported data layout",
"Unsupported HDR format");
4273 width = (
int) strtol(token, NULL, 10);
4278 if (comp) *comp = 3;
4279 if (req_comp == 0) req_comp = 3;
4282 hdr_data = (
float *) malloc(height * width * req_comp *
sizeof(
float));
4288 for (j=0; j <
height; ++j) {
4289 for (i=0; i <
width; ++i) {
4292 stbi__getn(s, rgbe, 4);
4293 stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
4300 for (j = 0; j <
height; ++j) {
4303 len = stbi__get8(s);
4304 if (c1 != 2 || c2 != 2 || (len & 0x80)) {
4311 rgbe[3] = (
stbi_uc) stbi__get8(s);
4312 stbi__hdr_convert(hdr_data, rgbe, req_comp);
4316 goto main_decode_loop;
4319 len |= stbi__get8(s);
4320 if (len != width) { free(hdr_data); free(scanline);
return stbi__errpf(
"invalid decoded scanline length",
"corrupt HDR"); }
4321 if (scanline == NULL) scanline = (
stbi_uc *) malloc(width * 4);
4323 for (k = 0; k < 4; ++k) {
4326 count = stbi__get8(s);
4329 value = stbi__get8(s);
4331 for (z = 0; z <
count; ++z)
4332 scanline[i++ * 4 + k] = value;
4335 for (z = 0; z <
count; ++z)
4336 scanline[i++ * 4 + k] = stbi__get8(s);
4340 for (i=0; i <
width; ++i)
4341 stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
4349 static int stbi__hdr_info(stbi__context *s,
int *x,
int *y,
int *comp)
4351 char buffer[STBI__HDR_BUFLEN];
4355 if (strcmp(stbi__hdr_gettoken(s,buffer),
"#?RADIANCE") != 0) {
4361 token = stbi__hdr_gettoken(s,buffer);
4362 if (token[0] == 0)
break;
4363 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
4370 token = stbi__hdr_gettoken(s,buffer);
4371 if (strncmp(token,
"-Y ", 3)) {
4376 *y = (
int) strtol(token, &token, 10);
4377 while (*token ==
' ') ++token;
4378 if (strncmp(token,
"+X ", 3)) {
4383 *x = (
int) strtol(token, NULL, 10);
4387 #endif // STBI_NO_HDR
4389 static int stbi__bmp_info(stbi__context *s,
int *x,
int *y,
int *comp)
4392 if (stbi__get8(s) !=
'B' || stbi__get8(s) !=
'M') {
4397 hsz = stbi__get32le(s);
4398 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) {
4403 *x = stbi__get16le(s);
4404 *y = stbi__get16le(s);
4406 *x = stbi__get32le(s);
4407 *y = stbi__get32le(s);
4409 if (stbi__get16le(s) != 1) {
4413 *comp = stbi__get16le(s) / 8;
4417 static int stbi__psd_info(stbi__context *s,
int *x,
int *y,
int *comp)
4420 if (stbi__get32be(s) != 0x38425053) {
4424 if (stbi__get16be(s) != 1) {
4429 channelCount = stbi__get16be(s);
4430 if (channelCount < 0 || channelCount > 16) {
4434 *y = stbi__get32be(s);
4435 *x = stbi__get32be(s);
4436 if (stbi__get16be(s) != 8) {
4440 if (stbi__get16be(s) != 3) {
4448 static int stbi__pic_info(stbi__context *s,
int *x,
int *y,
int *comp)
4450 int act_comp=0,num_packets=0,chained;
4451 stbi__pic_packet packets[10];
4455 *x = stbi__get16be(s);
4456 *y = stbi__get16be(s);
4457 if (stbi__at_eof(s))
return 0;
4458 if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
4466 stbi__pic_packet *packet;
4468 if (num_packets==
sizeof(packets)/
sizeof(packets[0]))
4471 packet = &packets[num_packets++];
4472 chained = stbi__get8(s);
4473 packet->size = stbi__get8(s);
4474 packet->type = stbi__get8(s);
4475 packet->channel = stbi__get8(s);
4476 act_comp |= packet->channel;
4478 if (stbi__at_eof(s)) {
4482 if (packet->size != 8) {
4488 *comp = (act_comp & 0x10 ? 4 : 3);
4493 static int stbi__info_main(stbi__context *s,
int *x,
int *y,
int *comp)
4495 if (stbi__jpeg_info(s, x, y, comp))
4497 if (stbi__png_info(s, x, y, comp))
4499 if (stbi__gif_info(s, x, y, comp))
4501 if (stbi__bmp_info(s, x, y, comp))
4503 if (stbi__psd_info(s, x, y, comp))
4505 if (stbi__pic_info(s, x, y, comp))
4508 if (stbi__hdr_info(s, x, y, comp))
4512 if (stbi__tga_info(s, x, y, comp))
4514 return stbi__err(
"unknown image type",
"Image not of any known type, or corrupt");
4517 #ifndef STBI_NO_STDIO
4520 FILE *f = stbi__fopen(filename,
"rb");
4522 if (!f)
return stbi__err(
"can't fopen",
"Unable to open file");
4532 long pos = ftell(f);
4533 stbi__start_file(&s, f);
4534 r = stbi__info_main(&s,x,y,comp);
4535 fseek(f,pos,SEEK_SET);
4538 #endif // !STBI_NO_STDIO
4543 stbi__start_mem(&s,buffer,len);
4544 return stbi__info_main(&s,x,y,comp);
4551 return stbi__info_main(&s,x,y,comp);
4554 #endif // STB_IMAGE_IMPLEMENTATION
STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp)
STBIDEF void stbi_hdr_to_ldr_gamma(float gamma)
GLboolean GLboolean GLboolean GLboolean a
STBIDEF float * stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
STBIDEF stbi_uc * stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
STBIDEF float * stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
GLint GLsizei GLsizei height
GLenum GLuint GLintptr offset
STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
GLint GLfloat GLfloat GLfloat v2
STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
STBIDEF stbi_uc * stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
STBIDEF int stbi_is_hdr(char const *filename)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei count
STBIDEF void stbi_ldr_to_hdr_gamma(float gamma)
STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
STBIDEF char * stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen)
STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
STBIDEF const char * stbi_failure_reason(void)
STBIDEF void stbi_ldr_to_hdr_scale(float scale)
STBIDEF float * stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
STBIDEF int stbi_is_hdr_from_file(FILE *f)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLfloat GLfloat GLfloat w
GLint GLenum GLsizei GLsizei GLsizei depth
STBIDEF stbi_uc * stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
GLfloat GLfloat GLfloat GLfloat h
STBIDEF void stbi_hdr_to_ldr_scale(float scale)
STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp)
GLenum GLsizei GLsizei GLint * values
STBIDEF char * stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
GLenum GLuint GLintptr GLsizeiptr size
STBIDEF void stbi_image_free(void *retval_from_stbi_load)
STBIDEF char * stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen)
GLint GLint GLint GLint GLint x
GLenum GLuint GLenum GLsizei length
STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
const void GLsizei GLsizei stride
STBIDEF char * stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
typedef void(GL_APIENTRYP PFNGLBLENDBARRIERKHRPROC)(void)
STBIDEF float * stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
GLboolean GLboolean GLboolean b
STBIDEF stbi_uc * stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)