/*
 * Copyright 2011 Specto Technologies Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.spectotechnologies.imageio.plugins.eps;

import java.io.IOException;
import java.util.Locale;
import javax.imageio.ImageReader;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;

/**
 *
 * @author Specto Technologies Inc., Alexandre Lavoie
 */
public class EPSImageReaderSPI extends ImageReaderSpi
{
	static final String vendorName = "My Company";
	static final String version = "1.0_beta33_build9467";
	static final String readerClassName = "com.spectotechnologies.imageio.plugins.eps.EPSImageReader";//com.mycompany.imageio.MyFormatImageReader";
	static final String[] names = { "myformat" };
	static final String[] suffixes = { "eps" };
	static final String[] MIMETypes = { "application/eps" };
	static final String[] writerSpiNames = { "com.spectotechnologies.imageio.plugins.eps.EPSImageWriterSPI" };
	// Metadata formats, more information below
	static final boolean supportsStandardStreamMetadataFormat = false;
	static final String nativeStreamMetadataFormatName = null;
	static final String nativeStreamMetadataFormatClassName = null;
	static final String[] extraStreamMetadataFormatNames = null;
	static final String[] extraStreamMetadataFormatClassNames = null;
	static final boolean supportsStandardImageMetadataFormat = false;
	static final String nativeImageMetadataFormatName = "com.spectotechnologies.imageio.plugins.eps.EPSMetaData_1.0";
	static final String nativeImageMetadataFormatClassName = "com.spectotechnologies.imageio.plugins.eps.EPSMetaData";
	static final String[] extraImageMetadataFormatNames = null;
	static final String[] extraImageMetadataFormatClassNames = null;

	public EPSImageReaderSPI()
	{
		super(vendorName,
				version,
				names,
				suffixes,
				MIMETypes,
				readerClassName,
				STANDARD_INPUT_TYPE, // Accept ImageInputStreams
				writerSpiNames,
				supportsStandardStreamMetadataFormat,
				nativeStreamMetadataFormatName,
				nativeStreamMetadataFormatClassName,
				extraStreamMetadataFormatNames,
				extraStreamMetadataFormatClassNames,
				supportsStandardImageMetadataFormat,
				nativeImageMetadataFormatName,
				nativeImageMetadataFormatClassName,
				extraImageMetadataFormatNames,
				extraImageMetadataFormatClassNames
		);
	}

	@Override
	public String getDescription(Locale locale)
	{
		// Localize as appropriate
		return "Description goes here";
	}

	@Override
	public boolean canDecodeInput(Object p_oInput) throws IOException
	{
		byte[] aStart = new byte[4];

		if(!(p_oInput instanceof ImageInputStream))
		{
			return false;
		}

		ImageInputStream oDataStream = (ImageInputStream)p_oInput;

		String sSignature;
		
		try
		{
			oDataStream.mark();

			oDataStream.read(aStart);
			while(aStart[0] != '%' || aStart[1] != '!' || aStart[2] != 'P' || aStart[3] != 'S')
			{
				aStart[0] = aStart[1];
				aStart[1] = aStart[2];
				aStart[2] = aStart[3];
				aStart[3] = oDataStream.readByte();
			}
			sSignature = new String(aStart);
			oDataStream.reset();
		}
		catch(IOException e)
		{
			return false;
		}

		// Cast unsigned character constants prior to comparison
		return sSignature.compareTo("%!PS") == 0;
	}

	@Override
	public ImageReader createReaderInstance(Object extension)
	{
		return new EPSImageReader(this);
	}
}
