2 Copyright (C) 2015 Aaron Suen <warr1024@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "guiscalingfilter.h"
20 #include "imagefilters.h"
22 #include "main.h" // for g_settings
23 #include "util/numeric.h"
26 /* Maintain a static cache to store the images that correspond to textures
27 * in a format that's manipulable by code. Some platforms exhibit issues
28 * converting textures back into images repeatedly, and some don't even
31 std::map<io::path, video::IImage *> imgCache;
33 /* Maintain a static cache of all pre-scaled textures. These need to be
34 * cleared as well when the cached images.
36 std::map<io::path, video::ITexture *> txrCache;
38 /* Manually insert an image into the cache, useful to avoid texture-to-image
39 * conversion whenever we can intercept it.
41 void guiScalingCache(io::path key, video::IVideoDriver *driver, video::IImage *value) {
42 if (!g_settings->getBool("gui_scaling_filter"))
44 video::IImage *copied = driver->createImage(value->getColorFormat(),
45 value->getDimension());
46 value->copyTo(copied);
47 imgCache[key] = copied;
50 // Manually clear the cache, e.g. when switching to different worlds.
51 void guiScalingCacheClear(video::IVideoDriver *driver) {
52 for (std::map<io::path, video::IImage *>::iterator it = imgCache.begin();
53 it != imgCache.end(); it++) {
54 if (it->second != NULL)
58 for (std::map<io::path, video::ITexture *>::iterator it = txrCache.begin();
59 it != txrCache.end(); it++) {
60 if (it->second != NULL)
61 driver->removeTexture(it->second);
66 /* Get a cached, high-quality pre-scaled texture for display purposes. If the
67 * texture is not already cached, attempt to create it. Returns a pre-scaled texture,
68 * or the original texture if unable to pre-scale it.
70 video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver, video::ITexture *src,
71 const core::rect<s32> &srcrect, const core::rect<s32> &destrect) {
73 if (!g_settings->getBool("gui_scaling_filter"))
76 // Calculate scaled texture name.
78 sprintf(rectstr, "%d:%d:%d:%d:%d:%d",
79 srcrect.UpperLeftCorner.X,
80 srcrect.UpperLeftCorner.Y,
84 destrect.getHeight());
85 io::path origname = src->getName().getPath();
86 io::path scalename = origname + "@guiScalingFilter:" + rectstr;
88 // Search for existing scaled texture.
89 video::ITexture *scaled = txrCache[scalename];
93 // Try to find the texture converted to an image in the cache.
94 // If the image was not found, try to extract it from the texture.
95 video::IImage* srcimg = imgCache[origname];
97 if (!g_settings->getBool("gui_scaling_filter_txr2img"))
99 srcimg = driver->createImageFromData(src->getColorFormat(),
100 src->getSize(), src->lock(), false);
102 imgCache[origname] = srcimg;
105 // Create a new destination image and scale the source into it.
106 imageCleanTransparent(srcimg, 0);
107 video::IImage *destimg = driver->createImage(src->getColorFormat(),
108 core::dimension2d<u32>((u32)destrect.getWidth(),
109 (u32)destrect.getHeight()));
110 imageScaleNNAA(srcimg, srcrect, destimg);
113 // Android is very picky about textures being powers of 2, so expand
114 // the image dimensions to the next power of 2, if necessary, for
116 video::IImage *po2img = driver->createImage(src->getColorFormat(),
117 core::dimension2d<u32>(npot2((u32)destrect.getWidth()),
118 npot2((u32)destrect.getHeight())));
119 po2img->fill(video::SColor(0, 0, 0, 0));
120 destimg->copyTo(po2img);
125 // Convert the scaled image back into a texture.
126 scaled = driver->addTexture(scalename, destimg, NULL);
128 txrCache[scalename] = scaled;
133 /* Convenience wrapper for guiScalingResizeCached that accepts parameters that
134 * are available at GUI imagebutton creation time.
136 video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, video::ITexture *src,
137 s32 width, s32 height) {
138 return guiScalingResizeCached(driver, src,
139 core::rect<s32>(0, 0, src->getSize().Width, src->getSize().Height),
140 core::rect<s32>(0, 0, width, height));
143 /* Replacement for driver->draw2DImage() that uses the high-quality pre-scaled
144 * texture, if configured.
146 void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
147 const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
148 const core::rect<s32> *cliprect, const video::SColor *const colors,
151 // Attempt to pre-scale image in software in high quality.
152 video::ITexture *scaled = guiScalingResizeCached(driver, txr, srcrect, destrect);
154 // Correct source rect based on scaled image.
155 const core::rect<s32> mysrcrect = (scaled != txr)
156 ? core::rect<s32>(0, 0, destrect.getWidth(), destrect.getHeight())
159 driver->draw2DImage(scaled, destrect, mysrcrect, cliprect, colors, usealpha);