Example usage for java.awt.image DataBuffer getSize

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

Introduction

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

Prototype

public int getSize() 

Source Link

Document

Returns the size (in array elements) of all banks.

Usage

From source file:fr.ens.transcriptome.corsen.gui.qt.ResultGraphs.java

private static final byte[] toByte(DataBuffer buffer) {

    if (buffer == null)
        return null;

    final int size = buffer.getSize();

    byte[] result = new byte[size * 4];

    for (int i = 0; i < size; i++) {

        final int val = buffer.getElem(i);
        final int j = i * 4;

        result[j + 3] = (byte) (((val & 0xFF000000) >> 24) & 0xFF);
        result[j + 2] = (byte) (((val & 0x00FF0000) >> 16) & 0xFF);
        result[j + 1] = (byte) (((val & 0x0000FF00) >> 8) & 0xFF);
        result[j] = (byte) (val & 0xFF);
    }/*w  w  w  . j  av a2 s  .c  om*/

    return result;
}

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  www. ja  v a  2 s  .com
 *
 * @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 ww w.j ava 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:RasterDemo.java

public void flipBufferedImage() {
    bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), bi1.getType());

    DataBuffer db1 = bi1.getRaster().getDataBuffer();
    DataBuffer db2 = bi2.getRaster().getDataBuffer();

    for (int i = db1.getSize() - 1, j = 0; i >= 0; --i, j++) {
        db2.setElem(j, db1.getElem(i));/*from  w ww.j ava  2 s  .c  o  m*/
    }
}

From source file:com.sat.dbds.vcs.login.LoginStepDef.java

/**
 * This method will try to match images and conclude if
 * the are identical.//from   ww w  .j a v a2  s  . co m
 *
 * @param sourceImage the source image
 * @param targetImage the target image
 * @return true, if successful
 * @throws IOException Signals that an I/O exception has occurred.
 */
boolean doImagesMatch(String sourceImage, String targetImage) throws IOException {

    LogHandler.info("Source image is:" + sourceImage);
    LogHandler.info("Target image is:" + targetImage);

    File fileInput = new File(sourceImage);
    File fileOutPut = new File(targetImage);

    BufferedImage bufFileInput = ImageIO.read(fileInput);
    DataBuffer dataFileInput = bufFileInput.getData().getDataBuffer();
    int sizeFileInput = dataFileInput.getSize();

    BufferedImage bufFileOutPut = ImageIO.read(fileOutPut);
    DataBuffer dataFileOutPut = bufFileOutPut.getData().getDataBuffer();
    int sizeFileOutPut = dataFileOutPut.getSize();

    boolean matchFlag = true;
    if (sizeFileInput == sizeFileOutPut) {
        for (int j = 0; j < sizeFileInput; j++) {
            if (dataFileInput.getElem(j) != dataFileOutPut.getElem(j)) {
                matchFlag = false;
                break;
            }
        }
    } else
        matchFlag = false;

    return matchFlag;
}

From source file:org.esa.s2tbx.dataio.jp2.internal.JP2TileOpImage.java

private void computeRectIndirect(WritableRaster dest, Rectangle destRect) {
    try {/* www . j  a  v  a 2  s  .  co m*/
        Path tile = decompressTile(tileIndex, getLevel());
        RenderedImage readTileImage = null;
        if (tile != null) {
            try (ImageReader imageReader = new ImageReader(tile)) {
                final DataBuffer dataBuffer = dest.getDataBuffer();
                int tileWidth = this.getTileWidth();
                int tileHeight = this.getTileHeight();
                final int fileTileX = destRect.x / tileLayout.tileWidth;
                final int fileTileY = destRect.y / tileLayout.tileHeight;
                int fileTileOriginX = destRect.x - fileTileX * tileLayout.tileWidth;
                int fileTileOriginY = destRect.y - fileTileY * tileLayout.tileHeight;
                Rectangle fileTileRect = tileDims.get(tile);
                if (fileTileRect == null) {
                    fileTileRect = new Rectangle(0, 0, imageReader.getImageWidth(),
                            imageReader.getImageHeight());
                    tileDims.put(tile, fileTileRect);
                }
                if (fileTileOriginX == 0 && tileLayout.tileWidth == tileWidth && fileTileOriginY == 0
                        && tileLayout.tileHeight == tileHeight
                        && tileWidth * tileHeight == dataBuffer.getSize()) {
                    readTileImage = imageReader.read();
                } else {
                    final Rectangle tileRect = new Rectangle(fileTileOriginX, fileTileOriginY, tileWidth,
                            tileHeight);
                    final Rectangle intersection = fileTileRect.intersection(tileRect);
                    if (!intersection.isEmpty()) {
                        readTileImage = imageReader.read(intersection);
                    }
                }
                if (readTileImage != null) {
                    Raster readBandRaster = readTileImage.getData().createChild(0, 0, readTileImage.getWidth(),
                            readTileImage.getHeight(), 0, 0, new int[] { bandIndex });
                    dest.setDataElements(dest.getMinX(), dest.getMinY(), readBandRaster);
                }
            } catch (IOException e) {
                logger.severe(e.getMessage());
            }
        }
    } catch (IOException e) {
        logger.severe(e.getMessage());
    }
}

From source file:org.esa.s2tbx.dataio.jp2.internal.JP2TileOpImage.java

private void computeRectDirect(WritableRaster dest, Rectangle destRect) {
    try (OpenJP2Decoder decoder = new OpenJP2Decoder(this.cacheDir, this.imageFile, this.bandIndex,
            this.dataType, getLevel(), 20, tileIndex)) {
        Raster readTileImage = null;
        final DataBuffer dataBuffer = dest.getDataBuffer();
        int tileWidth = this.getTileWidth();
        int tileHeight = this.getTileHeight();
        final int fileTileX = destRect.x / tileLayout.tileWidth;
        final int fileTileY = destRect.y / tileLayout.tileHeight;
        int fileTileOriginX = destRect.x - fileTileX * tileLayout.tileWidth;
        int fileTileOriginY = destRect.y - fileTileY * tileLayout.tileHeight;
        Dimension dimensions = decoder.getImageDimensions();
        Rectangle fileTileRect = new Rectangle(0, 0, dimensions.width, dimensions.height);

        if (fileTileOriginX == 0 && tileLayout.tileWidth == tileWidth && fileTileOriginY == 0
                && tileLayout.tileHeight == tileHeight && tileWidth * tileHeight == dataBuffer.getSize()) {
            readTileImage = decoder.read(null);
        } else {/*from   w w  w .  java  2  s .  co m*/
            final Rectangle tileRect = new Rectangle(fileTileOriginX, fileTileOriginY, tileWidth, tileHeight);
            final Rectangle intersection = fileTileRect.intersection(tileRect);
            if (!intersection.isEmpty()) {
                readTileImage = decoder.read(intersection);
            }
        }
        if (readTileImage != null) {
            Raster readBandRaster = readTileImage.createChild(0, 0, readTileImage.getWidth(),
                    readTileImage.getHeight(), 0, 0, bands);
            dest.setDataElements(dest.getMinX(), dest.getMinY(), readBandRaster);
        }

    } catch (IOException e) {
        logger.severe(e.getMessage());
    }
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

private static boolean isNotBlank(BufferedImage image) {
    // yes, i know this is bonkers. yes, the next step is to try sampling
    // to avoid iterating over each pixel.
    // tests on my linux machine typically find a non-blank pixel within the
    // first 100 iterations, and fewer than 5 retries to get a non-blank
    // *image* (typically 1-2 retries though)
    DataBuffer buf = image.getRaster().getDataBuffer();
    ColorModel model = image.getColorModel();
    boolean result = false;
    int i;/*from   w w w  . ja v  a  2  s  .  c  om*/
    for (i = 0; i < buf.getSize(); i++) {
        // it's apparently not sufficient to simply grab the value directly;
        // Linux seems to store the "blank" value as -16777216 (-2^24, which
        // makes a lot of sense for images), while on OS X the blank value
        // is simply 0. i suspect the getRGB stuff is a candidate for
        // inlining by the JIT, but profiling is needed.
        int rgb = model.getRGB(buf.getElem(i));
        if (rgb != -16777216) {
            logger.trace("found non-blank value '{}' at index {}", rgb, i);
            result = true;
            break;
        }
    }
    logger.trace("{} iterations to return {}", i, result);
    return result;
}