Skip to content

Instantly share code, notes, and snippets.

@Zeno-

Zeno-/gist:3e6e2ae6644e5c8c7e29 Secret

Created Mar 23, 2015
Embed
What would you like to do?
/* Upscale textures to user's requested minimum size. This is a trick to make
* filters look as good on low-res textures as on high-res ones, by making
* low-res textures BECOME high-res ones. This is helpful for worlds that
* mix high- and low-res textures, or for mods with least-common-denominator
* textures that don't have the resources to offer high-res alternatives.
*/
video::IImage *textureMinSizeUpscale(video::IVideoDriver *driver, video::IImage *orig) {
if(orig == NULL)
return orig;
s32 scaleto = g_settings->getS32("texture_min_size");
if (scaleto < 2)
return orig;
const core::dimension2d<u32> dim = orig->getDimension();
// Don't upscale 1px images. They don't benefit from it anyway
// (wouldn't have been blurred) and MIGHT be sun/moon tonemaps.
if (dim.Width <= 1 || dim.Height <= 1)
return orig;
/* Calculate scaling needed to make the shortest texture dimension
* equal to the target minimum. If e.g. this is a vertical frames
* animation, the short dimension will be the real size.
*/
u32 xscale = scaleto / dim.Width;
u32 yscale = scaleto / dim.Height;
u32 scale = (xscale > yscale) ? xscale : yscale;
// Never downscale; only scale up by 2x or more.
if (scale > 1) {
u32 w = scale * dim.Width;
u32 h = scale * dim.Height;
const core::dimension2d<u32> newdim = core::dimension2d<u32>(w, h);
video::IImage *newimg = driver->createImage(
orig->getColorFormat(), newdim);
orig->copyToScaling(newimg);
return newimg;
}
return orig;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment