mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 04:38:00 +00:00
cleanup in ImageCache
This commit is contained in:
@@ -60,6 +60,7 @@ public class ImageCache {
|
|||||||
|
|
||||||
|
|
||||||
public static BufferedImage getImage(final InventoryItem card, final int width, final int height) {
|
public static BufferedImage getImage(final InventoryItem card, final int width, final int height) {
|
||||||
|
// TODO: move all the path-building logics here from the very objects. They don't have to know where their picture is
|
||||||
String key = card.getImageFilename();
|
String key = card.getImageFilename();
|
||||||
|
|
||||||
return scaleImage(key, width, height);
|
return scaleImage(key, width, height);
|
||||||
@@ -72,17 +73,21 @@ public class ImageCache {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static BufferedImage scaleImage(String key, final int width, final int height) {
|
private static BufferedImage scaleImage(String key, final int width, final int height) {
|
||||||
|
if (3 > width || 3 > height) {
|
||||||
|
// picture too small; return a blank
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
StringBuilder rsKey = new StringBuilder(key);
|
StringBuilder rsKey = new StringBuilder(key);
|
||||||
rsKey.append("#").append(width).append('x').append(height);
|
rsKey.append("#").append(width).append('x').append(height);
|
||||||
String resizedKey = rsKey.toString();
|
String resizedKey = rsKey.toString();
|
||||||
|
|
||||||
final BufferedImage ready = CACHE.getIfPresent(resizedKey);
|
final BufferedImage cached = CACHE.getIfPresent(resizedKey);
|
||||||
if (null != ready) {
|
if (null != cached) {
|
||||||
return ready;
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedImage original = getImage(key);
|
BufferedImage original = getImage(key);
|
||||||
|
|
||||||
double scale = Math.min((double) width / original.getWidth(), (double) height / original.getHeight());
|
double scale = Math.min((double) width / original.getWidth(), (double) height / original.getHeight());
|
||||||
// here would be the place to limit the scaling option in menu ?
|
// here would be the place to limit the scaling option in menu ?
|
||||||
if ((scale > 1) && !Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_SCALE_LARGER)) {
|
if ((scale > 1) && !Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_SCALE_LARGER)) {
|
||||||
@@ -95,12 +100,6 @@ public class ImageCache {
|
|||||||
} else {
|
} else {
|
||||||
int destWidth = (int) (original.getWidth() * scale);
|
int destWidth = (int) (original.getWidth() * scale);
|
||||||
int destHeight = (int) (original.getHeight() * scale);
|
int destHeight = (int) (original.getHeight() * scale);
|
||||||
|
|
||||||
if (3 > width || 3 > height) {
|
|
||||||
// picture too small; return a blank
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResampleOp resampler = new ResampleOp(destWidth, destHeight);
|
ResampleOp resampler = new ResampleOp(destWidth, destHeight);
|
||||||
|
|
||||||
result = resampler.filter(original, null);
|
result = resampler.filter(original, null);
|
||||||
@@ -109,20 +108,6 @@ public class ImageCache {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>
|
|
||||||
* getOriginalImage.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param card
|
|
||||||
* a {@link forge.Card} object.
|
|
||||||
* @return a {@link java.awt.image.BufferedImage} object.
|
|
||||||
*/
|
|
||||||
public static BufferedImage getOriginalImage(final Card card) {
|
|
||||||
final String key = card.canBeShownTo(Singletons.getControl().getPlayer()) ? ImageCache.getKey(card) : "Morph" ;
|
|
||||||
return ImageCache.getImage(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Image corresponding to the key.
|
* Returns the Image corresponding to the key.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user