From 291ad22e84d18bea400873adc991b2d324bc7cda Mon Sep 17 00:00:00 2001 From: Thatcher Ulrich Date: Wed, 2 Mar 2016 15:31:07 -0500 Subject: [PATCH 1/8] Replace large stack allocations with dynamic allocations. --- stb_image.h | 39 ++++++++++++++++++++++----------------- stb_image_write.h | 3 ++- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/stb_image.h b/stb_image.h index e06f7a1..a128a5e 100644 --- a/stb_image.h +++ b/stb_image.h @@ -3408,10 +3408,13 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp static unsigned char *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp) { - stbi__jpeg j; - j.s = s; - stbi__setup_jpeg(&j); - return load_jpeg_image(&j, x,y,comp,req_comp); + unsigned char* result; + stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg)); + j->s = s; + stbi__setup_jpeg(j); + result = load_jpeg_image(j, x,y,comp,req_comp); + STBI_FREE(j); + return result; } static int stbi__jpeg_test(stbi__context *s) @@ -5649,13 +5652,15 @@ static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_in static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) { - stbi__gif g; - if (!stbi__gif_header(s, &g, comp, 1)) { + stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); + if (!stbi__gif_header(s, g, comp, 1)) { + STBI_FREE(g); stbi__rewind( s ); return 0; } - if (x) *x = g.w; - if (y) *y = g.h; + if (x) *x = g->w; + if (y) *y = g->h; + STBI_FREE(g); return 1; } @@ -5908,20 +5913,20 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i static stbi_uc *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp) { stbi_uc *u = 0; - stbi__gif g; - memset(&g, 0, sizeof(g)); + stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); + memset(g, 0, sizeof(*g)); - u = stbi__gif_load_next(s, &g, comp, req_comp); + u = stbi__gif_load_next(s, g, comp, req_comp); if (u == (stbi_uc *) s) u = 0; // end of animated gif marker if (u) { - *x = g.w; - *y = g.h; + *x = g->w; + *y = g->h; if (req_comp && req_comp != 4) - u = stbi__convert_format(u, 4, req_comp, g.w, g.h); + u = stbi__convert_format(u, 4, req_comp, g->w, g->h); } - else if (g.out) - STBI_FREE(g.out); - + else if (g->out) + STBI_FREE(g->out); + STBI_FREE(g); return u; } diff --git a/stb_image_write.h b/stb_image_write.h index 98fa410..0935dc1 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -736,7 +736,7 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l unsigned int bitbuf=0; int i,j, bitcount=0; unsigned char *out = NULL; - unsigned char **hash_table[stbiw__ZHASH]; // 64KB on the stack! + unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(char**)); if (quality < 5) quality = 5; stbiw__sbpush(out, 0x78); // DEFLATE 32K window @@ -808,6 +808,7 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l for (i=0; i < stbiw__ZHASH; ++i) (void) stbiw__sbfree(hash_table[i]); + STBIW_FREE(hash_table); { // compute adler32 on input From 7e1ee2d386b7ec334cf01660ced88426e5188a40 Mon Sep 17 00:00:00 2001 From: Thatcher Ulrich Date: Wed, 2 Mar 2016 15:56:53 -0500 Subject: [PATCH 2/8] Allocate large structure using malloc instead of stack. --- stb_image.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/stb_image.h b/stb_image.h index a128a5e..08d2f84 100644 --- a/stb_image.h +++ b/stb_image.h @@ -3442,9 +3442,12 @@ static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp) static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp) { - stbi__jpeg j; - j.s = s; - return stbi__jpeg_info_raw(&j, x, y, comp); + int result; + stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg))); + j->s = s; + result = stbi__jpeg_info_raw(j, x, y, comp); + STBI_FREE(j); + return result; } #endif From 94f2ceac1595d7297d76a734fcd1bc111622f6dd Mon Sep 17 00:00:00 2001 From: Matthew Gregan Date: Sat, 12 Mar 2016 01:55:59 +1300 Subject: [PATCH 3/8] Fix typo in stbi__parse_uncompressed_block --- stb_image.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb_image.h b/stb_image.h index e06f7a1..289d804 100644 --- a/stb_image.h +++ b/stb_image.h @@ -3732,7 +3732,7 @@ static int stbi__compute_huffman_codes(stbi__zbuf *a) return 1; } -static int stbi__parse_uncomperssed_block(stbi__zbuf *a) +static int stbi__parse_uncompressed_block(stbi__zbuf *a) { stbi_uc header[4]; int len,nlen,k; @@ -3798,7 +3798,7 @@ static int stbi__parse_zlib(stbi__zbuf *a, int parse_header) final = stbi__zreceive(a,1); type = stbi__zreceive(a,2); if (type == 0) { - if (!stbi__parse_uncomperssed_block(a)) return 0; + if (!stbi__parse_uncompressed_block(a)) return 0; } else if (type == 3) { return 0; } else { From 0985e893350d4a599d36de7da94e6bf4101ecdd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Heusipp?= Date: Thu, 17 Mar 2016 09:23:45 +0100 Subject: [PATCH 4/8] stb_vorbis: Fix memory leak in decode_residue() and inverse_mdct() when redefining temp_alloc() and temp_free() temp_alloc() and temp_free() are documented as customization points in section "MEMORY ALLOCATION" (stb_vorbis.c:81). However, in decode_residue() and inverse_mdct() (via temp_block_array() and temp_alloc() respectively), stb_vorbis allocates temporary memory but does not call temp_free() when finished. It does call temp_alloc_restore() though, but there is no sane way to provide an implementation thereof when using a malloc()/free()-like allocation backend. Adding calls to temp_free() before the respective calls to temp_alloc_restore() is safe, because in case of a non-empty temp_alloc_restore() implementation, temp_free() would simply be implemented empty (the current implementation of temp_*() is fine in this regard). That way, all possible temporary memory allocation schemes (i.e. alloca(), custom provided alloc_buffer, malloc()) are handled properly. Add the appropriate temp_free() calls. --- stb_vorbis.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stb_vorbis.c b/stb_vorbis.c index d54087f..e5fc13e 100644 --- a/stb_vorbis.c +++ b/stb_vorbis.c @@ -2325,6 +2325,11 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int } done: CHECK(f); + #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + temp_free(f,part_classdata); + #else + temp_free(f,classifications); + #endif temp_alloc_restore(f,temp_alloc_point); } @@ -2970,6 +2975,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) } } + temp_free(f,buf2); temp_alloc_restore(f,save_point); } From aeba55604a52690921ea3e94543d017906c6aa21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Heusipp?= Date: Thu, 24 Mar 2016 18:20:39 +0100 Subject: [PATCH 5/8] stb_vorbis: Fix memory leak in start_decoder(). --- stb_vorbis.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stb_vorbis.c b/stb_vorbis.c index 4cd02de..6baf9ec 100644 --- a/stb_vorbis.c +++ b/stb_vorbis.c @@ -3810,7 +3810,6 @@ static int start_decoder(vorb *f) } } } - setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); c->lookup_type = 2; } else @@ -3826,11 +3825,11 @@ static int start_decoder(vorb *f) if (c->sequence_p) last = val; } - setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); } #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK skip:; #endif + setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); CHECK(f); } From a8876b884d6a916497e534d0b241734a79f46db9 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sat, 2 Apr 2016 03:40:42 -0700 Subject: [PATCH 6/8] fix _WIN32 if STB_THREADS --- stb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb.h b/stb.h index 11350ba..89aac37 100644 --- a/stb.h +++ b/stb.h @@ -11205,7 +11205,7 @@ int stb_arith_decode_byte(stb_arith *a) // Threads // -#ifndef WIN32 +#ifndef _WIN32 #ifdef STB_THREADS #error "threads not implemented except for Windows" #endif From 591c7f8cb348a3915a4afa64e102e7b225119cd6 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sat, 2 Apr 2016 03:40:55 -0700 Subject: [PATCH 7/8] remove white matting when loading transparent PSD --- stb_image.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/stb_image.h b/stb_image.h index 80a4231..af663cf 100644 --- a/stb_image.h +++ b/stb_image.h @@ -201,10 +201,11 @@ Janez Zemva John Bartholomew Michal Cichon svdijk@github Jonathan Blow Ken Hamada Tero Hanninen Baldur Karlsson Laurent Gomila Cort Stratton Sergio Gonzalez romigrou@github - Aruelien Pocheville Thibault Reuille Cass Everitt + Aruelien Pocheville Thibault Reuille Cass Everitt Ryamond Barbiero Paul Du Bois Engin Manap + Michaelangel007@github Oriol Ferrer Mesia Blazej Dariusz Roszkowski - Michaelangel007@github + Oriol Ferrer Mesia LICENSE @@ -5453,6 +5454,21 @@ static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int } } + if (channelCount >= 3) { + for (i=0; i < w*h; ++i) { + unsigned char *pixel = out + 4*i; + if (pixel[3] != 0 && pixel[3] != 255) { + // remove weird white matte from PSD + float a = pixel[3] / 255.0f; + float ra = 1.0f / a; + float inv_a = 255.0f * (1 - ra); + pixel[0] = (unsigned char) (pixel[0]*ra + inv_a); + pixel[1] = (unsigned char) (pixel[1]*ra + inv_a); + pixel[2] = (unsigned char) (pixel[2]*ra + inv_a); + } + } + } + if (req_comp && req_comp != 4) { out = stbi__convert_format(out, 4, req_comp, w, h); if (out == NULL) return out; // stbi__convert_format frees input on failure From 75c5908f951513bdc619fd7c9cb3b96283d4f397 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sat, 2 Apr 2016 03:41:26 -0700 Subject: [PATCH 8/8] fix includes for linux alloca --- stb_vorbis.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stb_vorbis.c b/stb_vorbis.c index 6baf9ec..0642c11 100644 --- a/stb_vorbis.c +++ b/stb_vorbis.c @@ -552,6 +552,9 @@ enum STBVorbisError #include #if !(defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh)) #include +#if defined(__linux__) || defined(__linux) +#include +#endif #endif #else // STB_VORBIS_NO_CRT #define NULL 0