67#ifndef STBI_INCLUDE_STB_IMAGE_H
68 #define STBI_INCLUDE_STB_IMAGE_H
190 #ifndef STBI_NO_STDIO
192 #if defined(_MSC_VER) && _MSC_VER >= 0x1400
193 #define _CRT_SECURE_NO_WARNINGS
201 #define STBI_VERSION 1
212typedef unsigned char stbi_uc;
227extern stbi_uc* stbi_load_from_memory(stbi_uc
const* buffer,
234 #ifndef STBI_NO_STDIO
235extern stbi_uc* stbi_load(
char const* filename,
240extern stbi_uc* stbi_load_from_file(FILE* f,
250 int (*read)(
void* user,
254 void (*skip)(
void* user,
unsigned n);
255 int (*eof)(
void* user);
258extern stbi_uc* stbi_load_from_callbacks(stbi_io_callbacks
const* clbk,
266extern float* stbi_loadf_from_memory(stbi_uc
const* buffer,
273 #ifndef STBI_NO_STDIO
274extern float* stbi_loadf(
char const* filename,
279extern float* stbi_loadf_from_file(FILE* f,
286extern float* stbi_loadf_from_callbacks(stbi_io_callbacks
const* clbk,
293extern void stbi_hdr_to_ldr_gamma(
float gamma);
294extern void stbi_hdr_to_ldr_scale(
float scale);
296extern void stbi_ldr_to_hdr_gamma(
float gamma);
297extern void stbi_ldr_to_hdr_scale(
float scale);
301extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks
const* clbk,
303extern int stbi_is_hdr_from_memory(stbi_uc
const* buffer,
int len);
304 #ifndef STBI_NO_STDIO
305extern int stbi_is_hdr(
char const* filename);
306extern int stbi_is_hdr_from_file(FILE* f);
311extern const char* stbi_failure_reason(
void);
314extern void stbi_image_free(
void* retval_from_stbi_load);
317extern int stbi_info_from_memory(stbi_uc
const* buffer,
322extern int stbi_info_from_callbacks(stbi_io_callbacks
const* clbk,
328 #ifndef STBI_NO_STDIO
329extern int stbi_info(
char const* filename,
int* x,
int* y,
int* comp);
330extern int stbi_info_from_file(FILE* f,
int* x,
int* y,
int* comp);
337extern void stbi_set_unpremultiply_on_load(
338 int flag_true_if_should_unpremultiply);
342extern void stbi_convert_iphone_png_to_rgb(
int flag_true_if_should_convert);
346extern char* stbi_zlib_decode_malloc_guesssize(
const char* buffer,
350extern char* stbi_zlib_decode_malloc(
const char* buffer,
int len,
int* outlen);
351extern int stbi_zlib_decode_buffer(
char* obuffer,
356extern char* stbi_zlib_decode_noheader_malloc(
const char* buffer,
359extern int stbi_zlib_decode_noheader_buffer(
char* obuffer,
366typedef void (*stbi_idct_8x8)(stbi_uc* out,
369 unsigned short* dequantize);
374typedef void (*stbi_YCbCr_to_RGB_run)(stbi_uc* output,
387extern void stbi_install_idct(stbi_idct_8x8 func);
388extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
400#ifndef STBI_HEADER_FILE_ONLY
407 #ifndef STBI_NO_STDIO
417 #define stbi_inline inline
422 #define stbi_inline __forceinline
426typedef unsigned char uint8;
427typedef unsigned short uint16;
428typedef signed short int16;
429typedef unsigned int uint32;
430typedef signed int int32;
431typedef unsigned int uint;
434typedef unsigned char validate_uint32[
sizeof(uint32) == 4 ? 1 : -1];
436 #if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE)
437 #define STBI_NO_WRITE
440 #define STBI_NOTUSED(v) (void)sizeof(v)
443 #define STBI_HAS_LROTL
446 #ifdef STBI_HAS_LROTL
447 #define stbi_lrot(x, y) _lrotl(x, y)
449 #define stbi_lrot(x, y) (((x) << (y)) | ((x) >> (32 - (y))))
460 int img_n, img_out_n;
462 stbi_io_callbacks io;
465 int read_from_callbacks;
467 uint8 buffer_start[128];
469 uint8 *img_buffer, *img_buffer_end;
470 uint8* img_buffer_original;
473static void refill_buffer(stbi* s);
476static void start_mem(stbi* s, uint8
const* buffer,
int len) {
478 s->read_from_callbacks = 0;
479 s->img_buffer = s->img_buffer_original = (uint8*)buffer;
480 s->img_buffer_end = (uint8*)buffer + len;
484static void start_callbacks(stbi* s, stbi_io_callbacks* c,
void* user) {
486 s->io_user_data = user;
487 s->buflen =
sizeof(s->buffer_start);
488 s->read_from_callbacks = 1;
489 s->img_buffer_original = s->buffer_start;
493 #ifndef STBI_NO_STDIO
495static int stdio_read(
void* user,
char* data,
int size) {
496 return (
int)fread(data, 1, size, (FILE*)user);
499static void stdio_skip(
void* user,
unsigned n) {
500 fseek((FILE*)user, n, SEEK_CUR);
503static int stdio_eof(
void* user) {
504 return feof((FILE*)user);
507static stbi_io_callbacks stbi_stdio_callbacks = {
513static void start_file(stbi* s, FILE* f) {
514 start_callbacks(s, &stbi_stdio_callbacks, (
void*)f);
521static void stbi_rewind(stbi* s) {
526 s->img_buffer = s->img_buffer_original;
529static int stbi_jpeg_test(stbi* s);
530static stbi_uc* stbi_jpeg_load(stbi* s,
535static int stbi_jpeg_info(stbi* s,
int* x,
int* y,
int* comp);
536static int stbi_png_test(stbi* s);
537static stbi_uc* stbi_png_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp);
538static int stbi_png_info(stbi* s,
int* x,
int* y,
int* comp);
539static int stbi_bmp_test(stbi* s);
540static stbi_uc* stbi_bmp_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp);
541static int stbi_tga_test(stbi* s);
542static stbi_uc* stbi_tga_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp);
543static int stbi_tga_info(stbi* s,
int* x,
int* y,
int* comp);
544static int stbi_psd_test(stbi* s);
545static stbi_uc* stbi_psd_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp);
546static int stbi_hdr_test(stbi* s);
547static float* stbi_hdr_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp);
548static int stbi_pic_test(stbi* s);
549static stbi_uc* stbi_pic_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp);
550static int stbi_gif_test(stbi* s);
551static stbi_uc* stbi_gif_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp);
552static int stbi_gif_info(stbi* s,
int* x,
int* y,
int* comp);
555static const char* failure_reason;
557const char* stbi_failure_reason(
void) {
558 return failure_reason;
561static int e(
const char* str) {
562 failure_reason = str;
570 #ifdef STBI_NO_FAILURE_STRINGS
572 #elif defined(STBI_FAILURE_USERMSG)
578 #define epf(x, y) ((float*)(e(x, y) ? NULL : NULL))
579 #define epuc(x, y) ((unsigned char*)(e(x, y) ? NULL : NULL))
581void stbi_image_free(
void* retval_from_stbi_load) {
582 free(retval_from_stbi_load);
586static float* ldr_to_hdr(stbi_uc* data,
int x,
int y,
int comp);
587static stbi_uc* hdr_to_ldr(
float* data,
int x,
int y,
int comp);
590static unsigned char* stbi_load_main(stbi* s,
595 if (stbi_jpeg_test(s))
596 return stbi_jpeg_load(s, x, y, comp, req_comp);
597 if (stbi_png_test(s))
598 return stbi_png_load(s, x, y, comp, req_comp);
599 if (stbi_bmp_test(s))
600 return stbi_bmp_load(s, x, y, comp, req_comp);
601 if (stbi_gif_test(s))
602 return stbi_gif_load(s, x, y, comp, req_comp);
603 if (stbi_psd_test(s))
604 return stbi_psd_load(s, x, y, comp, req_comp);
605 if (stbi_pic_test(s))
606 return stbi_pic_load(s, x, y, comp, req_comp);
609 if (stbi_hdr_test(s)) {
610 float* hdr = stbi_hdr_load(s, x, y, comp, req_comp);
611 return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
616 if (stbi_tga_test(s))
617 return stbi_tga_load(s, x, y, comp, req_comp);
618 return epuc(
"unknown image type",
"Image not of any known type, or corrupt");
621 #ifndef STBI_NO_STDIO
622unsigned char* stbi_load(
char const* filename,
627 FILE* f = fopen(filename,
"rb");
628 unsigned char* result;
630 return epuc(
"can't fopen",
"Unable to open file");
631 result = stbi_load_from_file(f, x, y, comp, req_comp);
636unsigned char* stbi_load_from_file(FILE* f,
643 return stbi_load_main(&s, x, y, comp, req_comp);
647unsigned char* stbi_load_from_memory(stbi_uc
const* buffer,
654 start_mem(&s, buffer, len);
655 return stbi_load_main(&s, x, y, comp, req_comp);
658unsigned char* stbi_load_from_callbacks(stbi_io_callbacks
const* clbk,
665 start_callbacks(&s, (stbi_io_callbacks*)clbk, user);
666 return stbi_load_main(&s, x, y, comp, req_comp);
671float* stbi_loadf_main(stbi* s,
int* x,
int* y,
int* comp,
int req_comp) {
674 if (stbi_hdr_test(s))
675 return stbi_hdr_load(s, x, y, comp, req_comp);
677 data = stbi_load_main(s, x, y, comp, req_comp);
679 return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
680 return epf(
"unknown image type",
"Image not of any known type, or corrupt");
683float* stbi_loadf_from_memory(stbi_uc
const* buffer,
690 start_mem(&s, buffer, len);
691 return stbi_loadf_main(&s, x, y, comp, req_comp);
694float* stbi_loadf_from_callbacks(stbi_io_callbacks
const* clbk,
701 start_callbacks(&s, (stbi_io_callbacks*)clbk, user);
702 return stbi_loadf_main(&s, x, y, comp, req_comp);
705 #ifndef STBI_NO_STDIO
706float* stbi_loadf(
char const* filename,
711 FILE* f = fopen(filename,
"rb");
714 return epf(
"can't fopen",
"Unable to open file");
715 result = stbi_loadf_from_file(f, x, y, comp, req_comp);
720float* stbi_loadf_from_file(FILE* f,
int* x,
int* y,
int* comp,
int req_comp) {
723 return stbi_loadf_main(&s, x, y, comp, req_comp);
733int stbi_is_hdr_from_memory(stbi_uc
const* buffer,
int len) {
736 start_mem(&s, buffer, len);
737 return stbi_hdr_test(&s);
739 STBI_NOTUSED(buffer);
745 #ifndef STBI_NO_STDIO
746extern int stbi_is_hdr(
char const* filename) {
747 FILE* f = fopen(filename,
"rb");
750 result = stbi_is_hdr_from_file(f);
756extern int stbi_is_hdr_from_file(FILE* f) {
760 return stbi_hdr_test(&s);
767extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks
const* clbk,
771 start_callbacks(&s, (stbi_io_callbacks*)clbk, user);
772 return stbi_hdr_test(&s);
779static float h2l_gamma_i = 1.0f / 2.2f, h2l_scale_i = 1.0f;
780static float l2h_gamma = 2.2f, l2h_scale = 1.0f;
782void stbi_hdr_to_ldr_gamma(
float gamma) {
783 h2l_gamma_i = 1 / gamma;
785void stbi_hdr_to_ldr_scale(
float scale) {
786 h2l_scale_i = 1 / scale;
789void stbi_ldr_to_hdr_gamma(
float gamma) {
792void stbi_ldr_to_hdr_scale(
float scale) {
802enum { SCAN_load = 0, SCAN_type, SCAN_header };
804static void refill_buffer(stbi* s) {
805 int n = (s->io.read)(s->io_user_data, (
char*)s->buffer_start, s->buflen);
808 s->read_from_callbacks = 0;
809 s->img_buffer = s->img_buffer_end - 1;
812 s->img_buffer = s->buffer_start;
813 s->img_buffer_end = s->buffer_start + n;
817stbi_inline
static int get8(stbi* s) {
818 if (s->img_buffer < s->img_buffer_end)
819 return *s->img_buffer++;
820 if (s->read_from_callbacks) {
822 return *s->img_buffer++;
827stbi_inline
static int at_eof(stbi* s) {
829 if (!(s->io.eof)(s->io_user_data))
833 if (s->read_from_callbacks == 0)
837 return s->img_buffer >= s->img_buffer_end;
840stbi_inline
static uint8 get8u(stbi* s) {
841 return (uint8)get8(s);
844static void skip(stbi* s,
int n) {
846 int blen = s->img_buffer_end - s->img_buffer;
848 s->img_buffer = s->img_buffer_end;
849 (s->io.skip)(s->io_user_data, n - blen);
856static int getn(stbi* s, stbi_uc* buffer,
int n) {
858 int blen = s->img_buffer_end - s->img_buffer;
862 memcpy(buffer, s->img_buffer, blen);
864 count = (s->io.read)(s->io_user_data, (
char*)buffer + blen, n - blen);
865 res = (count == (n - blen));
866 s->img_buffer = s->img_buffer_end;
871 if (s->img_buffer + n <= s->img_buffer_end) {
872 memcpy(buffer, s->img_buffer, n);
879static int get16(stbi* s) {
881 return (z << 8) + get8(s);
884static uint32 get32(stbi* s) {
886 return (z << 16) + get16(s);
889static int get16le(stbi* s) {
891 return z + (get8(s) << 8);
894static uint32 get32le(stbi* s) {
895 uint32 z = get16le(s);
896 return z + (get16le(s) << 16);
910static uint8 compute_y(
int r,
int g,
int b) {
911 return (uint8)(((r * 77) + (g * 150) + (29 * b)) >> 8);
914static unsigned char* convert_format(
unsigned char* data,
922 if (req_comp == img_n)
924 assert(req_comp >= 1 && req_comp <= 4);
926 good = (
unsigned char*)malloc(req_comp * x * y);
929 return epuc(
"outofmem",
"Out of memory");
932 for (j = 0; j < (int)y; ++j) {
933 unsigned char* src = data + j * x * img_n;
934 unsigned char* dest = good + j * x * req_comp;
936 #define COMBO(a, b) ((a)*8 + (b))
939 for (i = x - 1; i >= 0; --i, src += a, dest += b)
943 switch (COMBO(img_n, req_comp)) {
944 CASE(1, 2) dest[0] = src[0], dest[1] = 255;
946 CASE(1, 3) dest[0] = dest[1] = dest[2] = src[0];
948 CASE(1, 4) dest[0] = dest[1] = dest[2] = src[0], dest[3] = 255;
950 CASE(2, 1) dest[0] = src[0];
952 CASE(2, 3) dest[0] = dest[1] = dest[2] = src[0];
954 CASE(2, 4) dest[0] = dest[1] = dest[2] = src[0], dest[3] = src[1];
957 dest[0] = src[0], dest[1] = src[1], dest[2] = src[2], dest[3] = 255;
959 CASE(3, 1) dest[0] = compute_y(src[0], src[1], src[2]);
961 CASE(3, 2) dest[0] = compute_y(src[0], src[1], src[2]), dest[1] = 255;
963 CASE(4, 1) dest[0] = compute_y(src[0], src[1], src[2]);
965 CASE(4, 2) dest[0] = compute_y(src[0], src[1], src[2]), dest[1] = src[3];
967 CASE(4, 3) dest[0] = src[0], dest[1] = src[1], dest[2] = src[2];
980static float* ldr_to_hdr(stbi_uc* data,
int x,
int y,
int comp) {
982 float* output = (
float*)malloc(x * y * comp *
sizeof(
float));
983 if (output == NULL) {
985 return epf(
"outofmem",
"Out of memory");
992 for (i = 0; i < x * y; ++i) {
993 for (k = 0; k < n; ++k) {
994 output[i * comp + k] =
995 (float)pow(data[i * comp + k] / 255.0f, l2h_gamma) * l2h_scale;
998 output[i * comp + k] = data[i * comp + k] / 255.0f;
1004 #define float2int(x) ((int)(x))
1005static stbi_uc* hdr_to_ldr(
float* data,
int x,
int y,
int comp) {
1007 stbi_uc* output = (stbi_uc*)malloc(x * y * comp);
1008 if (output == NULL) {
1010 return epuc(
"outofmem",
"Out of memory");
1017 for (i = 0; i < x * y; ++i) {
1018 for (k = 0; k < n; ++k) {
1020 (float)pow(data[i * comp + k] * h2l_scale_i, h2l_gamma_i) * 255 +
1026 output[i * comp + k] = (uint8)float2int(z);
1029 float z = data[i * comp + k] * 255 + 0.5f;
1034 output[i * comp + k] = (uint8)float2int(z);
1073 uint8 fast[1 << FAST_BITS];
1078 unsigned int maxcode[18];
1084 unsigned short dequant2[4][64];
1089 uint8 dequant[4][64];
1092 int img_h_max, img_v_max;
1093 int img_mcu_x, img_mcu_y;
1094 int img_mcu_w, img_mcu_h;
1112 unsigned char marker;
1115 int scan_n, order[4];
1116 int restart_interval, todo;
1119static int build_huffman(huffman* h,
int* count) {
1120 int i, j, k = 0, code;
1122 for (i = 0; i < 16; ++i)
1123 for (j = 0; j < count[i]; ++j)
1124 h->size[k++] = (uint8)(i + 1);
1130 for (j = 1; j <= 16; ++j) {
1132 h->delta[j] = k - code;
1133 if (h->size[k] == j) {
1134 while (h->size[k] == j)
1135 h->code[k++] = (uint16)(code++);
1136 if (code - 1 >= (1 << j))
1137 return e(
"bad code lengths",
"Corrupt JPEG");
1140 h->maxcode[j] = code << (16 - j);
1143 h->maxcode[j] = 0xffffffff;
1146 memset(h->fast, 255, 1 << FAST_BITS);
1147 for (i = 0; i < k; ++i) {
1149 if (s <= FAST_BITS) {
1150 int c = h->code[i] << (FAST_BITS - s);
1151 int m = 1 << (FAST_BITS - s);
1152 for (j = 0; j < m; ++j) {
1153 h->fast[c + j] = (uint8)i;
1160static void grow_buffer_unsafe(jpeg* j) {
1162 int b = j->nomore ? 0 : get8(j->s);
1166 j->marker = (
unsigned char)c;
1171 j->code_buffer |= b << (24 - j->code_bits);
1173 }
while (j->code_bits <= 24);
1177static uint32 bmask[17] = {0, 1, 3, 7, 15, 31, 63, 127, 255,
1178 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535};
1181stbi_inline
static int decode(jpeg* j, huffman* h) {
1185 if (j->code_bits < 16)
1186 grow_buffer_unsafe(j);
1190 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS) - 1);
1194 if (s > j->code_bits)
1196 j->code_buffer <<= s;
1198 return h->values[k];
1207 temp = j->code_buffer >> 16;
1208 for (k = FAST_BITS + 1;; ++k)
1209 if (temp < h->maxcode[k])
1217 if (k > j->code_bits)
1221 c = ((j->code_buffer >> (32 - k)) & bmask[k]) + h->delta[k];
1222 assert((((j->code_buffer) >> (32 - h->size[c])) & bmask[h->size[c]]) ==
1227 j->code_buffer <<= k;
1228 return h->values[c];
1233stbi_inline
static int extend_receive(jpeg* j,
int n) {
1234 unsigned int m = 1 << (n - 1);
1236 if (j->code_bits < n)
1237 grow_buffer_unsafe(j);
1240 k = stbi_lrot(j->code_buffer, n);
1241 j->code_buffer = k & ~bmask[n];
1245 k = (j->code_buffer >> (32 - n)) & bmask[n];
1247 j->code_buffer <<= n;
1253 return (-1 << n) + k + 1;
1260static uint8 dezigzag[64 + 15] = {
1261 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40,
1262 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36,
1263 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61,
1266 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63};
1269static int decode_block(jpeg* j,
1275 int t = decode(j, hdc);
1277 return e(
"bad huffman code",
"Corrupt JPEG");
1280 memset(data, 0, 64 *
sizeof(data[0]));
1282 diff = t ? extend_receive(j, t) : 0;
1283 dc = j->img_comp[b].dc_pred + diff;
1284 j->img_comp[b].dc_pred = dc;
1285 data[0] = (short)dc;
1291 int rs = decode(j, hac);
1293 return e(
"bad huffman code",
"Corrupt JPEG");
1303 data[dezigzag[k++]] = (short)extend_receive(j, s);
1310stbi_inline
static uint8 clamp(
int x) {
1312 if ((
unsigned int)x > 255) {
1321 #define f2f(x) (int)(((x)*4096 + 0.5))
1322 #define fsh(x) ((x) << 12)
1325 #define IDCT_1D(s0, s1, s2, s3, s4, s5, s6, s7) \
1326 int t0, t1, t2, t3, p1, p2, p3, p4, p5, x0, x1, x2, x3; \
1329 p1 = (p2 + p3) * f2f(0.5411961f); \
1330 t2 = p1 + p3 * f2f(-1.847759065f); \
1331 t3 = p1 + p2 * f2f(0.765366865f); \
1334 t0 = fsh(p2 + p3); \
1335 t1 = fsh(p2 - p3); \
1348 p5 = (p3 + p4) * f2f(1.175875602f); \
1349 t0 = t0 * f2f(0.298631336f); \
1350 t1 = t1 * f2f(2.053119869f); \
1351 t2 = t2 * f2f(3.072711026f); \
1352 t3 = t3 * f2f(1.501321110f); \
1353 p1 = p5 + p1 * f2f(-0.899976223f); \
1354 p2 = p5 + p2 * f2f(-2.562915447f); \
1355 p3 = p3 * f2f(-1.961570560f); \
1356 p4 = p4 * f2f(-0.390180644f); \
1363typedef unsigned short stbi_dequantize_t;
1365typedef uint8 stbi_dequantize_t;
1369static void idct_block(uint8* out,
1372 stbi_dequantize_t* dequantize) {
1373 int i, val[64], *v = val;
1374 stbi_dequantize_t* dq = dequantize;
1379 for (i = 0; i < 8; ++i, ++d, ++dq, ++v) {
1381 if (d[8] == 0 && d[16] == 0 && d[24] == 0 && d[32] == 0 && d[40] == 0 &&
1382 d[48] == 0 && d[56] == 0) {
1387 int dcterm = d[0] * dq[0] << 2;
1388 v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
1390 IDCT_1D(d[0] * dq[0], d[8] * dq[8], d[16] * dq[16], d[24] * dq[24],
1391 d[32] * dq[32], d[40] * dq[40], d[48] * dq[48], d[56] * dq[56])
1398 v[0] = (x0 + t3) >> 10;
1399 v[56] = (x0 - t3) >> 10;
1400 v[8] = (x1 + t2) >> 10;
1401 v[48] = (x1 - t2) >> 10;
1402 v[16] = (x2 + t1) >> 10;
1403 v[40] = (x2 - t1) >> 10;
1404 v[24] = (x3 + t0) >> 10;
1405 v[32] = (x3 - t0) >> 10;
1409 for (i = 0, v = val, o = out; i < 8; ++i, v += 8, o += out_stride) {
1411 IDCT_1D(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
1418 x0 += 65536 + (128 << 17);
1419 x1 += 65536 + (128 << 17);
1420 x2 += 65536 + (128 << 17);
1421 x3 += 65536 + (128 << 17);
1424 o[0] = clamp((x0 + t3) >> 17);
1425 o[7] = clamp((x0 - t3) >> 17);
1426 o[1] = clamp((x1 + t2) >> 17);
1427 o[6] = clamp((x1 - t2) >> 17);
1428 o[2] = clamp((x2 + t1) >> 17);
1429 o[5] = clamp((x2 - t1) >> 17);
1430 o[3] = clamp((x3 + t0) >> 17);
1431 o[4] = clamp((x3 - t0) >> 17);
1436static stbi_idct_8x8 stbi_idct_installed = idct_block;
1438void stbi_install_idct(stbi_idct_8x8 func) {
1439 stbi_idct_installed = func;
1443 #define MARKER_none 0xff
1447static uint8 get_marker(jpeg* j) {
1449 if (j->marker != MARKER_none) {
1451 j->marker = MARKER_none;
1464 #define RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
1468static void reset(jpeg* j) {
1472 j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
1473 j->marker = MARKER_none;
1474 j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
1479static int parse_entropy_coded_data(jpeg* z) {
1481 if (z->scan_n == 1) {
1484 __declspec(align(16))
1487 int n = z->order[0];
1492 int w = (z->img_comp[n].x + 7) >> 3;
1493 int h = (z->img_comp[n].y + 7) >> 3;
1494 for (j = 0; j < h; ++j) {
1495 for (i = 0; i < w; ++i) {
1496 if (!decode_block(z, data, z->huff_dc + z->img_comp[n].hd,
1497 z->huff_ac + z->img_comp[n].ha, n))
1500 stbi_idct_installed(
1501 z->img_comp[n].data + z->img_comp[n].w2 * j * 8 + i * 8,
1502 z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
1504 idct_block(z->img_comp[n].data + z->img_comp[n].w2 * j * 8 + i * 8,
1505 z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
1508 if (--z->todo <= 0) {
1509 if (z->code_bits < 24)
1510 grow_buffer_unsafe(z);
1513 if (!RESTART(z->marker))
1522 for (j = 0; j < z->img_mcu_y; ++j) {
1523 for (i = 0; i < z->img_mcu_x; ++i) {
1525 for (k = 0; k < z->scan_n; ++k) {
1526 int n = z->order[k];
1529 for (y = 0; y < z->img_comp[n].v; ++y) {
1530 for (x = 0; x < z->img_comp[n].h; ++x) {
1531 int x2 = (i * z->img_comp[n].h + x) * 8;
1532 int y2 = (j * z->img_comp[n].v + y) * 8;
1533 if (!decode_block(z, data, z->huff_dc + z->img_comp[n].hd,
1534 z->huff_ac + z->img_comp[n].ha, n))
1537 stbi_idct_installed(
1538 z->img_comp[n].data + z->img_comp[n].w2 * y2 + x2,
1539 z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
1541 idct_block(z->img_comp[n].data + z->img_comp[n].w2 * y2 + x2,
1542 z->img_comp[n].w2, data,
1543 z->dequant[z->img_comp[n].tq]);
1550 if (--z->todo <= 0) {
1551 if (z->code_bits < 24)
1552 grow_buffer_unsafe(z);
1555 if (!RESTART(z->marker))
1565static int process_marker(jpeg* z,
int m) {
1569 return e(
"expected marker",
"Corrupt JPEG");
1572 return e(
"progressive jpeg",
"JPEG format not supported (progressive)");
1575 if (get16(z->s) != 4)
1576 return e(
"bad DRI len",
"Corrupt JPEG");
1577 z->restart_interval = get16(z->s);
1581 L = get16(z->s) - 2;
1587 return e(
"bad DQT type",
"Corrupt JPEG");
1589 return e(
"bad DQT table",
"Corrupt JPEG");
1590 for (i = 0; i < 64; ++i)
1591 z->dequant[t][dezigzag[i]] = get8u(z->s);
1593 for (i = 0; i < 64; ++i)
1594 z->dequant2[t][i] = z->dequant[t][i];
1601 L = get16(z->s) - 2;
1604 int sizes[16], i, m = 0;
1608 if (tc > 1 || th > 3)
1609 return e(
"bad DHT header",
"Corrupt JPEG");
1610 for (i = 0; i < 16; ++i) {
1611 sizes[i] = get8(z->s);
1616 if (!build_huffman(z->huff_dc + th, sizes))
1618 v = z->huff_dc[th].values;
1620 if (!build_huffman(z->huff_ac + th, sizes))
1622 v = z->huff_ac[th].values;
1624 for (i = 0; i < m; ++i)
1631 if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
1632 skip(z->s, get16(z->s) - 2);
1639static int process_scan_header(jpeg* z) {
1641 int Ls = get16(z->s);
1642 z->scan_n = get8(z->s);
1643 if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (
int)z->s->img_n)
1644 return e(
"bad SOS component count",
"Corrupt JPEG");
1645 if (Ls != 6 + 2 * z->scan_n)
1646 return e(
"bad SOS len",
"Corrupt JPEG");
1647 for (i = 0; i < z->scan_n; ++i) {
1648 int id = get8(z->s), which;
1650 for (which = 0; which < z->s->img_n; ++which)
1651 if (z->img_comp[which].id ==
id)
1653 if (which == z->s->img_n)
1655 z->img_comp[which].hd = q >> 4;
1656 if (z->img_comp[which].hd > 3)
1657 return e(
"bad DC huff",
"Corrupt JPEG");
1658 z->img_comp[which].ha = q & 15;
1659 if (z->img_comp[which].ha > 3)
1660 return e(
"bad AC huff",
"Corrupt JPEG");
1661 z->order[i] = which;
1663 if (get8(z->s) != 0)
1664 return e(
"bad SOS",
"Corrupt JPEG");
1666 if (get8(z->s) != 0)
1667 return e(
"bad SOS",
"Corrupt JPEG");
1672static int process_frame_header(jpeg* z,
int scan) {
1674 int Lf, p, i, q, h_max = 1, v_max = 1, c;
1677 return e(
"bad SOF len",
"Corrupt JPEG");
1680 return e(
"only 8-bit",
1681 "JPEG format not supported: 8-bit only");
1682 s->img_y = get16(s);
1686 "JPEG format not supported: delayed height");
1689 s->img_x = get16(s);
1691 return e(
"0 width",
"Corrupt JPEG");
1693 if (c != 3 && c != 1)
1694 return e(
"bad component count",
"Corrupt JPEG");
1696 for (i = 0; i < c; ++i) {
1697 z->img_comp[i].data = NULL;
1698 z->img_comp[i].linebuf = NULL;
1701 if (Lf != 8 + 3 * s->img_n)
1702 return e(
"bad SOF len",
"Corrupt JPEG");
1704 for (i = 0; i < s->img_n; ++i) {
1705 z->img_comp[i].id = get8(s);
1706 if (z->img_comp[i].id != i + 1)
1707 if (z->img_comp[i].id !=
1709 return e(
"bad component ID",
"Corrupt JPEG");
1711 z->img_comp[i].h = (q >> 4);
1712 if (!z->img_comp[i].h || z->img_comp[i].h > 4)
1713 return e(
"bad H",
"Corrupt JPEG");
1714 z->img_comp[i].v = q & 15;
1715 if (!z->img_comp[i].v || z->img_comp[i].v > 4)
1716 return e(
"bad V",
"Corrupt JPEG");
1717 z->img_comp[i].tq = get8(s);
1718 if (z->img_comp[i].tq > 3)
1719 return e(
"bad TQ",
"Corrupt JPEG");
1722 if (scan != SCAN_load)
1725 if ((1 << 30) / s->img_x / s->img_n < s->img_y)
1726 return e(
"too large",
"Image too large to decode");
1728 for (i = 0; i < s->img_n; ++i) {
1729 if (z->img_comp[i].h > h_max)
1730 h_max = z->img_comp[i].h;
1731 if (z->img_comp[i].v > v_max)
1732 v_max = z->img_comp[i].v;
1736 z->img_h_max = h_max;
1737 z->img_v_max = v_max;
1738 z->img_mcu_w = h_max * 8;
1739 z->img_mcu_h = v_max * 8;
1740 z->img_mcu_x = (s->img_x + z->img_mcu_w - 1) / z->img_mcu_w;
1741 z->img_mcu_y = (s->img_y + z->img_mcu_h - 1) / z->img_mcu_h;
1743 for (i = 0; i < s->img_n; ++i) {
1745 z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max - 1) / h_max;
1746 z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max - 1) / v_max;
1751 z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
1752 z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
1753 z->img_comp[i].raw_data =
1754 malloc(z->img_comp[i].w2 * z->img_comp[i].h2 + 15);
1755 if (z->img_comp[i].raw_data == NULL) {
1756 for (--i; i >= 0; --i) {
1757 free(z->img_comp[i].raw_data);
1758 z->img_comp[i].data = NULL;
1760 return e(
"outofmem",
"Out of memory");
1763 z->img_comp[i].data =
1764 (uint8*)(((
size_t)z->img_comp[i].raw_data + 15) & ~15);
1765 z->img_comp[i].linebuf = NULL;
1772 #define DNL(x) ((x) == 0xdc)
1773 #define SOI(x) ((x) == 0xd8)
1774 #define EOI(x) ((x) == 0xd9)
1775 #define SOF(x) ((x) == 0xc0 || (x) == 0xc1)
1776 #define SOS(x) ((x) == 0xda)
1778static int decode_jpeg_header(jpeg* z,
int scan) {
1780 z->marker = MARKER_none;
1783 return e(
"no SOI",
"Corrupt JPEG");
1784 if (scan == SCAN_type)
1788 if (!process_marker(z, m))
1791 while (m == MARKER_none) {
1794 return e(
"no SOF",
"Corrupt JPEG");
1798 if (!process_frame_header(z, scan))
1803static int decode_jpeg_image(jpeg* j) {
1805 j->restart_interval = 0;
1806 if (!decode_jpeg_header(j, SCAN_load))
1811 if (!process_scan_header(j))
1813 if (!parse_entropy_coded_data(j))
1815 if (j->marker == MARKER_none) {
1817 while (!at_eof(j->s)) {
1820 j->marker = get8u(j->s);
1822 }
else if (x != 0) {
1830 if (!process_marker(j, m))
1841 *resample_row_func)(uint8* out, uint8* in0, uint8* in1,
int w,
int hs);
1843 #define div4(x) ((uint8)((x) >> 2))
1845static uint8* resample_row_1(uint8* out,
1851 STBI_NOTUSED(in_far);
1857static uint8* resample_row_v_2(uint8* out,
1865 for (i = 0; i < w; ++i)
1866 out[i] = div4(3 * in_near[i] + in_far[i] + 2);
1870static uint8* resample_row_h_2(uint8* out,
1877 uint8* input = in_near;
1881 out[0] = out[1] = input[0];
1886 out[1] = div4(input[0] * 3 + input[1] + 2);
1887 for (i = 1; i < w - 1; ++i) {
1888 int n = 3 * input[i] + 2;
1889 out[i * 2 + 0] = div4(n + input[i - 1]);
1890 out[i * 2 + 1] = div4(n + input[i + 1]);
1892 out[i * 2 + 0] = div4(input[w - 2] * 3 + input[w - 1] + 2);
1893 out[i * 2 + 1] = input[w - 1];
1895 STBI_NOTUSED(in_far);
1901 #define div16(x) ((uint8)((x) >> 4))
1903static uint8* resample_row_hv_2(uint8* out,
1911 out[0] = out[1] = div4(3 * in_near[0] + in_far[0] + 2);
1915 t1 = 3 * in_near[0] + in_far[0];
1916 out[0] = div4(t1 + 2);
1917 for (i = 1; i < w; ++i) {
1919 t1 = 3 * in_near[i] + in_far[i];
1920 out[i * 2 - 1] = div16(3 * t0 + t1 + 8);
1921 out[i * 2] = div16(3 * t1 + t0 + 8);
1923 out[w * 2 - 1] = div4(t1 + 2);
1930static uint8* resample_row_generic(uint8* out,
1937 for (i = 0; i < w; ++i)
1938 for (j = 0; j < hs; ++j)
1939 out[i * hs + j] = in_near[i];
1943 #define float2fixed(x) ((int)((x)*65536 + 0.5))
1947static void YCbCr_to_RGB_row(uint8* out,
1954 for (i = 0; i < count; ++i) {
1955 int y_fixed = (y[i] << 16) + 32768;
1957 int cr = pcr[i] - 128;
1958 int cb = pcb[i] - 128;
1959 r = y_fixed + cr * float2fixed(1.40200f);
1960 g = y_fixed - cr * float2fixed(0.71414f) - cb * float2fixed(0.34414f);
1961 b = y_fixed + cb * float2fixed(1.77200f);
1965 if ((
unsigned)r > 255) {
1971 if ((
unsigned)g > 255) {
1977 if ((
unsigned)b > 255) {
1992static stbi_YCbCr_to_RGB_run stbi_YCbCr_installed = YCbCr_to_RGB_row;
1994void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func) {
1995 stbi_YCbCr_installed = func;
2000static void cleanup_jpeg(jpeg* j) {
2002 for (i = 0; i < j->s->img_n; ++i) {
2003 if (j->img_comp[i].data) {
2004 free(j->img_comp[i].raw_data);
2005 j->img_comp[i].data = NULL;
2007 if (j->img_comp[i].linebuf) {
2008 free(j->img_comp[i].linebuf);
2009 j->img_comp[i].linebuf = NULL;
2015 resample_row_func resample;
2016 uint8 *line0, *line1;
2023static uint8* load_jpeg_image(jpeg* z,
2030 if (req_comp < 0 || req_comp > 4)
2031 return epuc(
"bad req_comp",
"Internal error");
2035 if (!decode_jpeg_image(z)) {
2041 n = req_comp ? req_comp : z->s->img_n;
2043 if (z->s->img_n == 3 && n < 3)
2046 decode_n = z->s->img_n;
2055 stbi_resample res_comp[4];
2057 for (k = 0; k < decode_n; ++k) {
2058 stbi_resample* r = &res_comp[k];
2062 z->img_comp[k].linebuf = (uint8*)malloc(z->s->img_x + 3);
2063 if (!z->img_comp[k].linebuf) {
2065 return epuc(
"outofmem",
"Out of memory");
2068 r->hs = z->img_h_max / z->img_comp[k].h;
2069 r->vs = z->img_v_max / z->img_comp[k].v;
2070 r->ystep = r->vs >> 1;
2071 r->w_lores = (z->s->img_x + r->hs - 1) / r->hs;
2073 r->line0 = r->line1 = z->img_comp[k].data;
2075 if (r->hs == 1 && r->vs == 1)
2076 r->resample = resample_row_1;
2077 else if (r->hs == 1 && r->vs == 2)
2078 r->resample = resample_row_v_2;
2079 else if (r->hs == 2 && r->vs == 1)
2080 r->resample = resample_row_h_2;
2081 else if (r->hs == 2 && r->vs == 2)
2082 r->resample = resample_row_hv_2;
2084 r->resample = resample_row_generic;
2088 output = (uint8*)malloc(n * z->s->img_x * z->s->img_y + 1);
2091 return epuc(
"outofmem",
"Out of memory");
2095 for (j = 0; j < z->s->img_y; ++j) {
2096 uint8* out = output + n * z->s->img_x * j;
2097 for (k = 0; k < decode_n; ++k) {
2098 stbi_resample* r = &res_comp[k];
2099 int y_bot = r->ystep >= (r->vs >> 1);
2101 r->resample(z->img_comp[k].linebuf, y_bot ? r->line1 : r->line0,
2102 y_bot ? r->line0 : r->line1, r->w_lores, r->hs);
2103 if (++r->ystep >= r->vs) {
2105 r->line0 = r->line1;
2106 if (++r->ypos < z->img_comp[k].y)
2107 r->line1 += z->img_comp[k].w2;
2111 uint8* y = coutput[0];
2112 if (z->s->img_n == 3) {
2114 stbi_YCbCr_installed(out, y, coutput[1], coutput[2], z->s.img_x, n);
2116 YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s->img_x, n);
2119 for (i = 0; i < z->s->img_x; ++i) {
2120 out[0] = out[1] = out[2] = y[i];
2125 uint8* y = coutput[0];
2127 for (i = 0; i < z->s->img_x; ++i)
2130 for (i = 0; i < z->s->img_x; ++i)
2131 *out++ = y[i], *out++ = 255;
2135 *out_x = z->s->img_x;
2136 *out_y = z->s->img_y;
2138 *comp = z->s->img_n;
2143static unsigned char* stbi_jpeg_load(stbi* s,
2150 return load_jpeg_image(&j, x, y, comp, req_comp);
2153static int stbi_jpeg_test(stbi* s) {
2157 r = decode_jpeg_header(&j, SCAN_type);
2162static int stbi_jpeg_info_raw(jpeg* j,
int* x,
int* y,
int* comp) {
2163 if (!decode_jpeg_header(j, SCAN_header)) {
2172 *comp = j->s->img_n;
2176static int stbi_jpeg_info(stbi* s,
int* x,
int* y,
int* comp) {
2179 return stbi_jpeg_info_raw(&j, x, y, comp);
2190 #define ZFAST_BITS 9
2191 #define ZFAST_MASK ((1 << ZFAST_BITS) - 1)
2196 uint16 fast[1 << ZFAST_BITS];
2197 uint16 firstcode[16];
2199 uint16 firstsymbol[16];
2204stbi_inline
static int bitreverse16(
int n) {
2205 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
2206 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
2207 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
2208 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
2212stbi_inline
static int bit_reverse(
int v,
int bits) {
2216 return bitreverse16(v) >> (16 - bits);
2219static int zbuild_huffman(zhuffman* z, uint8* sizelist,
int num) {
2221 int code, next_code[16], sizes[17];
2224 memset(sizes, 0,
sizeof(sizes));
2225 memset(z->fast, 255,
sizeof(z->fast));
2226 for (i = 0; i < num; ++i)
2227 ++sizes[sizelist[i]];
2229 for (i = 1; i < 16; ++i)
2230 assert(sizes[i] <= (1 << i));
2232 for (i = 1; i < 16; ++i) {
2233 next_code[i] = code;
2234 z->firstcode[i] = (uint16)code;
2235 z->firstsymbol[i] = (uint16)k;
2236 code = (code + sizes[i]);
2238 if (code - 1 >= (1 << i))
2239 return e(
"bad codelengths",
"Corrupt JPEG");
2240 z->maxcode[i] = code << (16 - i);
2244 z->maxcode[16] = 0x10000;
2245 for (i = 0; i < num; ++i) {
2246 int s = sizelist[i];
2248 int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
2249 z->size[c] = (uint8)s;
2250 z->value[c] = (uint16)i;
2251 if (s <= ZFAST_BITS) {
2252 int k = bit_reverse(next_code[s], s);
2253 while (k < (1 << ZFAST_BITS)) {
2254 z->fast[k] = (uint16)c;
2271 uint8 *zbuffer, *zbuffer_end;
2280 zhuffman z_length, z_distance;
2283stbi_inline
static int zget8(zbuf* z) {
2284 if (z->zbuffer >= z->zbuffer_end)
2286 return *z->zbuffer++;
2289static void fill_bits(zbuf* z) {
2291 assert(z->code_buffer < (1U << z->num_bits));
2292 z->code_buffer |= zget8(z) << z->num_bits;
2294 }
while (z->num_bits <= 24);
2297stbi_inline
static unsigned int zreceive(zbuf* z,
int n) {
2299 if (z->num_bits < n)
2301 k = z->code_buffer & ((1 << n) - 1);
2302 z->code_buffer >>= n;
2307stbi_inline
static int zhuffman_decode(zbuf* a, zhuffman* z) {
2309 if (a->num_bits < 16)
2311 b = z->fast[a->code_buffer & ZFAST_MASK];
2314 a->code_buffer >>= s;
2321 k = bit_reverse(a->code_buffer, 16);
2322 for (s = ZFAST_BITS + 1;; ++s)
2323 if (k < z->maxcode[s])
2328 b = (k >> (16 - s)) - z->firstcode[s] + z->firstsymbol[s];
2329 assert(z->size[b] == s);
2330 a->code_buffer >>= s;
2335static int expand(zbuf* z,
int n)
2339 if (!z->z_expandable)
2340 return e(
"output buffer limit",
"Corrupt PNG");
2341 cur = (int)(z->zout - z->zout_start);
2342 limit = (int)(z->zout_end - z->zout_start);
2343 while (cur + n > limit)
2345 q = (
char*)realloc(z->zout_start, limit);
2347 return e(
"outofmem",
"Out of memory");
2350 z->zout_end = q + limit;
2354static int length_base[31] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15,
2355 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83,
2356 99, 115, 131, 163, 195, 227, 258, 0, 0};
2358static int length_extra[31] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
2359 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0};
2361static int dist_base[32] = {1, 2, 3, 4, 5, 7, 9, 13,
2362 17, 25, 33, 49, 65, 97, 129, 193,
2363 257, 385, 513, 769, 1025, 1537, 2049, 3073,
2364 4097, 6145, 8193, 12289, 16385, 24577, 0, 0};
2366static int dist_extra[32] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
2367 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
2368 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
2370static int parse_huffman_block(zbuf* a) {
2372 int z = zhuffman_decode(a, &a->z_length);
2375 return e(
"bad huffman code",
"Corrupt PNG");
2376 if (a->zout >= a->zout_end)
2379 *a->zout++ = (char)z;
2386 len = length_base[z];
2387 if (length_extra[z])
2388 len += zreceive(a, length_extra[z]);
2389 z = zhuffman_decode(a, &a->z_distance);
2391 return e(
"bad huffman code",
"Corrupt PNG");
2392 dist = dist_base[z];
2394 dist += zreceive(a, dist_extra[z]);
2395 if (a->zout - a->zout_start < dist)
2396 return e(
"bad dist",
"Corrupt PNG");
2397 if (a->zout + len > a->zout_end)
2398 if (!expand(a, len))
2400 p = (uint8*)(a->zout - dist);
2407static int compute_huffman_codes(zbuf* a) {
2408 static uint8 length_dezigzag[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
2409 11, 4, 12, 3, 13, 2, 14, 1, 15};
2410 zhuffman z_codelength;
2411 uint8 lencodes[286 + 32 + 137];
2412 uint8 codelength_sizes[19];
2415 int hlit = zreceive(a, 5) + 257;
2416 int hdist = zreceive(a, 5) + 1;
2417 int hclen = zreceive(a, 4) + 4;
2419 memset(codelength_sizes, 0,
sizeof(codelength_sizes));
2420 for (i = 0; i < hclen; ++i) {
2421 int s = zreceive(a, 3);
2422 codelength_sizes[length_dezigzag[i]] = (uint8)s;
2424 if (!zbuild_huffman(&z_codelength, codelength_sizes, 19))
2428 while (n < hlit + hdist) {
2429 int c = zhuffman_decode(a, &z_codelength);
2430 assert(c >= 0 && c < 19);
2432 lencodes[n++] = (uint8)c;
2434 c = zreceive(a, 2) + 3;
2435 memset(lencodes + n, lencodes[n - 1], c);
2437 }
else if (c == 17) {
2438 c = zreceive(a, 3) + 3;
2439 memset(lencodes + n, 0, c);
2443 c = zreceive(a, 7) + 11;
2444 memset(lencodes + n, 0, c);
2448 if (n != hlit + hdist)
2449 return e(
"bad codelengths",
"Corrupt PNG");
2450 if (!zbuild_huffman(&a->z_length, lencodes, hlit))
2452 if (!zbuild_huffman(&a->z_distance, lencodes + hlit, hdist))
2457static int parse_uncompressed_block(zbuf* a) {
2460 if (a->num_bits & 7)
2461 zreceive(a, a->num_bits & 7);
2464 while (a->num_bits > 0) {
2465 header[k++] = (uint8)(a->code_buffer & 255);
2466 a->code_buffer >>= 8;
2469 assert(a->num_bits == 0);
2472 header[k++] = (uint8)zget8(a);
2473 len = header[1] * 256 + header[0];
2474 nlen = header[3] * 256 + header[2];
2475 if (nlen != (len ^ 0xffff))
2476 return e(
"zlib corrupt",
"Corrupt PNG");
2477 if (a->zbuffer + len > a->zbuffer_end)
2478 return e(
"read past buffer",
"Corrupt PNG");
2479 if (a->zout + len > a->zout_end)
2480 if (!expand(a, len))
2482 memcpy(a->zout, a->zbuffer, len);
2488static int parse_zlib_header(zbuf* a) {
2493 if ((cmf * 256 + flg) % 31 != 0)
2494 return e(
"bad zlib header",
"Corrupt PNG");
2496 return e(
"no preset dict",
2499 return e(
"bad compression",
"Corrupt PNG");
2505static uint8 default_length[288], default_distance[32];
2506static void init_defaults(
void) {
2508 for (i = 0; i <= 143; ++i)
2509 default_length[i] = 8;
2510 for (; i <= 255; ++i)
2511 default_length[i] = 9;
2512 for (; i <= 279; ++i)
2513 default_length[i] = 7;
2514 for (; i <= 287; ++i)
2515 default_length[i] = 8;
2517 for (i = 0; i <= 31; ++i)
2518 default_distance[i] = 5;
2521int stbi_png_partial;
2523static int parse_zlib(zbuf* a,
int parse_header) {
2526 if (!parse_zlib_header(a))
2531 final = zreceive(a, 1);
2532 type = zreceive(a, 2);
2534 if (!parse_uncompressed_block(a))
2536 }
else if (type == 3) {
2541 if (!default_distance[31])
2543 if (!zbuild_huffman(&a->z_length, default_length, 288))
2545 if (!zbuild_huffman(&a->z_distance, default_distance, 32))
2548 if (!compute_huffman_codes(a))
2551 if (!parse_huffman_block(a))
2554 if (stbi_png_partial && a->zout - a->zout_start > 65536)
2560static int do_zlib(zbuf* a,
char* obuf,
int olen,
int exp,
int parse_header) {
2561 a->zout_start = obuf;
2563 a->zout_end = obuf + olen;
2564 a->z_expandable = exp;
2566 return parse_zlib(a, parse_header);
2569char* stbi_zlib_decode_malloc_guesssize(
const char* buffer,
2574 char* p = (
char*)malloc(initial_size);
2577 a.zbuffer = (uint8*)buffer;
2578 a.zbuffer_end = (uint8*)buffer + len;
2579 if (do_zlib(&a, p, initial_size, 1, 1)) {
2581 *outlen = (int)(a.zout - a.zout_start);
2582 return a.zout_start;
2589char* stbi_zlib_decode_malloc(
char const* buffer,
int len,
int* outlen) {
2590 return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
2593char* stbi_zlib_decode_malloc_guesssize_headerflag(
const char* buffer,
2599 char* p = (
char*)malloc(initial_size);
2602 a.zbuffer = (uint8*)buffer;
2603 a.zbuffer_end = (uint8*)buffer + len;
2604 if (do_zlib(&a, p, initial_size, 1, parse_header)) {
2606 *outlen = (int)(a.zout - a.zout_start);
2607 return a.zout_start;
2614int stbi_zlib_decode_buffer(
char* obuffer,
2616 char const* ibuffer,
2619 a.zbuffer = (uint8*)ibuffer;
2620 a.zbuffer_end = (uint8*)ibuffer + ilen;
2621 if (do_zlib(&a, obuffer, olen, 0, 1))
2622 return (
int)(a.zout - a.zout_start);
2627char* stbi_zlib_decode_noheader_malloc(
char const* buffer,
2631 char* p = (
char*)malloc(16384);
2634 a.zbuffer = (uint8*)buffer;
2635 a.zbuffer_end = (uint8*)buffer + len;
2636 if (do_zlib(&a, p, 16384, 1, 0)) {
2638 *outlen = (int)(a.zout - a.zout_start);
2639 return a.zout_start;
2646int stbi_zlib_decode_noheader_buffer(
char* obuffer,
2648 const char* ibuffer,
2651 a.zbuffer = (uint8*)ibuffer;
2652 a.zbuffer_end = (uint8*)ibuffer + ilen;
2653 if (do_zlib(&a, obuffer, olen, 0, 0))
2654 return (
int)(a.zout - a.zout_start);
2674 #define PNG_TYPE(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
2676static chunk get_chunk_header(stbi* s) {
2678 c.length = get32(s);
2683static int check_png_header(stbi* s) {
2684 static uint8 png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
2686 for (i = 0; i < 8; ++i)
2687 if (get8u(s) != png_sig[i])
2688 return e(
"bad png sig",
"Not a PNG");
2694 uint8 *idata, *expanded, *out;
2707static uint8 first_row_filter[5] = {F_none, F_sub, F_none, F_avg_first,
2710static int paeth(
int a,
int b,
int c) {
2712 int pa = abs(p - a);
2713 int pb = abs(p - b);
2714 int pc = abs(p - c);
2715 if (pa <= pb && pa <= pc)
2723static int create_png_image_raw(png* a,
2730 uint32 i, j, stride = x * out_n;
2732 int img_n = s->img_n;
2733 assert(out_n == s->img_n || out_n == s->img_n + 1);
2734 if (stbi_png_partial)
2736 a->out = (uint8*)malloc(x * y * out_n);
2738 return e(
"outofmem",
"Out of memory");
2739 if (!stbi_png_partial) {
2740 if (s->img_x == x && s->img_y == y) {
2741 if (raw_len != (img_n * x + 1) * y)
2742 return e(
"not enough pixels",
"Corrupt PNG");
2744 if (raw_len < (img_n * x + 1) * y)
2745 return e(
"not enough pixels",
"Corrupt PNG");
2748 for (j = 0; j < y; ++j) {
2749 uint8* cur = a->out + stride * j;
2750 uint8* prior = cur - stride;
2751 int filter = *raw++;
2753 return e(
"invalid filter",
"Corrupt PNG");
2756 filter = first_row_filter[filter];
2758 for (k = 0; k < img_n; ++k) {
2767 cur[k] = raw[k] + prior[k];
2770 cur[k] = raw[k] + (prior[k] >> 1);
2773 cur[k] = (uint8)(raw[k] + paeth(0, prior[k], 0));
2790 if (img_n == out_n) {
2793 for (i = x - 1; i >= 1; --i, raw += img_n, cur += img_n, prior += img_n) \
2794 for (k = 0; k < img_n; ++k)
2796 CASE(F_none) cur[k] = raw[k];
2798 CASE(F_sub) cur[k] = raw[k] + cur[k - img_n];
2800 CASE(F_up) cur[k] = raw[k] + prior[k];
2802 CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k - img_n]) >> 1);
2806 (uint8)(raw[k] + paeth(cur[k - img_n], prior[k], prior[k - img_n]));
2808 CASE(F_avg_first) cur[k] = raw[k] + (cur[k - img_n] >> 1);
2811 cur[k] = (uint8)(raw[k] + paeth(cur[k - img_n], 0, 0));
2816 assert(img_n + 1 == out_n);
2819 for (i = x - 1; i >= 1; \
2820 --i, cur[img_n] = 255, raw += img_n, cur += out_n, prior += out_n) \
2821 for (k = 0; k < img_n; ++k)
2823 CASE(F_none) cur[k] = raw[k];
2825 CASE(F_sub) cur[k] = raw[k] + cur[k - out_n];
2827 CASE(F_up) cur[k] = raw[k] + prior[k];
2829 CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k - out_n]) >> 1);
2833 (uint8)(raw[k] + paeth(cur[k - out_n], prior[k], prior[k - out_n]));
2835 CASE(F_avg_first) cur[k] = raw[k] + (cur[k - out_n] >> 1);
2838 cur[k] = (uint8)(raw[k] + paeth(cur[k - out_n], 0, 0));
2847static int create_png_image(png* a,
2856 return create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x,
2858 save = stbi_png_partial;
2859 stbi_png_partial = 0;
2862 final = (uint8*)malloc(a->s->img_x * a->s->img_y * out_n);
2863 for (p = 0; p < 7; ++p) {
2864 int xorig[] = {0, 4, 0, 2, 0, 1, 0};
2865 int yorig[] = {0, 0, 4, 0, 2, 0, 1};
2866 int xspc[] = {8, 8, 4, 4, 2, 2, 1};
2867 int yspc[] = {8, 8, 8, 4, 4, 2, 2};
2870 x = (a->s->img_x - xorig[p] + xspc[p] - 1) / xspc[p];
2871 y = (a->s->img_y - yorig[p] + yspc[p] - 1) / yspc[p];
2873 if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
2877 for (j = 0; j < y; ++j)
2878 for (i = 0; i < x; ++i)
2879 memcpy(
final + (j * yspc[p] + yorig[p]) * a->s->img_x * out_n +
2880 (i * xspc[p] + xorig[p]) * out_n,
2881 a->out + (j * x + i) * out_n, out_n);
2883 raw += (x * out_n + 1) * y;
2884 raw_len -= (x * out_n + 1) * y;
2889 stbi_png_partial = save;
2893static int compute_transparency(png* z, uint8 tc[3],
int out_n) {
2895 uint32 i, pixel_count = s->img_x * s->img_y;
2900 assert(out_n == 2 || out_n == 4);
2903 for (i = 0; i < pixel_count; ++i) {
2904 p[1] = (p[0] == tc[0] ? 0 : 255);
2908 for (i = 0; i < pixel_count; ++i) {
2909 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
2917static int expand_palette(png* a, uint8* palette,
int len,
int pal_img_n) {
2918 uint32 i, pixel_count = a->s->img_x * a->s->img_y;
2919 uint8 *p, *temp_out, *orig = a->out;
2921 p = (uint8*)malloc(pixel_count * pal_img_n);
2923 return e(
"outofmem",
"Out of memory");
2928 if (pal_img_n == 3) {
2929 for (i = 0; i < pixel_count; ++i) {
2930 int n = orig[i] * 4;
2932 p[1] = palette[n + 1];
2933 p[2] = palette[n + 2];
2937 for (i = 0; i < pixel_count; ++i) {
2938 int n = orig[i] * 4;
2940 p[1] = palette[n + 1];
2941 p[2] = palette[n + 2];
2942 p[3] = palette[n + 3];
2954static int stbi_unpremultiply_on_load = 0;
2955static int stbi_de_iphone_flag = 0;
2957void stbi_set_unpremultiply_on_load(
int flag_true_if_should_unpremultiply) {
2958 stbi_unpremultiply_on_load = flag_true_if_should_unpremultiply;
2960void stbi_convert_iphone_png_to_rgb(
int flag_true_if_should_convert) {
2961 stbi_de_iphone_flag = flag_true_if_should_convert;
2964static void stbi_de_iphone(png* z) {
2966 uint32 i, pixel_count = s->img_x * s->img_y;
2969 if (s->img_out_n == 3) {
2970 for (i = 0; i < pixel_count; ++i) {
2977 assert(s->img_out_n == 4);
2978 if (stbi_unpremultiply_on_load) {
2980 for (i = 0; i < pixel_count; ++i) {
2984 p[0] = p[2] * 255 / a;
2985 p[1] = p[1] * 255 / a;
2995 for (i = 0; i < pixel_count; ++i) {
3005static int parse_png_file(png* z,
int scan,
int req_comp) {
3006 uint8 palette[1024], pal_img_n = 0;
3007 uint8 has_trans = 0, tc[3];
3008 uint32 ioff = 0, idata_limit = 0, i, pal_len = 0;
3009 int first = 1, k, interlace = 0, iphone = 0;
3016 if (!check_png_header(s))
3019 if (scan == SCAN_type)
3023 chunk c = get_chunk_header(s);
3025 case PNG_TYPE(
'C',
'g',
'B',
'I'):
3026 iphone = stbi_de_iphone_flag;
3029 case PNG_TYPE(
'I',
'H',
'D',
'R'): {
3030 int depth, color, comp, filter;
3032 return e(
"multiple IHDR",
"Corrupt PNG");
3035 return e(
"bad IHDR len",
"Corrupt PNG");
3036 s->img_x = get32(s);
3037 if (s->img_x > (1 << 24))
3038 return e(
"too large",
"Very large image (corrupt?)");
3039 s->img_y = get32(s);
3040 if (s->img_y > (1 << 24))
3041 return e(
"too large",
"Very large image (corrupt?)");
3044 return e(
"8bit only",
"PNG not supported: 8-bit only");
3047 return e(
"bad ctype",
"Corrupt PNG");
3051 return e(
"bad ctype",
"Corrupt PNG");
3054 return e(
"bad comp method",
"Corrupt PNG");
3057 return e(
"bad filter method",
"Corrupt PNG");
3058 interlace = get8(s);
3060 return e(
"bad interlace method",
"Corrupt PNG");
3061 if (!s->img_x || !s->img_y)
3062 return e(
"0-pixel image",
"Corrupt PNG");
3064 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
3065 if ((1 << 30) / s->img_x / s->img_n < s->img_y)
3066 return e(
"too large",
"Image too large to decode");
3067 if (scan == SCAN_header)
3073 if ((1 << 30) / s->img_x / 4 < s->img_y)
3074 return e(
"too large",
"Corrupt PNG");
3080 case PNG_TYPE(
'P',
'L',
'T',
'E'): {
3082 return e(
"first not IHDR",
"Corrupt PNG");
3083 if (c.length > 256 * 3)
3084 return e(
"invalid PLTE",
"Corrupt PNG");
3085 pal_len = c.length / 3;
3086 if (pal_len * 3 != c.length)
3087 return e(
"invalid PLTE",
"Corrupt PNG");
3088 for (i = 0; i < pal_len; ++i) {
3089 palette[i * 4 + 0] = get8u(s);
3090 palette[i * 4 + 1] = get8u(s);
3091 palette[i * 4 + 2] = get8u(s);
3092 palette[i * 4 + 3] = 255;
3097 case PNG_TYPE(
't',
'R',
'N',
'S'): {
3099 return e(
"first not IHDR",
"Corrupt PNG");
3101 return e(
"tRNS after IDAT",
"Corrupt PNG");
3103 if (scan == SCAN_header) {
3108 return e(
"tRNS before PLTE",
"Corrupt PNG");
3109 if (c.length > pal_len)
3110 return e(
"bad tRNS len",
"Corrupt PNG");
3112 for (i = 0; i < c.length; ++i)
3113 palette[i * 4 + 3] = get8u(s);
3115 if (!(s->img_n & 1))
3116 return e(
"tRNS with alpha",
"Corrupt PNG");
3117 if (c.length != (uint32)s->img_n * 2)
3118 return e(
"bad tRNS len",
"Corrupt PNG");
3120 for (k = 0; k < s->img_n; ++k)
3121 tc[k] = (uint8)get16(s);
3126 case PNG_TYPE(
'I',
'D',
'A',
'T'): {
3128 return e(
"first not IHDR",
"Corrupt PNG");
3129 if (pal_img_n && !pal_len)
3130 return e(
"no PLTE",
"Corrupt PNG");
3131 if (scan == SCAN_header) {
3132 s->img_n = pal_img_n;
3135 if (ioff + c.length > idata_limit) {
3137 if (idata_limit == 0)
3138 idata_limit = c.length > 4096 ? c.length : 4096;
3139 while (ioff + c.length > idata_limit)
3141 p = (uint8*)realloc(z->idata, idata_limit);
3143 return e(
"outofmem",
"Out of memory");
3146 if (!getn(s, z->idata + ioff, c.length))
3147 return e(
"outofdata",
"Corrupt PNG");
3152 case PNG_TYPE(
'I',
'E',
'N',
'D'): {
3155 return e(
"first not IHDR",
"Corrupt PNG");
3156 if (scan != SCAN_load)
3158 if (z->idata == NULL)
3159 return e(
"no IDAT",
"Corrupt PNG");
3160 z->expanded = (uint8*)stbi_zlib_decode_malloc_guesssize_headerflag(
3161 (
char*)z->idata, ioff, 16384, (
int*)&raw_len, !iphone);
3162 if (z->expanded == NULL)
3166 if ((req_comp == s->img_n + 1 && req_comp != 3 && !pal_img_n) ||
3168 s->img_out_n = s->img_n + 1;
3170 s->img_out_n = s->img_n;
3171 if (!create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace))
3174 if (!compute_transparency(z, tc, s->img_out_n))
3176 if (iphone && s->img_out_n > 2)
3180 s->img_n = pal_img_n;
3181 s->img_out_n = pal_img_n;
3183 s->img_out_n = req_comp;
3184 if (!expand_palette(z, palette, pal_len, s->img_out_n))
3195 return e(
"first not IHDR",
"Corrupt PNG");
3196 if ((c.type & (1 << 29)) == 0) {
3197 #ifndef STBI_NO_FAILURE_STRINGS
3199 static char invalid_chunk[] =
"XXXX chunk not known";
3200 invalid_chunk[0] = (uint8)(c.type >> 24);
3201 invalid_chunk[1] = (uint8)(c.type >> 16);
3202 invalid_chunk[2] = (uint8)(c.type >> 8);
3203 invalid_chunk[3] = (uint8)(c.type >> 0);
3205 return e(invalid_chunk,
"PNG not supported: unknown chunk type");
3215static unsigned char* do_png(png* p,
int* x,
int* y,
int* n,
int req_comp) {
3216 unsigned char* result = NULL;
3217 if (req_comp < 0 || req_comp > 4)
3218 return epuc(
"bad req_comp",
"Internal error");
3219 if (parse_png_file(p, SCAN_load, req_comp)) {
3222 if (req_comp && req_comp != p->s->img_out_n) {
3223 result = convert_format(result, p->s->img_out_n, req_comp, p->s->img_x,
3225 p->s->img_out_n = req_comp;
3244static unsigned char* stbi_png_load(stbi* s,
3251 return do_png(&p, x, y, comp, req_comp);
3254static int stbi_png_test(stbi* s) {
3256 r = check_png_header(s);
3261static int stbi_png_info_raw(png* p,
int* x,
int* y,
int* comp) {
3262 if (!parse_png_file(p, SCAN_header, 0)) {
3271 *comp = p->s->img_n;
3275static int stbi_png_info(stbi* s,
int* x,
int* y,
int* comp) {
3278 return stbi_png_info_raw(&p, x, y, comp);
3283static int bmp_test(stbi* s) {
3294 if (sz == 12 || sz == 40 || sz == 56 || sz == 108)
3299static int stbi_bmp_test(stbi* s) {
3300 int r = bmp_test(s);
3306static int high_bit(
unsigned int z) {
3323static int bitcount(
unsigned int a) {
3324 a = (a & 0x55555555) + ((a >> 1) & 0x55555555);
3325 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
3326 a = (a + (a >> 4)) & 0x0f0f0f0f;
3328 a = (a + (a >> 16));
3332static int shiftsigned(
int v,
int shift,
int bits) {
3350static stbi_uc* bmp_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp) {
3352 unsigned int mr = 0, mg = 0, mb = 0, ma = 0, fake_a = 0;
3353 fake_a = fake_a + 0;
3354 stbi_uc pal[256][4];
3355 int psize = 0, i, j, compress = 0, width;
3356 int bpp, flip_vertically, pad, target, offset, hsz;
3357 if (get8(s) !=
'B' || get8(s) !=
'M')
3358 return epuc(
"not BMP",
"Corrupt BMP");
3362 offset = get32le(s);
3364 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108)
3365 return epuc(
"unknown BMP",
"BMP type not supported: unknown");
3367 s->img_x = get16le(s);
3368 s->img_y = get16le(s);
3370 s->img_x = get32le(s);
3371 s->img_y = get32le(s);
3373 if (get16le(s) != 1)
3374 return epuc(
"bad BMP",
"bad BMP");
3377 return epuc(
"monochrome",
"BMP type not supported: 1-bit");
3378 flip_vertically = ((int)s->img_y) > 0;
3379 s->img_y = abs((
int)s->img_y);
3382 psize = (offset - 14 - 24) / 3;
3384 compress = get32le(s);
3385 if (compress == 1 || compress == 2)
3386 return epuc(
"BMP RLE",
"BMP type not supported: RLE");
3392 if (hsz == 40 || hsz == 56) {
3399 if (bpp == 16 || bpp == 32) {
3401 if (compress == 0) {
3414 }
else if (compress == 3) {
3419 if (mr == mg && mg == mb) {
3421 return epuc(
"bad BMP",
"bad BMP");
3424 return epuc(
"bad BMP",
"bad BMP");
3433 for (i = 0; i < 12; ++i)
3437 psize = (offset - 14 - hsz) >> 2;
3439 s->img_n = ma ? 4 : 3;
3440 if (req_comp && req_comp >= 3)
3444 out = (stbi_uc*)malloc(target * s->img_x * s->img_y);
3446 return epuc(
"outofmem",
"Out of memory");
3449 if (psize == 0 || psize > 256) {
3451 return epuc(
"invalid",
"Corrupt BMP");
3453 for (i = 0; i < psize; ++i) {
3454 pal[i][2] = get8u(s);
3455 pal[i][1] = get8u(s);
3456 pal[i][0] = get8u(s);
3461 skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
3463 width = (s->img_x + 1) >> 1;
3468 return epuc(
"bad bpp",
"Corrupt BMP");
3471 for (j = 0; j < (int)s->img_y; ++j) {
3472 for (i = 0; i < (int)s->img_x; i += 2) {
3473 int v = get8(s), v2 = 0;
3478 out[z++] = pal[v][0];
3479 out[z++] = pal[v][1];
3480 out[z++] = pal[v][2];
3483 if (i + 1 == (
int)s->img_x)
3485 v = (bpp == 8) ? get8(s) : v2;
3486 out[z++] = pal[v][0];
3487 out[z++] = pal[v][1];
3488 out[z++] = pal[v][2];
3495 int rshift = 0, gshift = 0, bshift = 0, ashift = 0, rcount = 0, gcount = 0,
3496 bcount = 0, acount = 0;
3499 skip(s, offset - 14 - hsz);
3501 width = 3 * s->img_x;
3503 width = 2 * s->img_x;
3509 }
else if (bpp == 32) {
3510 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
3514 if (!mr || !mg || !mb) {
3516 return epuc(
"bad masks",
"Corrupt BMP");
3519 rshift = high_bit(mr) - 7;
3520 rcount = bitcount(mr);
3521 gshift = high_bit(mg) - 7;
3522 gcount = bitcount(mr);
3523 bshift = high_bit(mb) - 7;
3524 bcount = bitcount(mr);
3525 ashift = high_bit(ma) - 7;
3526 acount = bitcount(mr);
3528 for (j = 0; j < (int)s->img_y; ++j) {
3530 for (i = 0; i < (int)s->img_x; ++i) {
3532 out[z + 2] = get8u(s);
3533 out[z + 1] = get8u(s);
3534 out[z + 0] = get8u(s);
3536 a = (easy == 2 ? get8(s) : 255);
3538 out[z++] = (uint8)a;
3541 for (i = 0; i < (int)s->img_x; ++i) {
3542 uint32 v = (bpp == 16 ? (uint32)get16le(s) : get32le(s));
3544 out[z++] = (uint8)shiftsigned(v & mr, rshift, rcount);
3545 out[z++] = (uint8)shiftsigned(v & mg, gshift, gcount);
3546 out[z++] = (uint8)shiftsigned(v & mb, bshift, bcount);
3547 a = (ma ? shiftsigned(v & ma, ashift, acount) : 255);
3549 out[z++] = (uint8)a;
3555 if (flip_vertically) {
3557 for (j = 0; j < ((int)s->img_y >> 1); ++j) {
3558 stbi_uc* p1 = out + j * s->img_x * target;
3559 stbi_uc* p2 = out + (s->img_y - 1 - j) * s->img_x * target;
3560 for (i = 0; i < (int)s->img_x * target; ++i) {
3561 t = p1[i], p1[i] = p2[i], p2[i] = t;
3566 if (req_comp && req_comp != target) {
3567 out = convert_format(out, target, req_comp, s->img_x, s->img_y);
3579static stbi_uc* stbi_bmp_load(stbi* s,
3584 return bmp_load(s, x, y, comp, req_comp);
3590static int tga_info(stbi* s,
int* x,
int* y,
int* comp) {
3591 int tga_w, tga_h, tga_comp;
3601 if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) &&
3617 if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) {
3627 *comp = tga_comp / 8;
3631int stbi_tga_info(stbi* s,
int* x,
int* y,
int* comp) {
3632 return tga_info(s, x, y, comp);
3635static int tga_test(stbi* s) {
3642 if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) &&
3655 if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32))
3660static int stbi_tga_test(stbi* s) {
3661 int res = tga_test(s);
3666static stbi_uc* tga_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp) {
3668 int tga_offset = get8u(s);
3669 int tga_indexed = get8u(s);
3670 int tga_image_type = get8u(s);
3672 int tga_palette_start = get16le(s);
3673 int tga_palette_len = get16le(s);
3674 int tga_palette_bits = get8u(s);
3675 int tga_x_origin = get16le(s);
3676 int tga_y_origin = get16le(s);
3677 int tga_width = get16le(s);
3678 int tga_height = get16le(s);
3679 int tga_bits_per_pixel = get8u(s);
3680 int tga_inverted = get8u(s);
3682 unsigned char* tga_data;
3683 unsigned char* tga_palette = NULL;
3685 unsigned char raw_data[4];
3686 unsigned char trans_data[4];
3688 int RLE_repeating = 0;
3689 int read_next_pixel = 1;
3692 if (tga_image_type >= 8) {
3693 tga_image_type -= 8;
3697 tga_inverted = 1 - ((tga_inverted >> 5) & 1);
3701 (tga_width < 1) || (tga_height < 1) || (tga_image_type < 1) ||
3702 (tga_image_type > 3) ||
3703 ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
3704 (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))) {
3711 tga_bits_per_pixel = tga_palette_bits;
3717 if ((req_comp < 1) || (req_comp > 4)) {
3719 req_comp = tga_bits_per_pixel / 8;
3723 *comp = tga_bits_per_pixel / 8;
3725 tga_data = (
unsigned char*)malloc(tga_width * tga_height * req_comp);
3727 return epuc(
"outofmem",
"Out of memory");
3730 skip(s, tga_offset);
3734 skip(s, tga_palette_start);
3737 (
unsigned char*)malloc(tga_palette_len * tga_palette_bits / 8);
3739 return epuc(
"outofmem",
"Out of memory");
3740 if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8)) {
3743 return epuc(
"bad palette",
"Corrupt TGA");
3747 trans_data[0] = trans_data[1] = trans_data[2] = trans_data[3] = 0;
3748 for (i = 0; i < tga_width * tga_height; ++i) {
3751 if (RLE_count == 0) {
3753 int RLE_cmd = get8u(s);
3754 RLE_count = 1 + (RLE_cmd & 127);
3755 RLE_repeating = RLE_cmd >> 7;
3756 read_next_pixel = 1;
3757 }
else if (!RLE_repeating) {
3758 read_next_pixel = 1;
3761 read_next_pixel = 1;
3764 if (read_next_pixel) {
3768 int pal_idx = get8u(s);
3769 if (pal_idx >= tga_palette_len) {
3773 pal_idx *= tga_bits_per_pixel / 8;
3774 for (j = 0; j * 8 < tga_bits_per_pixel; ++j) {
3775 raw_data[j] = tga_palette[pal_idx + j];
3779 for (j = 0; j * 8 < tga_bits_per_pixel; ++j) {
3780 raw_data[j] = get8u(s);
3784 switch (tga_bits_per_pixel) {
3787 trans_data[0] = raw_data[0];
3788 trans_data[1] = raw_data[0];
3789 trans_data[2] = raw_data[0];
3790 trans_data[3] = 255;
3794 trans_data[0] = raw_data[0];
3795 trans_data[1] = raw_data[0];
3796 trans_data[2] = raw_data[0];
3797 trans_data[3] = raw_data[1];
3801 trans_data[0] = raw_data[2];
3802 trans_data[1] = raw_data[1];
3803 trans_data[2] = raw_data[0];
3804 trans_data[3] = 255;
3808 trans_data[0] = raw_data[2];
3809 trans_data[1] = raw_data[1];
3810 trans_data[2] = raw_data[0];
3811 trans_data[3] = raw_data[3];
3815 read_next_pixel = 0;
3821 tga_data[i * req_comp + 0] =
3822 compute_y(trans_data[0], trans_data[1], trans_data[2]);
3826 tga_data[i * req_comp + 0] =
3827 compute_y(trans_data[0], trans_data[1], trans_data[2]);
3828 tga_data[i * req_comp + 1] = trans_data[3];
3832 tga_data[i * req_comp + 0] = trans_data[0];
3833 tga_data[i * req_comp + 1] = trans_data[1];
3834 tga_data[i * req_comp + 2] = trans_data[2];
3838 tga_data[i * req_comp + 0] = trans_data[0];
3839 tga_data[i * req_comp + 1] = trans_data[1];
3840 tga_data[i * req_comp + 2] = trans_data[2];
3841 tga_data[i * req_comp + 3] = trans_data[3];
3849 for (j = 0; j * 2 < tga_height; ++j) {
3850 int index1 = j * tga_width * req_comp;
3851 int index2 = (tga_height - 1 - j) * tga_width * req_comp;
3852 for (i = tga_width * req_comp; i > 0; --i) {
3853 unsigned char temp = tga_data[index1];
3854 tga_data[index1] = tga_data[index2];
3855 tga_data[index2] = temp;
3862 if (tga_palette != NULL) {
3867 tga_palette_start = tga_palette_len = tga_palette_bits = tga_x_origin =
3873static stbi_uc* stbi_tga_load(stbi* s,
3878 return tga_load(s, x, y, comp, req_comp);
3885static int psd_test(stbi* s) {
3886 if (get32(s) != 0x38425053)
3892static int stbi_psd_test(stbi* s) {
3893 int r = psd_test(s);
3898static stbi_uc* psd_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp) {
3900 int channelCount, compression;
3901 int channel, i, count, len;
3906 if (get32(s) != 0x38425053)
3907 return epuc(
"not PSD",
"Corrupt PSD image");
3911 return epuc(
"wrong version",
"Unsupported version of PSD image");
3917 channelCount = get16(s);
3918 if (channelCount < 0 || channelCount > 16)
3919 return epuc(
"wrong channel count",
3920 "Unsupported number of channels in PSD image");
3928 return epuc(
"unsupported bit depth",
"PSD bit depth is not 8 bit");
3941 return epuc(
"wrong color format",
"PSD is not in RGB color format");
3957 compression = get16(s);
3958 if (compression > 1)
3959 return epuc(
"bad compression",
"PSD has an unknown compression format");
3962 out = (stbi_uc*)malloc(4 * w * h);
3964 return epuc(
"outofmem",
"Out of memory");
3982 skip(s, h * channelCount * 2);
3985 for (channel = 0; channel < 4; channel++) {
3989 if (channel >= channelCount) {
3991 for (i = 0; i < pixelCount; i++)
3992 *p = (channel == 3 ? 255 : 0), p += 4;
3996 while (count < pixelCount) {
4000 }
else if (len < 128) {
4009 }
else if (len > 128) {
4033 for (channel = 0; channel < 4; channel++) {
4037 if (channel > channelCount) {
4039 for (i = 0; i < pixelCount; i++)
4040 *p = channel == 3 ? 255 : 0, p += 4;
4043 for (i = 0; i < pixelCount; i++)
4044 *p = get8u(s), p += 4;
4049 if (req_comp && req_comp != 4) {
4050 out = convert_format(out, 4, req_comp, w, h);
4056 *comp = channelCount;
4063static stbi_uc* stbi_psd_load(stbi* s,
4068 return psd_load(s, x, y, comp, req_comp);
4078static int pic_is4(stbi* s,
const char* str) {
4080 for (i = 0; i < 4; ++i)
4081 if (get8(s) != (stbi_uc)str[i])
4087static int pic_test(stbi* s) {
4090 if (!pic_is4(s,
"\x53\x80\xF6\x34"))
4093 for (i = 0; i < 84; ++i)
4096 if (!pic_is4(s,
"PICT"))
4103 stbi_uc size, type, channel;
4106static stbi_uc* pic_readval(stbi* s,
int channel, stbi_uc* dest) {
4109 for (i = 0; i < 4; ++i, mask >>= 1) {
4110 if (channel & mask) {
4112 return epuc(
"bad file",
"PIC file too short");
4120static void pic_copyval(
int channel, stbi_uc* dest,
const stbi_uc* src) {
4123 for (i = 0; i < 4; ++i, mask >>= 1)
4128static stbi_uc* pic_load2(stbi* s,
4133 int act_comp = 0, num_packets = 0, y, chained;
4134 pic_packet_t packets[10];
4139 pic_packet_t* packet;
4141 if (num_packets ==
sizeof(packets) /
sizeof(packets[0]))
4142 return epuc(
"bad format",
"too many packets");
4144 packet = &packets[num_packets++];
4147 packet->size = get8u(s);
4148 packet->type = get8u(s);
4149 packet->channel = get8u(s);
4151 act_comp |= packet->channel;
4154 return epuc(
"bad file",
"file too short (reading packets)");
4155 if (packet->size != 8)
4156 return epuc(
"bad format",
"packet isn't 8bpp");
4159 *comp = (act_comp & 0x10 ? 4 : 3);
4161 for (y = 0; y < height; ++y) {
4164 for (packet_idx = 0; packet_idx < num_packets; ++packet_idx) {
4165 pic_packet_t* packet = &packets[packet_idx];
4166 stbi_uc* dest = result + y * width * 4;
4168 switch (packet->type) {
4170 return epuc(
"bad format",
"packet has bad compression type");
4175 for (x = 0; x < width; ++x, dest += 4)
4176 if (!pic_readval(s, packet->channel, dest))
4183 int left = width, i;
4186 stbi_uc count, value[4];
4190 return epuc(
"bad file",
"file too short (pure read count)");
4193 count = (uint8)left;
4195 if (!pic_readval(s, packet->channel, value))
4198 for (i = 0; i < count; ++i, dest += 4)
4199 pic_copyval(packet->channel, dest, value);
4207 int count = get8(s), i;
4209 return epuc(
"bad file",
"file too short (mixed read count)");
4220 return epuc(
"bad file",
"scanline overrun");
4222 if (!pic_readval(s, packet->channel, value))
4225 for (i = 0; i < count; ++i, dest += 4)
4226 pic_copyval(packet->channel, dest, value);
4230 return epuc(
"bad file",
"scanline overrun");
4232 for (i = 0; i < count; ++i, dest += 4)
4233 if (!pic_readval(s, packet->channel, dest))
4247static stbi_uc* pic_load(stbi* s,
int* px,
int* py,
int* comp,
int req_comp) {
4251 for (i = 0; i < 92; ++i)
4257 return epuc(
"bad file",
"file too short (pic header)");
4258 if ((1 << 28) / x < y)
4259 return epuc(
"too large",
"Image too large to decode");
4266 result = (stbi_uc*)malloc(x * y * 4);
4267 memset(result, 0xff, x * y * 4);
4269 if (!pic_load2(s, x, y, comp, result)) {
4277 result = convert_format(result, 4, req_comp, x, y);
4282static int stbi_pic_test(stbi* s) {
4283 int r = pic_test(s);
4288static stbi_uc* stbi_pic_load(stbi* s,
4293 return pic_load(s, x, y, comp, req_comp);
4298typedef struct stbi_gif_lzw_struct {
4304typedef struct stbi_gif_struct {
4307 int flags, bgindex, ratio, transparent, eflags;
4310 stbi_gif_lzw codes[4096];
4314 int start_x, start_y;
4320static int gif_test(stbi* s) {
4322 if (get8(s) !=
'G' || get8(s) !=
'I' || get8(s) !=
'F' || get8(s) !=
'8')
4325 if (sz !=
'9' && sz !=
'7')
4332static int stbi_gif_test(stbi* s) {
4333 int r = gif_test(s);
4338static void stbi_gif_parse_colortable(stbi* s,
4343 for (i = 0; i < num_entries; ++i) {
4344 pal[i][2] = get8u(s);
4345 pal[i][1] = get8u(s);
4346 pal[i][0] = get8u(s);
4347 pal[i][3] = transp ? 0 : 255;
4351static int stbi_gif_header(stbi* s, stbi_gif* g,
int* comp,
int is_info) {
4353 if (get8(s) !=
'G' || get8(s) !=
'I' || get8(s) !=
'F' || get8(s) !=
'8')
4354 return e(
"not GIF",
"Corrupt GIF");
4357 if (version !=
'7' && version !=
'9')
4358 return e(
"not GIF",
"Corrupt GIF");
4360 return e(
"not GIF",
"Corrupt GIF");
4362 failure_reason =
"";
4366 g->bgindex = get8(s);
4368 g->transparent = -1;
4377 if (g->flags & 0x80)
4378 stbi_gif_parse_colortable(s, g->pal, 2 << (g->flags & 7), -1);
4383static int stbi_gif_info_raw(stbi* s,
int* x,
int* y,
int* comp) {
4385 if (!stbi_gif_header(s, &g, comp, 1)) {
4396static void stbi_out_gif_code(stbi_gif* g, uint16 code) {
4401 if (g->codes[code].prefix >= 0)
4402 stbi_out_gif_code(g, g->codes[code].prefix);
4404 if (g->cur_y >= g->max_y)
4407 p = &g->out[g->cur_x + g->cur_y];
4408 c = &g->color_table[g->codes[code].suffix * 4];
4418 if (g->cur_x >= g->max_x) {
4419 g->cur_x = g->start_x;
4420 g->cur_y += g->step;
4422 while (g->cur_y >= g->max_y && g->parse > 0) {
4423 g->step = (1 << g->parse) * g->line_size;
4424 g->cur_y = g->start_y + (g->step >> 1);
4430static uint8* stbi_process_gif_raster(stbi* s, stbi_gif* g) {
4434 int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
4438 clear = 1 << lzw_cs;
4440 codesize = lzw_cs + 1;
4441 codemask = (1 << codesize) - 1;
4444 for (code = 0; code < clear; code++) {
4445 g->codes[code].prefix = -1;
4446 g->codes[code].first = (uint8)code;
4447 g->codes[code].suffix = (uint8)code;
4456 if (valid_bits < codesize) {
4463 bits |= (int32)get8(s) << valid_bits;
4466 int32 code = bits & codemask;
4468 valid_bits -= codesize;
4470 if (code == clear) {
4471 codesize = lzw_cs + 1;
4472 codemask = (1 << codesize) - 1;
4476 }
else if (code == clear + 1) {
4478 while ((len = get8(s)) > 0)
4481 }
else if (code <= avail) {
4483 return epuc(
"no clear code",
"Corrupt GIF");
4486 p = &g->codes[avail++];
4488 return epuc(
"too many codes",
"Corrupt GIF");
4489 p->prefix = (int16)oldcode;
4490 p->first = g->codes[oldcode].first;
4491 p->suffix = (code == avail) ? p->first : g->codes[code].first;
4492 }
else if (code == avail)
4493 return epuc(
"illegal code in raster",
"Corrupt GIF");
4495 stbi_out_gif_code(g, (uint16)code);
4497 if ((avail & codemask) == 0 && avail <= 0x0FFF) {
4499 codemask = (1 << codesize) - 1;
4504 return epuc(
"illegal code in raster",
"Corrupt GIF");
4510static void stbi_fill_gif_background(stbi_gif* g) {
4512 uint8* c = g->pal[g->bgindex];
4514 for (i = 0; i < g->w * g->h * 4; i += 4) {
4515 uint8* p = &g->out[i];
4525static uint8* stbi_gif_load_next(stbi* s,
4533 if (!stbi_gif_header(s, g, comp, 0))
4535 g->out = (uint8*)malloc(4 * g->w * g->h);
4537 return epuc(
"outofmem",
"Out of memory");
4538 stbi_fill_gif_background(g);
4541 if (((g->eflags & 0x1C) >> 2) == 3) {
4543 g->out = (uint8*)malloc(4 * g->w * g->h);
4545 return epuc(
"outofmem",
"Out of memory");
4546 memcpy(g->out, old_out, g->w * g->h * 4);
4561 if (((x + w) > (g->w)) || ((y + h) > (g->h)))
4562 return epuc(
"bad Image Descriptor",
"Corrupt GIF");
4564 g->line_size = g->w * 4;
4566 g->start_y = y * g->line_size;
4567 g->max_x = g->start_x + w * 4;
4568 g->max_y = g->start_y + h * g->line_size;
4569 g->cur_x = g->start_x;
4570 g->cur_y = g->start_y;
4572 g->lflags = get8(s);
4574 if (g->lflags & 0x40) {
4575 g->step = 8 * g->line_size;
4578 g->step = g->line_size;
4582 if (g->lflags & 0x80) {
4583 stbi_gif_parse_colortable(s, g->lpal, 2 << (g->lflags & 7),
4584 g->eflags & 0x01 ? g->transparent : -1);
4585 g->color_table = (uint8*)g->lpal;
4586 }
else if (g->flags & 0x80) {
4587 for (i = 0; i < 256;
4590 if (g->transparent >= 0 && (g->eflags & 0x01))
4591 g->pal[g->transparent][3] = 0;
4592 g->color_table = (uint8*)g->pal;
4594 return epuc(
"missing color table",
"Corrupt GIF");
4596 o = stbi_process_gif_raster(s, g);
4600 if (req_comp && req_comp != 4)
4601 o = convert_format(o, 4, req_comp, g->w, g->h);
4608 if (get8(s) == 0xF9) {
4611 g->eflags = get8(s);
4613 g->transparent = get8(s);
4619 while ((len = get8(s)) != 0)
4628 return epuc(
"unknown code",
"Corrupt GIF");
4633static stbi_uc* stbi_gif_load(stbi* s,
4640 memset(&g, 0,
sizeof(g));
4642 u = stbi_gif_load_next(s, &g, comp, req_comp);
4653static int stbi_gif_info(stbi* s,
int* x,
int* y,
int* comp) {
4654 return stbi_gif_info_raw(s, x, y, comp);
4661static int hdr_test(stbi* s) {
4662 const char* signature =
"#?RADIANCE\n";
4664 for (i = 0; signature[i]; ++i)
4665 if (get8(s) != signature[i])
4670static int stbi_hdr_test(stbi* s) {
4671 int r = hdr_test(s);
4676 #define HDR_BUFLEN 1024
4677static char* hdr_gettoken(stbi* z,
char* buffer) {
4683 while (!at_eof(z) && c !=
'\n') {
4685 if (len == HDR_BUFLEN - 1) {
4687 while (!at_eof(z) && get8(z) !=
'\n')
4698static void hdr_convert(
float* output, stbi_uc* input,
int req_comp) {
4699 if (input[3] != 0) {
4702 f1 = (float)ldexp(1.0f, input[3] - (
int)(128 + 8));
4704 output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
4706 output[0] = input[0] * f1;
4707 output[1] = input[1] * f1;
4708 output[2] = input[2] * f1;
4719 output[0] = output[1] = output[2] = 0;
4730static float* hdr_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp) {
4731 char buffer[HDR_BUFLEN];
4738 unsigned char count, value;
4739 int i, j, k, c1, c2, z;
4742 if (strcmp(hdr_gettoken(s, buffer),
"#?RADIANCE") != 0)
4743 return epf(
"not HDR",
"Corrupt HDR image");
4747 token = hdr_gettoken(s, buffer);
4750 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0)
4755 return epf(
"unsupported format",
"Unsupported HDR format");
4759 token = hdr_gettoken(s, buffer);
4760 if (strncmp(token,
"-Y ", 3))
4761 return epf(
"unsupported data layout",
"Unsupported HDR format");
4763 height = strtol(token, &token, 10);
4764 while (*token ==
' ')
4766 if (strncmp(token,
"+X ", 3))
4767 return epf(
"unsupported data layout",
"Unsupported HDR format");
4769 width = strtol(token, NULL, 10);
4779 hdr_data = (
float*)malloc(height * width * req_comp *
sizeof(
float));
4783 if (width < 8 || width >= 32768) {
4785 for (j = 0; j < height; ++j) {
4786 for (i = 0; i < width; ++i) {
4790 hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe,
4798 for (j = 0; j < height; ++j) {
4802 if (c1 != 2 || c2 != 2 || (len & 0x80)) {
4807 rgbe[0] = (uint8)c1;
4808 rgbe[1] = (uint8)c2;
4809 rgbe[2] = (uint8)len;
4810 rgbe[3] = (uint8)get8u(s);
4811 hdr_convert(hdr_data, rgbe, req_comp);
4815 goto main_decode_loop;
4822 return epf(
"invalid decoded scanline length",
"corrupt HDR");
4824 if (scanline == NULL)
4825 scanline = (stbi_uc*)malloc(width * 4);
4827 for (k = 0; k < 4; ++k) {
4835 for (z = 0; z < count; ++z)
4836 scanline[i++ * 4 + k] = value;
4839 for (z = 0; z < count; ++z)
4840 scanline[i++ * 4 + k] = get8u(s);
4844 for (i = 0; i < width; ++i)
4845 hdr_convert(hdr_data + (j * width + i) * req_comp, scanline + i * 4,
4854static float* stbi_hdr_load(stbi* s,
int* x,
int* y,
int* comp,
int req_comp) {
4855 return hdr_load(s, x, y, comp, req_comp);
4858static int stbi_hdr_info(stbi* s,
int* x,
int* y,
int* comp) {
4859 char buffer[HDR_BUFLEN];
4863 if (strcmp(hdr_gettoken(s, buffer),
"#?RADIANCE") != 0) {
4869 token = hdr_gettoken(s, buffer);
4872 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0)
4880 token = hdr_gettoken(s, buffer);
4881 if (strncmp(token,
"-Y ", 3)) {
4886 *y = strtol(token, &token, 10);
4887 while (*token ==
' ')
4889 if (strncmp(token,
"+X ", 3)) {
4894 *x = strtol(token, NULL, 10);
4900static int stbi_bmp_info(stbi* s,
int* x,
int* y,
int* comp) {
4902 if (get8(s) !=
'B' || get8(s) !=
'M') {
4908 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) {
4919 if (get16le(s) != 1) {
4923 *comp = get16le(s) / 8;
4927static int stbi_psd_info(stbi* s,
int* x,
int* y,
int* comp) {
4929 if (get32(s) != 0x38425053) {
4933 if (get16(s) != 1) {
4938 channelCount = get16(s);
4939 if (channelCount < 0 || channelCount > 16) {
4945 if (get16(s) != 8) {
4949 if (get16(s) != 3) {
4957static int stbi_pic_info(stbi* s,
int* x,
int* y,
int* comp) {
4958 int act_comp = 0, num_packets = 0, chained;
4959 pic_packet_t packets[10];
4967 if ((*x) != 0 && (1 << 28) / (*x) < (*y)) {
4975 pic_packet_t* packet;
4977 if (num_packets ==
sizeof(packets) /
sizeof(packets[0]))
4980 packet = &packets[num_packets++];
4982 packet->size = get8u(s);
4983 packet->type = get8u(s);
4984 packet->channel = get8u(s);
4985 act_comp |= packet->channel;
4991 if (packet->size != 8) {
4997 *comp = (act_comp & 0x10 ? 4 : 3);
5002static int stbi_info_main(stbi* s,
int* x,
int* y,
int* comp) {
5003 if (stbi_jpeg_info(s, x, y, comp))
5005 if (stbi_png_info(s, x, y, comp))
5007 if (stbi_gif_info(s, x, y, comp))
5009 if (stbi_bmp_info(s, x, y, comp))
5011 if (stbi_psd_info(s, x, y, comp))
5013 if (stbi_pic_info(s, x, y, comp))
5016 if (stbi_hdr_info(s, x, y, comp))
5020 if (stbi_tga_info(s, x, y, comp))
5022 return e(
"unknown image type",
"Image not of any known type, or corrupt");
5025 #ifndef STBI_NO_STDIO
5026int stbi_info(
char const* filename,
int* x,
int* y,
int* comp) {
5027 FILE* f = fopen(filename,
"rb");
5030 return e(
"can't fopen",
"Unable to open file");
5031 result = stbi_info_from_file(f, x, y, comp);
5036int stbi_info_from_file(FILE* f,
int* x,
int* y,
int* comp) {
5039 long pos = ftell(f);
5041 r = stbi_info_main(&s, x, y, comp);
5042 fseek(f, pos, SEEK_SET);
5047int stbi_info_from_memory(stbi_uc
const* buffer,
5053 start_mem(&s, buffer, len);
5054 return stbi_info_main(&s, x, y, comp);
5057int stbi_info_from_callbacks(stbi_io_callbacks
const* c,
5063 start_callbacks(&s, (stbi_io_callbacks*)c, user);
5064 return stbi_info_main(&s, x, y, comp);