Example usage for java.awt.image DataBuffer getDataType

List of usage examples for java.awt.image DataBuffer getDataType

Introduction

In this page you can find the example usage for java.awt.image DataBuffer getDataType.

Prototype

public int getDataType() 

Source Link

Document

Returns the data type of this DataBuffer.

Usage

From source file:GraphicsUtil.java

/**
 * Creates a new raster that has a <b>copy</b> of the data in
 * <tt>ras</tt>.  This is highly optimized for speed.  There is
 * no provision for changing any aspect of the SampleModel.
 * However you can specify a new location for the returned raster.
 *
 * This method should be used when you need to change the contents
 * of a Raster that you do not "own" (ie the result of a
 * <tt>getData</tt> call)./*w  w w .j av a2s.c o  m*/
 *
 * @param ras The Raster to copy.
 *
 * @param minX The x location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @param minY The y location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @return    A writable copy of <tt>ras</tt>
 */
public static WritableRaster copyRaster(Raster ras, int minX, int minY) {
    WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), new Point(0, 0));
    ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(),
            ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null);

    // Use System.arraycopy to copy the data between the two...
    DataBuffer srcDB = ras.getDataBuffer();
    DataBuffer retDB = ret.getDataBuffer();
    if (srcDB.getDataType() != retDB.getDataType()) {
        throw new IllegalArgumentException("New DataBuffer doesn't match original");
    }
    int len = srcDB.getSize();
    int banks = srcDB.getNumBanks();
    int[] offsets = srcDB.getOffsets();
    for (int b = 0; b < banks; b++) {
        switch (srcDB.getDataType()) {
        case DataBuffer.TYPE_BYTE: {
            DataBufferByte srcDBT = (DataBufferByte) srcDB;
            DataBufferByte retDBT = (DataBufferByte) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_INT: {
            DataBufferInt srcDBT = (DataBufferInt) srcDB;
            DataBufferInt retDBT = (DataBufferInt) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_SHORT: {
            DataBufferShort srcDBT = (DataBufferShort) srcDB;
            DataBufferShort retDBT = (DataBufferShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_USHORT: {
            DataBufferUShort srcDBT = (DataBufferUShort) srcDB;
            DataBufferUShort retDBT = (DataBufferUShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        }
    }

    return ret;
}

From source file:GraphicsUtil.java

/**
 * Creates a new raster that has a <b>copy</b> of the data in
 * <tt>ras</tt>.  This is highly optimized for speed.  There is
 * no provision for changing any aspect of the SampleModel.
 * However you can specify a new location for the returned raster.
 *
 * This method should be used when you need to change the contents
 * of a Raster that you do not "own" (ie the result of a
 * <tt>getData</tt> call).//from  w  w w.  ja va  2  s  . c  o  m
 *
 * @param ras The Raster to copy.
 *
 * @param minX The x location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @param minY The y location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @return    A writable copy of <tt>ras</tt>
 */
public static WritableRaster copyRaster(Raster ras, int minX, int minY) {
    WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), new Point(0, 0));
    ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(),
            ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null);

    // Use System.arraycopy to copy the data between the two...
    DataBuffer srcDB = ras.getDataBuffer();
    DataBuffer retDB = ret.getDataBuffer();
    if (srcDB.getDataType() != retDB.getDataType()) {
        throw new IllegalArgumentException("New DataBuffer doesn't match original");
    }
    int len = srcDB.getSize();
    int banks = srcDB.getNumBanks();
    int[] offsets = srcDB.getOffsets();
    for (int b = 0; b < banks; b++) {
        switch (srcDB.getDataType()) {
        case DataBuffer.TYPE_BYTE: {
            DataBufferByte srcDBT = (DataBufferByte) srcDB;
            DataBufferByte retDBT = (DataBufferByte) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_INT: {
            DataBufferInt srcDBT = (DataBufferInt) srcDB;
            DataBufferInt retDBT = (DataBufferInt) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_SHORT: {
            DataBufferShort srcDBT = (DataBufferShort) srcDB;
            DataBufferShort retDBT = (DataBufferShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_USHORT: {
            DataBufferUShort srcDBT = (DataBufferUShort) srcDB;
            DataBufferUShort retDBT = (DataBufferUShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        }
    }

    return ret;
}

From source file:nitf.imagej.NITF_Reader.java

private static Object getPixelsFromBufferedImage(BufferedImage bufImage) {
    DataBuffer dataBuffer = bufImage.getData().getDataBuffer();
    if (dataBuffer.getDataType() == DataBuffer.TYPE_BYTE)
        return ((DataBufferByte) dataBuffer).getData();
    else if (dataBuffer.getDataType() == DataBuffer.TYPE_USHORT)
        return ((DataBufferUShort) dataBuffer).getData();
    return null;// w  ww. java2s .  c  o m
}

From source file:org.apache.pdfbox.filter.JBIG2Filter.java

/**
 * Decode JBIG2 data using Java ImageIO library.
 *
 * {@inheritDoc}//from  ww  w . ja  va  2 s .  c  om
 *
 */
public void decode(InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex)
        throws IOException {
    BufferedImage bi = ImageIO.read(compressedData);
    if (bi != null) {
        DataBuffer dBuf = bi.getData().getDataBuffer();
        if (dBuf.getDataType() == DataBuffer.TYPE_BYTE) {
            result.write(((DataBufferByte) dBuf).getData());
        } else {
            log.error("Image data buffer not of type byte but type " + dBuf.getDataType());
        }
    } else {
        Iterator<ImageReader> reader = ImageIO.getImageReadersByFormatName("JBIG2");
        if (!reader.hasNext()) {
            log.error("Can't find an ImageIO plugin to decode the JBIG2 encoded datastream.");
        } else {
            log.error("Something went wrong when decoding the JBIG2 encoded datastream.");
        }
    }
}

From source file:org.apache.pdfbox.filter.JPXFilter.java

/**
 * Decode JPEG2000 data using Java ImageIO library.
 *
 * {@inheritDoc}//  w ww. j a  v  a 2s .  c  o m
 *
 */
public void decode(InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex)
        throws IOException {
    BufferedImage bi = ImageIO.read(compressedData);
    if (bi != null) {
        DataBuffer dBuf = bi.getData().getDataBuffer();
        if (dBuf.getDataType() == DataBuffer.TYPE_BYTE) {
            result.write(((DataBufferByte) dBuf).getData());
        } else {
            log.error("Image data buffer not of type byte but type " + dBuf.getDataType());
        }
    }
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java

/**
 * Reads in an image of a given size and returns it as a WritableRaster.
 *//*from   w ww .j av  a2 s  .c  o m*/
private void decodePass(final WritableRaster imRas, final int xOffset, final int yOffset, final int xStep,
        final int yStep, final int passWidth, final int passHeight) {
    if (passWidth == 0 || passHeight == 0) {
        return;
    }

    final int bytesPerRow = (this.inputBands * passWidth * this.bitDepth + 7) / 8;
    final int eltsPerRow = this.bitDepth == 16 ? bytesPerRow / 2 : bytesPerRow;
    byte[] curr = new byte[bytesPerRow];
    byte[] prior = new byte[bytesPerRow];

    // Create a 1-row tall Raster to hold the data
    final WritableRaster passRow = createRaster(passWidth, 1, this.inputBands, eltsPerRow, this.bitDepth);
    final DataBuffer dataBuffer = passRow.getDataBuffer();
    final int type = dataBuffer.getDataType();
    byte[] byteData = null;
    short[] shortData = null;
    if (type == DataBuffer.TYPE_BYTE) {
        byteData = ((DataBufferByte) dataBuffer).getData();
    } else {
        shortData = ((DataBufferUShort) dataBuffer).getData();
    }

    // Decode the (sub)image row-by-row
    int srcY, dstY;
    for (srcY = 0, dstY = yOffset; srcY < passHeight; srcY++, dstY += yStep) {
        // Read the filter type byte and a row of data
        int filter = 0;
        try {
            filter = this.dataStream.read();
            this.dataStream.readFully(curr, 0, bytesPerRow);
        } catch (final Exception e) {
            log.error("Exception", e);
        }

        switch (filter) {
        case PNG_FILTER_NONE:
            break;
        case PNG_FILTER_SUB:
            decodeSubFilter(curr, bytesPerRow, this.bytesPerPixel);
            break;
        case PNG_FILTER_UP:
            decodeUpFilter(curr, prior, bytesPerRow);
            break;
        case PNG_FILTER_AVERAGE:
            decodeAverageFilter(curr, prior, bytesPerRow, this.bytesPerPixel);
            break;
        case PNG_FILTER_PAETH:
            decodePaethFilter(curr, prior, bytesPerRow, this.bytesPerPixel);
            break;
        default:
            // Error -- uknown filter type
            final String msg = PropertyUtil.getString("PNGImageDecoder16");
            throw new RuntimeException(msg);
        }

        // Copy data into passRow byte by byte
        if (this.bitDepth < 16) {
            System.arraycopy(curr, 0, byteData, 0, bytesPerRow);
        } else {
            int idx = 0;
            for (int j = 0; j < eltsPerRow; j++) {
                shortData[j] = (short) (curr[idx] << 8 | curr[idx + 1] & 0xff);
                idx += 2;
            }
        }

        processPixels(this.postProcess, passRow, imRas, xOffset, xStep, dstY, passWidth);

        // Swap curr and prior
        final byte[] tmp = prior;
        prior = curr;
        curr = tmp;
    }
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGRed.java

/**
 * Reads in an image of a given size and returns it as a WritableRaster.
 *//*  www  .j  ava  2 s  . com*/
private void decodePass(final WritableRaster imRas, final int xOffset, final int yOffset, final int xStep,
        final int yStep, final int passWidth, final int passHeight) {
    if (passWidth == 0 || passHeight == 0) {
        return;
    }

    final int bytesPerRow = (this.inputBands * passWidth * this.bitDepth + 7) / 8;
    final int eltsPerRow = this.bitDepth == 16 ? bytesPerRow / 2 : bytesPerRow;
    byte[] curr = new byte[bytesPerRow];
    byte[] prior = new byte[bytesPerRow];

    // Create a 1-row tall Raster to hold the data
    final WritableRaster passRow = createRaster(passWidth, 1, this.inputBands, eltsPerRow, this.bitDepth);
    final DataBuffer dataBuffer = passRow.getDataBuffer();
    final int type = dataBuffer.getDataType();
    byte[] byteData = null;
    short[] shortData = null;
    if (type == DataBuffer.TYPE_BYTE) {
        byteData = ((DataBufferByte) dataBuffer).getData();
    } else {
        shortData = ((DataBufferUShort) dataBuffer).getData();
    }

    // Decode the (sub)image row-by-row
    int srcY, dstY;
    for (srcY = 0, dstY = yOffset; srcY < passHeight; srcY++, dstY += yStep) {
        // Read the filter type byte and a row of data
        int filter = 0;
        try {
            filter = this.dataStream.read();
            this.dataStream.readFully(curr, 0, bytesPerRow);
        } catch (final Exception e) {
            log.error("Exception", e);
        }

        switch (filter) {
        case PNG_FILTER_NONE:
            break;
        case PNG_FILTER_SUB:
            decodeSubFilter(curr, bytesPerRow, this.bytesPerPixel);
            break;
        case PNG_FILTER_UP:
            decodeUpFilter(curr, prior, bytesPerRow);
            break;
        case PNG_FILTER_AVERAGE:
            decodeAverageFilter(curr, prior, bytesPerRow, this.bytesPerPixel);
            break;
        case PNG_FILTER_PAETH:
            decodePaethFilter(curr, prior, bytesPerRow, this.bytesPerPixel);
            break;
        default:
            // Error -- unknown filter type
            final String msg = PropertyUtil.getString("PNGImageDecoder16");
            throw new RuntimeException(msg);
        }

        // Copy data into passRow byte by byte
        if (this.bitDepth < 16) {
            System.arraycopy(curr, 0, byteData, 0, bytesPerRow);
        } else {
            int idx = 0;
            for (int j = 0; j < eltsPerRow; j++) {
                shortData[j] = (short) (curr[idx] << 8 | curr[idx + 1] & 0xff);
                idx += 2;
            }
        }

        processPixels(this.postProcess, passRow, imRas, xOffset, xStep, dstY, passWidth);

        // Swap curr and prior
        final byte[] tmp = prior;
        prior = curr;
        curr = tmp;
    }
}