Make ImageDataProvider responsible for loading the actual image instead of

returning an ImageInputStream.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2475 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-04-27 06:33:11 +00:00
parent a4e09831c5
commit 8973ca0d8c
3 changed files with 43 additions and 67 deletions
@@ -1,10 +1,10 @@
// //
// $Id: ImageDataProvider.java,v 1.1 2003/01/13 22:49:46 mdb Exp $ // $Id: ImageDataProvider.java,v 1.2 2003/04/27 06:33:11 mdb Exp $
package com.threerings.media.image; package com.threerings.media.image;
import java.io.IOException; import java.io.IOException;
import javax.imageio.stream.ImageInputStream; import java.awt.image.BufferedImage;
/** /**
* Provides access to image data for the image with the specified * Provides access to image data for the image with the specified
@@ -22,8 +22,7 @@ public interface ImageDataProvider
public String getIdent (); public String getIdent ();
/** /**
* Returns an input stream from which the image data for the specified * Returns the image at the specified path.
* image may be loaded.
*/ */
public ImageInputStream loadImageData (String path) throws IOException; public BufferedImage loadImage (String path) throws IOException;
} }
@@ -1,24 +0,0 @@
//
// $Id: ImageLoader.java,v 1.2 2003/01/08 04:09:02 mdb Exp $
package com.threerings.media.image;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
/**
* In order to take advantage of the new J2SE 1.4 image loading facilites
* (in the form of <code>ImageIO</code> and friends), while preserving the
* ability to operate on J2SE 1.3 and previous, we have the image loader
* interface. It allows us to attempt to load the ImageIO based loader and
* fall back if the class is not available.
*/
public interface ImageLoader
{
/**
* Load up the image from the supplied input stream.
*/
public Image loadImage (InputStream source)
throws IOException;
}
@@ -1,5 +1,5 @@
// //
// $Id: ImageManager.java,v 1.50 2003/04/25 22:12:14 mdb Exp $ // $Id: ImageManager.java,v 1.51 2003/04/27 06:33:11 mdb Exp $
package com.threerings.media.image; package com.threerings.media.image;
@@ -237,15 +237,15 @@ public class ImageManager
ImageDataProvider dprov = (ImageDataProvider)_providers.get(rset); ImageDataProvider dprov = (ImageDataProvider)_providers.get(rset);
if (dprov == null) { if (dprov == null) {
dprov = new ImageDataProvider() { dprov = new ImageDataProvider() {
public ImageInputStream loadImageData (String path) public BufferedImage loadImage (String path)
throws IOException { throws IOException {
// first attempt to load the image from the specified // first attempt to load the image from the specified
// resource set // resource set
try { try {
return _rmgr.getImageResource(rset, path); return read(_rmgr.getImageResource(rset, path));
} catch (FileNotFoundException fnfe) { } catch (FileNotFoundException fnfe) {
// fall back to trying the classpath // fall back to trying the classpath
return _rmgr.getImageResource(path); return read(_rmgr.getImageResource(path));
} }
} }
@@ -270,11 +270,9 @@ public class ImageManager
// } // }
BufferedImage image = null; BufferedImage image = null;
ImageInputStream imgin = null;
try { try {
Log.debug("Loading image " + key + "."); Log.debug("Loading image " + key + ".");
imgin = key.daprov.loadImageData(key.path); image = key.daprov.loadImage(key.path);
image = loadImage(imgin);
if (image == null) { if (image == null) {
Log.warning("ImageIO.read(" + key + ") returned null."); Log.warning("ImageIO.read(" + key + ") returned null.");
} }
@@ -287,42 +285,46 @@ public class ImageManager
image = createImage(1, 1, Transparency.OPAQUE); image = createImage(1, 1, Transparency.OPAQUE);
} }
if (imgin != null) {
try {
imgin.close();
} catch (IOException ioe) {
// jesus fucking hoppalong cassidy christ on a polyester
// pogo stick! those fuckers at sun have
// ImageInputStreamImpl.close() throwing a fucking
// IOException if it's already closed; there's no way to
// find out if it's already closed or not, so we have to
// check for their bullshit exception; as if we should
// just "trust" ImageIO.read() to close the fucking input
// stream when it's done, especially after the goddamned
// fiasco with PNGImageReader not closing it's fucking
// inflaters; for the love of humanity
if (!"closed".equals(ioe.getMessage())) {
Log.warning("Failure closing image input '" + key + "'.");
Log.logStackTrace(ioe);
}
}
}
return image; return image;
} }
/** /**
* Called by {@link #loadImage(ImageKey)} to do the actual image * Helper function for loading images.
* loading.
*/ */
protected BufferedImage loadImage (ImageInputStream imgin) protected static BufferedImage read (ImageInputStream iis)
throws IOException throws IOException
{ {
// ParameterBlock pb = new ParameterBlock(); BufferedImage image = ImageIO.read(iis);
// pb.add(imgin); closeIIS(iis);
// return JAI.create("BMP", pb); return image;
}
return ImageIO.read(imgin); /**
* Helper function for closing image input streams.
*/
protected static void closeIIS (ImageInputStream iis)
{
if (iis == null) {
return;
}
try {
iis.close();
} catch (IOException ioe) {
// jesus fucking hoppalong cassidy christ on a polyester pogo
// stick! ImageInputStreamImpl.close() throws a fucking
// IOException if it's already closed; there's no way to find
// out if it's already closed or not, so we have to check for
// their bullshit exception; as if we should just "trust"
// ImageIO.read() to close the fucking input stream when it's
// done, especially after the goddamned fiasco with
// PNGImageReader not closing it's fucking inflaters; for the
// love of humanity
if (!"closed".equals(ioe.getMessage())) {
Log.warning("Failure closing image input '" + iis + "'.");
Log.logStackTrace(ioe);
}
}
} }
/** /**
@@ -502,9 +504,8 @@ public class ImageManager
/** Our default data provider. */ /** Our default data provider. */
protected ImageDataProvider _defaultProvider = new ImageDataProvider() { protected ImageDataProvider _defaultProvider = new ImageDataProvider() {
public ImageInputStream loadImageData (String path) public BufferedImage loadImage (String path) throws IOException {
throws IOException { return read(_rmgr.getImageResource(path));
return _rmgr.getImageResource(path);
} }
public String getIdent () { public String getIdent () {