JAI directly supports several of the most common image file formats, BMP, FPX, GIF, JPEG, PNG, PNM, TIFF.
An image file usually has at least two parts: a file header and the image data. The header contains fields of pertinent information regarding the following image data. At the very least, the header must provide all the information necessary to reconstruct the original image from the stored image data. The image data itself may or may not be compressed.
For most image types, JAI offers the option of reading an image data file as a java.io.File object or as one of the subclasses of java.io.InputStream.
The Stream operation reads an image from a SeekableStream.
// Load the source image from a Stream.
RenderedImage im = JAI.create("stream", stream);
The FileLoad operation reads an image from a file.
// Load the source image from a file.
RenderedImage src = (RenderedImage)JAI.create("fileload", fileName);
The TIFF operation reads TIFF data from a TIFF SeekableStream.
For TIFF Palette color images, the colorMap always has entries of short data type, the color black being represented by 0,0,0 and white by 65536,65536,65536. To display these images, the default behavior is to dither the short values down to 8 bits. The dithering is done by calling the decode16BitsTo8Bit method for each short value that needs to be dithered.
A TIFF file may contain more than one Image File Directory (IFD). Each IFD defines a subfile, which may be used to describe related images. To determine the number of images in a TIFF file, use the TIFFDirectory.getNumDirectories() method.
API: com.sun.media.jai.codec.TIFFDecodeParam
* void setDecodePaletteAsShorts(boolean decodePaletteAsShorts)
if set, the entries in the palette will be decoded as shorts and no short-to-byte lookup will be applied to them.
* boolean getDecodePaletteAsShorts()
returns true if palette entries will be decoded as shorts, resulting in a output image with short datatype.
* byte decode16BitsTo8Bits(int s)
returns an unsigned 8-bit value computed by dithering the unsigned 16-bit value.
API: com.sun.media.jai.codec.TIFFDirectory
* static int getNumDirectories(SeekableStream stream)
returns the number of image directories (subimages) stored in a given TIFF file, represented by a SeekableStream.
API: com.sun.media.jai.codec.TIFFField
* int getTag()
returns the tag number, between 0 and 65535.
* int getType()
returns the type of the data stored in the IFD.
* int getCount()
returns the number of elements in the IFD.
API: com.sun.media.jai.codec.TIFFDirectory
* TIFFDirectory(SeekableStream stream, int directory)
constructs a TIFFDirectory from a SeekableStream.
* TIFFDirectory(SeekableStream stream, long ifd_offset)
constructs a TIFFDirectory by reading a SeekableStream.
* int getNumEntries()
returns the number of directory entries.
* TIFFField getField(int tag)
returns the value of a given tag as a TIFFField, or null if the tag is not present.
* boolean isTagPresent(int tag)
returns true if a tag appears in the directory.
* int[] getTags()
returns an ordered array of ints indicating the tag values.
* TIFFField[] getFields()
returns an array of TIFFFields containing all the fields in this directory.
* byte getFieldAsByte(int tag, int index)
returns the value of a particular index of a given tag as a byte.
* byte getFieldAsByte(int tag)
returns the value of index 0 of a given tag as a byte.
* long getFieldAsLong(int tag, int index)
returns the value of a particular index of a given tag as a long.
* long getFieldAsLong(int tag)
returns the value of index 0 of a given tag as a long.
* float getFieldAsFloat(int tag, int index)
returns the value of a particular index of a given tag as a float.
* float getFieldAsFloat(int tag)
returns the value of a index 0 of a given tag as a float.
* double getFieldAsDouble(int tag, int index)
returns the value of a particular index of a given tag as a double.
* double getFieldAsDouble(int tag)
returns the value of index 0 of a given tag as a double.
Example of Reading a BMP Image
// Wrap the InputStream in a SeekableStream.
InputStream is = new FileInputStream(filename);
SeekableStream s = SeekableStream.wrapInputStream(is, false);
// Create the ParameterBlock and add the SeekableStream to it.
ParameterBlock pb = new ParameterBlock();
pb.add(s);
// Perform the BMP operation
op = JAI.create("BMP", pb);
Example of Reading a PNM Image
// Create the ParameterBlock.
InputStream image = new FileInputStream(filename);
ParameterBlock pb = new ParameterBlock();
pb.add(image);
// Create the PNM operation.
op = JAI.create("PNM", pb);
Example of Reading an AWT Image
// Create the ParameterBlock.
ParameterBlock pb = new ParameterBlock();
pb.add(image);
// Create the AWTImage operation.
PlanarImage im = (PlanarImage)JAI.create("awtImage", pb);
Example of Reading a URL Image
// Define the URL to the image.
url = new URL("http://webstuff/images/duke.gif");
// Read the image from the designated URL.
RenderedOp src = JAI.create("url", url);
Example of Converting a Rendered Image to Renderable
// Derive the RenderableImage from the source RenderedImage.
ParameterBlock pb = new ParameterBlock();
pb.addSource(src);
pb.add(null).add(null).add(null).add(null).add(null);
// Create the Renderable operation.
RenderableImage ren = JAI.createRenderable("renderable", pb);
Example Constant Operation
// Create the ParameterBlock.
Byte[] bandValues = new Byte[1];
bandValues[0] = alpha1;
pb = new ParameterBlock();
pb.add(new Float(src1.getWidth())); // The width
pb.add(new Float(src1.getHeight())); // The height
pb.add(bandValues); // The band values
// Create the constant operation.
PlanarImage afa1 = (PlanarImage)JAI.create("constant", pb);
Tuesday, 24 July 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment