Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2008, 12:30 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
How to write an image of a given format
Code:
import java.awt.Rectangle; import java.awt.image.Raster; import java.io.IOException; import javax.imageio.IIOImage; import javax.imageio.ImageTypeSpecifier; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.metadata.IIOMetadata; import javax.imageio.metadata.IIOMetadataFormat; import javax.imageio.metadata.IIOMetadataNode; import javax.imageio.spi.ImageWriterSpi; import javax.imageio.stream.ImageOutputStream; import org.w3c.dom.Node; /** * ch5ImageWriter.java -- this class provides the functionality to write an * image of format ch5. */ public class ch5ImageWriter extends ImageWriter { public ch5ImageWriter(ImageWriterSpi originatingProvider) { super(originatingProvider); streamMetadataWritten = false; } /** * this method is used to convert an ImageReader's image metadata which is * in a particular format into image metadata that can be used for this * ImageWriter. Primarily this is used for transcoding (format conversion). * This ImageWriter does not support such conversions */ public IIOMetadata convertImageMetadata(IIOMetadata metadata, ImageTypeSpecifier specifier, ImageWriteParam param) { return null; } /** * this method is used to convert an ImageReader's stream metadata which is * in a particular format into stream metadata that can be used for this * ImageWriter. Primarily this is used for transcoding (format conversion). * This ImageWriter does not support such conversions */ public IIOMetadata convertStreamMetadata(IIOMetadata metadata, ImageWriteParam param) { return null; } /** * provide default values for the image metadata */ public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier specifier, ImageWriteParam param) { ch5ImageMetadata imagemd = new ch5ImageMetadata(); int width = raster.getWidth(); int height = raster.getHeight(); imagemd.initialize(width, height); // default image size return imagemd; } /** * provide default values for the stream metadata */ public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) { ch5StreamMetadata streammd = new ch5StreamMetadata(); streammd.initialize(1); // default number of images return streammd; } /** * write out the output image specified by index imageIndex using the * parameters specified by the ImageWriteParam object param */ public void write(IIOMetadata metadata, IIOImage iioimage, ImageWriteParam param) { Node root = null; Node dimensionsElementNode = null; if (iioimage.getRenderedImage() != null) raster = iioimage.getRenderedImage().getData(); else raster = iioimage.getRaster(); /* * since this format allows you to write multiple images, the * streamMetadataWritten variable makes sure the stream metadata is * written only once */ if (streamMetadataWritten == false) { if (metadata == null) metadata = getDefaultStreamMetadata(param); root = metadata.getAsTree("ch5.imageio.ch5stream_1.00"); dimensionsElementNode = root.getFirstChild(); Node numberImagesAttributeNode = dimensionsElementNode .getAttributes().getNamedItem("numberImages"); String numberImages = numberImagesAttributeNode.getNodeValue(); try { ios.writeBytes("5\n"); ios.writeBytes(numberImages); ios.flush(); } catch (IOException ioe) { System.err.println("IOException " + ioe.getMessage()); } streamMetadataWritten = true; } String widthString; String heightString; IIOMetadata imageMetadata = (ch5ImageMetadata) iioimage.getMetadata(); /* * don't really need image metadata object here since raster knows * necessary information */ if (imageMetadata == null) imageMetadata = getDefaultImageMetadata(null, param); root = imageMetadata.getAsTree("ch5.imageio.ch5image_1.00"); dimensionsElementNode = root.getFirstChild(); Node widthAttributeNode = dimensionsElementNode.getAttributes() .getNamedItem("imageWidth"); widthString = widthAttributeNode.getNodeValue(); Node heightAttributeNode = dimensionsElementNode.getAttributes() .getNamedItem("imageHeight"); heightString = heightAttributeNode.getNodeValue(); int sourceWidth = Integer.parseInt(widthString); int sourceHeight = Integer.parseInt(heightString); int destinationWidth = -1; int destinationHeight = -1; int sourceRegionWidth = -1; int sourceRegionHeight = -1; int sourceRegionXOffset = -1; int sourceRegionYOffset = -1; int xSubsamplingFactor = -1; int ySubsamplingFactor = -1; if (param == null) param = getDefaultWriteParam(); /* * get Rectangle object which will be used to clip the source image's * dimensions. */ Rectangle sourceRegion = param.getSourceRegion(); if (sourceRegion != null) { sourceRegionWidth = (int) sourceRegion.getWidth(); sourceRegionHeight = (int) sourceRegion.getHeight(); sourceRegionXOffset = (int) sourceRegion.getX(); sourceRegionYOffset = (int) sourceRegion.getY(); /* * correct for overextended source regions */ if (sourceRegionXOffset + sourceRegionWidth > sourceWidth) destinationWidth = sourceWidth - sourceRegionXOffset; else destinationWidth = sourceRegionWidth; if (sourceRegionYOffset + sourceRegionHeight > sourceHeight) destinationHeight = sourceHeight - sourceRegionYOffset; else destinationHeight = sourceRegionHeight; } else { destinationWidth = sourceWidth; destinationHeight = sourceHeight; sourceRegionXOffset = sourceRegionYOffset = 0; } /* * get subsampling factors */ xSubsamplingFactor = param.getSourceXSubsampling(); ySubsamplingFactor = param.getSourceYSubsampling(); destinationWidth = (destinationWidth - 1) / xSubsamplingFactor + 1; destinationHeight = (destinationHeight - 1) / ySubsamplingFactor + 1; byte[] sourceBuffer; byte[] destinationBuffer = new byte[destinationWidth]; try { ios.writeBytes(new String("\n")); ios.writeBytes(new String(destinationWidth + "\n")); ios.writeBytes(new String(destinationHeight + "\n")); int jj; int index; for (int j = 0; j < sourceWidth; j++) { sourceBuffer = (byte[]) raster.getDataElements(0, j, sourceWidth, 1, null); jj = j - sourceRegionYOffset; if (jj % ySubsamplingFactor == 0) { jj /= ySubsamplingFactor; if ((jj >= 0) && (jj < destinationHeight)) { for (int i = 0; i < destinationWidth; i++) { index = sourceRegionXOffset + i * xSubsamplingFactor; destinationBuffer[i] = sourceBuffer[index]; } ios.write(destinationBuffer, 0, destinationWidth); ios.flush(); } } } } catch (IOException e) { System.err.println("IOException: " + e.getMessage()); } } public void setOutput(Object output) { super.setOutput(output); if (output == null) throw new IllegalArgumentException("output is null"); if (!(output instanceof ImageOutputStream)) throw new IllegalArgumentException( "output not an ImageOutputStream"); ios = (ImageOutputStream) output; streamMetadataWritten = false; } private ImageOutputStream ios; private boolean streamMetadataWritten; private Raster raster; } /** * ch5StreamMetadata.java -- holds stream metadata for the ch5 format. The * internal tree for holding this metadata is read only */ class ch5StreamMetadata extends IIOMetadata { static final String nativeMetadataFormatName = "ch5.imageio.ch5stream_1.00"; static final String nativeMetadataFormatClassName = "ch5.imageio.ch5stream"; static final String[] extraMetadataFormatNames = null; static final String[] extraMetadataFormatClassNames = null; static final boolean standardMetadataFormatSupported = false; public int numberImages; public ch5StreamMetadata() { super(standardMetadataFormatSupported, nativeMetadataFormatName, nativeMetadataFormatClassName, extraMetadataFormatNames, extraMetadataFormatClassNames); numberImages = -1; } public boolean isReadOnly() { return true; } /** * IIOMetadataFormat objects are meant to describe the structure of metadata * returned from the getAsTree method. In this case, no such description is * available */ public IIOMetadataFormat getMetadataFormat(String formatName) { if (formatName.equals(nativeMetadataFormatName)) { return null; } else { throw new IllegalArgumentException("Unrecognized format!"); } } /** * returns the stream metadata in a tree corresponding to the provided * formatName */ public Node getAsTree(String formatName) { if (formatName.equals(nativeMetadataFormatName)) { return getNativeTree(); } else { throw new IllegalArgumentException("Unrecognized format!"); } } /** * returns the stream metadata in a tree using the following format * <!ELEMENT ch5.imageio.ch5stream_1.00 (imageDimensions)> <!ATTLIST * imageDimensions numberImages CDATA #REQUIRED */ private Node getNativeTree() { IIOMetadataNode node; // scratch node IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName); // Image descriptor node = new IIOMetadataNode("imageDimensions"); node.setAttribute("numberImages", Integer.toString(numberImages)); root.appendChild(node); return root; } public void setFromTree(String formatName, Node root) { throw new IllegalStateException("Metadata is read-only!"); } public void mergeTree(String formatName, Node root) { throw new IllegalStateException("Metadata is read-only!"); } public void reset() { throw new IllegalStateException("Metadata is read-only!"); } /** * initialize the stream metadata element numberImages */ public void initialize(int numberImages) { this.numberImages = numberImages; } } /** * ch5ImageMetadata.java -- holds image metadata for the ch5 format. * The internal tree for holding this metadata is read only */ class ch5ImageMetadata extends IIOMetadata { static final String nativeMetadataFormatName = "ch5.imageio.ch5image_1.00"; static final String nativeMetadataFormatClassName = "ch5.imageio.ch5image"; static final String[] extraMetadataFormatNames = null; static final String[] extraMetadataFormatClassNames = null; static final boolean standardMetadataFormatSupported = false; public int imageWidth; public int imageHeight; public ch5ImageMetadata() { super(standardMetadataFormatSupported, nativeMetadataFormatName, nativeMetadataFormatClassName, extraMetadataFormatNames, extraMetadataFormatClassNames ); imageWidth = -1; imageHeight = -1; } public boolean isReadOnly() { return true; } /** * IIOMetadataFormat objects are meant to describe the structure of * metadata returned from the getAsTree method. In this case, * no such description is available */ public IIOMetadataFormat getMetadataFormat(String formatName) { if (formatName.equals(nativeMetadataFormatName)) { return null; } else { throw new IllegalArgumentException("Unrecognized format!"); } } /** * returns the image metadata in a tree corresponding to the * provided formatName */ public Node getAsTree(String formatName) { if (formatName.equals(nativeMetadataFormatName)) { return getNativeTree(); } else { throw new IllegalArgumentException("Unrecognized format!"); } } /** * returns the image metadata in a tree using the following format * <!ELEMENT ch5.imageio.ch5image_1.00 (imageDimensions)> * <!ATTLIST imageDimensions * imageWidth CDATA #REQUIRED * imageHeight CDATA #REQUIRED */ private Node getNativeTree() { IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName); IIOMetadataNode node = new IIOMetadataNode("imageDimensions"); node.setAttribute("imageWidth", Integer.toString(imageWidth)); node.setAttribute("imageHeight", Integer.toString(imageHeight)); root.appendChild(node); return root; } public void setFromTree(String formatName, Node root) { throw new IllegalStateException("Metadata is read-only!"); } public void mergeTree(String formatName, Node root) { throw new IllegalStateException("Metadata is read-only!"); } public void reset() { throw new IllegalStateException("Metadata is read-only!"); } /** * initialize the image metadata elements width and height */ public void initialize(int width, int height) { imageWidth = width; imageHeight = height; } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display available ImageReaders and ImageWriters by image format and MIME type Java Tip java.awt 0 06-24-2008 01:25 AM
Timestamp in SQL format Java Tip Java Tips 0 02-10-2008 01:37 PM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 07:29 PM
Code format Eric XML 1 07-06-2007 09:42 AM
show a RTF FORMAT Jack Advanced Java 2 07-04-2007 05:37 AM


All times are GMT +3. The time now is 10:35 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org