imagedatafactory.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 668 行 · 第 1/2 页

JAVA
668
字号
            createImmutableImageDataRegion(dataDest, dataSource,                                           x, y, width, height,                                           transform,                                           dataSource.isMutable());        } catch(OutOfMemoryError e) {            garbageCollectImages(false);            try {                createImmutableImageDataRegion(dataDest,                                               dataSource,                                               x, y, width, height,                                               transform,                                               dataSource.isMutable());            } catch(OutOfMemoryError e2) {                garbageCollectImages(true);                createImmutableImageDataRegion(dataDest, dataSource,                                               x, y, width, height,                                               transform,                                               dataSource.isMutable());            }        }        return dataDest;    }    /**     * Creates an immutable image from decoded image data obtained from an     * <code>InputStream</code>.  This method blocks until all image data has     * been read and decoded.  After this method completes (whether by     * returning or by throwing an exception) the stream is left open and its     * current position is undefined.     *     * @param stream the name of the resource containing the image data     * in one of the supported image formats     *     * @return the created image data     * @throws java.io.IOException if an I/O error occurs, if the image data     * cannot be loaded, or if the image data cannot be decoded     *     */    public ImageData createImmutableImageData(InputStream stream)      throws IOException {        ImageData data = new ImageData();        // width, height and native data will be set below        try {            getImageDataFromStream(data, stream);        } catch(OutOfMemoryError e) {            garbageCollectImages(false);            try {                getImageDataFromStream(data, stream);            } catch(OutOfMemoryError e2) {                garbageCollectImages(true);                getImageDataFromStream(data, stream);            }        }        return data;    }    /**     * Creates an immutable image from a sequence of ARGB values, specified     * as <code>0xAARRGGBB</code>.     * The ARGB data within the <code>rgb</code> array is arranged     * horizontally from left to right within each row,     * row by row from top to bottom.     * If <code>processAlpha</code> is <code>true</code>,     * the high-order byte specifies opacity; that is,     * <code>0x00RRGGBB</code> specifies     * a fully transparent pixel and <code>0xFFRRGGBB</code> specifies     * a fully opaque     * pixel.  Intermediate alpha values specify semitransparency.  If the     * implementation does not support alpha blending for image rendering     * operations, it must replace any semitransparent pixels with fully     * transparent pixels.  (See <a href="#alpha">Alpha Processing</a>     * for further discussion.)  If <code>processAlpha</code> is     * <code>false</code>, the alpha values     * are ignored and all pixels must be treated as fully opaque.     *     * <p>Consider <code>P(a,b)</code> to be the value of the pixel     * located at column <code>a</code> and row <code>b</code> of the     * Image, where rows and columns are numbered downward from the     * top starting at zero, and columns are numbered rightward from     * the left starting at zero. This operation can then be defined     * as:</p>     *     * <TABLE BORDER="2">     * <TR>     * <TD ROWSPAN="1" COLSPAN="1">     *    <pre><code>     *    P(a, b) = rgb[a + b * width];    </code></pre>     * </TD>     * </TR>     * </TABLE>     * <p>for</p>     *     * <TABLE BORDER="2">     * <TR>     * <TD ROWSPAN="1" COLSPAN="1">     *    <pre><code>     *     0 &lt;= a &lt; width     *     0 &lt;= b &lt; height    </code></pre>     * </TD>     * </TR>     * </TABLE>     * <p> </p>     *     * @param rgb an array of ARGB values that composes the image     * @param width the width of the image     * @param height the height of the image     * @param processAlpha <code>true</code> if <code>rgb</code>     * has an alpha channel,     * <code>false</code> if all pixels are fully opaque     * @return the created image data     */    public ImageData createImmutableImageData(int rgb[], int width, int height,                                              boolean processAlpha) {        ImageData data = new ImageData(width, height, false);        // create native image data below        try {            createImmutableImageDecodeRGBImage(data, rgb,                                               width, height,                                               processAlpha);        } catch(OutOfMemoryError e) {            garbageCollectImages(false);            try {                createImmutableImageDecodeRGBImage(data, rgb,                                                   width, height,                                                   processAlpha);            } catch(OutOfMemoryError e2) {                garbageCollectImages(true);                createImmutableImageDecodeRGBImage(data, rgb,                                                   width, height,                                                   processAlpha);            }        }        return data;    }    /**     * Create a immutable image from romized image data.     *     * @param imageDataArrayPtr native pointer to image data as Java int     * @param imageDataArrayLength length of image data array     * @return the created image data     * @throws IllegalArgumentException if the id is invalid     */    public ImageData createImmutableImageData(int imageDataArrayPtr,            int imageDataArrayLength) {        ImageData data = new ImageData();        // width, height and native image data will be set below        if (!loadRomizedImage(data, imageDataArrayPtr,                    imageDataArrayLength)) {            throw new IllegalArgumentException();        }        return data;    }        /**      * Load and create image data from cache. The real work is done in      * the native function.      *      * @param   data The ImageData object      * @param   resName  Image resource name      * @return  true if image was loaded and created, false otherwise      */     private boolean loadAndCreateImmutableImageDataFromCache(ImageData data,                                                              String resName) {         try {             return imageCache.loadAndCreateImmutableImageData(data, resName);        } catch(OutOfMemoryError e) {             garbageCollectImages(false);              try {                 return imageCache.loadAndCreateImmutableImageData(data,                                                                 resName);            } catch(OutOfMemoryError e2) {                 garbageCollectImages(true);                  return imageCache.loadAndCreateImmutableImageData(data,                                                                 resName);            }         }     }     /**     * helper function called by the create functions above.     * Upon return, the input stream will be closed.     *     * @param data  the ImageData object     * @param istream the name of the input stream containing image     *                data in a supported format     * @throws IOException if there is an error with the stream     */    private void getImageDataFromStream(ImageData data, InputStream istream)        throws java.io.IOException    {        int blocksize = 4096; // the size of blocks to read and allocate        /*         * Allocate an array assuming available is correct.         * Only reading an EOF is the real end of file         * so allocate an extra byte to read the EOF into.         * If available is incorrect, increase the buffer         * size and keep reading.         */        int l = istream.available();        byte[] buffer = new byte[l+1];        int length = 0;        // TBD: Guard against an implementation with incorrect available        while ((l = istream.read(buffer, length,                                 buffer.length-length)) != -1) {            length += l;            if (length == buffer.length) {                byte[] b = new byte[buffer.length + blocksize];                System.arraycopy(buffer, 0, b, 0, length);                buffer = b;            }        }        try {            createImmutableImageDecodeImage(data, buffer, 0, length);        } catch (IllegalArgumentException iae) {            // Data cannot be not decoded            throw new java.io.IOException();        } finally {            istream.close();        }    }    /**     * Create a mutable image data     *     * @param data The ImageData object     * @param width The width of the new mutable image     * @param height The height of the new mutable image     */    private native void createMutableImageData(ImageData data,                                               int width, int height);    /**     * Native function to create an immutable copy of an image data.     *     * @param dest  The ImageData where to make a copy     * @param source The Image to make a copy of, either mutable or immutable.     */    private native void createImmutableImageDataCopy(ImageData dest,                                                     ImageData source);    /**     * Native function that creates an immutable image data from     * a region of another image data, applying the given transform     *     * @param dataDest The ImageData to make a copy to     * @param dataSource The ImageData to make a copy of     * @param x The horizontal offset of the top left of the region to copy     * @param y The vertical offset of the top left of the region to copy     * @param width The width of the new Image     * @param height The height of the new Image     * @param transform The transformation to apply     * @param isMutable True if the Image is mutable, false otherwise     *     */    private native void createImmutableImageDataRegion(ImageData dataDest,                                                       ImageData dataSource,                                                       int x, int y,                                                       int width, int height,                                                       int transform,                                                       boolean isMutable);    /**     * Native function to decode an ImageData from a byte array     *     * @param data The ImageData object     * @param inputData The byte array image data     * @param offset The start of the image data within the byte array     * @param length The length of the image data in the byte array     * @throws IllegalArgumentException if the data cannot be decoded     */    private native void createImmutableImageDecodeImage(ImageData data,                                                        byte[] inputData,                                                        int offset,                                                        int length);    /**     * Native function to load an ImageData directly out of the rom image.     *     * @param data the ImageData object     * @param imageDataArrayPtr native pointer to image data as Java int     * @param imageDataArrayPtrLength length of image data array     * @return true if the imaged data loading was successful,     *         otherwise it returns false     */    private native boolean loadRomizedImage(ImageData data,            int imageDataArrayPtr, int imageDataArrayPtrLength);    /**     * Native function to decode an ImageData from an array of RGB data     *     * @param data the ImageData object     * @param inputData an array of ARGB values that composes     *                  the image.     * @param width the width of the image     * @param height the height of the image     * @param processAlpha true if rgb has an alpha channel,     *                     false if all pixels are fully opaque     */    private native void createImmutableImageDecodeRGBImage(ImageData data,                                                           int[] inputData,                                                           int width,                                                           int height,                                                   boolean processAlpha);    /**     * Garbage collected to free native resources from zombie images.     *     * @param doFullGC boolean indicating whether to do a full GC or only     *                 request young generation GC.     */    private static native void garbageCollectImages(boolean doFullGC);}

⌨️ 快捷键说明

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