/*
 * FSJPEGEncodingTable.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;

/**
 * FSJPEGEncodingTable defines an encoding table for JPEG images.
 * 
 * <p>
 * The encoding table is shared between all images defined using the
 * FSDefineJPEGImage class so there should only be one FSJPEGEncodingTable
 * object defined in a movie.
 * </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>encodingTable</td>
 * <td>An array of bytes containing the encoding table data.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * The FSJPEGEncodingTable class is not essential to define JPEG encoded images
 * in a movie using the FSDefineJPEGImage class. If an FSJPEGEncodingTable
 * object is created with an empty encoding table then the Flash Player will
 * still display JPEG images defined using FSDefineJPEGImage objects correctly.
 * When an FSJPEGEncodingTable with an empty encoding table is encoded to a
 * Flash file, the "end of stream" marker 0xFFD9 is encoded allowing the empty
 * table to be decoded correctly.
 * </p>
 * 
 * <h1 class="datasheet">Examples</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(s) 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 FSJPEGEncodingTable class represents the JPEGEncodingTable tag from the
 * Macromedia Flash (SWF) File Format Specification. It was introduced in Flash
 * 1.
 * </p>
 */
public class FSJPEGEncodingTable extends FSMovieObject
{
	private byte[] encodingTable = null;

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

	/**
	 * Constructs an FSJPEGEncodingTable object with an empty encoding table.
	 */
	public FSJPEGEncodingTable()
	{
		super(FSMovieObject.JPEGTables);
	}

	/**
	 * Constructs an FSJPEGEncodingTable object with the encoding table data.
	 * 
	 * @param bytes
	 *            an array of bytes contains the data for the encoding table.
	 */
	public FSJPEGEncodingTable(byte[] bytes)
	{
		super(FSMovieObject.JPEGTables);
		setEncodingTable(bytes);
	}

	/**
	 * Constructs an FSJPEGEncodingTable object by copying values from an
	 * existing object.
	 * 
	 * @param obj
	 *            an FSJPEGEncodingTable object.
	 */
	public FSJPEGEncodingTable(FSJPEGEncodingTable obj)
	{
		super(obj);

		if (obj.encodingTable != null)
			encodingTable = Transform.clone(obj.encodingTable);
	}

	/**
	 * Gets the encoding table.
	 * 
	 * @return the array of bytes contains the data for the encoding table.
	 */
	public byte[] getEncodingTable()
	{
		return encodingTable;
	}

	/**
	 * Sets the encoding table.
	 * 
	 * @param bytes
	 *            an array of bytes contains the data for the encoding table.
	 */
	public void setEncodingTable(byte[] bytes)
	{
		encodingTable = bytes;
	}

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

		anObject.encodingTable = Transform.clone(encodingTable);

		return anObject;
	}

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

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

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

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

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

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

		length += (encodingTable == null) ? 2 : encodingTable.length;

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);

		if (encodingTable == null)
		{
			coder.writeWord((byte) 0xFF, 1);
			coder.writeWord((byte) 0xD9, 1);
		} else
		{
			coder.writeBytes(encodingTable);
		}
		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);

		if (length == 0)
		{
			encodingTable = null;
		} else
		{
			encodingTable = new byte[length];
			coder.readBytes(encodingTable);
		}
		coder.endObject(name());
	}
}
