Skip to content

Commit

Permalink
Actually use cached images
Browse files Browse the repository at this point in the history
Also, remove colorCache because reasons
  • Loading branch information
Morphan1 committed Mar 6, 2015
1 parent 405173b commit 4fd4dde
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/main/java/net/aufdemrand/denizen/utilities/maps/MapImage.java
Expand Up @@ -27,7 +27,7 @@ public class MapImage extends MapObject {

public MapImage(String xTag, String yTag, String visibilityTag, boolean debug, String fileTag,
int width, int height) {
this(xTag, yTag, visibilityTag, debug, fileTag, width, height, false);
this(xTag, yTag, visibilityTag, debug, fileTag, width, height, true);
}

public MapImage(String xTag, String yTag, String visibilityTag, boolean debug, String fileTag,
Expand Down Expand Up @@ -94,37 +94,30 @@ public void render(MapView mapView, MapCanvas mapCanvas, dPlayer player, UUID uu
}
}

private static Color[] bukkitColors = null;
// Since color conversions will never change, remember them instead of using a bunch of math every single time
private final static Map<Integer, Byte> colorCache = new HashMap<Integer, Byte>();
private static final Color[] bukkitColors;

private static byte[] imageToBytes(Image image, int width, int height) {
if (bukkitColors == null) {
try {
Field field = MapPalette.class.getDeclaredField("colors");
field.setAccessible(true);
bukkitColors = (Color[]) field.get(null);
} catch (Exception e) {
dB.echoError(e);
}
static {
Color[] colors = null;
try {
Field field = MapPalette.class.getDeclaredField("colors");
field.setAccessible(true);
colors = (Color[]) field.get(null);
} catch (Exception e) {
dB.echoError(e);
}
BufferedImage temp = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
bukkitColors = colors;
}

private static byte[] imageToBytes(Image image, int width, int height) {
BufferedImage temp = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = temp.createGraphics();
graphics.drawImage(image, 0, 0, width, height, null);
graphics.dispose();
int[] pixels = new int[width * height];
temp.getRGB(0, 0, width, height, pixels, 0, width);
byte[] result = new byte[width * height];
for (int i = 0; i < pixels.length; i++) {
int pixel = pixels[i];
if (colorCache.containsKey(pixel)) {
result[i] = colorCache.get(pixel);
}
else {
byte color = matchColor(new Color(pixel));
colorCache.put(pixel, color);
result[i] = color;
}
result[i] = matchColor(new Color(pixels[i]));
}
return result;
}
Expand Down

0 comments on commit 4fd4dde

Please sign in to comment.