imagedatafactory.java

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

JAVA
764
字号
     *     * <p>     * The size of the returned image will be the size of the specified region     * with the transform applied.  For example, if the region is     * <code>100&nbsp;x&nbsp;50</code> pixels and the transform is     * <code>TRANS_ROT90</code>, the     * returned image will be <code>50&nbsp;x&nbsp;100</code> pixels.</p>     *     * <p><strong>Note:</strong> If all of the following conditions     * are met, this method may     * simply return the source <code>Image</code> without creating a     * new one:</p>     * <ul>     * <li>the source image is immutable;</li>     * <li>the region represents the entire source image; and</li>     * <li>the transform is <code>TRANS_NONE</code>.</li>     * </ul>     *     * @param dataSource the source image data to be copied from     * @param x the horizontal location of the region to be copied     * @param y the vertical location of the region to be copied     * @param w the width of the region to be copied     * @param h the height of the region to be copied     * @param transform the transform to be applied to the region     * @return the new, immutable image     *     * @throws NullPointerException if <code>image</code> is <code>null</code>     * @throws IllegalArgumentException if the region to be copied exceeds     * the bounds of the source image     * @throws IllegalArgumentException if either <code>width</code> or     * <code>height</code> is zero or less     * @throws IllegalArgumentException if the <code>transform</code>     * is not valid     *     */    public ImageData createImmutableImageData(ImageData dataSource,                                              int x, int y, int w, int h,                                              int transform) {        ImageData dataDest = null;        if ((transform & Image.TRANSFORM_SWAP_AXIS) ==            Image.TRANSFORM_SWAP_AXIS) {            dataDest = new ImageData(h, w, false, false,                                     dataSource.hasAlpha());        } else {            dataDest = new ImageData(w, h, false, false,                                     dataSource.hasAlpha());        }        // Copy either the Java or romized data to its own array        loadRegion(dataDest, dataSource, x, y, w, h, transform);        return dataDest;    }    /**     * Creates an immutable <code>ImageData</code>     * 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 <code>ImageData</code>     * @throws NullPointerException if <code>rgb</code> is <code>null</code>.     * @throws IllegalArgumentException if either <code>width</code> or     * <code>height</code> is zero or less     * @throws ArrayIndexOutOfBoundsException if the length of     * <code>rgb</code> is     * less than<code> width&nbsp;*&nbsp;height</code>.     *     */    public ImageData createImmutableImageData(int rgb[], int width, int height,                                              boolean processAlpha) {        ImageData data = new ImageData(width, height, false, false,                                       processAlpha);        loadRGB(data, rgb);        return data;    }    /**     * Function to decode an <code>ImageData</code> from romized data.     *     * @param imageDataArrayPtr native pointer to image data as Java int     * @param imageDataArrayLength length of image data array     * @return <code>ImageData</code> created from romized data.     * @throws IllegalArgumentException if the id is invalid     */    public ImageData createImmutableImageData(int imageDataArrayPtr,            int imageDataArrayLength) {        ImageData data = new ImageData();        if (!loadRomizedImage(data, imageDataArrayPtr,                    imageDataArrayLength)) {            throw new IllegalArgumentException();        }        return data;    }    /**     * 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     * @throws NullPointerException if <code>stream</code> is <code>null</code>     * @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();        createImageFromStream(data, stream);        return data;    }    /**     * Populates an immutable <code>ImageData</code>     * from decoded 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 data The <code>ImageData</code> to be populated     * @param stream the name of the resource containing the image data     * in one of the supported image formats     *     * @throws NullPointerException if <code>stream</code> is <code>null</code>     * @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     *     */    private void createImageFromStream(ImageData data, InputStream stream)        throws java.io.IOException {        if (stream == null) {            throw new java.io.IOException();        } else {            /*             * 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 = stream.available();            byte[] buffer = new byte[l+1];            int length = 0;            // TBD: Guard against an implementation with incorrect available            while ((l = stream.read(buffer, length, buffer.length-length))                   != -1) {                length += l;                if (length == buffer.length) {                    byte[] b = new byte[buffer.length + 4096];                    System.arraycopy(buffer, 0, b, 0, length);                    buffer = b;                }            }            stream.close();            try {                decode(data, buffer, 0, length);            } catch (IllegalArgumentException e) {                throw new java.io.IOException();            }        }    }    /**     * Load an <code>ImageData</code> from cache. The real work is done in     * the native function.     *     * @param   imageData The <code>ImageData</code> to be populated     * @param   resName  Image resource name     * @return  true if image was loaded and created, false otherwise     */    private boolean loadCachedImage(ImageData imageData,                                    String resName) {        return imageCache.loadAndCreateImmutableImageData(imageData,                                                             resName);    }    /**     * Function to decode an <code>ImageData</code> from PNG data.     *     * @param imageData the <code>ImageData</code> to be populated     * @param imageBytes the array of image data in a supported image format     * @param imageOffset the offset of the start of the data in the array     * @param imageLength the length of the data in the array     */    private void decodePNG(ImageData imageData,                           byte[] imageBytes,                           int imageOffset, int imageLength) {        // find the format of the image data        if (imageLength < pngHeader.length + 8) {            throw new IllegalArgumentException();

⌨️ 快捷键说明

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