/*
 * FSDefineJPEGImage.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

/**
 * FSDefineJPEGImage is used to define a JPEG encoded image.
 * 
 * <p>
 * FSDefineJPEGImage objects only contain the image data, the encoding table for
 * the image is defined in a FSJPEGEncodingTable object. All images using a
 * shared FSJPEGEncodingTable object to represent the encoding table have the
 * same compression ratio.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>identifier</td>
 * <td>A unique identifier, in the range 1..65535, that is used to reference
 * the image from other objects.</td>
 * </tr>
 * 
 * <tr>
 * <td>image</td>
 * <td>An array of bytes containing the JPEG compressed image.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * Images can only be displayed inside a shape using the FSBitmapFill class.
 * </p>
 * 
 * <p>
 * The image data may be taken directly from a JPEG image file. No further
 * decoding is necessary.
 * </p>
 * 
 * <p>
 * Although the FSDefineJPEGImage class is supposed to be used with the
 * FSJPEGEncodingTable class which defines the encoding table for the images it
 * is not essential. If an FSJPEGEncodingTable object is created with an empty
 * encoding table then the Flash Player will still display the JPEG image
 * correctly.
 * </p>
 * 
 * <h1 class="datasheet">Example</h1>
 * 
 * <p>
 * The simplest way to use the FSJPEGEncodingTable and FSDefineJPEGImage classes
 * to define JPEG encoded images is to create an empty encoding table then
 * construct the FSDefineJPEGImage object with the image data from a file:
 * </p>
 * 
 * <pre>
 * File aFile = new File(filename);
 * byte[] bytes = new byte[(int) aFile.length()];
 * 
 * try
 * {
 * 	FileInputStream imageContents = new FileInputStream(aFile);
 * 	imageContents.read(bytes);
 * 	imageContents.close();
 * } catch (FileNotFoundException e)
 * {
 * 	throw new FileNotFoundException(filename);
 * } catch (IOException e)
 * {
 * 	throw new IOException(filename);
 * }
 * 
 * movie.add(new FSJPEGEncodingTable());
 * movie.add(new FSDefineJPEGImage(movie.newIdentifier(), bytes));
 * </pre>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSDefineJPEGImage class represents the DefineBits data structure from the
 * Macromedia Flash (SWF) File Format Specification. It was introduced in Flash
 * 1.
 * </p>
 */
public class FSDefineJPEGImage extends FSDefineObject
{
	private byte[] image = null;
	private int width;
	private int height;

	/**
	 * Construct an FSDefineJPEGImage object, initialising it with values decoded
	 * from an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSDefineJPEGImage(FSCoder coder)
	{
		super(FSMovieObject.DefineJPEGImage, 0);
		decode(coder);
	}

	/**
	 * Constructs an FSDefineJPEGImage object with the identifier and JPEG data.
	 * 
	 * @param anIdentifier
	 *            the unique identifier for this object
	 * @param bytes
	 *            the JPEG encoded image data.
	 */
	public FSDefineJPEGImage(int anIdentifier, byte[] bytes)
	{
		super(FSMovieObject.DefineJPEGImage, anIdentifier);
		setImage(bytes);
	}

	/**
	 * Constructs an FSDefineJPEGImage object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSDefineJPEGImage object.
	 */
	public FSDefineJPEGImage(FSDefineJPEGImage obj)
	{
		super(obj);
		width = obj.width;
		height = obj.height;
		image = Transform.clone(obj.image);
	}

	/**
	 * Gets the width of the image in pixels.
	 * 
	 * @return the width of the image.
	 */
	public int getWidth()
	{
		return width;
	}

	/**
	 * Gets the height of the image in pixels.
	 * 
	 * @return the height of the image in pixels.
	 */
	public int getHeight()
	{
		return height;
	}

	/**
	 * Gets the image data.
	 * 
	 * @return the array of bytes containing the image data.
	 */
	public byte[] getImage()
	{
		return image;
	}

	/**
	 * Sets the image data. The image data may be taken directly from a file
	 * containing a JPEG encoded image.
	 * 
	 * @param bytes
	 *            an array of bytes containing the image data.
	 */
	public void setImage(byte[] bytes)
	{
		image = bytes;
		decodeInfo();
	}

	public Object clone()
	{
		FSDefineJPEGImage anObject = (FSDefineJPEGImage) super.clone();

		anObject.image = Transform.clone(image);
		anObject.width = width;
		anObject.height = height;

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSDefineJPEGImage typedObject = (FSDefineJPEGImage) anObject;

			result = Transform.equals(image, typedObject.image);
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "image", "<data>");
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		super.length(coder);

		length += image.length;

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);
		coder.writeBytes(image);
		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);
		byte[] data = new byte[length - 2];
		coder.readBytes(data);
		setImage(data);
		coder.endObject(name());
	}

	private boolean decodeInfo()
	{
		FSCoder coder = new FSCoder(FSCoder.BIG_ENDIAN, image);

		if (coder.readWord(2, false) != 0xffd8)
			return false;

		while (true)
		{
			int marker = coder.readWord(2, false);
			int size = coder.readWord(2, false);

			if ((marker & 0xff00) != 0xff00)
				return false;

			if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4
							&& marker != 0xffc8)
			{
				coder.readWord(1, false);
				height = coder.readWord(2, false);
				width = coder.readWord(2, false);
				return true;
			} else
			{
				coder.adjustPointer((size - 2) << 3);
			}
		}
	}
}
