Clamping texture sizes

This commit is contained in:
mid 2025-05-10 19:17:23 +03:00
parent 943a8cd1d9
commit 3c780c7290

View File

@ -253,15 +253,27 @@ static int refresh_texture(struct ResManRes *res) {
int w, h, origN; int w, h, origN;
unsigned char *data = stbi_load(namebuf, &w, &h, &origN, n); unsigned char *data = stbi_load(namebuf, &w, &h, &origN, n);
int stbifree = 1;
if(TextureResolutionReduction && data) { int newW = w, newH = h;
int newW = w >> TextureResolutionReduction;
int newH = h >> TextureResolutionReduction; if(TextureResolutionReduction) {
newW = newW >> TextureResolutionReduction;
if(newW <= 4) newW = 4; newH = newH >> TextureResolutionReduction;
if(newH <= 4) newH = 4; }
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); 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); 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; w = newW;
h = newH; h = newH;
data = data2; data = data2;
stbifree = 0;
} else { } else {
free(data2); free(data2);
} }
@ -283,12 +294,7 @@ static int refresh_texture(struct ResManRes *res) {
if(data) { if(data) {
k3TexUpdate(res->thing, type, 0, w, h, data); k3TexUpdate(res->thing, type, 0, w, h, data);
free(data);
if(stbifree) {
stbi_image_free(data);
} else {
free(data);
}
} }
} }