Allowing raw images to be read from a stream.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@758 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Matt Jensen
2009-01-20 01:21:50 +00:00
parent f381f4fe01
commit 00d60b7397
@@ -24,9 +24,11 @@ package com.threerings.resource;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.nio.MappedByteBuffer; import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
@@ -39,6 +41,8 @@ import java.awt.image.IndexColorModel;
import java.awt.image.PixelInterleavedSampleModel; import java.awt.image.PixelInterleavedSampleModel;
import java.awt.image.WritableRaster; import java.awt.image.WritableRaster;
import org.apache.commons.io.IOUtils;
/** /**
* Provides routines for writing and reading uncompressed 8-bit color * Provides routines for writing and reading uncompressed 8-bit color
* mapped images in a manner that is extremely fast and generates a * mapped images in a manner that is extremely fast and generates a
@@ -46,7 +50,9 @@ import java.awt.image.WritableRaster;
*/ */
public class FastImageIO public class FastImageIO
{ {
/** A suffix for use when storing raw images in bundles or on the file system. */ /**
* A suffix for use when storing raw images in bundles or on the file system.
*/
public static final String FILE_SUFFIX = ".raw"; public static final String FILE_SUFFIX = ".raw";
/** /**
@@ -112,9 +118,36 @@ public class FastImageIO
try { try {
MappedByteBuffer mbuf = fchan.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MappedByteBuffer mbuf = fchan.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
return read(mbuf);
} finally {
fchan.close();
raf.close();
}
}
/**
* Reads an image from the supplied input stream (which must return the image format previously
* written via a call to {@link #write}).
*
* @exception IOException thrown if an error occurs reading from the file.
*/
public static BufferedImage read (InputStream in)
throws IOException
{
return read(ByteBuffer.wrap(IOUtils.toByteArray(in)));
}
/**
* Reads an image from the supplied byte buffer (which must return the image format previously
* written via a call to {@link #write}).
*
* @exception IOException thrown if an error occurs reading from the file.
*/
public static BufferedImage read (ByteBuffer byteBuffer)
throws IOException
{
// read in our integer fields // read in our integer fields
IntBuffer ibuf = mbuf.asIntBuffer(); IntBuffer ibuf = byteBuffer.asIntBuffer();
int width = ibuf.get(); int width = ibuf.get();
int height = ibuf.get(); int height = ibuf.get();
/* int tpixel = */ ibuf.get(); /* int tpixel = */ ibuf.get();
@@ -132,15 +165,16 @@ public class FastImageIO
} }
// read in the data and create our colormap // read in the data and create our colormap
ibuf.get(_cmap, 0, msize); ibuf.get(_cmap, 0, msize);
cmodel = new IndexColorModel(8, msize, _cmap, 0, DataBuffer.TYPE_BYTE, null); cmodel = new IndexColorModel(
8, msize, _cmap, 0, DataBuffer.TYPE_BYTE, null);
} }
// advance the byte buffer accordingly // advance the byte buffer accordingly
mbuf.position(ibuf.position() * 4); byteBuffer.position(ibuf.position() * 4);
// read in the image data itself // read in the image data itself
byte[] data = new byte[width*height]; byte[] data = new byte[width*height];
mbuf.get(data); byteBuffer.get(data);
// create the image from our component parts // create the image from our component parts
DataBuffer dbuf = new DataBufferByte(data, data.length, 0); DataBuffer dbuf = new DataBufferByte(data, data.length, 0);
@@ -150,11 +184,6 @@ public class FastImageIO
DataBuffer.TYPE_BYTE, width, height, 1, width, offsets); DataBuffer.TYPE_BYTE, width, height, 1, width, offsets);
WritableRaster raster = WritableRaster.createWritableRaster(smodel, dbuf, _origin); WritableRaster raster = WritableRaster.createWritableRaster(smodel, dbuf, _origin);
return new BufferedImage(cmodel, raster, false, null); return new BufferedImage(cmodel, raster, false, null);
} finally {
fchan.close();
raf.close();
}
} }
/** Used when loading our color map. */ /** Used when loading our color map. */