Skip to content
Myr

Merge branch 'optimize' of https://github.com/rygorous/stb into working

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Conflicts: stb_image.h Also disable VC6 automatic precompiled headers which were enabled in some projects.

authored by Sean Barrett · committed by Sean Barrett · parents 8ac015c, 1996a01 · Browse files · 8 files+72 added−42 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+72 added−42 removed

  • tests:+12 added−6 removed
  • other:+60 added−36 removed

8 files+72 added−42 removed

modified:tests/c_lexer_test.dsp+2 added-1 removed
62 unmodified lines
63# PROP Output_Dir "Debug"64# PROP Intermediate_Dir "Debug\c_lexer_test"65# PROP Target_Dir ""66# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c68# SUBTRACT CPP /YX69# ADD BASE RSC /l 0x409 /d "_DEBUG"70# ADD RSC /l 0x409 /d "_DEBUG"71BSC32=bscmake.exe72# ADD BASE BSC32 /nologo
17 unmodified lines
modified:tests/image_test.c+2 added-1 removed
41 unmodified lines
42 for (i=0; i < stb_arr_len(files); ++i) {43 int n;44 char **failed = NULL;45 unsigned char *data;46 printf(".");46 printf("%s\n", files[i]);47 //printf("%s\n", files[i]);48 data = stbi_load(files[i], &w, &h, &n, 0); if (data) free(data); else stb_arr_push(failed, "&n");49 data = stbi_load(files[i], &w, &h, 0, 1); if (data) free(data); else stb_arr_push(failed, "1");50 data = stbi_load(files[i], &w, &h, 0, 2); if (data) free(data); else stb_arr_push(failed, "2");51 data = stbi_load(files[i], &w, &h, 0, 3); if (data) free(data); else stb_arr_push(failed, "3");
41 unmodified lines
modified:tests/image_test.dsp+2 added-1 removed
62 unmodified lines
63# PROP Output_Dir "Debug"64# PROP Intermediate_Dir "Debug\image_test"65# PROP Target_Dir ""66# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c68# SUBTRACT CPP /YX69# ADD BASE RSC /l 0x409 /d "_DEBUG"70# ADD RSC /l 0x409 /d "_DEBUG"71BSC32=bscmake.exe72# ADD BASE BSC32 /nologo
25 unmodified lines
modified:tests/resize.dsp+2 added-1 removed
63 unmodified lines
64# PROP Output_Dir "Debug"65# PROP Intermediate_Dir "Debug"66# PROP Target_Dir ""67# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68# ADD CPP /nologo /W3 /WX /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68# ADD CPP /nologo /W3 /WX /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c69# SUBTRACT CPP /YX70# ADD BASE RSC /l 0x409 /d "_DEBUG"71# ADD RSC /l 0x409 /d "_DEBUG"72BSC32=bscmake.exe73# ADD BASE BSC32 /nologo
21 unmodified lines
modified:tests/stb_cpp.dsp+2 added-1 removed
63 unmodified lines
64# PROP Intermediate_Dir "Debug\stb_cpp"65# PROP Ignore_Export_Lib 066# PROP Target_Dir ""67# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68# ADD CPP /nologo /MTd /W3 /GX /Zd /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68# ADD CPP /nologo /MTd /W3 /GX /Zd /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c69# SUBTRACT CPP /YX70# ADD BASE RSC /l 0x409 /d "_DEBUG"71# ADD RSC /l 0x409 /d "_DEBUG"72BSC32=bscmake.exe73# ADD BASE BSC32 /nologo
25 unmodified lines
modified:tests/stretch_test.dsp+2 added-1 removed
62 unmodified lines
63# PROP Output_Dir "Debug"64# PROP Intermediate_Dir "Debug\stretch_test"65# PROP Target_Dir ""66# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c68# SUBTRACT CPP /YX69# ADD BASE RSC /l 0x409 /d "_DEBUG"70# ADD RSC /l 0x409 /d "_DEBUG"71BSC32=bscmake.exe72# ADD BASE BSC32 /nologo
17 unmodified lines
modified:tools/make_readme.dsp+2 added-1 removed
62 unmodified lines
63# PROP Output_Dir "Debug"64# PROP Intermediate_Dir "Debug\make_readme"65# PROP Target_Dir ""66# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c68# SUBTRACT CPP /YX69# ADD BASE RSC /l 0x409 /d "_DEBUG"70# ADD RSC /l 0x409 /d "_DEBUG"71BSC32=bscmake.exe72# ADD BASE BSC32 /nologo
21 unmodified lines
modified:stb_image.h+58 added-35 removed
583 unmodified lines
584}585586#ifndef STBI_NO_STDIO587588FILE *stbi__fopen(char const *filename, char const *mode)588static FILE *stbi__fopen(char const *filename, char const *mode)589{590 FILE *f;591#if defined(_MSC_VER) && _MSC_VER >= 1400592 if (0 != fopen_s(&f, filename, mode))
35 unmodified lines
628 stbi__start_mem(&s,buffer,len);629 return stbi_load_main(&s,x,y,comp,req_comp);630}631632unsigned char *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)632STBIDEF unsigned char *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)633{634 stbi__context s;635 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);636 return stbi_load_main(&s,x,y,comp,req_comp);637}638639#ifndef STBI_NO_HDR640641float *stbi_loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)641static float *stbi_loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)642{643 unsigned char *data;644 #ifndef STBI_NO_HDR645 if (stbi__hdr_test(s))
4 unmodified lines
650 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);651 return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");652}653654float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)654STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)655{656 stbi__context s;657 stbi__start_mem(&s,buffer,len);658 return stbi_loadf_main(&s,x,y,comp,req_comp);659}660661float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)661STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)662{663 stbi__context s;664 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);665 return stbi_loadf_main(&s,x,y,comp,req_comp);666}667668#ifndef STBI_NO_STDIO669float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)669STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)670{671 float *result;672 FILE *f = stbi__fopen(filename, "rb");673 if (!f) return stbi__errpf("can't fopen", "Unable to open file");
1 unmodified line
675 fclose(f);676 return result;677}678679float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)679STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)680{681 stbi__context s;682 stbi__start_file(&s,f);683 return stbi_loadf_main(&s,x,y,comp,req_comp);
1352 unmodified lines
2036 int code, next_code[16], sizes[17];20372038 // DEFLATE spec for generating codes2039 memset(sizes, 0, sizeof(sizes));2040 memset(z->fast, 255, sizeof(z->fast));2040 memset(z->fast, 0, sizeof(z->fast));2041 for (i=0; i < num; ++i) 2042 ++sizes[sizelist[i]];2043 sizes[0] = 0;2044 for (i=1; i < 16; ++i)
14 unmodified lines
2059 for (i=0; i < num; ++i) {2060 int s = sizelist[i];2061 if (s) {2062 int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];2063 stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i);2064 z->size [c] = (stbi_uc ) s;2065 z->value[c] = (stbi__uint16) i;2066 if (s <= STBI__ZFAST_BITS) {2067 int k = stbi__bit_reverse(next_code[s],s);2068 while (k < (1 << STBI__ZFAST_BITS)) {2068 z->fast[k] = (stbi__uint16) c;2069 z->fast[k] = fastv;2070 k += (1 << s);2071 }2072 }2073 ++next_code[s];
46 unmodified lines
2120 z->num_bits -= n;2121 return k; 2122}21232123stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)2124static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)2125{2126 int b,s,k;2126 if (a->num_bits < 16) stbi__fill_bits(a);2127 b = z->fast[a->code_buffer & STBI__ZFAST_MASK];2128 if (b < 0xffff) {2129 s = z->size[b];2130 a->code_buffer >>= s;2131 a->num_bits -= s;2132 return z->value[b];2133 }21342127 // not resolved by fast table, so compute it the slow way2128 // use jpeg approach, which requires MSbits at top2129 k = stbi__bit_reverse(a->code_buffer, 16);2130 for (s=STBI__ZFAST_BITS+1; ; ++s)
7 unmodified lines
2138 a->num_bits -= s;2139 return z->value[b];2140}21412150static int stbi__zexpand(stbi__zbuf *z, int n) // need to make room for n bytes2142stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)2143{2144 int b,s;2145 if (a->num_bits < 16) stbi__fill_bits(a);2146 b = z->fast[a->code_buffer & STBI__ZFAST_MASK];2147 if (b) {2148 s = b >> 9;2149 a->code_buffer >>= s;2150 a->num_bits -= s;2151 return b & 511;2152 }2153 return stbi__zhuffman_decode_slowpath(a, z);2154}21552156static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes2157{2158 char *q;2159 int cur, limit;2160 z->zout = zout;2161 if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");2162 cur = (int) (z->zout - z->zout_start);2163 limit = (int) (z->zout_end - z->zout_start);2164 while (cur + n > limit)
21 unmodified lines
2186{ 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};21872188static int stbi__parse_huffman_block(stbi__zbuf *a)2189{2190 char *zout = a->zout;2191 for(;;) {2192 int z = stbi__zhuffman_decode(a, &a->z_length);2193 if (z < 256) {2194 if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes2187 if (a->zout >= a->zout_end) if (!stbi__zexpand(a, 1)) return 0;2188 *a->zout++ = (char) z;2195 if (zout >= a->zout_end) {2196 if (!stbi__zexpand(a, zout, 1)) return 0;2197 zout = a->zout;2198 }2199 *zout++ = (char) z;2200 } else {2201 stbi_uc *p;2202 int len,dist;2192 if (z == 256) return 1;2203 if (z == 256) {2204 a->zout = zout;2205 return 1;2206 }2207 z -= 257;2208 len = stbi__zlength_base[z];2209 if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);2210 z = stbi__zhuffman_decode(a, &a->z_distance);2211 if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");2212 dist = stbi__zdist_base[z];2213 if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);2200 if (a->zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");2201 if (a->zout + len > a->zout_end) if (!stbi__zexpand(a, len)) return 0;2202 p = (stbi_uc *) (a->zout - dist);2203 while (len--)2204 *a->zout++ = *p++;2214 if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");2215 if (zout + len > a->zout_end) {2216 if (!stbi__zexpand(a, zout, len)) return 0;2217 zout = a->zout;2218 }2219 p = (stbi_uc *) (zout - dist);2220 if (dist == 1) { // run of one byte; common in images.2221 stbi_uc v = *p;2222 do *zout++ = v; while (--len);2223 } else {2224 do *zout++ = *p++; while (--len);2225 }2226 }2227 }2228}2229
64 unmodified lines
2294 nlen = header[3] * 256 + header[2];2295 if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG");2296 if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG");2297 if (a->zout + len > a->zout_end)2277 if (!stbi__zexpand(a, len)) return 0;2298 if (!stbi__zexpand(a, a->zout, len)) return 0;2299 memcpy(a->zout, a->zbuffer, len);2300 a->zbuffer += len;2301 a->zout += len;2302 return 1;
271 unmodified lines
2574 cur += 1;2575 prior += 1;2576 }25772557 // @TODO: special case filter_bytes = 1, or just rewrite whole thing to not use a nested loop25582578 // this is a little gross, so that we don't switch per-pixel or per-component2579 if (depth < 8 || img_n == out_n) {2580 int nk = (width - 1)*img_n;2581 #define CASE(f) \2582 case f: \2563 for (i=width-1; i >= 1; --i, raw+=filter_bytes,cur+=filter_bytes,prior+=filter_bytes) \2564 for (k=0; k < filter_bytes; ++k)2583 for (k=0; k < nk; ++k)2584 switch (filter) {2566 CASE(STBI__F_none) cur[k] = raw[k]; break;2585 // "none" filter turns into a memcpy here; make that explicit.2586 case STBI__F_none: memcpy(cur, raw, nk); break;2587 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); break;2588 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;2589 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); break;2590 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); break;2591 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); break;2592 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); break;2593 }2594 #undef CASE2595 raw += nk;2596 } else {2597 STBI_ASSERT(img_n+1 == out_n);2598 #define CASE(f) \2599 case f: \
354 unmodified lines
2954 stbi__uint32 raw_len;2955 if (first) return stbi__err("first not IHDR", "Corrupt PNG");2956 if (scan != SCAN_load) return 1;2957 if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");2958 // initial guess for decoded data size to avoid unnecessary reallocs2959 raw_len = s->img_x * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;2937 z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !is_iphone);2960 z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);2961 if (z->expanded == NULL) return 0; // zlib should set error2962 free(z->idata); z->idata = NULL;2963 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)2964 s->img_out_n = s->img_n+1;
1858 unmodified lines