2015-03-09 14:32:11 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2015 Aaron Suen <warr1024@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "guiscalingfilter.h"
|
|
|
|
#include "imagefilters.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "util/numeric.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* Maintain a static cache to store the images that correspond to textures
|
|
|
|
* in a format that's manipulable by code. Some platforms exhibit issues
|
|
|
|
* converting textures back into images repeatedly, and some don't even
|
|
|
|
* allow it at all.
|
|
|
|
*/
|
2015-04-01 06:19:10 +02:00
|
|
|
std::map<io::path, video::IImage *> g_imgCache;
|
2015-03-09 14:32:11 +01:00
|
|
|
|
|
|
|
/* Maintain a static cache of all pre-scaled textures. These need to be
|
|
|
|
* cleared as well when the cached images.
|
|
|
|
*/
|
2015-04-01 06:19:10 +02:00
|
|
|
std::map<io::path, video::ITexture *> g_txrCache;
|
2015-03-09 14:32:11 +01:00
|
|
|
|
|
|
|
/* Manually insert an image into the cache, useful to avoid texture-to-image
|
|
|
|
* conversion whenever we can intercept it.
|
|
|
|
*/
|
2015-04-01 06:19:10 +02:00
|
|
|
void guiScalingCache(io::path key, video::IVideoDriver *driver, video::IImage *value)
|
|
|
|
{
|
2015-03-09 14:32:11 +01:00
|
|
|
if (!g_settings->getBool("gui_scaling_filter"))
|
|
|
|
return;
|
|
|
|
video::IImage *copied = driver->createImage(value->getColorFormat(),
|
|
|
|
value->getDimension());
|
|
|
|
value->copyTo(copied);
|
2015-04-01 06:19:10 +02:00
|
|
|
g_imgCache[key] = copied;
|
2015-03-09 14:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Manually clear the cache, e.g. when switching to different worlds.
|
2015-04-01 06:19:10 +02:00
|
|
|
void guiScalingCacheClear(video::IVideoDriver *driver)
|
|
|
|
{
|
|
|
|
for (std::map<io::path, video::IImage *>::iterator it = g_imgCache.begin();
|
2015-08-25 22:23:05 +02:00
|
|
|
it != g_imgCache.end(); ++it) {
|
2015-03-09 14:32:11 +01:00
|
|
|
if (it->second != NULL)
|
|
|
|
it->second->drop();
|
|
|
|
}
|
2015-04-01 06:19:10 +02:00
|
|
|
g_imgCache.clear();
|
|
|
|
for (std::map<io::path, video::ITexture *>::iterator it = g_txrCache.begin();
|
2015-08-25 22:23:05 +02:00
|
|
|
it != g_txrCache.end(); ++it) {
|
2015-03-09 14:32:11 +01:00
|
|
|
if (it->second != NULL)
|
|
|
|
driver->removeTexture(it->second);
|
|
|
|
}
|
2015-04-01 06:19:10 +02:00
|
|
|
g_txrCache.clear();
|
2015-03-09 14:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a cached, high-quality pre-scaled texture for display purposes. If the
|
|
|
|
* texture is not already cached, attempt to create it. Returns a pre-scaled texture,
|
|
|
|
* or the original texture if unable to pre-scale it.
|
|
|
|
*/
|
2015-04-01 06:19:10 +02:00
|
|
|
video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver,
|
|
|
|
video::ITexture *src, const core::rect<s32> &srcrect,
|
|
|
|
const core::rect<s32> &destrect)
|
|
|
|
{
|
2015-04-02 14:58:18 +02:00
|
|
|
if (src == NULL)
|
|
|
|
return src;
|
2015-03-09 14:32:11 +01:00
|
|
|
if (!g_settings->getBool("gui_scaling_filter"))
|
|
|
|
return src;
|
|
|
|
|
|
|
|
// Calculate scaled texture name.
|
|
|
|
char rectstr[200];
|
2015-04-01 06:19:10 +02:00
|
|
|
snprintf(rectstr, sizeof(rectstr), "%d:%d:%d:%d:%d:%d",
|
2015-03-09 14:32:11 +01:00
|
|
|
srcrect.UpperLeftCorner.X,
|
|
|
|
srcrect.UpperLeftCorner.Y,
|
|
|
|
srcrect.getWidth(),
|
|
|
|
srcrect.getHeight(),
|
|
|
|
destrect.getWidth(),
|
|
|
|
destrect.getHeight());
|
|
|
|
io::path origname = src->getName().getPath();
|
|
|
|
io::path scalename = origname + "@guiScalingFilter:" + rectstr;
|
|
|
|
|
|
|
|
// Search for existing scaled texture.
|
2015-04-01 06:19:10 +02:00
|
|
|
video::ITexture *scaled = g_txrCache[scalename];
|
2015-03-09 14:32:11 +01:00
|
|
|
if (scaled)
|
|
|
|
return scaled;
|
|
|
|
|
|
|
|
// Try to find the texture converted to an image in the cache.
|
|
|
|
// If the image was not found, try to extract it from the texture.
|
2015-04-01 06:19:10 +02:00
|
|
|
video::IImage* srcimg = g_imgCache[origname];
|
2015-03-09 14:32:11 +01:00
|
|
|
if (srcimg == NULL) {
|
|
|
|
if (!g_settings->getBool("gui_scaling_filter_txr2img"))
|
|
|
|
return src;
|
|
|
|
srcimg = driver->createImageFromData(src->getColorFormat(),
|
|
|
|
src->getSize(), src->lock(), false);
|
|
|
|
src->unlock();
|
2015-04-01 06:19:10 +02:00
|
|
|
g_imgCache[origname] = srcimg;
|
2015-03-09 14:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new destination image and scale the source into it.
|
|
|
|
imageCleanTransparent(srcimg, 0);
|
|
|
|
video::IImage *destimg = driver->createImage(src->getColorFormat(),
|
|
|
|
core::dimension2d<u32>((u32)destrect.getWidth(),
|
|
|
|
(u32)destrect.getHeight()));
|
|
|
|
imageScaleNNAA(srcimg, srcrect, destimg);
|
|
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
// Android is very picky about textures being powers of 2, so expand
|
|
|
|
// the image dimensions to the next power of 2, if necessary, for
|
|
|
|
// that platform.
|
|
|
|
video::IImage *po2img = driver->createImage(src->getColorFormat(),
|
|
|
|
core::dimension2d<u32>(npot2((u32)destrect.getWidth()),
|
|
|
|
npot2((u32)destrect.getHeight())));
|
|
|
|
po2img->fill(video::SColor(0, 0, 0, 0));
|
|
|
|
destimg->copyTo(po2img);
|
|
|
|
destimg->drop();
|
|
|
|
destimg = po2img;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Convert the scaled image back into a texture.
|
|
|
|
scaled = driver->addTexture(scalename, destimg, NULL);
|
|
|
|
destimg->drop();
|
2015-04-01 06:19:10 +02:00
|
|
|
g_txrCache[scalename] = scaled;
|
2015-03-09 14:32:11 +01:00
|
|
|
|
|
|
|
return scaled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convenience wrapper for guiScalingResizeCached that accepts parameters that
|
|
|
|
* are available at GUI imagebutton creation time.
|
|
|
|
*/
|
2015-04-01 06:19:10 +02:00
|
|
|
video::ITexture *guiScalingImageButton(video::IVideoDriver *driver,
|
|
|
|
video::ITexture *src, s32 width, s32 height)
|
|
|
|
{
|
2015-04-02 14:58:18 +02:00
|
|
|
if (src == NULL)
|
|
|
|
return src;
|
2015-03-09 14:32:11 +01:00
|
|
|
return guiScalingResizeCached(driver, src,
|
|
|
|
core::rect<s32>(0, 0, src->getSize().Width, src->getSize().Height),
|
|
|
|
core::rect<s32>(0, 0, width, height));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replacement for driver->draw2DImage() that uses the high-quality pre-scaled
|
|
|
|
* texture, if configured.
|
|
|
|
*/
|
|
|
|
void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
|
|
|
|
const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
|
|
|
|
const core::rect<s32> *cliprect, const video::SColor *const colors,
|
2015-04-01 06:19:10 +02:00
|
|
|
bool usealpha)
|
|
|
|
{
|
2015-03-09 14:32:11 +01:00
|
|
|
// Attempt to pre-scale image in software in high quality.
|
|
|
|
video::ITexture *scaled = guiScalingResizeCached(driver, txr, srcrect, destrect);
|
2015-04-02 14:58:18 +02:00
|
|
|
if (scaled == NULL)
|
|
|
|
return;
|
2015-03-09 14:32:11 +01:00
|
|
|
|
|
|
|
// Correct source rect based on scaled image.
|
|
|
|
const core::rect<s32> mysrcrect = (scaled != txr)
|
|
|
|
? core::rect<s32>(0, 0, destrect.getWidth(), destrect.getHeight())
|
|
|
|
: srcrect;
|
|
|
|
|
|
|
|
driver->draw2DImage(scaled, destrect, mysrcrect, cliprect, colors, usealpha);
|
|
|
|
}
|