Improve icon scaling for 1.25x / 1.5x / 1.75x scale factors

This commit is contained in:
Reinhard Pointner 2018-07-11 18:06:18 +07:00
parent b1e32bb503
commit e2a9d51df6
1 changed files with 45 additions and 5 deletions

View File

@ -3,9 +3,14 @@ package net.filebot;
import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -16,6 +21,8 @@ import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import net.filebot.util.SystemProperty;
public final class ResourceManager {
private static final Map<String, Icon> cache = synchronizedMap(new HashMap<String, Icon>(256));
@ -25,7 +32,7 @@ public final class ResourceManager {
// load image
URL[] resource = getMultiResolutionImageResource(i);
if (resource.length > 0) {
return new ImageIcon(getMultiResolutionImage(resource));
return getMultiResolutionIcon(resource);
}
// default image
@ -47,11 +54,28 @@ public final class ResourceManager {
private static Image getMultiResolutionImage(URL[] resource) {
try {
Image[] image = new Image[resource.length];
for (int i = 0; i < image.length; i++) {
image[i] = ImageIO.read(resource[i]);
List<BufferedImage> image = new ArrayList<BufferedImage>(resource.length);
for (URL r : resource) {
image.add(ImageIO.read(r));
}
return new BaseMultiResolutionImage(image);
return new BaseMultiResolutionImage(image.toArray(new Image[0]));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Icon getMultiResolutionIcon(URL[] resource) {
if (PRIMARY_SCALE_FACTOR == 1 || PRIMARY_SCALE_FACTOR == 2) {
return new ImageIcon(getMultiResolutionImage(resource));
}
try {
BufferedImage[] image = new BufferedImage[resource.length + 1];
for (int i = 0; i < resource.length; i++) {
image[i + 1] = ImageIO.read(resource[i]);
}
image[0] = scale(PRIMARY_SCALE_FACTOR, image[image.length - 1]);
return new ImageIcon(new BaseMultiResolutionImage(1, image));
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -65,6 +89,22 @@ public final class ResourceManager {
return ResourceManager.class.getResource("resources/" + name + ".png");
}
private static final float PRIMARY_SCALE_FACTOR = SystemProperty.of("sun.java2d.uiScale", Float::parseFloat, Toolkit.getDefaultToolkit().getScreenResolution() / 96f).get();
private static BufferedImage scale(float scale, BufferedImage image) {
int w = (int) (scale * image.getWidth());
int h = (int) (scale * image.getHeight());
BufferedImage scaledImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = scaledImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(image, 0, 0, w, h, 0, 0, image.getWidth(), image.getHeight(), null);
g2d.dispose();
return scaledImage;
}
private ResourceManager() {
throw new UnsupportedOperationException();
}