package com.riskdecisions.utils.imagesize;

import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;

public class imageSize
{
	private String imageName;
	private int width=-1;
	private int height=-1;

	private void calculateSize()
	{
	try
		{
		File infn = new File( imageName );
		FileImageInputStream inf = new FileImageInputStream( infn );
		java.util.Iterator it = ImageIO.getImageReaders( inf );
		Object rdr = it.next();
		ImageReader reader = (ImageReader)rdr;

		reader.setInput( inf );
		int indx = reader.getMinIndex();
		width = reader.getWidth( indx );
		height = reader.getHeight( indx );
		}
	catch( FileNotFoundException efnf )
		{
		System.out.println( "File " + imageName + " not found: " + efnf );
		}
	catch( IOException eio )
		{
		System.out.println( "IO Exception reading File " + imageName + " : " + eio );
		}

	}
	public imageSize()
   {
   imageName="---Not Set----";
   width = -1;
   height = -1;
	}

	public imageSize( String image )
	{
	imageName = image;
	calculateSize();
	}

	public static String test()
	{
	return "311";
	}
	public static String getWidthXalan( org.apache.xalan.extensions.ExpressionContext con )
	{
		String fn = "src/images/bimage1.jpg";
		imageSize tmp = new imageSize( fn );
		return Integer.toString( tmp.getWidth() );
	}
	public static String getHeight( String fn )
	{
		imageSize tmp = new imageSize( fn );
		return Integer.toString( tmp.getHeight() );
	}

	public static void main(String[] args)
   {
   	System.out.println( "About to get info for " + args.length + " files:" );
   	for( int i=0; i<args.length; ++i )
   		{
			imageSize imagesize = new imageSize(args[i]);

			System.out.println( "   Image " + args[i] + " is " + imagesize.getWidth()
				+ " by " + imagesize.getHeight() );
			}

	}


	public String getImageName()
	{
		return imageName;
	}

	public void setImageName(String value)
	{
		imageName = value;
	}

	public int getWidth()
	{
		return width;
	}


	public int getHeight()
	{
		return height;
	}

}
