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

📄 fsimageconstructor.java

📁 利用opensource的开源jar实现生成flash文件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     *
     * @throws FileNotFoundException is the file cannot be found.
     *
     * @throws IOException if an error occurs while reading the file.
     *
     * @throws DataFormatException if the file contains an unsupported image format or if an error occurs 
     * while decoding the image in the file.
     */
    public FSImageConstructor(String filename) throws IOException, DataFormatException
    {
        setImage(dataFromFile(filename));
    }   
    /**
     * Constructs and FSImageConstructor object and loads the encoded image data. The FSImageConstructor
     * class support Windows bitmap (BMP), Portable Network Graphics (PNG) or JPEG encoded images.
     *
     * @param bytes an array of bytes containing the encoded image.
     *
     * @throws DataFormatException if the data contains an unsupported image format or if an error occurs 
     * while decoding the image.
     */
    public FSImageConstructor(byte[] bytes) throws DataFormatException
    {
        setImage(bytes);
    }
    
    /**
     * Initialises the FSImageConstructor object with the image in the specified file. This method can be used
     * to generate the image definition objects for a Flash movie using the same FSImageConstructor object.
     *
     * @param filename the name of the file containing the image.
     *
     * @throws FileNotFoundException is the file cannot be found.
     *
     * @throws IOException if an error occurs while reading the file.
     *
     * @throws DataFormatException if the file contains an unsupported image format or if an error occurs 
     * while decoding the image in the file.
     */
    public void setImageFromFile(String filename) throws IOException, DataFormatException
    {
        setImage(dataFromFile(filename));
    }
    
    /**
     * Initialises the FSImageConstructor object with the image data.
     *
     * @param bytes an array of bytes containing the encoded image.
     *
     * @throws DataFormatException if the data contains an unsupported image format or if an error occurs 
     * while decoding the image.
     */
    public void setImage(byte[] bytes) throws DataFormatException
    {
        int signature = ((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF);

        format = 0;
        width = 0;
        height = 0;
    
        attributes[BIT_DEPTH] = 0;
        attributes[COLOUR_COMPONENTS] = 0;
        attributes[COLOUR_TYPE] = 0;
        attributes[COMPRESSION_METHOD] = 0;
        attributes[FILTER_METHOD] = 0;
        attributes[INTERLACE_METHOD] = 0;
        chunkData = new byte[0];

        attributes[TRANSPARENT_GREY] = -1;    
        attributes[TRANSPARENT_RED] = -1;
        attributes[TRANSPARENT_GREEN] = -1;
        attributes[TRANSPARENT_BLUE] = -1;

        attributes[RED_MASK] = 0;
        attributes[GREEN_MASK] = 0;
        attributes[BLUE_MASK] = 0;

        colourTable = null;
        indexedImage = null;
        colorImage = null;
        jpegImage = null;
        
        switch (signature)
        {
            case 0xffd8: decodeJPEG(bytes); break;
            case 0x424d: decodeBMP(bytes); break;
            case 0x8950: decodePNG(bytes); break;
            default: 
                throw new DataFormatException("Unsupported image format");  
        }
    }
    
    /**
     * Returns a constant identifying the format for the way the pixels are encoded.
     *
     * @return the image format.
     */
    public int getFormat()
    {
        return format;
    }

    /**
     * Returns the width of the image in pixels.
     * 
     * @return the image width in pixels.
     */
    public int getWidth()
    {
        return width;
    }
    /**
     * Returns the height of the image in pixels.
     * 
     * @return the image height in pixels.
     */
    public int getHeight()
    {
        return height;
    }   
    /**
     * Returns a copy of the colour table used in an indexed image. Each entry in the 
     * colour table contains 4 bytes with one byte for the alpha channel and each
     * of the three colour channels, red, green and blue. The alpha channel occupied the 
     * most significant byte (3) followed by red, green and blue (the least significant byte).
     * 
     * For images in IDX8 format the alpha channel defaults to 255 (completely opaque).
     * 
     * If the image format is JPEG or one of the true colour formats (RGB5, RGB8, RGBA)
     * then the colour table returned is null.
     *
     * @return a two-dimensional array of bytes containing the colours used in an indexed image.
     */
    public byte[][] getColorTable()
    {
        byte[][] table = null;
        
        if (colourTable != null)
        {
            table = new byte[colourTable.length][4];
        
            for (int i=0; i<colourTable.length; i++)
            {
                for (int j=0; j<4; j++)
                    table[i][j] = colourTable[i][j];            
            }
        }
        return table;
    }
    /**
     * Returns a copy of the image data decoded from an indexed image. A two
     * dimensional array is returned, byte[height][width] with each entry containing an 
     * index into the colour table.
     * 
     * If the image format is JPEG or one of the true colour formats (RGB5, RGB8, RGBA)
     * then the indexed image returned is null.
     *
     * @return a two-dimensional array of bytes (height x width) with each entry containing 
     * an index into the colour table.
     */
    public byte[][] getIndexedImage()
    {
        byte[][] image = null;
        
        if (indexedImage != null)
        {
            image = new byte[height][width];
        
            for (int i=0; i<height; i++)
            {
                for (int j=0; j<width; j++)
                    image[i][j] = indexedImage[i][j];            
            }
        }
        return image;
    }
    /**
     * Returns a copy of the image data decoded from a true colour file. A three
     * dimensional array is returned with four bytes for each pixel in the image,
     * byte[height][width][4] - one byte for the red [0], green [1], blue [2] and
     * alpha [3] channels.
     * 
     * If the image format is JPEG or an indexed image (IDX8, IDXA) then the image
     * data returned is null.
     *
     * @return an array of bytes containing the colour channels for each pixel in
     * the image.
     */
    public byte[][][] getColorImage()
    {
        byte[][][] image = null;
        
        if (colorImage != null)
        {
            image = new byte[height][width][4];

            for (int h=0; h<height; h++)
            {
                for (int w=0; w<width; w++)
                {
                    image[h][w][0] = colorImage[h][w][0];    
                    image[h][w][1] = colorImage[h][w][1];    
                    image[h][w][2] = colorImage[h][w][2];    
                    image[h][w][3] = colorImage[h][w][3];    
                }
            }
        }
        return image;
    }
    /**
     * Returns a copy of the encoded data decoded from a JPEG image file.
     * 
     * If the image format is an indexed or true colour image (IDX8, IDXA, RGB5, 
     * RGB8, RGBA) then the indexed image returned is null.
     *
     * @return an array of bytes containing the JPEG encoded image.
     */
    public byte[] getJPEGImage()
    {
        byte[] image = null;
        
        if (jpegImage != null)
        {
            image = new byte[jpegImage.length];
        
            System.arraycopy(jpegImage, 0, image, 0, jpegImage.length);
        }
        return image;
    }
    /**
     * Sets the image data for an indexed image.
     * 
     * @param encoding the format for the encoded image, either IDX8, or IDXA.
     * @param imageWidth the width of the image in pixels.
     * @param imageHeight the height of the image in pixels.
     * @param table a two-dimensional array containing four bytes for each colour, [n][4]. For each 
     * entry [n][3] contains alpha, [n][2] red, [n][1] green and [n][0] blue.
     * @param image a two-dimensional array of bytes, [imageHeight][imageWidth] containing
     * the index into the colour table for each pixel in the image.
     */
    public void setIndexedImage(int encoding, int imageWidth, int imageHeight, byte[][] table, byte[][] image)
    {
        if (encoding != IDX8 && encoding != IDXA)
            throw new IllegalArgumentException("Indexed images must have a format of IDX8 or IDXA");
            
        format = encoding;
        width = imageWidth;
        height = imageHeight;
        
        colourTable = new byte[table.length][4];

        for (int i=0; i<table.length; i++)
        {
            for (int j=0; j<4; j++)
                colourTable[i][j] = table[i][j];    
        }

        indexedImage = new byte[height][width];

        for (int h=0; h<height; h++)
        {
            for (int w=0; w<width; w++)
                indexedImage[h][w] = image[h][w];    
        }
    }

    /**
     * Sets the image data for a true colour image.
     * 
     * @param encoding the format for the encoded image, either RGB5, RGB8 or RGBA.
     * @param imageWidth the width of the image in pixels.
     * @param imageHeight the height of the image in pixels.
     * @param image a three-dimensional array of bytes, [imageHeight][imageWidth][4] containing
     * the colours for each pixel in the image. Four bytes are used to specify the colour. The
     * order of the colour information from the most significant [3] to the least [0] is alpha, red, 
     * green and blue.   
     */
    public void setColorImage(int encoding, int imageWidth, int imageHeight, byte[][][] image)
    {
        if (encoding != RGB5 && encoding != RGB8 && encoding != RGBA)
            throw new IllegalArgumentException("True colour images must has a format of RGB5, RGB8 or RGBA");
            
        format = encoding;
        width = imageWidth;
        height = imageHeight;
        
        colorImage = new byte[height][width][4];

        for (int h=0; h<height; h++)
        {
            for (int w=0; w<width; w++)
            {
                colorImage[h][w][0] = image[h][w][0];    
                colorImage[h][w][1] = image[h][w][1];    
                colorImage[h][w][2] = image[h][w][2];    
                colorImage[h][w][3] = image[h][w][3];    
            }
        }
    }

    /**
     * Sets the image data for a JPEG encoded image.
     * 
     * @param imageWidth the width of the image in pixels.
     * @param imageHeight the height of the image in pixels.
     * @param image an array of bytes containing the JPEG encoded image.   
     */
    public void setJPEGImage(int imageWidth, int imageHeight, byte[] image)
    {
        format = JPEG;
        width = imageWidth;
        height = imageHeight;
        
        jpegImage = new byte[image.length];
        
        System.arraycopy(image, 0, jpegImage, 0, image.length);
    }

    /**
     * Generates an object used to define an image in a Flash file. The class of the object returned is 
     * determined by the format of the image data:
     *
     * <table>
     * <tr><th>Class</th><th>Generated when...</th></tr>

⌨️ 快捷键说明

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