diff --git a/src/loaders.inc b/src/loaders.inc index d1ab3f0..e0b818a 100644 --- a/src/loaders.inc +++ b/src/loaders.inc @@ -253,15 +253,27 @@ static int refresh_texture(struct ResManRes *res) { int w, h, origN; unsigned char *data = stbi_load(namebuf, &w, &h, &origN, n); - int stbifree = 1; - if(TextureResolutionReduction && data) { - int newW = w >> TextureResolutionReduction; - int newH = h >> TextureResolutionReduction; - - if(newW <= 4) newW = 4; - if(newH <= 4) newH = 4; - + int newW = w, newH = h; + + if(TextureResolutionReduction) { + newW = newW >> TextureResolutionReduction; + newH = newH >> TextureResolutionReduction; + } + if(newW > k3TexSzMax()) { + newW = k3TexSzMax(); + } + if(newH > k3TexSzMax()) { + newH = k3TexSzMax(); + } + if(newW < 4) { + newW = 4; + } + if(newH < 4) { + newH = 4; + } + + if(newW != w || newH != h) { unsigned char *data2 = malloc(newW * newH * n); int success = stbir_resize_uint8_generic(data, w, h, 0, data2, newW, newH, 0, n, n == 4 ? 3 : -1, 0, STBIR_EDGE_WRAP, STBIR_FILTER_DEFAULT, gamma ? STBIR_COLORSPACE_SRGB : STBIR_COLORSPACE_LINEAR, NULL); @@ -271,7 +283,6 @@ static int refresh_texture(struct ResManRes *res) { w = newW; h = newH; data = data2; - stbifree = 0; } else { free(data2); } @@ -283,12 +294,7 @@ static int refresh_texture(struct ResManRes *res) { if(data) { k3TexUpdate(res->thing, type, 0, w, h, data); - - if(stbifree) { - stbi_image_free(data); - } else { - free(data); - } + free(data); } }