74 #ifndef STBI_INCLUDE_STB_IMAGE_H
75 #define STBI_INCLUDE_STB_IMAGE_H
191 #ifndef STBI_NO_STDIO
193 #if defined(_MSC_VER) && _MSC_VER >= 1400
194 #define _CRT_SECURE_NO_WARNINGS // suppress warnings about fopen()
195 #pragma warning(push)
196 #pragma warning(disable:4996) // suppress even more warnings about fopen()
199 #endif // STBI_NO_STDIO
201 #define STBI_VERSION 1
219 #ifdef STB_IMAGE_STATIC
220 #define STBIDEF static
222 #define STBIDEF extern
236 #ifndef STBI_NO_STDIO
254 #ifndef STBI_NO_STDIO
266 #endif // STBI_NO_HDR
271 #ifndef STBI_NO_STDIO
274 #endif // STBI_NO_STDIO
288 #ifndef STBI_NO_STDIO
319 typedef void (*stbi_idct_8x8)(
stbi_uc *out,
int out_stride,
short data[64],
unsigned short *dequantize);
332 STBIDEF void stbi_install_idct(stbi_idct_8x8 func);
333 STBIDEF void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
344 #endif // STBI_INCLUDE_STB_IMAGE_H
346 #ifdef STB_IMAGE_IMPLEMENTATION
353 #ifndef STBI_NO_STDIO
364 #define stbi_inline inline
369 #define stbi_inline __forceinline
374 typedef unsigned short stbi__uint16;
375 typedef signed short stbi__int16;
376 typedef unsigned int stbi__uint32;
377 typedef signed int stbi__int32;
380 typedef uint16_t stbi__uint16;
381 typedef int16_t stbi__int16;
383 typedef int32_t stbi__int32;
387 typedef unsigned char validate_uint32[
sizeof(stbi__uint32)==4 ? 1 : -1];
390 #define STBI_NOTUSED(v) (void)(v)
392 #define STBI_NOTUSED(v) (void)sizeof(v)
396 #define STBI_HAS_LROTL
399 #ifdef STBI_HAS_LROTL
400 #define stbi_lrot(x,y) _lrotl(x,y)
402 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
413 stbi__uint32 img_x, img_y;
414 int img_n, img_out_n;
419 int read_from_callbacks;
423 stbi_uc *img_buffer, *img_buffer_end;
428 static void stbi__refill_buffer(stbi__context *
s);
434 s->read_from_callbacks = 0;
435 s->img_buffer = s->img_buffer_original = (
stbi_uc *) buffer;
436 s->img_buffer_end = (
stbi_uc *) buffer+len;
443 s->io_user_data = user;
444 s->buflen =
sizeof(s->buffer_start);
445 s->read_from_callbacks = 1;
446 s->img_buffer_original = s->buffer_start;
447 stbi__refill_buffer(s);
450 #ifndef STBI_NO_STDIO
452 static int stbi__stdio_read(
void *user,
char *
data,
int size)
454 return (
int) fread(data,1,size,(FILE*) user);
457 static void stbi__stdio_skip(
void *user,
int n)
459 fseek((FILE*) user, n, SEEK_CUR);
462 static int stbi__stdio_eof(
void *user)
464 return feof((FILE*) user);
474 static void stbi__start_file(stbi__context *
s, FILE *
f)
476 stbi__start_callbacks(s, &stbi__stdio_callbacks, (
void *) f);
481 #endif // !STBI_NO_STDIO
483 static void stbi__rewind(stbi__context *s)
488 s->img_buffer = s->img_buffer_original;
491 static int stbi__jpeg_test(stbi__context *s);
492 static stbi_uc *stbi__jpeg_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
493 static int stbi__jpeg_info(stbi__context *s,
int *
x,
int *
y,
int *comp);
494 static int stbi__png_test(stbi__context *s);
495 static stbi_uc *stbi__png_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
496 static int stbi__png_info(stbi__context *s,
int *
x,
int *
y,
int *comp);
497 static int stbi__bmp_test(stbi__context *s);
498 static stbi_uc *stbi__bmp_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
499 static int stbi__tga_test(stbi__context *s);
500 static stbi_uc *stbi__tga_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
501 static int stbi__tga_info(stbi__context *s,
int *
x,
int *
y,
int *comp);
502 static int stbi__psd_test(stbi__context *s);
503 static stbi_uc *stbi__psd_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
505 static int stbi__hdr_test(stbi__context *s);
506 static float *stbi__hdr_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
508 static int stbi__pic_test(stbi__context *s);
509 static stbi_uc *stbi__pic_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
510 static int stbi__gif_test(stbi__context *s);
511 static stbi_uc *stbi__gif_load(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp);
512 static int stbi__gif_info(stbi__context *s,
int *
x,
int *
y,
int *comp);
516 static const char *stbi__g_failure_reason;
520 return stbi__g_failure_reason;
523 static int stbi__err(
const char *str)
525 stbi__g_failure_reason = str;
533 #ifdef STBI_NO_FAILURE_STRINGS
534 #define stbi__err(x,y) 0
535 #elif defined(STBI_FAILURE_USERMSG)
536 #define stbi__err(x,y) stbi__err(y)
538 #define stbi__err(x,y) stbi__err(x)
541 #define stbi__errpf(x,y) ((float *) (stbi__err(x,y)?NULL:NULL))
542 #define stbi__errpuc(x,y) ((unsigned char *) (stbi__err(x,y)?NULL:NULL))
546 free(retval_from_stbi_load);
550 static float *stbi__ldr_to_hdr(
stbi_uc *data,
int x,
int y,
int comp);
551 static stbi_uc *stbi__hdr_to_ldr(
float *data,
int x,
int y,
int comp);
554 static unsigned char *stbi_load_main(stbi__context *s,
int *
x,
int *
y,
int *comp,
int req_comp)
556 if (stbi__jpeg_test(s))
return stbi__jpeg_load(s,x,y,comp,req_comp);
557 if (stbi__png_test(s))
return stbi__png_load(s,x,y,comp,req_comp);
558 if (stbi__bmp_test(s))
return stbi__bmp_load(s,x,y,comp,req_comp);
559 if (stbi__gif_test(s))
return stbi__gif_load(s,x,y,comp,req_comp);
560 if (stbi__psd_test(s))
return stbi__psd_load(s,x,y,comp,req_comp);
561 if (stbi__pic_test(s))
return stbi__pic_load(s,x,y,comp,req_comp);
564 if (stbi__hdr_test(s)) {
565 float *hdr = stbi__hdr_load(s, x,y,comp,req_comp);
566 return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
571 if (stbi__tga_test(s))
572 return stbi__tga_load(s,x,y,comp,req_comp);
573 return stbi__errpuc(
"unknown image type",
"Image not of any known type, or corrupt");
576 #ifndef STBI_NO_STDIO
577 STBIDEF unsigned char *
stbi_load(
char const *filename,
int *x,
int *y,
int *comp,
int req_comp)
579 FILE *f = fopen(filename,
"rb");
581 if (!f)
return stbi__errpuc(
"can't fopen",
"Unable to open file");
591 stbi__start_file(&s,f);
592 result = stbi_load_main(&s,x,y,comp,req_comp);
595 fseek(f, - (
int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
604 stbi__start_mem(&s,buffer,len);
605 return stbi_load_main(&s,x,y,comp,req_comp);
612 return stbi_load_main(&s,x,y,comp,req_comp);
617 float *stbi_loadf_main(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
621 if (stbi__hdr_test(s))
622 return stbi__hdr_load(s,x,y,comp,req_comp);
624 data = stbi_load_main(s, x, y, comp, req_comp);
626 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
627 return stbi__errpf(
"unknown image type",
"Image not of any known type, or corrupt");
633 stbi__start_mem(&s,buffer,len);
634 return stbi_loadf_main(&s,x,y,comp,req_comp);
641 return stbi_loadf_main(&s,x,y,comp,req_comp);
644 #ifndef STBI_NO_STDIO
645 float *
stbi_loadf(
char const *filename,
int *x,
int *y,
int *comp,
int req_comp)
647 FILE *f = fopen(filename,
"rb");
649 if (!f)
return stbi__errpf(
"can't fopen",
"Unable to open file");
658 stbi__start_file(&s,f);
659 return stbi_loadf_main(&s,x,y,comp,req_comp);
661 #endif // !STBI_NO_STDIO
663 #endif // !STBI_NO_HDR
673 stbi__start_mem(&s,buffer,len);
674 return stbi__hdr_test(&s);
676 STBI_NOTUSED(buffer);
682 #ifndef STBI_NO_STDIO
685 FILE *f = fopen(filename,
"rb");
698 stbi__start_file(&s,f);
699 return stbi__hdr_test(&s);
704 #endif // !STBI_NO_STDIO
711 return stbi__hdr_test(&s);
718 static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;
719 static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;
741 static void stbi__refill_buffer(stbi__context *s)
743 int n = (s->io.read)(s->io_user_data,(
char*)s->buffer_start,s->buflen);
747 s->read_from_callbacks = 0;
748 s->img_buffer = s->buffer_start;
749 s->img_buffer_end = s->buffer_start+1;
752 s->img_buffer = s->buffer_start;
753 s->img_buffer_end = s->buffer_start +
n;
757 stbi_inline
static stbi_uc stbi__get8(stbi__context *s)
759 if (s->img_buffer < s->img_buffer_end)
760 return *s->img_buffer++;
761 if (s->read_from_callbacks) {
762 stbi__refill_buffer(s);
763 return *s->img_buffer++;
768 stbi_inline
static int stbi__at_eof(stbi__context *s)
771 if (!(s->io.eof)(s->io_user_data))
return 0;
774 if (s->read_from_callbacks == 0)
return 1;
777 return s->img_buffer >= s->img_buffer_end;
780 static void stbi__skip(stbi__context *s,
int n)
783 int blen = (
int) (s->img_buffer_end - s->img_buffer);
785 s->img_buffer = s->img_buffer_end;
786 (s->io.skip)(s->io_user_data, n - blen);
793 static int stbi__getn(stbi__context *s,
stbi_uc *
buffer,
int n)
796 int blen = (
int) (s->img_buffer_end - s->img_buffer);
800 memcpy(buffer, s->img_buffer, blen);
802 count = (s->io.read)(s->io_user_data, (
char*) buffer + blen, n - blen);
803 res = (count == (n-blen));
804 s->img_buffer = s->img_buffer_end;
809 if (s->img_buffer+
n <= s->img_buffer_end) {
810 memcpy(buffer, s->img_buffer, n);
817 static int stbi__get16be(stbi__context *s)
819 int z = stbi__get8(s);
820 return (z << 8) + stbi__get8(s);
823 static stbi__uint32 stbi__get32be(stbi__context *s)
825 stbi__uint32 z = stbi__get16be(s);
826 return (z << 16) + stbi__get16be(s);
829 static int stbi__get16le(stbi__context *s)
831 int z = stbi__get8(s);
832 return z + (stbi__get8(s) << 8);
835 static stbi__uint32 stbi__get32le(stbi__context *s)
837 stbi__uint32 z = stbi__get16le(s);
838 return z + (stbi__get16le(s) << 16);
852 static stbi_uc stbi__compute_y(
int r,
int g,
int b)
854 return (
stbi_uc) (((r*77) + (g*150) + (29*
b)) >> 8);
857 static unsigned char *stbi__convert_format(
unsigned char *data,
int img_n,
int req_comp,
unsigned int x,
unsigned int y)
862 if (req_comp == img_n)
return data;
863 assert(req_comp >= 1 && req_comp <= 4);
865 good = (
unsigned char *) malloc(req_comp * x * y);
868 return stbi__errpuc(
"outofmem",
"Out of memory");
871 for (j=0; j < (
int) y; ++j) {
872 unsigned char *
src = data + j * x * img_n ;
873 unsigned char *dest = good + j * x * req_comp;
875 #define COMBO(a,b) ((a)*8+(b))
876 #define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
879 switch (COMBO(img_n, req_comp)) {
880 CASE(1,2) dest[0]=src[0], dest[1]=255; break;
881 CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;
882 CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;
883 CASE(2,1) dest[0]=src[0]; break;
884 CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;
885 CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;
886 CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;
887 CASE(3,1) dest[0]=stbi__compute_y(src[0],src[1],src[2]); break;
888 CASE(3,2) dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
889 CASE(4,1) dest[0]=stbi__compute_y(src[0],src[1],src[2]); break;
890 CASE(4,2) dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;
891 CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;
902 static float *stbi__ldr_to_hdr(
stbi_uc *data,
int x,
int y,
int comp)
905 float *
output = (
float *) malloc(x * y * comp *
sizeof(
float));
906 if (output == NULL) { free(data);
return stbi__errpf(
"outofmem",
"Out of memory"); }
908 if (comp & 1) n = comp;
else n = comp-1;
909 for (i=0; i < x*
y; ++
i) {
910 for (k=0; k <
n; ++k) {
911 output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
913 if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
919 #define stbi__float2int(x) ((int) (x))
920 static stbi_uc *stbi__hdr_to_ldr(
float *data,
int x,
int y,
int comp)
924 if (output == NULL) { free(data);
return stbi__errpuc(
"outofmem",
"Out of memory"); }
926 if (comp & 1) n = comp;
else n = comp-1;
927 for (i=0; i < x*
y; ++
i) {
928 for (k=0; k <
n; ++k) {
929 float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;
931 if (z > 255) z = 255;
932 output[i*comp + k] = (
stbi_uc) stbi__float2int(z);
935 float z = data[i*comp+k] * 255 + 0.5f;
937 if (z > 255) z = 255;
938 output[i*comp + k] = (
stbi_uc) stbi__float2int(z);
974 #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
980 stbi__uint16 code[256];
983 unsigned int maxcode[18];
990 unsigned short dequant2[4][64];
993 stbi__huffman huff_dc[4];
994 stbi__huffman huff_ac[4];
998 int img_h_max, img_v_max;
999 int img_mcu_x, img_mcu_y;
1000 int img_mcu_w, img_mcu_h;
1017 stbi__uint32 code_buffer;
1019 unsigned char marker;
1022 int scan_n,
order[4];
1023 int restart_interval, todo;
1026 static int stbi__build_huffman(stbi__huffman *
h,
int *
count)
1030 for (i=0; i < 16; ++
i)
1031 for (j=0; j < count[
i]; ++j)
1032 h->size[k++] = (
stbi_uc) (i+1);
1038 for(j=1; j <= 16; ++j) {
1040 h->delta[j] = k - code;
1041 if (h->size[k] == j) {
1042 while (h->size[k] == j)
1043 h->code[k++] = (stbi__uint16) (code++);
1044 if (code-1 >= (1 << j))
return stbi__err(
"bad code lengths",
"Corrupt JPEG");
1047 h->maxcode[j] = code << (16-j);
1050 h->maxcode[j] = 0xffffffff;
1053 memset(h->fast, 255, 1 << FAST_BITS);
1054 for (i=0; i < k; ++
i) {
1056 if (s <= FAST_BITS) {
1057 int c = h->code[
i] << (FAST_BITS-
s);
1058 int m = 1 << (FAST_BITS-
s);
1059 for (j=0; j <
m; ++j) {
1067 static void stbi__grow_buffer_unsafe(stbi__jpeg *j)
1070 int b = j->nomore ? 0 : stbi__get8(j->s);
1072 int c = stbi__get8(j->s);
1074 j->marker = (
unsigned char) c;
1079 j->code_buffer |= b << (24 - j->code_bits);
1081 }
while (j->code_bits <= 24);
1085 static stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
1088 stbi_inline
static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
1093 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
1097 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
1101 if (s > j->code_bits)
1103 j->code_buffer <<=
s;
1105 return h->values[k];
1114 temp = j->code_buffer >> 16;
1115 for (k=FAST_BITS+1 ; ; ++k)
1116 if (temp < h->maxcode[k])
1124 if (k > j->code_bits)
1128 c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
1129 assert((((j->code_buffer) >> (32 - h->size[
c])) & stbi__bmask[h->size[
c]]) == h->code[c]);
1133 j->code_buffer <<= k;
1134 return h->values[
c];
1139 stbi_inline
static int stbi__extend_receive(stbi__jpeg *j,
int n)
1141 unsigned int m = 1 << (n-1);
1143 if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
1146 k = stbi_lrot(j->code_buffer, n);
1147 j->code_buffer = k & ~stbi__bmask[
n];
1148 k &= stbi__bmask[
n];
1151 k = (j->code_buffer >> (32 -
n)) & stbi__bmask[
n];
1153 j->code_buffer <<=
n;
1159 return (-1 << n) + k + 1;
1166 static stbi_uc stbi__jpeg_dezigzag[64+15] =
1168 0, 1, 8, 16, 9, 2, 3, 10,
1169 17, 24, 32, 25, 18, 11, 4, 5,
1170 12, 19, 26, 33, 40, 48, 41, 34,
1171 27, 20, 13, 6, 7, 14, 21, 28,
1172 35, 42, 49, 56, 57, 50, 43, 36,
1173 29, 22, 15, 23, 30, 37, 44, 51,
1174 58, 59, 52, 45, 38, 31, 39, 46,
1175 53, 60, 61, 54, 47, 55, 62, 63,
1177 63, 63, 63, 63, 63, 63, 63, 63,
1178 63, 63, 63, 63, 63, 63, 63
1182 static int stbi__jpeg_decode_block(stbi__jpeg *j,
short data[64], stbi__huffman *hdc, stbi__huffman *hac,
int b)
1185 int t = stbi__jpeg_huff_decode(j, hdc);
1186 if (t < 0)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
1189 memset(data,0,64*
sizeof(data[0]));
1191 diff = t ? stbi__extend_receive(j, t) : 0;
1192 dc = j->img_comp[
b].dc_pred + diff;
1193 j->img_comp[
b].dc_pred = dc;
1194 data[0] = (short) dc;
1200 int rs = stbi__jpeg_huff_decode(j, hac);
1201 if (rs < 0)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
1205 if (rs != 0xf0)
break;
1210 data[stbi__jpeg_dezigzag[k++]] = (short) stbi__extend_receive(j,s);
1217 stbi_inline
static stbi_uc stbi__clamp(
int x)
1220 if ((
unsigned int) x > 255) {
1221 if (x < 0)
return 0;
1222 if (x > 255)
return 255;
1227 #define stbi__f2f(x) (int) (((x) * 4096 + 0.5))
1228 #define stbi__fsh(x) ((x) << 12)
1231 #define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
1232 int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
1235 p1 = (p2+p3) * stbi__f2f(0.5411961f); \
1236 t2 = p1 + p3*stbi__f2f(-1.847759065f); \
1237 t3 = p1 + p2*stbi__f2f( 0.765366865f); \
1240 t0 = stbi__fsh(p2+p3); \
1241 t1 = stbi__fsh(p2-p3); \
1254 p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
1255 t0 = t0*stbi__f2f( 0.298631336f); \
1256 t1 = t1*stbi__f2f( 2.053119869f); \
1257 t2 = t2*stbi__f2f( 3.072711026f); \
1258 t3 = t3*stbi__f2f( 1.501321110f); \
1259 p1 = p5 + p1*stbi__f2f(-0.899976223f); \
1260 p2 = p5 + p2*stbi__f2f(-2.562915447f); \
1261 p3 = p3*stbi__f2f(-1.961570560f); \
1262 p4 = p4*stbi__f2f(-0.390180644f); \
1269 typedef unsigned short stbi_dequantize_t;
1271 typedef stbi_uc stbi_dequantize_t;
1275 static void stbi__idct_block(
stbi_uc *out,
int out_stride,
short data[64], stbi_dequantize_t *dequantize)
1278 stbi_dequantize_t *dq = dequantize;
1283 for (i=0; i < 8; ++
i,++
d,++dq, ++
v) {
1285 if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
1286 && d[40]==0 && d[48]==0 && d[56]==0) {
1291 int dcterm = d[0] * dq[0] << 2;
1292 v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
1294 STBI__IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],
1295 d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])
1298 x0 += 512; x1 += 512; x2 += 512; x3 += 512;
1299 v[ 0] = (x0+t3) >> 10;
1300 v[56] = (x0-t3) >> 10;
1301 v[ 8] = (x1+t2) >> 10;
1302 v[48] = (x1-t2) >> 10;
1303 v[16] = (x2+t1) >> 10;
1304 v[40] = (x2-t1) >> 10;
1305 v[24] = (x3+t0) >> 10;
1306 v[32] = (x3-t0) >> 10;
1310 for (i=0, v=val, o=out; i < 8; ++
i,v+=8,o+=out_stride) {
1312 STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
1319 x0 += 65536 + (128<<17);
1320 x1 += 65536 + (128<<17);
1321 x2 += 65536 + (128<<17);
1322 x3 += 65536 + (128<<17);
1325 o[0] = stbi__clamp((x0+t3) >> 17);
1326 o[7] = stbi__clamp((x0-t3) >> 17);
1327 o[1] = stbi__clamp((x1+t2) >> 17);
1328 o[6] = stbi__clamp((x1-t2) >> 17);
1329 o[2] = stbi__clamp((x2+t1) >> 17);
1330 o[5] = stbi__clamp((x2-t1) >> 17);
1331 o[3] = stbi__clamp((x3+t0) >> 17);
1332 o[4] = stbi__clamp((x3-t0) >> 17);
1337 static stbi_idct_8x8 stbi__idct_installed = stbi__idct_block;
1339 STBIDEF void stbi_install_idct(stbi_idct_8x8 func)
1341 stbi__idct_installed = func;
1345 #define STBI__MARKER_none 0xff
1349 static stbi_uc stbi__get_marker(stbi__jpeg *j)
1352 if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none;
return x; }
1353 x = stbi__get8(j->s);
1354 if (x != 0xff)
return STBI__MARKER_none;
1356 x = stbi__get8(j->s);
1362 #define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
1366 static void stbi__jpeg_reset(stbi__jpeg *j)
1371 j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
1372 j->marker = STBI__MARKER_none;
1373 j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
1378 static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
1380 stbi__jpeg_reset(z);
1381 if (z->scan_n == 1) {
1384 __declspec(align(16))
1387 int n = z->order[0];
1392 int w = (z->img_comp[
n].x+7) >> 3;
1393 int h = (z->img_comp[
n].y+7) >> 3;
1394 for (j=0; j <
h; ++j) {
1395 for (i=0; i <
w; ++
i) {
1396 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;
1398 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]);
1400 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]);
1403 if (--z->todo <= 0) {
1404 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
1407 if (!STBI__RESTART(z->marker))
return 1;
1408 stbi__jpeg_reset(z);
1415 for (j=0; j < z->img_mcu_y; ++j) {
1416 for (i=0; i < z->img_mcu_x; ++
i) {
1418 for (k=0; k < z->scan_n; ++k) {
1419 int n = z->order[k];
1422 for (y=0; y < z->img_comp[
n].v; ++
y) {
1423 for (x=0; x < z->img_comp[
n].h; ++
x) {
1424 int x2 = (i*z->img_comp[
n].h +
x)*8;
1425 int y2 = (j*z->img_comp[
n].v +
y)*8;
1426 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;
1428 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]);
1430 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]);
1437 if (--z->todo <= 0) {
1438 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
1441 if (!STBI__RESTART(z->marker))
return 1;
1442 stbi__jpeg_reset(z);
1450 static int stbi__process_marker(stbi__jpeg *z,
int m)
1454 case STBI__MARKER_none:
1455 return stbi__err(
"expected marker",
"Corrupt JPEG");
1458 return stbi__err(
"progressive jpeg",
"JPEG format not supported (progressive)");
1461 if (stbi__get16be(z->s) != 4)
return stbi__err(
"bad DRI len",
"Corrupt JPEG");
1462 z->restart_interval = stbi__get16be(z->s);
1466 L = stbi__get16be(z->s)-2;
1468 int q = stbi__get8(z->s);
1471 if (p != 0)
return stbi__err(
"bad DQT type",
"Corrupt JPEG");
1472 if (t > 3)
return stbi__err(
"bad DQT table",
"Corrupt JPEG");
1473 for (i=0; i < 64; ++
i)
1474 z->dequant[t][stbi__jpeg_dezigzag[i]] = stbi__get8(z->s);
1476 for (i=0; i < 64; ++
i)
1477 z->dequant2[t][i] = z->dequant[t][i];
1484 L = stbi__get16be(z->s)-2;
1487 int sizes[16],
i,n=0;
1488 int q = stbi__get8(z->s);
1491 if (tc > 1 || th > 3)
return stbi__err(
"bad DHT header",
"Corrupt JPEG");
1492 for (i=0; i < 16; ++
i) {
1493 sizes[
i] = stbi__get8(z->s);
1498 if (!stbi__build_huffman(z->huff_dc+th, sizes))
return 0;
1499 v = z->huff_dc[th].values;
1501 if (!stbi__build_huffman(z->huff_ac+th, sizes))
return 0;
1502 v = z->huff_ac[th].values;
1504 for (i=0; i <
n; ++
i)
1505 v[i] = stbi__get8(z->s);
1511 if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
1512 stbi__skip(z->s, stbi__get16be(z->s)-2);
1519 static int stbi__process_scan_header(stbi__jpeg *z)
1522 int Ls = stbi__get16be(z->s);
1523 z->scan_n = stbi__get8(z->s);
1524 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");
1525 if (Ls != 6+2*z->scan_n)
return stbi__err(
"bad stbi__SOS len",
"Corrupt JPEG");
1526 for (i=0; i < z->scan_n; ++
i) {
1527 int id = stbi__get8(z->s), which;
1528 int q = stbi__get8(z->s);
1529 for (which = 0; which < z->s->img_n; ++which)
1530 if (z->img_comp[which].id ==
id)
1532 if (which == z->s->img_n)
return 0;
1533 z->img_comp[which].hd = q >> 4;
if (z->img_comp[which].hd > 3)
return stbi__err(
"bad DC huff",
"Corrupt JPEG");
1534 z->img_comp[which].ha = q & 15;
if (z->img_comp[which].ha > 3)
return stbi__err(
"bad AC huff",
"Corrupt JPEG");
1535 z->order[
i] = which;
1537 if (stbi__get8(z->s) != 0)
return stbi__err(
"bad stbi__SOS",
"Corrupt JPEG");
1539 if (stbi__get8(z->s) != 0)
return stbi__err(
"bad stbi__SOS",
"Corrupt JPEG");
1544 static int stbi__process_frame_header(stbi__jpeg *z,
int scan)
1546 stbi__context *s = z->s;
1547 int Lf,
p,
i,
q, h_max=1,v_max=1,
c;
1548 Lf = stbi__get16be(s);
if (Lf < 11)
return stbi__err(
"bad stbi__SOF len",
"Corrupt JPEG");
1549 p = stbi__get8(s);
if (p != 8)
return stbi__err(
"only 8-bit",
"JPEG format not supported: 8-bit only");
1550 s->img_y = stbi__get16be(s);
if (s->img_y == 0)
return stbi__err(
"no header height",
"JPEG format not supported: delayed height");
1551 s->img_x = stbi__get16be(s);
if (s->img_x == 0)
return stbi__err(
"0 width",
"Corrupt JPEG");
1553 if (c != 3 && c != 1)
return stbi__err(
"bad component count",
"Corrupt JPEG");
1555 for (i=0; i <
c; ++
i) {
1556 z->img_comp[
i].data = NULL;
1557 z->img_comp[
i].linebuf = NULL;
1560 if (Lf != 8+3*s->img_n)
return stbi__err(
"bad stbi__SOF len",
"Corrupt JPEG");
1562 for (i=0; i < s->img_n; ++
i) {
1563 z->img_comp[
i].id = stbi__get8(s);
1564 if (z->img_comp[i].id != i+1)
1565 if (z->img_comp[i].id != i)
1566 return stbi__err(
"bad component ID",
"Corrupt JPEG");
1568 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");
1569 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");
1570 z->img_comp[
i].tq = stbi__get8(s);
if (z->img_comp[i].tq > 3)
return stbi__err(
"bad TQ",
"Corrupt JPEG");
1573 if (scan != SCAN_load)
return 1;
1575 if ((1 << 30) / s->img_x / s->img_n < s->img_y)
return stbi__err(
"too large",
"Image too large to decode");
1577 for (i=0; i < s->img_n; ++
i) {
1578 if (z->img_comp[i].h > h_max) h_max = z->img_comp[
i].h;
1579 if (z->img_comp[i].v > v_max) v_max = z->img_comp[
i].v;
1583 z->img_h_max = h_max;
1584 z->img_v_max = v_max;
1585 z->img_mcu_w = h_max * 8;
1586 z->img_mcu_h = v_max * 8;
1587 z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
1588 z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
1590 for (i=0; i < s->img_n; ++
i) {
1592 z->img_comp[
i].x = (s->img_x * z->img_comp[
i].h + h_max-1) / h_max;
1593 z->img_comp[
i].y = (s->img_y * z->img_comp[
i].v + v_max-1) / v_max;
1598 z->img_comp[
i].w2 = z->img_mcu_x * z->img_comp[
i].h * 8;
1599 z->img_comp[
i].h2 = z->img_mcu_y * z->img_comp[
i].v * 8;
1600 z->img_comp[
i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
1601 if (z->img_comp[i].raw_data == NULL) {
1602 for(--i; i >= 0; --
i) {
1603 free(z->img_comp[i].raw_data);
1604 z->img_comp[
i].data = NULL;
1606 return stbi__err(
"outofmem",
"Out of memory");
1609 z->img_comp[
i].data = (
stbi_uc*) (((
size_t) z->img_comp[i].raw_data + 15) & ~15);
1610 z->img_comp[
i].linebuf = NULL;
1617 #define stbi__DNL(x) ((x) == 0xdc)
1618 #define stbi__SOI(x) ((x) == 0xd8)
1619 #define stbi__EOI(x) ((x) == 0xd9)
1620 #define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1)
1621 #define stbi__SOS(x) ((x) == 0xda)
1623 static int decode_jpeg_header(stbi__jpeg *z,
int scan)
1626 z->marker = STBI__MARKER_none;
1627 m = stbi__get_marker(z);
1628 if (!stbi__SOI(m))
return stbi__err(
"no stbi__SOI",
"Corrupt JPEG");
1629 if (scan == SCAN_type)
return 1;
1630 m = stbi__get_marker(z);
1631 while (!stbi__SOF(m)) {
1632 if (!stbi__process_marker(z,m))
return 0;
1633 m = stbi__get_marker(z);
1634 while (m == STBI__MARKER_none) {
1636 if (stbi__at_eof(z->s))
return stbi__err(
"no stbi__SOF",
"Corrupt JPEG");
1637 m = stbi__get_marker(z);
1640 if (!stbi__process_frame_header(z, scan))
return 0;
1644 static int decode_jpeg_image(stbi__jpeg *j)
1647 j->restart_interval = 0;
1648 if (!decode_jpeg_header(j, SCAN_load))
return 0;
1649 m = stbi__get_marker(j);
1650 while (!stbi__EOI(m)) {
1652 if (!stbi__process_scan_header(j))
return 0;
1653 if (!stbi__parse_entropy_coded_data(j))
return 0;
1654 if (j->marker == STBI__MARKER_none ) {
1656 while (!stbi__at_eof(j->s)) {
1657 int x = stbi__get8(j->s);
1659 j->marker = stbi__get8(j->s);
1661 }
else if (x != 0) {
1668 if (!stbi__process_marker(j, m))
return 0;
1670 m = stbi__get_marker(j);
1680 #define stbi__div4(x) ((stbi_uc) ((x) >> 2))
1685 STBI_NOTUSED(in_far);
1696 for (i=0; i <
w; ++
i)
1697 out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2);
1709 out[0] = out[1] = input[0];
1714 out[1] = stbi__div4(input[0]*3 + input[1] + 2);
1715 for (i=1; i < w-1; ++
i) {
1716 int n = 3*input[
i]+2;
1717 out[i*2+0] = stbi__div4(n+input[i-1]);
1718 out[i*2+1] = stbi__div4(n+input[i+1]);
1720 out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2);
1721 out[i*2+1] = input[w-1];
1723 STBI_NOTUSED(in_far);
1729 #define stbi__div16(x) ((stbi_uc) ((x) >> 4))
1736 out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
1740 t1 = 3*in_near[0] + in_far[0];
1741 out[0] = stbi__div4(t1+2);
1742 for (i=1; i <
w; ++
i) {
1744 t1 = 3*in_near[
i]+in_far[
i];
1745 out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
1746 out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
1748 out[w*2-1] = stbi__div4(t1+2);
1759 STBI_NOTUSED(in_far);
1760 for (i=0; i <
w; ++
i)
1761 for (j=0; j < hs; ++j)
1762 out[i*hs+j] = in_near[i];
1766 #define float2fixed(x) ((int) ((x) * 65536 + 0.5))
1773 for (i=0; i <
count; ++
i) {
1774 int y_fixed = (y[
i] << 16) + 32768;
1776 int cr = pcr[
i] - 128;
1777 int cb = pcb[
i] - 128;
1778 r = y_fixed + cr*float2fixed(1.40200f);
1779 g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);
1780 b = y_fixed + cb*float2fixed(1.77200f);
1784 if ((
unsigned) r > 255) {
if (r < 0) r = 0;
else r = 255; }
1785 if ((
unsigned) g > 255) {
if (g < 0) g = 0;
else g = 255; }
1786 if ((
unsigned) b > 255) {
if (b < 0) b = 0;
else b = 255; }
1796 static stbi_YCbCr_to_RGB_run stbi__YCbCr_installed = stbi__YCbCr_to_RGB_row;
1798 STBIDEF void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)
1800 stbi__YCbCr_installed = func;
1806 static void stbi__cleanup_jpeg(stbi__jpeg *j)
1809 for (i=0; i < j->s->img_n; ++
i) {
1810 if (j->img_comp[i].data) {
1811 free(j->img_comp[i].raw_data);
1812 j->img_comp[
i].data = NULL;
1814 if (j->img_comp[i].linebuf) {
1815 free(j->img_comp[i].linebuf);
1816 j->img_comp[
i].linebuf = NULL;
1823 resample_row_func resample;
1831 static stbi_uc *load_jpeg_image(stbi__jpeg *z,
int *out_x,
int *out_y,
int *comp,
int req_comp)
1835 if (req_comp < 0 || req_comp > 4)
return stbi__errpuc(
"bad req_comp",
"Internal error");
1839 if (!decode_jpeg_image(z)) { stbi__cleanup_jpeg(z);
return NULL; }
1842 n = req_comp ? req_comp : z->s->img_n;
1844 if (z->s->img_n == 3 && n < 3)
1847 decode_n = z->s->img_n;
1856 stbi__resample res_comp[4];
1858 for (k=0; k < decode_n; ++k) {
1859 stbi__resample *r = &res_comp[k];
1863 z->img_comp[k].linebuf = (
stbi_uc *) malloc(z->s->img_x + 3);
1864 if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z);
return stbi__errpuc(
"outofmem",
"Out of memory"); }
1866 r->hs = z->img_h_max / z->img_comp[k].h;
1867 r->vs = z->img_v_max / z->img_comp[k].v;
1868 r->ystep = r->vs >> 1;
1869 r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
1871 r->line0 = r->line1 = z->img_comp[k].data;
1873 if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
1874 else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2;
1875 else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2;
1876 else if (r->hs == 2 && r->vs == 2) r->resample = stbi__resample_row_hv_2;
1877 else r->resample = stbi__resample_row_generic;
1881 output = (
stbi_uc *) malloc(n * z->s->img_x * z->s->img_y + 1);
1882 if (!output) { stbi__cleanup_jpeg(z);
return stbi__errpuc(
"outofmem",
"Out of memory"); }
1885 for (j=0; j < z->s->img_y; ++j) {
1886 stbi_uc *out = output + n * z->s->img_x * j;
1887 for (k=0; k < decode_n; ++k) {
1888 stbi__resample *r = &res_comp[k];
1889 int y_bot = r->ystep >= (r->vs >> 1);
1890 coutput[k] = r->resample(z->img_comp[k].linebuf,
1891 y_bot ? r->line1 : r->line0,
1892 y_bot ? r->line0 : r->line1,
1894 if (++r->ystep >= r->vs) {
1896 r->line0 = r->line1;
1897 if (++r->ypos < z->img_comp[k].y)
1898 r->line1 += z->img_comp[k].w2;
1903 if (z->s->img_n == 3) {
1905 stbi__YCbCr_installed(out, y, coutput[1], coutput[2], z->s->img_x, n);
1907 stbi__YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s->img_x, n);
1910 for (i=0; i < z->s->img_x; ++
i) {
1911 out[0] = out[1] = out[2] = y[
i];
1918 for (i=0; i < z->s->img_x; ++
i) out[i] = y[i];
1920 for (i=0; i < z->s->img_x; ++
i) *out++ = y[i], *out++ = 255;
1923 stbi__cleanup_jpeg(z);
1924 *out_x = z->s->img_x;
1925 *out_y = z->s->img_y;
1926 if (comp) *comp = z->s->img_n;
1931 static unsigned char *stbi__jpeg_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
1935 return load_jpeg_image(&j, x,y,comp,req_comp);
1938 static int stbi__jpeg_test(stbi__context *s)
1943 r = decode_jpeg_header(&j, SCAN_type);
1948 static int stbi__jpeg_info_raw(stbi__jpeg *j,
int *x,
int *y,
int *comp)
1950 if (!decode_jpeg_header(j, SCAN_header)) {
1951 stbi__rewind( j->s );
1954 if (x) *x = j->s->img_x;
1955 if (y) *y = j->s->img_y;
1956 if (comp) *comp = j->s->img_n;
1960 static int stbi__jpeg_info(stbi__context *s,
int *x,
int *y,
int *comp)
1964 return stbi__jpeg_info_raw(&j, x, y, comp);
1975 #define STBI__ZFAST_BITS 9 // accelerate all cases in default tables
1976 #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
1982 stbi__uint16 fast[1 << STBI__ZFAST_BITS];
1983 stbi__uint16 firstcode[16];
1985 stbi__uint16 firstsymbol[16];
1987 stbi__uint16
value[288];
1990 stbi_inline
static int stbi__bitreverse16(
int n)
1992 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
1993 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
1994 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
1995 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
1999 stbi_inline
static int stbi__bit_reverse(
int v,
int bits)
2004 return stbi__bitreverse16(v) >> (16-bits);
2007 static int stbi__zbuild_huffman(stbi__zhuffman *z,
stbi_uc *sizelist,
int num)
2010 int code, next_code[16], sizes[17];
2013 memset(sizes, 0,
sizeof(sizes));
2014 memset(z->fast, 255,
sizeof(z->fast));
2015 for (i=0; i <
num; ++
i)
2016 ++sizes[sizelist[i]];
2018 for (i=1; i < 16; ++
i)
2019 assert(sizes[i] <= (1 << i));
2021 for (i=1; i < 16; ++
i) {
2022 next_code[
i] = code;
2023 z->firstcode[
i] = (stbi__uint16) code;
2024 z->firstsymbol[
i] = (stbi__uint16) k;
2025 code = (code + sizes[
i]);
2027 if (code-1 >= (1 << i))
return stbi__err(
"bad codelengths",
"Corrupt JPEG");
2028 z->maxcode[
i] = code << (16-
i);
2032 z->maxcode[16] = 0x10000;
2033 for (i=0; i <
num; ++
i) {
2034 int s = sizelist[
i];
2036 int c = next_code[
s] - z->firstcode[
s] + z->firstsymbol[
s];
2038 z->value[
c] = (stbi__uint16) i;
2039 if (s <= STBI__ZFAST_BITS) {
2040 int k = stbi__bit_reverse(next_code[s],s);
2041 while (k < (1 << STBI__ZFAST_BITS)) {
2042 z->fast[k] = (stbi__uint16) c;
2060 stbi_uc *zbuffer, *zbuffer_end;
2062 stbi__uint32 code_buffer;
2069 stbi__zhuffman z_length, z_distance;
2072 stbi_inline
static stbi_uc stbi__zget8(stbi__zbuf *z)
2074 if (z->zbuffer >= z->zbuffer_end)
return 0;
2075 return *z->zbuffer++;
2078 static void stbi__fill_bits(stbi__zbuf *z)
2081 assert(z->code_buffer < (1U << z->num_bits));
2082 z->code_buffer |= stbi__zget8(z) << z->num_bits;
2084 }
while (z->num_bits <= 24);
2087 stbi_inline
static unsigned int stbi__zreceive(stbi__zbuf *z,
int n)
2090 if (z->num_bits < n) stbi__fill_bits(z);
2091 k = z->code_buffer & ((1 <<
n) - 1);
2092 z->code_buffer >>=
n;
2097 stbi_inline
static int stbi__zhuffman_decode(stbi__zbuf *
a, stbi__zhuffman *z)
2100 if (a->num_bits < 16) stbi__fill_bits(a);
2101 b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
2104 a->code_buffer >>=
s;
2111 k = stbi__bit_reverse(a->code_buffer, 16);
2112 for (s=STBI__ZFAST_BITS+1; ; ++
s)
2113 if (k < z->maxcode[s])
2115 if (s == 16)
return -1;
2117 b = (k >> (16-
s)) - z->firstcode[
s] + z->firstsymbol[
s];
2118 assert(z->size[b] == s);
2119 a->code_buffer >>=
s;
2124 static int stbi__zexpand(stbi__zbuf *z,
int n)
2128 if (!z->z_expandable)
return stbi__err(
"output buffer limit",
"Corrupt PNG");
2129 cur = (
int) (z->zout - z->zout_start);
2130 limit = (
int) (z->zout_end - z->zout_start);
2131 while (cur + n > limit)
2133 q = (
char *) realloc(z->zout_start, limit);
2134 if (q == NULL)
return stbi__err(
"outofmem",
"Out of memory");
2137 z->zout_end = q +
limit;
2141 static int stbi__zlength_base[31] = {
2142 3,4,5,6,7,8,9,10,11,13,
2143 15,17,19,23,27,31,35,43,51,59,
2144 67,83,99,115,131,163,195,227,258,0,0 };
2146 static int stbi__zlength_extra[31]=
2147 { 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 };
2149 static int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
2150 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
2152 static int stbi__zdist_extra[32] =
2153 { 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};
2155 static int stbi__parse_huffman_block(stbi__zbuf *a)
2158 int z = stbi__zhuffman_decode(a, &a->z_length);
2160 if (z < 0)
return stbi__err(
"bad huffman code",
"Corrupt PNG");
2161 if (a->zout >= a->zout_end)
if (!stbi__zexpand(a, 1))
return 0;
2162 *a->zout++ = (char) z;
2166 if (z == 256)
return 1;
2168 len = stbi__zlength_base[
z];
2169 if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
2170 z = stbi__zhuffman_decode(a, &a->z_distance);
2171 if (z < 0)
return stbi__err(
"bad huffman code",
"Corrupt PNG");
2172 dist = stbi__zdist_base[
z];
2173 if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
2174 if (a->zout - a->zout_start < dist)
return stbi__err(
"bad dist",
"Corrupt PNG");
2175 if (a->zout + len > a->zout_end)
if (!stbi__zexpand(a, len))
return 0;
2176 p = (
stbi_uc *) (a->zout - dist);
2183 static int stbi__compute_huffman_codes(stbi__zbuf *a)
2185 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 };
2186 stbi__zhuffman z_codelength;
2191 int hlit = stbi__zreceive(a,5) + 257;
2192 int hdist = stbi__zreceive(a,5) + 1;
2193 int hclen = stbi__zreceive(a,4) + 4;
2195 memset(codelength_sizes, 0,
sizeof(codelength_sizes));
2196 for (i=0; i < hclen; ++
i) {
2197 int s = stbi__zreceive(a,3);
2198 codelength_sizes[length_dezigzag[
i]] = (
stbi_uc) s;
2200 if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19))
return 0;
2203 while (n < hlit + hdist) {
2204 int c = stbi__zhuffman_decode(a, &z_codelength);
2205 assert(c >= 0 && c < 19);
2209 c = stbi__zreceive(a,2)+3;
2210 memset(lencodes+n, lencodes[n-1], c);
2212 }
else if (c == 17) {
2213 c = stbi__zreceive(a,3)+3;
2214 memset(lencodes+n, 0, c);
2218 c = stbi__zreceive(a,7)+11;
2219 memset(lencodes+n, 0, c);
2223 if (n != hlit+hdist)
return stbi__err(
"bad codelengths",
"Corrupt PNG");
2224 if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit))
return 0;
2225 if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist))
return 0;
2229 static int stbi__parse_uncomperssed_block(stbi__zbuf *a)
2233 if (a->num_bits & 7)
2234 stbi__zreceive(a, a->num_bits & 7);
2237 while (a->num_bits > 0) {
2238 header[k++] = (
stbi_uc) (a->code_buffer & 255);
2239 a->code_buffer >>= 8;
2242 assert(a->num_bits == 0);
2245 header[k++] = stbi__zget8(a);
2246 len = header[1] * 256 + header[0];
2247 nlen = header[3] * 256 + header[2];
2248 if (nlen != (len ^ 0xffff))
return stbi__err(
"zlib corrupt",
"Corrupt PNG");
2249 if (a->zbuffer + len > a->zbuffer_end)
return stbi__err(
"read past buffer",
"Corrupt PNG");
2250 if (a->zout + len > a->zout_end)
2251 if (!stbi__zexpand(a, len))
return 0;
2252 memcpy(a->zout, a->zbuffer, len);
2258 static int stbi__parse_zlib_header(stbi__zbuf *a)
2260 int cmf = stbi__zget8(a);
2263 int flg = stbi__zget8(a);
2264 if ((cmf*256+flg) % 31 != 0)
return stbi__err(
"bad zlib header",
"Corrupt PNG");
2265 if (flg & 32)
return stbi__err(
"no preset dict",
"Corrupt PNG");
2266 if (cm != 8)
return stbi__err(
"bad compression",
"Corrupt PNG");
2272 static stbi_uc stbi__zdefault_length[288], stbi__zdefault_distance[32];
2273 static void stbi__init_zdefaults(
void)
2276 for (i=0; i <= 143; ++
i) stbi__zdefault_length[i] = 8;
2277 for ( ; i <= 255; ++
i) stbi__zdefault_length[i] = 9;
2278 for ( ; i <= 279; ++
i) stbi__zdefault_length[i] = 7;
2279 for ( ; i <= 287; ++
i) stbi__zdefault_length[i] = 8;
2281 for (i=0; i <= 31; ++
i) stbi__zdefault_distance[i] = 5;
2284 static int stbi__parse_zlib(stbi__zbuf *a,
int parse_header)
2288 if (!stbi__parse_zlib_header(a))
return 0;
2292 final = stbi__zreceive(a,1);
2293 type = stbi__zreceive(a,2);
2295 if (!stbi__parse_uncomperssed_block(a))
return 0;
2296 }
else if (type == 3) {
2301 if (!stbi__zdefault_distance[31]) stbi__init_zdefaults();
2302 if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288))
return 0;
2303 if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32))
return 0;
2305 if (!stbi__compute_huffman_codes(a))
return 0;
2307 if (!stbi__parse_huffman_block(a))
return 0;
2313 static int stbi__do_zlib(stbi__zbuf *a,
char *obuf,
int olen,
int exp,
int parse_header)
2315 a->zout_start = obuf;
2317 a->zout_end = obuf + olen;
2318 a->z_expandable = exp;
2320 return stbi__parse_zlib(a, parse_header);
2326 char *p = (
char *) malloc(initial_size);
2327 if (p == NULL)
return NULL;
2328 a.zbuffer = (
stbi_uc *) buffer;
2329 a.zbuffer_end = (
stbi_uc *) buffer + len;
2330 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
2331 if (outlen) *outlen = (
int) (a.zout - a.zout_start);
2332 return a.zout_start;
2347 char *p = (
char *) malloc(initial_size);
2348 if (p == NULL)
return NULL;
2349 a.zbuffer = (
stbi_uc *) buffer;
2350 a.zbuffer_end = (
stbi_uc *) buffer + len;
2351 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
2352 if (outlen) *outlen = (
int) (a.zout - a.zout_start);
2353 return a.zout_start;
2363 a.zbuffer = (
stbi_uc *) ibuffer;
2364 a.zbuffer_end = (
stbi_uc *) ibuffer + ilen;
2365 if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
2366 return (
int) (a.zout - a.zout_start);
2374 char *p = (
char *) malloc(16384);
2375 if (p == NULL)
return NULL;
2376 a.zbuffer = (
stbi_uc *) buffer;
2377 a.zbuffer_end = (
stbi_uc *) buffer+len;
2378 if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
2379 if (outlen) *outlen = (
int) (a.zout - a.zout_start);
2380 return a.zout_start;
2390 a.zbuffer = (
stbi_uc *) ibuffer;
2391 a.zbuffer_end = (
stbi_uc *) ibuffer + ilen;
2392 if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
2393 return (
int) (a.zout - a.zout_start);
2415 #define PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
2417 static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
2420 c.length = stbi__get32be(s);
2421 c.type = stbi__get32be(s);
2425 static int stbi__check_png_header(stbi__context *s)
2427 static stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };
2429 for (i=0; i < 8; ++
i)
2430 if (stbi__get8(s) != png_sig[i])
return stbi__err(
"bad png sig",
"Not a PNG");
2437 stbi_uc *idata, *expanded, *out;
2442 STBI__F_none=0, STBI__F_sub=1, STBI__F_up=2, STBI__F_avg=3, STBI__F_paeth=4,
2443 STBI__F_avg_first, STBI__F_paeth_first
2446 static stbi_uc first_row_filter[5] =
2448 STBI__F_none, STBI__F_sub, STBI__F_none, STBI__F_avg_first, STBI__F_paeth_first
2451 static int stbi__paeth(
int a,
int b,
int c)
2457 if (pa <= pb && pa <= pc)
return a;
2458 if (pb <= pc)
return b;
2462 #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
2465 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)
2467 stbi__context *s = a->s;
2468 stbi__uint32
i,j,
stride = x*out_n;
2470 int img_n = s->img_n;
2471 assert(out_n == s->img_n || out_n == s->img_n+1);
2472 a->out = (
stbi_uc *) malloc(x * y * out_n);
2473 if (!a->out)
return stbi__err(
"outofmem",
"Out of memory");
2474 if (s->img_x == x && s->img_y == y) {
2475 if (raw_len != (img_n * x + 1) * y)
return stbi__err(
"not enough pixels",
"Corrupt PNG");
2477 if (raw_len < (img_n * x + 1) * y)
return stbi__err(
"not enough pixels",
"Corrupt PNG");
2479 for (j=0; j <
y; ++j) {
2480 stbi_uc *cur = a->out + stride*j;
2483 if (filter > 4)
return stbi__err(
"invalid filter",
"Corrupt PNG");
2485 if (j == 0) filter = first_row_filter[
filter];
2487 for (k=0; k < img_n; ++k) {
2489 case STBI__F_none : cur[k] = raw[k];
break;
2490 case STBI__F_sub : cur[k] = raw[k];
break;
2491 case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]);
break;
2492 case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1));
break;
2493 case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0));
break;
2494 case STBI__F_avg_first : cur[k] = raw[k];
break;
2495 case STBI__F_paeth_first: cur[k] = raw[k];
break;
2498 if (img_n != out_n) cur[img_n] = 255;
2503 if (img_n == out_n) {
2506 for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \
2507 for (k=0; k < img_n; ++k)
2509 CASE(STBI__F_none) cur[k] = raw[k]; break;
2510 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-img_n]); break;
2511 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
2512 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-img_n])>>1)); break;
2513 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;
2514 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-img_n] >> 1)); break;
2515 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-img_n],0,0)); break;
2519 assert(img_n+1 == out_n);
2522 for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
2523 for (k=0; k < img_n; ++k)
2525 CASE(STBI__F_none) cur[k] = raw[k]; break;
2526 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-out_n]); break;
2527 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
2528 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-out_n])>>1)); break;
2529 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
2530 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-out_n] >> 1)); break;
2531 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],0,0)); break;
2539 static int stbi__create_png_image(stbi__png *a,
stbi_uc *raw, stbi__uint32 raw_len,
int out_n,
int interlaced)
2544 return stbi__create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y);
2547 final = (
stbi_uc *) malloc(a->s->img_x * a->s->img_y * out_n);
2548 for (p=0; p < 7; ++
p) {
2549 int xorig[] = { 0,4,0,2,0,1,0 };
2550 int yorig[] = { 0,0,4,0,2,0,1 };
2551 int xspc[] = { 8,8,4,4,2,2,1 };
2552 int yspc[] = { 8,8,8,4,4,2,2 };
2555 x = (a->s->img_x - xorig[
p] + xspc[
p]-1) / xspc[p];
2556 y = (a->s->img_y - yorig[
p] + yspc[
p]-1) / yspc[p];
2558 if (!stbi__create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
2562 for (j=0; j <
y; ++j)
2563 for (i=0; i <
x; ++
i)
2564 memcpy(
final + (j*yspc[p]+yorig[p])*a->s->img_x*out_n + (i*xspc[p]+xorig[p])*out_n,
2565 a->out + (j*x+i)*out_n, out_n);
2567 raw += (x*out_n+1)*y;
2568 raw_len -= (x*out_n+1)*y;
2576 static int stbi__compute_transparency(stbi__png *z,
stbi_uc tc[3],
int out_n)
2578 stbi__context *s = z->s;
2579 stbi__uint32
i, pixel_count = s->img_x * s->img_y;
2584 assert(out_n == 2 || out_n == 4);
2587 for (i=0; i < pixel_count; ++
i) {
2588 p[1] = (p[0] == tc[0] ? 0 : 255);
2592 for (i=0; i < pixel_count; ++
i) {
2593 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
2601 static int stbi__expand_png_palette(stbi__png *a,
stbi_uc *
palette,
int len,
int pal_img_n)
2603 stbi__uint32
i, pixel_count = a->s->img_x * a->s->img_y;
2604 stbi_uc *
p, *temp_out, *orig = a->out;
2606 p = (
stbi_uc *) malloc(pixel_count * pal_img_n);
2607 if (p == NULL)
return stbi__err(
"outofmem",
"Out of memory");
2612 if (pal_img_n == 3) {
2613 for (i=0; i < pixel_count; ++
i) {
2616 p[1] = palette[n+1];
2617 p[2] = palette[n+2];
2621 for (i=0; i < pixel_count; ++
i) {
2624 p[1] = palette[n+1];
2625 p[2] = palette[n+2];
2626 p[3] = palette[n+3];
2638 static int stbi__unpremultiply_on_load = 0;
2639 static int stbi__de_iphone_flag = 0;
2643 stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;
2648 stbi__de_iphone_flag = flag_true_if_should_convert;
2651 static void stbi__de_iphone(stbi__png *z)
2653 stbi__context *s = z->s;
2654 stbi__uint32
i, pixel_count = s->img_x * s->img_y;
2657 if (s->img_out_n == 3) {
2658 for (i=0; i < pixel_count; ++
i) {
2665 assert(s->img_out_n == 4);
2666 if (stbi__unpremultiply_on_load) {
2668 for (i=0; i < pixel_count; ++
i) {
2672 p[0] = p[2] * 255 /
a;
2673 p[1] = p[1] * 255 /
a;
2683 for (i=0; i < pixel_count; ++
i) {
2693 static int stbi__parse_png_file(stbi__png *z,
int scan,
int req_comp)
2695 stbi_uc palette[1024], pal_img_n=0;
2697 stbi__uint32 ioff=0, idata_limit=0,
i, pal_len=0;
2698 int first=1,k,interlace=0, is_iphone=0;
2699 stbi__context *s = z->s;
2705 if (!stbi__check_png_header(s))
return 0;
2707 if (scan == SCAN_type)
return 1;
2710 stbi__pngchunk c = stbi__get_chunk_header(s);
2712 case PNG_TYPE(
'C',
'g',
'B',
'I'):
2714 stbi__skip(s, c.length);
2716 case PNG_TYPE(
'I',
'H',
'D',
'R'): {
2718 if (!first)
return stbi__err(
"multiple IHDR",
"Corrupt PNG");
2720 if (c.length != 13)
return stbi__err(
"bad IHDR len",
"Corrupt PNG");
2721 s->img_x = stbi__get32be(s);
if (s->img_x > (1 << 24))
return stbi__err(
"too large",
"Very large image (corrupt?)");
2722 s->img_y = stbi__get32be(s);
if (s->img_y > (1 << 24))
return stbi__err(
"too large",
"Very large image (corrupt?)");
2723 depth = stbi__get8(s);
if (depth != 8)
return stbi__err(
"8bit only",
"PNG not supported: 8-bit only");
2724 color = stbi__get8(s);
if (color > 6)
return stbi__err(
"bad ctype",
"Corrupt PNG");
2725 if (color == 3) pal_img_n = 3;
else if (color & 1)
return stbi__err(
"bad ctype",
"Corrupt PNG");
2726 comp = stbi__get8(s);
if (comp)
return stbi__err(
"bad comp method",
"Corrupt PNG");
2727 filter= stbi__get8(s);
if (filter)
return stbi__err(
"bad filter method",
"Corrupt PNG");
2728 interlace = stbi__get8(s);
if (interlace>1)
return stbi__err(
"bad interlace method",
"Corrupt PNG");
2729 if (!s->img_x || !s->img_y)
return stbi__err(
"0-pixel image",
"Corrupt PNG");
2731 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
2732 if ((1 << 30) / s->img_x / s->img_n < s->img_y)
return stbi__err(
"too large",
"Image too large to decode");
2733 if (scan == SCAN_header)
return 1;
2738 if ((1 << 30) / s->img_x / 4 < s->img_y)
return stbi__err(
"too large",
"Corrupt PNG");
2744 case PNG_TYPE(
'P',
'L',
'T',
'E'): {
2745 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2746 if (c.length > 256*3)
return stbi__err(
"invalid PLTE",
"Corrupt PNG");
2747 pal_len = c.length / 3;
2748 if (pal_len * 3 != c.length)
return stbi__err(
"invalid PLTE",
"Corrupt PNG");
2749 for (i=0; i < pal_len; ++
i) {
2750 palette[i*4+0] = stbi__get8(s);
2751 palette[i*4+1] = stbi__get8(s);
2752 palette[i*4+2] = stbi__get8(s);
2753 palette[i*4+3] = 255;
2758 case PNG_TYPE(
't',
'R',
'N',
'S'): {
2759 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2760 if (z->idata)
return stbi__err(
"tRNS after IDAT",
"Corrupt PNG");
2762 if (scan == SCAN_header) { s->img_n = 4;
return 1; }
2763 if (pal_len == 0)
return stbi__err(
"tRNS before PLTE",
"Corrupt PNG");
2764 if (c.length > pal_len)
return stbi__err(
"bad tRNS len",
"Corrupt PNG");
2766 for (i=0; i < c.length; ++
i)
2767 palette[i*4+3] = stbi__get8(s);
2769 if (!(s->img_n & 1))
return stbi__err(
"tRNS with alpha",
"Corrupt PNG");
2770 if (c.length != (stbi__uint32) s->img_n*2)
return stbi__err(
"bad tRNS len",
"Corrupt PNG");
2772 for (k=0; k < s->img_n; ++k)
2773 tc[k] = (
stbi_uc) (stbi__get16be(s) & 255);
2778 case PNG_TYPE(
'I',
'D',
'A',
'T'): {
2779 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2780 if (pal_img_n && !pal_len)
return stbi__err(
"no PLTE",
"Corrupt PNG");
2781 if (scan == SCAN_header) { s->img_n = pal_img_n;
return 1; }
2782 if (ioff + c.length > idata_limit) {
2784 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
2785 while (ioff + c.length > idata_limit)
2787 p = (
stbi_uc *) realloc(z->idata, idata_limit);
if (p == NULL)
return stbi__err(
"outofmem",
"Out of memory");
2790 if (!stbi__getn(s, z->idata+ioff,c.length))
return stbi__err(
"outofdata",
"Corrupt PNG");
2795 case PNG_TYPE(
'I',
'E',
'N',
'D'): {
2796 stbi__uint32 raw_len;
2797 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2798 if (scan != SCAN_load)
return 1;
2799 if (z->idata == NULL)
return stbi__err(
"no IDAT",
"Corrupt PNG");
2801 if (z->expanded == NULL)
return 0;
2802 free(z->idata); z->idata = NULL;
2803 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
2804 s->img_out_n = s->img_n+1;
2806 s->img_out_n = s->img_n;
2807 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace))
return 0;
2809 if (!stbi__compute_transparency(z, tc, s->img_out_n))
return 0;
2810 if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
2814 s->img_n = pal_img_n;
2815 s->img_out_n = pal_img_n;
2816 if (req_comp >= 3) s->img_out_n = req_comp;
2817 if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
2820 free(z->expanded); z->expanded = NULL;
2826 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
2827 if ((c.type & (1 << 29)) == 0) {
2828 #ifndef STBI_NO_FAILURE_STRINGS
2830 static char invalid_chunk[] =
"XXXX PNG chunk not known";
2831 invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
2832 invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
2833 invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
2834 invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
2836 return stbi__err(invalid_chunk,
"PNG not supported: unknown PNG chunk type");
2838 stbi__skip(s, c.length);
2846 static unsigned char *stbi__do_png(stbi__png *p,
int *x,
int *y,
int *n,
int req_comp)
2848 unsigned char *result=NULL;
2849 if (req_comp < 0 || req_comp > 4)
return stbi__errpuc(
"bad req_comp",
"Internal error");
2850 if (stbi__parse_png_file(p, SCAN_load, req_comp)) {
2853 if (req_comp && req_comp != p->s->img_out_n) {
2854 result = stbi__convert_format(result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
2855 p->s->img_out_n = req_comp;
2856 if (result == NULL)
return result;
2860 if (n) *n = p->s->img_n;
2862 free(p->out); p->out = NULL;
2863 free(p->expanded); p->expanded = NULL;
2864 free(p->idata); p->idata = NULL;
2869 static unsigned char *stbi__png_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
2873 return stbi__do_png(&p, x,y,comp,req_comp);
2876 static int stbi__png_test(stbi__context *s)
2879 r = stbi__check_png_header(s);
2884 static int stbi__png_info_raw(stbi__png *p,
int *x,
int *y,
int *comp)
2886 if (!stbi__parse_png_file(p, SCAN_header, 0)) {
2887 stbi__rewind( p->s );
2890 if (x) *x = p->s->img_x;
2891 if (y) *y = p->s->img_y;
2892 if (comp) *comp = p->s->img_n;
2896 static int stbi__png_info(stbi__context *s,
int *x,
int *y,
int *comp)
2900 return stbi__png_info_raw(&p, x, y, comp);
2904 static int stbi__bmp_test_raw(stbi__context *s)
2908 if (stbi__get8(s) !=
'B')
return 0;
2909 if (stbi__get8(s) !=
'M')
return 0;
2914 sz = stbi__get32le(s);
2915 r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
2919 static int stbi__bmp_test(stbi__context *s)
2921 int r = stbi__bmp_test_raw(s);
2928 static int stbi__high_bit(
unsigned int z)
2931 if (z == 0)
return -1;
2932 if (z >= 0x10000) n += 16, z >>= 16;
2933 if (z >= 0x00100) n += 8, z >>= 8;
2934 if (z >= 0x00010) n += 4, z >>= 4;
2935 if (z >= 0x00004) n += 2, z >>= 2;
2936 if (z >= 0x00002) n += 1, z >>= 1;
2940 static int stbi__bitcount(
unsigned int a)
2942 a = (a & 0x55555555) + ((a >> 1) & 0x55555555);
2943 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
2944 a = (a + (a >> 4)) & 0x0f0f0f0f;
2946 a = (a + (a >> 16));
2950 static int stbi__shiftsigned(
int v,
int shift,
int bits)
2955 if (shift < 0) v <<= -shift;
2967 static stbi_uc *stbi__bmp_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
2970 unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
2972 int psize=0,
i,j,compress=0,
width;
2974 if (stbi__get8(s) !=
'B' || stbi__get8(s) !=
'M')
return stbi__errpuc(
"not BMP",
"Corrupt BMP");
2978 offset = stbi__get32le(s);
2979 hsz = stbi__get32le(s);
2980 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124)
return stbi__errpuc(
"unknown BMP",
"BMP type not supported: unknown");
2982 s->img_x = stbi__get16le(s);
2983 s->img_y = stbi__get16le(s);
2985 s->img_x = stbi__get32le(s);
2986 s->img_y = stbi__get32le(s);
2988 if (stbi__get16le(s) != 1)
return stbi__errpuc(
"bad BMP",
"bad BMP");
2989 bpp = stbi__get16le(s);
2990 if (bpp == 1)
return stbi__errpuc(
"monochrome",
"BMP type not supported: 1-bit");
2991 flip_vertically = ((
int) s->img_y) > 0;
2992 s->img_y = abs((
int) s->img_y);
2995 psize = (offset - 14 - 24) / 3;
2997 compress = stbi__get32le(s);
2998 if (compress == 1 || compress == 2)
return stbi__errpuc(
"BMP RLE",
"BMP type not supported: RLE");
3004 if (hsz == 40 || hsz == 56) {
3011 if (bpp == 16 || bpp == 32) {
3013 if (compress == 0) {
3020 STBI_NOTUSED(fake_a);
3026 }
else if (compress == 3) {
3027 mr = stbi__get32le(s);
3028 mg = stbi__get32le(s);
3029 mb = stbi__get32le(s);
3031 if (mr == mg && mg == mb) {
3033 return stbi__errpuc(
"bad BMP",
"bad BMP");
3036 return stbi__errpuc(
"bad BMP",
"bad BMP");
3039 assert(hsz == 108 || hsz == 124);
3040 mr = stbi__get32le(s);
3041 mg = stbi__get32le(s);
3042 mb = stbi__get32le(s);
3043 ma = stbi__get32le(s);
3045 for (i=0; i < 12; ++
i)
3055 psize = (offset - 14 - hsz) >> 2;
3057 s->img_n = ma ? 4 : 3;
3058 if (req_comp && req_comp >= 3)
3062 out = (
stbi_uc *) malloc(target * s->img_x * s->img_y);
3063 if (!out)
return stbi__errpuc(
"outofmem",
"Out of memory");
3066 if (psize == 0 || psize > 256) { free(out);
return stbi__errpuc(
"invalid",
"Corrupt BMP"); }
3067 for (i=0; i < psize; ++
i) {
3068 pal[
i][2] = stbi__get8(s);
3069 pal[
i][1] = stbi__get8(s);
3070 pal[
i][0] = stbi__get8(s);
3071 if (hsz != 12) stbi__get8(s);
3074 stbi__skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
3075 if (bpp == 4)
width = (s->img_x + 1) >> 1;
3076 else if (bpp == 8)
width = s->img_x;
3077 else { free(out);
return stbi__errpuc(
"bad bpp",
"Corrupt BMP"); }
3079 for (j=0; j < (
int) s->img_y; ++j) {
3080 for (i=0; i < (
int) s->img_x; i += 2) {
3081 int v=stbi__get8(s),
v2=0;
3086 out[z++] = pal[
v][0];
3087 out[z++] = pal[
v][1];
3088 out[z++] = pal[
v][2];
3089 if (target == 4) out[z++] = 255;
3090 if (i+1 == (
int) s->img_x)
break;
3091 v = (bpp == 8) ? stbi__get8(s) :
v2;
3092 out[z++] = pal[
v][0];
3093 out[z++] = pal[
v][1];
3094 out[z++] = pal[
v][2];
3095 if (target == 4) out[z++] = 255;
3100 int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
3103 stbi__skip(s, offset - 14 - hsz);
3104 if (bpp == 24)
width = 3 * s->img_x;
3105 else if (bpp == 16)
width = 2*s->img_x;
3110 }
else if (bpp == 32) {
3111 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
3115 if (!mr || !mg || !mb) { free(out);
return stbi__errpuc(
"bad masks",
"Corrupt BMP"); }
3117 rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);
3118 gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
3119 bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
3120 ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
3122 for (j=0; j < (
int) s->img_y; ++j) {
3124 for (i=0; i < (
int) s->img_x; ++i) {
3126 out[z+2] = stbi__get8(s);
3127 out[z+1] = stbi__get8(s);
3128 out[z+0] = stbi__get8(s);
3130 a = (easy == 2 ? stbi__get8(s) : 255);
3131 if (target == 4) out[z++] =
a;
3134 for (i=0; i < (
int) s->img_x; ++i) {
3135 stbi__uint32 v = (stbi__uint32) (bpp == 16 ? stbi__get16le(s) : stbi__get32le(s));
3137 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount));
3138 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount));
3139 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
3140 a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
3141 if (target == 4) out[z++] = STBI__BYTECAST(a);
3147 if (flip_vertically) {
3149 for (j=0; j < (
int) s->img_y>>1; ++j) {
3151 stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
3152 for (i=0; i < (
int) s->img_x*target; ++i) {
3153 t = p1[
i], p1[
i] = p2[
i], p2[
i] =
t;
3158 if (req_comp && req_comp != target) {
3159 out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
3160 if (out == NULL)
return out;
3165 if (comp) *comp = s->img_n;
3172 static int stbi__tga_info(stbi__context *s,
int *x,
int *y,
int *comp)
3174 int tga_w, tga_h, tga_comp;
3184 if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11))
return 0;
3186 tga_w = stbi__get16le(s);
3191 tga_h = stbi__get16le(s);
3198 if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) {
3205 if (comp) *comp = tga_comp / 8;
3209 static int stbi__tga_test(stbi__context *s)
3215 if ( sz > 1 )
return 0;
3217 if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) )
return 0;
3223 if ( stbi__get16be(s) < 1 )
return 0;
3224 if ( stbi__get16be(s) < 1 )
return 0;
3226 if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) )
3234 static stbi_uc *stbi__tga_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
3237 int tga_offset = stbi__get8(s);
3238 int tga_indexed = stbi__get8(s);
3239 int tga_image_type = stbi__get8(s);
3241 int tga_palette_start = stbi__get16le(s);
3242 int tga_palette_len = stbi__get16le(s);
3243 int tga_palette_bits = stbi__get8(s);
3244 int tga_x_origin = stbi__get16le(s);
3245 int tga_y_origin = stbi__get16le(s);
3246 int tga_width = stbi__get16le(s);
3247 int tga_height = stbi__get16le(s);
3248 int tga_bits_per_pixel = stbi__get8(s);
3249 int tga_comp = tga_bits_per_pixel / 8;
3250 int tga_inverted = stbi__get8(s);
3252 unsigned char *tga_data;
3253 unsigned char *tga_palette = NULL;
3255 unsigned char raw_data[4];
3257 int RLE_repeating = 0;
3258 int read_next_pixel = 1;
3261 if ( tga_image_type >= 8 )
3263 tga_image_type -= 8;
3267 tga_inverted = 1 - ((tga_inverted >> 5) & 1);
3271 (tga_width < 1) || (tga_height < 1) ||
3272 (tga_image_type < 1) || (tga_image_type > 3) ||
3273 ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
3274 (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
3283 tga_comp = tga_palette_bits / 8;
3289 if (comp) *comp = tga_comp;
3291 tga_data = (
unsigned char*)malloc( tga_width * tga_height * tga_comp );
3292 if (!tga_data)
return stbi__errpuc(
"outofmem",
"Out of memory");
3295 stbi__skip(s, tga_offset );
3297 if ( !tga_indexed && !tga_is_RLE) {
3298 for (i=0; i < tga_height; ++
i) {
3299 int y = tga_inverted ? tga_height -i - 1 :
i;
3300 stbi_uc *tga_row = tga_data + y*tga_width*tga_comp;
3301 stbi__getn(s, tga_row, tga_width * tga_comp);
3308 stbi__skip(s, tga_palette_start );
3310 tga_palette = (
unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
3313 return stbi__errpuc(
"outofmem",
"Out of memory");
3315 if (!stbi__getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {
3318 return stbi__errpuc(
"bad palette",
"Corrupt TGA");
3322 for (i=0; i < tga_width * tga_height; ++
i)
3327 if ( RLE_count == 0 )
3330 int RLE_cmd = stbi__get8(s);
3331 RLE_count = 1 + (RLE_cmd & 127);
3332 RLE_repeating = RLE_cmd >> 7;
3333 read_next_pixel = 1;
3334 }
else if ( !RLE_repeating )
3336 read_next_pixel = 1;
3340 read_next_pixel = 1;
3343 if ( read_next_pixel )
3349 int pal_idx = stbi__get8(s);
3350 if ( pal_idx >= tga_palette_len )
3355 pal_idx *= tga_bits_per_pixel / 8;
3356 for (j = 0; j*8 < tga_bits_per_pixel; ++j)
3358 raw_data[j] = tga_palette[pal_idx+j];
3363 for (j = 0; j*8 < tga_bits_per_pixel; ++j)
3365 raw_data[j] = stbi__get8(s);
3369 read_next_pixel = 0;
3373 for (j = 0; j < tga_comp; ++j)
3374 tga_data[i*tga_comp+j] = raw_data[j];
3382 for (j = 0; j*2 < tga_height; ++j)
3384 int index1 = j * tga_width * tga_comp;
3385 int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
3386 for (i = tga_width * tga_comp; i > 0; --
i)
3388 unsigned char temp = tga_data[index1];
3389 tga_data[index1] = tga_data[index2];
3390 tga_data[index2] = temp;
3397 if ( tga_palette != NULL )
3399 free( tga_palette );
3406 unsigned char* tga_pixel = tga_data;
3407 for (i=0; i < tga_width * tga_height; ++
i)
3409 unsigned char temp = tga_pixel[0];
3410 tga_pixel[0] = tga_pixel[2];
3411 tga_pixel[2] = temp;
3412 tga_pixel += tga_comp;
3417 if (req_comp && req_comp != tga_comp)
3418 tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
3422 tga_palette_start = tga_palette_len = tga_palette_bits =
3423 tga_x_origin = tga_y_origin = 0;
3431 static int stbi__psd_test(stbi__context *s)
3433 int r = (stbi__get32be(s) == 0x38425053);
3438 static stbi_uc *stbi__psd_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
3447 if (stbi__get32be(s) != 0x38425053)
3448 return stbi__errpuc(
"not PSD",
"Corrupt PSD image");
3451 if (stbi__get16be(s) != 1)
3452 return stbi__errpuc(
"wrong version",
"Unsupported version of PSD image");
3458 channelCount = stbi__get16be(s);
3459 if (channelCount < 0 || channelCount > 16)
3460 return stbi__errpuc(
"wrong channel count",
"Unsupported number of channels in PSD image");
3463 h = stbi__get32be(s);
3464 w = stbi__get32be(s);
3467 if (stbi__get16be(s) != 8)
3468 return stbi__errpuc(
"unsupported bit depth",
"PSD bit depth is not 8 bit");
3480 if (stbi__get16be(s) != 3)
3481 return stbi__errpuc(
"wrong color format",
"PSD is not in RGB color format");
3484 stbi__skip(s,stbi__get32be(s) );
3487 stbi__skip(s, stbi__get32be(s) );
3490 stbi__skip(s, stbi__get32be(s) );
3496 compression = stbi__get16be(s);
3497 if (compression > 1)
3498 return stbi__errpuc(
"bad compression",
"PSD has an unknown compression format");
3501 out = (
stbi_uc *) malloc(4 * w*h);
3502 if (!out)
return stbi__errpuc(
"outofmem",
"Out of memory");
3520 stbi__skip(s, h * channelCount * 2 );
3523 for (channel = 0; channel < 4; channel++) {
3527 if (channel >= channelCount) {
3529 for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
3533 while (count < pixelCount) {
3534 len = stbi__get8(s);
3537 }
else if (len < 128) {
3546 }
else if (len > 128) {
3552 val = stbi__get8(s);
3569 for (channel = 0; channel < 4; channel++) {
3573 if (channel > channelCount) {
3575 for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
3578 for (i = 0; i < pixelCount; i++)
3579 *p = stbi__get8(s), p += 4;
3584 if (req_comp && req_comp != 4) {
3585 out = stbi__convert_format(out, 4, req_comp, w, h);
3586 if (out == NULL)
return out;
3589 if (comp) *comp = channelCount;
3603 static int stbi__pic_is4(stbi__context *s,
const char *str)
3607 if (stbi__get8(s) != (
stbi_uc)str[i])
3613 static int stbi__pic_test_core(stbi__context *s)
3617 if (!stbi__pic_is4(s,
"\x53\x80\xF6\x34"))
3623 if (!stbi__pic_is4(s,
"PICT"))
3634 static stbi_uc *stbi__readval(stbi__context *s,
int channel,
stbi_uc *dest)
3638 for (i=0; i<4; ++i, mask>>=1) {
3639 if (channel & mask) {
3640 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"PIC file too short");
3641 dest[
i]=stbi__get8(s);
3648 static void stbi__copyval(
int channel,
stbi_uc *dest,
const stbi_uc *src)
3652 for (i=0;i<4; ++i, mask>>=1)
3659 int act_comp=0,num_packets=0,
y,chained;
3660 stbi__pic_packet packets[10];
3665 stbi__pic_packet *packet;
3667 if (num_packets==
sizeof(packets)/
sizeof(packets[0]))
3668 return stbi__errpuc(
"bad format",
"too many packets");
3670 packet = &packets[num_packets++];
3672 chained = stbi__get8(s);
3673 packet->size = stbi__get8(s);
3674 packet->type = stbi__get8(s);
3675 packet->channel = stbi__get8(s);
3677 act_comp |= packet->channel;
3679 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (reading packets)");
3680 if (packet->size != 8)
return stbi__errpuc(
"bad format",
"packet isn't 8bpp");
3683 *comp = (act_comp & 0x10 ? 4 : 3);
3688 for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
3689 stbi__pic_packet *packet = &packets[packet_idx];
3690 stbi_uc *dest = result+y*width*4;
3692 switch (packet->type) {
3694 return stbi__errpuc(
"bad format",
"packet has bad compression type");
3699 for(x=0;x<
width;++
x, dest+=4)
3700 if (!stbi__readval(s,packet->channel,dest))
3712 count=stbi__get8(s);
3713 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (pure read count)");
3718 if (!stbi__readval(s,packet->channel,value))
return 0;
3720 for(i=0; i<
count; ++
i,dest+=4)
3721 stbi__copyval(packet->channel,dest,value);
3730 int count = stbi__get8(s),
i;
3731 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (mixed read count)");
3738 count = stbi__get16be(s);
3742 return stbi__errpuc(
"bad file",
"scanline overrun");
3744 if (!stbi__readval(s,packet->channel,value))
3747 for(i=0;i<
count;++
i, dest += 4)
3748 stbi__copyval(packet->channel,dest,value);
3751 if (count>left)
return stbi__errpuc(
"bad file",
"scanline overrun");
3753 for(i=0;i<
count;++
i, dest+=4)
3754 if (!stbi__readval(s,packet->channel,dest))
3768 static stbi_uc *stbi__pic_load(stbi__context *s,
int *px,
int *py,
int *comp,
int req_comp)
3773 for (i=0; i<92; ++
i)
3776 x = stbi__get16be(s);
3777 y = stbi__get16be(s);
3778 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (pic header)");
3779 if ((1 << 28) / x < y)
return stbi__errpuc(
"too large",
"Image too large to decode");
3786 result = (
stbi_uc *) malloc(x*y*4);
3787 memset(result, 0xff, x*y*4);
3789 if (!stbi__pic_load_core(s,x,y,comp, result)) {
3795 if (req_comp == 0) req_comp = *comp;
3796 result=stbi__convert_format(result,4,req_comp,x,y);
3801 static int stbi__pic_test(stbi__context *s)
3803 int r = stbi__pic_test_core(s);
3821 int flags, bgindex, ratio, transparent, eflags;
3824 stbi__gif_lzw codes[4096];
3828 int start_x, start_y;
3834 static int stbi__gif_test_raw(stbi__context *s)
3837 if (stbi__get8(s) !=
'G' || stbi__get8(s) !=
'I' || stbi__get8(s) !=
'F' || stbi__get8(s) !=
'8')
return 0;
3839 if (sz !=
'9' && sz !=
'7')
return 0;
3840 if (stbi__get8(s) !=
'a')
return 0;
3844 static int stbi__gif_test(stbi__context *s)
3846 int r = stbi__gif_test_raw(s);
3851 static void stbi__gif_parse_colortable(stbi__context *s,
stbi_uc pal[256][4],
int num_entries,
int transp)
3854 for (i=0; i < num_entries; ++
i) {
3855 pal[
i][2] = stbi__get8(s);
3856 pal[
i][1] = stbi__get8(s);
3857 pal[
i][0] = stbi__get8(s);
3858 pal[
i][3] = transp ? 0 : 255;
3862 static int stbi__gif_header(stbi__context *s, stbi__gif *g,
int *comp,
int is_info)
3865 if (stbi__get8(s) !=
'G' || stbi__get8(s) !=
'I' || stbi__get8(s) !=
'F' || stbi__get8(s) !=
'8')
3866 return stbi__err(
"not GIF",
"Corrupt GIF");
3868 version = stbi__get8(s);
3869 if (version !=
'7' && version !=
'9')
return stbi__err(
"not GIF",
"Corrupt GIF");
3870 if (stbi__get8(s) !=
'a')
return stbi__err(
"not GIF",
"Corrupt GIF");
3872 stbi__g_failure_reason =
"";
3873 g->w = stbi__get16le(s);
3874 g->h = stbi__get16le(s);
3875 g->flags = stbi__get8(s);
3876 g->bgindex = stbi__get8(s);
3877 g->ratio = stbi__get8(s);
3878 g->transparent = -1;
3880 if (comp != 0) *comp = 4;
3882 if (is_info)
return 1;
3884 if (g->flags & 0x80)
3885 stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
3890 static int stbi__gif_info_raw(stbi__context *s,
int *x,
int *y,
int *comp)
3893 if (!stbi__gif_header(s, &g, comp, 1)) {
3902 static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
3908 if (g->codes[code].prefix >= 0)
3909 stbi__out_gif_code(g, g->codes[code].prefix);
3911 if (g->cur_y >= g->max_y)
return;
3913 p = &g->out[g->cur_x + g->cur_y];
3914 c = &g->color_table[g->codes[code].suffix * 4];
3924 if (g->cur_x >= g->max_x) {
3925 g->cur_x = g->start_x;
3926 g->cur_y += g->step;
3928 while (g->cur_y >= g->max_y && g->parse > 0) {
3929 g->step = (1 << g->parse) * g->line_size;
3930 g->cur_y = g->start_y + (g->step >> 1);
3936 static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
3939 stbi__int32
len, code;
3941 stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits,
clear;
3944 lzw_cs = stbi__get8(s);
3945 clear = 1 << lzw_cs;
3947 codesize = lzw_cs + 1;
3948 codemask = (1 << codesize) - 1;
3951 for (code = 0; code <
clear; code++) {
3952 g->codes[code].prefix = -1;
3953 g->codes[code].first = (
stbi_uc) code;
3954 g->codes[code].suffix = (
stbi_uc) code;
3963 if (valid_bits < codesize) {
3965 len = stbi__get8(s);
3970 bits |= (stbi__int32) stbi__get8(s) << valid_bits;
3973 stbi__int32 code = bits & codemask;
3975 valid_bits -= codesize;
3977 if (code == clear) {
3978 codesize = lzw_cs + 1;
3979 codemask = (1 << codesize) - 1;
3983 }
else if (code == clear + 1) {
3985 while ((len = stbi__get8(s)) > 0)
3988 }
else if (code <= avail) {
3989 if (first)
return stbi__errpuc(
"no clear code",
"Corrupt GIF");
3992 p = &g->codes[avail++];
3993 if (avail > 4096)
return stbi__errpuc(
"too many codes",
"Corrupt GIF");
3994 p->prefix = (stbi__int16) oldcode;
3995 p->first = g->codes[oldcode].first;
3996 p->suffix = (code == avail) ? p->first : g->codes[code].first;
3997 }
else if (code == avail)
3998 return stbi__errpuc(
"illegal code in raster",
"Corrupt GIF");
4000 stbi__out_gif_code(g, (stbi__uint16) code);
4002 if ((avail & codemask) == 0 && avail <= 0x0FFF) {
4004 codemask = (1 << codesize) - 1;
4009 return stbi__errpuc(
"illegal code in raster",
"Corrupt GIF");
4015 static void stbi__fill_gif_background(stbi__gif *g)
4018 stbi_uc *c = g->pal[g->bgindex];
4020 for (i = 0; i < g->w * g->h * 4; i += 4) {
4030 static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g,
int *comp,
int req_comp)
4036 if (!stbi__gif_header(s, g, comp,0))
return 0;
4037 g->out = (
stbi_uc *) malloc(4 * g->w * g->h);
4038 if (g->out == 0)
return stbi__errpuc(
"outofmem",
"Out of memory");
4039 stbi__fill_gif_background(g);
4042 if (((g->eflags & 0x1C) >> 2) == 3) {
4044 g->out = (
stbi_uc *) malloc(4 * g->w * g->h);
4045 if (g->out == 0)
return stbi__errpuc(
"outofmem",
"Out of memory");
4046 memcpy(g->out, old_out, g->w*g->h*4);
4051 switch (stbi__get8(s)) {
4054 stbi__int32
x,
y,
w,
h;
4057 x = stbi__get16le(s);
4058 y = stbi__get16le(s);
4059 w = stbi__get16le(s);
4060 h = stbi__get16le(s);
4061 if (((x + w) > (g->w)) || ((y + h) > (g->h)))
4062 return stbi__errpuc(
"bad Image Descriptor",
"Corrupt GIF");
4064 g->line_size = g->w * 4;
4066 g->start_y = y * g->line_size;
4067 g->max_x = g->start_x + w * 4;
4068 g->max_y = g->start_y + h * g->line_size;
4069 g->cur_x = g->start_x;
4070 g->cur_y = g->start_y;
4072 g->lflags = stbi__get8(s);
4074 if (g->lflags & 0x40) {
4075 g->step = 8 * g->line_size;
4078 g->step = g->line_size;
4082 if (g->lflags & 0x80) {
4083 stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
4084 g->color_table = (
stbi_uc *) g->lpal;
4085 }
else if (g->flags & 0x80) {
4086 for (i=0; i < 256; ++
i)
4088 if (g->transparent >= 0 && (g->eflags & 0x01))
4089 g->pal[g->transparent][3] = 0;
4090 g->color_table = (
stbi_uc *) g->pal;
4092 return stbi__errpuc(
"missing color table",
"Corrupt GIF");
4094 o = stbi__process_gif_raster(s, g);
4095 if (o == NULL)
return NULL;
4097 if (req_comp && req_comp != 4)
4098 o = stbi__convert_format(o, 4, req_comp, g->w, g->h);
4105 if (stbi__get8(s) == 0xF9) {
4106 len = stbi__get8(s);
4108 g->eflags = stbi__get8(s);
4110 g->transparent = stbi__get8(s);
4116 while ((len = stbi__get8(s)) != 0)
4125 return stbi__errpuc(
"unknown code",
"Corrupt GIF");
4130 static stbi_uc *stbi__gif_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
4134 memset(&g, 0,
sizeof(g));
4136 u = stbi__gif_load_next(s, &g, comp, req_comp);
4137 if (u == (
void *) 1) u = 0;
4146 static int stbi__gif_info(stbi__context *s,
int *x,
int *y,
int *comp)
4148 return stbi__gif_info_raw(s,x,y,comp);
4156 static int stbi__hdr_test_core(stbi__context *s)
4158 const char *signature =
"#?RADIANCE\n";
4160 for (i=0; signature[
i]; ++
i)
4161 if (stbi__get8(s) != signature[i])
4166 static int stbi__hdr_test(stbi__context* s)
4168 int r = stbi__hdr_test_core(s);
4173 #define STBI__HDR_BUFLEN 1024
4174 static char *stbi__hdr_gettoken(stbi__context *z,
char *buffer)
4179 c = (char) stbi__get8(z);
4181 while (!stbi__at_eof(z) && c !=
'\n') {
4183 if (len == STBI__HDR_BUFLEN-1) {
4185 while (!stbi__at_eof(z) && stbi__get8(z) !=
'\n')
4189 c = (char) stbi__get8(z);
4196 static void stbi__hdr_convert(
float *output,
stbi_uc *input,
int req_comp)
4198 if ( input[3] != 0 ) {
4201 f1 = (float) ldexp(1.0f, input[3] - (
int)(128 + 8));
4203 output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
4205 output[0] = input[0] * f1;
4206 output[1] = input[1] * f1;
4207 output[2] = input[2] * f1;
4209 if (req_comp == 2) output[1] = 1;
4210 if (req_comp == 4) output[3] = 1;
4213 case 4: output[3] = 1;
4214 case 3: output[0] = output[1] = output[2] = 0;
4216 case 2: output[1] = 1;
4217 case 1: output[0] = 0;
4223 static float *stbi__hdr_load(stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
4225 char buffer[STBI__HDR_BUFLEN];
4233 int i, j, k, c1,c2,
z;
4237 if (strcmp(stbi__hdr_gettoken(s,buffer),
"#?RADIANCE") != 0)
4238 return stbi__errpf(
"not HDR",
"Corrupt HDR image");
4242 token = stbi__hdr_gettoken(s,buffer);
4243 if (token[0] == 0)
break;
4244 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
4247 if (!valid)
return stbi__errpf(
"unsupported format",
"Unsupported HDR format");
4251 token = stbi__hdr_gettoken(s,buffer);
4252 if (strncmp(token,
"-Y ", 3))
return stbi__errpf(
"unsupported data layout",
"Unsupported HDR format");
4254 height = (
int) strtol(token, &token, 10);
4255 while (*token ==
' ') ++token;
4256 if (strncmp(token,
"+X ", 3))
return stbi__errpf(
"unsupported data layout",
"Unsupported HDR format");
4258 width = (
int) strtol(token, NULL, 10);
4263 if (comp) *comp = 3;
4264 if (req_comp == 0) req_comp = 3;
4267 hdr_data = (
float *) malloc(height * width * req_comp *
sizeof(
float));
4273 for (j=0; j <
height; ++j) {
4274 for (i=0; i <
width; ++
i) {
4277 stbi__getn(s, rgbe, 4);
4278 stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
4285 for (j = 0; j <
height; ++j) {
4288 len = stbi__get8(s);
4289 if (c1 != 2 || c2 != 2 || (len & 0x80)) {
4296 rgbe[3] = (
stbi_uc) stbi__get8(s);
4297 stbi__hdr_convert(hdr_data, rgbe, req_comp);
4301 goto main_decode_loop;
4304 len |= stbi__get8(s);
4305 if (len != width) { free(hdr_data); free(scanline);
return stbi__errpf(
"invalid decoded scanline length",
"corrupt HDR"); }
4306 if (scanline == NULL) scanline = (
stbi_uc *) malloc(width * 4);
4308 for (k = 0; k < 4; ++k) {
4311 count = stbi__get8(s);
4314 value = stbi__get8(s);
4316 for (z = 0; z <
count; ++
z)
4317 scanline[i++ * 4 + k] = value;
4320 for (z = 0; z <
count; ++
z)
4321 scanline[i++ * 4 + k] = stbi__get8(s);
4325 for (i=0; i <
width; ++
i)
4326 stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
4334 static int stbi__hdr_info(stbi__context *s,
int *x,
int *y,
int *comp)
4336 char buffer[STBI__HDR_BUFLEN];
4340 if (strcmp(stbi__hdr_gettoken(s,buffer),
"#?RADIANCE") != 0) {
4346 token = stbi__hdr_gettoken(s,buffer);
4347 if (token[0] == 0)
break;
4348 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
4355 token = stbi__hdr_gettoken(s,buffer);
4356 if (strncmp(token,
"-Y ", 3)) {
4361 *y = (
int) strtol(token, &token, 10);
4362 while (*token ==
' ') ++token;
4363 if (strncmp(token,
"+X ", 3)) {
4368 *x = (
int) strtol(token, NULL, 10);
4372 #endif // STBI_NO_HDR
4374 static int stbi__bmp_info(stbi__context *s,
int *x,
int *y,
int *comp)
4377 if (stbi__get8(s) !=
'B' || stbi__get8(s) !=
'M') {
4382 hsz = stbi__get32le(s);
4383 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) {
4388 *x = stbi__get16le(s);
4389 *y = stbi__get16le(s);
4391 *x = stbi__get32le(s);
4392 *y = stbi__get32le(s);
4394 if (stbi__get16le(s) != 1) {
4398 *comp = stbi__get16le(s) / 8;
4402 static int stbi__psd_info(stbi__context *s,
int *x,
int *y,
int *comp)
4405 if (stbi__get32be(s) != 0x38425053) {
4409 if (stbi__get16be(s) != 1) {
4414 channelCount = stbi__get16be(s);
4415 if (channelCount < 0 || channelCount > 16) {
4419 *y = stbi__get32be(s);
4420 *x = stbi__get32be(s);
4421 if (stbi__get16be(s) != 8) {
4425 if (stbi__get16be(s) != 3) {
4433 static int stbi__pic_info(stbi__context *s,
int *x,
int *y,
int *comp)
4435 int act_comp=0,num_packets=0,chained;
4436 stbi__pic_packet packets[10];
4440 *x = stbi__get16be(s);
4441 *y = stbi__get16be(s);
4442 if (stbi__at_eof(s))
return 0;
4443 if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
4451 stbi__pic_packet *packet;
4453 if (num_packets==
sizeof(packets)/
sizeof(packets[0]))
4456 packet = &packets[num_packets++];
4457 chained = stbi__get8(s);
4458 packet->size = stbi__get8(s);
4459 packet->type = stbi__get8(s);
4460 packet->channel = stbi__get8(s);
4461 act_comp |= packet->channel;
4463 if (stbi__at_eof(s)) {
4467 if (packet->size != 8) {
4473 *comp = (act_comp & 0x10 ? 4 : 3);
4478 static int stbi__info_main(stbi__context *s,
int *x,
int *y,
int *comp)
4480 if (stbi__jpeg_info(s, x, y, comp))
4482 if (stbi__png_info(s, x, y, comp))
4484 if (stbi__gif_info(s, x, y, comp))
4486 if (stbi__bmp_info(s, x, y, comp))
4488 if (stbi__psd_info(s, x, y, comp))
4490 if (stbi__pic_info(s, x, y, comp))
4493 if (stbi__hdr_info(s, x, y, comp))
4497 if (stbi__tga_info(s, x, y, comp))
4499 return stbi__err(
"unknown image type",
"Image not of any known type, or corrupt");
4502 #ifndef STBI_NO_STDIO
4505 FILE *f = fopen(filename,
"rb");
4507 if (!f)
return stbi__err(
"can't fopen",
"Unable to open file");
4517 long pos = ftell(f);
4518 stbi__start_file(&s, f);
4519 r = stbi__info_main(&s,x,y,comp);
4520 fseek(f,pos,SEEK_SET);
4523 #endif // !STBI_NO_STDIO
4528 stbi__start_mem(&s,buffer,len);
4529 return stbi__info_main(&s,x,y,comp);
4536 return stbi__info_main(&s,x,y,comp);
4539 #endif // STB_IMAGE_IMPLEMENTATION
4541 #if !defined(STBI_NO_STDIO) && defined(_MSC_VER) && _MSC_VER >= 1400
4542 #pragma warning(pop)
STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
GLuint GLdouble GLdouble GLint GLint order
STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
GLint GLint GLsizei GLsizei GLsizei depth
GLenum GLenum GLenum GLenum GLenum scale
STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
STBIDEF const char * stbi_failure_reason(void)
GLenum GLenum GLenum input
STBIDEF void stbi_hdr_to_ldr_scale(float scale)
SDL_Rect line_size(const std::string &line, int font_size, int style)
Determine the size of a line of text given a certain font size.
void clear(const std::string &key)
GLfloat GLfloat GLfloat v2
GLuint const GLfloat * val
STBIDEF float * stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
STBIDEF stbi_uc * stbi_load_from_memory(stbi_uc const *buffer, int len, 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)
STBIDEF float * stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
GLint GLint GLint GLint GLint GLint y
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
STBIDEF void stbi_ldr_to_hdr_gamma(float gamma)
GLboolean GLenum GLenum GLvoid * values
GLuint GLsizei GLsizei * length
GLdouble GLdouble GLdouble b
GLdouble GLdouble GLdouble GLdouble q
STBIDEF stbi_uc * stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
STBIDEF void stbi_hdr_to_ldr_gamma(float gamma)
STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
STBIDEF void stbi_ldr_to_hdr_scale(float scale)
GLubyte GLubyte GLubyte GLubyte w
GLsizei const GLfloat * value
GLboolean GLboolean GLboolean GLboolean a
STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
STBIDEF int stbi_is_hdr_from_file(FILE *f)
STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
GLuint GLuint GLsizei count
cl_event GLbitfield flags
static const char * output
STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp)
STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp)
STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
STBIDEF int stbi_is_hdr(char const *filename)
GLfloat GLfloat GLfloat GLfloat h
GLint GLint GLint GLint GLint x
void read(config &cfg, std::istream &in, abstract_validator *validator)
GLdouble GLdouble GLdouble r
STBIDEF stbi_uc * stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
STBIDEF float * stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
STBIDEF void stbi_image_free(void *retval_from_stbi_load)
STBIDEF char * stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen)
STBIDEF char * stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
STBIDEF char * stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen)
GLint GLint GLint GLint GLint GLint GLsizei width
STBIDEF stbi_uc * stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
std::vector< Uint32 > palette(color_range cr)
Creates a reference color palette from a color range.
const std::string version
STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
STBIDEF char * stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
const std::string valid
Little parts of regex templates used to parse Wml annoations.