00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.skin.io;
00023
00024 import com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache;
00025 import com.realtime.crossfire.jxclient.skin.skin.JXCSkinException;
00026 import com.realtime.crossfire.jxclient.skin.source.JXCSkinSource;
00027 import java.awt.Color;
00028 import java.awt.Image;
00029 import java.awt.image.BufferedImage;
00030 import java.io.IOException;
00031 import java.io.InputStream;
00032 import javax.imageio.ImageIO;
00033 import org.jetbrains.annotations.NotNull;
00034 import org.jetbrains.annotations.Nullable;
00035
00041 public class ImageParser {
00042
00046 @NotNull
00047 private final JXCSkinCache<BufferedImage> definedImages = new JXCSkinCache<BufferedImage>("image");
00048
00052 @NotNull
00053 private final JXCSkinSource skinSource;
00054
00059 public ImageParser(@NotNull final JXCSkinSource skinSource) {
00060 this.skinSource = skinSource;
00061 }
00062
00066 public void clear() {
00067 definedImages.clear();
00068 }
00069
00077 @Nullable
00078 public Image getImage(@Nullable final Color color, @NotNull final String name) throws IOException {
00079
00080 return color != null ? null : getImage(name);
00081 }
00082
00089 @NotNull
00090 public BufferedImage getImage(@NotNull final String name) throws IOException {
00091 try {
00092 return definedImages.lookup(name);
00093 } catch (final JXCSkinException ignored) {
00094
00095 }
00096
00097 final String filename = "pictures/"+name+".png";
00098 final BufferedImage image;
00099 final InputStream inputStream = skinSource.getInputStream(filename);
00100 try {
00101 image = ImageIO.read(inputStream);
00102 } finally {
00103 inputStream.close();
00104 }
00105 if (image == null) {
00106 throw new IOException("image '"+skinSource.getURI(filename)+"' does not exist");
00107 }
00108 try {
00109 definedImages.insert(name, image);
00110 } catch (final JXCSkinException ex) {
00111 throw new AssertionError(ex);
00112 }
00113 return image;
00114 }
00115
00116 }