imagedatafactory.java

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

JAVA
764
字号
        }        int width = ((imageBytes[imageOffset + 16] & 0x0ff) << 24) +                    ((imageBytes[imageOffset + 17] & 0x0ff) << 16) +                    ((imageBytes[imageOffset + 18] & 0x0ff) <<  8) +                    (imageBytes[imageOffset + 19] & 0x0ff);        int height = ((imageBytes[imageOffset + 20] & 0x0ff) << 24) +                     ((imageBytes[imageOffset + 21] & 0x0ff) << 16) +                     ((imageBytes[imageOffset + 22] & 0x0ff) <<  8) +                     (imageBytes[imageOffset + 23] & 0x0ff);        if (width <= 0 || height <= 0) {            throw new IllegalArgumentException();        }        imageData.initImageData(width, height, false, true);        // load the decoded PNG data into the data byte arrays        if (loadPNG(imageData, imageBytes, imageOffset, imageLength) ==            false) {            // if loadPNG returns false, the image contains            // only opaque pixel and the alpha data is not needed            imageData.removeAlpha();        }    }    /**     * Function to decode an <code>ImageData</code> from JPEG 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 decodeJPEG(ImageData imageData,                            byte[] imageBytes,                            int imageOffset,                            int imageLength) {        // find the format of the image data        if (imageLength < jpegHeader.length + 8) {            throw new IllegalArgumentException();        }        int width = 0;        int height = 0;        /**         * Find SOF (Start Of Frame) marker:         * format of SOF         *   2 bytes    Marker Identity  (0xff 0xc<N>)         *   2 bytes    Length of block         *   1 byte     bits/sample         *   2 bytes    Image Height         *   2 bytes    Image Width         *   1 bytes    Number of components         *   n bytes    the components         *         * Searching for the byte pair representing SOF is unsafe         * because a prior marker might contain the SOFn pattern         * so we must skip over the preceding markers.         *         * When editing this code, don't forget to make the appropriate changes         * in src/lowlevelui/graphics_util/reference/native/gxutl_image_util.c.         */        int idx = imageOffset + 2;        while (idx + 8 < imageOffset + imageLength) {            if (imageBytes[idx] != (byte)0xff) {                break;            }            if ((byte) (imageBytes[idx + 1] & 0xf0) == (byte) 0xc0) {                byte code = imageBytes[idx + 1];                if (code != (byte) 0xc4 || code != (byte) 0xcc) {                    /* Big endian */                    height = ((imageBytes[idx + 5] & 0xff) << 8) +                            (imageBytes[idx + 6] & 0xff);                    width = ((imageBytes[idx + 7] & 0xff) << 8) +                            (imageBytes[idx + 8] & 0xff);                    break;                }            }            /* Go to the next marker */            int field_len = ((imageBytes[idx + 2] & 0xff) << 8) +                    (imageBytes[idx + 3] & 0xff);            idx += field_len + 2;        }        if (width <= 0 || height <= 0) {            throw new IllegalArgumentException();        }        imageData.initImageData(width, height, false, false);        // load the decoded JPEG data into the pixelData array        loadJPEG(imageData, imageBytes, imageOffset, imageLength);    }    /**     * Function to decode an <code>ImageData</code> from RAW 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 decodeRAW(ImageData imageData,                            byte[] imageBytes,                            int imageOffset,                            int imageLength) {        // find the format of the image data        if (imageLength < rawHeader.length + 8) {            throw new IllegalArgumentException();        }        loadRAW(imageData, imageBytes, imageOffset, imageLength);    }    /**     * Function to compare byte data to the given header     *     * @param header header data to compare imageData with     * @param imageData 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     * @return true if the header.length bytes at imageData[imageOffset]     * are equal to the bytes in header array, false otherwise     */    private boolean headerMatch(byte[] header,                                byte[] imageData,                                int imageOffset,                                int imageLength) {        if (imageLength < header.length) {            return false;        }        for (int i = 0; i < header.length; i++) {            if (imageData[imageOffset + i] != header[i]) {                return false;            }        }        return true;    }    /**     * Function to decode an <code>ImageData</code> from byte data.     *     * @param imageData the <code>ImageData<code> to populate     * @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     * @throws IllegalArgumentException if the data is not formatted correctly     */    private void decode(ImageData imageData,                        byte[] imageBytes, int imageOffset, int imageLength) {        // check if it is a PNG image        if (headerMatch(pngHeader, imageBytes, imageOffset, imageLength)) {            // image type is PNG            decodePNG(imageData, imageBytes, imageOffset, imageLength);        } else if (headerMatch(jpegHeader, imageBytes,                               imageOffset, imageLength)) {            // image type is JPEG            decodeJPEG(imageData, imageBytes, imageOffset, imageLength);        } else if (headerMatch(rawHeader, imageBytes,                               imageOffset, imageLength)) {            // image type is RAW            decodeRAW(imageData, imageBytes, imageOffset, imageLength);        } else {            // does not match supported image type            throw new IllegalArgumentException();        }    }    /**     * Native function to load an <code>ImageData</code> from PNG data.     *     * @param imageData the <code>ImageData</code> to load to     * @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     *     * @return true if there is alpha data     */    private native boolean loadPNG(ImageData imageData,                                   byte[] imageBytes,                                   int imageOffset,                                   int imageLength);    /**     * Native function to load an <code>ImageData </code>from JPEG data.     *     * @param imageData the <code>ImageData</code> to load to     * @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 native void loadJPEG(ImageData imageData,                                 byte[] imageBytes,                                 int imageOffset,                                 int imageLength);    /**     * Native function to load an <code>ImageData</code>     * directly out of the rom image.     *     * @param data The <code>ImageData</code> to load to     * @param imageDataArrayPtr native pointer to image data as Java int     * @param imageDataArrayLength length of image data array     * @return true if romized image was loaded successfully,     *         false - otherwise     */    private native boolean loadRomizedImage(ImageData data,            int imageDataArrayPtr, int imageDataArrayLength);    /**     * Native function to load an <code>ImageData</code> from ARGB data.     *     * @param imgData The <code>ImageData</code> to load to     * @param rgb the array of image data in a ARGB format     */    private native void loadRGB(ImageData imgData, int[] rgb);    /**     * Native function to load an <code>ImageData</code> from RAW data.     *     * @param imgData The <code>ImageData</code> to load to     * @param imageBytes the array of image data in a RAW format     */    private native void loadRAW(ImageData imgData,                                byte[] imageBytes,                                int imageOffset,                                int imageLength);    /**     * Copy either Java or romized data into another <code>ImageData</code>     * from an <code>ImageData</code> region.     *     * @param dest the <code>ImageData</code> to copy to     * @param source the source image 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 width the width of the region to be copied     * @param height the height of the region to be copied     * @param transform the transform to be applied to the region     */    private native void loadRegion(ImageData dest, ImageData source,                                   int x, int y,                                   int width, int height,                                   int transform);}

⌨️ 快捷键说明

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