⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fsimageconstructor.java

📁 利用opensource的开源jar实现生成flash文件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 *  FSImageConstructor.java
 *  Transform Utilities
 *
 * Copyright (c) 2001-2006 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.util;

import java.io.*;
import java.util.*;
import java.util.zip.*;

import com.flagstone.transform.FSDefineObject;
import com.flagstone.transform.FSDefineImage;
import com.flagstone.transform.FSDefineImage2;
import com.flagstone.transform.FSDefineJPEGImage2;
import com.flagstone.transform.FSDefineShape3;

import com.flagstone.transform.FSCoder;
import com.flagstone.transform.FSShape;
import com.flagstone.transform.FSCoordTransform;
import com.flagstone.transform.FSBounds;
import com.flagstone.transform.FSShapeStyle;
import com.flagstone.transform.FSLine;

import com.flagstone.transform.FSSolidLine;

import com.flagstone.transform.FSFillStyle;
import com.flagstone.transform.FSBitmapFill;

/**
 * The FSImageConstructor class is used to generate an image definition object from an image stored
 * in a file. Currently PNG, BMP and JPEG encoded images are supported. The name of the file containing 
 * the image can be specified directly in the class constructor:</p>
 *
 * <pre>
 *     FSImageConstructor imageConstructor = new FSImageConstructor(imageFile);
  * </pre>
 *
 * A single FSImageConstructor object can also be used to load a series of images:
 *
 * <pre>
 *     imageConstructor.setImageFromFile(imageFile);
 * </pre>
 *
 * <P>If an error occurs each method will throw either a FileNotFoundException, IOException or 
 * DataFormatException depending on the error detected.</P>
 *
 * <P>When an image is loaded the getFormat() method identifies the way the image is encoded:</P>
 *
 * <table>
 * <tr><th>Format</th><th>Description</th></tr>
 *
 * <tr>
 * <td>JPEG</td>
 * <td>A JPEG encoded image.</td>
 * </tr>
 *
 * <tr>
 * <td>IDX8</td>
 * <td>An indexed image where each pixel specifies an index into a colour table containing up to 
 * 256 24-bit colours. Transparent colours are not supported.</td>
 * </tr>
 *
 * <tr>
 * <td>IDXA</td>
 * <td>An indexed image where each pixel specifies an index into a colour table containing up to 
 * 256 32-bit colours - 8-bits for each colour channel and 8-bits for transparency.</td>
 * </tr>
 *
 * <tr>
 * <td>RGB5</td>
 * <td>An true colour image where each pixel specifies a 16-bit colour, with 5-bits per colour
 * channel. Transparent colours are not supported.</td>
 * </tr>
 *
 * <tr>
 * <td>RGB8</td>
 * <td>An true colour image where each pixel specifies a 24-bit colour, with 8-bits per colour
 * channel. Each pixel occupies 32-bits. Transparent colours are not supported so the first 
 * (most significant) byte is set to 255 by default.</td>
 * </tr>
 *
 * <tr>
 * <td>RGBA</td>
 * <td>An true colour image where each pixel specifies a 32-bit colour, with 8-bits per colour
 * channel and 8-bits for transparency. The first (most significant) byte contains the 
 * transparency information.</td>
 * </tr>
 *
 * </table>
 *
 * <P>Once an image is loaded the definition required to add the image to a Flash file is 
 * generated using the defineImage() method:</p>
 *
 * <pre>
 *     movie.add(imageConstructor.defineImage(movie.newIdentifier()));
 * </pre>
 *
 * <P>The defineImage()method returns an FSDefineObject (the abstract base class for all objects used 
 * to define shapes etc. in a Flash file. The exact class of the object generated depends of the format 
 * of the image loaded.</P>
 *
 * <table>
 * <tr><th>Class</th><th>Generated when...</th></tr>
 *
 * <tr>
 * <td>FSDefineJPEGImage2</td>
 * <td>A JPEG encoded image is loaded. The getFormat() method returns the class constant JPEG.</td>
 * </tr>
 *
 * <tr>
 * <td>FSDefineImage</td>
 * <td>An indexed BMP or PNG image contains a colour table without transparent colours or 
 * when a true colour image contains 16-bit or 24-bit colours is loaded. The getFormat() method returns 
 * the class constants IDX8, RGB5 or RGB8.</td>
 * </tr>
 *
 * <tr>
 * <td>FSDefineImage2</td>
 * <td>A BMP or PNG indexed image contains a colour table with transparent colours is loaded or 
 * when a true colour image contains 32-bit bit colours. The getFormat() method returns the class 
 * constants IDXA or RGBA.</td>
 * </tr>
 *
 * </table>
 * 
 * <P>Images are displayed in Flash by filling a shape with the image bitmap. The defineEnclosingShape()
 * method generates a rectangular shape object which wraps the image:
 *
 * <pre>
 *     int imageId = movie.newIdentifier();
 *     int shapeId = movie.newIdentifier();
 * 
 *     int xOrigin = imageConstructor.getWidth()/2;
 *     int yOrigin = imageConstructor.getHeight()/2;
 * 
 *     boolean border = false;
 * 
 *     movie.add(imageConstructor.defineImage(imageId));
 *     movie.add(imageConstructor.defineEnclosingShape(shapeId, imageId, xOrigin, yOrigin, border));
 * </pre>
 * 
 * <P>Here the origin, used when placing the shape on the screen, is defined as the centre of the shape.
 * Other points may be defined to suit the alignment of the shape when it is placed on the display list.</P>
 * 
 * <h2>Post Processing</h2>
 * 
 * <P>Once an image has been loaded, FSImageConstructor supports a range of methods to access the colour table 
 * or image data - depending on the image format. This allows the image data to be processed before the objects
 * used to add the image to a Flash file are defined.
 * 
 * <pre>
 *     imageConstructor.getColorTable();
 *     imageConstructor.getIndexedImage();
 *     imageConstructor.setColorImage();
 *     imageConstructor.getJPEGImage();
 * </pre>
 * 
 * <P>The information returned will depend on the image format. If an indexed image is loaded then the 
 * getColourTable() and getIndexedImage() will return the arrays of bytes for the colour table and image
 * respectively. If getColorImage() or getJPEGImage() are used on an indexed image then they will return 
 * null objects.</P>
 * 
 * <P>Once the image data has been processed separate set methods are available depending on the type of 
 * image:</P>
 *
 * <pre>
 *     imageConstructor.setIndexedImage(format, imageWidth, imageHeight, colourTable, image);
 *     imageConstructor.setColorImage(format, imageWidth, imageHeight, image);
 *     imageConstructor.setJPEGImage(imageWidth, imageHeight, image);
 * </pre>
 */
public class FSImageConstructor
{
    /** Format for JPEG encoded images */
    public static final int JPEG = 0;
    /** Format for indexed images containing a colour table with 24-bit colours. */
    public static final int IDX8 = 1;
    /** Format for indexed images containing a colour table with 32-bit colours. */
    public static final int IDXA = 2;
    /** Format for true colour images containing 16-bit colours, 5-bits for each colour channel. */
    public static final int RGB5 = 3;
    /** Format for true colour images containing 24-bit colours, 8-bits for each colour channel. */
    public static final int RGB8 = 4;
    /** Format for true colour images containing 32-bit colours, 8-bits for each colour channel plus transparency. */
    public static final int RGBA = 5;
    
    // Tables mapping greyscale values onto 8-bit colour channels
    
    private static final int[] monochrome = { 0, 255 };
    private static final int[] greyscale2 = { 0, 85, 170, 255 };
    private static final int[] greyscale4 = { 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255 };
    
    // Constants used for BMP images 
    
    private static final int[] bmpSignature = { 66, 77 };

    private final int BI_RGB = 0;
    private final int BI_RLE8 = 1;
    private final int BI_RLE4 = 2;
    private final int BI_BITFIELDS = 3;

    // Constants used for PNG images 
        
    private static final int[] pngSignature = { 137, 80, 78, 71, 13, 10, 26, 10 };

    /*
     * The constants commented out in the following section are defined
     * for completeness when decoding LNG format images.
     */
     
    // private static final int CRITICAL_CHUNK = 0x20000000;
    
    private static final int IHDR = 0x49484452;
    private static final int PLTE = 0x504c5445;
    private static final int IDAT = 0x49444154;
    private static final int IEND = 0x49454e44;
    private static final int tRNS = 0x74524e53;
    // private static final int bKGD = 0x624b4744;
    // private static final int cHRM = 0x6348524d;
    // private static final int fRAc = 0x66524163;
    // private static final int gAMA = 0x67414d41;
    // private static final int gIFg = 0x67494667;
    // private static final int gIFt = 0x67494674;
    // private static final int gIFx = 0x67494678;
    // private static final int hIST = 0x68495354;
    // private static final int iCCP = 0x69434350;
    // private static final int iTXt = 0x69545874;
    // private static final int oFFs = 0x6f464673;
    // private static final int pCAL = 0x7043414c;
    // private static final int pHYs = 0x70485973;
    // private static final int sBIT = 0x73424954;
    // private static final int sCAL = 0x7343414c;
    // private static final int sPLT = 0x73504c54;
    // private static final int sRGB = 0x73524742;
    // private static final int tEXt = 0x74455874;
    // private static final int tIME = 0x74494d45;
    // private static final int zTXt = 0x7a545874;
    
    private static final int GREYSCALE = 0;
    private static final int TRUE_COLOUR = 2;
    private static final int INDEXED_COLOUR = 3;
    private static final int ALPHA_GREYSCALE = 4;
    private static final int ALPHA_TRUECOLOUR = 6;
    
    private static final int NO_FILTER = 0;
    private static final int SUB_FILTER = 1;
    private static final int UP_FILTER = 2;
    private static final int AVG_FILTER = 3;
    private static final int PAETH_FILTER = 4;
    
    private static final int[] startRow =        { 0, 0, 4, 0, 2, 0, 1 };
    private static final int[] startColumn =     { 0, 4, 0, 2, 0, 1, 0 };
    private static final int[] rowIncrement =    { 8, 8, 8, 4, 4, 2, 2 };
    private static final int[] columnIncrement = { 8, 8, 4, 4, 2, 2, 1 };
    
    // Attributes shred between different image formats
    private static final int BIT_DEPTH = 0;
    private static final int COLOUR_COMPONENTS = 1;
    private static final int COMPRESSION_METHOD = 2;
    
    // BMP specific attributes
    private static final int RED_MASK = 4;
    private static final int GREEN_MASK = 5;
    private static final int BLUE_MASK = 6;

    // PNG specific attributes
    private static final int COLOUR_TYPE = 4;
    private static final int FILTER_METHOD = 5;
    private static final int INTERLACE_METHOD = 6;
    private static final int TRANSPARENT_GREY = 7;
    private static final int TRANSPARENT_RED = 8;
    private static final int TRANSPARENT_GREEN = 9;
    private static final int TRANSPARENT_BLUE = 10;
    
    private int format = 0;
    private int width = 0;
    private int height = 0;

    private byte[][] colourTable = null;
    private byte[][] indexedImage = null;
    private byte[][][] colorImage = null;
    private byte[] jpegImage = null;
    
    private int[] attributes = new int[16];
    private byte[] chunkData = new byte[0];
    
    /**
     * Constructs an FSImageConstructor object with no image.
     */
    public FSImageConstructor()
    {
    }
    
    /**
     * Constructs and FSImageConstructor object and loads the image from the specified file. The FSImageConstructor
     * class support Windows bitmap (BMP), Portable Network Graphics (PNG) or JPEG encoded images.
     *
     * @param filename the name of the file containing the image.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -