]> git.lizzy.rs Git - minetest.git/blob - src/guiscalingfilter.cpp
92dadeaec6c75351695b89df9dd7f700250cc245
[minetest.git] / src / guiscalingfilter.cpp
1 /*
2 Copyright (C) 2015 Aaron Suen <warr1024@gmail.com>
3
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.
8
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.
13
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.
17 */
18
19 #include "guiscalingfilter.h"
20 #include "imagefilters.h"
21 #include "settings.h"
22 #include "main.h"               // for g_settings
23 #include "util/numeric.h"
24 #include <stdio.h>
25
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
29  * allow it at all.
30  */
31 std::map<io::path, video::IImage *> imgCache;
32
33 /* Maintain a static cache of all pre-scaled textures.  These need to be
34  * cleared as well when the cached images.
35  */
36 std::map<io::path, video::ITexture *> txrCache;
37
38 /* Manually insert an image into the cache, useful to avoid texture-to-image
39  * conversion whenever we can intercept it.
40  */
41 void guiScalingCache(io::path key, video::IVideoDriver *driver, video::IImage *value) {
42         if (!g_settings->getBool("gui_scaling_filter"))
43                 return;
44         video::IImage *copied = driver->createImage(value->getColorFormat(),
45                         value->getDimension());
46         value->copyTo(copied);
47         imgCache[key] = copied;
48 }
49
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)
55                         it->second->drop();
56         }
57         imgCache.clear();
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);
62         }
63         txrCache.clear();
64 }
65
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.
69  */
70 video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver, video::ITexture *src,
71                 const core::rect<s32> &srcrect, const core::rect<s32> &destrect) {
72
73         if (!g_settings->getBool("gui_scaling_filter"))
74                 return src;
75
76         // Calculate scaled texture name.
77         char rectstr[200];
78         sprintf(rectstr, "%d:%d:%d:%d:%d:%d",
79                 srcrect.UpperLeftCorner.X,
80                 srcrect.UpperLeftCorner.Y,
81                 srcrect.getWidth(),
82                 srcrect.getHeight(),
83                 destrect.getWidth(),
84                 destrect.getHeight());
85         io::path origname = src->getName().getPath();
86         io::path scalename = origname + "@guiScalingFilter:" + rectstr;
87
88         // Search for existing scaled texture.
89         video::ITexture *scaled = txrCache[scalename];
90         if (scaled)
91                 return scaled;
92
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];
96         if (srcimg == NULL) {
97                 if (!g_settings->getBool("gui_scaling_filter_txr2img"))
98                         return src;
99                 srcimg = driver->createImageFromData(src->getColorFormat(),
100                         src->getSize(), src->lock(), false);
101                 src->unlock();
102                 imgCache[origname] = srcimg;
103         }
104
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);
111
112 #ifdef __ANDROID__
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
115         // that platform.
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);
121         destimg->drop();
122         destimg = po2img;
123 #endif
124
125         // Convert the scaled image back into a texture.
126         scaled = driver->addTexture(scalename, destimg, NULL);
127         destimg->drop();
128         txrCache[scalename] = scaled;
129
130         return scaled;
131 }
132
133 /* Convenience wrapper for guiScalingResizeCached that accepts parameters that
134  * are available at GUI imagebutton creation time.
135  */
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));
141 }
142
143 /* Replacement for driver->draw2DImage() that uses the high-quality pre-scaled
144  * texture, if configured.
145  */
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,
149                 bool usealpha) {
150
151         // Attempt to pre-scale image in software in high quality.
152         video::ITexture *scaled = guiScalingResizeCached(driver, txr, srcrect, destrect);
153
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())
157                 : srcrect;
158
159         driver->draw2DImage(scaled, destrect, mysrcrect, cliprect, colors, usealpha);
160 }