Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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-21-2008, 09:51 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
PNG file format decoder in Java
Code:
import java.awt.Graphics; import java.awt.Insets; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.DataBuffer; import java.awt.image.DataBufferByte; import java.awt.image.IndexColorModel; import java.awt.image.Raster; import java.awt.image.WritableRaster; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.zip.CRC32; import java.util.zip.InflaterInputStream; import javax.swing.JFrame; public class PNGDecoder { public static void main(String[] args) throws Exception { String name = "logo.png"; if (args.length > 0) name = args[0]; InputStream in = PNGDecoder.class.getResourceAsStream(name); final BufferedImage image = PNGDecoder.decode(in); in.close(); JFrame f = new JFrame() { public void paint(Graphics g) { Insets insets = getInsets(); g.drawImage(image, insets.left, insets.top, null); } }; f.setVisible(true); Insets insets = f.getInsets(); f.setSize(image.getWidth() + insets.left + insets.right, image .getHeight() + insets.top + insets.bottom); } public static BufferedImage decode(InputStream in) throws IOException { DataInputStream dataIn = new DataInputStream(in); readSignature(dataIn); PNGData chunks = readChunks(dataIn); long widthLong = chunks.getWidth(); long heightLong = chunks.getHeight(); if (widthLong > Integer.MAX_VALUE || heightLong > Integer.MAX_VALUE) throw new IOException("That image is too wide or tall."); int width = (int) widthLong; int height = (int) heightLong; ColorModel cm = chunks.getColorModel(); WritableRaster raster = chunks.getRaster(); BufferedImage image = new BufferedImage(cm, raster, false, null); return image; } protected static void readSignature(DataInputStream in) throws IOException { long signature = in.readLong(); if (signature != 0x89504e470d0a1a0aL) throw new IOException("PNG signature not found!"); } protected static PNGData readChunks(DataInputStream in) throws IOException { PNGData chunks = new PNGData(); boolean trucking = true; while (trucking) { try { // Read the length. int length = in.readInt(); if (length < 0) throw new IOException("Sorry, that file is too long."); // Read the type. byte[] typeBytes = new byte[4]; in.readFully(typeBytes); // Read the data. byte[] data = new byte[length]; in.readFully(data); // Read the CRC. long crc = in.readInt() & 0x00000000ffffffffL; // Make it // unsigned. if (verifyCRC(typeBytes, data, crc) == false) throw new IOException("That file appears to be corrupted."); PNGChunk chunk = new PNGChunk(typeBytes, data); chunks.add(chunk); } catch (EOFException eofe) { trucking = false; } } return chunks; } protected static boolean verifyCRC(byte[] typeBytes, byte[] data, long crc) { CRC32 crc32 = new CRC32(); crc32.update(typeBytes); crc32.update(data); long calculated = crc32.getValue(); return (calculated == crc); } } class PNGData { private int mNumberOfChunks; private PNGChunk[] mChunks; public PNGData() { mNumberOfChunks = 0; mChunks = new PNGChunk[10]; } public void add(PNGChunk chunk) { mChunks[mNumberOfChunks++] = chunk; if (mNumberOfChunks >= mChunks.length) { PNGChunk[] largerArray = new PNGChunk[mChunks.length + 10]; System.arraycopy(mChunks, 0, largerArray, 0, mChunks.length); mChunks = largerArray; } } public long getWidth() { return getChunk("IHDR").getUnsignedInt(0); } public long getHeight() { return getChunk("IHDR").getUnsignedInt(4); } public short getBitsPerPixel() { return getChunk("IHDR").getUnsignedByte(8); } public short getColorType() { return getChunk("IHDR").getUnsignedByte(9); } public short getCompression() { return getChunk("IHDR").getUnsignedByte(10); } public short getFilter() { return getChunk("IHDR").getUnsignedByte(11); } public short getInterlace() { return getChunk("IHDR").getUnsignedByte(12); } public ColorModel getColorModel() { short colorType = getColorType(); int bitsPerPixel = getBitsPerPixel(); if (colorType == 3) { byte[] paletteData = getChunk("PLTE").getData(); int paletteLength = paletteData.length / 3; return new IndexColorModel(bitsPerPixel, paletteLength, paletteData, 0, false); } System.out.println("Unsupported color type: " + colorType); return null; } public WritableRaster getRaster() { int width = (int) getWidth(); int height = (int) getHeight(); int bitsPerPixel = getBitsPerPixel(); short colorType = getColorType(); if (colorType == 3) { byte[] imageData = getImageData(); DataBuffer db = new DataBufferByte(imageData, imageData.length); WritableRaster raster = Raster.createPackedRaster(db, width, height, bitsPerPixel, null); return raster; } else System.out.println("Unsupported color type!"); return null; } public byte[] getImageData() { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); // Write all the IDAT data into the array. for (int i = 0; i < mNumberOfChunks; i++) { PNGChunk chunk = mChunks[i]; if (chunk.getTypeString().equals("IDAT")) { out.write(chunk.getData()); } } out.flush(); // Now deflate the data. InflaterInputStream in = new InflaterInputStream( new ByteArrayInputStream(out.toByteArray())); ByteArrayOutputStream inflatedOut = new ByteArrayOutputStream(); int readLength; byte[] block = new byte[8192]; while ((readLength = in.read(block)) != -1) inflatedOut.write(block, 0, readLength); inflatedOut.flush(); byte[] imageData = inflatedOut.toByteArray(); // Compute the real length. int width = (int) getWidth(); int height = (int) getHeight(); int bitsPerPixel = getBitsPerPixel(); int length = width * height * bitsPerPixel / 8; byte[] prunedData = new byte[length]; // We can only deal with non-interlaced images. if (getInterlace() == 0) { int index = 0; for (int i = 0; i < length; i++) { if ((i * 8 / bitsPerPixel) % width == 0) { index++; // Skip the filter byte. } prunedData[i] = imageData[index++]; } } else System.out.println("Couldn't undo interlacing."); return prunedData; } catch (IOException ioe) { } return null; } public PNGChunk getChunk(String type) { for (int i = 0; i < mNumberOfChunks; i++) if (mChunks[i].getTypeString().equals(type)) return mChunks[i]; return null; } } class PNGChunk { private byte[] mType; private byte[] mData; public PNGChunk(byte[] type, byte[] data) { mType = type; mData = data; } public String getTypeString() { try { return new String(mType, "UTF8"); } catch (UnsupportedEncodingException uee) { return ""; } } public byte[] getData() { return mData; } public long getUnsignedInt(int offset) { long value = 0; for (int i = 0; i < 4; i++) value += (mData[offset + i] & 0xff) << ((3 - i) * 8); return value; } public short getUnsignedByte(int offset) { return (short) (mData[offset] & 0x00ff); } }
__________________
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
How to parse the CSV(Comma separation values)file and validate the file using java padmajap13 Advanced Java 7 05-23-2008 04:46 AM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 10:13 AM
Convertin a .DAT file into readable format janakiram.attuluri Advanced Java 1 12-07-2007 11:00 PM
it's possible format data in java? paul New To Java 1 08-07-2007 06:10 AM
Date format display in CSV file after exporting latha Advanced Java 0 08-03-2007 09:09 AM


All times are GMT +3. The time now is 06:08 AM.


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