8#define STB_IMAGE_STATIC
9#define STB_IMAGE_IMPLEMENTATION
142#ifndef PLUTOVG_STB_IMAGE_H
143#define PLUTOVG_STB_IMAGE_H
361#define STBI_VERSION 1
382#ifdef STB_IMAGE_STATIC
383#define STBIDEF static
385#define STBIDEF extern
400 int (*read) (
void *user,
char *data,
int size);
401 void (*skip) (
void *user,
int n);
402 int (*eof) (
void *user);
410STBIDEF stbi_uc *stbi_load_from_memory (
stbi_uc const *buffer,
int len ,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
413STBIDEF stbi_uc *stbi_load (
char const *filename,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
414STBIDEF stbi_uc *stbi_load_from_file (FILE *f,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
417STBIDEF stbi_uc *stbi_load_gif_from_memory(
stbi_uc const *buffer,
int len,
int **delays,
int *x,
int *y,
int *z,
int *comp,
int req_comp);
419#ifdef STBI_WINDOWS_UTF8
420STBIDEF int stbi_convert_wchar_to_utf8(
char *buffer,
size_t bufferlen,
const wchar_t* input);
428STBIDEF stbi_us *stbi_load_16_from_memory (
stbi_uc const *buffer,
int len,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
431STBIDEF stbi_us *stbi_load_16 (
char const *filename,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
432STBIDEF stbi_us *stbi_load_from_file_16(FILE *f,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
438 STBIDEF float *stbi_loadf_from_memory (
stbi_uc const *buffer,
int len,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
439 STBIDEF float *stbi_loadf_from_callbacks (
stbi_io_callbacks const *clbk,
void *user,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
441 STBIDEF float *stbi_loadf (
char const *filename,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
442 STBIDEF float *stbi_loadf_from_file (FILE *f,
int *x,
int *y,
int *channels_in_file,
int desired_channels);
444 STBIDEF void stbi_hdr_to_ldr_gamma(
float gamma);
445 STBIDEF void stbi_hdr_to_ldr_scale(
float scale);
447 STBIDEF void stbi_ldr_to_hdr_gamma(
float gamma);
448 STBIDEF void stbi_ldr_to_hdr_scale(
float scale);
452STBIDEF int stbi_is_hdr_from_memory(
stbi_uc const *buffer,
int len);
453STBIDEF int stbi_is_hdr (
char const *filename);
454STBIDEF int stbi_is_hdr_from_file(FILE *f);
459STBIDEF const char *stbi_failure_reason (
void);
462STBIDEF void stbi_image_free (
void *retval_from_stbi_load);
465STBIDEF int stbi_info_from_memory(
stbi_uc const *buffer,
int len,
int *x,
int *y,
int *comp);
467STBIDEF int stbi_is_16_bit_from_memory(
stbi_uc const *buffer,
int len);
470STBIDEF int stbi_info (
char const *filename,
int *x,
int *y,
int *comp);
471STBIDEF int stbi_info_from_file (FILE *f,
int *x,
int *y,
int *comp);
472STBIDEF int stbi_is_16_bit (
char const *filename);
473STBIDEF int stbi_is_16_bit_from_file(FILE *f);
480STBIDEF void stbi_set_unpremultiply_on_load(
int flag_true_if_should_unpremultiply);
484STBIDEF void stbi_convert_iphone_png_to_rgb(
int flag_true_if_should_convert);
487STBIDEF void stbi_set_flip_vertically_on_load(
int flag_true_if_should_flip);
492STBIDEF void stbi_set_unpremultiply_on_load_thread(
int flag_true_if_should_unpremultiply);
493STBIDEF void stbi_convert_iphone_png_to_rgb_thread(
int flag_true_if_should_convert);
494STBIDEF void stbi_set_flip_vertically_on_load_thread(
int flag_true_if_should_flip);
498STBIDEF char *stbi_zlib_decode_malloc_guesssize(
const char *buffer,
int len,
int initial_size,
int *outlen);
499STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(
const char *buffer,
int len,
int initial_size,
int *outlen,
int parse_header);
500STBIDEF char *stbi_zlib_decode_malloc(
const char *buffer,
int len,
int *outlen);
501STBIDEF int stbi_zlib_decode_buffer(
char *obuffer,
int olen,
const char *ibuffer,
int ilen);
503STBIDEF char *stbi_zlib_decode_noheader_malloc(
const char *buffer,
int len,
int *outlen);
504STBIDEF int stbi_zlib_decode_noheader_buffer(
char *obuffer,
int olen,
const char *ibuffer,
int ilen);
516#ifdef STB_IMAGE_IMPLEMENTATION
533#define STBI_ASSERT(x) assert(x)
537#define STBI_EXTERN extern "C"
539#define STBI_EXTERN extern
545 #define stbi_inline inline
550 #define stbi_inline __forceinline
553#ifndef STBI_NO_THREAD_LOCALS
554 #if defined(__cplusplus) && __cplusplus >= 201103L
555 #define STBI_THREAD_LOCAL thread_local
556 #elif defined(__GNUC__) && __GNUC__ < 5
557 #define STBI_THREAD_LOCAL __thread
558 #elif defined(_MSC_VER)
559 #define STBI_THREAD_LOCAL __declspec(thread)
560 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
561 #define STBI_THREAD_LOCAL _Thread_local
564 #ifndef STBI_THREAD_LOCAL
565 #if defined(__GNUC__)
566 #define STBI_THREAD_LOCAL __thread
571#if defined(_MSC_VER) || defined(__SYMBIAN32__)
588#define STBI_NOTUSED(v) (void)(v)
590#define STBI_NOTUSED(v) (void)sizeof(v)
594#define STBI_HAS_LROTL
598 #define stbi_lrot(x,y) _lrotl(x,y)
600 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31)))
603#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))
605#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED)
608#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)."
612#define STBI_MALLOC(sz) malloc(sz)
613#define STBI_REALLOC(p,newsz) realloc(p,newsz)
614#define STBI_FREE(p) free(p)
617#ifndef STBI_REALLOC_SIZED
618#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)
622#if defined(__x86_64__) || defined(_M_X64)
623#define STBI__X64_TARGET
624#elif defined(__i386) || defined(_M_IX86)
625#define STBI__X86_TARGET
628#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD)
639#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)
654#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))
656#include <emmintrin.h>
662static int stbi__cpuid3(
void)
669static int stbi__cpuid3(
void)
681#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
683#if defined(STBI_SSE2)
684static int stbi__sse2_available(
void)
686 int info3 = stbi__cpuid3();
687 return ((info3 >> 26) & 1) != 0;
692#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
694#if defined(STBI_SSE2)
695static int stbi__sse2_available(
void)
708#if defined(STBI_NO_SIMD) && defined(STBI_NEON)
715#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
717#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
721#ifndef STBI_SIMD_ALIGN
722#define STBI_SIMD_ALIGN(type, name) type name
725#ifndef STBI_MAX_DIMENSIONS
726#define STBI_MAX_DIMENSIONS (1 << 24)
774 stbi__refill_buffer(s);
779static int stbi__stdio_read(
void *user,
char *data,
int size)
781 return (
int) fread(data,1,size,(FILE*) user);
784static void stbi__stdio_skip(
void *user,
int n)
787 fseek((FILE*) user, n, SEEK_CUR);
788 ch = fgetc((FILE*) user);
790 ungetc(ch, (FILE *) user);
794static int stbi__stdio_eof(
void *user)
796 return feof((FILE*) user) || ferror((FILE *) user);
808 stbi__start_callbacks(s, &stbi__stdio_callbacks, (
void *) f);
838static int stbi__jpeg_info(
stbi__context *s,
int *x,
int *y,
int *comp);
842static int stbi__png_info(
stbi__context *s,
int *x,
int *y,
int *comp);
847static int stbi__bmp_info(
stbi__context *s,
int *x,
int *y,
int *comp);
851static int stbi__tga_info(
stbi__context *s,
int *x,
int *y,
int *comp);
855static int stbi__psd_info(
stbi__context *s,
int *x,
int *y,
int *comp);
860static int stbi__hdr_info(
stbi__context *s,
int *x,
int *y,
int *comp);
864static int stbi__pic_info(
stbi__context *s,
int *x,
int *y,
int *comp);
868static void *stbi__load_gif_main(
stbi__context *s,
int **delays,
int *x,
int *y,
int *z,
int *comp,
int req_comp);
869static int stbi__gif_info(
stbi__context *s,
int *x,
int *y,
int *comp);
873static int stbi__pnm_info(
stbi__context *s,
int *x,
int *y,
int *comp);
877#ifdef STBI_THREAD_LOCAL
880const char *stbi__g_failure_reason;
882STBIDEF const char *stbi_failure_reason(
void)
884 return stbi__g_failure_reason;
887#ifndef STBI_NO_FAILURE_STRINGS
890 stbi__g_failure_reason = str;
895static void *stbi__malloc(
size_t size)
912static int stbi__addsizes_valid(
int a,
int b)
919 return a <= INT_MAX - b;
924static int stbi__mul2sizes_valid(
int a,
int b)
926 if (a < 0 || b < 0)
return 0;
927 if (b == 0)
return 1;
929 return a <= INT_MAX/b;
933static int stbi__mad2sizes_valid(
int a,
int b,
int add)
935 return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add);
939static int stbi__mad3sizes_valid(
int a,
int b,
int c,
int add)
941 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
942 stbi__addsizes_valid(a*b*c, add);
946static 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) && stbi__addsizes_valid(a*b*c*d, add);
953static void *stbi__malloc_mad2(
int a,
int b,
int add)
955 if (!stbi__mad2sizes_valid(a, b, add))
return NULL;
956 return stbi__malloc(a*b + add);
959static void *stbi__malloc_mad3(
int a,
int b,
int c,
int add)
961 if (!stbi__mad3sizes_valid(a, b, c, add))
return NULL;
962 return stbi__malloc(a*b*c + add);
965static void *stbi__malloc_mad4(
int a,
int b,
int c,
int d,
int add)
967 if (!stbi__mad4sizes_valid(a, b, c, d, add))
return NULL;
968 return stbi__malloc(a*b*c*d + add);
972static int stbi__addints_valid(
int a,
int b)
974 if ((a >= 0) != (b >= 0))
return 1;
975 if (a < 0 && b < 0)
return a >= INT_MIN - b;
976 return a <= INT_MAX - b;
980static int stbi__mul2shorts_valid(
int a,
int b)
982 if (b == 0 || b == -1)
return 1;
983 if ((a >= 0) == (b >= 0))
return a <= SHRT_MAX/b;
984 if (b < 0)
return a <= SHRT_MIN / b;
985 return a >= SHRT_MIN / b;
992#ifdef STBI_NO_FAILURE_STRINGS
993 #define stbi__err(x,y) 0
994#elif defined(STBI_FAILURE_USERMSG)
995 #define stbi__err(x,y) stbi__err(y)
997 #define stbi__err(x,y) stbi__err(x)
1000#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL))
1001#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL))
1003STBIDEF void stbi_image_free(
void *retval_from_stbi_load)
1008static float *stbi__ldr_to_hdr(
stbi_uc *data,
int x,
int y,
int comp);
1010static stbi_uc *stbi__hdr_to_ldr(
float *data,
int x,
int y,
int comp);
1012static int stbi__vertically_flip_on_load_global = 0;
1014STBIDEF void stbi_set_flip_vertically_on_load(
int flag_true_if_should_flip)
1016 stbi__vertically_flip_on_load_global = flag_true_if_should_flip;
1019#ifndef STBI_THREAD_LOCAL
1020#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global
1022static STBI_THREAD_LOCAL
int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set;
1024STBIDEF void stbi_set_flip_vertically_on_load_thread(
int flag_true_if_should_flip)
1026 stbi__vertically_flip_on_load_local = flag_true_if_should_flip;
1027 stbi__vertically_flip_on_load_set = 1;
1030#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \
1031 ? stbi__vertically_flip_on_load_local \
1032 : stbi__vertically_flip_on_load_global)
1037 memset(ri, 0,
sizeof(*ri));
1044 if (stbi__png_test(s))
return stbi__png_load(s,x,y,comp,req_comp, ri);
1045 if (stbi__bmp_test(s))
return stbi__bmp_load(s,x,y,comp,req_comp, ri);
1046 if (stbi__gif_test(s))
return stbi__gif_load(s,x,y,comp,req_comp, ri);
1047 if (stbi__psd_test(s))
return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc);
1048 if (stbi__pic_test(s))
return stbi__pic_load(s,x,y,comp,req_comp, ri);
1053 if (stbi__jpeg_test(s))
return stbi__jpeg_load(s,x,y,comp,req_comp, ri);
1054 if (stbi__pnm_test(s))
return stbi__pnm_load(s,x,y,comp,req_comp, ri);
1056 if (stbi__hdr_test(s)) {
1057 float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri);
1058 return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
1062 if (stbi__tga_test(s))
1063 return stbi__tga_load(s,x,y,comp,req_comp, ri);
1065 return stbi__errpuc(
"unknown image type",
"Image not of any known type, or corrupt");
1071 int img_len = w * h * channels;
1074 reduced = (
stbi_uc *) stbi__malloc(img_len);
1075 if (reduced == NULL)
return stbi__errpuc(
"outofmem",
"Out of memory");
1077 for (i = 0; i < img_len; ++i)
1078 reduced[i] = (
stbi_uc)((orig[i] >> 8) & 0xFF);
1087 int img_len = w * h * channels;
1093 for (i = 0; i < img_len; ++i)
1094 enlarged[i] = (
stbi__uint16)((orig[i] << 8) + orig[i]);
1100static void stbi__vertical_flip(
void *image,
int w,
int h,
int bytes_per_pixel)
1103 size_t bytes_per_row = (size_t)w * bytes_per_pixel;
1107 for (row = 0; row < (h>>1); row++) {
1108 stbi_uc *row0 = bytes + row*bytes_per_row;
1109 stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row;
1111 size_t bytes_left = bytes_per_row;
1112 while (bytes_left) {
1113 size_t bytes_copy = (bytes_left <
sizeof(temp)) ? bytes_left :
sizeof(temp);
1114 memcpy(temp, row0, bytes_copy);
1115 memcpy(row0, row1, bytes_copy);
1116 memcpy(row1, temp, bytes_copy);
1119 bytes_left -= bytes_copy;
1124static void stbi__vertical_flip_slices(
void *image,
int w,
int h,
int z,
int bytes_per_pixel)
1127 int slice_size = w * h * bytes_per_pixel;
1130 for (slice = 0; slice < z; ++slice) {
1131 stbi__vertical_flip(bytes, w, h, bytes_per_pixel);
1132 bytes += slice_size;
1136static unsigned char *stbi__load_and_postprocess_8bit(
stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
1139 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);
1148 result = stbi__convert_16_to_8((
stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
1155 int channels = req_comp ? req_comp : *comp;
1156 stbi__vertical_flip(result, *x, *y, channels *
sizeof(
stbi_uc));
1159 return (
unsigned char *) result;
1165 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16);
1174 result = stbi__convert_8_to_16((
stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
1182 int channels = req_comp ? req_comp : *comp;
1183 stbi__vertical_flip(result, *x, *y, channels *
sizeof(
stbi__uint16));
1189static void stbi__float_postprocess(
float *result,
int *x,
int *y,
int *comp,
int req_comp)
1192 int channels = req_comp ? req_comp : *comp;
1193 stbi__vertical_flip(result, *x, *y, channels *
sizeof(
float));
1198#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
1199STBI_EXTERN __declspec(dllimport)
int __stdcall MultiByteToWideChar(
unsigned int cp,
unsigned long flags,
const char *str,
int cbmb,
wchar_t *widestr,
int cchwide);
1200STBI_EXTERN __declspec(dllimport)
int __stdcall WideCharToMultiByte(
unsigned int cp,
unsigned long flags,
const wchar_t *widestr,
int cchwide,
char *str,
int cbmb,
const char *defchar,
int *used_default);
1203#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
1204STBIDEF int stbi_convert_wchar_to_utf8(
char *buffer,
size_t bufferlen,
const wchar_t* input)
1206 return WideCharToMultiByte(65001 , 0, input, -1, buffer, (
int) bufferlen, NULL, NULL);
1210static FILE *stbi__fopen(
char const *filename,
char const *mode)
1213#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
1215 wchar_t wFilename[1024];
1216 if (0 == MultiByteToWideChar(65001 , 0, filename, -1, wFilename,
sizeof(wFilename)/
sizeof(*wFilename)))
1219 if (0 == MultiByteToWideChar(65001 , 0, mode, -1, wMode,
sizeof(wMode)/
sizeof(*wMode)))
1222#if defined(_MSC_VER) && _MSC_VER >= 1400
1223 if (0 != _wfopen_s(&f, wFilename, wMode))
1226 f = _wfopen(wFilename, wMode);
1229#elif defined(_MSC_VER) && _MSC_VER >= 1400
1230 if (0 != fopen_s(&f, filename, mode))
1233 f = fopen(filename, mode);
1239STBIDEF stbi_uc *stbi_load(
char const *filename,
int *x,
int *y,
int *comp,
int req_comp)
1241 FILE *f = stbi__fopen(filename,
"rb");
1242 unsigned char *result;
1243 if (!f)
return stbi__errpuc(
"can't fopen",
"Unable to open file");
1244 result = stbi_load_from_file(f,x,y,comp,req_comp);
1249STBIDEF stbi_uc *stbi_load_from_file(FILE *f,
int *x,
int *y,
int *comp,
int req_comp)
1251 unsigned char *result;
1253 stbi__start_file(&s,f);
1254 result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1262STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f,
int *x,
int *y,
int *comp,
int req_comp)
1266 stbi__start_file(&s,f);
1267 result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp);
1275STBIDEF stbi_us *stbi_load_16(
char const *filename,
int *x,
int *y,
int *comp,
int req_comp)
1277 FILE *f = stbi__fopen(filename,
"rb");
1280 result = stbi_load_from_file_16(f,x,y,comp,req_comp);
1287STBIDEF stbi_us *stbi_load_16_from_memory(
stbi_uc const *buffer,
int len,
int *x,
int *y,
int *channels_in_file,
int desired_channels)
1290 stbi__start_mem(&s,buffer,len);
1291 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
1298 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
1301STBIDEF stbi_uc *stbi_load_from_memory(
stbi_uc const *buffer,
int len,
int *x,
int *y,
int *comp,
int req_comp)
1304 stbi__start_mem(&s,buffer,len);
1305 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1312 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1315STBIDEF stbi_uc *stbi_load_gif_from_memory(
stbi_uc const *buffer,
int len,
int **delays,
int *x,
int *y,
int *z,
int *comp,
int req_comp)
1317 unsigned char *result;
1319 stbi__start_mem(&s,buffer,len);
1321 result = (
unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
1323 stbi__vertical_flip_slices( result, *x, *y, *z, *comp );
1329static float *stbi__loadf_main(
stbi__context *s,
int *x,
int *y,
int *comp,
int req_comp)
1331 unsigned char *data;
1332 if (stbi__hdr_test(s)) {
1334 float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri);
1336 stbi__float_postprocess(hdr_data,x,y,comp,req_comp);
1339 data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
1341 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
1342 return stbi__errpf(
"unknown image type",
"Image not of any known type, or corrupt");
1345STBIDEF float *stbi_loadf_from_memory(
stbi_uc const *buffer,
int len,
int *x,
int *y,
int *comp,
int req_comp)
1348 stbi__start_mem(&s,buffer,len);
1349 return stbi__loadf_main(&s,x,y,comp,req_comp);
1352STBIDEF float *stbi_loadf_from_callbacks(
stbi_io_callbacks const *clbk,
void *user,
int *x,
int *y,
int *comp,
int req_comp)
1356 return stbi__loadf_main(&s,x,y,comp,req_comp);
1359STBIDEF float *stbi_loadf(
char const *filename,
int *x,
int *y,
int *comp,
int req_comp)
1362 FILE *f = stbi__fopen(filename,
"rb");
1363 if (!f)
return stbi__errpf(
"can't fopen",
"Unable to open file");
1364 result = stbi_loadf_from_file(f,x,y,comp,req_comp);
1369STBIDEF float *stbi_loadf_from_file(FILE *f,
int *x,
int *y,
int *comp,
int req_comp)
1372 stbi__start_file(&s,f);
1373 return stbi__loadf_main(&s,x,y,comp,req_comp);
1378STBIDEF int stbi_is_hdr_from_memory(
stbi_uc const *buffer,
int len)
1381 stbi__start_mem(&s,buffer,len);
1382 return stbi__hdr_test(&s);
1385STBIDEF int stbi_is_hdr (
char const *filename)
1387 FILE *f = stbi__fopen(filename,
"rb");
1390 result = stbi_is_hdr_from_file(f);
1396STBIDEF int stbi_is_hdr_from_file(FILE *f)
1398 long pos = ftell(f);
1401 stbi__start_file(&s,f);
1402 res = stbi__hdr_test(&s);
1403 fseek(f, pos, SEEK_SET);
1411 return stbi__hdr_test(&s);
1414static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;
1416STBIDEF void stbi_ldr_to_hdr_gamma(
float gamma) { stbi__l2h_gamma = gamma; }
1417STBIDEF void stbi_ldr_to_hdr_scale(
float scale) { stbi__l2h_scale = scale; }
1419static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;
1421STBIDEF void stbi_hdr_to_ldr_gamma(
float gamma) { stbi__h2l_gamma_i = 1/gamma; }
1422STBIDEF void stbi_hdr_to_ldr_scale(
float scale) { stbi__h2l_scale_i = 1/scale; }
1459 stbi__refill_buffer(s);
1505 res = (count == (n-blen));
1521 int z = stbi__get8(s);
1522 return (z << 8) + stbi__get8(s);
1528 return (z << 16) + stbi__get16be(s);
1533 int z = stbi__get8(s);
1534 return z + (stbi__get8(s) << 8);
1544#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255))
1557static stbi_uc stbi__compute_y(
int r,
int g,
int b)
1559 return (
stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);
1562static unsigned char *stbi__convert_format(
unsigned char *data,
int img_n,
int req_comp,
unsigned int x,
unsigned int y)
1565 unsigned char *good;
1567 if (req_comp == img_n)
return data;
1570 good = (
unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);
1576 for (j=0; j < (int) y; ++j) {
1577 unsigned char *src = data + j * x * img_n ;
1578 unsigned char *dest = good + j * x * req_comp;
1580 #define STBI__COMBO(a,b) ((a)*8+(b))
1581 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1585 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; }
break;
1586 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; }
break;
1587 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; }
break;
1589 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; }
break;
1590 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; }
break;
1591 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; }
break;
1592 STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); }
break;
1593 STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; }
break;
1594 STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); }
break;
1595 STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; }
break;
1596 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; }
break;
1606static stbi__uint16 stbi__compute_y_16(
int r,
int g,
int b)
1608 return (
stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);
1611static stbi__uint16 *stbi__convert_format16(
stbi__uint16 *data,
int img_n,
int req_comp,
unsigned int x,
unsigned int y)
1616 if (req_comp == img_n)
return data;
1619 good = (
stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
1625 for (j=0; j < (int) y; ++j) {
1629 #define STBI__COMBO(a,b) ((a)*8+(b))
1630 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
1634 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; }
break;
1635 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; }
break;
1636 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; }
break;
1638 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; }
break;
1639 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; }
break;
1640 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; }
break;
1641 STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); }
break;
1642 STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; }
break;
1643 STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); }
break;
1644 STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; }
break;
1645 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; }
break;
1655static float *stbi__ldr_to_hdr(
stbi_uc *data,
int x,
int y,
int comp)
1659 if (!data)
return NULL;
1660 output = (
float *) stbi__malloc_mad4(x, y, comp,
sizeof(
float), 0);
1663 if (comp & 1) n = comp;
else n = comp-1;
1664 for (i=0; i < x*y; ++i) {
1665 for (k=0; k < n; ++k) {
1666 output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
1670 for (i=0; i < x*y; ++i) {
1671 output[i*comp + n] = data[i*comp + n]/255.0f;
1678#define stbi__float2int(x) ((int) (x))
1679static stbi_uc *stbi__hdr_to_ldr(
float *data,
int x,
int y,
int comp)
1683 if (!data)
return NULL;
1684 output = (
stbi_uc *) stbi__malloc_mad3(x, y, comp, 0);
1687 if (comp & 1) n = comp;
else n = comp-1;
1688 for (i=0; i < x*y; ++i) {
1689 for (k=0; k < n; ++k) {
1690 float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;
1692 if (z > 255) z = 255;
1696 float z = data[i*comp+k] * 255 + 0.5f;
1698 if (z > 255) z = 255;
1738 unsigned int maxcode[18];
1787 int scan_n, order[4];
1791 void (*idct_block_kernel)(
stbi_uc *out,
int out_stride,
short data[64]);
1801 for (i=0; i < 16; ++i) {
1802 for (j=0; j < count[i]; ++j) {
1804 if(k >= 257)
return stbi__err(
"bad size list",
"Corrupt JPEG");
1812 for(j=1; j <= 16; ++j) {
1814 h->
delta[j] = k - code;
1815 if (h->
size[k] == j) {
1816 while (h->
size[k] == j)
1818 if (code-1 >= (1u << j))
return stbi__err(
"bad code lengths",
"Corrupt JPEG");
1821 h->
maxcode[j] = code << (16-j);
1828 for (i=0; i < k; ++i) {
1833 for (j=0; j < m; ++j) {
1850 int rs = h->
values[fast];
1851 int run = (rs >> 4) & 15;
1852 int magbits = rs & 15;
1853 int len = h->
size[fast];
1855 if (magbits && len + magbits <=
FAST_BITS) {
1858 int m = 1 << (magbits - 1);
1859 if (k < m) k += (~0U << magbits) + 1;
1861 if (k >= -128 && k <= 127)
1862 fast_ac[i] = (
stbi__int16) ((k * 256) + (run * 16) + (len + magbits));
1868static void stbi__grow_buffer_unsafe(
stbi__jpeg *j)
1871 unsigned int b = j->
nomore ? 0 : stbi__get8(j->
s);
1873 int c = stbi__get8(j->
s);
1874 while (c == 0xff) c = stbi__get8(j->
s);
1876 j->
marker = (
unsigned char) c;
1887static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
1895 if (j->
code_bits < 16) stbi__grow_buffer_unsafe(j);
1918 if (temp < h->maxcode[k])
1931 if(c < 0 || c >= 256)
1942static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};
1950 if (j->
code_bits < n) stbi__grow_buffer_unsafe(j);
1956 k &= stbi__bmask[n];
1958 return k + (stbi__jbias[n] & (sgn - 1));
1965 if (j->
code_bits < n) stbi__grow_buffer_unsafe(j);
1969 k &= stbi__bmask[n];
1977 if (j->
code_bits < 1) stbi__grow_buffer_unsafe(j);
1982 return k & 0x80000000;
1987static const stbi_uc stbi__jpeg_dezigzag[64+15] =
1989 0, 1, 8, 16, 9, 2, 3, 10,
1990 17, 24, 32, 25, 18, 11, 4, 5,
1991 12, 19, 26, 33, 40, 48, 41, 34,
1992 27, 20, 13, 6, 7, 14, 21, 28,
1993 35, 42, 49, 56, 57, 50, 43, 36,
1994 29, 22, 15, 23, 30, 37, 44, 51,
1995 58, 59, 52, 45, 38, 31, 39, 46,
1996 53, 60, 61, 54, 47, 55, 62, 63,
1998 63, 63, 63, 63, 63, 63, 63, 63,
1999 63, 63, 63, 63, 63, 63, 63
2008 if (j->
code_bits < 16) stbi__grow_buffer_unsafe(j);
2009 t = stbi__jpeg_huff_decode(j, hdc);
2010 if (t < 0 || t > 15)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2013 memset(data,0,64*
sizeof(data[0]));
2015 diff = t ? stbi__extend_receive(j, t) : 0;
2019 if (!stbi__mul2shorts_valid(dc, dequant[0]))
return stbi__err(
"can't merge dc and ac",
"Corrupt JPEG");
2020 data[0] = (short) (dc * dequant[0]);
2027 if (j->
code_bits < 16) stbi__grow_buffer_unsafe(j);
2033 if (s > j->
code_bits)
return stbi__err(
"bad huffman code",
"Combined length longer than code bits available");
2037 zig = stbi__jpeg_dezigzag[k++];
2038 data[zig] = (short) ((r >> 8) * dequant[zig]);
2040 int rs = stbi__jpeg_huff_decode(j, hac);
2041 if (rs < 0)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2045 if (rs != 0xf0)
break;
2050 zig = stbi__jpeg_dezigzag[k++];
2051 data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]);
2062 if (j->
spec_end != 0)
return stbi__err(
"can't merge dc and ac",
"Corrupt JPEG");
2064 if (j->
code_bits < 16) stbi__grow_buffer_unsafe(j);
2068 memset(data,0,64*
sizeof(data[0]));
2069 t = stbi__jpeg_huff_decode(j, hdc);
2070 if (t < 0 || t > 15)
return stbi__err(
"can't merge dc and ac",
"Corrupt JPEG");
2071 diff = t ? stbi__extend_receive(j, t) : 0;
2076 if (!stbi__mul2shorts_valid(dc, 1 << j->
succ_low))
return stbi__err(
"can't merge dc and ac",
"Corrupt JPEG");
2077 data[0] = (short) (dc * (1 << j->
succ_low));
2080 if (stbi__jpeg_get_bit(j))
2081 data[0] += (
short) (1 << j->
succ_low);
2105 if (j->
code_bits < 16) stbi__grow_buffer_unsafe(j);
2111 if (s > j->
code_bits)
return stbi__err(
"bad huffman code",
"Combined length longer than code bits available");
2114 zig = stbi__jpeg_dezigzag[k++];
2115 data[zig] = (short) ((r >> 8) * (1 << shift));
2117 int rs = stbi__jpeg_huff_decode(j, hac);
2118 if (rs < 0)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2125 j->
eob_run += stbi__jpeg_get_bits(j, r);
2132 zig = stbi__jpeg_dezigzag[k++];
2133 data[zig] = (short) (stbi__extend_receive(j,s) * (1 << shift));
2136 }
while (k <= j->spec_end);
2140 short bit = (short) (1 << j->
succ_low);
2145 short *p = &data[stbi__jpeg_dezigzag[k]];
2147 if (stbi__jpeg_get_bit(j))
2148 if ((*p & bit)==0) {
2159 int rs = stbi__jpeg_huff_decode(j, hac);
2160 if (rs < 0)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2167 j->
eob_run += stbi__jpeg_get_bits(j, r);
2175 if (s != 1)
return stbi__err(
"bad huffman code",
"Corrupt JPEG");
2177 if (stbi__jpeg_get_bit(j))
2184 while (k <= j->spec_end) {
2185 short *p = &data[stbi__jpeg_dezigzag[k++]];
2187 if (stbi__jpeg_get_bit(j))
2188 if ((*p & bit)==0) {
2202 }
while (k <= j->spec_end);
2212 if ((
unsigned int) x > 255) {
2213 if (x < 0)
return 0;
2214 if (x > 255)
return 255;
2219#define stbi__f2f(x) ((int) (((x) * 4096 + 0.5)))
2220#define stbi__fsh(x) ((x) * 4096)
2223#define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
2224 int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
2227 p1 = (p2+p3) * stbi__f2f(0.5411961f); \
2228 t2 = p1 + p3*stbi__f2f(-1.847759065f); \
2229 t3 = p1 + p2*stbi__f2f( 0.765366865f); \
2232 t0 = stbi__fsh(p2+p3); \
2233 t1 = stbi__fsh(p2-p3); \
2246 p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
2247 t0 = t0*stbi__f2f( 0.298631336f); \
2248 t1 = t1*stbi__f2f( 2.053119869f); \
2249 t2 = t2*stbi__f2f( 3.072711026f); \
2250 t3 = t3*stbi__f2f( 1.501321110f); \
2251 p1 = p5 + p1*stbi__f2f(-0.899976223f); \
2252 p2 = p5 + p2*stbi__f2f(-2.562915447f); \
2253 p3 = p3*stbi__f2f(-1.961570560f); \
2254 p4 = p4*stbi__f2f(-0.390180644f); \
2260static void stbi__idct_block(
stbi_uc *out,
int out_stride,
short data[64])
2262 int i,val[64],*v=val;
2267 for (i=0; i < 8; ++i,++d, ++v) {
2269 if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
2270 && d[40]==0 && d[48]==0 && d[56]==0) {
2275 int dcterm = d[0]*4;
2276 v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
2278 STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56])
2281 x0 += 512; x1 += 512; x2 += 512; x3 += 512;
2282 v[ 0] = (x0+t3) >> 10;
2283 v[56] = (x0-t3) >> 10;
2284 v[ 8] = (x1+t2) >> 10;
2285 v[48] = (x1-t2) >> 10;
2286 v[16] = (x2+t1) >> 10;
2287 v[40] = (x2-t1) >> 10;
2288 v[24] = (x3+t0) >> 10;
2289 v[32] = (x3-t0) >> 10;
2293 for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
2302 x0 += 65536 + (128<<17);
2303 x1 += 65536 + (128<<17);
2304 x2 += 65536 + (128<<17);
2305 x3 += 65536 + (128<<17);
2308 o[0] = stbi__clamp((x0+t3) >> 17);
2309 o[7] = stbi__clamp((x0-t3) >> 17);
2310 o[1] = stbi__clamp((x1+t2) >> 17);
2311 o[6] = stbi__clamp((x1-t2) >> 17);
2312 o[2] = stbi__clamp((x2+t1) >> 17);
2313 o[5] = stbi__clamp((x2-t1) >> 17);
2314 o[3] = stbi__clamp((x3+t0) >> 17);
2315 o[4] = stbi__clamp((x3-t0) >> 17);
2323static void stbi__idct_simd(
stbi_uc *out,
int out_stride,
short data[64])
2326 __m128i row0, row1, row2, row3, row4, row5, row6, row7;
2330 #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y))
2334 #define dct_rot(out0,out1, x,y,c0,c1) \
2335 __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \
2336 __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \
2337 __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \
2338 __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \
2339 __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \
2340 __m128i out1##_h = _mm_madd_epi16(c0##hi, c1)
2343 #define dct_widen(out, in) \
2344 __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \
2345 __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4)
2348 #define dct_wadd(out, a, b) \
2349 __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \
2350 __m128i out##_h = _mm_add_epi32(a##_h, b##_h)
2353 #define dct_wsub(out, a, b) \
2354 __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \
2355 __m128i out##_h = _mm_sub_epi32(a##_h, b##_h)
2358 #define dct_bfly32o(out0, out1, a,b,bias,s) \
2360 __m128i abiased_l = _mm_add_epi32(a##_l, bias); \
2361 __m128i abiased_h = _mm_add_epi32(a##_h, bias); \
2362 dct_wadd(sum, abiased, b); \
2363 dct_wsub(dif, abiased, b); \
2364 out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \
2365 out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \
2369 #define dct_interleave8(a, b) \
2371 a = _mm_unpacklo_epi8(a, b); \
2372 b = _mm_unpackhi_epi8(tmp, b)
2375 #define dct_interleave16(a, b) \
2377 a = _mm_unpacklo_epi16(a, b); \
2378 b = _mm_unpackhi_epi16(tmp, b)
2380 #define dct_pass(bias,shift) \
2383 dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \
2384 __m128i sum04 = _mm_add_epi16(row0, row4); \
2385 __m128i dif04 = _mm_sub_epi16(row0, row4); \
2386 dct_widen(t0e, sum04); \
2387 dct_widen(t1e, dif04); \
2388 dct_wadd(x0, t0e, t3e); \
2389 dct_wsub(x3, t0e, t3e); \
2390 dct_wadd(x1, t1e, t2e); \
2391 dct_wsub(x2, t1e, t2e); \
2393 dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \
2394 dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \
2395 __m128i sum17 = _mm_add_epi16(row1, row7); \
2396 __m128i sum35 = _mm_add_epi16(row3, row5); \
2397 dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \
2398 dct_wadd(x4, y0o, y4o); \
2399 dct_wadd(x5, y1o, y5o); \
2400 dct_wadd(x6, y2o, y5o); \
2401 dct_wadd(x7, y3o, y4o); \
2402 dct_bfly32o(row0,row7, x0,x7,bias,shift); \
2403 dct_bfly32o(row1,row6, x1,x6,bias,shift); \
2404 dct_bfly32o(row2,row5, x2,x5,bias,shift); \
2405 dct_bfly32o(row3,row4, x3,x4,bias,shift); \
2418 __m128i bias_0 = _mm_set1_epi32(512);
2419 __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17));
2422 row0 = _mm_load_si128((
const __m128i *) (data + 0*8));
2423 row1 = _mm_load_si128((
const __m128i *) (data + 1*8));
2424 row2 = _mm_load_si128((
const __m128i *) (data + 2*8));
2425 row3 = _mm_load_si128((
const __m128i *) (data + 3*8));
2426 row4 = _mm_load_si128((
const __m128i *) (data + 4*8));
2427 row5 = _mm_load_si128((
const __m128i *) (data + 5*8));
2428 row6 = _mm_load_si128((
const __m128i *) (data + 6*8));
2429 row7 = _mm_load_si128((
const __m128i *) (data + 7*8));
2432 dct_pass(bias_0, 10);
2436 dct_interleave16(row0, row4);
2437 dct_interleave16(row1, row5);
2438 dct_interleave16(row2, row6);
2439 dct_interleave16(row3, row7);
2442 dct_interleave16(row0, row2);
2443 dct_interleave16(row1, row3);
2444 dct_interleave16(row4, row6);
2445 dct_interleave16(row5, row7);
2448 dct_interleave16(row0, row1);
2449 dct_interleave16(row2, row3);
2450 dct_interleave16(row4, row5);
2451 dct_interleave16(row6, row7);
2455 dct_pass(bias_1, 17);
2459 __m128i p0 = _mm_packus_epi16(row0, row1);
2460 __m128i p1 = _mm_packus_epi16(row2, row3);
2461 __m128i p2 = _mm_packus_epi16(row4, row5);
2462 __m128i p3 = _mm_packus_epi16(row6, row7);
2465 dct_interleave8(p0, p2);
2466 dct_interleave8(p1, p3);
2469 dct_interleave8(p0, p1);
2470 dct_interleave8(p2, p3);
2473 dct_interleave8(p0, p2);
2474 dct_interleave8(p1, p3);
2477 _mm_storel_epi64((__m128i *) out, p0); out += out_stride;
2478 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride;
2479 _mm_storel_epi64((__m128i *) out, p2); out += out_stride;
2480 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride;
2481 _mm_storel_epi64((__m128i *) out, p1); out += out_stride;
2482 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride;
2483 _mm_storel_epi64((__m128i *) out, p3); out += out_stride;
2484 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e));
2493#undef dct_interleave8
2494#undef dct_interleave16
2504static void stbi__idct_simd(
stbi_uc *out,
int out_stride,
short data[64])
2506 int16x8_t row0, row1, row2, row3, row4, row5, row6, row7;
2508 int16x4_t rot0_0 = vdup_n_s16(
stbi__f2f(0.5411961f));
2509 int16x4_t rot0_1 = vdup_n_s16(
stbi__f2f(-1.847759065f));
2510 int16x4_t rot0_2 = vdup_n_s16(
stbi__f2f( 0.765366865f));
2511 int16x4_t rot1_0 = vdup_n_s16(
stbi__f2f( 1.175875602f));
2512 int16x4_t rot1_1 = vdup_n_s16(
stbi__f2f(-0.899976223f));
2513 int16x4_t rot1_2 = vdup_n_s16(
stbi__f2f(-2.562915447f));
2514 int16x4_t rot2_0 = vdup_n_s16(
stbi__f2f(-1.961570560f));
2515 int16x4_t rot2_1 = vdup_n_s16(
stbi__f2f(-0.390180644f));
2516 int16x4_t rot3_0 = vdup_n_s16(
stbi__f2f( 0.298631336f));
2517 int16x4_t rot3_1 = vdup_n_s16(
stbi__f2f( 2.053119869f));
2518 int16x4_t rot3_2 = vdup_n_s16(
stbi__f2f( 3.072711026f));
2519 int16x4_t rot3_3 = vdup_n_s16(
stbi__f2f( 1.501321110f));
2521#define dct_long_mul(out, inq, coeff) \
2522 int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \
2523 int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff)
2525#define dct_long_mac(out, acc, inq, coeff) \
2526 int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \
2527 int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff)
2529#define dct_widen(out, inq) \
2530 int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \
2531 int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12)
2534#define dct_wadd(out, a, b) \
2535 int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \
2536 int32x4_t out##_h = vaddq_s32(a##_h, b##_h)
2539#define dct_wsub(out, a, b) \
2540 int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \
2541 int32x4_t out##_h = vsubq_s32(a##_h, b##_h)
2544#define dct_bfly32o(out0,out1, a,b,shiftop,s) \
2546 dct_wadd(sum, a, b); \
2547 dct_wsub(dif, a, b); \
2548 out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \
2549 out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \
2552#define dct_pass(shiftop, shift) \
2555 int16x8_t sum26 = vaddq_s16(row2, row6); \
2556 dct_long_mul(p1e, sum26, rot0_0); \
2557 dct_long_mac(t2e, p1e, row6, rot0_1); \
2558 dct_long_mac(t3e, p1e, row2, rot0_2); \
2559 int16x8_t sum04 = vaddq_s16(row0, row4); \
2560 int16x8_t dif04 = vsubq_s16(row0, row4); \
2561 dct_widen(t0e, sum04); \
2562 dct_widen(t1e, dif04); \
2563 dct_wadd(x0, t0e, t3e); \
2564 dct_wsub(x3, t0e, t3e); \
2565 dct_wadd(x1, t1e, t2e); \
2566 dct_wsub(x2, t1e, t2e); \
2568 int16x8_t sum15 = vaddq_s16(row1, row5); \
2569 int16x8_t sum17 = vaddq_s16(row1, row7); \
2570 int16x8_t sum35 = vaddq_s16(row3, row5); \
2571 int16x8_t sum37 = vaddq_s16(row3, row7); \
2572 int16x8_t sumodd = vaddq_s16(sum17, sum35); \
2573 dct_long_mul(p5o, sumodd, rot1_0); \
2574 dct_long_mac(p1o, p5o, sum17, rot1_1); \
2575 dct_long_mac(p2o, p5o, sum35, rot1_2); \
2576 dct_long_mul(p3o, sum37, rot2_0); \
2577 dct_long_mul(p4o, sum15, rot2_1); \
2578 dct_wadd(sump13o, p1o, p3o); \
2579 dct_wadd(sump24o, p2o, p4o); \
2580 dct_wadd(sump23o, p2o, p3o); \
2581 dct_wadd(sump14o, p1o, p4o); \
2582 dct_long_mac(x4, sump13o, row7, rot3_0); \
2583 dct_long_mac(x5, sump24o, row5, rot3_1); \
2584 dct_long_mac(x6, sump23o, row3, rot3_2); \
2585 dct_long_mac(x7, sump14o, row1, rot3_3); \
2586 dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \
2587 dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \
2588 dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \
2589 dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \
2593 row0 = vld1q_s16(data + 0*8);
2594 row1 = vld1q_s16(data + 1*8);
2595 row2 = vld1q_s16(data + 2*8);
2596 row3 = vld1q_s16(data + 3*8);
2597 row4 = vld1q_s16(data + 4*8);
2598 row5 = vld1q_s16(data + 5*8);
2599 row6 = vld1q_s16(data + 6*8);
2600 row7 = vld1q_s16(data + 7*8);
2603 row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0));
2606 dct_pass(vrshrn_n_s32, 10);
2612#define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; }
2613#define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); }
2614#define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); }
2617 dct_trn16(row0, row1);
2618 dct_trn16(row2, row3);
2619 dct_trn16(row4, row5);
2620 dct_trn16(row6, row7);
2623 dct_trn32(row0, row2);
2624 dct_trn32(row1, row3);
2625 dct_trn32(row4, row6);
2626 dct_trn32(row5, row7);
2629 dct_trn64(row0, row4);
2630 dct_trn64(row1, row5);
2631 dct_trn64(row2, row6);
2632 dct_trn64(row3, row7);
2643 dct_pass(vshrn_n_s32, 16);
2647 uint8x8_t p0 = vqrshrun_n_s16(row0, 1);
2648 uint8x8_t p1 = vqrshrun_n_s16(row1, 1);
2649 uint8x8_t p2 = vqrshrun_n_s16(row2, 1);
2650 uint8x8_t p3 = vqrshrun_n_s16(row3, 1);
2651 uint8x8_t p4 = vqrshrun_n_s16(row4, 1);
2652 uint8x8_t p5 = vqrshrun_n_s16(row5, 1);
2653 uint8x8_t p6 = vqrshrun_n_s16(row6, 1);
2654 uint8x8_t p7 = vqrshrun_n_s16(row7, 1);
2657#define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; }
2658#define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); }
2659#define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); }
2671 dct_trn8_16(p0, p2);
2672 dct_trn8_16(p1, p3);
2673 dct_trn8_16(p4, p6);
2674 dct_trn8_16(p5, p7);
2677 dct_trn8_32(p0, p4);
2678 dct_trn8_32(p1, p5);
2679 dct_trn8_32(p2, p6);
2680 dct_trn8_32(p3, p7);
2683 vst1_u8(out, p0); out += out_stride;
2684 vst1_u8(out, p1); out += out_stride;
2685 vst1_u8(out, p2); out += out_stride;
2686 vst1_u8(out, p3); out += out_stride;
2687 vst1_u8(out, p4); out += out_stride;
2688 vst1_u8(out, p5); out += out_stride;
2689 vst1_u8(out, p6); out += out_stride;
2708#define STBI__MARKER_none 0xff
2716 x = stbi__get8(j->
s);
2719 x = stbi__get8(j->
s);
2725#define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
2742static int stbi__parse_entropy_coded_data(
stbi__jpeg *z)
2744 stbi__jpeg_reset(z);
2749 int n = z->
order[0];
2756 for (j=0; j < h; ++j) {
2757 for (i=0; i < w; ++i) {
2762 if (--z->
todo <= 0) {
2763 if (z->
code_bits < 24) stbi__grow_buffer_unsafe(z);
2767 stbi__jpeg_reset(z);
2778 for (k=0; k < z->
scan_n; ++k) {
2779 int n = z->
order[k];
2794 if (--z->
todo <= 0) {
2795 if (z->
code_bits < 24) stbi__grow_buffer_unsafe(z);
2797 stbi__jpeg_reset(z);
2806 int n = z->
order[0];
2813 for (j=0; j < h; ++j) {
2814 for (i=0; i < w; ++i) {
2817 if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->
huff_dc[z->
img_comp[n].
hd], n))
2821 if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->
huff_ac[ha], z->
fast_ac[ha]))
2825 if (--z->
todo <= 0) {
2826 if (z->
code_bits < 24) stbi__grow_buffer_unsafe(z);
2828 stbi__jpeg_reset(z);
2838 for (k=0; k < z->
scan_n; ++k) {
2839 int n = z->
order[k];
2847 if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->
huff_dc[z->
img_comp[n].
hd], n))
2854 if (--z->
todo <= 0) {
2855 if (z->
code_bits < 24) stbi__grow_buffer_unsafe(z);
2857 stbi__jpeg_reset(z);
2866static void stbi__jpeg_dequantize(
short *data,
stbi__uint16 *dequant)
2869 for (i=0; i < 64; ++i)
2870 data[i] *= dequant[i];
2878 for (n=0; n < z->
s->
img_n; ++n) {
2881 for (j=0; j < h; ++j) {
2882 for (i=0; i < w; ++i) {
2892static int stbi__process_marker(
stbi__jpeg *z,
int m)
2897 return stbi__err(
"expected marker",
"Corrupt JPEG");
2900 if (stbi__get16be(z->
s) != 4)
return stbi__err(
"bad DRI len",
"Corrupt JPEG");
2905 L = stbi__get16be(z->
s)-2;
2907 int q = stbi__get8(z->
s);
2908 int p = q >> 4, sixteen = (p != 0);
2910 if (p != 0 && p != 1)
return stbi__err(
"bad DQT type",
"Corrupt JPEG");
2911 if (t > 3)
return stbi__err(
"bad DQT table",
"Corrupt JPEG");
2913 for (i=0; i < 64; ++i)
2914 z->
dequant[t][stbi__jpeg_dezigzag[i]] = (
stbi__uint16)(sixteen ? stbi__get16be(z->
s) : stbi__get8(z->s));
2915 L -= (sixteen ? 129 : 65);
2920 L = stbi__get16be(z->
s)-2;
2923 int sizes[16],i,n=0;
2924 int q = stbi__get8(z->
s);
2927 if (tc > 1 || th > 3)
return stbi__err(
"bad DHT header",
"Corrupt JPEG");
2928 for (i=0; i < 16; ++i) {
2929 sizes[i] = stbi__get8(z->
s);
2932 if(n > 256)
return stbi__err(
"bad DHT header",
"Corrupt JPEG");
2935 if (!stbi__build_huffman(z->
huff_dc+th, sizes))
return 0;
2938 if (!stbi__build_huffman(z->
huff_ac+th, sizes))
return 0;
2941 for (i=0; i < n; ++i)
2942 v[i] = stbi__get8(z->
s);
2951 if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
2952 L = stbi__get16be(z->
s);
2955 return stbi__err(
"bad COM len",
"Corrupt JPEG");
2957 return stbi__err(
"bad APP len",
"Corrupt JPEG");
2961 if (m == 0xE0 && L >= 5) {
2962 static const unsigned char tag[5] = {
'J',
'F',
'I',
'F',
'\0'};
2965 for (i=0; i < 5; ++i)
2966 if (stbi__get8(z->
s) != tag[i])
2971 }
else if (m == 0xEE && L >= 12) {
2972 static const unsigned char tag[6] = {
'A',
'd',
'o',
'b',
'e',
'\0'};
2975 for (i=0; i < 6; ++i)
2976 if (stbi__get8(z->
s) != tag[i])
2981 stbi__get16be(z->
s);
2982 stbi__get16be(z->
s);
2988 stbi__skip(z->
s, L);
2992 return stbi__err(
"unknown marker",
"Corrupt JPEG");
2996static int stbi__process_scan_header(
stbi__jpeg *z)
2999 int Ls = stbi__get16be(z->
s);
3002 if (Ls != 6+2*z->
scan_n)
return stbi__err(
"bad SOS len",
"Corrupt JPEG");
3003 for (i=0; i < z->
scan_n; ++i) {
3004 int id = stbi__get8(z->
s), which;
3005 int q = stbi__get8(z->
s);
3006 for (which = 0; which < z->
s->
img_n; ++which)
3009 if (which == z->
s->
img_n)
return 0;
3012 z->
order[i] = which;
3019 aa = stbi__get8(z->
s);
3024 return stbi__err(
"bad SOS",
"Corrupt JPEG");
3035static int stbi__free_jpeg_components(
stbi__jpeg *z,
int ncomp,
int why)
3038 for (i=0; i < ncomp; ++i) {
3057static int stbi__process_frame_header(
stbi__jpeg *z,
int scan)
3060 int Lf,p,i,q, h_max=1,v_max=1,c;
3061 Lf = stbi__get16be(s);
if (Lf < 11)
return stbi__err(
"bad SOF len",
"Corrupt JPEG");
3062 p = stbi__get8(s);
if (p != 8)
return stbi__err(
"only 8-bit",
"JPEG format not supported: 8-bit only");
3063 s->
img_y = stbi__get16be(s);
if (s->
img_y == 0)
return stbi__err(
"no header height",
"JPEG format not supported: delayed height");
3064 s->
img_x = stbi__get16be(s);
if (s->
img_x == 0)
return stbi__err(
"0 width",
"Corrupt JPEG");
3068 if (c != 3 && c != 1 && c != 4)
return stbi__err(
"bad component count",
"Corrupt JPEG");
3070 for (i=0; i < c; ++i) {
3075 if (Lf != 8+3*s->
img_n)
return stbi__err(
"bad SOF len",
"Corrupt JPEG");
3078 for (i=0; i < s->
img_n; ++i) {
3079 static const unsigned char rgb[3] = {
'R',
'G',
'B' };
3091 if (!stbi__mad3sizes_valid(s->
img_x, s->
img_y, s->
img_n, 0))
return stbi__err(
"too large",
"Image too large to decode");
3093 for (i=0; i < s->
img_n; ++i) {
3100 for (i=0; i < s->
img_n; ++i) {
3114 for (i=0; i < s->
img_n; ++i) {
3132 return stbi__free_jpeg_components(z, i+1,
stbi__err(
"outofmem",
"Out of memory"));
3141 return stbi__free_jpeg_components(z, i+1,
stbi__err(
"outofmem",
"Out of memory"));
3150#define stbi__DNL(x) ((x) == 0xdc)
3151#define stbi__SOI(x) ((x) == 0xd8)
3152#define stbi__EOI(x) ((x) == 0xd9)
3153#define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2)
3154#define stbi__SOS(x) ((x) == 0xda)
3156#define stbi__SOF_progressive(x) ((x) == 0xc2)
3158static int stbi__decode_jpeg_header(
stbi__jpeg *z,
int scan)
3164 m = stbi__get_marker(z);
3167 m = stbi__get_marker(z);
3169 if (!stbi__process_marker(z,m))
return 0;
3170 m = stbi__get_marker(z);
3173 if (stbi__at_eof(z->
s))
return stbi__err(
"no SOF",
"Corrupt JPEG");
3174 m = stbi__get_marker(z);
3178 if (!stbi__process_frame_header(z, scan))
return 0;
3186 while (!stbi__at_eof(j->
s)) {
3190 x = stbi__get8(j->
s);
3191 if (x != 0x00 && x != 0xff) {
3205static int stbi__decode_jpeg_image(
stbi__jpeg *j)
3208 for (m = 0; m < 4; m++) {
3214 m = stbi__get_marker(j);
3217 if (!stbi__process_scan_header(j))
return 0;
3218 if (!stbi__parse_entropy_coded_data(j))
return 0;
3220 j->
marker = stbi__skip_jpeg_junk_at_end(j);
3223 m = stbi__get_marker(j);
3225 m = stbi__get_marker(j);
3227 int Ld = stbi__get16be(j->
s);
3229 if (Ld != 4)
return stbi__err(
"bad DNL len",
"Corrupt JPEG");
3230 if (NL != j->
s->
img_y)
return stbi__err(
"bad DNL height",
"Corrupt JPEG");
3231 m = stbi__get_marker(j);
3233 if (!stbi__process_marker(j, m))
return 1;
3234 m = stbi__get_marker(j);
3238 stbi__jpeg_finish(j);
3247#define stbi__div4(x) ((stbi_uc) ((x) >> 2))
3263 for (i=0; i < w; ++i)
3264 out[i] =
stbi__div4(3*in_near[i] + in_far[i] + 2);
3276 out[0] = out[1] = input[0];
3281 out[1] =
stbi__div4(input[0]*3 + input[1] + 2);
3282 for (i=1; i < w-1; ++i) {
3283 int n = 3*input[i]+2;
3287 out[i*2+0] =
stbi__div4(input[w-2]*3 + input[w-1] + 2);
3288 out[i*2+1] = input[w-1];
3296#define stbi__div16(x) ((stbi_uc) ((x) >> 4))
3303 out[0] = out[1] =
stbi__div4(3*in_near[0] + in_far[0] + 2);
3307 t1 = 3*in_near[0] + in_far[0];
3309 for (i=1; i < w; ++i) {
3311 t1 = 3*in_near[i]+in_far[i];
3322#if defined(STBI_SSE2) || defined(STBI_NEON)
3329 out[0] = out[1] =
stbi__div4(3*in_near[0] + in_far[0] + 2);
3333 t1 = 3*in_near[0] + in_far[0];
3337 for (; i < ((w-1) & ~7); i += 8) {
3338#if defined(STBI_SSE2)
3341 __m128i zero = _mm_setzero_si128();
3342 __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i));
3343 __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i));
3344 __m128i farw = _mm_unpacklo_epi8(farb, zero);
3345 __m128i nearw = _mm_unpacklo_epi8(nearb, zero);
3346 __m128i diff = _mm_sub_epi16(farw, nearw);
3347 __m128i nears = _mm_slli_epi16(nearw, 2);
3348 __m128i curr = _mm_add_epi16(nears, diff);
3355 __m128i prv0 = _mm_slli_si128(curr, 2);
3356 __m128i nxt0 = _mm_srli_si128(curr, 2);
3357 __m128i prev = _mm_insert_epi16(prv0, t1, 0);
3358 __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7);
3364 __m128i bias = _mm_set1_epi16(8);
3365 __m128i curs = _mm_slli_epi16(curr, 2);
3366 __m128i prvd = _mm_sub_epi16(prev, curr);
3367 __m128i nxtd = _mm_sub_epi16(next, curr);
3368 __m128i curb = _mm_add_epi16(curs, bias);
3369 __m128i even = _mm_add_epi16(prvd, curb);
3370 __m128i odd = _mm_add_epi16(nxtd, curb);
3373 __m128i int0 = _mm_unpacklo_epi16(even, odd);
3374 __m128i int1 = _mm_unpackhi_epi16(even, odd);
3375 __m128i de0 = _mm_srli_epi16(int0, 4);
3376 __m128i de1 = _mm_srli_epi16(int1, 4);
3379 __m128i outv = _mm_packus_epi16(de0, de1);
3380 _mm_storeu_si128((__m128i *) (out + i*2), outv);
3381#elif defined(STBI_NEON)
3384 uint8x8_t farb = vld1_u8(in_far + i);
3385 uint8x8_t nearb = vld1_u8(in_near + i);
3386 int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb));
3387 int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2));
3388 int16x8_t curr = vaddq_s16(nears, diff);
3395 int16x8_t prv0 = vextq_s16(curr, curr, 7);
3396 int16x8_t nxt0 = vextq_s16(curr, curr, 1);
3397 int16x8_t prev = vsetq_lane_s16(t1, prv0, 0);
3398 int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7);
3404 int16x8_t curs = vshlq_n_s16(curr, 2);
3405 int16x8_t prvd = vsubq_s16(prev, curr);
3406 int16x8_t nxtd = vsubq_s16(next, curr);
3407 int16x8_t even = vaddq_s16(curs, prvd);
3408 int16x8_t odd = vaddq_s16(curs, nxtd);
3412 o.val[0] = vqrshrun_n_s16(even, 4);
3413 o.val[1] = vqrshrun_n_s16(odd, 4);
3414 vst2_u8(out + i*2, o);
3418 t1 = 3*in_near[i+7] + in_far[i+7];
3422 t1 = 3*in_near[i] + in_far[i];
3425 for (++i; i < w; ++i) {
3427 t1 = 3*in_near[i]+in_far[i];
3444 for (i=0; i < w; ++i)
3445 for (j=0; j < hs; ++j)
3446 out[i*hs+j] = in_near[i];
3452#define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8)
3456 for (i=0; i < count; ++i) {
3457 int y_fixed = (y[i] << 20) + (1<<19);
3459 int cr = pcr[i] - 128;
3460 int cb = pcb[i] - 128;
3467 if ((
unsigned) r > 255) {
if (r < 0) r = 0;
else r = 255; }
3468 if ((
unsigned) g > 255) {
if (g < 0) g = 0;
else g = 255; }
3469 if ((
unsigned) b > 255) {
if (b < 0) b = 0;
else b = 255; }
3478#if defined(STBI_SSE2) || defined(STBI_NEON)
3489 __m128i signflip = _mm_set1_epi8(-0x80);
3490 __m128i cr_const0 = _mm_set1_epi16( (
short) ( 1.40200f*4096.0f+0.5f));
3491 __m128i cr_const1 = _mm_set1_epi16( - (
short) ( 0.71414f*4096.0f+0.5f));
3492 __m128i cb_const0 = _mm_set1_epi16( - (
short) ( 0.34414f*4096.0f+0.5f));
3493 __m128i cb_const1 = _mm_set1_epi16( (
short) ( 1.77200f*4096.0f+0.5f));
3494 __m128i y_bias = _mm_set1_epi8((
char) (
unsigned char) 128);
3495 __m128i xw = _mm_set1_epi16(255);
3497 for (; i+7 < count; i += 8) {
3499 __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i));
3500 __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i));
3501 __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i));
3502 __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip);
3503 __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip);
3506 __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes);
3507 __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased);
3508 __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased);
3511 __m128i yws = _mm_srli_epi16(yw, 4);
3512 __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw);
3513 __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw);
3514 __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1);
3515 __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1);
3516 __m128i rws = _mm_add_epi16(cr0, yws);
3517 __m128i gwt = _mm_add_epi16(cb0, yws);
3518 __m128i bws = _mm_add_epi16(yws, cb1);
3519 __m128i gws = _mm_add_epi16(gwt, cr1);
3522 __m128i rw = _mm_srai_epi16(rws, 4);
3523 __m128i bw = _mm_srai_epi16(bws, 4);
3524 __m128i gw = _mm_srai_epi16(gws, 4);
3527 __m128i brb = _mm_packus_epi16(rw, bw);
3528 __m128i gxb = _mm_packus_epi16(gw, xw);
3531 __m128i t0 = _mm_unpacklo_epi8(brb, gxb);
3532 __m128i t1 = _mm_unpackhi_epi8(brb, gxb);
3533 __m128i o0 = _mm_unpacklo_epi16(t0, t1);
3534 __m128i o1 = _mm_unpackhi_epi16(t0, t1);
3537 _mm_storeu_si128((__m128i *) (out + 0), o0);
3538 _mm_storeu_si128((__m128i *) (out + 16), o1);
3548 uint8x8_t signflip = vdup_n_u8(0x80);
3549 int16x8_t cr_const0 = vdupq_n_s16( (
short) ( 1.40200f*4096.0f+0.5f));
3550 int16x8_t cr_const1 = vdupq_n_s16( - (
short) ( 0.71414f*4096.0f+0.5f));
3551 int16x8_t cb_const0 = vdupq_n_s16( - (
short) ( 0.34414f*4096.0f+0.5f));
3552 int16x8_t cb_const1 = vdupq_n_s16( (
short) ( 1.77200f*4096.0f+0.5f));
3554 for (; i+7 < count; i += 8) {
3556 uint8x8_t y_bytes = vld1_u8(y + i);
3557 uint8x8_t cr_bytes = vld1_u8(pcr + i);
3558 uint8x8_t cb_bytes = vld1_u8(pcb + i);
3559 int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip));
3560 int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip));
3563 int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4));
3564 int16x8_t crw = vshll_n_s8(cr_biased, 7);
3565 int16x8_t cbw = vshll_n_s8(cb_biased, 7);
3568 int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0);
3569 int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0);
3570 int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1);
3571 int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1);
3572 int16x8_t rws = vaddq_s16(yws, cr0);
3573 int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1);
3574 int16x8_t bws = vaddq_s16(yws, cb1);
3578 o.val[0] = vqrshrun_n_s16(rws, 4);
3579 o.val[1] = vqrshrun_n_s16(gws, 4);
3580 o.val[2] = vqrshrun_n_s16(bws, 4);
3581 o.val[3] = vdup_n_u8(255);
3590 for (; i < count; ++i) {
3591 int y_fixed = (y[i] << 20) + (1<<19);
3593 int cr = pcr[i] - 128;
3594 int cb = pcb[i] - 128;
3601 if ((
unsigned) r > 255) {
if (r < 0) r = 0;
else r = 255; }
3602 if ((
unsigned) g > 255) {
if (g < 0) g = 0;
else g = 255; }
3603 if ((
unsigned) b > 255) {
if (b < 0) b = 0;
else b = 255; }
3621 if (stbi__sse2_available()) {
3638 stbi__free_jpeg_components(j, j->
s->
img_n, 0);
3654 unsigned int t = x*y + 128;
3655 return (
stbi_uc) ((t + (t >>8)) >> 8);
3658static stbi_uc *load_jpeg_image(
stbi__jpeg *z,
int *out_x,
int *out_y,
int *comp,
int req_comp)
3660 int n, decode_n, is_rgb;
3664 if (req_comp < 0 || req_comp > 4)
return stbi__errpuc(
"bad req_comp",
"Internal error");
3667 if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z);
return NULL; }
3670 n = req_comp ? req_comp : z->
s->
img_n >= 3 ? 3 : 1;
3674 if (z->
s->
img_n == 3 && n < 3 && !is_rgb)
3681 if (decode_n <= 0) { stbi__cleanup_jpeg(z);
return NULL; }
3688 stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL };
3692 for (k=0; k < decode_n; ++k) {
3707 if (r->
hs == 1 && r->
vs == 1) r->
resample = resample_row_1;
3708 else if (r->
hs == 1 && r->
vs == 2) r->
resample = stbi__resample_row_v_2;
3709 else if (r->
hs == 2 && r->
vs == 1) r->
resample = stbi__resample_row_h_2;
3711 else r->
resample = stbi__resample_row_generic;
3716 if (!output) { stbi__cleanup_jpeg(z);
return stbi__errpuc(
"outofmem",
"Out of memory"); }
3719 for (j=0; j < z->
s->
img_y; ++j) {
3721 for (k=0; k < decode_n; ++k) {
3723 int y_bot = r->
ystep >= (r->
vs >> 1);
3725 y_bot ? r->
line1 : r->line0,
3726 y_bot ? r->line0 : r->line1,
3739 for (i=0; i < z->
s->
img_x; ++i) {
3741 out[1] = coutput[1][i];
3742 out[2] = coutput[2][i];
3749 }
else if (z->
s->
img_n == 4) {
3751 for (i=0; i < z->
s->
img_x; ++i) {
3753 out[0] = stbi__blinn_8x8(coutput[0][i], m);
3754 out[1] = stbi__blinn_8x8(coutput[1][i], m);
3755 out[2] = stbi__blinn_8x8(coutput[2][i], m);
3761 for (i=0; i < z->
s->
img_x; ++i) {
3763 out[0] = stbi__blinn_8x8(255 - out[0], m);
3764 out[1] = stbi__blinn_8x8(255 - out[1], m);
3765 out[2] = stbi__blinn_8x8(255 - out[2], m);
3772 for (i=0; i < z->
s->
img_x; ++i) {
3773 out[0] = out[1] = out[2] = y[i];
3780 for (i=0; i < z->
s->
img_x; ++i)
3781 *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);
3783 for (i=0; i < z->
s->
img_x; ++i, out += 2) {
3784 out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);
3789 for (i=0; i < z->
s->
img_x; ++i) {
3791 stbi_uc r = stbi__blinn_8x8(coutput[0][i], m);
3792 stbi_uc g = stbi__blinn_8x8(coutput[1][i], m);
3793 stbi_uc b = stbi__blinn_8x8(coutput[2][i], m);
3794 out[0] = stbi__compute_y(r, g, b);
3799 for (i=0; i < z->
s->
img_x; ++i) {
3800 out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]);
3807 for (i=0; i < z->
s->
img_x; ++i) out[i] = y[i];
3809 for (i=0; i < z->
s->
img_x; ++i) { *out++ = y[i]; *out++ = 255; }
3813 stbi__cleanup_jpeg(z);
3816 if (comp) *comp = z->
s->
img_n >= 3 ? 3 : 1;
3823 unsigned char* result;
3825 if (!j)
return stbi__errpuc(
"outofmem",
"Out of memory");
3829 stbi__setup_jpeg(j);
3830 result = load_jpeg_image(j, x,y,comp,req_comp);
3839 if (!j)
return stbi__err(
"outofmem",
"Out of memory");
3842 stbi__setup_jpeg(j);
3849static int stbi__jpeg_info_raw(
stbi__jpeg *j,
int *x,
int *y,
int *comp)
3852 stbi__rewind( j->
s );
3857 if (comp) *comp = j->
s->
img_n >= 3 ? 3 : 1;
3861static int stbi__jpeg_info(
stbi__context *s,
int *x,
int *y,
int *comp)
3865 if (!j)
return stbi__err(
"outofmem",
"Out of memory");
3868 result = stbi__jpeg_info_raw(j, x, y, comp);
3882#define STBI__ZFAST_BITS 9
3883#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
3884#define STBI__ZNSYMS 288
3900 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
3901 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
3902 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
3903 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
3907stbi_inline static int stbi__bit_reverse(
int v,
int bits)
3912 return stbi__bitreverse16(v) >> (16-bits);
3918 int code, next_code[16], sizes[17];
3921 memset(sizes, 0,
sizeof(sizes));
3922 memset(z->
fast, 0,
sizeof(z->
fast));
3923 for (i=0; i < num; ++i)
3924 ++sizes[sizelist[i]];
3926 for (i=1; i < 16; ++i)
3927 if (sizes[i] > (1 << i))
3928 return stbi__err(
"bad sizes",
"Corrupt PNG");
3930 for (i=1; i < 16; ++i) {
3931 next_code[i] = code;
3934 code = (code + sizes[i]);
3936 if (code-1 >= (1 << i))
return stbi__err(
"bad codelengths",
"Corrupt PNG");
3937 z->
maxcode[i] = code << (16-i);
3942 for (i=0; i < num; ++i) {
3943 int s = sizelist[i];
3950 int j = stbi__bit_reverse(next_code[s],s);
3990 return stbi__zeof(z) ? 0 : *z->
zbuffer++;
4008 if (z->
num_bits < n) stbi__fill_bits(z);
4022 if (k < z->maxcode[s])
4024 if (s >= 16)
return -1;
4028 if (z->
size[b] != s)
return -1;
4038 if (stbi__zeof(a)) {
4061 return stbi__zhuffman_decode_slowpath(a, z);
4064static int stbi__zexpand(
stbi__zbuf *z,
char *zout,
int n)
4067 unsigned int cur, limit, old_limit;
4072 if (UINT_MAX - cur < (
unsigned) n)
return stbi__err(
"outofmem",
"Out of memory");
4073 while (cur + n > limit) {
4074 if(limit > UINT_MAX / 2)
return stbi__err(
"outofmem",
"Out of memory");
4079 if (q == NULL)
return stbi__err(
"outofmem",
"Out of memory");
4086static const int stbi__zlength_base[31] = {
4087 3,4,5,6,7,8,9,10,11,13,
4088 15,17,19,23,27,31,35,43,51,59,
4089 67,83,99,115,131,163,195,227,258,0,0 };
4091static const int stbi__zlength_extra[31]=
4092{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
4094static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
4095257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
4097static const int stbi__zdist_extra[32] =
4098{ 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
4100static int stbi__parse_huffman_block(
stbi__zbuf *a)
4102 char *zout = a->
zout;
4104 int z = stbi__zhuffman_decode(a, &a->
z_length);
4106 if (z < 0)
return stbi__err(
"bad huffman code",
"Corrupt PNG");
4108 if (!stbi__zexpand(a, zout, 1))
return 0;
4122 return stbi__err(
"unexpected end",
"Corrupt PNG");
4126 if (z >= 286)
return stbi__err(
"bad huffman code",
"Corrupt PNG");
4128 len = stbi__zlength_base[z];
4129 if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
4130 z = stbi__zhuffman_decode(a, &a->
z_distance);
4131 if (z < 0 || z >= 30)
return stbi__err(
"bad huffman code",
"Corrupt PNG");
4132 dist = stbi__zdist_base[z];
4133 if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
4136 if (!stbi__zexpand(a, zout, len))
return 0;
4139 p = (
stbi_uc *) (zout - dist);
4142 if (len) {
do *zout++ = v;
while (--len); }
4144 if (len) {
do *zout++ = *p++;
while (--len); }
4150static int stbi__compute_huffman_codes(
stbi__zbuf *a)
4152 static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
4158 int hlit = stbi__zreceive(a,5) + 257;
4159 int hdist = stbi__zreceive(a,5) + 1;
4160 int hclen = stbi__zreceive(a,4) + 4;
4161 int ntot = hlit + hdist;
4163 memset(codelength_sizes, 0,
sizeof(codelength_sizes));
4164 for (i=0; i < hclen; ++i) {
4165 int s = stbi__zreceive(a,3);
4166 codelength_sizes[length_dezigzag[i]] = (
stbi_uc) s;
4168 if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19))
return 0;
4172 int c = stbi__zhuffman_decode(a, &z_codelength);
4173 if (c < 0 || c >= 19)
return stbi__err(
"bad codelengths",
"Corrupt PNG");
4179 c = stbi__zreceive(a,2)+3;
4180 if (n == 0)
return stbi__err(
"bad codelengths",
"Corrupt PNG");
4181 fill = lencodes[n-1];
4182 }
else if (c == 17) {
4183 c = stbi__zreceive(a,3)+3;
4184 }
else if (c == 18) {
4185 c = stbi__zreceive(a,7)+11;
4187 return stbi__err(
"bad codelengths",
"Corrupt PNG");
4189 if (ntot - n < c)
return stbi__err(
"bad codelengths",
"Corrupt PNG");
4190 memset(lencodes+n, fill, c);
4194 if (n != ntot)
return stbi__err(
"bad codelengths",
"Corrupt PNG");
4195 if (!stbi__zbuild_huffman(&a->
z_length, lencodes, hlit))
return 0;
4196 if (!stbi__zbuild_huffman(&a->
z_distance, lencodes+hlit, hdist))
return 0;
4200static int stbi__parse_uncompressed_block(
stbi__zbuf *a)
4205 stbi__zreceive(a, a->
num_bits & 7);
4216 header[k++] = stbi__zget8(a);
4217 len = header[1] * 256 + header[0];
4218 nlen = header[3] * 256 + header[2];
4219 if (nlen != (len ^ 0xffff))
return stbi__err(
"zlib corrupt",
"Corrupt PNG");
4222 if (!stbi__zexpand(a, a->
zout, len))
return 0;
4229static int stbi__parse_zlib_header(
stbi__zbuf *a)
4231 int cmf = stbi__zget8(a);
4234 int flg = stbi__zget8(a);
4235 if (stbi__zeof(a))
return stbi__err(
"bad zlib header",
"Corrupt PNG");
4236 if ((cmf*256+flg) % 31 != 0)
return stbi__err(
"bad zlib header",
"Corrupt PNG");
4237 if (flg & 32)
return stbi__err(
"no preset dict",
"Corrupt PNG");
4238 if (cm != 8)
return stbi__err(
"bad compression",
"Corrupt PNG");
4245 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4246 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4247 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4248 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4249 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
4250 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
4251 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
4252 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
4253 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8
4255static const stbi_uc stbi__zdefault_distance[32] =
4257 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
4272static int stbi__parse_zlib(
stbi__zbuf *a,
int parse_header)
4276 if (!stbi__parse_zlib_header(a))
return 0;
4281 final = stbi__zreceive(a,1);
4282 type = stbi__zreceive(a,2);
4284 if (!stbi__parse_uncompressed_block(a))
return 0;
4285 }
else if (type == 3) {
4291 if (!stbi__zbuild_huffman(&a->
z_distance, stbi__zdefault_distance, 32))
return 0;
4293 if (!stbi__compute_huffman_codes(a))
return 0;
4295 if (!stbi__parse_huffman_block(a))
return 0;
4301static int stbi__do_zlib(
stbi__zbuf *a,
char *obuf,
int olen,
int exp,
int parse_header)
4308 return stbi__parse_zlib(a, parse_header);
4311STBIDEF char *stbi_zlib_decode_malloc_guesssize(
const char *buffer,
int len,
int initial_size,
int *outlen)
4314 char *p = (
char *) stbi__malloc(initial_size);
4315 if (p == NULL)
return NULL;
4318 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
4327STBIDEF char *stbi_zlib_decode_malloc(
char const *buffer,
int len,
int *outlen)
4329 return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
4332STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(
const char *buffer,
int len,
int initial_size,
int *outlen,
int parse_header)
4335 char *p = (
char *) stbi__malloc(initial_size);
4336 if (p == NULL)
return NULL;
4339 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
4348STBIDEF int stbi_zlib_decode_buffer(
char *obuffer,
int olen,
char const *ibuffer,
int ilen)
4353 if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
4359STBIDEF char *stbi_zlib_decode_noheader_malloc(
char const *buffer,
int len,
int *outlen)
4362 char *p = (
char *) stbi__malloc(16384);
4363 if (p == NULL)
return NULL;
4366 if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
4375STBIDEF int stbi_zlib_decode_noheader_buffer(
char *obuffer,
int olen,
const char *ibuffer,
int ilen)
4380 if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
4405 c.
length = stbi__get32be(s);
4406 c.
type = stbi__get32be(s);
4412 static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };
4414 for (i=0; i < 8; ++i)
4415 if (stbi__get8(s) != png_sig[i])
return stbi__err(
"bad png sig",
"Not a PNG");
4437static stbi_uc first_row_filter[5] =
4446static int stbi__paeth(
int a,
int b,
int c)
4451 int thresh = c*3 - (a + b);
4452 int lo = a < b ? a : b;
4453 int hi = a < b ? b : a;
4454 int t0 = (hi <= thresh) ? lo : c;
4455 int t1 = (thresh <= lo) ? hi : t0;
4459static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };
4469 for (i=x-1; i >= 0; --i) {
4471 dest[i*2+0] = src[i];
4475 for (i=x-1; i >= 0; --i) {
4477 dest[i*4+2] = src[i*3+2];
4478 dest[i*4+1] = src[i*3+1];
4479 dest[i*4+0] = src[i*3+0];
4487 int bytes = (depth == 16 ? 2 : 1);
4494 int img_n = s->
img_n;
4496 int output_bytes = out_n*bytes;
4497 int filter_bytes = img_n*bytes;
4501 a->
out = (
stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0);
4502 if (!a->
out)
return stbi__err(
"outofmem",
"Out of memory");
4506 if (!stbi__mad3sizes_valid(img_n, x, depth, 7))
return stbi__err(
"too large",
"Corrupt PNG");
4507 img_width_bytes = (((img_n * x * depth) + 7) >> 3);
4508 if (!stbi__mad2sizes_valid(img_width_bytes, y, img_width_bytes))
return stbi__err(
"too large",
"Corrupt PNG");
4509 img_len = (img_width_bytes + 1) * y;
4514 if (raw_len < img_len)
return stbi__err(
"not enough pixels",
"Corrupt PNG");
4517 filter_buf = (
stbi_uc *) stbi__malloc_mad2(img_width_bytes, 2, 0);
4518 if (!filter_buf)
return stbi__err(
"outofmem",
"Out of memory");
4523 width = img_width_bytes;
4526 for (j=0; j < y; ++j) {
4528 stbi_uc *cur = filter_buf + (j & 1)*img_width_bytes;
4529 stbi_uc *prior = filter_buf + (~j & 1)*img_width_bytes;
4531 int nk = width * filter_bytes;
4532 int filter = *
raw++;
4536 all_ok =
stbi__err(
"invalid filter",
"Corrupt PNG");
4541 if (j == 0) filter = first_row_filter[filter];
4546 memcpy(cur,
raw, nk);
4549 memcpy(cur,
raw, filter_bytes);
4550 for (k = filter_bytes; k < nk; ++k)
4554 for (k = 0; k < nk; ++k)
4558 for (k = 0; k < filter_bytes; ++k)
4560 for (k = filter_bytes; k < nk; ++k)
4564 for (k = 0; k < filter_bytes; ++k)
4566 for (k = filter_bytes; k < nk; ++k)
4567 cur[k] =
STBI__BYTECAST(
raw[k] + stbi__paeth(cur[k-filter_bytes], prior[k], prior[k-filter_bytes]));
4570 memcpy(cur,
raw, filter_bytes);
4571 for (k = filter_bytes; k < nk; ++k)
4580 stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1;
4588 for (i=0; i < nsmp; ++i) {
4589 if ((i & 1) == 0) inb = *in++;
4590 *out++ = scale * (inb >> 4);
4593 }
else if (depth == 2) {
4594 for (i=0; i < nsmp; ++i) {
4595 if ((i & 3) == 0) inb = *in++;
4596 *out++ = scale * (inb >> 6);
4601 for (i=0; i < nsmp; ++i) {
4602 if ((i & 7) == 0) inb = *in++;
4603 *out++ = scale * (inb >> 7);
4610 stbi__create_png_alpha_expand8(dest, dest, x, img_n);
4611 }
else if (depth == 8) {
4613 memcpy(dest, cur, x*img_n);
4615 stbi__create_png_alpha_expand8(dest, cur, x, img_n);
4616 }
else if (depth == 16) {
4621 if (img_n == out_n) {
4622 for (i = 0; i < nsmp; ++i, ++dest16, cur += 2)
4623 *dest16 = (cur[0] << 8) | cur[1];
4627 for (i = 0; i < x; ++i, dest16 += 2, cur += 2) {
4628 dest16[0] = (cur[0] << 8) | cur[1];
4633 for (i = 0; i < x; ++i, dest16 += 4, cur += 6) {
4634 dest16[0] = (cur[0] << 8) | cur[1];
4635 dest16[1] = (cur[2] << 8) | cur[3];
4636 dest16[2] = (cur[4] << 8) | cur[5];
4645 if (!all_ok)
return 0;
4650static int stbi__create_png_image(
stbi__png *a,
stbi_uc *image_data,
stbi__uint32 image_data_len,
int out_n,
int depth,
int color,
int interlaced)
4652 int bytes = (depth == 16 ? 2 : 1);
4653 int out_bytes = out_n * bytes;
4657 return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->
s->
img_x, a->
s->
img_y, depth, color);
4661 if (!
final)
return stbi__err(
"outofmem",
"Out of memory");
4662 for (p=0; p < 7; ++p) {
4663 int xorig[] = { 0,4,0,2,0,1,0 };
4664 int yorig[] = { 0,0,4,0,2,0,1 };
4665 int xspc[] = { 8,8,4,4,2,2,1 };
4666 int yspc[] = { 8,8,8,4,4,2,2 };
4669 x = (a->
s->
img_x - xorig[p] + xspc[p]-1) / xspc[p];
4670 y = (a->
s->
img_y - yorig[p] + yspc[p]-1) / yspc[p];
4673 if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {
4677 for (j=0; j < y; ++j) {
4678 for (i=0; i < x; ++i) {
4679 int out_y = j*yspc[p]+yorig[p];
4680 int out_x = i*xspc[p]+xorig[p];
4681 memcpy(
final + out_y*a->
s->
img_x*out_bytes + out_x*out_bytes,
4682 a->
out + (j*x+i)*out_bytes, out_bytes);
4686 image_data += img_len;
4687 image_data_len -= img_len;
4695static int stbi__compute_transparency(
stbi__png *z,
stbi_uc tc[3],
int out_n)
4706 for (i=0; i < pixel_count; ++i) {
4707 p[1] = (p[0] == tc[0] ? 0 : 255);
4711 for (i=0; i < pixel_count; ++i) {
4712 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
4731 for (i = 0; i < pixel_count; ++i) {
4732 p[1] = (p[0] == tc[0] ? 0 : 65535);
4736 for (i = 0; i < pixel_count; ++i) {
4737 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
4745static int stbi__expand_png_palette(
stbi__png *a,
stbi_uc *palette,
int len,
int pal_img_n)
4750 p = (
stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0);
4751 if (p == NULL)
return stbi__err(
"outofmem",
"Out of memory");
4756 if (pal_img_n == 3) {
4757 for (i=0; i < pixel_count; ++i) {
4760 p[1] = palette[n+1];
4761 p[2] = palette[n+2];
4765 for (i=0; i < pixel_count; ++i) {
4768 p[1] = palette[n+1];
4769 p[2] = palette[n+2];
4770 p[3] = palette[n+3];
4782static int stbi__unpremultiply_on_load_global = 0;
4783static int stbi__de_iphone_flag_global = 0;
4785STBIDEF void stbi_set_unpremultiply_on_load(
int flag_true_if_should_unpremultiply)
4787 stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply;
4790STBIDEF void stbi_convert_iphone_png_to_rgb(
int flag_true_if_should_convert)
4792 stbi__de_iphone_flag_global = flag_true_if_should_convert;
4795#ifndef STBI_THREAD_LOCAL
4796#define stbi__unpremultiply_on_load stbi__unpremultiply_on_load_global
4797#define stbi__de_iphone_flag stbi__de_iphone_flag_global
4799static STBI_THREAD_LOCAL
int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set;
4800static STBI_THREAD_LOCAL
int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set;
4802STBIDEF void stbi_set_unpremultiply_on_load_thread(
int flag_true_if_should_unpremultiply)
4804 stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply;
4805 stbi__unpremultiply_on_load_set = 1;
4808STBIDEF void stbi_convert_iphone_png_to_rgb_thread(
int flag_true_if_should_convert)
4810 stbi__de_iphone_flag_local = flag_true_if_should_convert;
4811 stbi__de_iphone_flag_set = 1;
4814#define stbi__unpremultiply_on_load (stbi__unpremultiply_on_load_set \
4815 ? stbi__unpremultiply_on_load_local \
4816 : stbi__unpremultiply_on_load_global)
4817#define stbi__de_iphone_flag (stbi__de_iphone_flag_set \
4818 ? stbi__de_iphone_flag_local \
4819 : stbi__de_iphone_flag_global)
4822static void stbi__de_iphone(
stbi__png *z)
4829 for (i=0; i < pixel_count; ++i) {
4839 for (i=0; i < pixel_count; ++i) {
4844 p[0] = (p[2] * 255 + half) / a;
4845 p[1] = (p[1] * 255 + half) / a;
4846 p[2] = ( t * 255 + half) / a;
4855 for (i=0; i < pixel_count; ++i) {
4865#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d))
4867static int stbi__parse_png_file(
stbi__png *z,
int scan,
int req_comp)
4869 stbi_uc palette[1024], pal_img_n=0;
4870 stbi_uc has_trans=0, tc[3]={0};
4873 int first=1,k,interlace=0, color=0, is_iphone=0;
4880 if (!stbi__check_png_header(s))
return 0;
4893 if (!first)
return stbi__err(
"multiple IHDR",
"Corrupt PNG");
4896 s->
img_x = stbi__get32be(s);
4897 s->
img_y = stbi__get32be(s);
4900 z->
depth = stbi__get8(s);
if (z->
depth != 1 && z->
depth != 2 && z->
depth != 4 && z->
depth != 8 && z->
depth != 16)
return stbi__err(
"1/2/4/8/16-bit only",
"PNG not supported: 1/2/4/8/16-bit only");
4901 color = stbi__get8(s);
if (color > 6)
return stbi__err(
"bad ctype",
"Corrupt PNG");
4902 if (color == 3 && z->
depth == 16)
return stbi__err(
"bad ctype",
"Corrupt PNG");
4903 if (color == 3) pal_img_n = 3;
else if (color & 1)
return stbi__err(
"bad ctype",
"Corrupt PNG");
4904 comp = stbi__get8(s);
if (comp)
return stbi__err(
"bad comp method",
"Corrupt PNG");
4905 filter= stbi__get8(s);
if (filter)
return stbi__err(
"bad filter method",
"Corrupt PNG");
4906 interlace = stbi__get8(s);
if (interlace>1)
return stbi__err(
"bad interlace method",
"Corrupt PNG");
4909 s->
img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
4922 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
4925 if (pal_len * 3 != c.
length)
return stbi__err(
"invalid PLTE",
"Corrupt PNG");
4926 for (i=0; i < pal_len; ++i) {
4927 palette[i*4+0] = stbi__get8(s);
4928 palette[i*4+1] = stbi__get8(s);
4929 palette[i*4+2] = stbi__get8(s);
4930 palette[i*4+3] = 255;
4936 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
4940 if (pal_len == 0)
return stbi__err(
"tRNS before PLTE",
"Corrupt PNG");
4941 if (c.
length > pal_len)
return stbi__err(
"bad tRNS len",
"Corrupt PNG");
4943 for (i=0; i < c.
length; ++i)
4944 palette[i*4+3] = stbi__get8(s);
4946 if (!(s->
img_n & 1))
return stbi__err(
"tRNS with alpha",
"Corrupt PNG");
4951 if (z->
depth == 16) {
4952 for (k = 0; k < s->
img_n && k < 3; ++k)
4955 for (k = 0; k < s->
img_n && k < 3; ++k)
4956 tc[k] = (
stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->
depth];
4963 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
4964 if (pal_img_n && !pal_len)
return stbi__err(
"no PLTE",
"Corrupt PNG");
4968 s->
img_n = pal_img_n;
4971 if (c.
length > (1u << 30))
return stbi__err(
"IDAT size limit",
"IDAT section larger than 2^30 bytes");
4972 if ((
int)(ioff + c.
length) < (
int)ioff)
return 0;
4973 if (ioff + c.
length > idata_limit) {
4976 if (idata_limit == 0) idata_limit = c.
length > 4096 ? c.
length : 4096;
4977 while (ioff + c.
length > idata_limit)
4990 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
4996 z->
expanded = (
stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((
char *) z->
idata, ioff, raw_len, (
int *) &raw_len, !is_iphone);
4999 if ((req_comp == s->
img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
5003 if (!stbi__create_png_image(z, z->
expanded, raw_len, s->
img_out_n, z->
depth, color, interlace))
return 0;
5005 if (z->
depth == 16) {
5006 if (!stbi__compute_transparency16(z, tc16, s->
img_out_n))
return 0;
5008 if (!stbi__compute_transparency(z, tc, s->
img_out_n))
return 0;
5015 s->
img_n = pal_img_n;
5017 if (req_comp >= 3) s->
img_out_n = req_comp;
5018 if (!stbi__expand_png_palette(z, palette, pal_len, s->
img_out_n))
5020 }
else if (has_trans) {
5032 if (first)
return stbi__err(
"first not IHDR",
"Corrupt PNG");
5033 if ((c.
type & (1 << 29)) == 0) {
5034 #ifndef STBI_NO_FAILURE_STRINGS
5036 static char invalid_chunk[] =
"XXXX PNG chunk not known";
5042 return stbi__err(invalid_chunk,
"PNG not supported: unknown PNG chunk type");
5055 if (req_comp < 0 || req_comp > 4)
return stbi__errpuc(
"bad req_comp",
"Internal error");
5059 else if (p->
depth == 16)
5062 return stbi__errpuc(
"bad bits_per_channel",
"PNG not supported: unsupported color depth");
5065 if (req_comp && req_comp != p->
s->
img_out_n) {
5067 result = stbi__convert_format((
unsigned char *) result, p->
s->
img_out_n, req_comp, p->
s->
img_x, p->
s->
img_y);
5071 if (result == NULL)
return result;
5088 return stbi__do_png(&p, x,y,comp,req_comp, ri);
5094 r = stbi__check_png_header(s);
5099static int stbi__png_info_raw(
stbi__png *p,
int *x,
int *y,
int *comp)
5102 stbi__rewind( p->
s );
5107 if (comp) *comp = p->
s->
img_n;
5111static int stbi__png_info(
stbi__context *s,
int *x,
int *y,
int *comp)
5115 return stbi__png_info_raw(&p, x, y, comp);
5122 if (!stbi__png_info_raw(&p, NULL, NULL, NULL))
5124 if (p.
depth != 16) {
5137 if (stbi__get8(s) !=
'B')
return 0;
5138 if (stbi__get8(s) !=
'M')
return 0;
5143 sz = stbi__get32le(s);
5144 r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
5150 int r = stbi__bmp_test_raw(s);
5157static int stbi__high_bit(
unsigned int z)
5160 if (z == 0)
return -1;
5161 if (z >= 0x10000) { n += 16; z >>= 16; }
5162 if (z >= 0x00100) { n += 8; z >>= 8; }
5163 if (z >= 0x00010) { n += 4; z >>= 4; }
5164 if (z >= 0x00004) { n += 2; z >>= 2; }
5165 if (z >= 0x00002) { n += 1; }
5169static int stbi__bitcount(
unsigned int a)
5171 a = (a & 0x55555555) + ((a >> 1) & 0x55555555);
5172 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
5173 a = (a + (a >> 4)) & 0x0f0f0f0f;
5175 a = (a + (a >> 16));
5182static int stbi__shiftsigned(
unsigned int v,
int shift,
int bits)
5184 static unsigned int mul_table[9] = {
5186 0xff, 0x55, 0x49, 0x11,
5187 0x21, 0x41, 0x81, 0x01,
5189 static unsigned int shift_table[9] = {
5199 return (
int) ((unsigned) v * mul_table[bits]) >> shift_table[bits];
5209static int stbi__bmp_set_mask_defaults(
stbi__bmp_data *info,
int compress)
5215 if (compress == 0) {
5216 if (info->
bpp == 16) {
5217 info->
mr = 31u << 10;
5218 info->
mg = 31u << 5;
5219 info->
mb = 31u << 0;
5220 }
else if (info->
bpp == 32) {
5221 info->
mr = 0xffu << 16;
5222 info->
mg = 0xffu << 8;
5223 info->
mb = 0xffu << 0;
5224 info->
ma = 0xffu << 24;
5228 info->
mr = info->
mg = info->
mb = info->
ma = 0;
5238 if (stbi__get8(s) !=
'B' || stbi__get8(s) !=
'M')
return stbi__errpuc(
"not BMP",
"Corrupt BMP");
5242 info->
offset = stbi__get32le(s);
5243 info->
hsz = hsz = stbi__get32le(s);
5244 info->
mr = info->
mg = info->
mb = info->
ma = 0;
5249 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124)
return stbi__errpuc(
"unknown BMP",
"BMP type not supported: unknown");
5251 s->
img_x = stbi__get16le(s);
5252 s->
img_y = stbi__get16le(s);
5254 s->
img_x = stbi__get32le(s);
5255 s->
img_y = stbi__get32le(s);
5257 if (stbi__get16le(s) != 1)
return stbi__errpuc(
"bad BMP",
"bad BMP");
5258 info->
bpp = stbi__get16le(s);
5260 int compress = stbi__get32le(s);
5261 if (compress == 1 || compress == 2)
return stbi__errpuc(
"BMP RLE",
"BMP type not supported: RLE");
5262 if (compress >= 4)
return stbi__errpuc(
"BMP JPEG/PNG",
"BMP type not supported: unsupported compression");
5263 if (compress == 3 && info->
bpp != 16 && info->
bpp != 32)
return stbi__errpuc(
"bad BMP",
"bad BMP");
5269 if (hsz == 40 || hsz == 56) {
5276 if (info->
bpp == 16 || info->
bpp == 32) {
5277 if (compress == 0) {
5278 stbi__bmp_set_mask_defaults(info, compress);
5279 }
else if (compress == 3) {
5280 info->
mr = stbi__get32le(s);
5281 info->
mg = stbi__get32le(s);
5282 info->
mb = stbi__get32le(s);
5285 if (info->
mr == info->
mg && info->
mg == info->
mb) {
5295 if (hsz != 108 && hsz != 124)
5297 info->
mr = stbi__get32le(s);
5298 info->
mg = stbi__get32le(s);
5299 info->
mb = stbi__get32le(s);
5300 info->
ma = stbi__get32le(s);
5302 stbi__bmp_set_mask_defaults(info, compress);
5304 for (i=0; i < 12; ++i)
5321 unsigned int mr=0,mg=0,mb=0,ma=0, all_a;
5323 int psize=0,i,j,width;
5324 int flip_vertically, pad, target;
5329 if (stbi__bmp_parse_header(s, &info) == NULL)
5332 flip_vertically = ((int) s->
img_y) > 0;
5344 if (info.
hsz == 12) {
5355 int header_limit = 1024;
5356 int extra_data_limit = 256*4;
5357 if (bytes_read_so_far <= 0 || bytes_read_so_far > header_limit) {
5364 if (info.
offset < bytes_read_so_far || info.
offset - bytes_read_so_far > extra_data_limit) {
5367 stbi__skip(s, info.
offset - bytes_read_so_far);
5371 if (info.
bpp == 24 && ma == 0xff000000)
5374 s->
img_n = ma ? 4 : 3;
5375 if (req_comp && req_comp >= 3)
5381 if (!stbi__mad3sizes_valid(target, s->
img_x, s->
img_y, 0))
5385 if (!out)
return stbi__errpuc(
"outofmem",
"Out of memory");
5386 if (info.
bpp < 16) {
5389 for (i=0; i < psize; ++i) {
5390 pal[i][2] = stbi__get8(s);
5391 pal[i][1] = stbi__get8(s);
5392 pal[i][0] = stbi__get8(s);
5393 if (info.
hsz != 12) stbi__get8(s);
5397 if (info.
bpp == 1) width = (s->
img_x + 7) >> 3;
5398 else if (info.
bpp == 4) width = (s->
img_x + 1) >> 1;
5399 else if (info.
bpp == 8) width = s->
img_x;
5402 if (info.
bpp == 1) {
5403 for (j=0; j < (int) s->
img_y; ++j) {
5404 int bit_offset = 7, v = stbi__get8(s);
5405 for (i=0; i < (int) s->
img_x; ++i) {
5406 int color = (v>>bit_offset)&0x1;
5407 out[z++] = pal[color][0];
5408 out[z++] = pal[color][1];
5409 out[z++] = pal[color][2];
5410 if (target == 4) out[z++] = 255;
5411 if (i+1 == (
int) s->
img_x)
break;
5412 if((--bit_offset) < 0) {
5420 for (j=0; j < (int) s->
img_y; ++j) {
5421 for (i=0; i < (int) s->
img_x; i += 2) {
5422 int v=stbi__get8(s),v2=0;
5423 if (info.
bpp == 4) {
5427 out[z++] = pal[v][0];
5428 out[z++] = pal[v][1];
5429 out[z++] = pal[v][2];
5430 if (target == 4) out[z++] = 255;
5431 if (i+1 == (
int) s->
img_x)
break;
5432 v = (info.
bpp == 8) ? stbi__get8(s) : v2;
5433 out[z++] = pal[v][0];
5434 out[z++] = pal[v][1];
5435 out[z++] = pal[v][2];
5436 if (target == 4) out[z++] = 255;
5442 int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
5446 if (info.
bpp == 24) width = 3 * s->
img_x;
5447 else if (info.
bpp == 16) width = 2*s->
img_x;
5450 if (info.
bpp == 24) {
5452 }
else if (info.
bpp == 32) {
5453 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
5459 rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);
5460 gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
5461 bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
5462 ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
5463 if (rcount > 8 || gcount > 8 || bcount > 8 || acount > 8) {
STBI_FREE(out);
return stbi__errpuc(
"bad masks",
"Corrupt BMP"); }
5465 for (j=0; j < (int) s->
img_y; ++j) {
5467 for (i=0; i < (int) s->
img_x; ++i) {
5469 out[z+2] = stbi__get8(s);
5470 out[z+1] = stbi__get8(s);
5471 out[z+0] = stbi__get8(s);
5473 a = (easy == 2 ? stbi__get8(s) : 255);
5475 if (target == 4) out[z++] = a;
5479 for (i=0; i < (int) s->
img_x; ++i) {
5482 out[z++] =
STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount));
5483 out[z++] =
STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount));
5484 out[z++] =
STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
5485 a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
5495 if (target == 4 && all_a == 0)
5496 for (i=4*s->
img_x*s->
img_y-1; i >= 0; i -= 4)
5499 if (flip_vertically) {
5501 for (j=0; j < (int) s->
img_y>>1; ++j) {
5504 for (i=0; i < (int) s->
img_x*target; ++i) {
5505 t = p1[i]; p1[i] = p2[i]; p2[i] = t;
5510 if (req_comp && req_comp != target) {
5511 out = stbi__convert_format(out, target, req_comp, s->
img_x, s->
img_y);
5512 if (out == NULL)
return out;
5517 if (comp) *comp = s->
img_n;
5524static int stbi__tga_get_comp(
int bits_per_pixel,
int is_grey,
int* is_rgb16)
5527 if (is_rgb16) *is_rgb16 = 0;
5528 switch(bits_per_pixel) {
5532 case 15:
if(is_rgb16) *is_rgb16 = 1;
5535 case 32:
return bits_per_pixel/8;
5540static int stbi__tga_info(
stbi__context *s,
int *x,
int *y,
int *comp)
5542 int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp;
5543 int sz, tga_colormap_type;
5545 tga_colormap_type = stbi__get8(s);
5546 if( tga_colormap_type > 1 ) {
5550 tga_image_type = stbi__get8(s);
5551 if ( tga_colormap_type == 1 ) {
5552 if (tga_image_type != 1 && tga_image_type != 9) {
5558 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) {
5563 tga_colormap_bpp = sz;
5565 if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) {
5570 tga_colormap_bpp = 0;
5572 tga_w = stbi__get16le(s);
5577 tga_h = stbi__get16le(s);
5582 tga_bits_per_pixel = stbi__get8(s);
5584 if (tga_colormap_bpp != 0) {
5585 if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) {
5591 tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
5593 tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
5601 if (comp) *comp = tga_comp;
5608 int sz, tga_color_type;
5610 tga_color_type = stbi__get8(s);
5611 if ( tga_color_type > 1 )
goto errorEnd;
5613 if ( tga_color_type == 1 ) {
5614 if (sz != 1 && sz != 9)
goto errorEnd;
5617 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) )
goto errorEnd;
5620 if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) )
goto errorEnd;
5623 if ( stbi__get16le(s) < 1 )
goto errorEnd;
5624 if ( stbi__get16le(s) < 1 )
goto errorEnd;
5626 if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) )
goto errorEnd;
5627 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) )
goto errorEnd;
5642 int r = (px >> 10) & fiveBitMask;
5643 int g = (px >> 5) & fiveBitMask;
5644 int b = px & fiveBitMask;
5646 out[0] = (
stbi_uc)((r * 255)/31);
5647 out[1] = (
stbi_uc)((g * 255)/31);
5648 out[2] = (
stbi_uc)((b * 255)/31);
5659 int tga_offset = stbi__get8(s);
5660 int tga_indexed = stbi__get8(s);
5661 int tga_image_type = stbi__get8(s);
5663 int tga_palette_start = stbi__get16le(s);
5664 int tga_palette_len = stbi__get16le(s);
5665 int tga_palette_bits = stbi__get8(s);
5666 int tga_x_origin = stbi__get16le(s);
5667 int tga_y_origin = stbi__get16le(s);
5668 int tga_width = stbi__get16le(s);
5669 int tga_height = stbi__get16le(s);
5670 int tga_bits_per_pixel = stbi__get8(s);
5671 int tga_comp, tga_rgb16=0;
5672 int tga_inverted = stbi__get8(s);
5675 unsigned char *tga_data;
5676 unsigned char *tga_palette = NULL;
5678 unsigned char raw_data[4] = {0};
5680 int RLE_repeating = 0;
5681 int read_next_pixel = 1;
5690 if ( tga_image_type >= 8 )
5692 tga_image_type -= 8;
5695 tga_inverted = 1 - ((tga_inverted >> 5) & 1);
5698 if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
5699 else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16);
5702 return stbi__errpuc(
"bad format",
"Can't find out TGA pixelformat");
5707 if (comp) *comp = tga_comp;
5709 if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0))
5712 tga_data = (
unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0);
5713 if (!tga_data)
return stbi__errpuc(
"outofmem",
"Out of memory");
5716 stbi__skip(s, tga_offset );
5718 if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) {
5719 for (i=0; i < tga_height; ++i) {
5720 int row = tga_inverted ? tga_height -i - 1 : i;
5721 stbi_uc *tga_row = tga_data + row*tga_width*tga_comp;
5722 stbi__getn(s, tga_row, tga_width * tga_comp);
5728 if (tga_palette_len == 0) {
5734 stbi__skip(s, tga_palette_start );
5736 tga_palette = (
unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0);
5742 stbi_uc *pal_entry = tga_palette;
5744 for (i=0; i < tga_palette_len; ++i) {
5745 stbi__tga_read_rgb16(s, pal_entry);
5746 pal_entry += tga_comp;
5748 }
else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
5755 for (i=0; i < tga_width * tga_height; ++i)
5760 if ( RLE_count == 0 )
5763 int RLE_cmd = stbi__get8(s);
5764 RLE_count = 1 + (RLE_cmd & 127);
5765 RLE_repeating = RLE_cmd >> 7;
5766 read_next_pixel = 1;
5767 }
else if ( !RLE_repeating )
5769 read_next_pixel = 1;
5773 read_next_pixel = 1;
5776 if ( read_next_pixel )
5782 int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s);
5783 if ( pal_idx >= tga_palette_len ) {
5787 pal_idx *= tga_comp;
5788 for (j = 0; j < tga_comp; ++j) {
5789 raw_data[j] = tga_palette[pal_idx+j];
5791 }
else if(tga_rgb16) {
5793 stbi__tga_read_rgb16(s, raw_data);
5796 for (j = 0; j < tga_comp; ++j) {
5797 raw_data[j] = stbi__get8(s);
5801 read_next_pixel = 0;
5805 for (j = 0; j < tga_comp; ++j)
5806 tga_data[i*tga_comp+j] = raw_data[j];
5814 for (j = 0; j*2 < tga_height; ++j)
5816 int index1 = j * tga_width * tga_comp;
5817 int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
5818 for (i = tga_width * tga_comp; i > 0; --i)
5820 unsigned char temp = tga_data[index1];
5821 tga_data[index1] = tga_data[index2];
5822 tga_data[index2] = temp;
5829 if ( tga_palette != NULL )
5836 if (tga_comp >= 3 && !tga_rgb16)
5838 unsigned char* tga_pixel = tga_data;
5839 for (i=0; i < tga_width * tga_height; ++i)
5841 unsigned char temp = tga_pixel[0];
5842 tga_pixel[0] = tga_pixel[2];
5843 tga_pixel[2] = temp;
5844 tga_pixel += tga_comp;
5849 if (req_comp && req_comp != tga_comp)
5850 tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
5854 tga_palette_start = tga_palette_len = tga_palette_bits =
5855 tga_x_origin = tga_y_origin = 0;
5866 int r = (stbi__get32be(s) == 0x38425053);
5873 int count, nleft, len;
5876 while ((nleft = pixelCount - count) > 0) {
5877 len = stbi__get8(s);
5880 }
else if (len < 128) {
5883 if (len > nleft)
return 0;
5890 }
else if (len > 128) {
5895 if (len > nleft)
return 0;
5896 val = stbi__get8(s);
5912 int channelCount, compression;
5920 if (stbi__get32be(s) != 0x38425053)
5924 if (stbi__get16be(s) != 1)
5925 return stbi__errpuc(
"wrong version",
"Unsupported version of PSD image");
5931 channelCount = stbi__get16be(s);
5932 if (channelCount < 0 || channelCount > 16)
5933 return stbi__errpuc(
"wrong channel count",
"Unsupported number of channels in PSD image");
5936 h = stbi__get32be(s);
5937 w = stbi__get32be(s);
5943 bitdepth = stbi__get16be(s);
5944 if (bitdepth != 8 && bitdepth != 16)
5945 return stbi__errpuc(
"unsupported bit depth",
"PSD bit depth is not 8 or 16 bit");
5957 if (stbi__get16be(s) != 3)
5958 return stbi__errpuc(
"wrong color format",
"PSD is not in RGB color format");
5961 stbi__skip(s,stbi__get32be(s) );
5964 stbi__skip(s, stbi__get32be(s) );
5967 stbi__skip(s, stbi__get32be(s) );
5973 compression = stbi__get16be(s);
5974 if (compression > 1)
5975 return stbi__errpuc(
"bad compression",
"PSD has an unknown compression format");
5978 if (!stbi__mad3sizes_valid(4, w, h, 0))
5983 if (!compression && bitdepth == 16 && bpc == 16) {
5984 out = (
stbi_uc *) stbi__malloc_mad3(8, w, h, 0);
5987 out = (
stbi_uc *) stbi__malloc(4 * w*h);
5989 if (!out)
return stbi__errpuc(
"outofmem",
"Out of memory");
6007 stbi__skip(s, h * channelCount * 2 );
6010 for (channel = 0; channel < 4; channel++) {
6014 if (channel >= channelCount) {
6016 for (i = 0; i < pixelCount; i++, p += 4)
6017 *p = (channel == 3 ? 255 : 0);
6020 if (!stbi__psd_decode_rle(s, p, pixelCount)) {
6032 for (channel = 0; channel < 4; channel++) {
6033 if (channel >= channelCount) {
6035 if (bitdepth == 16 && bpc == 16) {
6038 for (i = 0; i < pixelCount; i++, q += 4)
6042 stbi_uc val = channel == 3 ? 255 : 0;
6043 for (i = 0; i < pixelCount; i++, p += 4)
6049 for (i = 0; i < pixelCount; i++, q += 4)
6053 if (bitdepth == 16) {
6054 for (i = 0; i < pixelCount; i++, p += 4)
6055 *p = (
stbi_uc) (stbi__get16be(s) >> 8);
6057 for (i = 0; i < pixelCount; i++, p += 4)
6066 if (channelCount >= 4) {
6068 for (i=0; i < w*h; ++i) {
6070 if (pixel[3] != 0 && pixel[3] != 65535) {
6071 float a = pixel[3] / 65535.0f;
6072 float ra = 1.0f / a;
6073 float inv_a = 65535.0f * (1 - ra);
6080 for (i=0; i < w*h; ++i) {
6081 unsigned char *pixel = out + 4*i;
6082 if (pixel[3] != 0 && pixel[3] != 255) {
6083 float a = pixel[3] / 255.0f;
6084 float ra = 1.0f / a;
6085 float inv_a = 255.0f * (1 - ra);
6086 pixel[0] = (
unsigned char) (pixel[0]*ra + inv_a);
6087 pixel[1] = (
unsigned char) (pixel[1]*ra + inv_a);
6088 pixel[2] = (
unsigned char) (pixel[2]*ra + inv_a);
6095 if (req_comp && req_comp != 4) {
6099 out = stbi__convert_format(out, 4, req_comp, w, h);
6100 if (out == NULL)
return out;
6103 if (comp) *comp = 4;
6121 if (stbi__get8(s) != (
stbi_uc)str[i])
6131 if (!stbi__pic_is4(s,
"\x53\x80\xF6\x34"))
6137 if (!stbi__pic_is4(s,
"PICT"))
6152 for (i=0; i<4; ++i, mask>>=1) {
6153 if (channel & mask) {
6154 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"PIC file too short");
6155 dest[i]=stbi__get8(s);
6162static void stbi__copyval(
int channel,
stbi_uc *dest,
const stbi_uc *src)
6166 for (i=0;i<4; ++i, mask>>=1)
6173 int act_comp=0,num_packets=0,y,chained;
6181 if (num_packets==
sizeof(packets)/
sizeof(packets[0]))
6184 packet = &packets[num_packets++];
6186 chained = stbi__get8(s);
6187 packet->
size = stbi__get8(s);
6188 packet->
type = stbi__get8(s);
6189 packet->
channel = stbi__get8(s);
6193 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (reading packets)");
6194 if (packet->
size != 8)
return stbi__errpuc(
"bad format",
"packet isn't 8bpp");
6197 *comp = (act_comp & 0x10 ? 4 : 3);
6199 for(y=0; y<height; ++y) {
6202 for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
6204 stbi_uc *dest = result+y*width*4;
6206 switch (packet->
type) {
6208 return stbi__errpuc(
"bad format",
"packet has bad compression type");
6213 for(x=0;x<width;++x, dest+=4)
6214 if (!stbi__readval(s,packet->
channel,dest))
6226 count=stbi__get8(s);
6227 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (pure read count)");
6232 if (!stbi__readval(s,packet->
channel,value))
return 0;
6234 for(i=0; i<count; ++i,dest+=4)
6235 stbi__copyval(packet->
channel,dest,value);
6244 int count = stbi__get8(s), i;
6245 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (mixed read count)");
6251 count = stbi__get16be(s);
6257 if (!stbi__readval(s,packet->
channel,value))
6260 for(i=0;i<count;++i, dest += 4)
6261 stbi__copyval(packet->
channel,dest,value);
6264 if (count>left)
return stbi__errpuc(
"bad file",
"scanline overrun");
6266 for(i=0;i<count;++i, dest+=4)
6267 if (!stbi__readval(s,packet->
channel,dest))
6284 int i, x,y, internal_comp;
6287 if (!comp) comp = &internal_comp;
6289 for (i=0; i<92; ++i)
6292 x = stbi__get16be(s);
6293 y = stbi__get16be(s);
6298 if (stbi__at_eof(s))
return stbi__errpuc(
"bad file",
"file too short (pic header)");
6299 if (!stbi__mad3sizes_valid(x, y, 4, 0))
return stbi__errpuc(
"too large",
"PIC image too large to decode");
6306 result = (
stbi_uc *) stbi__malloc_mad3(x, y, 4, 0);
6307 if (!result)
return stbi__errpuc(
"outofmem",
"Out of memory");
6308 memset(result, 0xff, x*y*4);
6310 if (!stbi__pic_load_core(s,x,y,comp, result)) {
6316 if (req_comp == 0) req_comp = *comp;
6317 result=stbi__convert_format(result,4,req_comp,x,y);
6324 int r = stbi__pic_test_core(s);
6362 if (stbi__get8(s) !=
'G' || stbi__get8(s) !=
'I' || stbi__get8(s) !=
'F' || stbi__get8(s) !=
'8')
return 0;
6364 if (sz !=
'9' && sz !=
'7')
return 0;
6365 if (stbi__get8(s) !=
'a')
return 0;
6371 int r = stbi__gif_test_raw(s);
6376static void stbi__gif_parse_colortable(
stbi__context *s,
stbi_uc pal[256][4],
int num_entries,
int transp)
6379 for (i=0; i < num_entries; ++i) {
6380 pal[i][2] = stbi__get8(s);
6381 pal[i][1] = stbi__get8(s);
6382 pal[i][0] = stbi__get8(s);
6383 pal[i][3] = transp == i ? 0 : 255;
6390 if (stbi__get8(s) !=
'G' || stbi__get8(s) !=
'I' || stbi__get8(s) !=
'F' || stbi__get8(s) !=
'8')
6391 return stbi__err(
"not GIF",
"Corrupt GIF");
6394 if (version !=
'7' && version !=
'9')
return stbi__err(
"not GIF",
"Corrupt GIF");
6395 if (stbi__get8(s) !=
'a')
return stbi__err(
"not GIF",
"Corrupt GIF");
6397 stbi__g_failure_reason =
"";
6398 g->
w = stbi__get16le(s);
6399 g->
h = stbi__get16le(s);
6400 g->
flags = stbi__get8(s);
6402 g->
ratio = stbi__get8(s);
6408 if (comp != 0) *comp = 4;
6410 if (is_info)
return 1;
6412 if (g->
flags & 0x80)
6413 stbi__gif_parse_colortable(s,g->
pal, 2 << (g->
flags & 7), -1);
6418static int stbi__gif_info_raw(
stbi__context *s,
int *x,
int *y,
int *comp)
6421 if (!g)
return stbi__err(
"outofmem",
"Out of memory");
6422 if (!stbi__gif_header(s, g, comp, 1)) {
6475 stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
6478 lzw_cs = stbi__get8(s);
6479 if (lzw_cs > 12)
return NULL;
6480 clear = 1 << lzw_cs;
6482 codesize = lzw_cs + 1;
6483 codemask = (1 << codesize) - 1;
6486 for (init_code = 0; init_code < clear; init_code++) {
6498 if (valid_bits < codesize) {
6500 len = stbi__get8(s);
6505 bits |= (
stbi__int32) stbi__get8(s) << valid_bits;
6510 valid_bits -= codesize;
6512 if (code == clear) {
6513 codesize = lzw_cs + 1;
6514 codemask = (1 << codesize) - 1;
6518 }
else if (code == clear + 1) {
6520 while ((len = stbi__get8(s)) > 0)
6523 }
else if (code <= avail) {
6529 p = &g->
codes[avail++];
6536 p->
suffix = (code == avail) ? p->
first : g->codes[code].first;
6537 }
else if (code == avail)
6538 return stbi__errpuc(
"illegal code in raster",
"Corrupt GIF");
6542 if ((avail & codemask) == 0 && avail <= 0x0FFF) {
6544 codemask = (1 << codesize) - 1;
6549 return stbi__errpuc(
"illegal code in raster",
"Corrupt GIF");
6568 if (!stbi__gif_header(s, g, comp,0))
return 0;
6569 if (!stbi__mad3sizes_valid(4, g->
w, g->
h, 0))
6570 return stbi__errpuc(
"too large",
"GIF image is too large");
6571 pcount = g->
w * g->
h;
6572 g->
out = (
stbi_uc *) stbi__malloc(4 * pcount);
6581 memset(g->
out, 0x00, 4 * pcount);
6583 memset(g->
history, 0x00, pcount);
6587 dispose = (g->
eflags & 0x1C) >> 2;
6588 pcount = g->
w * g->
h;
6590 if ((dispose == 3) && (two_back == 0)) {
6595 for (pi = 0; pi < pcount; ++pi) {
6597 memcpy( &g->
out[pi * 4], &two_back[pi * 4], 4 );
6600 }
else if (dispose == 2) {
6602 for (pi = 0; pi < pcount; ++pi) {
6619 memset( g->
history, 0x00, g->
w * g->
h );
6622 int tag = stbi__get8(s);
6629 x = stbi__get16le(s);
6630 y = stbi__get16le(s);
6631 w = stbi__get16le(s);
6632 h = stbi__get16le(s);
6633 if (((x + w) > (g->
w)) || ((y + h) > (g->
h)))
6634 return stbi__errpuc(
"bad Image Descriptor",
"Corrupt GIF");
6651 g->
lflags = stbi__get8(s);
6664 }
else if (g->
flags & 0x80) {
6667 return stbi__errpuc(
"missing color table",
"Corrupt GIF");
6669 o = stbi__process_gif_raster(s, g);
6670 if (!o)
return NULL;
6673 pcount = g->
w * g->
h;
6674 if (first_frame && (g->
bgindex > 0)) {
6676 for (pi = 0; pi < pcount; ++pi) {
6690 int ext = stbi__get8(s);
6692 len = stbi__get8(s);
6694 g->
eflags = stbi__get8(s);
6695 g->
delay = 10 * stbi__get16le(s);
6716 while ((len = stbi__get8(s)) != 0) {
6731static void *stbi__load_gif_main_outofmem(
stbi__gif *g,
stbi_uc *out,
int **delays)
6738 if (delays && *delays)
STBI_FREE(*delays);
6742static void *stbi__load_gif_main(
stbi__context *s,
int **delays,
int *x,
int *y,
int *z,
int *comp,
int req_comp)
6744 if (stbi__gif_test(s)) {
6752 int delays_size = 0;
6757 memset(&g, 0,
sizeof(g));
6763 u = stbi__gif_load_next(s, &g, comp, req_comp, two_back);
6764 if (u == (
stbi_uc *) s) u = 0;
6770 stride = g.
w * g.
h * 4;
6775 return stbi__load_gif_main_outofmem(&g, out, delays);
6778 out_size = layers * stride;
6782 int *new_delays = (
int*)
STBI_REALLOC_SIZED( *delays, delays_size,
sizeof(
int) * layers );
6784 return stbi__load_gif_main_outofmem(&g, out, delays);
6785 *delays = new_delays;
6786 delays_size = layers *
sizeof(int);
6789 out = (
stbi_uc*)stbi__malloc( layers * stride );
6791 return stbi__load_gif_main_outofmem(&g, out, delays);
6792 out_size = layers * stride;
6794 *delays = (
int*) stbi__malloc( layers *
sizeof(
int) );
6796 return stbi__load_gif_main_outofmem(&g, out, delays);
6797 delays_size = layers *
sizeof(int);
6800 memcpy( out + ((layers - 1) * stride), u, stride );
6802 two_back = out - 2 * stride;
6806 (*delays)[layers - 1U] = g.
delay;
6817 if (req_comp && req_comp != 4)
6818 out = stbi__convert_format(out, 4, req_comp, layers * g.
w, g.
h);
6823 return stbi__errpuc(
"not GIF",
"Image was not as a gif type.");
6831 memset(&g, 0,
sizeof(g));
6834 u = stbi__gif_load_next(s, &g, comp, req_comp, 0);
6835 if (u == (
stbi_uc *) s) u = 0;
6842 if (req_comp && req_comp != 4)
6843 u = stbi__convert_format(u, 4, req_comp, g.
w, g.
h);
6856static int stbi__gif_info(
stbi__context *s,
int *x,
int *y,
int *comp)
6858 return stbi__gif_info_raw(s,x,y,comp);
6864static int stbi__hdr_test_core(
stbi__context *s,
const char *signature)
6867 for (i=0; signature[i]; ++i)
6868 if (stbi__get8(s) != signature[i])
6876 int r = stbi__hdr_test_core(s,
"#?RADIANCE\n");
6879 r = stbi__hdr_test_core(s,
"#?RGBE\n");
6885#define STBI__HDR_BUFLEN 1024
6886static char *stbi__hdr_gettoken(
stbi__context *z,
char *buffer)
6891 c = (char) stbi__get8(z);
6893 while (!stbi__at_eof(z) && c !=
'\n') {
6897 while (!stbi__at_eof(z) && stbi__get8(z) !=
'\n')
6901 c = (char) stbi__get8(z);
6908static void stbi__hdr_convert(
float *output,
stbi_uc *input,
int req_comp)
6910 if ( input[3] != 0 ) {
6913 f1 = (float) ldexp(1.0f, input[3] - (
int)(128 + 8));
6915 output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
6917 output[0] = input[0] * f1;
6918 output[1] = input[1] * f1;
6919 output[2] = input[2] * f1;
6921 if (req_comp == 2) output[1] = 1;
6922 if (req_comp == 4) output[3] = 1;
6925 case 4: output[3] = 1;
6926 case 3: output[0] = output[1] = output[2] = 0;
6928 case 2: output[1] = 1;
6929 case 1: output[0] = 0;
6944 unsigned char count, value;
6945 int i, j, k, c1,c2, z;
6946 const char *headerToken;
6950 headerToken = stbi__hdr_gettoken(s,buffer);
6951 if (strcmp(headerToken,
"#?RADIANCE") != 0 && strcmp(headerToken,
"#?RGBE") != 0)
6952 return stbi__errpf(
"not HDR",
"Corrupt HDR image");
6956 token = stbi__hdr_gettoken(s,buffer);
6957 if (token[0] == 0)
break;
6958 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
6961 if (!valid)
return stbi__errpf(
"unsupported format",
"Unsupported HDR format");
6965 token = stbi__hdr_gettoken(s,buffer);
6966 if (strncmp(token,
"-Y ", 3))
return stbi__errpf(
"unsupported data layout",
"Unsupported HDR format");
6968 height = (int) strtol(token, &token, 10);
6969 while (*token ==
' ') ++token;
6970 if (strncmp(token,
"+X ", 3))
return stbi__errpf(
"unsupported data layout",
"Unsupported HDR format");
6972 width = (int) strtol(token, NULL, 10);
6980 if (comp) *comp = 3;
6981 if (req_comp == 0) req_comp = 3;
6983 if (!stbi__mad4sizes_valid(width, height, req_comp,
sizeof(
float), 0))
6984 return stbi__errpf(
"too large",
"HDR image is too large");
6987 hdr_data = (
float *) stbi__malloc_mad4(width, height, req_comp,
sizeof(
float), 0);
6993 if ( width < 8 || width >= 32768) {
6995 for (j=0; j < height; ++j) {
6996 for (i=0; i < width; ++i) {
6999 stbi__getn(s, rgbe, 4);
7000 stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
7007 for (j = 0; j < height; ++j) {
7010 len = stbi__get8(s);
7011 if (c1 != 2 || c2 != 2 || (len & 0x80)) {
7018 rgbe[3] = (
stbi_uc) stbi__get8(s);
7019 stbi__hdr_convert(hdr_data, rgbe, req_comp);
7023 goto main_decode_loop;
7026 len |= stbi__get8(s);
7028 if (scanline == NULL) {
7029 scanline = (
stbi_uc *) stbi__malloc_mad2(width, 4, 0);
7036 for (k = 0; k < 4; ++k) {
7039 while ((nleft = width - i) > 0) {
7040 count = stbi__get8(s);
7043 value = stbi__get8(s);
7046 for (z = 0; z < count; ++z)
7047 scanline[i++ * 4 + k] = value;
7051 for (z = 0; z < count; ++z)
7052 scanline[i++ * 4 + k] = stbi__get8(s);
7056 for (i=0; i < width; ++i)
7057 stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
7066static int stbi__hdr_info(
stbi__context *s,
int *x,
int *y,
int *comp)
7075 if (!comp) comp = &dummy;
7077 if (stbi__hdr_test(s) == 0) {
7083 token = stbi__hdr_gettoken(s,buffer);
7084 if (token[0] == 0)
break;
7085 if (strcmp(token,
"FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
7092 token = stbi__hdr_gettoken(s,buffer);
7093 if (strncmp(token,
"-Y ", 3)) {
7098 *y = (int) strtol(token, &token, 10);
7099 while (*token ==
' ') ++token;
7100 if (strncmp(token,
"+X ", 3)) {
7105 *x = (int) strtol(token, NULL, 10);
7110static int stbi__bmp_info(
stbi__context *s,
int *x,
int *y,
int *comp)
7116 p = stbi__bmp_parse_header(s, &info);
7121 if (x) *x = s->
img_x;
7122 if (y) *y = s->
img_y;
7124 if (info.
bpp == 24 && info.
ma == 0xff000000)
7127 *comp = info.
ma ? 4 : 3;
7132static int stbi__psd_info(
stbi__context *s,
int *x,
int *y,
int *comp)
7134 int channelCount, dummy, depth;
7137 if (!comp) comp = &dummy;
7138 if (stbi__get32be(s) != 0x38425053) {
7142 if (stbi__get16be(s) != 1) {
7147 channelCount = stbi__get16be(s);
7148 if (channelCount < 0 || channelCount > 16) {
7152 *y = stbi__get32be(s);
7153 *x = stbi__get32be(s);
7154 depth = stbi__get16be(s);
7155 if (depth != 8 && depth != 16) {
7159 if (stbi__get16be(s) != 3) {
7169 int channelCount, depth;
7170 if (stbi__get32be(s) != 0x38425053) {
7174 if (stbi__get16be(s) != 1) {
7179 channelCount = stbi__get16be(s);
7180 if (channelCount < 0 || channelCount > 16) {
7186 depth = stbi__get16be(s);
7194static int stbi__pic_info(
stbi__context *s,
int *x,
int *y,
int *comp)
7196 int act_comp=0,num_packets=0,chained,dummy;
7201 if (!comp) comp = &dummy;
7203 if (!stbi__pic_is4(s,
"\x53\x80\xF6\x34")) {
7210 *x = stbi__get16be(s);
7211 *y = stbi__get16be(s);
7212 if (stbi__at_eof(s)) {
7216 if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
7226 if (num_packets==
sizeof(packets)/
sizeof(packets[0]))
7229 packet = &packets[num_packets++];
7230 chained = stbi__get8(s);
7231 packet->
size = stbi__get8(s);
7232 packet->
type = stbi__get8(s);
7233 packet->
channel = stbi__get8(s);
7236 if (stbi__at_eof(s)) {
7240 if (packet->
size != 8) {
7246 *comp = (act_comp & 0x10 ? 4 : 3);
7266 p = (char) stbi__get8(s);
7267 t = (char) stbi__get8(s);
7268 if (p !=
'P' || (t !=
'5' && t !=
'6')) {
7289 if (comp) *comp = s->
img_n;
7295 if (!out)
return stbi__errpuc(
"outofmem",
"Out of memory");
7301 if (req_comp && req_comp != s->
img_n) {
7305 out = stbi__convert_format(out, s->
img_n, req_comp, s->
img_x, s->
img_y);
7307 if (out == NULL)
return out;
7312static int stbi__pnm_isspace(
char c)
7314 return c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\v' || c ==
'\f' || c ==
'\r';
7317static void stbi__pnm_skip_whitespace(
stbi__context *s,
char *c)
7320 while (!stbi__at_eof(s) && stbi__pnm_isspace(*c))
7321 *c = (char) stbi__get8(s);
7323 if (stbi__at_eof(s) || *c !=
'#')
7326 while (!stbi__at_eof(s) && *c !=
'\n' && *c !=
'\r' )
7327 *c = (
char) stbi__get8(s);
7331static int stbi__pnm_isdigit(
char c)
7333 return c >=
'0' && c <=
'9';
7340 while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) {
7341 value = value*10 + (*c -
'0');
7342 *c = (char) stbi__get8(s);
7343 if((value > 214748364) || (value == 214748364 && *c >
'7'))
7344 return stbi__err(
"integer parse overflow",
"Parsing an integer in the PPM header overflowed a 32-bit int");
7350static int stbi__pnm_info(
stbi__context *s,
int *x,
int *y,
int *comp)
7357 if (!comp) comp = &dummy;
7362 p = (char) stbi__get8(s);
7363 t = (char) stbi__get8(s);
7364 if (p !=
'P' || (t !=
'5' && t !=
'6')) {
7369 *comp = (t ==
'6') ? 3 : 1;
7371 c = (char) stbi__get8(s);
7372 stbi__pnm_skip_whitespace(s, &c);
7374 *x = stbi__pnm_getinteger(s, &c);
7376 return stbi__err(
"invalid width",
"PPM image header had zero or overflowing width");
7377 stbi__pnm_skip_whitespace(s, &c);
7379 *y = stbi__pnm_getinteger(s, &c);
7381 return stbi__err(
"invalid width",
"PPM image header had zero or overflowing width");
7382 stbi__pnm_skip_whitespace(s, &c);
7384 maxv = stbi__pnm_getinteger(s, &c);
7386 return stbi__err(
"max value > 65535",
"PPM image supports only 8-bit and 16-bit images");
7387 else if (maxv > 255)
7395 if (stbi__pnm_info(s, NULL, NULL, NULL) == 16)
7400static int stbi__info_main(
stbi__context *s,
int *x,
int *y,
int *comp)
7402 if (stbi__jpeg_info(s, x, y, comp))
return 1;
7404 if (stbi__png_info(s, x, y, comp))
return 1;
7406 if (stbi__gif_info(s, x, y, comp))
return 1;
7408 if (stbi__bmp_info(s, x, y, comp))
return 1;
7410 if (stbi__psd_info(s, x, y, comp))
return 1;
7412 if (stbi__pic_info(s, x, y, comp))
return 1;
7414 if (stbi__pnm_info(s, x, y, comp))
return 1;
7416 if (stbi__hdr_info(s, x, y, comp))
return 1;
7419 if (stbi__tga_info(s, x, y, comp))
7421 return stbi__err(
"unknown image type",
"Image not of any known type, or corrupt");
7426 if (stbi__png_is16(s))
return 1;
7428 if (stbi__psd_is16(s))
return 1;
7430 if (stbi__pnm_is16(s))
return 1;
7434STBIDEF int stbi_info(
char const *filename,
int *x,
int *y,
int *comp)
7436 FILE *f = stbi__fopen(filename,
"rb");
7438 if (!f)
return stbi__err(
"can't fopen",
"Unable to open file");
7439 result = stbi_info_from_file(f, x, y, comp);
7444STBIDEF int stbi_info_from_file(FILE *f,
int *x,
int *y,
int *comp)
7448 long pos = ftell(f);
7449 stbi__start_file(&s, f);
7450 r = stbi__info_main(&s,x,y,comp);
7451 fseek(f,pos,SEEK_SET);
7455STBIDEF int stbi_is_16_bit(
char const *filename)
7457 FILE *f = stbi__fopen(filename,
"rb");
7459 if (!f)
return stbi__err(
"can't fopen",
"Unable to open file");
7460 result = stbi_is_16_bit_from_file(f);
7465STBIDEF int stbi_is_16_bit_from_file(FILE *f)
7469 long pos = ftell(f);
7470 stbi__start_file(&s, f);
7471 r = stbi__is_16_main(&s);
7472 fseek(f,pos,SEEK_SET);
7476STBIDEF int stbi_info_from_memory(
stbi_uc const *buffer,
int len,
int *x,
int *y,
int *comp)
7479 stbi__start_mem(&s,buffer,len);
7480 return stbi__info_main(&s,x,y,comp);
7487 return stbi__info_main(&s,x,y,comp);
7490STBIDEF int stbi_is_16_bit_from_memory(
stbi_uc const *buffer,
int len)
7493 stbi__start_mem(&s,buffer,len);
7494 return stbi__is_16_main(&s);
7501 return stbi__is_16_main(&s);
7736static surface_t* surface_create_uninitialized(
int width,
int height)
7738 static const int kMaxSize = 1 << 15;
7739 if(width <= 0 || height <= 0 || width >= kMaxSize || height >= kMaxSize)
7741 const size_t size = width * height * 4;
7746 surface->width = width;
7747 surface->height = height;
7748 surface->stride = width * 4;
7749 surface->data = (uint8_t*)(surface + 1);
7772static surface_t* surface_load_from_image(
stbi_uc* image,
int width,
int height)
7777 stbi_image_free(image);
7783 int width, height, channels;
7787 return surface_load_from_image(image, width, height);
7792 int width, height, channels;
7796 return surface_load_from_image(image, width, height);
7799static const uint8_t base64_table[128] = {
7800 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7801 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7802 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7803 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7804 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7805 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F,
7806 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
7807 0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7808 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
7809 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
7810 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
7811 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
7812 0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
7813 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
7814 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
7815 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00
7825 enum : uint8_t { kWs = 0xFD, kPad = 0xFE, kBad = 0xFF };
7826 static const struct Table {
7830 for(
int i = 0; i < 256; ++i)
7832 for(
int i = 0; i < 26; ++i) {
7833 v[
'A' + i] = uint8_t(i);
7834 v[
'a' + i] = uint8_t(26 + i);
7836 for(
int i = 0; i < 10; ++i)
7837 v[
'0' + i] = uint8_t(52 + i);
7838 v[uint8_t(
'+')] = 62;
7839 v[uint8_t(
'/')] = 63;
7840 v[uint8_t(
'=')] = kPad;
7841 for(
int c : {
' ',
'\t',
'\n',
'\r',
'\f',
'\v'})
7848 length = int(strlen(data));
7852 auto* output_data =
static_cast<uint8_t*
>(malloc(
size_t(length)));
7853 if(output_data == NULL)
7862 for(
int i = 0; i < length; ++i) {
7865 if(bits == 0 && pads == 0 && i + 4 <= length) {
7866 const uint8_t a = table.v[uint8_t(data[i])], b = table.v[uint8_t(data[i + 1])];
7867 const uint8_t c = table.v[uint8_t(data[i + 2])], d = table.v[uint8_t(data[i + 3])];
7868 if((a | b | c | d) < 64) {
7869 output_data[didx] = uint8_t((a << 2) | (b >> 4));
7870 output_data[didx + 1] = uint8_t((b << 4) | (c >> 2));
7871 output_data[didx + 2] = uint8_t((c << 6) | d);
7879 const uint8_t v = table.v[uint8_t(data[i])];
7883 acc = (acc << 6) | v;
7888 output_data[didx++] = uint8_t(acc >> bits);
7890 }
else if(v == kPad) {
7892 }
else if(v == kBad) {
7897 if(count == 0 || pads > 2 || (count % 4) == 1)
7911 size_t output_length = 0;
7912 uint8_t* output_data =
base64_decode(data, length, &output_length);
7913 if(output_data == NULL)
7969 for(
int y = 0; y < height; y++) {
7970 const uint32_t* src_row = (
const uint32_t*)(src + stride * y);
7971 unsigned char* dst_row = dst + stride * y;
7972 for(
int x = 0; x < width; x++) {
7973 uint32_t pixel = src_row[x];
7974 uint32_t a = (pixel >> 24) & 0xFF;
7981 uint32_t r = (pixel >> 16) & 0xFF;
7982 uint32_t g = (pixel >> 8) & 0xFF;
7983 uint32_t b = (pixel >> 0) & 0xFF;
8001 for(
int y = 0; y < height; y++) {
8002 const unsigned char* src_row = src + stride * y;
8003 uint32_t* dst_row = (uint32_t*)(dst + stride * y);
8004 for(
int x = 0; x < width; x++) {
8005 uint32_t a = src_row[4 * x + 3];
8007 dst_row[x] = 0x00000000;
8009 uint32_t r = src_row[4 * x + 0];
8010 uint32_t g = src_row[4 * x + 1];
8011 uint32_t b = src_row[4 * x + 2];
8018 dst_row[x] = (a << 24) | (r << 16) | (g << 8) | b;
surface_t * surface_create_for_data(unsigned char *data, int width, int height, int stride)
Creates an image surface using existing pixel data.
NOVASVG_INLINE uint8_t * base64_decode(const char *data, int length, size_t *out_length)
int surface_get_width(const surface_t *surface)
Gets the width of the surface.
struct surface surface_t
Represents an image surface for drawing operations.
void convert_rgba_to_argb(unsigned char *dst, const unsigned char *src, int width, int height, int stride)
Converts pixel data from RGBA to premultiplied ARGB format.
int surface_get_height(const surface_t *surface)
Gets the height of the surface.
NOVASVG_INLINE unsigned int color_to_argb32(const color_t *color)
Converts a color to a 32-bit unsigned ARGB value.
int surface_get_stride(const surface_t *surface)
Gets the stride of the surface.
surface_t * surface_create(int width, int height)
Creates a new image surface with the specified dimensions.
NOVASVG_INLINE uint32_t premultiply_argb(uint32_t color)
surface_t * surface_load_from_image_data(const void *data, int length)
Loads an image surface from raw image data.
void surface_destroy(surface_t *surface)
Decrements the reference count and destroys the surface if the count reaches zero.
surface_t * surface_load_from_image_file(const char *filename)
Loads an image surface from a file.
int surface_get_reference_count(const surface_t *surface)
Gets the current reference count of a surface.
void convert_argb_to_rgba(unsigned char *dst, const unsigned char *src, int width, int height, int stride)
Converts pixel data from premultiplied ARGB to RGBA format.
NOVASVG_INLINE void memfill32(unsigned int *dest, int length, unsigned int value)
surface_t * surface_load_from_image_base64(const char *data, int length)
Loads an image surface from base64-encoded data.
surface_t * surface_reference(surface_t *surface)
Increments the reference count for a surface.
void surface_clear(surface_t *surface, const color_t *color)
Clears the entire surface with the specified color.
unsigned char * surface_get_data(const surface_t *surface)
Gets the pixel data of the surface.
Main namespace for the NovaSVG library.
int version()
Returns the version of the novasvg library encoded in a single integer.
#define novasvg_get_reference_count(ob)
#define novasvg_destroy_reference(ob)
#define novasvg_init_reference(ob)
#define novasvg_increment_reference(ob)
Represents a color with red, green, blue, and alpha components.
int callback_already_read
stbi_uc * img_buffer_original_end
stbi_uc buffer_start[128]
stbi_uc * img_buffer_original
stbi__gif_lzw codes[8192]
stbi__int16 fast_ac[4][1<< 9]
struct stbi__jpeg::@19 img_comp[4]
void(* YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step)
stbi__uint16 dequant[4][64]
stbi_uc *(* resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
void(* idct_block_kernel)(stbi_uc *out, int out_stride, short data[64])
int app14_color_transform
resample_row_func resample
stbi__zhuffman z_distance
stbi__uint16 firstcode[16]
stbi__uint16 firstsymbol[16]
void(* skip)(void *user, int n)
int(* read)(void *user, char *data, int size)
#define STBI__IDCT_1D(s0, s1, s2, s3, s4, s5, s6, s7)
#define STBI_SIMD_ALIGN(type, name)
#define STBI__PNG_TYPE(a, b, c, d)
#define stbi__SOF_progressive(x)
#define stbi__errpf(x, y)
unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 :-1]
#define stbi__errpuc(x, y)
#define STBI_MAX_DIMENSIONS
#define STBI__BYTECAST(x)
#define STBI__MARKER_none
#define stbi__float2int(x)
#define stbi__vertically_flip_on_load
stbi_uc *(* resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1, int w, int hs)
#define STBI_REALLOC_SIZED(p, oldsz, newsz)
#define stbi__float2fixed(x)
#define stbi__de_iphone_flag
#define stbi__unpremultiply_on_load
#define STBI__COMBO(a, b)