1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-10 11:25:04 -05:00
filebot/source/net/filebot/ResourceManager.java

100 lines
3.1 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot;
2018-03-05 06:44:24 -05:00
import static java.util.Collections.*;
2016-09-26 04:17:00 -04:00
import static java.util.stream.Collectors.*;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
2018-03-05 06:44:24 -05:00
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
2018-03-05 06:44:24 -05:00
import java.util.HashMap;
import java.util.List;
2018-03-05 06:44:24 -05:00
import java.util.Map;
import java.util.Objects;
2016-09-26 04:17:00 -04:00
import java.util.stream.Stream;
2018-03-05 06:44:24 -05:00
import javax.imageio.ImageIO;
2009-05-13 14:20:26 -04:00
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Method;
import org.imgscalr.Scalr.Mode;
2008-10-10 15:20:37 -04:00
public final class ResourceManager {
2018-03-05 06:44:24 -05:00
private static final Map<String, Icon> cache = synchronizedMap(new HashMap<String, Icon>(256));
2015-11-20 03:26:57 -05:00
2018-03-06 02:04:24 -05:00
public static Icon getIcon(String name) {
return cache.computeIfAbsent(name, i -> {
2018-03-05 06:44:24 -05:00
// load image
URL[] resource = getMultiResolutionImageResource(i);
if (resource.length > 0) {
return new ImageIcon(getMultiResolutionImage(resource));
2018-03-05 06:44:24 -05:00
}
2015-11-20 03:26:57 -05:00
2018-03-06 02:04:24 -05:00
// default image
return null;
2018-03-05 06:44:24 -05:00
});
}
public static Stream<URL> getApplicationIconResources() {
2018-03-06 02:36:01 -05:00
return Stream.of("window.icon16", "window.icon64").map(ResourceManager::getImageResource);
}
2018-03-05 06:44:24 -05:00
public static List<Image> getApplicationIconImages() {
2018-03-06 02:36:01 -05:00
return Stream.of("window.icon16", "window.icon64").map(ResourceManager::getMultiResolutionImageResource).map(ResourceManager::getMultiResolutionImage).collect(toList());
}
2009-05-13 14:20:26 -04:00
public static Icon getFlagIcon(String languageCode) {
2015-11-20 09:04:49 -05:00
return getIcon("flags/" + languageCode);
}
2018-03-05 06:44:24 -05:00
private static Image getMultiResolutionImage(URL[] resource) {
try {
// Load multi-resolution images only if necessary
if (PRIMARY_SCALE_FACTOR == 1) {
return ImageIO.read(resource[0]);
}
List<BufferedImage> image = new ArrayList<BufferedImage>(resource.length);
for (URL r : resource) {
image.add(ImageIO.read(r));
2018-03-05 06:44:24 -05:00
}
// Windows 10: use @2x image for non-integer scale factors 1.25 / 1.5 / 1.75
if (PRIMARY_SCALE_FACTOR != 1 && PRIMARY_SCALE_FACTOR != 2) {
BufferedImage hidpi = image.get(image.size() - 1);
if (PRIMARY_SCALE_FACTOR < 2) {
image.add(1, scale(PRIMARY_SCALE_FACTOR, hidpi));
} else {
image.add(scale(PRIMARY_SCALE_FACTOR, hidpi));
}
}
return new BaseMultiResolutionImage(image.toArray(new Image[0]));
2018-03-05 06:44:24 -05:00
} catch (Exception e) {
throw new RuntimeException(e);
}
}
2018-03-05 06:44:24 -05:00
private static URL[] getMultiResolutionImageResource(String name) {
return Stream.of(name, name + "@2x").map(ResourceManager::getImageResource).filter(Objects::nonNull).toArray(URL[]::new);
}
2018-03-05 06:44:24 -05:00
private static URL getImageResource(String name) {
return ResourceManager.class.getResource("resources/" + name + ".png");
}
public static final double PRIMARY_SCALE_FACTOR = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getDefaultTransform().getScaleX();
private static BufferedImage scale(double scale, BufferedImage image) {
int w = (int) (scale * image.getWidth());
int h = (int) (scale * image.getHeight());
return Scalr.resize(image, Method.ULTRA_QUALITY, Mode.FIT_TO_WIDTH, w, h, Scalr.OP_ANTIALIAS);
}
}