Search in sources :

Example 11 with DataBufferShort

use of java.awt.image.DataBufferShort in project jdk8u_jdk by JetBrains.

the class IncorrectUnmanagedImageRotatedClip method makeUnmanagedBI.

private static BufferedImage makeUnmanagedBI() {
    final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
    final DataBuffer db = bi.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            bi.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return bi;
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer)

Example 12 with DataBufferShort

use of java.awt.image.DataBufferShort in project jdk8u_jdk by JetBrains.

the class ImageUtil method setPackedBinaryData.

/**
     * Sets the supplied <code>Raster</code>'s data from an array
     * of packed binary data of the form returned by
     * <code>getPackedBinaryData()</code>.
     *
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
     * <code>false</code> with the <code>SampleModel</code> of the
     * supplied <code>Raster</code> as argument.
     */
public static void setPackedBinaryData(byte[] binaryDataArray, WritableRaster raster, Rectangle rect) {
    SampleModel sm = raster.getSampleModel();
    if (!isBinary(sm)) {
        throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
    }
    int rectX = rect.x;
    int rectY = rect.y;
    int rectWidth = rect.width;
    int rectHeight = rect.height;
    DataBuffer dataBuffer = raster.getDataBuffer();
    int dx = rectX - raster.getSampleModelTranslateX();
    int dy = rectY - raster.getSampleModelTranslateY();
    MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel) sm;
    int lineStride = mpp.getScanlineStride();
    int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
    int bitOffset = mpp.getBitOffset(dx);
    int b = 0;
    if (bitOffset == 0) {
        if (dataBuffer instanceof DataBufferByte) {
            byte[] data = ((DataBufferByte) dataBuffer).getData();
            if (data == binaryDataArray) {
                // Optimal case: simply return.
                return;
            }
            int stride = (rectWidth + 7) / 8;
            int offset = 0;
            for (int y = 0; y < rectHeight; y++) {
                System.arraycopy(binaryDataArray, offset, data, eltOffset, stride);
                offset += stride;
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
            short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int xRemaining = rectWidth;
                int i = eltOffset;
                while (xRemaining > 8) {
                    data[i++] = (short) (((binaryDataArray[b++] & 0xFF) << 8) | (binaryDataArray[b++] & 0xFF));
                    xRemaining -= 16;
                }
                if (xRemaining > 0) {
                    data[i++] = (short) ((binaryDataArray[b++] & 0xFF) << 8);
                }
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferInt) {
            int[] data = ((DataBufferInt) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int xRemaining = rectWidth;
                int i = eltOffset;
                while (xRemaining > 24) {
                    data[i++] = (int) (((binaryDataArray[b++] & 0xFF) << 24) | ((binaryDataArray[b++] & 0xFF) << 16) | ((binaryDataArray[b++] & 0xFF) << 8) | (binaryDataArray[b++] & 0xFF));
                    xRemaining -= 32;
                }
                int shift = 24;
                while (xRemaining > 0) {
                    data[i] |= (int) ((binaryDataArray[b++] & 0xFF) << shift);
                    shift -= 8;
                    xRemaining -= 8;
                }
                eltOffset += lineStride;
            }
        }
    } else {
        // bitOffset != 0
        int stride = (rectWidth + 7) / 8;
        int offset = 0;
        if (dataBuffer instanceof DataBufferByte) {
            byte[] data = ((DataBufferByte) dataBuffer).getData();
            if ((bitOffset & 7) == 0) {
                for (int y = 0; y < rectHeight; y++) {
                    System.arraycopy(binaryDataArray, offset, data, eltOffset, stride);
                    offset += stride;
                    eltOffset += lineStride;
                }
            } else {
                // bitOffset % 8 != 0
                int rightShift = bitOffset & 7;
                int leftShift = 8 - rightShift;
                int leftShift8 = 8 + leftShift;
                int mask = (byte) (255 << leftShift);
                int mask1 = (byte) ~mask;
                for (int y = 0; y < rectHeight; y++) {
                    int i = eltOffset;
                    int xRemaining = rectWidth;
                    while (xRemaining > 0) {
                        byte datum = binaryDataArray[b++];
                        if (xRemaining > leftShift8) {
                            // when all the bits in this BYTE will be set
                            // into the data buffer.
                            data[i] = (byte) ((data[i] & mask) | ((datum & 0xFF) >>> rightShift));
                            data[++i] = (byte) ((datum & 0xFF) << leftShift);
                        } else if (xRemaining > leftShift) {
                            // All the "leftShift" high bits will be set
                            // into the data buffer.  But not all the
                            // "rightShift" low bits will be set.
                            data[i] = (byte) ((data[i] & mask) | ((datum & 0xFF) >>> rightShift));
                            i++;
                            data[i] = (byte) ((data[i] & mask1) | ((datum & 0xFF) << leftShift));
                        } else {
                            // Less than "leftShift" high bits will be set.
                            int remainMask = (1 << leftShift - xRemaining) - 1;
                            data[i] = (byte) ((data[i] & (mask | remainMask)) | (datum & 0xFF) >>> rightShift & ~remainMask);
                        }
                        xRemaining -= 8;
                    }
                    eltOffset += lineStride;
                }
            }
        } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
            short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
            int rightShift = bitOffset & 7;
            int leftShift = 8 - rightShift;
            int leftShift16 = 16 + leftShift;
            int mask = (short) (~(255 << leftShift));
            int mask1 = (short) (65535 << leftShift);
            int mask2 = (short) ~mask1;
            for (int y = 0; y < rectHeight; y++) {
                int bOffset = bitOffset;
                int xRemaining = rectWidth;
                for (int x = 0; x < rectWidth; x += 8, bOffset += 8, xRemaining -= 8) {
                    int i = eltOffset + (bOffset >> 4);
                    int mod = bOffset & 15;
                    int datum = binaryDataArray[b++] & 0xFF;
                    if (mod <= 8) {
                        // This BYTE is set into one SHORT
                        if (xRemaining < 8) {
                            // Mask the bits to be set.
                            datum &= 255 << 8 - xRemaining;
                        }
                        data[i] = (short) ((data[i] & mask) | (datum << leftShift));
                    } else if (xRemaining > leftShift16) {
                        // This BYTE will be set into two SHORTs
                        data[i] = (short) ((data[i] & mask1) | ((datum >>> rightShift) & 0xFFFF));
                        data[++i] = (short) ((datum << leftShift) & 0xFFFF);
                    } else if (xRemaining > leftShift) {
                        // This BYTE will be set into two SHORTs;
                        // But not all the low bits will be set into SHORT
                        data[i] = (short) ((data[i] & mask1) | ((datum >>> rightShift) & 0xFFFF));
                        i++;
                        data[i] = (short) ((data[i] & mask2) | ((datum << leftShift) & 0xFFFF));
                    } else {
                        // Only some of the high bits will be set into
                        // SHORTs
                        int remainMask = (1 << leftShift - xRemaining) - 1;
                        data[i] = (short) ((data[i] & (mask1 | remainMask)) | ((datum >>> rightShift) & 0xFFFF & ~remainMask));
                    }
                }
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferInt) {
            int[] data = ((DataBufferInt) dataBuffer).getData();
            int rightShift = bitOffset & 7;
            int leftShift = 8 - rightShift;
            int leftShift32 = 32 + leftShift;
            int mask = 0xFFFFFFFF << leftShift;
            int mask1 = ~mask;
            for (int y = 0; y < rectHeight; y++) {
                int bOffset = bitOffset;
                int xRemaining = rectWidth;
                for (int x = 0; x < rectWidth; x += 8, bOffset += 8, xRemaining -= 8) {
                    int i = eltOffset + (bOffset >> 5);
                    int mod = bOffset & 31;
                    int datum = binaryDataArray[b++] & 0xFF;
                    if (mod <= 24) {
                        // This BYTE is set into one INT
                        int shift = 24 - mod;
                        if (xRemaining < 8) {
                            // Mask the bits to be set.
                            datum &= 255 << 8 - xRemaining;
                        }
                        data[i] = (data[i] & (~(255 << shift))) | (datum << shift);
                    } else if (xRemaining > leftShift32) {
                        // All the bits of this BYTE will be set into two INTs
                        data[i] = (data[i] & mask) | (datum >>> rightShift);
                        data[++i] = datum << leftShift;
                    } else if (xRemaining > leftShift) {
                        // This BYTE will be set into two INTs;
                        // But not all the low bits will be set into INT
                        data[i] = (data[i] & mask) | (datum >>> rightShift);
                        i++;
                        data[i] = (data[i] & mask1) | (datum << leftShift);
                    } else {
                        // Only some of the high bits will be set into INT
                        int remainMask = (1 << leftShift - xRemaining) - 1;
                        data[i] = (data[i] & (mask | remainMask)) | (datum >>> rightShift & ~remainMask);
                    }
                }
                eltOffset += lineStride;
            }
        }
    }
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferUShort(java.awt.image.DataBufferUShort) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Example 13 with DataBufferShort

use of java.awt.image.DataBufferShort in project jdk8u_jdk by JetBrains.

the class ImageUtil method getPackedBinaryData.

/**
     * For the case of binary data (<code>isBinary()</code> returns
     * <code>true</code>), return the binary data as a packed byte array.
     * The data will be packed as eight bits per byte with no bit offset,
     * i.e., the first bit in each image line will be the left-most of the
     * first byte of the line.  The line stride in bytes will be
     * <code>(int)((getWidth()+7)/8)</code>.  The length of the returned
     * array will be the line stride multiplied by <code>getHeight()</code>
     *
     * @return the binary data as a packed array of bytes with zero offset
     * of <code>null</code> if the data are not binary.
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
     * <code>false</code> with the <code>SampleModel</code> of the
     * supplied <code>Raster</code> as argument.
     */
public static byte[] getPackedBinaryData(Raster raster, Rectangle rect) {
    SampleModel sm = raster.getSampleModel();
    if (!isBinary(sm)) {
        throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
    }
    int rectX = rect.x;
    int rectY = rect.y;
    int rectWidth = rect.width;
    int rectHeight = rect.height;
    DataBuffer dataBuffer = raster.getDataBuffer();
    int dx = rectX - raster.getSampleModelTranslateX();
    int dy = rectY - raster.getSampleModelTranslateY();
    MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel) sm;
    int lineStride = mpp.getScanlineStride();
    int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
    int bitOffset = mpp.getBitOffset(dx);
    int numBytesPerRow = (rectWidth + 7) / 8;
    if (dataBuffer instanceof DataBufferByte && eltOffset == 0 && bitOffset == 0 && numBytesPerRow == lineStride && ((DataBufferByte) dataBuffer).getData().length == numBytesPerRow * rectHeight) {
        return ((DataBufferByte) dataBuffer).getData();
    }
    byte[] binaryDataArray = new byte[numBytesPerRow * rectHeight];
    int b = 0;
    if (bitOffset == 0) {
        if (dataBuffer instanceof DataBufferByte) {
            byte[] data = ((DataBufferByte) dataBuffer).getData();
            int stride = numBytesPerRow;
            int offset = 0;
            for (int y = 0; y < rectHeight; y++) {
                System.arraycopy(data, eltOffset, binaryDataArray, offset, stride);
                offset += stride;
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
            short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int xRemaining = rectWidth;
                int i = eltOffset;
                while (xRemaining > 8) {
                    short datum = data[i++];
                    binaryDataArray[b++] = (byte) ((datum >>> 8) & 0xFF);
                    binaryDataArray[b++] = (byte) (datum & 0xFF);
                    xRemaining -= 16;
                }
                if (xRemaining > 0) {
                    binaryDataArray[b++] = (byte) ((data[i] >>> 8) & 0XFF);
                }
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferInt) {
            int[] data = ((DataBufferInt) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int xRemaining = rectWidth;
                int i = eltOffset;
                while (xRemaining > 24) {
                    int datum = data[i++];
                    binaryDataArray[b++] = (byte) ((datum >>> 24) & 0xFF);
                    binaryDataArray[b++] = (byte) ((datum >>> 16) & 0xFF);
                    binaryDataArray[b++] = (byte) ((datum >>> 8) & 0xFF);
                    binaryDataArray[b++] = (byte) (datum & 0xFF);
                    xRemaining -= 32;
                }
                int shift = 24;
                while (xRemaining > 0) {
                    binaryDataArray[b++] = (byte) ((data[i] >>> shift) & 0xFF);
                    shift -= 8;
                    xRemaining -= 8;
                }
                eltOffset += lineStride;
            }
        }
    } else {
        // bitOffset != 0
        if (dataBuffer instanceof DataBufferByte) {
            byte[] data = ((DataBufferByte) dataBuffer).getData();
            if ((bitOffset & 7) == 0) {
                int stride = numBytesPerRow;
                int offset = 0;
                for (int y = 0; y < rectHeight; y++) {
                    System.arraycopy(data, eltOffset, binaryDataArray, offset, stride);
                    offset += stride;
                    eltOffset += lineStride;
                }
            } else {
                // bitOffset % 8 != 0
                int leftShift = bitOffset & 7;
                int rightShift = 8 - leftShift;
                for (int y = 0; y < rectHeight; y++) {
                    int i = eltOffset;
                    int xRemaining = rectWidth;
                    while (xRemaining > 0) {
                        if (xRemaining > rightShift) {
                            binaryDataArray[b++] = (byte) (((data[i++] & 0xFF) << leftShift) | ((data[i] & 0xFF) >>> rightShift));
                        } else {
                            binaryDataArray[b++] = (byte) ((data[i] & 0xFF) << leftShift);
                        }
                        xRemaining -= 8;
                    }
                    eltOffset += lineStride;
                }
            }
        } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
            short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int bOffset = bitOffset;
                for (int x = 0; x < rectWidth; x += 8, bOffset += 8) {
                    int i = eltOffset + bOffset / 16;
                    int mod = bOffset % 16;
                    int left = data[i] & 0xFFFF;
                    if (mod <= 8) {
                        binaryDataArray[b++] = (byte) (left >>> (8 - mod));
                    } else {
                        int delta = mod - 8;
                        int right = data[i + 1] & 0xFFFF;
                        binaryDataArray[b++] = (byte) ((left << delta) | (right >>> (16 - delta)));
                    }
                }
                eltOffset += lineStride;
            }
        } else if (dataBuffer instanceof DataBufferInt) {
            int[] data = ((DataBufferInt) dataBuffer).getData();
            for (int y = 0; y < rectHeight; y++) {
                int bOffset = bitOffset;
                for (int x = 0; x < rectWidth; x += 8, bOffset += 8) {
                    int i = eltOffset + bOffset / 32;
                    int mod = bOffset % 32;
                    int left = data[i];
                    if (mod <= 24) {
                        binaryDataArray[b++] = (byte) (left >>> (24 - mod));
                    } else {
                        int delta = mod - 24;
                        int right = data[i + 1];
                        binaryDataArray[b++] = (byte) ((left << delta) | (right >>> (32 - delta)));
                    }
                }
                eltOffset += lineStride;
            }
        }
    }
    return binaryDataArray;
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferUShort(java.awt.image.DataBufferUShort) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Example 14 with DataBufferShort

use of java.awt.image.DataBufferShort in project jdk8u_jdk by JetBrains.

the class ImageUtil method setUnpackedBinaryData.

/**
     * Copies data into the packed array of the <code>Raster</code>
     * from an array of unpacked data of the form returned by
     * <code>getUnpackedBinaryData()</code>.
     *
     * <p> If the data are binary, then the target bit will be set if
     * and only if the corresponding byte is non-zero.
     *
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
     * <code>false</code> with the <code>SampleModel</code> of the
     * supplied <code>Raster</code> as argument.
     */
public static void setUnpackedBinaryData(byte[] bdata, WritableRaster raster, Rectangle rect) {
    SampleModel sm = raster.getSampleModel();
    if (!isBinary(sm)) {
        throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
    }
    int rectX = rect.x;
    int rectY = rect.y;
    int rectWidth = rect.width;
    int rectHeight = rect.height;
    DataBuffer dataBuffer = raster.getDataBuffer();
    int dx = rectX - raster.getSampleModelTranslateX();
    int dy = rectY - raster.getSampleModelTranslateY();
    MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel) sm;
    int lineStride = mpp.getScanlineStride();
    int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
    int bitOffset = mpp.getBitOffset(dx);
    int k = 0;
    if (dataBuffer instanceof DataBufferByte) {
        byte[] data = ((DataBufferByte) dataBuffer).getData();
        for (int y = 0; y < rectHeight; y++) {
            int bOffset = eltOffset * 8 + bitOffset;
            for (int x = 0; x < rectWidth; x++) {
                if (bdata[k++] != (byte) 0) {
                    data[bOffset / 8] |= (byte) (0x00000001 << (7 - bOffset & 7));
                }
                bOffset++;
            }
            eltOffset += lineStride;
        }
    } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
        short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
        for (int y = 0; y < rectHeight; y++) {
            int bOffset = eltOffset * 16 + bitOffset;
            for (int x = 0; x < rectWidth; x++) {
                if (bdata[k++] != (byte) 0) {
                    data[bOffset / 16] |= (short) (0x00000001 << (15 - bOffset % 16));
                }
                bOffset++;
            }
            eltOffset += lineStride;
        }
    } else if (dataBuffer instanceof DataBufferInt) {
        int[] data = ((DataBufferInt) dataBuffer).getData();
        for (int y = 0; y < rectHeight; y++) {
            int bOffset = eltOffset * 32 + bitOffset;
            for (int x = 0; x < rectWidth; x++) {
                if (bdata[k++] != (byte) 0) {
                    data[bOffset / 32] |= (int) (0x00000001 << (31 - bOffset % 32));
                }
                bOffset++;
            }
            eltOffset += lineStride;
        }
    }
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferUShort(java.awt.image.DataBufferUShort) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Example 15 with DataBufferShort

use of java.awt.image.DataBufferShort in project imageio-ext by geosolutions-it.

the class TIFFDecompressor method decode.

/**
 * Decodes the input bit stream (located in the
 * <code>ImageInputStream</code> <code>stream</code>, at offset
 * <code>offset</code>, and continuing for <code>byteCount</code>
 * bytes) into the output <code>BufferedImage</code>
 * <code>image</code>.
 *
 * <p> The default implementation analyzes the destination image
 * to determine if it is suitable as the destination for the
 * <code>decodeRaw</code> method.  If not, a suitable image is
 * created.  Next, <code>decodeRaw</code> is called to perform the
 * actual decoding, and the results are copied into the
 * destination image if necessary.  Subsampling and offsetting are
 * performed automatically.
 *
 * <p> The precise responsibilities of this routine are as
 * follows.  The input bit stream is defined by the instance
 * variables <code>stream</code>, <code>offset</code>, and
 * <code>byteCount</code>.  These bits contain the data for the
 * region of the source image defined by <code>srcMinX</code>,
 * <code>srcMinY</code>, <code>srcWidth</code>, and
 * <code>srcHeight</code>.
 *
 * <p> The source data is required to be subsampling, starting at
 * the <code>sourceXOffset</code>th column and including
 * every <code>subsampleX</code>th pixel thereafter (and similarly
 * for <code>sourceYOffset</code> and
 * <code>subsampleY</code>).
 *
 * <p> Pixels are copied into the destination with an addition shift of
 * (<code>dstXOffset</code>, <code>dstYOffset</code>).  The complete
 * set of formulas relating the source and destination coordinate spaces
 * are:
 *
 * <pre>
 * dx = (sx - sourceXOffset)/subsampleX + dstXOffset;
 * dy = (sy - sourceYOffset)/subsampleY + dstYOffset;
 * </pre>
 *
 * Only source pixels such that <code>(sx - sourceXOffset) %
 * subsampleX == 0</code> and <code>(sy - sourceYOffset) %
 * subsampleY == 0</code> are copied.
 *
 * <p> The inverse mapping, from destination to source coordinates,
 * is one-to-one:
 *
 * <pre>
 * sx = (dx - dstXOffset)*subsampleX + sourceXOffset;
 * sy = (dy - dstYOffset)*subsampleY + sourceYOffset;
 * </pre>
 *
 * <p> The region of the destination image to be updated is given
 * by the instance variables <code>dstMinX</code>,
 * <code>dstMinY</code>, <code>dstWidth</code>, and
 * <code>dstHeight</code>.
 *
 * <p> It is possible that not all of the source data being read
 * will contribute to the destination image.  For example, the
 * destination offsets could be set such that some of the source
 * pixels land outside of the bounds of the image.  As a
 * convenience, the bounds of the active source region (that is,
 * the region of the strip or tile being read that actually
 * contributes to the destination image, taking clipping into
 * account) are available as <code>activeSrcMinX</code>,
 * <code>activeSrcMinY</code>, <code>activeSrcWidth</code> and
 * <code>activeSrcHeight</code>.  Thus, the source pixel at
 * (<code>activeSrcMinX</code>, <code>activeSrcMinY</code>) will
 * map to the destination pixel (<code>dstMinX</code>,
 * <code>dstMinY</code>).
 *
 * <p> The sequence of source bands given by
 * <code>sourceBands</code> are to be copied into the sequence of
 * bands in the destination given by
 * <code>destinationBands</code>.
 *
 * <p> Some standard tag information is provided the instance
 * variables <code>photometricInterpretation</code>,
 * <code>compression</code>, <code>samplesPerPixel</code>,
 * <code>bitsPerSample</code>, <code>sampleFormat</code>,
 * <code>extraSamples</code>, and <code>colorMap</code>.
 *
 * <p> In practice, unless there is a significant performance
 * advantage to be gained by overriding this routine, most users
 * will prefer to use the default implementation of this routine,
 * and instead override the <code>decodeRaw</code> and/or
 * <code>getRawImageType</code> methods.
 *
 * @exception IOException if an error occurs in
 * <code>decodeRaw</code>.
 */
public void decode() throws IOException {
    byte[] byteData = null;
    short[] shortData = null;
    int[] intData = null;
    float[] floatData = null;
    double[] doubleData = null;
    int dstOffset = 0;
    int pixelBitStride = 1;
    int scanlineStride = 0;
    if (useTurbo) {
        decodeRaw(byteData, dstOffset, pixelBitStride, scanlineStride);
    } else {
        // Analyze raw image
        this.rawImage = null;
        if (isImageSimple) {
            if (isBilevel) {
                rawImage = this.image;
            } else if (isContiguous) {
                rawImage = image.getSubimage(dstMinX, dstMinY, dstWidth, dstHeight);
            }
        }
        boolean isDirectCopy = rawImage != null;
        if (rawImage == null) {
            rawImage = createRawImage();
            if (rawImage == null) {
                throw new IIOException("Couldn't create image buffer!");
            }
        }
        WritableRaster ras = rawImage.getRaster();
        if (isBilevel) {
            Rectangle rect = isImageSimple ? new Rectangle(dstMinX, dstMinY, dstWidth, dstHeight) : ras.getBounds();
            byteData = ImageUtil.getPackedBinaryData(ras, rect);
            dstOffset = 0;
            pixelBitStride = 1;
            scanlineStride = (rect.width + 7) / 8;
        } else {
            SampleModel sm = ras.getSampleModel();
            DataBuffer db = ras.getDataBuffer();
            boolean isSupportedType = false;
            if (sm instanceof ComponentSampleModel) {
                ComponentSampleModel csm = (ComponentSampleModel) sm;
                dstOffset = csm.getOffset(-ras.getSampleModelTranslateX(), -ras.getSampleModelTranslateY());
                scanlineStride = csm.getScanlineStride();
                if (db instanceof DataBufferByte) {
                    DataBufferByte dbb = (DataBufferByte) db;
                    byteData = dbb.getData();
                    pixelBitStride = csm.getPixelStride() * 8;
                    isSupportedType = true;
                } else if (db instanceof DataBufferUShort) {
                    DataBufferUShort dbus = (DataBufferUShort) db;
                    shortData = dbus.getData();
                    pixelBitStride = csm.getPixelStride() * 16;
                    isSupportedType = true;
                } else if (db instanceof DataBufferShort) {
                    DataBufferShort dbs = (DataBufferShort) db;
                    shortData = dbs.getData();
                    pixelBitStride = csm.getPixelStride() * 16;
                    isSupportedType = true;
                } else if (db instanceof DataBufferInt) {
                    DataBufferInt dbi = (DataBufferInt) db;
                    intData = dbi.getData();
                    pixelBitStride = csm.getPixelStride() * 32;
                    isSupportedType = true;
                } else if (db instanceof DataBufferFloat) {
                    DataBufferFloat dbf = (DataBufferFloat) db;
                    floatData = dbf.getData();
                    pixelBitStride = csm.getPixelStride() * 32;
                    isSupportedType = true;
                } else if (db instanceof DataBufferDouble) {
                    DataBufferDouble dbf = (DataBufferDouble) db;
                    doubleData = dbf.getData();
                    pixelBitStride = csm.getPixelStride() * 64;
                    isSupportedType = true;
                }
            } else if (sm instanceof MultiPixelPackedSampleModel) {
                MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sm;
                dstOffset = mppsm.getOffset(-ras.getSampleModelTranslateX(), -ras.getSampleModelTranslateY());
                pixelBitStride = mppsm.getPixelBitStride();
                scanlineStride = mppsm.getScanlineStride();
                if (db instanceof DataBufferByte) {
                    DataBufferByte dbb = (DataBufferByte) db;
                    byteData = dbb.getData();
                    isSupportedType = true;
                } else if (db instanceof DataBufferUShort) {
                    DataBufferUShort dbus = (DataBufferUShort) db;
                    shortData = dbus.getData();
                    isSupportedType = true;
                } else if (db instanceof DataBufferInt) {
                    DataBufferInt dbi = (DataBufferInt) db;
                    intData = dbi.getData();
                    isSupportedType = true;
                }
            } else if (sm instanceof SinglePixelPackedSampleModel) {
                SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sm;
                dstOffset = sppsm.getOffset(-ras.getSampleModelTranslateX(), -ras.getSampleModelTranslateY());
                scanlineStride = sppsm.getScanlineStride();
                if (db instanceof DataBufferByte) {
                    DataBufferByte dbb = (DataBufferByte) db;
                    byteData = dbb.getData();
                    pixelBitStride = 8;
                    isSupportedType = true;
                } else if (db instanceof DataBufferUShort) {
                    DataBufferUShort dbus = (DataBufferUShort) db;
                    shortData = dbus.getData();
                    pixelBitStride = 16;
                    isSupportedType = true;
                } else if (db instanceof DataBufferInt) {
                    DataBufferInt dbi = (DataBufferInt) db;
                    intData = dbi.getData();
                    pixelBitStride = 32;
                    isSupportedType = true;
                }
            }
            if (!isSupportedType) {
                throw new IIOException("Unsupported raw image type: SampleModel = " + sm + "; DataBuffer = " + db);
            }
        }
        if (isBilevel) {
            // Bilevel data are always in a contiguous byte buffer.
            decodeRaw(byteData, dstOffset, pixelBitStride, scanlineStride);
        } else {
            SampleModel sm = ras.getSampleModel();
            // bits except at the end of a row.
            if (isDataBufferBitContiguous(sm)) {
                // Use byte or float data directly.
                if (byteData != null) {
                    if (DEBUG) {
                        System.out.println("Decoding bytes directly");
                    }
                    if (offset == 0 && byteCount == 0 && noData != null) {
                        setEmptyTile(byteData, dstOffset, pixelBitStride, scanlineStride, noData.byteValue());
                    } else {
                        decodeRaw(byteData, dstOffset, pixelBitStride, scanlineStride);
                    }
                } else if (floatData != null) {
                    if (DEBUG) {
                        System.out.println("Decoding floats directly");
                    }
                    if (offset == 0 && byteCount == 0 && noData != null) {
                        setEmptyTile(floatData, dstOffset, pixelBitStride, scanlineStride, noData.floatValue());
                    } else {
                        decodeRaw(floatData, dstOffset, pixelBitStride, scanlineStride);
                    }
                } else if (doubleData != null) {
                    if (DEBUG) {
                        System.out.println("Decoding doubles directly");
                    }
                    if (offset == 0 && byteCount == 0 && noData != null) {
                        setEmptyTile(doubleData, dstOffset, pixelBitStride, scanlineStride, noData.doubleValue());
                    } else {
                        decodeRaw(doubleData, dstOffset, pixelBitStride, scanlineStride);
                    }
                } else {
                    if (shortData != null) {
                        if (offset == 0 && byteCount == 0 && noData != null) {
                            setEmptyTile(shortData, dstOffset, pixelBitStride, scanlineStride, noData.shortValue());
                        } else if (areSampleSizesEqual(sm) && sm.getSampleSize(0) == 16) {
                            if (DEBUG) {
                                System.out.println("Decoding shorts directly");
                            }
                            // Decode directly into short data.
                            decodeRaw(shortData, dstOffset, pixelBitStride, scanlineStride);
                        } else {
                            if (DEBUG) {
                                System.out.println("Decoding bytes->shorts");
                            }
                            // Decode into bytes and reformat into shorts.
                            int bpp = getBitsPerPixel(sm);
                            int bytesPerRow = (bpp * srcWidth + 7) / 8;
                            byte[] buf = new byte[bytesPerRow * srcHeight];
                            decodeRaw(buf, 0, bpp, bytesPerRow);
                            reformatData(buf, bytesPerRow, srcHeight, shortData, null, dstOffset, scanlineStride);
                        }
                    } else if (intData != null) {
                        if (offset == 0 && byteCount == 0 && noData != null) {
                            setEmptyTile(intData, dstOffset, pixelBitStride, scanlineStride, noData.intValue());
                        } else if (areSampleSizesEqual(sm) && sm.getSampleSize(0) == 32) {
                            if (DEBUG) {
                                System.out.println("Decoding ints directly");
                            }
                            // Decode directly into int data.
                            decodeRaw(intData, dstOffset, pixelBitStride, scanlineStride);
                        } else {
                            if (DEBUG) {
                                System.out.println("Decoding bytes->ints");
                            }
                            // Decode into bytes and reformat into ints.
                            int bpp = getBitsPerPixel(sm);
                            int bytesPerRow = (bpp * srcWidth + 7) / 8;
                            byte[] buf = new byte[bytesPerRow * srcHeight];
                            decodeRaw(buf, 0, bpp, bytesPerRow);
                            reformatData(buf, bytesPerRow, srcHeight, null, intData, dstOffset, scanlineStride);
                        }
                    }
                }
            } else {
                if (DEBUG) {
                    System.out.println("Decoding discontiguous data");
                }
                // Read discontiguous data into bytes and set the samples
                // into the Raster.
                int bpp = getBitsPerPixel(sm);
                int bytesPerRow = (bpp * srcWidth + 7) / 8;
                byte[] buf = new byte[bytesPerRow * srcHeight];
                decodeRaw(buf, 0, bpp, bytesPerRow);
                reformatDiscontiguousData(buf, bytesPerRow, srcWidth, srcHeight, ras);
            }
        }
        if (colorConverter != null) {
            float[] rgb = new float[3];
            if (byteData != null) {
                for (int j = 0; j < dstHeight; j++) {
                    int idx = dstOffset;
                    for (int i = 0; i < dstWidth; i++) {
                        float x0 = (float) (byteData[idx] & 0xff);
                        float x1 = (float) (byteData[idx + 1] & 0xff);
                        float x2 = (float) (byteData[idx + 2] & 0xff);
                        colorConverter.toRGB(x0, x1, x2, rgb);
                        byteData[idx] = (byte) (rgb[0]);
                        byteData[idx + 1] = (byte) (rgb[1]);
                        byteData[idx + 2] = (byte) (rgb[2]);
                        idx += 3;
                    }
                    dstOffset += scanlineStride;
                }
            } else if (shortData != null) {
                if (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER) {
                    for (int j = 0; j < dstHeight; j++) {
                        int idx = dstOffset;
                        for (int i = 0; i < dstWidth; i++) {
                            float x0 = (float) shortData[idx];
                            float x1 = (float) shortData[idx + 1];
                            float x2 = (float) shortData[idx + 2];
                            colorConverter.toRGB(x0, x1, x2, rgb);
                            shortData[idx] = (short) (rgb[0]);
                            shortData[idx + 1] = (short) (rgb[1]);
                            shortData[idx + 2] = (short) (rgb[2]);
                            idx += 3;
                        }
                        dstOffset += scanlineStride;
                    }
                } else {
                    for (int j = 0; j < dstHeight; j++) {
                        int idx = dstOffset;
                        for (int i = 0; i < dstWidth; i++) {
                            float x0 = (float) (shortData[idx] & 0xffff);
                            float x1 = (float) (shortData[idx + 1] & 0xffff);
                            float x2 = (float) (shortData[idx + 2] & 0xffff);
                            colorConverter.toRGB(x0, x1, x2, rgb);
                            shortData[idx] = (short) (rgb[0]);
                            shortData[idx + 1] = (short) (rgb[1]);
                            shortData[idx + 2] = (short) (rgb[2]);
                            idx += 3;
                        }
                        dstOffset += scanlineStride;
                    }
                }
            } else if (intData != null) {
                for (int j = 0; j < dstHeight; j++) {
                    int idx = dstOffset;
                    for (int i = 0; i < dstWidth; i++) {
                        float x0 = (float) intData[idx];
                        float x1 = (float) intData[idx + 1];
                        float x2 = (float) intData[idx + 2];
                        colorConverter.toRGB(x0, x1, x2, rgb);
                        intData[idx] = (int) (rgb[0]);
                        intData[idx + 1] = (int) (rgb[1]);
                        intData[idx + 2] = (int) (rgb[2]);
                        idx += 3;
                    }
                    dstOffset += scanlineStride;
                }
            } else if (floatData != null) {
                for (int j = 0; j < dstHeight; j++) {
                    int idx = dstOffset;
                    for (int i = 0; i < dstWidth; i++) {
                        float x0 = floatData[idx];
                        float x1 = floatData[idx + 1];
                        float x2 = floatData[idx + 2];
                        colorConverter.toRGB(x0, x1, x2, rgb);
                        floatData[idx] = rgb[0];
                        floatData[idx + 1] = rgb[1];
                        floatData[idx + 2] = rgb[2];
                        idx += 3;
                    }
                    dstOffset += scanlineStride;
                }
            }
        // int[] p = new int[3];
        // ras.getPixel(0, 0, p);
        // System.out.println("p00 = " +
        // p[0] + " " + p[1] + " " + p[2]);
        // ras.getPixel(1, 0, p);
        // System.out.println("p10 = " +
        // p[0] + " " + p[1] + " " + p[2]);
        // ras.getPixel(2, 0, p);
        // System.out.println("p20 = " +
        // p[0] + " " + p[1] + " " + p[2]);
        // ras.getPixel(3, 0, p);
        // System.out.println("p30 = " +
        // p[0] + " " + p[1] + " " + p[2]);
        // ColorSpace rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        // ColorConvertOp op = new ColorConvertOp(colorSpace, rgb, null);
        // WritableRaster dest = op.createCompatibleDestRaster(ras);
        // op.filter(ras, dest);
        // ras = dest;
        }
        if (photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO) {
            if (byteData != null) {
                int bytesPerRow = (srcWidth * pixelBitStride + 7) / 8;
                for (int y = 0; y < srcHeight; y++) {
                    int offset = dstOffset + y * scanlineStride;
                    for (int i = 0; i < bytesPerRow; i++) {
                        byteData[offset + i] ^= 0xff;
                    }
                }
            } else if (shortData != null) {
                int shortsPerRow = (srcWidth * pixelBitStride + 15) / 16;
                if (sampleFormat[0] == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER) {
                    for (int y = 0; y < srcHeight; y++) {
                        int offset = dstOffset + y * scanlineStride;
                        for (int i = 0; i < shortsPerRow; i++) {
                            int shortOffset = offset + i;
                            // XXX Does this make any sense?
                            shortData[shortOffset] = (short) (Short.MAX_VALUE - shortData[shortOffset]);
                        }
                    }
                } else {
                    for (int y = 0; y < srcHeight; y++) {
                        int offset = dstOffset + y * scanlineStride;
                        for (int i = 0; i < shortsPerRow; i++) {
                            shortData[offset + i] ^= 0xffff;
                        }
                    }
                }
            } else if (intData != null) {
                int intsPerRow = (srcWidth * pixelBitStride + 15) / 16;
                for (int y = 0; y < srcHeight; y++) {
                    int offset = dstOffset + y * scanlineStride;
                    for (int i = 0; i < intsPerRow; i++) {
                        int intOffset = offset + i;
                        // XXX Does this make any sense?
                        intData[intOffset] = Integer.MAX_VALUE - intData[intOffset];
                    }
                }
            } else if (floatData != null) {
                int floatsPerRow = (srcWidth * pixelBitStride + 15) / 16;
                for (int y = 0; y < srcHeight; y++) {
                    int offset = dstOffset + y * scanlineStride;
                    for (int i = 0; i < floatsPerRow; i++) {
                        int floatOffset = offset + i;
                        // XXX Does this make any sense?
                        floatData[floatOffset] = 1.0F - floatData[floatOffset];
                    }
                }
            }
        }
        if (isBilevel) {
            Rectangle rect = isImageSimple ? new Rectangle(dstMinX, dstMinY, dstWidth, dstHeight) : ras.getBounds();
            ImageUtil.setPackedBinaryData(byteData, ras, rect);
        }
        // equals the raster of 'image' or is a child thereof.
        if (isDirectCopy) {
            // rawImage == image) {
            return;
        }
    }
    // Copy the raw image data into the true destination image
    Raster src = rawImage.getRaster();
    // Create band child of source
    Raster srcChild = src.createChild(0, 0, srcWidth, srcHeight, srcMinX, srcMinY, planar ? null : sourceBands);
    WritableRaster dst = image.getRaster();
    // Create dst child covering area and bands to be written
    WritableRaster dstChild = dst.createWritableChild(dstMinX, dstMinY, dstWidth, dstHeight, dstMinX, dstMinY, destinationBands);
    if (subsampleX == 1 && subsampleY == 1 && !adjustBitDepths) {
        srcChild = srcChild.createChild(activeSrcMinX, activeSrcMinY, activeSrcWidth, activeSrcHeight, dstMinX, dstMinY, null);
        dstChild.setRect(srcChild);
    } else if (subsampleX == 1 && !adjustBitDepths) {
        int sy = activeSrcMinY;
        int dy = dstMinY;
        while (sy < srcMinY + srcHeight) {
            Raster srcRow = srcChild.createChild(activeSrcMinX, sy, activeSrcWidth, 1, dstMinX, dy, null);
            dstChild.setRect(srcRow);
            sy += subsampleY;
            ++dy;
        }
    } else {
        // /init vars
        int numBands = srcChild.getNumBands();
        int sy = activeSrcMinY;
        int dy = dstMinY;
        // get the databuffer type
        final int type = srcChild.getDataBuffer().getDataType();
        switch(type) {
            case DataBuffer.TYPE_BYTE:
            case DataBuffer.TYPE_INT:
            case DataBuffer.TYPE_SHORT:
            case DataBuffer.TYPE_USHORT:
                int[] p = srcChild.getPixel(srcMinX, srcMinY, (int[]) null);
                while (sy < activeSrcMinY + activeSrcHeight) {
                    int sx = activeSrcMinX;
                    int dx = dstMinX;
                    while (sx < activeSrcMinX + activeSrcWidth) {
                        srcChild.getPixel(sx, sy, p);
                        if (adjustBitDepths) {
                            for (int band = 0; band < numBands; band++) {
                                p[band] = bitDepthScale[band][p[band]];
                            }
                        }
                        dstChild.setPixel(dx, dy, p);
                        sx += subsampleX;
                        ++dx;
                    }
                    sy += subsampleY;
                    ++dy;
                }
                break;
            case DataBuffer.TYPE_DOUBLE:
                double[] d = srcChild.getPixel(srcMinX, srcMinY, (double[]) null);
                while (sy < activeSrcMinY + activeSrcHeight) {
                    int sx = activeSrcMinX;
                    int dx = dstMinX;
                    while (sx < activeSrcMinX + activeSrcWidth) {
                        srcChild.getPixel(sx, sy, d);
                        // if (adjustBitDepths) {
                        // for (int band = 0; band < numBands; band++) {
                        // d[band] = bitDepthScale[band][d[band]];
                        // }
                        // }
                        dstChild.setPixel(dx, dy, d);
                        sx += subsampleX;
                        ++dx;
                    }
                    sy += subsampleY;
                    ++dy;
                }
                break;
            case DataBuffer.TYPE_FLOAT:
                float[] f = srcChild.getPixel(srcMinX, srcMinY, (float[]) null);
                while (sy < activeSrcMinY + activeSrcHeight) {
                    int sx = activeSrcMinX;
                    int dx = dstMinX;
                    while (sx < activeSrcMinX + activeSrcWidth) {
                        srcChild.getPixel(sx, sy, f);
                        // if (adjustBitDepths) {
                        // for (int band = 0; band < numBands; band++) {
                        // d[band] = bitDepthScale[band][d[band]];
                        // }
                        // }
                        dstChild.setPixel(dx, dy, f);
                        sx += subsampleX;
                        ++dx;
                    }
                    sy += subsampleY;
                    ++dy;
                }
                break;
            default:
                break;
        }
    }
}
Also used : DataBufferDouble(java.awt.image.DataBufferDouble) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Rectangle(java.awt.Rectangle) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) IIOException(javax.imageio.IIOException) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) WritableRaster(java.awt.image.WritableRaster) DataBufferFloat(java.awt.image.DataBufferFloat) DataBufferUShort(java.awt.image.DataBufferUShort) DataBuffer(java.awt.image.DataBuffer)

Aggregations

DataBufferShort (java.awt.image.DataBufferShort)17 DataBuffer (java.awt.image.DataBuffer)16 DataBufferByte (java.awt.image.DataBufferByte)16 DataBufferInt (java.awt.image.DataBufferInt)15 DataBufferUShort (java.awt.image.DataBufferUShort)10 SampleModel (java.awt.image.SampleModel)10 ComponentSampleModel (java.awt.image.ComponentSampleModel)8 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)7 BufferedImage (java.awt.image.BufferedImage)6 Point (java.awt.Point)5 DataBufferDouble (java.awt.image.DataBufferDouble)5 DataBufferFloat (java.awt.image.DataBufferFloat)5 BandedSampleModel (java.awt.image.BandedSampleModel)3 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)3 Raster (java.awt.image.Raster)3 WritableRaster (java.awt.image.WritableRaster)3 ByteBuffer (java.nio.ByteBuffer)3 Graphics2D (java.awt.Graphics2D)2 Rectangle (java.awt.Rectangle)2