101 #ifndef STBI_INCLUDE_STB_IMAGE_H
102 #define STBI_INCLUDE_STB_IMAGE_H
305 #ifndef STBI_NO_STDIO
307 #endif // STBI_NO_STDIO
309 #define STBI_VERSION 1
327 #ifdef STB_IMAGE_STATIC
328 #define STBIDEF static
330 #define STBIDEF extern
343 int (*read)(
void *user,
char *data,
int size);
346 void (*skip)(
void *user,
int n);
348 int (*eof)(
void *user);
357 int *y,
int *channels_in_file,
358 int desired_channels);
360 void *user,
int *x,
int *y,
361 int *channels_in_file,
362 int desired_channels);
364 #ifndef STBI_NO_STDIO
366 int *channels_in_file,
int desired_channels);
368 int *channels_in_file,
369 int desired_channels);
380 int *x,
int *y,
int *channels_in_file,
381 int desired_channels);
383 void *user,
int *x,
int *y,
384 int *channels_in_file,
385 int desired_channels);
387 #ifndef STBI_NO_STDIO
389 int *channels_in_file,
int desired_channels);
391 int *channels_in_file,
392 int desired_channels);
399 #ifndef STBI_NO_LINEAR
401 int *y,
int *channels_in_file,
402 int desired_channels);
404 void *user,
int *x,
int *y,
405 int *channels_in_file,
406 int desired_channels);
408 #ifndef STBI_NO_STDIO
410 int *channels_in_file,
int desired_channels);
412 int *channels_in_file,
413 int desired_channels);
420 #endif // STBI_NO_HDR
422 #ifndef STBI_NO_LINEAR
425 #endif // STBI_NO_LINEAR
431 #ifndef STBI_NO_STDIO
434 #endif // STBI_NO_STDIO
447 int *x,
int *y,
int *comp);
449 #ifndef STBI_NO_STDIO
472 int initial_size,
int *outlen);
480 const char *ibuffer,
int ilen);
485 const char *ibuffer,
int ilen);
494 #endif // STBI_INCLUDE_STB_IMAGE_H
496 #ifdef STB_IMAGE_IMPLEMENTATION
498 #if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || \
499 defined(STBI_ONLY_BMP) || defined(STBI_ONLY_TGA) || \
500 defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) || \
501 defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || \
502 defined(STBI_ONLY_PNM) || defined(STBI_ONLY_ZLIB)
503 #ifndef STBI_ONLY_JPEG
506 #ifndef STBI_ONLY_PNG
509 #ifndef STBI_ONLY_BMP
512 #ifndef STBI_ONLY_PSD
515 #ifndef STBI_ONLY_TGA
518 #ifndef STBI_ONLY_GIF
521 #ifndef STBI_ONLY_HDR
524 #ifndef STBI_ONLY_PIC
527 #ifndef STBI_ONLY_PNM
532 #if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && \
533 !defined(STBI_NO_ZLIB)
543 #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
547 #ifndef STBI_NO_STDIO
553 #define STBI_ASSERT(x) assert(x)
558 #define stbi_inline inline
563 #define stbi_inline __forceinline
567 typedef unsigned short stbi__uint16;
568 typedef signed short stbi__int16;
569 typedef unsigned int stbi__uint32;
570 typedef signed int stbi__int32;
573 typedef uint16_t stbi__uint16;
574 typedef int16_t stbi__int16;
575 typedef uint32_t stbi__uint32;
576 typedef int32_t stbi__int32;
580 typedef unsigned char validate_uint32[
sizeof(stbi__uint32) == 4 ? 1 : -1];
583 #define STBI_NOTUSED(v) (void)(v)
585 #define STBI_NOTUSED(v) (void)sizeof(v)
589 #define STBI_HAS_LROTL
592 #ifdef STBI_HAS_LROTL
593 #define stbi_lrot(x, y) _lrotl(x, y)
595 #define stbi_lrot(x, y) (((x) << (y)) | ((x) >> (32 - (y))))
598 #if defined(STBI_MALLOC) && defined(STBI_FREE) && \
599 (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))
601 #elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && \
602 !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED)
606 "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)."
610 #define STBI_MALLOC(sz) malloc(sz)
611 #define STBI_REALLOC(p, newsz) realloc(p, newsz)
612 #define STBI_FREE(p) free(p)
615 #ifndef STBI_REALLOC_SIZED
616 #define STBI_REALLOC_SIZED(p, oldsz, newsz) STBI_REALLOC(p, newsz)
620 #if defined(__x86_64__) || defined(_M_X64)
621 #define STBI__X64_TARGET
622 #elif defined(__i386) || defined(_M_IX86)
623 #define STBI__X86_TARGET
626 #if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && \
627 !defined(STBI_NO_SIMD)
638 #if defined(__MINGW32__) && defined(STBI__X86_TARGET) && \
639 !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)
656 #if !defined(STBI_NO_SIMD) && \
657 (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))
659 #include <emmintrin.h>
663 #if _MSC_VER >= 1400 // not VC6
665 static int stbi__cpuid3(
void)
672 static int stbi__cpuid3(
void)
684 #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
686 static int stbi__sse2_available(
void)
688 int info3 = stbi__cpuid3();
689 return ((info3 >> 26) & 1) != 0;
691 #else // assume GCC-style if not VC++
692 #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
694 static int stbi__sse2_available(
void)
705 #if defined(STBI_NO_SIMD) && defined(STBI_NEON)
710 #include <arm_neon.h>
712 #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
715 #ifndef STBI_SIMD_ALIGN
716 #define STBI_SIMD_ALIGN(type, name) type name
726 stbi__uint32 img_x, img_y;
727 int img_n, img_out_n;
732 int read_from_callbacks;
736 stbi_uc *img_buffer, *img_buffer_end;
737 stbi_uc *img_buffer_original, *img_buffer_original_end;
740 static void stbi__refill_buffer(stbi__context *s);
743 static void stbi__start_mem(stbi__context *s,
stbi_uc const *buffer,
int len)
746 s->read_from_callbacks = 0;
747 s->img_buffer = s->img_buffer_original = (
stbi_uc *)buffer;
748 s->img_buffer_end = s->img_buffer_original_end = (
stbi_uc *)buffer + len;
756 s->io_user_data = user;
757 s->buflen =
sizeof(s->buffer_start);
758 s->read_from_callbacks = 1;
759 s->img_buffer_original = s->buffer_start;
760 stbi__refill_buffer(s);
761 s->img_buffer_original_end = s->img_buffer_end;
764 #ifndef STBI_NO_STDIO
766 static int stbi__stdio_read(
void *user,
char *data,
int size)
768 return (
int)fread(data, 1, size, (FILE *)user);
771 static void stbi__stdio_skip(
void *user,
int n)
773 fseek((FILE *)user, n, SEEK_CUR);
776 static int stbi__stdio_eof(
void *user)
778 return feof((FILE *)user);
787 static void stbi__start_file(stbi__context *s, FILE *f)
789 stbi__start_callbacks(s, &stbi__stdio_callbacks, (
void *)f);
794 #endif // !STBI_NO_STDIO
796 static void stbi__rewind(stbi__context *s)
802 s->img_buffer = s->img_buffer_original;
803 s->img_buffer_end = s->img_buffer_original_end;
806 enum { STBI_ORDER_RGB, STBI_ORDER_BGR };
809 int bits_per_channel;
815 static int stbi__jpeg_test(stbi__context *s);
816 static void *stbi__jpeg_load(stbi__context *s,
int *x,
int *y,
int *comp,
817 int req_comp, stbi__result_info *ri);
818 static int stbi__jpeg_info(stbi__context *s,
int *x,
int *y,
int *comp);
822 static int stbi__png_test(stbi__context *s);
823 static void *stbi__png_load(stbi__context *s,
int *x,
int *y,
int *comp,
824 int req_comp, stbi__result_info *ri);
825 static int stbi__png_info(stbi__context *s,
int *x,
int *y,
int *comp);
829 static int stbi__bmp_test(stbi__context *s);
830 static void *stbi__bmp_load(stbi__context *s,
int *x,
int *y,
int *comp,
831 int req_comp, stbi__result_info *ri);
832 static int stbi__bmp_info(stbi__context *s,
int *x,
int *y,
int *comp);
836 static int stbi__tga_test(stbi__context *s);
837 static void *stbi__tga_load(stbi__context *s,
int *x,
int *y,
int *comp,
838 int req_comp, stbi__result_info *ri);
839 static int stbi__tga_info(stbi__context *s,
int *x,
int *y,
int *comp);
843 static int stbi__psd_test(stbi__context *s);
844 static void *stbi__psd_load(stbi__context *s,
int *x,
int *y,
int *comp,
845 int req_comp, stbi__result_info *ri,
int bpc);
846 static int stbi__psd_info(stbi__context *s,
int *x,
int *y,
int *comp);
850 static int stbi__hdr_test(stbi__context *s);
851 static float *stbi__hdr_load(stbi__context *s,
int *x,
int *y,
int *comp,
852 int req_comp, stbi__result_info *ri);
853 static int stbi__hdr_info(stbi__context *s,
int *x,
int *y,
int *comp);
857 static int stbi__pic_test(stbi__context *s);
858 static void *stbi__pic_load(stbi__context *s,
int *x,
int *y,
int *comp,
859 int req_comp, stbi__result_info *ri);
860 static int stbi__pic_info(stbi__context *s,
int *x,
int *y,
int *comp);
864 static int stbi__gif_test(stbi__context *s);
865 static void *stbi__gif_load(stbi__context *s,
int *x,
int *y,
int *comp,
866 int req_comp, stbi__result_info *ri);
867 static int stbi__gif_info(stbi__context *s,
int *x,
int *y,
int *comp);
871 static int stbi__pnm_test(stbi__context *s);
872 static void *stbi__pnm_load(stbi__context *s,
int *x,
int *y,
int *comp,
873 int req_comp, stbi__result_info *ri);
874 static int stbi__pnm_info(stbi__context *s,
int *x,
int *y,
int *comp);
878 static const char *stbi__g_failure_reason;
882 return stbi__g_failure_reason;
885 static int stbi__err(
const char *str)
887 stbi__g_failure_reason = str;
891 static void *stbi__malloc(
size_t size)
893 return STBI_MALLOC(size);
908 static int stbi__addsizes_valid(
int a,
int b)
916 return a <= INT_MAX - b;
921 static int stbi__mul2sizes_valid(
int a,
int b)
928 return a <= INT_MAX / b;
932 static int stbi__mad2sizes_valid(
int a,
int b,
int add)
934 return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a * b, add);
938 static int stbi__mad3sizes_valid(
int a,
int b,
int c,
int add)
940 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a * b, c) &&
941 stbi__addsizes_valid(a * b * c, add);
946 static int stbi__mad4sizes_valid(
int a,
int b,
int c,
int d,
int add)
948 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a * b, c) &&
949 stbi__mul2sizes_valid(a * b * c, d) &&
950 stbi__addsizes_valid(a * b * c * d, add);
954 static void *stbi__malloc_mad2(
int a,
int b,
int add)
956 if (!stbi__mad2sizes_valid(a, b, add))
958 return stbi__malloc(a * b + add);
961 static void *stbi__malloc_mad3(
int a,
int b,
int c,
int add)
963 if (!stbi__mad3sizes_valid(a, b, c, add))
965 return stbi__malloc(a * b * c + add);
968 static void *stbi__malloc_mad4(
int a,
int b,
int c,
int d,
int add)
970 if (!stbi__mad4sizes_valid(a, b, c, d, add))
972 return stbi__malloc(a * b * c * d + add);
979 #ifdef STBI_NO_FAILURE_STRINGS
980 #define stbi__err(x, y) 0
981 #elif defined(STBI_FAILURE_USERMSG)
982 #define stbi__err(x, y) stbi__err(y)
984 #define stbi__err(x, y) stbi__err(x)
987 #define stbi__errpf(x, y) ((float *)(size_t)(stbi__err(x, y) ? NULL : NULL))
988 #define stbi__errpuc(x, y) \
989 ((unsigned char *)(size_t)(stbi__err(x, y) ? NULL : NULL))
993 STBI_FREE(retval_from_stbi_load);
996 #ifndef STBI_NO_LINEAR
997 static float *stbi__ldr_to_hdr(
stbi_uc *data,
int x,
int y,
int comp);
1001 static stbi_uc *stbi__hdr_to_ldr(
float *data,
int x,
int y,
int comp);
1004 static int stbi__vertically_flip_on_load = 0;
1008 stbi__vertically_flip_on_load = flag_true_if_should_flip;
1011 static void *stbi__load_main(stbi__context *s,
int *x,
int *y,
int *comp,
1012 int req_comp, stbi__result_info *ri,
int bpc)
1016 ri->bits_per_channel =
1018 ri->channel_order = STBI_ORDER_RGB;
1021 ri->num_channels = 0;
1023 #ifndef STBI_NO_JPEG
1024 if (stbi__jpeg_test(s))
1025 return stbi__jpeg_load(s, x, y, comp, req_comp, ri);
1028 if (stbi__png_test(s))
1029 return stbi__png_load(s, x, y, comp, req_comp, ri);
1032 if (stbi__bmp_test(s))
1033 return stbi__bmp_load(s, x, y, comp, req_comp, ri);
1036 if (stbi__gif_test(s))
1037 return stbi__gif_load(s, x, y, comp, req_comp, ri);
1040 if (stbi__psd_test(s))
1041 return stbi__psd_load(s, x, y, comp, req_comp, ri, bpc);
1044 if (stbi__pic_test(s))
1045 return stbi__pic_load(s, x, y, comp, req_comp, ri);
1048 if (stbi__pnm_test(s))
1049 return stbi__pnm_load(s, x, y, comp, req_comp, ri);
1053 if (stbi__hdr_test(s)) {
1054 float *hdr = stbi__hdr_load(s, x, y, comp, req_comp, ri);
1055 return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
1061 if (stbi__tga_test(s))
1062 return stbi__tga_load(s, x, y, comp, req_comp, ri);
1065 return stbi__errpuc(
"unknown image type",
1066 "Image not of any known type, or corrupt");
1069 static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig,
int w,
int h,
1073 int img_len = w * h * channels;
1076 reduced = (
stbi_uc *)stbi__malloc(img_len);
1077 if (reduced == NULL)
1078 return stbi__errpuc(
"outofmem",
"Out of memory");
1080 for (i = 0; i < img_len; ++i)
1081 reduced[i] = (
stbi_uc)((orig[i] >> 8) & 0xFF);
1089 static stbi__uint16 *stbi__convert_8_to_16(
stbi_uc *orig,
int w,
int h,
1093 int img_len = w * h * channels;
1094 stbi__uint16 *enlarged;
1096 enlarged = (stbi__uint16 *)stbi__malloc(img_len * 2);
1097 if (enlarged == NULL)
1098 return (stbi__uint16 *)stbi__errpuc(
"outofmem",
"Out of memory");
1100 for (i = 0; i < img_len; ++i)
1101 enlarged[i] = (stbi__uint16)(
1109 static void stbi__vertical_flip(
void *image,
int w,
int h,
int bytes_per_pixel)
1112 size_t bytes_per_row = (size_t)w * bytes_per_pixel;
1116 for (row = 0; row < (h >> 1); row++) {
1117 stbi_uc *row0 = bytes + row * bytes_per_row;
1118 stbi_uc *row1 = bytes + (h - row - 1) * bytes_per_row;
1120 size_t bytes_left = bytes_per_row;
1121 while (bytes_left) {
1123 (bytes_left <
sizeof(temp)) ? bytes_left :
sizeof(temp);
1124 memcpy(temp, row0, bytes_copy);
1125 memcpy(row0, row1, bytes_copy);
1126 memcpy(row1, temp, bytes_copy);
1129 bytes_left -= bytes_copy;
1134 static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s,
int *x,
1138 stbi__result_info ri;
1139 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);
1144 if (ri.bits_per_channel != 8) {
1145 STBI_ASSERT(ri.bits_per_channel == 16);
1146 result = stbi__convert_16_to_8((stbi__uint16 *)result, *x, *y,
1147 req_comp == 0 ? *comp : req_comp);
1148 ri.bits_per_channel = 8;
1153 if (stbi__vertically_flip_on_load) {
1154 int channels = req_comp ? req_comp : *comp;
1155 stbi__vertical_flip(result, *x, *y, channels *
sizeof(
stbi_uc));
1158 return (
unsigned char *)result;
1161 static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s,
int *x,
1165 stbi__result_info ri;
1166 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16);
1171 if (ri.bits_per_channel != 16) {
1172 STBI_ASSERT(ri.bits_per_channel == 8);
1173 result = stbi__convert_8_to_16((
stbi_uc *)result, *x, *y,
1174 req_comp == 0 ? *comp : req_comp);
1175 ri.bits_per_channel = 16;
1182 if (stbi__vertically_flip_on_load) {
1183 int channels = req_comp ? req_comp : *comp;
1184 stbi__vertical_flip(result, *x, *y, channels *
sizeof(stbi__uint16));
1187 return (stbi__uint16 *)result;
1191 static void stbi__float_postprocess(
float *result,
int *x,
int *y,
int *comp,
1194 if (stbi__vertically_flip_on_load && result != NULL) {
1195 int channels = req_comp ? req_comp : *comp;
1196 stbi__vertical_flip(result, *x, *y, channels *
sizeof(
float));
1201 #ifndef STBI_NO_STDIO
1203 static FILE *stbi__fopen(
char const *filename,
char const *mode)
1206 #if defined(_MSC_VER) && _MSC_VER >= 1400
1207 if (0 != fopen_s(&f, filename, mode))
1210 f = fopen(filename, mode);
1218 FILE *f = stbi__fopen(filename,
"rb");
1219 unsigned char *result;
1221 return stbi__errpuc(
"can't fopen",
"Unable to open file");
1230 unsigned char *result;
1232 stbi__start_file(&s, f);
1233 result = stbi__load_and_postprocess_8bit(&s, x, y, comp, req_comp);
1236 fseek(f, -(
int)(s.img_buffer_end - s.img_buffer), SEEK_CUR);
1244 stbi__uint16 *result;
1246 stbi__start_file(&s, f);
1247 result = stbi__load_and_postprocess_16bit(&s, x, y, comp, req_comp);
1250 fseek(f, -(
int)(s.img_buffer_end - s.img_buffer), SEEK_CUR);
1258 FILE *f = stbi__fopen(filename,
"rb");
1259 stbi__uint16 *result;
1261 return (
stbi_us *)stbi__errpuc(
"can't fopen",
"Unable to open file");
1270 int *x,
int *y,
int *channels_in_file,
1271 int desired_channels)
1274 stbi__start_mem(&s, buffer, len);
1275 return stbi__load_and_postprocess_16bit(&s, x, y, channels_in_file,
1280 void *user,
int *x,
int *y,
1281 int *channels_in_file,
1282 int desired_channels)
1286 return stbi__load_and_postprocess_16bit(&s, x, y, channels_in_file,
1291 int *y,
int *comp,
int req_comp)
1294 stbi__start_mem(&s, buffer, len);
1295 return stbi__load_and_postprocess_8bit(&s, x, y, comp, req_comp);
1299 void *user,
int *x,
int *y,
int *comp,
1304 return stbi__load_and_postprocess_8bit(&s, x, y, comp, req_comp);
1307 #ifndef STBI_NO_LINEAR
1308 static float *stbi__loadf_main(stbi__context *s,
int *x,
int *y,
int *comp,
1311 unsigned char *data;
1313 if (stbi__hdr_test(s)) {
1314 stbi__result_info ri;
1315 float *hdr_data = stbi__hdr_load(s, x, y, comp, req_comp, &ri);
1317 stbi__float_postprocess(hdr_data, x, y, comp, req_comp);
1321 data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
1323 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
1324 return stbi__errpf(
"unknown image type",
1325 "Image not of any known type, or corrupt");
1329 int *y,
int *comp,
int req_comp)
1332 stbi__start_mem(&s, buffer, len);
1333 return stbi__loadf_main(&s, x, y, comp, req_comp);
1337 void *user,
int *x,
int *y,
int *comp,
1342 return stbi__loadf_main(&s, x, y, comp, req_comp);
1345 #ifndef STBI_NO_STDIO
1350 FILE *f = stbi__fopen(filename,
"rb");
1352 return stbi__errpf(
"can't fopen",
"Unable to open file");
1362 stbi__start_file(&s, f);
1363 return stbi__loadf_main(&s, x, y, comp, req_comp);
1365 #endif // !STBI_NO_STDIO
1367 #endif // !STBI_NO_LINEAR
1377 stbi__start_mem(&s, buffer, len);
1378 return stbi__hdr_test(&s);
1380 STBI_NOTUSED(buffer);
1386 #ifndef STBI_NO_STDIO
1389 FILE *f = stbi__fopen(filename,
"rb");
1402 stbi__start_file(&s, f);
1403 return stbi__hdr_test(&s);
1409 #endif // !STBI_NO_STDIO
1417 return stbi__hdr_test(&s);
1425 #ifndef STBI_NO_LINEAR
1426 static float stbi__l2h_gamma = 2.2f, stbi__l2h_scale = 1.0f;
1430 stbi__l2h_gamma = gamma;
1434 stbi__l2h_scale = scale;
1438 static float stbi__h2l_gamma_i = 1.0f / 2.2f, stbi__h2l_scale_i = 1.0f;
1442 stbi__h2l_gamma_i = 1 / gamma;
1446 stbi__h2l_scale_i = 1 / scale;
1454 enum { STBI__SCAN_load = 0, STBI__SCAN_type, STBI__SCAN_header };
1456 static void stbi__refill_buffer(stbi__context *s)
1458 int n = (s->io.read)(s->io_user_data, (
char *)s->buffer_start, s->buflen);
1462 s->read_from_callbacks = 0;
1463 s->img_buffer = s->buffer_start;
1464 s->img_buffer_end = s->buffer_start + 1;
1467 s->img_buffer = s->buffer_start;
1468 s->img_buffer_end = s->buffer_start + n;
1472 stbi_inline
static stbi_uc stbi__get8(stbi__context *s)
1474 if (s->img_buffer < s->img_buffer_end)
1475 return *s->img_buffer++;
1476 if (s->read_from_callbacks) {
1477 stbi__refill_buffer(s);
1478 return *s->img_buffer++;
1483 stbi_inline
static int stbi__at_eof(stbi__context *s)
1486 if (!(s->io.eof)(s->io_user_data))
1490 if (s->read_from_callbacks == 0)
1494 return s->img_buffer >= s->img_buffer_end;
1497 static void stbi__skip(stbi__context *s,
int n)
1500 s->img_buffer = s->img_buffer_end;
1504 int blen = (int)(s->img_buffer_end - s->img_buffer);
1506 s->img_buffer = s->img_buffer_end;
1507 (s->io.skip)(s->io_user_data, n - blen);
1514 static int stbi__getn(stbi__context *s,
stbi_uc *buffer,
int n)
1517 int blen = (int)(s->img_buffer_end - s->img_buffer);
1521 memcpy(buffer, s->img_buffer, blen);
1524 (s->io.read)(s->io_user_data, (
char *)buffer + blen, n - blen);
1525 res = (count == (n - blen));
1526 s->img_buffer = s->img_buffer_end;
1531 if (s->img_buffer + n <= s->img_buffer_end) {
1532 memcpy(buffer, s->img_buffer, n);
1539 static int stbi__get16be(stbi__context *s)
1541 int z = stbi__get8(s);
1542 return (z << 8) + stbi__get8(s);
1545 static stbi__uint32 stbi__get32be(stbi__context *s)
1547 stbi__uint32 z = stbi__get16be(s);
1548 return (z << 16) + stbi__get16be(s);
1551 #if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)
1554 static int stbi__get16le(stbi__context *s)
1556 int z = stbi__get8(s);
1557 return z + (stbi__get8(s) << 8);
1562 static stbi__uint32 stbi__get32le(stbi__context *s)
1564 stbi__uint32 z = stbi__get16le(s);
1565 return z + (stbi__get16le(s) << 16);
1569 #define STBI__BYTECAST(x) \
1570 ((stbi_uc)((x)&255)) // truncate int to byte without warnings
1583 static stbi_uc stbi__compute_y(
int r,
int g,
int b)
1585 return (
stbi_uc)(((r * 77) + (g * 150) + (29 * b)) >> 8);
1588 static unsigned char *stbi__convert_format(
unsigned char *data,
int img_n,
1589 int req_comp,
unsigned int x,
1593 unsigned char *good;
1595 if (req_comp == img_n)
1597 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1599 good = (
unsigned char *)stbi__malloc_mad3(req_comp, x, y, 0);
1602 return stbi__errpuc(
"outofmem",
"Out of memory");
1605 for (j = 0; j < (int)y; ++j) {
1606 unsigned char *src = data + j * x * img_n;
1607 unsigned char *dest = good + j * x * req_comp;
1609 #define STBI__COMBO(a, b) ((a)*8 + (b))
1610 #define STBI__CASE(a, b) \
1611 case STBI__COMBO(a, b): \
1612 for (i = x - 1; i >= 0; --i, src += a, dest += b)
1616 switch (STBI__COMBO(img_n, req_comp)) {
1617 STBI__CASE(1, 2) { dest[0] = src[0], dest[1] = 255; }
1619 STBI__CASE(1, 3) { dest[0] = dest[1] = dest[2] = src[0]; }
1623 dest[0] = dest[1] = dest[2] = src[0], dest[3] = 255;
1626 STBI__CASE(2, 1) { dest[0] = src[0]; }
1628 STBI__CASE(2, 3) { dest[0] = dest[1] = dest[2] = src[0]; }
1632 dest[0] = dest[1] = dest[2] = src[0], dest[3] = src[1];
1637 dest[0] = src[0], dest[1] = src[1], dest[2] = src[2],
1643 dest[0] = stbi__compute_y(src[0], src[1], src[2]);
1648 dest[0] = stbi__compute_y(src[0], src[1], src[2]),
1654 dest[0] = stbi__compute_y(src[0], src[1], src[2]);
1659 dest[0] = stbi__compute_y(src[0], src[1], src[2]),
1665 dest[0] = src[0], dest[1] = src[1], dest[2] = src[2];
1678 static stbi__uint16 stbi__compute_y_16(
int r,
int g,
int b)
1680 return (stbi__uint16)(((r * 77) + (g * 150) + (29 * b)) >> 8);
1683 static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data,
int img_n,
1684 int req_comp,
unsigned int x,
1690 if (req_comp == img_n)
1692 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1694 good = (stbi__uint16 *)stbi__malloc(req_comp * x * y * 2);
1697 return (stbi__uint16 *)stbi__errpuc(
"outofmem",
"Out of memory");
1700 for (j = 0; j < (int)y; ++j) {
1701 stbi__uint16 *src = data + j * x * img_n;
1702 stbi__uint16 *dest = good + j * x * req_comp;
1704 #define STBI__COMBO(a, b) ((a)*8 + (b))
1705 #define STBI__CASE(a, b) \
1706 case STBI__COMBO(a, b): \
1707 for (i = x - 1; i >= 0; --i, src += a, dest += b)
1711 switch (STBI__COMBO(img_n, req_comp)) {
1712 STBI__CASE(1, 2) { dest[0] = src[0], dest[1] = 0xffff; }
1714 STBI__CASE(1, 3) { dest[0] = dest[1] = dest[2] = src[0]; }
1718 dest[0] = dest[1] = dest[2] = src[0], dest[3] = 0xffff;
1721 STBI__CASE(2, 1) { dest[0] = src[0]; }
1723 STBI__CASE(2, 3) { dest[0] = dest[1] = dest[2] = src[0]; }
1727 dest[0] = dest[1] = dest[2] = src[0], dest[3] = src[1];
1732 dest[0] = src[0], dest[1] = src[1], dest[2] = src[2],
1738 dest[0] = stbi__compute_y_16(src[0], src[1], src[2]);
1743 dest[0] = stbi__compute_y_16(src[0], src[1], src[2]),
1749 dest[0] = stbi__compute_y_16(src[0], src[1], src[2]);
1754 dest[0] = stbi__compute_y_16(src[0], src[1], src[2]),
1760 dest[0] = src[0], dest[1] = src[1], dest[2] = src[2];
1773 #ifndef STBI_NO_LINEAR
1774 static float *stbi__ldr_to_hdr(
stbi_uc *data,
int x,
int y,
int comp)
1780 output = (
float *)stbi__malloc_mad4(x, y, comp,
sizeof(
float), 0);
1781 if (output == NULL) {
1783 return stbi__errpf(
"outofmem",
"Out of memory");
1790 for (i = 0; i < x * y; ++i) {
1791 for (k = 0; k < n; ++k) {
1792 output[i * comp + k] =
1793 (float)(pow(data[i * comp + k] / 255.0f, stbi__l2h_gamma) *
1797 output[i * comp + k] = data[i * comp + k] / 255.0f;
1805 #define stbi__float2int(x) ((int)(x))
1806 static stbi_uc *stbi__hdr_to_ldr(
float *data,
int x,
int y,
int comp)
1812 output = (
stbi_uc *)stbi__malloc_mad3(x, y, comp, 0);
1813 if (output == NULL) {
1815 return stbi__errpuc(
"outofmem",
"Out of memory");
1822 for (i = 0; i < x * y; ++i) {
1823 for (k = 0; k < n; ++k) {
1824 float z = (float)pow(data[i * comp + k] * stbi__h2l_scale_i,
1825 stbi__h2l_gamma_i) *
1832 output[i * comp + k] = (
stbi_uc)stbi__float2int(z);
1835 float z = data[i * comp + k] * 255 + 0.5f;
1840 output[i * comp + k] = (
stbi_uc)stbi__float2int(z);
1870 #ifndef STBI_NO_JPEG
1873 #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
1878 stbi__uint16 code[256];
1881 unsigned int maxcode[18];
1887 stbi__huffman huff_dc[4];
1888 stbi__huffman huff_ac[4];
1889 stbi__uint16 dequant[4][64];
1890 stbi__int16 fast_ac[4][1 << FAST_BITS];
1893 int img_h_max, img_v_max;
1894 int img_mcu_x, img_mcu_y;
1895 int img_mcu_w, img_mcu_h;
1907 void *raw_data, *raw_coeff;
1910 int coeff_w, coeff_h;
1913 stbi__uint32 code_buffer;
1915 unsigned char marker;
1925 int app14_color_transform;
1928 int scan_n, order[4];
1929 int restart_interval, todo;
1932 void (*idct_block_kernel)(
stbi_uc *out,
int out_stride,
short data[64]);
1935 int count,
int step);
1937 stbi_uc *in_far,
int w,
int hs);
1940 static int stbi__build_huffman(stbi__huffman *h,
int *count)
1942 int i, j, k = 0, code;
1944 for (i = 0; i < 16; ++i)
1945 for (j = 0; j < count[i]; ++j)
1946 h->size[k++] = (
stbi_uc)(i + 1);
1952 for (j = 1; j <= 16; ++j) {
1954 h->delta[j] = k - code;
1955 if (h->size[k] == j) {
1956 while (h->size[k] == j)
1957 h->code[k++] = (stbi__uint16)(code++);
1958 if (code - 1 >= (1 << j))
1959 return stbi__err(
"bad code lengths",
"Corrupt JPEG");
1962 h->maxcode[j] = code << (16 - j);
1965 h->maxcode[j] = 0xffffffff;
1968 memset(h->fast, 255, 1 << FAST_BITS);
1969 for (i = 0; i < k; ++i) {
1971 if (s <= FAST_BITS) {
1972 int c = h->code[i] << (FAST_BITS - s);
1973 int m = 1 << (FAST_BITS - s);
1974 for (j = 0; j < m; ++j) {
1984 static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h)
1987 for (i = 0; i < (1 << FAST_BITS); ++i) {
1991 int rs = h->values[fast];
1992 int run = (rs >> 4) & 15;
1993 int magbits = rs & 15;
1994 int len = h->size[fast];
1996 if (magbits && len + magbits <= FAST_BITS) {
1998 int k = ((i << len) & ((1 << FAST_BITS) - 1)) >>
1999 (FAST_BITS - magbits);
2000 int m = 1 << (magbits - 1);
2002 k += (~0
U << magbits) + 1;
2004 if (k >= -128 && k <= 127)
2006 (stbi__int16)((k << 8) + (run << 4) + (len + magbits));
2012 static void stbi__grow_buffer_unsafe(stbi__jpeg *j)
2015 int b = j->nomore ? 0 : stbi__get8(j->s);
2017 int c = stbi__get8(j->s);
2019 c = stbi__get8(j->s);
2021 j->marker = (
unsigned char)c;
2026 j->code_buffer |= b << (24 - j->code_bits);
2028 }
while (j->code_bits <= 24);
2032 static stbi__uint32 stbi__bmask[17] = {0, 1, 3, 7, 15, 31,
2033 63, 127, 255, 511, 1023, 2047,
2034 4095, 8191, 16383, 32767, 65535};
2037 stbi_inline
static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
2042 if (j->code_bits < 16)
2043 stbi__grow_buffer_unsafe(j);
2047 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS) - 1);
2051 if (s > j->code_bits)
2053 j->code_buffer <<= s;
2055 return h->values[k];
2064 temp = j->code_buffer >> 16;
2065 for (k = FAST_BITS + 1;; ++k)
2066 if (temp < h->maxcode[k])
2074 if (k > j->code_bits)
2078 c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
2079 STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) &
2080 stbi__bmask[h->size[c]]) == h->code[c]);
2084 j->code_buffer <<= k;
2085 return h->values[c];
2089 static int const stbi__jbias[16] = {0, -1, -3, -7, -15, -31,
2090 -63, -127, -255, -511, -1023, -2047,
2091 -4095, -8191, -16383, -32767};
2095 stbi_inline
static int stbi__extend_receive(stbi__jpeg *j,
int n)
2099 if (j->code_bits < n)
2100 stbi__grow_buffer_unsafe(j);
2102 sgn = (stbi__int32)j->code_buffer >> 31;
2103 k = stbi_lrot(j->code_buffer, n);
2104 STBI_ASSERT(n >= 0 &&
2105 n < (
int)(
sizeof(stbi__bmask) /
sizeof(*stbi__bmask)));
2106 j->code_buffer = k & ~stbi__bmask[n];
2107 k &= stbi__bmask[n];
2109 return k + (stbi__jbias[n] & ~sgn);
2113 stbi_inline
static int stbi__jpeg_get_bits(stbi__jpeg *j,
int n)
2116 if (j->code_bits < n)
2117 stbi__grow_buffer_unsafe(j);
2118 k = stbi_lrot(j->code_buffer, n);
2119 j->code_buffer = k & ~stbi__bmask[n];
2120 k &= stbi__bmask[n];
2125 stbi_inline
static int stbi__jpeg_get_bit(stbi__jpeg *j)
2128 if (j->code_bits < 1)
2129 stbi__grow_buffer_unsafe(j);
2131 j->code_buffer <<= 1;
2133 return k & 0x80000000;
2138 static stbi_uc stbi__jpeg_dezigzag[64 + 15] = {
2139 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40,
2140 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36,
2141 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61,
2144 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63};
2147 static int stbi__jpeg_decode_block(stbi__jpeg *j,
short data[64],
2148 stbi__huffman *hdc, stbi__huffman *hac,
2149 stbi__int16 *fac,
int b,
2150 stbi__uint16 *dequant)
2155 if (j->code_bits < 16)
2156 stbi__grow_buffer_unsafe(j);
2157 t = stbi__jpeg_huff_decode(j, hdc);
2159 return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2162 memset(data, 0, 64 *
sizeof(data[0]));
2164 diff = t ? stbi__extend_receive(j, t) : 0;
2165 dc = j->img_comp[b].dc_pred + diff;
2166 j->img_comp[b].dc_pred = dc;
2167 data[0] = (short)(dc * dequant[0]);
2174 if (j->code_bits < 16)
2175 stbi__grow_buffer_unsafe(j);
2176 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS) - 1);
2181 j->code_buffer <<= s;
2184 zig = stbi__jpeg_dezigzag[k++];
2185 data[zig] = (short)((r >> 8) * dequant[zig]);
2187 int rs = stbi__jpeg_huff_decode(j, hac);
2189 return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2199 zig = stbi__jpeg_dezigzag[k++];
2200 data[zig] = (short)(stbi__extend_receive(j, s) * dequant[zig]);
2207 static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j,
short data[64],
2208 stbi__huffman *hdc,
int b)
2212 if (j->spec_end != 0)
2213 return stbi__err(
"can't merge dc and ac",
"Corrupt JPEG");
2215 if (j->code_bits < 16)
2216 stbi__grow_buffer_unsafe(j);
2218 if (j->succ_high == 0) {
2220 memset(data, 0, 64 *
sizeof(data[0]));
2221 t = stbi__jpeg_huff_decode(j, hdc);
2222 diff = t ? stbi__extend_receive(j, t) : 0;
2224 dc = j->img_comp[b].dc_pred + diff;
2225 j->img_comp[b].dc_pred = dc;
2226 data[0] = (short)(dc << j->succ_low);
2229 if (stbi__jpeg_get_bit(j))
2230 data[0] += (
short)(1 << j->succ_low);
2237 static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j,
short data[64],
2238 stbi__huffman *hac, stbi__int16 *fac)
2241 if (j->spec_start == 0)
2242 return stbi__err(
"can't merge dc and ac",
"Corrupt JPEG");
2244 if (j->succ_high == 0) {
2245 int shift = j->succ_low;
2256 if (j->code_bits < 16)
2257 stbi__grow_buffer_unsafe(j);
2258 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS) - 1);
2263 j->code_buffer <<= s;
2265 zig = stbi__jpeg_dezigzag[k++];
2266 data[zig] = (short)((r >> 8) << shift);
2268 int rs = stbi__jpeg_huff_decode(j, hac);
2270 return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2275 j->eob_run = (1 << r);
2277 j->eob_run += stbi__jpeg_get_bits(j, r);
2284 zig = stbi__jpeg_dezigzag[k++];
2285 data[zig] = (short)(stbi__extend_receive(j, s) << shift);
2288 }
while (k <= j->spec_end);
2292 short bit = (short)(1 << j->succ_low);
2296 for (k = j->spec_start; k <= j->spec_end; ++k) {
2297 short *p = &data[stbi__jpeg_dezigzag[k]];
2299 if (stbi__jpeg_get_bit(j))
2300 if ((*p & bit) == 0) {
2311 int rs = stbi__jpeg_huff_decode(j, hac);
2317 return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2322 j->eob_run = (1 << r) - 1;
2324 j->eob_run += stbi__jpeg_get_bits(j, r);
2333 return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2335 if (stbi__jpeg_get_bit(j))
2342 while (k <= j->spec_end) {
2343 short *p = &data[stbi__jpeg_dezigzag[k++]];
2345 if (stbi__jpeg_get_bit(j))
2346 if ((*p & bit) == 0) {
2360 }
while (k <= j->spec_end);
2367 stbi_inline
static stbi_uc stbi__clamp(
int x)
2370 if ((
unsigned int)x > 255) {
2379 #define stbi__f2f(x) ((int)(((x)*4096 + 0.5)))
2380 #define stbi__fsh(x) ((x) << 12)
2383 #define STBI__IDCT_1D(s0, s1, s2, s3, s4, s5, s6, s7) \
2384 int t0, t1, t2, t3, p1, p2, p3, p4, p5, x0, x1, x2, x3; \
2387 p1 = (p2 + p3) * stbi__f2f(0.5411961f); \
2388 t2 = p1 + p3 * stbi__f2f(-1.847759065f); \
2389 t3 = p1 + p2 * stbi__f2f(0.765366865f); \
2392 t0 = stbi__fsh(p2 + p3); \
2393 t1 = stbi__fsh(p2 - p3); \
2406 p5 = (p3 + p4) * stbi__f2f(1.175875602f); \
2407 t0 = t0 * stbi__f2f(0.298631336f); \
2408 t1 = t1 * stbi__f2f(2.053119869f); \
2409 t2 = t2 * stbi__f2f(3.072711026f); \
2410 t3 = t3 * stbi__f2f(1.501321110f); \
2411 p1 = p5 + p1 * stbi__f2f(-0.899976223f); \
2412 p2 = p5 + p2 * stbi__f2f(-2.562915447f); \
2413 p3 = p3 * stbi__f2f(-1.961570560f); \
2414 p4 = p4 * stbi__f2f(-0.390180644f); \
2420 static void stbi__idct_block(
stbi_uc *out,
int out_stride,
short data[64])
2422 int i, val[64], *v = val;
2427 for (i = 0; i < 8; ++i, ++d, ++v) {
2429 if (d[8] == 0 && d[16] == 0 && d[24] == 0 && d[32] == 0 && d[40] == 0 &&
2430 d[48] == 0 && d[56] == 0) {
2435 int dcterm = d[0] << 2;
2436 v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] =
2439 STBI__IDCT_1D(d[0], d[8], d[16], d[24], d[32], d[40], d[48], d[56])
2446 v[0] = (x0 + t3) >> 10;
2447 v[56] = (x0 - t3) >> 10;
2448 v[8] = (x1 + t2) >> 10;
2449 v[48] = (x1 - t2) >> 10;
2450 v[16] = (x2 + t1) >> 10;
2451 v[40] = (x2 - t1) >> 10;
2452 v[24] = (x3 + t0) >> 10;
2453 v[32] = (x3 - t0) >> 10;
2457 for (i = 0, v = val, o = out; i < 8; ++i, v += 8, o += out_stride) {
2459 STBI__IDCT_1D(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
2466 x0 += 65536 + (128 << 17);
2467 x1 += 65536 + (128 << 17);
2468 x2 += 65536 + (128 << 17);
2469 x3 += 65536 + (128 << 17);
2472 o[0] = stbi__clamp((x0 + t3) >> 17);
2473 o[7] = stbi__clamp((x0 - t3) >> 17);
2474 o[1] = stbi__clamp((x1 + t2) >> 17);
2475 o[6] = stbi__clamp((x1 - t2) >> 17);
2476 o[2] = stbi__clamp((x2 + t1) >> 17);
2477 o[5] = stbi__clamp((x2 - t1) >> 17);
2478 o[3] = stbi__clamp((x3 + t0) >> 17);
2479 o[4] = stbi__clamp((x3 - t0) >> 17);
2487 static void stbi__idct_simd(
stbi_uc *out,
int out_stride,
short data[64])
2490 __m128i row0, row1, row2, row3, row4, row5, row6, row7;
2494 #define dct_const(x, y) _mm_setr_epi16((x), (y), (x), (y), (x), (y), (x), (y))
2498 #define dct_rot(out0, out1, x, y, c0, c1) \
2499 __m128i c0##lo = _mm_unpacklo_epi16((x), (y)); \
2500 __m128i c0##hi = _mm_unpackhi_epi16((x), (y)); \
2501 __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \
2502 __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \
2503 __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \
2504 __m128i out1##_h = _mm_madd_epi16(c0##hi, c1)
2507 #define dct_widen(out, in) \
2509 _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \
2511 _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4)
2514 #define dct_wadd(out, a, b) \
2515 __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \
2516 __m128i out##_h = _mm_add_epi32(a##_h, b##_h)
2519 #define dct_wsub(out, a, b) \
2520 __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \
2521 __m128i out##_h = _mm_sub_epi32(a##_h, b##_h)
2524 #define dct_bfly32o(out0, out1, a, b, bias, s) \
2526 __m128i abiased_l = _mm_add_epi32(a##_l, bias); \
2527 __m128i abiased_h = _mm_add_epi32(a##_h, bias); \
2528 dct_wadd(sum, abiased, b); \
2529 dct_wsub(dif, abiased, b); \
2530 out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), \
2531 _mm_srai_epi32(sum_h, s)); \
2532 out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), \
2533 _mm_srai_epi32(dif_h, s)); \
2537 #define dct_interleave8(a, b) \
2539 a = _mm_unpacklo_epi8(a, b); \
2540 b = _mm_unpackhi_epi8(tmp, b)
2543 #define dct_interleave16(a, b) \
2545 a = _mm_unpacklo_epi16(a, b); \
2546 b = _mm_unpackhi_epi16(tmp, b)
2548 #define dct_pass(bias, shift) \
2551 dct_rot(t2e, t3e, row2, row6, rot0_0, rot0_1); \
2552 __m128i sum04 = _mm_add_epi16(row0, row4); \
2553 __m128i dif04 = _mm_sub_epi16(row0, row4); \
2554 dct_widen(t0e, sum04); \
2555 dct_widen(t1e, dif04); \
2556 dct_wadd(x0, t0e, t3e); \
2557 dct_wsub(x3, t0e, t3e); \
2558 dct_wadd(x1, t1e, t2e); \
2559 dct_wsub(x2, t1e, t2e); \
2561 dct_rot(y0o, y2o, row7, row3, rot2_0, rot2_1); \
2562 dct_rot(y1o, y3o, row5, row1, rot3_0, rot3_1); \
2563 __m128i sum17 = _mm_add_epi16(row1, row7); \
2564 __m128i sum35 = _mm_add_epi16(row3, row5); \
2565 dct_rot(y4o, y5o, sum17, sum35, rot1_0, rot1_1); \
2566 dct_wadd(x4, y0o, y4o); \
2567 dct_wadd(x5, y1o, y5o); \
2568 dct_wadd(x6, y2o, y5o); \
2569 dct_wadd(x7, y3o, y4o); \
2570 dct_bfly32o(row0, row7, x0, x7, bias, shift); \
2571 dct_bfly32o(row1, row6, x1, x6, bias, shift); \
2572 dct_bfly32o(row2, row5, x2, x5, bias, shift); \
2573 dct_bfly32o(row3, row4, x3, x4, bias, shift); \
2577 dct_const(stbi__f2f(0.5411961f),
2578 stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f));
2579 __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f(0.765366865f),
2580 stbi__f2f(0.5411961f));
2582 dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f),
2583 stbi__f2f(1.175875602f));
2585 dct_const(stbi__f2f(1.175875602f),
2586 stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f));
2588 dct_const(stbi__f2f(-1.961570560f) + stbi__f2f(0.298631336f),
2589 stbi__f2f(-1.961570560f));
2591 dct_const(stbi__f2f(-1.961570560f),
2592 stbi__f2f(-1.961570560f) + stbi__f2f(3.072711026f));
2594 dct_const(stbi__f2f(-0.390180644f) + stbi__f2f(2.053119869f),
2595 stbi__f2f(-0.390180644f));
2597 dct_const(stbi__f2f(-0.390180644f),
2598 stbi__f2f(-0.390180644f) + stbi__f2f(1.501321110f));
2602 __m128i bias_0 = _mm_set1_epi32(512);
2603 __m128i bias_1 = _mm_set1_epi32(65536 + (128 << 17));
2606 row0 = _mm_load_si128((
const __m128i *)(data + 0 * 8));
2607 row1 = _mm_load_si128((
const __m128i *)(data + 1 * 8));
2608 row2 = _mm_load_si128((
const __m128i *)(data + 2 * 8));
2609 row3 = _mm_load_si128((
const __m128i *)(data + 3 * 8));
2610 row4 = _mm_load_si128((
const __m128i *)(data + 4 * 8));
2611 row5 = _mm_load_si128((
const __m128i *)(data + 5 * 8));
2612 row6 = _mm_load_si128((
const __m128i *)(data + 6 * 8));
2613 row7 = _mm_load_si128((
const __m128i *)(data + 7 * 8));
2616 dct_pass(bias_0, 10);
2620 dct_interleave16(row0, row4);
2621 dct_interleave16(row1, row5);
2622 dct_interleave16(row2, row6);
2623 dct_interleave16(row3, row7);
2626 dct_interleave16(row0, row2);
2627 dct_interleave16(row1, row3);
2628 dct_interleave16(row4, row6);
2629 dct_interleave16(row5, row7);
2632 dct_interleave16(row0, row1);
2633 dct_interleave16(row2, row3);
2634 dct_interleave16(row4, row5);
2635 dct_interleave16(row6, row7);
2639 dct_pass(bias_1, 17);
2643 __m128i p0 = _mm_packus_epi16(row0, row1);
2644 __m128i p1 = _mm_packus_epi16(row2, row3);
2645 __m128i p2 = _mm_packus_epi16(row4, row5);
2646 __m128i p3 = _mm_packus_epi16(row6, row7);
2649 dct_interleave8(p0, p2);
2650 dct_interleave8(p1, p3);
2653 dct_interleave8(p0, p1);
2654 dct_interleave8(p2, p3);
2657 dct_interleave8(p0, p2);
2658 dct_interleave8(p1, p3);
2661 _mm_storel_epi64((__m128i *)out, p0);
2663 _mm_storel_epi64((__m128i *)out, _mm_shuffle_epi32(p0, 0x4e));
2665 _mm_storel_epi64((__m128i *)out, p2);
2667 _mm_storel_epi64((__m128i *)out, _mm_shuffle_epi32(p2, 0x4e));
2669 _mm_storel_epi64((__m128i *)out, p1);
2671 _mm_storel_epi64((__m128i *)out, _mm_shuffle_epi32(p1, 0x4e));
2673 _mm_storel_epi64((__m128i *)out, p3);
2675 _mm_storel_epi64((__m128i *)out, _mm_shuffle_epi32(p3, 0x4e));
2684 #undef dct_interleave8
2685 #undef dct_interleave16
2695 static void stbi__idct_simd(
stbi_uc *out,
int out_stride,
short data[64])
2697 int16x8_t row0, row1, row2, row3, row4, row5, row6, row7;
2699 int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f));
2700 int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f));
2701 int16x4_t rot0_2 = vdup_n_s16(stbi__f2f(0.765366865f));
2702 int16x4_t rot1_0 = vdup_n_s16(stbi__f2f(1.175875602f));
2703 int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f));
2704 int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f));
2705 int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f));
2706 int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f));
2707 int16x4_t rot3_0 = vdup_n_s16(stbi__f2f(0.298631336f));
2708 int16x4_t rot3_1 = vdup_n_s16(stbi__f2f(2.053119869f));
2709 int16x4_t rot3_2 = vdup_n_s16(stbi__f2f(3.072711026f));
2710 int16x4_t rot3_3 = vdup_n_s16(stbi__f2f(1.501321110f));
2712 #define dct_long_mul(out, inq, coeff) \
2713 int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \
2714 int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff)
2716 #define dct_long_mac(out, acc, inq, coeff) \
2717 int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \
2718 int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff)
2720 #define dct_widen(out, inq) \
2721 int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \
2722 int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12)
2725 #define dct_wadd(out, a, b) \
2726 int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \
2727 int32x4_t out##_h = vaddq_s32(a##_h, b##_h)
2730 #define dct_wsub(out, a, b) \
2731 int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \
2732 int32x4_t out##_h = vsubq_s32(a##_h, b##_h)
2735 #define dct_bfly32o(out0, out1, a, b, shiftop, s) \
2737 dct_wadd(sum, a, b); \
2738 dct_wsub(dif, a, b); \
2739 out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \
2740 out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \
2743 #define dct_pass(shiftop, shift) \
2746 int16x8_t sum26 = vaddq_s16(row2, row6); \
2747 dct_long_mul(p1e, sum26, rot0_0); \
2748 dct_long_mac(t2e, p1e, row6, rot0_1); \
2749 dct_long_mac(t3e, p1e, row2, rot0_2); \
2750 int16x8_t sum04 = vaddq_s16(row0, row4); \
2751 int16x8_t dif04 = vsubq_s16(row0, row4); \
2752 dct_widen(t0e, sum04); \
2753 dct_widen(t1e, dif04); \
2754 dct_wadd(x0, t0e, t3e); \
2755 dct_wsub(x3, t0e, t3e); \
2756 dct_wadd(x1, t1e, t2e); \
2757 dct_wsub(x2, t1e, t2e); \
2759 int16x8_t sum15 = vaddq_s16(row1, row5); \
2760 int16x8_t sum17 = vaddq_s16(row1, row7); \
2761 int16x8_t sum35 = vaddq_s16(row3, row5); \
2762 int16x8_t sum37 = vaddq_s16(row3, row7); \
2763 int16x8_t sumodd = vaddq_s16(sum17, sum35); \
2764 dct_long_mul(p5o, sumodd, rot1_0); \
2765 dct_long_mac(p1o, p5o, sum17, rot1_1); \
2766 dct_long_mac(p2o, p5o, sum35, rot1_2); \
2767 dct_long_mul(p3o, sum37, rot2_0); \
2768 dct_long_mul(p4o, sum15, rot2_1); \
2769 dct_wadd(sump13o, p1o, p3o); \
2770 dct_wadd(sump24o, p2o, p4o); \
2771 dct_wadd(sump23o, p2o, p3o); \
2772 dct_wadd(sump14o, p1o, p4o); \
2773 dct_long_mac(x4, sump13o, row7, rot3_0); \
2774 dct_long_mac(x5, sump24o, row5, rot3_1); \
2775 dct_long_mac(x6, sump23o, row3, rot3_2); \
2776 dct_long_mac(x7, sump14o, row1, rot3_3); \
2777 dct_bfly32o(row0, row7, x0, x7, shiftop, shift); \
2778 dct_bfly32o(row1, row6, x1, x6, shiftop, shift); \
2779 dct_bfly32o(row2, row5, x2, x5, shiftop, shift); \
2780 dct_bfly32o(row3, row4, x3, x4, shiftop, shift); \
2784 row0 = vld1q_s16(data + 0 * 8);
2785 row1 = vld1q_s16(data + 1 * 8);
2786 row2 = vld1q_s16(data + 2 * 8);
2787 row3 = vld1q_s16(data + 3 * 8);
2788 row4 = vld1q_s16(data + 4 * 8);
2789 row5 = vld1q_s16(data + 5 * 8);
2790 row6 = vld1q_s16(data + 6 * 8);
2791 row7 = vld1q_s16(data + 7 * 8);
2794 row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0));
2797 dct_pass(vrshrn_n_s32, 10);
2803 #define dct_trn16(x, y) \
2805 int16x8x2_t t = vtrnq_s16(x, y); \
2809 #define dct_trn32(x, y) \
2812 vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); \
2813 x = vreinterpretq_s16_s32(t.val[0]); \
2814 y = vreinterpretq_s16_s32(t.val[1]); \
2816 #define dct_trn64(x, y) \
2820 x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); \
2821 y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); \
2825 dct_trn16(row0, row1);
2826 dct_trn16(row2, row3);
2827 dct_trn16(row4, row5);
2828 dct_trn16(row6, row7);
2831 dct_trn32(row0, row2);
2832 dct_trn32(row1, row3);
2833 dct_trn32(row4, row6);
2834 dct_trn32(row5, row7);
2837 dct_trn64(row0, row4);
2838 dct_trn64(row1, row5);
2839 dct_trn64(row2, row6);
2840 dct_trn64(row3, row7);
2851 dct_pass(vshrn_n_s32, 16);
2855 uint8x8_t p0 = vqrshrun_n_s16(row0, 1);
2856 uint8x8_t p1 = vqrshrun_n_s16(row1, 1);
2857 uint8x8_t p2 = vqrshrun_n_s16(row2, 1);
2858 uint8x8_t p3 = vqrshrun_n_s16(row3, 1);
2859 uint8x8_t p4 = vqrshrun_n_s16(row4, 1);
2860 uint8x8_t p5 = vqrshrun_n_s16(row5, 1);
2861 uint8x8_t p6 = vqrshrun_n_s16(row6, 1);
2862 uint8x8_t p7 = vqrshrun_n_s16(row7, 1);
2865 #define dct_trn8_8(x, y) \
2867 uint8x8x2_t t = vtrn_u8(x, y); \
2871 #define dct_trn8_16(x, y) \
2874 vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); \
2875 x = vreinterpret_u8_u16(t.val[0]); \
2876 y = vreinterpret_u8_u16(t.val[1]); \
2878 #define dct_trn8_32(x, y) \
2881 vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); \
2882 x = vreinterpret_u8_u32(t.val[0]); \
2883 y = vreinterpret_u8_u32(t.val[1]); \
2896 dct_trn8_16(p0, p2);
2897 dct_trn8_16(p1, p3);
2898 dct_trn8_16(p4, p6);
2899 dct_trn8_16(p5, p7);
2902 dct_trn8_32(p0, p4);
2903 dct_trn8_32(p1, p5);
2904 dct_trn8_32(p2, p6);
2905 dct_trn8_32(p3, p7);
2940 #define STBI__MARKER_none 0xff
2944 static stbi_uc stbi__get_marker(stbi__jpeg *j)
2947 if (j->marker != STBI__MARKER_none) {
2949 j->marker = STBI__MARKER_none;
2952 x = stbi__get8(j->s);
2954 return STBI__MARKER_none;
2956 x = stbi__get8(j->s);
2962 #define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
2966 static void stbi__jpeg_reset(stbi__jpeg *j)
2971 j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred =
2972 j->img_comp[3].dc_pred = 0;
2973 j->marker = STBI__MARKER_none;
2974 j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
2980 static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
2982 stbi__jpeg_reset(z);
2983 if (!z->progressive) {
2984 if (z->scan_n == 1) {
2986 STBI_SIMD_ALIGN(
short, data[64]);
2987 int n = z->order[0];
2992 int w = (z->img_comp[n].x + 7) >> 3;
2993 int h = (z->img_comp[n].y + 7) >> 3;
2994 for (j = 0; j < h; ++j) {
2995 for (i = 0; i < w; ++i) {
2996 int ha = z->img_comp[n].ha;
2997 if (!stbi__jpeg_decode_block(
2998 z, data, z->huff_dc + z->img_comp[n].hd,
2999 z->huff_ac + ha, z->fast_ac[ha], n,
3000 z->dequant[z->img_comp[n].tq]))
3002 z->idct_block_kernel(z->img_comp[n].data +
3003 z->img_comp[n].w2 * j * 8 + i * 8,
3004 z->img_comp[n].w2, data);
3007 if (--z->todo <= 0) {
3008 if (z->code_bits < 24)
3009 stbi__grow_buffer_unsafe(z);
3012 if (!STBI__RESTART(z->marker))
3014 stbi__jpeg_reset(z);
3021 STBI_SIMD_ALIGN(
short, data[64]);
3022 for (j = 0; j < z->img_mcu_y; ++j) {
3023 for (i = 0; i < z->img_mcu_x; ++i) {
3026 for (k = 0; k < z->scan_n; ++k) {
3027 int n = z->order[k];
3031 for (y = 0; y < z->img_comp[n].v; ++y) {
3032 for (x = 0; x < z->img_comp[n].h; ++x) {
3033 int x2 = (i * z->img_comp[n].h + x) * 8;
3034 int y2 = (j * z->img_comp[n].v + y) * 8;
3035 int ha = z->img_comp[n].ha;
3036 if (!stbi__jpeg_decode_block(
3037 z, data, z->huff_dc + z->img_comp[n].hd,
3038 z->huff_ac + ha, z->fast_ac[ha], n,
3039 z->dequant[z->img_comp[n].tq]))
3041 z->idct_block_kernel(
3042 z->img_comp[n].data +
3043 z->img_comp[n].w2 * y2 + x2,
3044 z->img_comp[n].w2, data);
3050 if (--z->todo <= 0) {
3051 if (z->code_bits < 24)
3052 stbi__grow_buffer_unsafe(z);
3053 if (!STBI__RESTART(z->marker))
3055 stbi__jpeg_reset(z);
3062 if (z->scan_n == 1) {
3064 int n = z->order[0];
3069 int w = (z->img_comp[n].x + 7) >> 3;
3070 int h = (z->img_comp[n].y + 7) >> 3;
3071 for (j = 0; j < h; ++j) {
3072 for (i = 0; i < w; ++i) {
3073 short *data = z->img_comp[n].coeff +
3074 64 * (i + j * z->img_comp[n].coeff_w);
3075 if (z->spec_start == 0) {
3076 if (!stbi__jpeg_decode_block_prog_dc(
3077 z, data, &z->huff_dc[z->img_comp[n].hd], n))
3080 int ha = z->img_comp[n].ha;
3081 if (!stbi__jpeg_decode_block_prog_ac(
3082 z, data, &z->huff_ac[ha], z->fast_ac[ha]))
3087 if (--z->todo <= 0) {
3088 if (z->code_bits < 24)
3089 stbi__grow_buffer_unsafe(z);
3090 if (!STBI__RESTART(z->marker))
3092 stbi__jpeg_reset(z);
3099 for (j = 0; j < z->img_mcu_y; ++j) {
3100 for (i = 0; i < z->img_mcu_x; ++i) {
3103 for (k = 0; k < z->scan_n; ++k) {
3104 int n = z->order[k];
3108 for (y = 0; y < z->img_comp[n].v; ++y) {
3109 for (x = 0; x < z->img_comp[n].h; ++x) {
3110 int x2 = (i * z->img_comp[n].h + x);
3111 int y2 = (j * z->img_comp[n].v + y);
3113 z->img_comp[n].coeff +
3114 64 * (x2 + y2 * z->img_comp[n].coeff_w);
3115 if (!stbi__jpeg_decode_block_prog_dc(
3116 z, data, &z->huff_dc[z->img_comp[n].hd],
3124 if (--z->todo <= 0) {
3125 if (z->code_bits < 24)
3126 stbi__grow_buffer_unsafe(z);
3127 if (!STBI__RESTART(z->marker))
3129 stbi__jpeg_reset(z);
3138 static void stbi__jpeg_dequantize(
short *data, stbi__uint16 *dequant)
3141 for (i = 0; i < 64; ++i)
3142 data[i] *= dequant[i];
3145 static void stbi__jpeg_finish(stbi__jpeg *z)
3147 if (z->progressive) {
3150 for (n = 0; n < z->s->img_n; ++n) {
3151 int w = (z->img_comp[n].x + 7) >> 3;
3152 int h = (z->img_comp[n].y + 7) >> 3;
3153 for (j = 0; j < h; ++j) {
3154 for (i = 0; i < w; ++i) {
3155 short *data = z->img_comp[n].coeff +
3156 64 * (i + j * z->img_comp[n].coeff_w);
3157 stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]);
3158 z->idct_block_kernel(z->img_comp[n].data +
3159 z->img_comp[n].w2 * j * 8 + i * 8,
3160 z->img_comp[n].w2, data);
3167 static int stbi__process_marker(stbi__jpeg *z,
int m)
3171 case STBI__MARKER_none:
3172 return stbi__err(
"expected marker",
"Corrupt JPEG");
3175 if (stbi__get16be(z->s) != 4)
3176 return stbi__err(
"bad DRI len",
"Corrupt JPEG");
3177 z->restart_interval = stbi__get16be(z->s);
3181 L = stbi__get16be(z->s) - 2;
3183 int q = stbi__get8(z->s);
3184 int p = q >> 4, sixteen = (p != 0);
3186 if (p != 0 && p != 1)
3187 return stbi__err(
"bad DQT type",
"Corrupt JPEG");
3189 return stbi__err(
"bad DQT table",
"Corrupt JPEG");
3191 for (i = 0; i < 64; ++i)
3192 z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(
3193 sixteen ? stbi__get16be(z->s) : stbi__get8(z->s));
3194 L -= (sixteen ? 129 : 65);
3199 L = stbi__get16be(z->s) - 2;
3202 int sizes[16], i, n = 0;
3203 int q = stbi__get8(z->s);
3206 if (tc > 1 || th > 3)
3207 return stbi__err(
"bad DHT header",
"Corrupt JPEG");
3208 for (i = 0; i < 16; ++i) {
3209 sizes[i] = stbi__get8(z->s);
3214 if (!stbi__build_huffman(z->huff_dc + th, sizes))
3216 v = z->huff_dc[th].values;
3218 if (!stbi__build_huffman(z->huff_ac + th, sizes))
3220 v = z->huff_ac[th].values;
3222 for (i = 0; i < n; ++i)
3223 v[i] = stbi__get8(z->s);
3225 stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th);
3232 if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
3233 L = stbi__get16be(z->s);
3236 return stbi__err(
"bad COM len",
"Corrupt JPEG");
3238 return stbi__err(
"bad APP len",
"Corrupt JPEG");
3242 if (m == 0xE0 && L >= 5) {
3243 static const unsigned char tag[5] = {
'J',
'F',
'I',
'F',
'\0'};
3246 for (i = 0; i < 5; ++i)
3247 if (stbi__get8(z->s) != tag[i])
3252 }
else if (m == 0xEE && L >= 12) {
3253 static const unsigned char tag[6] = {
'A',
'd',
'o',
'b',
'e',
'\0'};
3256 for (i = 0; i < 6; ++i)
3257 if (stbi__get8(z->s) != tag[i])
3262 stbi__get16be(z->s);
3263 stbi__get16be(z->s);
3264 z->app14_color_transform = stbi__get8(z->s);
3269 stbi__skip(z->s, L);
3273 return stbi__err(
"unknown marker",
"Corrupt JPEG");
3277 static int stbi__process_scan_header(stbi__jpeg *z)
3280 int Ls = stbi__get16be(z->s);
3281 z->scan_n = stbi__get8(z->s);
3282 if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (
int)z->s->img_n)
3283 return stbi__err(
"bad SOS component count",
"Corrupt JPEG");
3284 if (Ls != 6 + 2 * z->scan_n)
3285 return stbi__err(
"bad SOS len",
"Corrupt JPEG");
3286 for (i = 0; i < z->scan_n; ++i) {
3287 int id = stbi__get8(z->s), which;
3288 int q = stbi__get8(z->s);
3289 for (which = 0; which < z->s->img_n; ++which)
3290 if (z->img_comp[which].id ==
id)
3292 if (which == z->s->img_n)
3294 z->img_comp[which].hd = q >> 4;
3295 if (z->img_comp[which].hd > 3)
3296 return stbi__err(
"bad DC huff",
"Corrupt JPEG");
3297 z->img_comp[which].ha = q & 15;
3298 if (z->img_comp[which].ha > 3)
3299 return stbi__err(
"bad AC huff",
"Corrupt JPEG");
3300 z->order[i] = which;
3305 z->spec_start = stbi__get8(z->s);
3306 z->spec_end = stbi__get8(z->s);
3307 aa = stbi__get8(z->s);
3308 z->succ_high = (aa >> 4);
3309 z->succ_low = (aa & 15);
3310 if (z->progressive) {
3311 if (z->spec_start > 63 || z->spec_end > 63 ||
3312 z->spec_start > z->spec_end || z->succ_high > 13 ||
3314 return stbi__err(
"bad SOS",
"Corrupt JPEG");
3316 if (z->spec_start != 0)
3317 return stbi__err(
"bad SOS",
"Corrupt JPEG");
3318 if (z->succ_high != 0 || z->succ_low != 0)
3319 return stbi__err(
"bad SOS",
"Corrupt JPEG");
3327 static int stbi__free_jpeg_components(stbi__jpeg *z,
int ncomp,
int why)
3330 for (i = 0; i < ncomp; ++i) {
3331 if (z->img_comp[i].raw_data) {
3332 STBI_FREE(z->img_comp[i].raw_data);
3333 z->img_comp[i].raw_data = NULL;
3334 z->img_comp[i].data = NULL;
3336 if (z->img_comp[i].raw_coeff) {
3337 STBI_FREE(z->img_comp[i].raw_coeff);
3338 z->img_comp[i].raw_coeff = 0;
3339 z->img_comp[i].coeff = 0;
3341 if (z->img_comp[i].linebuf) {
3342 STBI_FREE(z->img_comp[i].linebuf);
3343 z->img_comp[i].linebuf = NULL;
3349 static int stbi__process_frame_header(stbi__jpeg *z,
int scan)
3351 stbi__context *s = z->s;
3352 int Lf, p, i, q, h_max = 1, v_max = 1, c;
3353 Lf = stbi__get16be(s);
3355 return stbi__err(
"bad SOF len",
"Corrupt JPEG");
3360 "JPEG format not supported: 8-bit only");
3361 s->img_y = stbi__get16be(s);
3365 "JPEG format not supported: delayed height");
3368 s->img_x = stbi__get16be(s);
3370 return stbi__err(
"0 width",
"Corrupt JPEG");
3372 if (c != 3 && c != 1 && c != 4)
3373 return stbi__err(
"bad component count",
"Corrupt JPEG");
3375 for (i = 0; i < c; ++i) {
3376 z->img_comp[i].data = NULL;
3377 z->img_comp[i].linebuf = NULL;
3380 if (Lf != 8 + 3 * s->img_n)
3381 return stbi__err(
"bad SOF len",
"Corrupt JPEG");
3384 for (i = 0; i < s->img_n; ++i) {
3385 static unsigned char rgb[3] = {
'R',
'G',
'B'};
3386 z->img_comp[i].id = stbi__get8(s);
3387 if (s->img_n == 3 && z->img_comp[i].id == rgb[i])
3390 z->img_comp[i].h = (q >> 4);
3391 if (!z->img_comp[i].h || z->img_comp[i].h > 4)
3392 return stbi__err(
"bad H",
"Corrupt JPEG");
3393 z->img_comp[i].v = q & 15;
3394 if (!z->img_comp[i].v || z->img_comp[i].v > 4)
3395 return stbi__err(
"bad V",
"Corrupt JPEG");
3396 z->img_comp[i].tq = stbi__get8(s);
3397 if (z->img_comp[i].tq > 3)
3398 return stbi__err(
"bad TQ",
"Corrupt JPEG");
3401 if (scan != STBI__SCAN_load)
3404 if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0))
3405 return stbi__err(
"too large",
"Image too large to decode");
3407 for (i = 0; i < s->img_n; ++i) {
3408 if (z->img_comp[i].h > h_max)
3409 h_max = z->img_comp[i].h;
3410 if (z->img_comp[i].v > v_max)
3411 v_max = z->img_comp[i].v;
3415 z->img_h_max = h_max;
3416 z->img_v_max = v_max;
3417 z->img_mcu_w = h_max * 8;
3418 z->img_mcu_h = v_max * 8;
3420 z->img_mcu_x = (s->img_x + z->img_mcu_w - 1) / z->img_mcu_w;
3421 z->img_mcu_y = (s->img_y + z->img_mcu_h - 1) / z->img_mcu_h;
3423 for (i = 0; i < s->img_n; ++i) {
3425 z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max - 1) / h_max;
3426 z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max - 1) / v_max;
3435 z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
3436 z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
3437 z->img_comp[i].coeff = 0;
3438 z->img_comp[i].raw_coeff = 0;
3439 z->img_comp[i].linebuf = NULL;
3440 z->img_comp[i].raw_data =
3441 stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15);
3442 if (z->img_comp[i].raw_data == NULL)
3443 return stbi__free_jpeg_components(
3444 z, i + 1, stbi__err(
"outofmem",
"Out of memory"));
3446 z->img_comp[i].data =
3447 (
stbi_uc *)(((
size_t)z->img_comp[i].raw_data + 15) & ~15);
3448 if (z->progressive) {
3450 z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8;
3451 z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8;
3452 z->img_comp[i].raw_coeff = stbi__malloc_mad3(
3453 z->img_comp[i].w2, z->img_comp[i].h2,
sizeof(
short), 15);
3454 if (z->img_comp[i].raw_coeff == NULL)
3455 return stbi__free_jpeg_components(
3456 z, i + 1, stbi__err(
"outofmem",
"Out of memory"));
3457 z->img_comp[i].coeff =
3458 (
short *)(((
size_t)z->img_comp[i].raw_coeff + 15) & ~15);
3466 #define stbi__DNL(x) ((x) == 0xdc)
3467 #define stbi__SOI(x) ((x) == 0xd8)
3468 #define stbi__EOI(x) ((x) == 0xd9)
3469 #define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2)
3470 #define stbi__SOS(x) ((x) == 0xda)
3472 #define stbi__SOF_progressive(x) ((x) == 0xc2)
3474 static int stbi__decode_jpeg_header(stbi__jpeg *z,
int scan)
3478 z->app14_color_transform = -1;
3479 z->marker = STBI__MARKER_none;
3480 m = stbi__get_marker(z);
3482 return stbi__err(
"no SOI",
"Corrupt JPEG");
3483 if (scan == STBI__SCAN_type)
3485 m = stbi__get_marker(z);
3486 while (!stbi__SOF(m)) {
3487 if (!stbi__process_marker(z, m))
3489 m = stbi__get_marker(z);
3490 while (m == STBI__MARKER_none) {
3493 if (stbi__at_eof(z->s))
3494 return stbi__err(
"no SOF",
"Corrupt JPEG");
3495 m = stbi__get_marker(z);
3498 z->progressive = stbi__SOF_progressive(m);
3499 if (!stbi__process_frame_header(z, scan))
3505 static int stbi__decode_jpeg_image(stbi__jpeg *j)
3508 for (m = 0; m < 4; m++) {
3509 j->img_comp[m].raw_data = NULL;
3510 j->img_comp[m].raw_coeff = NULL;
3512 j->restart_interval = 0;
3513 if (!stbi__decode_jpeg_header(j, STBI__SCAN_load))
3515 m = stbi__get_marker(j);
3516 while (!stbi__EOI(m)) {
3518 if (!stbi__process_scan_header(j))
3520 if (!stbi__parse_entropy_coded_data(j))
3522 if (j->marker == STBI__MARKER_none) {
3524 while (!stbi__at_eof(j->s)) {
3525 int x = stbi__get8(j->s);
3527 j->marker = stbi__get8(j->s);
3534 }
else if (stbi__DNL(m)) {
3535 int Ld = stbi__get16be(j->s);
3536 stbi__uint32 NL = stbi__get16be(j->s);
3538 stbi__err(
"bad DNL len",
"Corrupt JPEG");
3539 if (NL != j->s->img_y)
3540 stbi__err(
"bad DNL height",
"Corrupt JPEG");
3542 if (!stbi__process_marker(j, m))
3545 m = stbi__get_marker(j);
3548 stbi__jpeg_finish(j);
3557 #define stbi__div4(x) ((stbi_uc)((x) >> 2))
3563 STBI_NOTUSED(in_far);
3570 stbi_uc *in_far,
int w,
int hs)
3575 for (i = 0; i < w; ++i)
3576 out[i] = stbi__div4(3 * in_near[i] + in_far[i] + 2);
3581 stbi_uc *in_far,
int w,
int hs)
3589 out[0] = out[1] = input[0];
3594 out[1] = stbi__div4(input[0] * 3 + input[1] + 2);
3595 for (i = 1; i < w - 1; ++i) {
3596 int n = 3 * input[i] + 2;
3597 out[i * 2 + 0] = stbi__div4(n + input[i - 1]);
3598 out[i * 2 + 1] = stbi__div4(n + input[i + 1]);
3600 out[i * 2 + 0] = stbi__div4(input[w - 2] * 3 + input[w - 1] + 2);
3601 out[i * 2 + 1] = input[w - 1];
3603 STBI_NOTUSED(in_far);
3609 #define stbi__div16(x) ((stbi_uc)((x) >> 4))
3612 stbi_uc *in_far,
int w,
int hs)
3617 out[0] = out[1] = stbi__div4(3 * in_near[0] + in_far[0] + 2);
3621 t1 = 3 * in_near[0] + in_far[0];
3622 out[0] = stbi__div4(t1 + 2);
3623 for (i = 1; i < w; ++i) {
3625 t1 = 3 * in_near[i] + in_far[i];
3626 out[i * 2 - 1] = stbi__div16(3 * t0 + t1 + 8);
3627 out[i * 2] = stbi__div16(3 * t1 + t0 + 8);
3629 out[w * 2 - 1] = stbi__div4(t1 + 2);
3636 #if defined(STBI_SSE2) || defined(STBI_NEON)
3638 stbi_uc *in_far,
int w,
int hs)
3644 out[0] = out[1] = stbi__div4(3 * in_near[0] + in_far[0] + 2);
3648 t1 = 3 * in_near[0] + in_far[0];
3652 for (; i < ((w - 1) & ~7); i += 8) {
3653 #if defined(STBI_SSE2)
3656 __m128i zero = _mm_setzero_si128();
3657 __m128i farb = _mm_loadl_epi64((__m128i *)(in_far + i));
3658 __m128i nearb = _mm_loadl_epi64((__m128i *)(in_near + i));
3659 __m128i farw = _mm_unpacklo_epi8(farb, zero);
3660 __m128i nearw = _mm_unpacklo_epi8(nearb, zero);
3661 __m128i diff = _mm_sub_epi16(farw, nearw);
3662 __m128i nears = _mm_slli_epi16(nearw, 2);
3663 __m128i curr = _mm_add_epi16(nears, diff);
3670 __m128i prv0 = _mm_slli_si128(curr, 2);
3671 __m128i nxt0 = _mm_srli_si128(curr, 2);
3672 __m128i prev = _mm_insert_epi16(prv0, t1, 0);
3674 _mm_insert_epi16(nxt0, 3 * in_near[i + 8] + in_far[i + 8], 7);
3680 __m128i bias = _mm_set1_epi16(8);
3681 __m128i curs = _mm_slli_epi16(curr, 2);
3682 __m128i prvd = _mm_sub_epi16(prev, curr);
3683 __m128i nxtd = _mm_sub_epi16(next, curr);
3684 __m128i curb = _mm_add_epi16(curs, bias);
3685 __m128i even = _mm_add_epi16(prvd, curb);
3686 __m128i odd = _mm_add_epi16(nxtd, curb);
3689 __m128i int0 = _mm_unpacklo_epi16(even, odd);
3690 __m128i int1 = _mm_unpackhi_epi16(even, odd);
3691 __m128i de0 = _mm_srli_epi16(int0, 4);
3692 __m128i de1 = _mm_srli_epi16(int1, 4);
3695 __m128i outv = _mm_packus_epi16(de0, de1);
3696 _mm_storeu_si128((__m128i *)(out + i * 2), outv);
3697 #elif defined(STBI_NEON)
3700 uint8x8_t farb = vld1_u8(in_far + i);
3701 uint8x8_t nearb = vld1_u8(in_near + i);
3702 int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb));
3703 int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2));
3704 int16x8_t curr = vaddq_s16(nears, diff);
3711 int16x8_t prv0 = vextq_s16(curr, curr, 7);
3712 int16x8_t nxt0 = vextq_s16(curr, curr, 1);
3713 int16x8_t prev = vsetq_lane_s16(t1, prv0, 0);
3715 vsetq_lane_s16(3 * in_near[i + 8] + in_far[i + 8], nxt0, 7);
3721 int16x8_t curs = vshlq_n_s16(curr, 2);
3722 int16x8_t prvd = vsubq_s16(prev, curr);
3723 int16x8_t nxtd = vsubq_s16(next, curr);
3724 int16x8_t even = vaddq_s16(curs, prvd);
3725 int16x8_t odd = vaddq_s16(curs, nxtd);
3729 o.val[0] = vqrshrun_n_s16(even, 4);
3730 o.val[1] = vqrshrun_n_s16(odd, 4);
3731 vst2_u8(out + i * 2, o);
3735 t1 = 3 * in_near[i + 7] + in_far[i + 7];
3739 t1 = 3 * in_near[i] + in_far[i];
3740 out[i * 2] = stbi__div16(3 * t1 + t0 + 8);
3742 for (++i; i < w; ++i) {
3744 t1 = 3 * in_near[i] + in_far[i];
3745 out[i * 2 - 1] = stbi__div16(3 * t0 + t1 + 8);
3746 out[i * 2] = stbi__div16(3 * t1 + t0 + 8);
3748 out[w * 2 - 1] = stbi__div4(t1 + 2);
3757 stbi_uc *in_far,
int w,
int hs)
3761 STBI_NOTUSED(in_far);
3762 for (i = 0; i < w; ++i)
3763 for (j = 0; j < hs; ++j)
3764 out[i * hs + j] = in_near[i];
3770 #define stbi__float2fixed(x) (((int)((x)*4096.0f + 0.5f)) << 8)
3771 static void stbi__YCbCr_to_RGB_row(
stbi_uc *out,
const stbi_uc *y,
3773 int count,
int step)
3776 for (i = 0; i < count; ++i) {
3777 int y_fixed = (y[i] << 20) + (1 << 19);
3779 int cr = pcr[i] - 128;
3780 int cb = pcb[i] - 128;
3781 r = y_fixed + cr * stbi__float2fixed(1.40200f);
3782 g = y_fixed + (cr * -stbi__float2fixed(0.71414f)) +
3783 ((cb * -stbi__float2fixed(0.34414f)) & 0xffff0000);
3784 b = y_fixed + cb * stbi__float2fixed(1.77200f);
3788 if ((
unsigned)r > 255) {
3794 if ((
unsigned)g > 255) {
3800 if ((
unsigned)b > 255) {
3814 #if defined(STBI_SSE2) || defined(STBI_NEON)
3815 static void stbi__YCbCr_to_RGB_simd(
stbi_uc *out,
stbi_uc const *y,
3817 int count,
int step)
3828 __m128i signflip = _mm_set1_epi8(-0x80);
3829 __m128i cr_const0 = _mm_set1_epi16((
short)(1.40200f * 4096.0f + 0.5f));
3830 __m128i cr_const1 = _mm_set1_epi16(-(
short)(0.71414f * 4096.0f + 0.5f));
3831 __m128i cb_const0 = _mm_set1_epi16(-(
short)(0.34414f * 4096.0f + 0.5f));
3832 __m128i cb_const1 = _mm_set1_epi16((
short)(1.77200f * 4096.0f + 0.5f));
3833 __m128i y_bias = _mm_set1_epi8((
char)(
unsigned char)128);
3834 __m128i xw = _mm_set1_epi16(255);
3836 for (; i + 7 < count; i += 8) {
3838 __m128i y_bytes = _mm_loadl_epi64((__m128i *)(y + i));
3839 __m128i cr_bytes = _mm_loadl_epi64((__m128i *)(pcr + i));
3840 __m128i cb_bytes = _mm_loadl_epi64((__m128i *)(pcb + i));
3841 __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip);
3842 __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip);
3845 __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes);
3846 __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased);
3847 __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased);
3850 __m128i yws = _mm_srli_epi16(yw, 4);
3851 __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw);
3852 __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw);
3853 __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1);
3854 __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1);
3855 __m128i rws = _mm_add_epi16(cr0, yws);
3856 __m128i gwt = _mm_add_epi16(cb0, yws);
3857 __m128i bws = _mm_add_epi16(yws, cb1);
3858 __m128i gws = _mm_add_epi16(gwt, cr1);
3861 __m128i rw = _mm_srai_epi16(rws, 4);
3862 __m128i bw = _mm_srai_epi16(bws, 4);
3863 __m128i gw = _mm_srai_epi16(gws, 4);
3866 __m128i brb = _mm_packus_epi16(rw, bw);
3867 __m128i gxb = _mm_packus_epi16(gw, xw);
3870 __m128i t0 = _mm_unpacklo_epi8(brb, gxb);
3871 __m128i t1 = _mm_unpackhi_epi8(brb, gxb);
3872 __m128i o0 = _mm_unpacklo_epi16(t0, t1);
3873 __m128i o1 = _mm_unpackhi_epi16(t0, t1);
3876 _mm_storeu_si128((__m128i *)(out + 0), o0);
3877 _mm_storeu_si128((__m128i *)(out + 16), o1);
3889 uint8x8_t signflip = vdup_n_u8(0x80);
3890 int16x8_t cr_const0 = vdupq_n_s16((
short)(1.40200f * 4096.0f + 0.5f));
3891 int16x8_t cr_const1 = vdupq_n_s16(-(
short)(0.71414f * 4096.0f + 0.5f));
3892 int16x8_t cb_const0 = vdupq_n_s16(-(
short)(0.34414f * 4096.0f + 0.5f));
3893 int16x8_t cb_const1 = vdupq_n_s16((
short)(1.77200f * 4096.0f + 0.5f));
3895 for (; i + 7 < count; i += 8) {
3897 uint8x8_t y_bytes = vld1_u8(y + i);
3898 uint8x8_t cr_bytes = vld1_u8(pcr + i);
3899 uint8x8_t cb_bytes = vld1_u8(pcb + i);
3900 int8x8_t cr_biased =
3901 vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip));
3902 int8x8_t cb_biased =
3903 vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip));
3906 int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4));
3907 int16x8_t crw = vshll_n_s8(cr_biased, 7);
3908 int16x8_t cbw = vshll_n_s8(cb_biased, 7);
3911 int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0);
3912 int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0);
3913 int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1);
3914 int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1);
3915 int16x8_t rws = vaddq_s16(yws, cr0);
3916 int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1);
3917 int16x8_t bws = vaddq_s16(yws, cb1);
3921 o.val[0] = vqrshrun_n_s16(rws, 4);
3922 o.val[1] = vqrshrun_n_s16(gws, 4);
3923 o.val[2] = vqrshrun_n_s16(bws, 4);
3924 o.val[3] = vdup_n_u8(255);
3933 for (; i < count; ++i) {
3934 int y_fixed = (y[i] << 20) + (1 << 19);
3936 int cr = pcr[i] - 128;
3937 int cb = pcb[i] - 128;
3938 r = y_fixed + cr * stbi__float2fixed(1.40200f);
3939 g = y_fixed + cr * -stbi__float2fixed(0.71414f) +
3940 ((cb * -stbi__float2fixed(0.34414f)) & 0xffff0000);
3941 b = y_fixed + cb * stbi__float2fixed(1.77200f);
3945 if ((
unsigned)r > 255) {
3951 if ((
unsigned)g > 255) {
3957 if ((
unsigned)b > 255) {
3973 static void stbi__setup_jpeg(stbi__jpeg *j)
3975 j->idct_block_kernel = stbi__idct_block;
3976 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row;
3977 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2;
3980 if (stbi__sse2_available()) {
3981 j->idct_block_kernel = stbi__idct_simd;
3982 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
3983 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
3988 j->idct_block_kernel = stbi__idct_simd;
3989 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
3990 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
3995 static void stbi__cleanup_jpeg(stbi__jpeg *j)
3997 stbi__free_jpeg_components(j, j->s->img_n, 0);
4001 resample_row_func resample;
4012 unsigned int t = x * y + 128;
4013 return (
stbi_uc)((t + (t >> 8)) >> 8);
4016 static stbi_uc *load_jpeg_image(stbi__jpeg *z,
int *out_x,
int *out_y,
4017 int *comp,
int req_comp)
4019 int n, decode_n, is_rgb;
4023 if (req_comp < 0 || req_comp > 4)
4024 return stbi__errpuc(
"bad req_comp",
"Internal error");
4027 if (!stbi__decode_jpeg_image(z)) {
4028 stbi__cleanup_jpeg(z);
4033 n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1;
4035 is_rgb = z->s->img_n == 3 &&
4036 (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif));
4038 if (z->s->img_n == 3 && n < 3 && !is_rgb)
4041 decode_n = z->s->img_n;
4050 stbi__resample res_comp[4];
4052 for (k = 0; k < decode_n; ++k) {
4053 stbi__resample *r = &res_comp[k];
4057 z->img_comp[k].linebuf = (
stbi_uc *)stbi__malloc(z->s->img_x + 3);
4058 if (!z->img_comp[k].linebuf) {
4059 stbi__cleanup_jpeg(z);
4060 return stbi__errpuc(
"outofmem",
"Out of memory");
4063 r->hs = z->img_h_max / z->img_comp[k].h;
4064 r->vs = z->img_v_max / z->img_comp[k].v;
4065 r->ystep = r->vs >> 1;
4066 r->w_lores = (z->s->img_x + r->hs - 1) / r->hs;
4068 r->line0 = r->line1 = z->img_comp[k].data;
4070 if (r->hs == 1 && r->vs == 1)
4071 r->resample = resample_row_1;
4072 else if (r->hs == 1 && r->vs == 2)
4073 r->resample = stbi__resample_row_v_2;
4074 else if (r->hs == 2 && r->vs == 1)
4075 r->resample = stbi__resample_row_h_2;
4076 else if (r->hs == 2 && r->vs == 2)
4077 r->resample = z->resample_row_hv_2_kernel;
4079 r->resample = stbi__resample_row_generic;
4083 output = (
stbi_uc *)stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1);
4085 stbi__cleanup_jpeg(z);
4086 return stbi__errpuc(
"outofmem",
"Out of memory");
4090 for (j = 0; j < z->s->img_y; ++j) {
4091 stbi_uc *out = output + n * z->s->img_x * j;
4092 for (k = 0; k < decode_n; ++k) {
4093 stbi__resample *r = &res_comp[k];
4094 int y_bot = r->ystep >= (r->vs >> 1);
4095 coutput[k] = r->resample(
4096 z->img_comp[k].linebuf, y_bot ? r->line1 : r->line0,
4097 y_bot ? r->line0 : r->line1, r->w_lores, r->hs);
4098 if (++r->ystep >= r->vs) {
4100 r->line0 = r->line1;
4101 if (++r->ypos < z->img_comp[k].y)
4102 r->line1 += z->img_comp[k].w2;
4107 if (z->s->img_n == 3) {
4109 for (i = 0; i < z->s->img_x; ++i) {
4111 out[1] = coutput[1][i];
4112 out[2] = coutput[2][i];
4117 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2],
4120 }
else if (z->s->img_n == 4) {
4121 if (z->app14_color_transform == 0) {
4122 for (i = 0; i < z->s->img_x; ++i) {
4124 out[0] = stbi__blinn_8x8(coutput[0][i], m);
4125 out[1] = stbi__blinn_8x8(coutput[1][i], m);
4126 out[2] = stbi__blinn_8x8(coutput[2][i], m);
4130 }
else if (z->app14_color_transform == 2) {
4131 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2],
4133 for (i = 0; i < z->s->img_x; ++i) {
4135 out[0] = stbi__blinn_8x8(255 - out[0], m);
4136 out[1] = stbi__blinn_8x8(255 - out[1], m);
4137 out[2] = stbi__blinn_8x8(255 - out[2], m);
4142 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2],
4146 for (i = 0; i < z->s->img_x; ++i) {
4147 out[0] = out[1] = out[2] = y[i];
4154 for (i = 0; i < z->s->img_x; ++i)
4155 *out++ = stbi__compute_y(
4156 coutput[0][i], coutput[1][i], coutput[2][i]);
4158 for (i = 0; i < z->s->img_x; ++i, out += 2) {
4159 out[0] = stbi__compute_y(
4160 coutput[0][i], coutput[1][i], coutput[2][i]);
4164 }
else if (z->s->img_n == 4 && z->app14_color_transform == 0) {
4165 for (i = 0; i < z->s->img_x; ++i) {
4167 stbi_uc r = stbi__blinn_8x8(coutput[0][i], m);
4168 stbi_uc g = stbi__blinn_8x8(coutput[1][i], m);
4169 stbi_uc b = stbi__blinn_8x8(coutput[2][i], m);
4170 out[0] = stbi__compute_y(r, g, b);
4174 }
else if (z->s->img_n == 4 && z->app14_color_transform == 2) {
4175 for (i = 0; i < z->s->img_x; ++i) {
4177 stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]);
4184 for (i = 0; i < z->s->img_x; ++i)
4187 for (i = 0; i < z->s->img_x; ++i)
4188 *out++ = y[i], *out++ = 255;
4192 stbi__cleanup_jpeg(z);
4193 *out_x = z->s->img_x;
4194 *out_y = z->s->img_y;
4196 *comp = z->s->img_n >= 3
4203 static void *stbi__jpeg_load(stbi__context *s,
int *x,
int *y,
int *comp,
4204 int req_comp, stbi__result_info *ri)
4206 unsigned char *result;
4207 stbi__jpeg *j = (stbi__jpeg *)stbi__malloc(
sizeof(stbi__jpeg));
4210 stbi__setup_jpeg(j);
4211 result = load_jpeg_image(j, x, y, comp, req_comp);
4216 static int stbi__jpeg_test(stbi__context *s)
4219 stbi__jpeg *j = (stbi__jpeg *)stbi__malloc(
sizeof(stbi__jpeg));
4221 stbi__setup_jpeg(j);
4222 r = stbi__decode_jpeg_header(j, STBI__SCAN_type);
4228 static int stbi__jpeg_info_raw(stbi__jpeg *j,
int *x,
int *y,
int *comp)
4230 if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) {
4239 *comp = j->s->img_n >= 3 ? 3 : 1;
4243 static int stbi__jpeg_info(stbi__context *s,
int *x,
int *y,
int *comp)
4246 stbi__jpeg *j = (stbi__jpeg *)(stbi__malloc(
sizeof(stbi__jpeg)));
4248 result = stbi__jpeg_info_raw(j, x, y, comp);
4262 #ifndef STBI_NO_ZLIB
4265 #define STBI__ZFAST_BITS 9 // accelerate all cases in default tables
4266 #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
4271 stbi__uint16 fast[1 << STBI__ZFAST_BITS];
4272 stbi__uint16 firstcode[16];
4274 stbi__uint16 firstsymbol[16];
4276 stbi__uint16 value[288];
4279 stbi_inline
static int stbi__bitreverse16(
int n)
4281 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
4282 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
4283 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
4284 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
4288 stbi_inline
static int stbi__bit_reverse(
int v,
int bits)
4290 STBI_ASSERT(bits <= 16);
4293 return stbi__bitreverse16(v) >> (16 - bits);
4296 static int stbi__zbuild_huffman(stbi__zhuffman *z,
const stbi_uc *sizelist,
4300 int code, next_code[16], sizes[17];
4303 memset(sizes, 0,
sizeof(sizes));
4304 memset(z->fast, 0,
sizeof(z->fast));
4305 for (i = 0; i < num; ++i)
4306 ++sizes[sizelist[i]];
4308 for (i = 1; i < 16; ++i)
4309 if (sizes[i] > (1 << i))
4310 return stbi__err(
"bad sizes",
"Corrupt PNG");
4312 for (i = 1; i < 16; ++i) {
4313 next_code[i] = code;
4314 z->firstcode[i] = (stbi__uint16)code;
4315 z->firstsymbol[i] = (stbi__uint16)k;
4316 code = (code + sizes[i]);
4318 if (code - 1 >= (1 << i))
4319 return stbi__err(
"bad codelengths",
"Corrupt PNG");
4320 z->maxcode[i] = code << (16 - i);
4324 z->maxcode[16] = 0x10000;
4325 for (i = 0; i < num; ++i) {
4326 int s = sizelist[i];
4328 int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
4329 stbi__uint16 fastv = (stbi__uint16)((s << 9) | i);
4331 z->value[c] = (stbi__uint16)i;
4332 if (s <= STBI__ZFAST_BITS) {
4333 int j = stbi__bit_reverse(next_code[s], s);
4334 while (j < (1 << STBI__ZFAST_BITS)) {
4352 stbi_uc *zbuffer, *zbuffer_end;
4354 stbi__uint32 code_buffer;
4361 stbi__zhuffman z_length, z_distance;
4364 stbi_inline
static stbi_uc stbi__zget8(stbi__zbuf *z)
4366 if (z->zbuffer >= z->zbuffer_end)
4368 return *z->zbuffer++;
4371 static void stbi__fill_bits(stbi__zbuf *z)
4374 STBI_ASSERT(z->code_buffer < (1U << z->num_bits));
4375 z->code_buffer |= (
unsigned int)stbi__zget8(z) << z->num_bits;
4377 }
while (z->num_bits <= 24);
4380 stbi_inline
static unsigned int stbi__zreceive(stbi__zbuf *z,
int n)
4383 if (z->num_bits < n)
4385 k = z->code_buffer & ((1 << n) - 1);
4386 z->code_buffer >>= n;
4391 static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
4396 k = stbi__bit_reverse(a->code_buffer, 16);
4397 for (s = STBI__ZFAST_BITS + 1;; ++s)
4398 if (k < z->maxcode[s])
4403 b = (k >> (16 - s)) - z->firstcode[s] + z->firstsymbol[s];
4404 STBI_ASSERT(z->size[b] == s);
4405 a->code_buffer >>= s;
4410 stbi_inline
static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
4413 if (a->num_bits < 16)
4415 b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
4418 a->code_buffer >>= s;
4422 return stbi__zhuffman_decode_slowpath(a, z);
4425 static int stbi__zexpand(stbi__zbuf *z,
char *zout,
4429 int cur, limit, old_limit;
4431 if (!z->z_expandable)
4432 return stbi__err(
"output buffer limit",
"Corrupt PNG");
4433 cur = (int)(z->zout - z->zout_start);
4434 limit = old_limit = (int)(z->zout_end - z->zout_start);
4435 while (cur + n > limit)
4437 q = (
char *)STBI_REALLOC_SIZED(z->zout_start, old_limit, limit);
4438 STBI_NOTUSED(old_limit);
4440 return stbi__err(
"outofmem",
"Out of memory");
4443 z->zout_end = q + limit;
4447 static int stbi__zlength_base[31] = {
4448 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
4449 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
4451 static int stbi__zlength_extra[31] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
4452 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4,
4453 4, 4, 5, 5, 5, 5, 0, 0, 0};
4455 static int stbi__zdist_base[32] = {
4456 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33,
4457 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537,
4458 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0};
4460 static int stbi__zdist_extra[32] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
4461 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
4462 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
4464 static int stbi__parse_huffman_block(stbi__zbuf *a)
4466 char *zout = a->zout;
4468 int z = stbi__zhuffman_decode(a, &a->z_length);
4471 return stbi__err(
"bad huffman code",
4473 if (zout >= a->zout_end) {
4474 if (!stbi__zexpand(a, zout, 1))
4487 len = stbi__zlength_base[z];
4488 if (stbi__zlength_extra[z])
4489 len += stbi__zreceive(a, stbi__zlength_extra[z]);
4490 z = stbi__zhuffman_decode(a, &a->z_distance);
4492 return stbi__err(
"bad huffman code",
"Corrupt PNG");
4493 dist = stbi__zdist_base[z];
4494 if (stbi__zdist_extra[z])
4495 dist += stbi__zreceive(a, stbi__zdist_extra[z]);
4496 if (zout - a->zout_start < dist)
4497 return stbi__err(
"bad dist",
"Corrupt PNG");
4498 if (zout + len > a->zout_end) {
4499 if (!stbi__zexpand(a, zout, len))
4522 static int stbi__compute_huffman_codes(stbi__zbuf *a)
4524 static stbi_uc length_dezigzag[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
4525 11, 4, 12, 3, 13, 2, 14, 1, 15};
4526 stbi__zhuffman z_codelength;
4527 stbi_uc lencodes[286 + 32 + 137];
4531 int hlit = stbi__zreceive(a, 5) + 257;
4532 int hdist = stbi__zreceive(a, 5) + 1;
4533 int hclen = stbi__zreceive(a, 4) + 4;
4534 int ntot = hlit + hdist;
4536 memset(codelength_sizes, 0,
sizeof(codelength_sizes));
4537 for (i = 0; i < hclen; ++i) {
4538 int s = stbi__zreceive(a, 3);
4539 codelength_sizes[length_dezigzag[i]] = (
stbi_uc)s;
4541 if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19))
4546 int c = stbi__zhuffman_decode(a, &z_codelength);
4547 if (c < 0 || c >= 19)
4548 return stbi__err(
"bad codelengths",
"Corrupt PNG");
4554 c = stbi__zreceive(a, 2) + 3;
4556 return stbi__err(
"bad codelengths",
"Corrupt PNG");
4557 fill = lencodes[n - 1];
4559 c = stbi__zreceive(a, 3) + 3;
4561 STBI_ASSERT(c == 18);
4562 c = stbi__zreceive(a, 7) + 11;
4565 return stbi__err(
"bad codelengths",
"Corrupt PNG");
4566 memset(lencodes + n, fill, c);
4571 return stbi__err(
"bad codelengths",
"Corrupt PNG");
4572 if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit))
4574 if (!stbi__zbuild_huffman(&a->z_distance, lencodes + hlit, hdist))
4579 static int stbi__parse_uncompressed_block(stbi__zbuf *a)
4583 if (a->num_bits & 7)
4584 stbi__zreceive(a, a->num_bits & 7);
4587 while (a->num_bits > 0) {
4589 (
stbi_uc)(a->code_buffer & 255);
4590 a->code_buffer >>= 8;
4593 STBI_ASSERT(a->num_bits == 0);
4596 header[k++] = stbi__zget8(a);
4597 len = header[1] * 256 + header[0];
4598 nlen = header[3] * 256 + header[2];
4599 if (nlen != (len ^ 0xffff))
4600 return stbi__err(
"zlib corrupt",
"Corrupt PNG");
4601 if (a->zbuffer + len > a->zbuffer_end)
4602 return stbi__err(
"read past buffer",
"Corrupt PNG");
4603 if (a->zout + len > a->zout_end)
4604 if (!stbi__zexpand(a, a->zout, len))
4606 memcpy(a->zout, a->zbuffer, len);
4612 static int stbi__parse_zlib_header(stbi__zbuf *a)
4614 int cmf = stbi__zget8(a);
4617 int flg = stbi__zget8(a);
4618 if ((cmf * 256 + flg) % 31 != 0)
4619 return stbi__err(
"bad zlib header",
"Corrupt PNG");
4621 return stbi__err(
"no preset dict",
4624 return stbi__err(
"bad compression",
4630 static const stbi_uc stbi__zdefault_length[288] = {
4631 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
4632 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
4633 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
4634 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
4635 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
4636 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
4637 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
4638 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
4639 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
4640 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
4641 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7,
4642 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8};
4643 static const stbi_uc stbi__zdefault_distance[32] = {
4644 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
4645 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
4659 static int stbi__parse_zlib(stbi__zbuf *a,
int parse_header)
4663 if (!stbi__parse_zlib_header(a))
4668 final = stbi__zreceive(a, 1);
4669 type = stbi__zreceive(a, 2);
4671 if (!stbi__parse_uncompressed_block(a))
4673 }
else if (type == 3) {
4678 if (!stbi__zbuild_huffman(&a->z_length, stbi__zdefault_length,
4681 if (!stbi__zbuild_huffman(&a->z_distance,
4682 stbi__zdefault_distance, 32))
4685 if (!stbi__compute_huffman_codes(a))
4688 if (!stbi__parse_huffman_block(a))
4695 static int stbi__do_zlib(stbi__zbuf *a,
char *obuf,
int olen,
int exp,
4698 a->zout_start = obuf;
4700 a->zout_end = obuf + olen;
4701 a->z_expandable = exp;
4703 return stbi__parse_zlib(a, parse_header);
4707 int initial_size,
int *outlen)
4710 char *p = (
char *)stbi__malloc(initial_size);
4713 a.zbuffer = (
stbi_uc *)buffer;
4714 a.zbuffer_end = (
stbi_uc *)buffer + len;
4715 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
4717 *outlen = (int)(a.zout - a.zout_start);
4718 return a.zout_start;
4720 STBI_FREE(a.zout_start);
4737 char *p = (
char *)stbi__malloc(initial_size);
4740 a.zbuffer = (
stbi_uc *)buffer;
4741 a.zbuffer_end = (
stbi_uc *)buffer + len;
4742 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
4744 *outlen = (int)(a.zout - a.zout_start);
4745 return a.zout_start;
4747 STBI_FREE(a.zout_start);
4753 char const *ibuffer,
int ilen)
4756 a.zbuffer = (
stbi_uc *)ibuffer;
4757 a.zbuffer_end = (
stbi_uc *)ibuffer + ilen;
4758 if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
4759 return (
int)(a.zout - a.zout_start);
4768 char *p = (
char *)stbi__malloc(16384);
4771 a.zbuffer = (
stbi_uc *)buffer;
4772 a.zbuffer_end = (
stbi_uc *)buffer + len;
4773 if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
4775 *outlen = (int)(a.zout - a.zout_start);
4776 return a.zout_start;
4778 STBI_FREE(a.zout_start);
4784 const char *ibuffer,
int ilen)
4787 a.zbuffer = (
stbi_uc *)ibuffer;
4788 a.zbuffer_end = (
stbi_uc *)ibuffer + ilen;
4789 if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
4790 return (
int)(a.zout - a.zout_start);
4808 stbi__uint32 length;
4812 static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
4815 c.length = stbi__get32be(s);
4816 c.type = stbi__get32be(s);
4820 static int stbi__check_png_header(stbi__context *s)
4822 static stbi_uc png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
4824 for (i = 0; i < 8; ++i)
4825 if (stbi__get8(s) != png_sig[i])
4826 return stbi__err(
"bad png sig",
"Not a PNG");
4832 stbi_uc *idata, *expanded, *out;
4848 static stbi_uc first_row_filter[5] = {STBI__F_none, STBI__F_sub, STBI__F_none,
4849 STBI__F_avg_first, STBI__F_paeth_first};
4851 static int stbi__paeth(
int a,
int b,
int c)
4854 int pa = abs(p - a);
4855 int pb = abs(p - b);
4856 int pc = abs(p - c);
4857 if (pa <= pb && pa <= pc)
4864 static stbi_uc stbi__depth_scale_table[9] = {0, 0xff, 0x55, 0, 0x11,
4868 static int stbi__create_png_image_raw(stbi__png *a,
stbi_uc *raw,
4869 stbi__uint32 raw_len,
int out_n,
4870 stbi__uint32 x, stbi__uint32 y,
int depth,
4873 int bytes = (depth == 16 ? 2 : 1);
4874 stbi__context *s = a->s;
4875 stbi__uint32 i, j, stride = x * out_n * bytes;
4876 stbi__uint32 img_len, img_width_bytes;
4878 int img_n = s->img_n;
4880 int output_bytes = out_n * bytes;
4881 int filter_bytes = img_n * bytes;
4884 STBI_ASSERT(out_n == s->img_n || out_n == s->img_n + 1);
4885 a->out = (
stbi_uc *)stbi__malloc_mad3(
4886 x, y, output_bytes, 0);
4888 return stbi__err(
"outofmem",
"Out of memory");
4890 img_width_bytes = (((img_n * x * depth) + 7) >> 3);
4891 img_len = (img_width_bytes + 1) * y;
4896 if (raw_len < img_len)
4897 return stbi__err(
"not enough pixels",
"Corrupt PNG");
4899 for (j = 0; j < y; ++j) {
4900 stbi_uc *cur = a->out + stride * j;
4902 int filter = *raw++;
4905 return stbi__err(
"invalid filter",
"Corrupt PNG");
4908 STBI_ASSERT(img_width_bytes <= x);
4909 cur += x * out_n - img_width_bytes;
4913 width = img_width_bytes;
4915 prior = cur - stride;
4920 filter = first_row_filter[filter];
4923 for (k = 0; k < filter_bytes; ++k) {
4932 cur[k] = STBI__BYTECAST(raw[k] + prior[k]);
4935 cur[k] = STBI__BYTECAST(raw[k] + (prior[k] >> 1));
4938 cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0, prior[k], 0));
4940 case STBI__F_avg_first:
4943 case STBI__F_paeth_first:
4955 }
else if (depth == 16) {
4956 if (img_n != out_n) {
4957 cur[filter_bytes] = 255;
4958 cur[filter_bytes + 1] = 255;
4960 raw += filter_bytes;
4961 cur += output_bytes;
4962 prior += output_bytes;
4971 if (depth < 8 || img_n == out_n) {
4972 int nk = (width - 1) * filter_bytes;
4973 #define STBI__CASE(f) \
4975 for (k = 0; k < nk; ++k)
4979 memcpy(cur, raw, nk);
4981 STBI__CASE(STBI__F_sub)
4983 cur[k] = STBI__BYTECAST(raw[k] + cur[k - filter_bytes]);
4986 STBI__CASE(STBI__F_up)
4988 cur[k] = STBI__BYTECAST(raw[k] + prior[k]);
4991 STBI__CASE(STBI__F_avg)
4993 cur[k] = STBI__BYTECAST(
4994 raw[k] + ((prior[k] + cur[k - filter_bytes]) >> 1));
4997 STBI__CASE(STBI__F_paeth)
4999 cur[k] = STBI__BYTECAST(
5000 raw[k] + stbi__paeth(cur[k - filter_bytes], prior[k],
5001 prior[k - filter_bytes]));
5004 STBI__CASE(STBI__F_avg_first)
5007 STBI__BYTECAST(raw[k] + (cur[k - filter_bytes] >> 1));
5010 STBI__CASE(STBI__F_paeth_first)
5012 cur[k] = STBI__BYTECAST(
5013 raw[k] + stbi__paeth(cur[k - filter_bytes], 0, 0));
5020 STBI_ASSERT(img_n + 1 == out_n);
5021 #define STBI__CASE(f) \
5023 for (i = x - 1; i >= 1; --i, cur[filter_bytes] = 255, \
5024 raw += filter_bytes, cur += output_bytes, prior += output_bytes) \
5025 for (k = 0; k < filter_bytes; ++k)
5027 STBI__CASE(STBI__F_none) { cur[k] = raw[k]; }
5029 STBI__CASE(STBI__F_sub)
5031 cur[k] = STBI__BYTECAST(raw[k] + cur[k - output_bytes]);
5034 STBI__CASE(STBI__F_up)
5036 cur[k] = STBI__BYTECAST(raw[k] + prior[k]);
5039 STBI__CASE(STBI__F_avg)
5041 cur[k] = STBI__BYTECAST(
5042 raw[k] + ((prior[k] + cur[k - output_bytes]) >> 1));
5045 STBI__CASE(STBI__F_paeth)
5047 cur[k] = STBI__BYTECAST(
5048 raw[k] + stbi__paeth(cur[k - output_bytes], prior[k],
5049 prior[k - output_bytes]));
5052 STBI__CASE(STBI__F_avg_first)
5055 STBI__BYTECAST(raw[k] + (cur[k - output_bytes] >> 1));
5058 STBI__CASE(STBI__F_paeth_first)
5060 cur[k] = STBI__BYTECAST(
5061 raw[k] + stbi__paeth(cur[k - output_bytes], 0, 0));
5073 for (i = 0; i < x; ++i, cur += output_bytes) {
5074 cur[filter_bytes + 1] = 255;
5084 for (j = 0; j < y; ++j) {
5085 stbi_uc *cur = a->out + stride * j;
5086 stbi_uc *in = a->out + stride * j + x * out_n - img_width_bytes;
5092 ? stbi__depth_scale_table[depth]
5103 for (k = x * img_n; k >= 2; k -= 2, ++in) {
5104 *cur++ = scale * ((*in >> 4));
5105 *cur++ = scale * ((*in) & 0x0f);
5108 *cur++ = scale * ((*in >> 4));
5109 }
else if (depth == 2) {
5110 for (k = x * img_n; k >= 4; k -= 4, ++in) {
5111 *cur++ = scale * ((*in >> 6));
5112 *cur++ = scale * ((*in >> 4) & 0x03);
5113 *cur++ = scale * ((*in >> 2) & 0x03);
5114 *cur++ = scale * ((*in) & 0x03);
5117 *cur++ = scale * ((*in >> 6));
5119 *cur++ = scale * ((*in >> 4) & 0x03);
5121 *cur++ = scale * ((*in >> 2) & 0x03);
5122 }
else if (depth == 1) {
5123 for (k = x * img_n; k >= 8; k -= 8, ++in) {
5124 *cur++ = scale * ((*in >> 7));
5125 *cur++ = scale * ((*in >> 6) & 0x01);
5126 *cur++ = scale * ((*in >> 5) & 0x01);
5127 *cur++ = scale * ((*in >> 4) & 0x01);
5128 *cur++ = scale * ((*in >> 3) & 0x01);
5129 *cur++ = scale * ((*in >> 2) & 0x01);
5130 *cur++ = scale * ((*in >> 1) & 0x01);
5131 *cur++ = scale * ((*in) & 0x01);
5134 *cur++ = scale * ((*in >> 7));
5136 *cur++ = scale * ((*in >> 6) & 0x01);
5138 *cur++ = scale * ((*in >> 5) & 0x01);
5140 *cur++ = scale * ((*in >> 4) & 0x01);
5142 *cur++ = scale * ((*in >> 3) & 0x01);
5144 *cur++ = scale * ((*in >> 2) & 0x01);
5146 *cur++ = scale * ((*in >> 1) & 0x01);
5148 if (img_n != out_n) {
5151 cur = a->out + stride * j;
5153 for (q = x - 1; q >= 0; --q) {
5154 cur[q * 2 + 1] = 255;
5155 cur[q * 2 + 0] = cur[q];
5158 STBI_ASSERT(img_n == 3);
5159 for (q = x - 1; q >= 0; --q) {
5160 cur[q * 4 + 3] = 255;
5161 cur[q * 4 + 2] = cur[q * 3 + 2];
5162 cur[q * 4 + 1] = cur[q * 3 + 1];
5163 cur[q * 4 + 0] = cur[q * 3 + 0];
5168 }
else if (depth == 16) {
5174 stbi__uint16 *cur16 = (stbi__uint16 *)cur;
5176 for (i = 0; i < x * y * out_n; ++i, cur16++, cur += 2) {
5177 *cur16 = (cur[0] << 8) | cur[1];
5184 static int stbi__create_png_image(stbi__png *a,
stbi_uc *image_data,
5185 stbi__uint32 image_data_len,
int out_n,
5186 int depth,
int color,
int interlaced)
5188 int bytes = (depth == 16 ? 2 : 1);
5189 int out_bytes = out_n * bytes;
5193 return stbi__create_png_image_raw(a, image_data, image_data_len, out_n,
5194 a->s->img_x, a->s->img_y, depth,
5199 (
stbi_uc *)stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0);
5200 for (p = 0; p < 7; ++p) {
5201 int xorig[] = {0, 4, 0, 2, 0, 1, 0};
5202 int yorig[] = {0, 0, 4, 0, 2, 0, 1};
5203 int xspc[] = {8, 8, 4, 4, 2, 2, 1};
5204 int yspc[] = {8, 8, 8, 4, 4, 2, 2};
5207 x = (a->s->img_x - xorig[p] + xspc[p] - 1) / xspc[p];
5208 y = (a->s->img_y - yorig[p] + yspc[p] - 1) / yspc[p];
5210 stbi__uint32 img_len =
5211 ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;
5212 if (!stbi__create_png_image_raw(a, image_data, image_data_len,
5213 out_n, x, y, depth, color)) {
5217 for (j = 0; j < y; ++j) {
5218 for (i = 0; i < x; ++i) {
5219 int out_y = j * yspc[p] + yorig[p];
5220 int out_x = i * xspc[p] + xorig[p];
5221 memcpy(
final + out_y * a->s->img_x * out_bytes +
5223 a->out + (j * x + i) * out_bytes, out_bytes);
5227 image_data += img_len;
5228 image_data_len -= img_len;
5236 static int stbi__compute_transparency(stbi__png *z,
stbi_uc tc[3],
int out_n)
5238 stbi__context *s = z->s;
5239 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
5244 STBI_ASSERT(out_n == 2 || out_n == 4);
5247 for (i = 0; i < pixel_count; ++i) {
5248 p[1] = (p[0] == tc[0] ? 0 : 255);
5252 for (i = 0; i < pixel_count; ++i) {
5253 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
5261 static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3],
5264 stbi__context *s = z->s;
5265 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
5266 stbi__uint16 *p = (stbi__uint16 *)z->out;
5270 STBI_ASSERT(out_n == 2 || out_n == 4);
5273 for (i = 0; i < pixel_count; ++i) {
5274 p[1] = (p[0] == tc[0] ? 0 : 65535);
5278 for (i = 0; i < pixel_count; ++i) {
5279 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
5287 static int stbi__expand_png_palette(stbi__png *a,
stbi_uc *palette,
int len,
5290 stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
5291 stbi_uc *p, *temp_out, *orig = a->out;
5293 p = (
stbi_uc *)stbi__malloc_mad2(pixel_count, pal_img_n, 0);
5295 return stbi__err(
"outofmem",
"Out of memory");
5300 if (pal_img_n == 3) {
5301 for (i = 0; i < pixel_count; ++i) {
5302 int n = orig[i] * 4;
5304 p[1] = palette[n + 1];
5305 p[2] = palette[n + 2];
5309 for (i = 0; i < pixel_count; ++i) {
5310 int n = orig[i] * 4;
5312 p[1] = palette[n + 1];
5313 p[2] = palette[n + 2];
5314 p[3] = palette[n + 3];
5326 static int stbi__unpremultiply_on_load = 0;
5327 static int stbi__de_iphone_flag = 0;
5332 stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;
5337 stbi__de_iphone_flag = flag_true_if_should_convert;
5340 static void stbi__de_iphone(stbi__png *z)
5342 stbi__context *s = z->s;
5343 stbi__uint32 i, pixel_count = s->img_x * s->img_y;
5346 if (s->img_out_n == 3) {
5347 for (i = 0; i < pixel_count; ++i) {
5354 STBI_ASSERT(s->img_out_n == 4);
5355 if (stbi__unpremultiply_on_load) {
5357 for (i = 0; i < pixel_count; ++i) {
5362 p[0] = (p[2] * 255 + half) / a;
5363 p[1] = (p[1] * 255 + half) / a;
5364 p[2] = (t * 255 + half) / a;
5373 for (i = 0; i < pixel_count; ++i) {
5383 #define STBI__PNG_TYPE(a, b, c, d) \
5384 (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
5386 static int stbi__parse_png_file(stbi__png *z,
int scan,
int req_comp)
5388 stbi_uc palette[1024], pal_img_n = 0;
5390 stbi__uint16 tc16[3];
5391 stbi__uint32 ioff = 0, idata_limit = 0, i, pal_len = 0;
5392 int first = 1, k, interlace = 0, color = 0, is_iphone = 0;
5393 stbi__context *s = z->s;
5399 if (!stbi__check_png_header(s))
5402 if (scan == STBI__SCAN_type)
5406 stbi__pngchunk c = stbi__get_chunk_header(s);
5408 case STBI__PNG_TYPE(
'C',
'g',
'B',
'I'):
5410 stbi__skip(s, c.length);
5412 case STBI__PNG_TYPE(
'I',
'H',
'D',
'R'): {
5415 return stbi__err(
"multiple IHDR",
"Corrupt PNG");
5418 return stbi__err(
"bad IHDR len",
"Corrupt PNG");
5419 s->img_x = stbi__get32be(s);
5420 if (s->img_x > (1 << 24))
5421 return stbi__err(
"too large",
"Very large image (corrupt?)");
5422 s->img_y = stbi__get32be(s);
5423 if (s->img_y > (1 << 24))
5424 return stbi__err(
"too large",
"Very large image (corrupt?)");
5425 z->depth = stbi__get8(s);
5426 if (z->depth != 1 && z->depth != 2 && z->depth != 4 &&
5427 z->depth != 8 && z->depth != 16)
5428 return stbi__err(
"1/2/4/8/16-bit only",
5429 "PNG not supported: 1/2/4/8/16-bit only");
5430 color = stbi__get8(s);
5432 return stbi__err(
"bad ctype",
"Corrupt PNG");
5433 if (color == 3 && z->depth == 16)
5434 return stbi__err(
"bad ctype",
"Corrupt PNG");
5438 return stbi__err(
"bad ctype",
"Corrupt PNG");
5439 comp = stbi__get8(s);
5441 return stbi__err(
"bad comp method",
"Corrupt PNG");
5442 filter = stbi__get8(s);
5444 return stbi__err(
"bad filter method",
"Corrupt PNG");
5445 interlace = stbi__get8(s);
5447 return stbi__err(
"bad interlace method",
"Corrupt PNG");
5448 if (!s->img_x || !s->img_y)
5449 return stbi__err(
"0-pixel image",
"Corrupt PNG");
5451 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
5452 if ((1 << 30) / s->img_x / s->img_n < s->img_y)
5453 return stbi__err(
"too large",
"Image too large to decode");
5454 if (scan == STBI__SCAN_header)
5460 if ((1 << 30) / s->img_x / 4 < s->img_y)
5461 return stbi__err(
"too large",
"Corrupt PNG");
5467 case STBI__PNG_TYPE(
'P',
'L',
'T',
'E'): {
5469 return stbi__err(
"first not IHDR",
"Corrupt PNG");
5470 if (c.length > 256 * 3)
5471 return stbi__err(
"invalid PLTE",
"Corrupt PNG");
5472 pal_len = c.length / 3;
5473 if (pal_len * 3 != c.length)
5474 return stbi__err(
"invalid PLTE",
"Corrupt PNG");
5475 for (i = 0; i < pal_len; ++i) {
5476 palette[i * 4 + 0] = stbi__get8(s);
5477 palette[i * 4 + 1] = stbi__get8(s);
5478 palette[i * 4 + 2] = stbi__get8(s);
5479 palette[i * 4 + 3] = 255;
5484 case STBI__PNG_TYPE(
't',
'R',
'N',
'S'): {
5486 return stbi__err(
"first not IHDR",
"Corrupt PNG");
5488 return stbi__err(
"tRNS after IDAT",
"Corrupt PNG");
5490 if (scan == STBI__SCAN_header) {
5495 return stbi__err(
"tRNS before PLTE",
"Corrupt PNG");
5496 if (c.length > pal_len)
5497 return stbi__err(
"bad tRNS len",
"Corrupt PNG");
5499 for (i = 0; i < c.length; ++i)
5500 palette[i * 4 + 3] = stbi__get8(s);
5502 if (!(s->img_n & 1))
5503 return stbi__err(
"tRNS with alpha",
"Corrupt PNG");
5504 if (c.length != (stbi__uint32)s->img_n * 2)
5505 return stbi__err(
"bad tRNS len",
"Corrupt PNG");
5507 if (z->depth == 16) {
5508 for (k = 0; k < s->img_n; ++k)
5509 tc16[k] = (stbi__uint16)stbi__get16be(
5512 for (k = 0; k < s->img_n; ++k)
5513 tc[k] = (
stbi_uc)(stbi__get16be(s) & 255) *
5514 stbi__depth_scale_table[z->depth];
5523 case STBI__PNG_TYPE(
'I',
'D',
'A',
'T'): {
5525 return stbi__err(
"first not IHDR",
"Corrupt PNG");
5526 if (pal_img_n && !pal_len)
5527 return stbi__err(
"no PLTE",
"Corrupt PNG");
5528 if (scan == STBI__SCAN_header) {
5529 s->img_n = pal_img_n;
5532 if ((
int)(ioff + c.length) < (
int)ioff)
5534 if (ioff + c.length > idata_limit) {
5535 stbi__uint32 idata_limit_old = idata_limit;
5537 if (idata_limit == 0)
5538 idata_limit = c.length > 4096 ? c.length : 4096;
5539 while (ioff + c.length > idata_limit)
5541 STBI_NOTUSED(idata_limit_old);
5542 p = (
stbi_uc *)STBI_REALLOC_SIZED(z->idata, idata_limit_old,
5545 return stbi__err(
"outofmem",
"Out of memory");
5548 if (!stbi__getn(s, z->idata + ioff, c.length))
5549 return stbi__err(
"outofdata",
"Corrupt PNG");
5554 case STBI__PNG_TYPE(
'I',
'E',
'N',
'D'): {
5555 stbi__uint32 raw_len, bpl;
5557 return stbi__err(
"first not IHDR",
"Corrupt PNG");
5558 if (scan != STBI__SCAN_load)
5560 if (z->idata == NULL)
5561 return stbi__err(
"no IDAT",
"Corrupt PNG");
5564 (s->img_x * z->depth + 7) / 8;
5565 raw_len = bpl * s->img_y * s->img_n +
5569 (
char *)z->idata, ioff, raw_len, (
int *)&raw_len,
5571 if (z->expanded == NULL)
5573 STBI_FREE(z->idata);
5575 if ((req_comp == s->img_n + 1 && req_comp != 3 && !pal_img_n) ||
5577 s->img_out_n = s->img_n + 1;
5579 s->img_out_n = s->img_n;
5580 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n,
5581 z->depth, color, interlace))
5584 if (z->depth == 16) {
5585 if (!stbi__compute_transparency16(z, tc16, s->img_out_n))
5588 if (!stbi__compute_transparency(z, tc, s->img_out_n))
5592 if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
5596 s->img_n = pal_img_n;
5597 s->img_out_n = pal_img_n;
5599 s->img_out_n = req_comp;
5600 if (!stbi__expand_png_palette(z, palette, pal_len,
5603 }
else if (has_trans) {
5608 STBI_FREE(z->expanded);
5616 return stbi__err(
"first not IHDR",
"Corrupt PNG");
5617 if ((c.type & (1 << 29)) == 0) {
5618 #ifndef STBI_NO_FAILURE_STRINGS
5620 static char invalid_chunk[] =
"XXXX PNG chunk not known";
5621 invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
5622 invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
5623 invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
5624 invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
5626 return stbi__err(invalid_chunk,
5627 "PNG not supported: unknown PNG chunk type");
5629 stbi__skip(s, c.length);
5637 static void *stbi__do_png(stbi__png *p,
int *x,
int *y,
int *n,
int req_comp,
5638 stbi__result_info *ri)
5640 void *result = NULL;
5641 if (req_comp < 0 || req_comp > 4)
5642 return stbi__errpuc(
"bad req_comp",
"Internal error");
5643 if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
5645 ri->bits_per_channel = 8;
5647 ri->bits_per_channel = p->depth;
5650 if (req_comp && req_comp != p->s->img_out_n) {
5651 if (ri->bits_per_channel == 8)
5652 result = stbi__convert_format((
unsigned char *)result,
5653 p->s->img_out_n, req_comp,
5654 p->s->img_x, p->s->img_y);
5656 result = stbi__convert_format16((stbi__uint16 *)result,
5657 p->s->img_out_n, req_comp,
5658 p->s->img_x, p->s->img_y);
5659 p->s->img_out_n = req_comp;
5670 STBI_FREE(p->expanded);
5672 STBI_FREE(p->idata);
5678 static void *stbi__png_load(stbi__context *s,
int *x,
int *y,
int *comp,
5679 int req_comp, stbi__result_info *ri)
5683 return stbi__do_png(&p, x, y, comp, req_comp, ri);
5686 static int stbi__png_test(stbi__context *s)
5689 r = stbi__check_png_header(s);
5694 static int stbi__png_info_raw(stbi__png *p,
int *x,
int *y,
int *comp)
5696 if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) {
5705 *comp = p->s->img_n;
5709 static int stbi__png_info(stbi__context *s,
int *x,
int *y,
int *comp)
5713 return stbi__png_info_raw(&p, x, y, comp);
5720 static int stbi__bmp_test_raw(stbi__context *s)
5724 if (stbi__get8(s) !=
'B')
5726 if (stbi__get8(s) !=
'M')
5732 sz = stbi__get32le(s);
5733 r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
5737 static int stbi__bmp_test(stbi__context *s)
5739 int r = stbi__bmp_test_raw(s);
5745 static int stbi__high_bit(
unsigned int z)
5763 static int stbi__bitcount(
unsigned int a)
5765 a = (a & 0x55555555) + ((a >> 1) & 0x55555555);
5766 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
5767 a = (a + (a >> 4)) & 0x0f0f0f0f;
5769 a = (a + (a >> 16));
5773 static int stbi__shiftsigned(
int v,
int shift,
int bits)
5793 int bpp, offset, hsz;
5794 unsigned int mr, mg, mb, ma, all_a;
5797 static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5800 if (stbi__get8(s) !=
'B' || stbi__get8(s) !=
'M')
5801 return stbi__errpuc(
"not BMP",
"Corrupt BMP");
5805 info->offset = stbi__get32le(s);
5806 info->hsz = hsz = stbi__get32le(s);
5807 info->mr = info->mg = info->mb = info->ma = 0;
5809 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124)
5810 return stbi__errpuc(
"unknown BMP",
"BMP type not supported: unknown");
5812 s->img_x = stbi__get16le(s);
5813 s->img_y = stbi__get16le(s);
5815 s->img_x = stbi__get32le(s);
5816 s->img_y = stbi__get32le(s);
5818 if (stbi__get16le(s) != 1)
5819 return stbi__errpuc(
"bad BMP",
"bad BMP");
5820 info->bpp = stbi__get16le(s);
5822 return stbi__errpuc(
"monochrome",
"BMP type not supported: 1-bit");
5824 int compress = stbi__get32le(s);
5825 if (compress == 1 || compress == 2)
5826 return stbi__errpuc(
"BMP RLE",
"BMP type not supported: RLE");
5832 if (hsz == 40 || hsz == 56) {
5839 if (info->bpp == 16 || info->bpp == 32) {
5840 if (compress == 0) {
5841 if (info->bpp == 32) {
5842 info->mr = 0xffu << 16;
5843 info->mg = 0xffu << 8;
5844 info->mb = 0xffu << 0;
5845 info->ma = 0xffu << 24;
5849 info->mr = 31u << 10;
5850 info->mg = 31u << 5;
5851 info->mb = 31u << 0;
5853 }
else if (compress == 3) {
5854 info->mr = stbi__get32le(s);
5855 info->mg = stbi__get32le(s);
5856 info->mb = stbi__get32le(s);
5859 if (info->mr == info->mg && info->mg == info->mb) {
5861 return stbi__errpuc(
"bad BMP",
"bad BMP");
5864 return stbi__errpuc(
"bad BMP",
"bad BMP");
5868 if (hsz != 108 && hsz != 124)
5869 return stbi__errpuc(
"bad BMP",
"bad BMP");
5870 info->mr = stbi__get32le(s);
5871 info->mg = stbi__get32le(s);
5872 info->mb = stbi__get32le(s);
5873 info->ma = stbi__get32le(s);
5875 for (i = 0; i < 12; ++i)
5888 static void *stbi__bmp_load(stbi__context *s,
int *x,
int *y,
int *comp,
5889 int req_comp, stbi__result_info *ri)
5892 unsigned int mr = 0, mg = 0, mb = 0, ma = 0, all_a;
5894 int psize = 0, i, j, width;
5895 int flip_vertically, pad, target;
5896 stbi__bmp_data info;
5900 if (stbi__bmp_parse_header(s, &info) == NULL)
5903 flip_vertically = ((int)s->img_y) > 0;
5904 s->img_y = abs((
int)s->img_y);
5912 if (info.hsz == 12) {
5914 psize = (info.offset - 14 - 24) / 3;
5917 psize = (info.offset - 14 - info.hsz) >> 2;
5920 s->img_n = ma ? 4 : 3;
5921 if (req_comp && req_comp >= 3)
5927 if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0))
5928 return stbi__errpuc(
"too large",
"Corrupt BMP");
5930 out = (
stbi_uc *)stbi__malloc_mad3(target, s->img_x, s->img_y, 0);
5932 return stbi__errpuc(
"outofmem",
"Out of memory");
5933 if (info.bpp < 16) {
5935 if (psize == 0 || psize > 256) {
5937 return stbi__errpuc(
"invalid",
"Corrupt BMP");
5939 for (i = 0; i < psize; ++i) {
5940 pal[i][2] = stbi__get8(s);
5941 pal[i][1] = stbi__get8(s);
5942 pal[i][0] = stbi__get8(s);
5947 stbi__skip(s, info.offset - 14 - info.hsz -
5948 psize * (info.hsz == 12 ? 3 : 4));
5950 width = (s->img_x + 1) >> 1;
5951 else if (info.bpp == 8)
5955 return stbi__errpuc(
"bad bpp",
"Corrupt BMP");
5958 for (j = 0; j < (int)s->img_y; ++j) {
5959 for (i = 0; i < (int)s->img_x; i += 2) {
5960 int v = stbi__get8(s), v2 = 0;
5961 if (info.bpp == 4) {
5965 out[z++] = pal[v][0];
5966 out[z++] = pal[v][1];
5967 out[z++] = pal[v][2];
5970 if (i + 1 == (
int)s->img_x)
5972 v = (info.bpp == 8) ? stbi__get8(s) : v2;
5973 out[z++] = pal[v][0];
5974 out[z++] = pal[v][1];
5975 out[z++] = pal[v][2];
5982 int rshift = 0, gshift = 0, bshift = 0, ashift = 0, rcount = 0,
5983 gcount = 0, bcount = 0, acount = 0;
5986 stbi__skip(s, info.offset - 14 - info.hsz);
5988 width = 3 * s->img_x;
5989 else if (info.bpp == 16)
5990 width = 2 * s->img_x;
5994 if (info.bpp == 24) {
5996 }
else if (info.bpp == 32) {
5997 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 &&
6002 if (!mr || !mg || !mb) {
6004 return stbi__errpuc(
"bad masks",
"Corrupt BMP");
6007 rshift = stbi__high_bit(mr) - 7;
6008 rcount = stbi__bitcount(mr);
6009 gshift = stbi__high_bit(mg) - 7;
6010 gcount = stbi__bitcount(mg);
6011 bshift = stbi__high_bit(mb) - 7;
6012 bcount = stbi__bitcount(mb);
6013 ashift = stbi__high_bit(ma) - 7;
6014 acount = stbi__bitcount(ma);
6016 for (j = 0; j < (int)s->img_y; ++j) {
6018 for (i = 0; i < (int)s->img_x; ++i) {
6020 out[z + 2] = stbi__get8(s);
6021 out[z + 1] = stbi__get8(s);
6022 out[z + 0] = stbi__get8(s);
6024 a = (easy == 2 ? stbi__get8(s) : 255);
6031 for (i = 0; i < (int)s->img_x; ++i) {
6032 stbi__uint32 v = (bpp == 16 ? (stbi__uint32)stbi__get16le(s)
6033 : stbi__get32le(s));
6035 out[z++] = STBI__BYTECAST(
6036 stbi__shiftsigned(v & mr, rshift, rcount));
6037 out[z++] = STBI__BYTECAST(
6038 stbi__shiftsigned(v & mg, gshift, gcount));
6039 out[z++] = STBI__BYTECAST(
6040 stbi__shiftsigned(v & mb, bshift, bcount));
6041 a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
6044 out[z++] = STBI__BYTECAST(a);
6052 if (target == 4 && all_a == 0)
6053 for (i = 4 * s->img_x * s->img_y - 1; i >= 0; i -= 4)
6056 if (flip_vertically) {
6058 for (j = 0; j<(int)s->img_y>> 1; ++j) {
6059 stbi_uc *p1 = out + j * s->img_x * target;
6060 stbi_uc *p2 = out + (s->img_y - 1 - j) * s->img_x * target;
6061 for (i = 0; i < (int)s->img_x * target; ++i) {
6062 t = p1[i], p1[i] = p2[i], p2[i] = t;
6067 if (req_comp && req_comp != target) {
6068 out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
6085 static int stbi__tga_get_comp(
int bits_per_pixel,
int is_grey,
int *is_rgb16)
6090 switch (bits_per_pixel) {
6103 return bits_per_pixel / 8;
6109 static int stbi__tga_info(stbi__context *s,
int *x,
int *y,
int *comp)
6111 int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel,
6113 int sz, tga_colormap_type;
6115 tga_colormap_type = stbi__get8(s);
6116 if (tga_colormap_type > 1) {
6120 tga_image_type = stbi__get8(s);
6121 if (tga_colormap_type == 1) {
6122 if (tga_image_type != 1 && tga_image_type != 9) {
6129 if ((sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32)) {
6134 tga_colormap_bpp = sz;
6136 if ((tga_image_type != 2) && (tga_image_type != 3) &&
6137 (tga_image_type != 10) && (tga_image_type != 11)) {
6142 tga_colormap_bpp = 0;
6144 tga_w = stbi__get16le(s);
6149 tga_h = stbi__get16le(s);
6154 tga_bits_per_pixel = stbi__get8(s);
6156 if (tga_colormap_bpp != 0) {
6157 if ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) {
6163 tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
6165 tga_comp = stbi__tga_get_comp(
6166 tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11),
6182 static int stbi__tga_test(stbi__context *s)
6185 int sz, tga_color_type;
6187 tga_color_type = stbi__get8(s);
6188 if (tga_color_type > 1)
6191 if (tga_color_type == 1) {
6192 if (sz != 1 && sz != 9)
6197 if ((sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32))
6201 if ((sz != 2) && (sz != 3) && (sz != 10) && (sz != 11))
6205 if (stbi__get16le(s) < 1)
6207 if (stbi__get16le(s) < 1)
6210 if ((tga_color_type == 1) && (sz != 8) && (sz != 16))
6212 if ((sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32))
6224 static void stbi__tga_read_rgb16(stbi__context *s,
stbi_uc *out)
6226 stbi__uint16 px = (stbi__uint16)stbi__get16le(s);
6227 stbi__uint16 fiveBitMask = 31;
6229 int r = (px >> 10) & fiveBitMask;
6230 int g = (px >> 5) & fiveBitMask;
6231 int b = px & fiveBitMask;
6234 out[0] = (
stbi_uc)((r * 255) / 31);
6235 out[1] = (
stbi_uc)((g * 255) / 31);
6236 out[2] = (
stbi_uc)((b * 255) / 31);
6244 static void *stbi__tga_load(stbi__context *s,
int *x,
int *y,
int *comp,
6245 int req_comp, stbi__result_info *ri)
6248 int tga_offset = stbi__get8(s);
6249 int tga_indexed = stbi__get8(s);
6250 int tga_image_type = stbi__get8(s);
6252 int tga_palette_start = stbi__get16le(s);
6253 int tga_palette_len = stbi__get16le(s);
6254 int tga_palette_bits = stbi__get8(s);
6255 int tga_x_origin = stbi__get16le(s);
6256 int tga_y_origin = stbi__get16le(s);
6257 int tga_width = stbi__get16le(s);
6258 int tga_height = stbi__get16le(s);
6259 int tga_bits_per_pixel = stbi__get8(s);
6260 int tga_comp, tga_rgb16 = 0;
6261 int tga_inverted = stbi__get8(s);
6265 unsigned char *tga_data;
6266 unsigned char *tga_palette = NULL;
6268 unsigned char raw_data[4] = {0};
6270 int RLE_repeating = 0;
6271 int read_next_pixel = 1;
6275 if (tga_image_type >= 8) {
6276 tga_image_type -= 8;
6279 tga_inverted = 1 - ((tga_inverted >> 5) & 1);
6283 tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
6285 tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3),
6290 return stbi__errpuc(
"bad format",
"Can't find out TGA pixelformat");
6298 if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0))
6299 return stbi__errpuc(
"too large",
"Corrupt TGA");
6302 (
unsigned char *)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0);
6304 return stbi__errpuc(
"outofmem",
"Out of memory");
6307 stbi__skip(s, tga_offset);
6309 if (!tga_indexed && !tga_is_RLE && !tga_rgb16) {
6310 for (i = 0; i < tga_height; ++i) {
6311 int row = tga_inverted ? tga_height - i - 1 : i;
6312 stbi_uc *tga_row = tga_data + row * tga_width * tga_comp;
6313 stbi__getn(s, tga_row, tga_width * tga_comp);
6319 stbi__skip(s, tga_palette_start);
6321 tga_palette = (
unsigned char *)stbi__malloc_mad2(tga_palette_len,
6324 STBI_FREE(tga_data);
6325 return stbi__errpuc(
"outofmem",
"Out of memory");
6328 stbi_uc *pal_entry = tga_palette;
6330 for (i = 0; i < tga_palette_len; ++i) {
6331 stbi__tga_read_rgb16(s, pal_entry);
6332 pal_entry += tga_comp;
6334 }
else if (!stbi__getn(s, tga_palette,
6335 tga_palette_len * tga_comp)) {
6336 STBI_FREE(tga_data);
6337 STBI_FREE(tga_palette);
6338 return stbi__errpuc(
"bad palette",
"Corrupt TGA");
6342 for (i = 0; i < tga_width * tga_height; ++i) {
6345 if (RLE_count == 0) {
6347 int RLE_cmd = stbi__get8(s);
6348 RLE_count = 1 + (RLE_cmd & 127);
6349 RLE_repeating = RLE_cmd >> 7;
6350 read_next_pixel = 1;
6351 }
else if (!RLE_repeating) {
6352 read_next_pixel = 1;
6355 read_next_pixel = 1;
6358 if (read_next_pixel) {
6362 int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s)
6364 if (pal_idx >= tga_palette_len) {
6368 pal_idx *= tga_comp;
6369 for (j = 0; j < tga_comp; ++j) {
6370 raw_data[j] = tga_palette[pal_idx + j];
6372 }
else if (tga_rgb16) {
6374 stbi__tga_read_rgb16(s, raw_data);
6377 for (j = 0; j < tga_comp; ++j) {
6378 raw_data[j] = stbi__get8(s);
6382 read_next_pixel = 0;
6386 for (j = 0; j < tga_comp; ++j)
6387 tga_data[i * tga_comp + j] = raw_data[j];
6394 for (j = 0; j * 2 < tga_height; ++j) {
6395 int index1 = j * tga_width * tga_comp;
6396 int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
6397 for (i = tga_width * tga_comp; i > 0; --i) {
6398 unsigned char temp = tga_data[index1];
6399 tga_data[index1] = tga_data[index2];
6400 tga_data[index2] = temp;
6407 if (tga_palette != NULL) {
6408 STBI_FREE(tga_palette);
6413 if (tga_comp >= 3 && !tga_rgb16) {
6414 unsigned char *tga_pixel = tga_data;
6415 for (i = 0; i < tga_width * tga_height; ++i) {
6416 unsigned char temp = tga_pixel[0];
6417 tga_pixel[0] = tga_pixel[2];
6418 tga_pixel[2] = temp;
6419 tga_pixel += tga_comp;
6424 if (req_comp && req_comp != tga_comp)
6425 tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width,
6430 tga_palette_start = tga_palette_len = tga_palette_bits = tga_x_origin =
6442 static int stbi__psd_test(stbi__context *s)
6444 int r = (stbi__get32be(s) == 0x38425053);
6449 static int stbi__psd_decode_rle(stbi__context *s,
stbi_uc *p,
int pixelCount)
6451 int count, nleft, len;
6454 while ((nleft = pixelCount - count) > 0) {
6455 len = stbi__get8(s);
6458 }
else if (len < 128) {
6469 }
else if (len > 128) {
6476 val = stbi__get8(s);
6489 static void *stbi__psd_load(stbi__context *s,
int *x,
int *y,
int *comp,
6490 int req_comp, stbi__result_info *ri,
int bpc)
6493 int channelCount, compression;
6501 if (stbi__get32be(s) != 0x38425053)
6502 return stbi__errpuc(
"not PSD",
"Corrupt PSD image");
6505 if (stbi__get16be(s) != 1)
6506 return stbi__errpuc(
"wrong version",
6507 "Unsupported version of PSD image");
6513 channelCount = stbi__get16be(s);
6514 if (channelCount < 0 || channelCount > 16)
6515 return stbi__errpuc(
"wrong channel count",
6516 "Unsupported number of channels in PSD image");
6519 h = stbi__get32be(s);
6520 w = stbi__get32be(s);
6523 bitdepth = stbi__get16be(s);
6524 if (bitdepth != 8 && bitdepth != 16)
6525 return stbi__errpuc(
"unsupported bit depth",
6526 "PSD bit depth is not 8 or 16 bit");
6538 if (stbi__get16be(s) != 3)
6539 return stbi__errpuc(
"wrong color format",
6540 "PSD is not in RGB color format");
6544 stbi__skip(s, stbi__get32be(s));
6547 stbi__skip(s, stbi__get32be(s));
6550 stbi__skip(s, stbi__get32be(s));
6556 compression = stbi__get16be(s);
6557 if (compression > 1)
6558 return stbi__errpuc(
"bad compression",
6559 "PSD has an unknown compression format");
6562 if (!stbi__mad3sizes_valid(4, w, h, 0))
6563 return stbi__errpuc(
"too large",
"Corrupt PSD");
6567 if (!compression && bitdepth == 16 && bpc == 16) {
6568 out = (
stbi_uc *)stbi__malloc_mad3(8, w, h, 0);
6569 ri->bits_per_channel = 16;
6571 out = (
stbi_uc *)stbi__malloc(4 * w * h);
6574 return stbi__errpuc(
"outofmem",
"Out of memory");
6592 stbi__skip(s, h * channelCount * 2);
6595 for (channel = 0; channel < 4; channel++) {
6599 if (channel >= channelCount) {
6601 for (i = 0; i < pixelCount; i++, p += 4)
6602 *p = (channel == 3 ? 255 : 0);
6605 if (!stbi__psd_decode_rle(s, p, pixelCount)) {
6607 return stbi__errpuc(
"corrupt",
"bad RLE data");
6618 for (channel = 0; channel < 4; channel++) {
6619 if (channel >= channelCount) {
6621 if (bitdepth == 16 && bpc == 16) {
6622 stbi__uint16 *q = ((stbi__uint16 *)out) + channel;
6623 stbi__uint16 val = channel == 3 ? 65535 : 0;
6624 for (i = 0; i < pixelCount; i++, q += 4)
6628 stbi_uc val = channel == 3 ? 255 : 0;
6629 for (i = 0; i < pixelCount; i++, p += 4)
6633 if (ri->bits_per_channel == 16) {
6634 stbi__uint16 *q = ((stbi__uint16 *)out) + channel;
6635 for (i = 0; i < pixelCount; i++, q += 4)
6636 *q = (stbi__uint16)stbi__get16be(s);
6639 if (bitdepth == 16) {
6640 for (i = 0; i < pixelCount; i++, p += 4)
6641 *p = (
stbi_uc)(stbi__get16be(s) >> 8);
6643 for (i = 0; i < pixelCount; i++, p += 4)
6652 if (channelCount >= 4) {
6653 if (ri->bits_per_channel == 16) {
6654 for (i = 0; i < w * h; ++i) {
6655 stbi__uint16 *pixel = (stbi__uint16 *)out + 4 * i;
6656 if (pixel[3] != 0 && pixel[3] != 65535) {
6657 float a = pixel[3] / 65535.0f;
6658 float ra = 1.0f / a;
6659 float inv_a = 65535.0f * (1 - ra);
6660 pixel[0] = (stbi__uint16)(pixel[0] * ra + inv_a);
6661 pixel[1] = (stbi__uint16)(pixel[1] * ra + inv_a);
6662 pixel[2] = (stbi__uint16)(pixel[2] * ra + inv_a);
6666 for (i = 0; i < w * h; ++i) {
6667 unsigned char *pixel = out + 4 * i;
6668 if (pixel[3] != 0 && pixel[3] != 255) {
6669 float a = pixel[3] / 255.0f;
6670 float ra = 1.0f / a;
6671 float inv_a = 255.0f * (1 - ra);
6672 pixel[0] = (
unsigned char)(pixel[0] * ra + inv_a);
6673 pixel[1] = (
unsigned char)(pixel[1] * ra + inv_a);
6674 pixel[2] = (
unsigned char)(pixel[2] * ra + inv_a);
6681 if (req_comp && req_comp != 4) {
6682 if (ri->bits_per_channel == 16)
6683 out = (
stbi_uc *)stbi__convert_format16((stbi__uint16 *)out, 4,
6686 out = stbi__convert_format(out, 4, req_comp, w, h);
6708 static int stbi__pic_is4(stbi__context *s,
const char *str)
6711 for (i = 0; i < 4; ++i)
6712 if (stbi__get8(s) != (
stbi_uc)str[i])
6718 static int stbi__pic_test_core(stbi__context *s)
6722 if (!stbi__pic_is4(s,
"\x53\x80\xF6\x34"))
6725 for (i = 0; i < 84; ++i)
6728 if (!stbi__pic_is4(s,
"PICT"))
6738 static stbi_uc *stbi__readval(stbi__context *s,
int channel,
stbi_uc *dest)
6742 for (i = 0; i < 4; ++i, mask >>= 1) {
6743 if (channel & mask) {
6744 if (stbi__at_eof(s))
6745 return stbi__errpuc(
"bad file",
"PIC file too short");
6746 dest[i] = stbi__get8(s);
6753 static void stbi__copyval(
int channel,
stbi_uc *dest,
const stbi_uc *src)
6757 for (i = 0; i < 4; ++i, mask >>= 1)
6762 static stbi_uc *stbi__pic_load_core(stbi__context *s,
int width,
int height,
6765 int act_comp = 0, num_packets = 0, y, chained;
6766 stbi__pic_packet packets[10];
6771 stbi__pic_packet *packet;
6773 if (num_packets ==
sizeof(packets) /
sizeof(packets[0]))
6774 return stbi__errpuc(
"bad format",
"too many packets");
6776 packet = &packets[num_packets++];
6778 chained = stbi__get8(s);
6779 packet->size = stbi__get8(s);
6780 packet->type = stbi__get8(s);
6781 packet->channel = stbi__get8(s);
6783 act_comp |= packet->channel;
6785 if (stbi__at_eof(s))
6786 return stbi__errpuc(
"bad file",
"file too short (reading packets)");
6787 if (packet->size != 8)
6788 return stbi__errpuc(
"bad format",
"packet isn't 8bpp");
6791 *comp = (act_comp & 0x10 ? 4 : 3);
6793 for (y = 0; y < height; ++y) {
6796 for (packet_idx = 0; packet_idx < num_packets; ++packet_idx) {
6797 stbi__pic_packet *packet = &packets[packet_idx];
6798 stbi_uc *dest = result + y * width * 4;
6800 switch (packet->type) {
6802 return stbi__errpuc(
"bad format",
6803 "packet has bad compression type");
6808 for (x = 0; x < width; ++x, dest += 4)
6809 if (!stbi__readval(s, packet->channel, dest))
6816 int left = width, i;
6821 count = stbi__get8(s);
6822 if (stbi__at_eof(s))
6823 return stbi__errpuc(
"bad file",
6824 "file too short (pure read count)");
6829 if (!stbi__readval(s, packet->channel, value))
6832 for (i = 0; i < count; ++i, dest += 4)
6833 stbi__copyval(packet->channel, dest, value);
6841 int count = stbi__get8(s), i;
6842 if (stbi__at_eof(s))
6843 return stbi__errpuc(
6844 "bad file",
"file too short (mixed read count)");
6850 count = stbi__get16be(s);
6854 return stbi__errpuc(
"bad file",
"scanline overrun");
6856 if (!stbi__readval(s, packet->channel, value))
6859 for (i = 0; i < count; ++i, dest += 4)
6860 stbi__copyval(packet->channel, dest, value);
6864 return stbi__errpuc(
"bad file",
"scanline overrun");
6866 for (i = 0; i < count; ++i, dest += 4)
6867 if (!stbi__readval(s, packet->channel, dest))
6881 static void *stbi__pic_load(stbi__context *s,
int *px,
int *py,
int *comp,
6882 int req_comp, stbi__result_info *ri)
6885 int i, x, y, internal_comp;
6889 comp = &internal_comp;
6891 for (i = 0; i < 92; ++i)
6894 x = stbi__get16be(s);
6895 y = stbi__get16be(s);
6896 if (stbi__at_eof(s))
6897 return stbi__errpuc(
"bad file",
"file too short (pic header)");
6898 if (!stbi__mad3sizes_valid(x, y, 4, 0))
6899 return stbi__errpuc(
"too large",
"PIC image too large to decode");
6906 result = (
stbi_uc *)stbi__malloc_mad3(x, y, 4, 0);
6907 memset(result, 0xff, x * y * 4);
6909 if (!stbi__pic_load_core(s, x, y, comp, result)) {
6917 result = stbi__convert_format(result, 4, req_comp, x, y);
6922 static int stbi__pic_test(stbi__context *s)
6924 int r = stbi__pic_test_core(s);
6944 int flags, bgindex, ratio, transparent, eflags, delay;
6947 stbi__gif_lzw codes[4096];
6951 int start_x, start_y;
6957 static int stbi__gif_test_raw(stbi__context *s)
6960 if (stbi__get8(s) !=
'G' || stbi__get8(s) !=
'I' || stbi__get8(s) !=
'F' ||
6961 stbi__get8(s) !=
'8')
6964 if (sz !=
'9' && sz !=
'7')
6966 if (stbi__get8(s) !=
'a')
6971 static int stbi__gif_test(stbi__context *s)
6973 int r = stbi__gif_test_raw(s);
6978 static void stbi__gif_parse_colortable(stbi__context *s,
stbi_uc pal[256][4],
6979 int num_entries,
int transp)
6982 for (i = 0; i < num_entries; ++i) {
6983 pal[i][2] = stbi__get8(s);
6984 pal[i][1] = stbi__get8(s);
6985 pal[i][0] = stbi__get8(s);
6986 pal[i][3] = transp == i ? 0 : 255;
6990 static int stbi__gif_header(stbi__context *s, stbi__gif *g,
int *comp,
6994 if (stbi__get8(s) !=
'G' || stbi__get8(s) !=
'I' || stbi__get8(s) !=
'F' ||
6995 stbi__get8(s) !=
'8')
6996 return stbi__err(
"not GIF",
"Corrupt GIF");
6998 version = stbi__get8(s);
6999 if (version !=
'7' && version !=
'9')
7000 return stbi__err(
"not GIF",
"Corrupt GIF");
7001 if (stbi__get8(s) !=
'a')
7002 return stbi__err(
"not GIF",
"Corrupt GIF");
7004 stbi__g_failure_reason =
"";
7005 g->w = stbi__get16le(s);
7006 g->h = stbi__get16le(s);
7007 g->flags = stbi__get8(s);
7008 g->bgindex = stbi__get8(s);
7009 g->ratio = stbi__get8(s);
7010 g->transparent = -1;
7019 if (g->flags & 0x80)
7020 stbi__gif_parse_colortable(s, g->pal, 2 << (g->flags & 7), -1);
7025 static int stbi__gif_info_raw(stbi__context *s,
int *x,
int *y,
int *comp)
7027 stbi__gif *g = (stbi__gif *)stbi__malloc(
sizeof(stbi__gif));
7028 if (!stbi__gif_header(s, g, comp, 1)) {
7041 static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
7047 if (g->codes[code].prefix >= 0)
7048 stbi__out_gif_code(g, g->codes[code].prefix);
7050 if (g->cur_y >= g->max_y)
7053 p = &g->out[g->cur_x + g->cur_y];
7054 c = &g->color_table[g->codes[code].suffix * 4];
7064 if (g->cur_x >= g->max_x) {
7065 g->cur_x = g->start_x;
7066 g->cur_y += g->step;
7068 while (g->cur_y >= g->max_y && g->parse > 0) {
7069 g->step = (1 << g->parse) * g->line_size;
7070 g->cur_y = g->start_y + (g->step >> 1);
7076 static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
7079 stbi__int32 len, init_code;
7081 stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
7084 lzw_cs = stbi__get8(s);
7087 clear = 1 << lzw_cs;
7089 codesize = lzw_cs + 1;
7090 codemask = (1 << codesize) - 1;
7093 for (init_code = 0; init_code < clear; init_code++) {
7094 g->codes[init_code].prefix = -1;
7095 g->codes[init_code].first = (
stbi_uc)init_code;
7096 g->codes[init_code].suffix = (
stbi_uc)init_code;
7105 if (valid_bits < codesize) {
7107 len = stbi__get8(s);
7112 bits |= (stbi__int32)stbi__get8(s) << valid_bits;
7115 stbi__int32 code = bits & codemask;
7117 valid_bits -= codesize;
7120 if (code == clear) {
7121 codesize = lzw_cs + 1;
7122 codemask = (1 << codesize) - 1;
7126 }
else if (code == clear + 1) {
7128 while ((len = stbi__get8(s)) > 0)
7131 }
else if (code <= avail) {
7133 return stbi__errpuc(
"no clear code",
"Corrupt GIF");
7136 p = &g->codes[avail++];
7138 return stbi__errpuc(
"too many codes",
"Corrupt GIF");
7139 p->prefix = (stbi__int16)oldcode;
7140 p->first = g->codes[oldcode].first;
7142 (code == avail) ? p->first : g->codes[code].first;
7143 }
else if (code == avail)
7144 return stbi__errpuc(
"illegal code in raster",
7147 stbi__out_gif_code(g, (stbi__uint16)code);
7149 if ((avail & codemask) == 0 && avail <= 0x0FFF) {
7151 codemask = (1 << codesize) - 1;
7156 return stbi__errpuc(
"illegal code in raster",
"Corrupt GIF");
7162 static void stbi__fill_gif_background(stbi__gif *g,
int x0,
int y0,
int x1,
7166 stbi_uc *c = g->pal[g->bgindex];
7167 for (y = y0; y < y1; y += 4 * g->w) {
7168 for (x = x0; x < x1; x += 4) {
7180 static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g,
int *comp,
7186 if (g->out == 0 && !stbi__gif_header(s, g, comp, 0))
7189 if (!stbi__mad3sizes_valid(g->w, g->h, 4, 0))
7190 return stbi__errpuc(
"too large",
"GIF too large");
7193 g->out = (
stbi_uc *)stbi__malloc_mad3(4, g->w, g->h, 0);
7195 return stbi__errpuc(
"outofmem",
"Out of memory");
7197 switch ((g->eflags & 0x1C) >> 2) {
7199 stbi__fill_gif_background(g, 0, 0, 4 * g->w, 4 * g->w * g->h);
7203 memcpy(g->out, prev_out, 4 * g->w * g->h);
7204 g->old_out = prev_out;
7208 memcpy(g->out, prev_out, 4 * g->w * g->h);
7209 stbi__fill_gif_background(g, g->start_x, g->start_y, g->max_x,
7214 for (i = g->start_y; i < g->max_y; i += 4 * g->w)
7215 memcpy(&g->out[i + g->start_x], &g->old_out[i + g->start_x],
7216 g->max_x - g->start_x);
7222 switch (stbi__get8(s)) {
7225 int prev_trans = -1;
7226 stbi__int32 x, y, w, h;
7229 x = stbi__get16le(s);
7230 y = stbi__get16le(s);
7231 w = stbi__get16le(s);
7232 h = stbi__get16le(s);
7233 if (((x + w) > (g->w)) || ((y + h) > (g->h)))
7234 return stbi__errpuc(
"bad Image Descriptor",
"Corrupt GIF");
7236 g->line_size = g->w * 4;
7238 g->start_y = y * g->line_size;
7239 g->max_x = g->start_x + w * 4;
7240 g->max_y = g->start_y + h * g->line_size;
7241 g->cur_x = g->start_x;
7242 g->cur_y = g->start_y;
7244 g->lflags = stbi__get8(s);
7246 if (g->lflags & 0x40) {
7247 g->step = 8 * g->line_size;
7250 g->step = g->line_size;
7254 if (g->lflags & 0x80) {
7255 stbi__gif_parse_colortable(s, g->lpal, 2 << (g->lflags & 7),
7256 g->eflags & 0x01 ? g->transparent
7258 g->color_table = (
stbi_uc *)g->lpal;
7259 }
else if (g->flags & 0x80) {
7260 if (g->transparent >= 0 && (g->eflags & 0x01)) {
7261 prev_trans = g->pal[g->transparent][3];
7262 g->pal[g->transparent][3] = 0;
7264 g->color_table = (
stbi_uc *)g->pal;
7266 return stbi__errpuc(
"missing color table",
"Corrupt GIF");
7268 o = stbi__process_gif_raster(s, g);
7272 if (prev_trans != -1)
7273 g->pal[g->transparent][3] = (
stbi_uc)prev_trans;
7281 if (stbi__get8(s) == 0xF9) {
7282 len = stbi__get8(s);
7284 g->eflags = stbi__get8(s);
7285 g->delay = stbi__get16le(s);
7286 g->transparent = stbi__get8(s);
7292 while ((len = stbi__get8(s)) != 0)
7301 return stbi__errpuc(
"unknown code",
"Corrupt GIF");
7305 STBI_NOTUSED(req_comp);
7308 static void *stbi__gif_load(stbi__context *s,
int *x,
int *y,
int *comp,
7309 int req_comp, stbi__result_info *ri)
7312 stbi__gif *g = (stbi__gif *)stbi__malloc(
sizeof(stbi__gif));
7313 memset(g, 0,
sizeof(*g));
7316 u = stbi__gif_load_next(s, g, comp, req_comp);
7322 if (req_comp && req_comp != 4)
7323 u = stbi__convert_format(u, 4, req_comp, g->w, g->h);
7330 static int stbi__gif_info(stbi__context *s,
int *x,
int *y,
int *comp)
7332 return stbi__gif_info_raw(s, x, y, comp);
7340 static int stbi__hdr_test_core(stbi__context *s,
const char *signature)
7343 for (i = 0; signature[i]; ++i)
7344 if (stbi__get8(s) != signature[i])
7350 static int stbi__hdr_test(stbi__context *s)
7352 int r = stbi__hdr_test_core(s,
"#?RADIANCE\n");
7355 r = stbi__hdr_test_core(s,
"#?RGBE\n");
7361 #define STBI__HDR_BUFLEN 1024
7362 static char *stbi__hdr_gettoken(stbi__context *z,
char *buffer)
7367 c = (char)stbi__get8(z);
7369 while (!stbi__at_eof(z) && c !=
'\n') {
7371 if (len == STBI__HDR_BUFLEN - 1) {
7373 while (!stbi__at_eof(z) && stbi__get8(z) !=
'\n')
7377 c = (char)stbi__get8(z);
7384 static void stbi__hdr_convert(
float *output,
stbi_uc *input,
int req_comp)
7386 if (input[3] != 0) {
7389 f1 = (float)ldexp(1.0f, input[3] - (
int)(128 + 8));
7391 output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
7393 output[0] = input[0] * f1;
7394 output[1] = input[1] * f1;
7395 output[2] = input[2] * f1;
7406 output[0] = output[1] = output[2] = 0;
7417 static float *stbi__hdr_load(stbi__context *s,
int *x,
int *y,
int *comp,
7418 int req_comp, stbi__result_info *ri)
7420 char buffer[STBI__HDR_BUFLEN];
7427 unsigned char count, value;
7428 int i, j, k, c1, c2, z;
7429 const char *headerToken;
7433 headerToken = stbi__hdr_gettoken(s, buffer);
7434 if (strcmp(headerToken,
"#?RADIANCE") != 0 &&
7435 strcmp(headerToken,
"#?RGBE") != 0)
7436 return stbi__errpf(
"not HDR",
"Corrupt HDR image");
7440 token = stbi__hdr_gettoken(s, buffer);
7443 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0)
7448 return stbi__errpf(
"unsupported format",
"Unsupported HDR format");
7452 token = stbi__hdr_gettoken(s, buffer);
7453 if (strncmp(token,
"-Y ", 3))
7454 return stbi__errpf(
"unsupported data layout",
"Unsupported HDR format");
7456 height = (int)strtol(token, &token, 10);
7457 while (*token ==
' ')
7459 if (strncmp(token,
"+X ", 3))
7460 return stbi__errpf(
"unsupported data layout",
"Unsupported HDR format");
7462 width = (int)strtol(token, NULL, 10);
7472 if (!stbi__mad4sizes_valid(width, height, req_comp,
sizeof(
float), 0))
7473 return stbi__errpf(
"too large",
"HDR image is too large");
7477 (
float *)stbi__malloc_mad4(width, height, req_comp,
sizeof(
float), 0);
7479 return stbi__errpf(
"outofmem",
"Out of memory");
7483 if (width < 8 || width >= 32768) {
7485 for (j = 0; j < height; ++j) {
7486 for (i = 0; i < width; ++i) {
7489 stbi__getn(s, rgbe, 4);
7490 stbi__hdr_convert(hdr_data + j * width * req_comp +
7499 for (j = 0; j < height; ++j) {
7502 len = stbi__get8(s);
7503 if (c1 != 2 || c2 != 2 || (len & 0x80)) {
7511 rgbe[3] = (
stbi_uc)stbi__get8(s);
7512 stbi__hdr_convert(hdr_data, rgbe, req_comp);
7515 STBI_FREE(scanline);
7516 goto main_decode_loop;
7519 len |= stbi__get8(s);
7521 STBI_FREE(hdr_data);
7522 STBI_FREE(scanline);
7523 return stbi__errpf(
"invalid decoded scanline length",
7526 if (scanline == NULL) {
7527 scanline = (
stbi_uc *)stbi__malloc_mad2(width, 4, 0);
7529 STBI_FREE(hdr_data);
7530 return stbi__errpf(
"outofmem",
"Out of memory");
7534 for (k = 0; k < 4; ++k) {
7537 while ((nleft = width - i) > 0) {
7538 count = stbi__get8(s);
7541 value = stbi__get8(s);
7543 if (count > nleft) {
7544 STBI_FREE(hdr_data);
7545 STBI_FREE(scanline);
7546 return stbi__errpf(
"corrupt",
7547 "bad RLE data in HDR");
7549 for (z = 0; z < count; ++z)
7550 scanline[i++ * 4 + k] = value;
7553 if (count > nleft) {
7554 STBI_FREE(hdr_data);
7555 STBI_FREE(scanline);
7556 return stbi__errpf(
"corrupt",
7557 "bad RLE data in HDR");
7559 for (z = 0; z < count; ++z)
7560 scanline[i++ * 4 + k] = stbi__get8(s);
7564 for (i = 0; i < width; ++i)
7565 stbi__hdr_convert(hdr_data + (j * width + i) * req_comp,
7566 scanline + i * 4, req_comp);
7569 STBI_FREE(scanline);
7575 static int stbi__hdr_info(stbi__context *s,
int *x,
int *y,
int *comp)
7577 char buffer[STBI__HDR_BUFLEN];
7589 if (stbi__hdr_test(s) == 0) {
7595 token = stbi__hdr_gettoken(s, buffer);
7598 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0)
7606 token = stbi__hdr_gettoken(s, buffer);
7607 if (strncmp(token,
"-Y ", 3)) {
7612 *y = (int)strtol(token, &token, 10);
7613 while (*token ==
' ')
7615 if (strncmp(token,
"+X ", 3)) {
7620 *x = (int)strtol(token, NULL, 10);
7624 #endif // STBI_NO_HDR
7627 static int stbi__bmp_info(stbi__context *s,
int *x,
int *y,
int *comp)
7630 stbi__bmp_data info;
7633 p = stbi__bmp_parse_header(s, &info);
7642 *comp = info.ma ? 4 : 3;
7648 static int stbi__psd_info(stbi__context *s,
int *x,
int *y,
int *comp)
7650 int channelCount, dummy;
7657 if (stbi__get32be(s) != 0x38425053) {
7661 if (stbi__get16be(s) != 1) {
7666 channelCount = stbi__get16be(s);
7667 if (channelCount < 0 || channelCount > 16) {
7671 *y = stbi__get32be(s);
7672 *x = stbi__get32be(s);
7673 if (stbi__get16be(s) != 8) {
7677 if (stbi__get16be(s) != 3) {
7687 static int stbi__pic_info(stbi__context *s,
int *x,
int *y,
int *comp)
7689 int act_comp = 0, num_packets = 0, chained, dummy;
7690 stbi__pic_packet packets[10];
7699 if (!stbi__pic_is4(s,
"\x53\x80\xF6\x34")) {
7706 *x = stbi__get16be(s);
7707 *y = stbi__get16be(s);
7708 if (stbi__at_eof(s)) {
7712 if ((*x) != 0 && (1 << 28) / (*x) < (*y)) {
7720 stbi__pic_packet *packet;
7722 if (num_packets ==
sizeof(packets) /
sizeof(packets[0]))
7725 packet = &packets[num_packets++];
7726 chained = stbi__get8(s);
7727 packet->size = stbi__get8(s);
7728 packet->type = stbi__get8(s);
7729 packet->channel = stbi__get8(s);
7730 act_comp |= packet->channel;
7732 if (stbi__at_eof(s)) {
7736 if (packet->size != 8) {
7742 *comp = (act_comp & 0x10 ? 4 : 3);
7762 static int stbi__pnm_test(stbi__context *s)
7765 p = (char)stbi__get8(s);
7766 t = (char)stbi__get8(s);
7767 if (p !=
'P' || (t !=
'5' && t !=
'6')) {
7774 static void *stbi__pnm_load(stbi__context *s,
int *x,
int *y,
int *comp,
7775 int req_comp, stbi__result_info *ri)
7780 if (!stbi__pnm_info(s, (
int *)&s->img_x, (
int *)&s->img_y,
7789 if (!stbi__mad3sizes_valid(s->img_n, s->img_x, s->img_y, 0))
7790 return stbi__errpuc(
"too large",
"PNM too large");
7792 out = (
stbi_uc *)stbi__malloc_mad3(s->img_n, s->img_x, s->img_y, 0);
7794 return stbi__errpuc(
"outofmem",
"Out of memory");
7795 stbi__getn(s, out, s->img_n * s->img_x * s->img_y);
7797 if (req_comp && req_comp != s->img_n) {
7798 out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
7805 static int stbi__pnm_isspace(
char c)
7807 return c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\v' || c ==
'\f' ||
7811 static void stbi__pnm_skip_whitespace(stbi__context *s,
char *c)
7814 while (!stbi__at_eof(s) && stbi__pnm_isspace(*c))
7815 *c = (char)stbi__get8(s);
7817 if (stbi__at_eof(s) || *c !=
'#')
7820 while (!stbi__at_eof(s) && *c !=
'\n' && *c !=
'\r')
7821 *c = (
char)stbi__get8(s);
7825 static int stbi__pnm_isdigit(
char c)
7827 return c >=
'0' && c <=
'9';
7830 static int stbi__pnm_getinteger(stbi__context *s,
char *c)
7834 while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) {
7835 value = value * 10 + (*c -
'0');
7836 *c = (char)stbi__get8(s);
7842 static int stbi__pnm_info(stbi__context *s,
int *x,
int *y,
int *comp)
7857 p = (char)stbi__get8(s);
7858 t = (char)stbi__get8(s);
7859 if (p !=
'P' || (t !=
'5' && t !=
'6')) {
7867 c = (char)stbi__get8(s);
7868 stbi__pnm_skip_whitespace(s, &c);
7870 *x = stbi__pnm_getinteger(s, &c);
7871 stbi__pnm_skip_whitespace(s, &c);
7873 *y = stbi__pnm_getinteger(s, &c);
7874 stbi__pnm_skip_whitespace(s, &c);
7876 maxv = stbi__pnm_getinteger(s, &c);
7879 return stbi__err(
"max value > 255",
"PPM image not 8-bit");
7885 static int stbi__info_main(stbi__context *s,
int *x,
int *y,
int *comp)
7887 #ifndef STBI_NO_JPEG
7888 if (stbi__jpeg_info(s, x, y, comp))
7893 if (stbi__png_info(s, x, y, comp))
7898 if (stbi__gif_info(s, x, y, comp))
7903 if (stbi__bmp_info(s, x, y, comp))
7908 if (stbi__psd_info(s, x, y, comp))
7913 if (stbi__pic_info(s, x, y, comp))
7918 if (stbi__pnm_info(s, x, y, comp))
7923 if (stbi__hdr_info(s, x, y, comp))
7929 if (stbi__tga_info(s, x, y, comp))
7932 return stbi__err(
"unknown image type",
7933 "Image not of any known type, or corrupt");
7936 #ifndef STBI_NO_STDIO
7939 FILE *f = stbi__fopen(filename,
"rb");
7942 return stbi__err(
"can't fopen",
"Unable to open file");
7952 long pos = ftell(f);
7953 stbi__start_file(&s, f);
7954 r = stbi__info_main(&s, x, y, comp);
7955 fseek(f, pos, SEEK_SET);
7958 #endif // !STBI_NO_STDIO
7964 stbi__start_mem(&s, buffer, len);
7965 return stbi__info_main(&s, x, y, comp);
7969 int *x,
int *y,
int *comp)
7973 return stbi__info_main(&s, x, y, comp);
7976 #endif // STB_IMAGE_IMPLEMENTATION
#define STBIDEF
Definition: stb_image.h:330
STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
Definition: stb_image.h:315
STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
STBIDEF const char * stbi_failure_reason(void)
STBIDEF stbi_uc * stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)
STBIDEF void stbi_hdr_to_ldr_scale(float scale)
STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
unsigned short stbi_us
Definition: stb_image.h:321
STBIDEF float * stbi_loadf(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels)
STBIDEF stbi_us * stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)
STBIDEF void stbi_ldr_to_hdr_gamma(float gamma)
Definition: stb_image.h:316
STBIDEF stbi_us * stbi_load_16(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels)
Definition: stb_image.h:342
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)
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)
STBIDEF stbi_uc * stbi_load_from_file(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels)
Definition: stb_image.h:317
STBIDEF float * stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)
STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp)
Definition: stb_image.h:314
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 stbi_us * stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)
STBIDEF stbi_uc * stbi_load(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels)
STBIDEF int stbi_is_hdr(char const *filename)
unsigned char stbi_uc
Definition: stb_image.h:320
STBIDEF void stbi_image_free(void *retval_from_stbi_load)
STBIDEF float * stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)
STBIDEF stbi_uc * stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)
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)
Definition: stb_image.h:312
STBIDEF float * stbi_loadf_from_file(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels)
STBIDEF stbi_us * stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels)
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)