All pastes #33838 Raw Edit

Unnamed

public java v1 · immutable
#33838 ·published 2005-12-16 00:38 UTC
rendered paste body
package org.stathissideris.imageinfo;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.nio.ByteBuffer;public class PNGFileMetadata extends ImageFileMetadata {		public enum ColourType {		Greyscale,		Truecolour,		Indexed_colour,		Greyscale_with_alpha,		Truecolour_with_alpha,		Invalid;				public static ColourType byteToColourType(byte b){			switch (b) {				case 0: return ColourType.Greyscale;				case 2: return ColourType.Truecolour;				case 3: return ColourType.Indexed_colour;				case 4: return ColourType.Greyscale_with_alpha;				case 6: return ColourType.Truecolour_with_alpha;				default: return ColourType.Invalid;			}		}				public String toString () {	        return super.toString().replace('_', ' ');	    }	}		public enum InterlaceMethod {		No_Interlace {public String toString(){return "No interlace";}},		Adam7,		Invalid	}		private ColourType colourType;	private InterlaceMethod interlaceMethod;		private static final byte[] signature = {(byte)137, 80, 78, 71, 13, 10, 26, 10};	private static final byte[] CHUNK_TYPE_IHDR = {73, 72, 68, 82};		public PNGFileMetadata(File file) throws FileNotFoundException, IOException {		setFile(file);		if(!getInfoFromFile()){			throw new RuntimeException(file+" is not a PNG file.");		}	}	protected boolean getInfoFromFile() throws FileNotFoundException, IOException {		if(getFile() == null) return false;				FileInputStream s = new FileInputStream(getFile());				byte[] possibleSig = new byte[8];		s.read(possibleSig);				if(!Utils.arraysEqual(possibleSig, signature)){			return false;		}				byte[] part = new byte[4];		ByteBuffer partBB = ByteBuffer.wrap(part);				//read length		s.read(part);				//read type		s.read(part);		if(Utils.arraysEqual(part, CHUNK_TYPE_IHDR)){			//read width			s.read(part);			setWidth(partBB.getInt(0));						//read height			s.read(part);			setHeight(partBB.getInt(0));						//Bit depth			s.read(part, 0, 1);			setBitDepth(partBB.get(0));						//Colour type			s.read(part, 0, 1);			setColourType(ColourType.byteToColourType(partBB.get(0)));						//Compression method (skip)			s.read(part, 0, 1);			//Filter method (skip)			s.read(part, 0, 1);			//Interlace method			s.read(part, 0, 1);			switch (partBB.get(0)){				case 0: setInterlaceMethod(InterlaceMethod.No_Interlace); break;				case 1: setInterlaceMethod(InterlaceMethod.Adam7); break;				default: setInterlaceMethod(InterlaceMethod.Invalid); break;			}		} else {			return false;		}				s.close();				return true;	}		public ColourType getColourType(){		return colourType;	}		protected void setColourType(ColourType colourType){		this.colourType = colourType;	}		public InterlaceMethod getInterlaceMethod() {		return interlaceMethod;	}	protected void setInterlaceMethod(InterlaceMethod interlaceMethod) {		this.interlaceMethod = interlaceMethod;	}		public String toString() {		return			super.toString()			+"Colour type: "+getColourType()+"\n"			+"Interlace method: "+getInterlaceMethod()+"\n";	}}