Java Code Examples for java.awt.image.DataBufferInt#getData()

The following examples show how to use java.awt.image.DataBufferInt#getData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShaderUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static final ByteBuffer getImageDataFromImage(BufferedImage bufferedImage) {
	WritableRaster wr;
	DataBuffer db;

	BufferedImage bi = new BufferedImage(128, 64, BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = bi.createGraphics();
	g.drawImage(bufferedImage, null, null);
	bufferedImage = bi;
	wr = bi.getRaster();
	db = wr.getDataBuffer();

	DataBufferInt dbi = (DataBufferInt) db;
	int[] data = dbi.getData();

	ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4);
	byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	byteBuffer.asIntBuffer().put(data);
	byteBuffer.flip();

	return byteBuffer;
}
 
Example 2
Source File: ShaderUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static final ByteBuffer getImageDataFromImage(BufferedImage bufferedImage) {
	WritableRaster wr;
	DataBuffer db;

	BufferedImage bi = new BufferedImage(128, 64, BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = bi.createGraphics();
	g.drawImage(bufferedImage, null, null);
	bufferedImage = bi;
	wr = bi.getRaster();
	db = wr.getDataBuffer();

	DataBufferInt dbi = (DataBufferInt) db;
	int[] data = dbi.getData();

	ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4);
	byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	byteBuffer.asIntBuffer().put(data);
	byteBuffer.flip();

	return byteBuffer;
}
 
Example 3
Source File: Screenshots.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void convertScreenShot2(IntBuffer bgraBuf, BufferedImage out){
        WritableRaster wr = out.getRaster();
        DataBufferInt db = (DataBufferInt) wr.getDataBuffer();
        
        int[] cpuArray = db.getData();
        
        bgraBuf.clear();
        bgraBuf.get(cpuArray);
        
//        int width  = wr.getWidth();
//        int height = wr.getHeight();
//
//        // flip the components the way AWT likes them
//        for (int y = 0; y < height / 2; y++){
//            for (int x = 0; x < width; x++){
//                int inPtr  = (y * width + x);
//                int outPtr = ((height-y-1) * width + x);
//                int pixel = cpuArray[inPtr];
//                cpuArray[inPtr] = cpuArray[outPtr];
//                cpuArray[outPtr] = pixel;
//            }
//        }
    }
 
Example 4
Source File: MultipleGradientPaintContext.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 5
Source File: MultipleGradientPaintContext.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 6
Source File: ImageUtilTest.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Compares whether two {@link BufferedImage}s are equal in data.
 *
 * @param expected The first {@link BufferedImage} to be compared.
 * @param actual   The second {@link BufferedImage} to be compared.
 * @return         A boolean indicating whether the given {@link BufferedImage}s are of the same image data.
 */
private boolean bufferedImagesEqual(final @Nonnull BufferedImage expected, final @Nonnull BufferedImage actual)
{
	if (expected.getWidth() != actual.getWidth())
	{
		return false;
	}

	if (!expected.getColorModel().equals(actual.getColorModel()))
	{
		return false;
	}

	final DataBuffer aBuffer = expected.getRaster().getDataBuffer();
	final DataBuffer bBuffer = actual.getRaster().getDataBuffer();
	final DataBufferInt aBufferInt = (DataBufferInt) aBuffer;
	final DataBufferInt bBufferInt = (DataBufferInt) bBuffer;

	if (aBufferInt.getNumBanks() != bBufferInt.getNumBanks())
	{
		return false;
	}

	for (int i = 0; i < aBufferInt.getNumBanks(); i++)
	{
		final int[] aDataBank = aBufferInt.getData(i);
		final int[] bDataBank = bBufferInt.getData(i);
		if (!Arrays.equals(aDataBank, bDataBank))
		{
			return false;
		}
	}

	return true;
}
 
Example 7
Source File: ImageInfoEditor.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private static BufferedImage createAlphaBackground(int width, int height) {
    BufferedImage background = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    DataBufferInt dataBuffer = (DataBufferInt) background.getRaster().getDataBuffer();
    int[] data = dataBuffer.getData();
    int gray = Color.LIGHT_GRAY.getRGB();
    int white = Color.WHITE.getRGB();
    for (int i = 0; i < data.length; i++) {
        int x = i % width;
        int y = i / width;
        data[i] = ((x / 4) % 2 == (y / 4) % 2) ? gray : white;
    }
    return background;
}
 
Example 8
Source File: RawRect.java    From cosmic with Apache License 2.0 5 votes vote down vote up
@Override
public void paint(final BufferedImage image, final Graphics2D graphics) {

    final DataBuffer dataBuf = image.getRaster().getDataBuffer();

    switch (dataBuf.getDataType()) {

        case DataBuffer.TYPE_INT: {
            // We chose RGB888 model, so Raster will use DataBufferInt type
            final DataBufferInt dataBuffer = (DataBufferInt) dataBuf;

            final int imageWidth = image.getWidth();
            final int imageHeight = image.getHeight();

            // Paint rectangle directly on buffer, line by line
            final int[] imageBuffer = dataBuffer.getData();
            for (int srcLine = 0, dstLine = y; srcLine < height && dstLine < imageHeight; srcLine++, dstLine++) {
                try {
                    System.arraycopy(buf, srcLine * width, imageBuffer, x + dstLine * imageWidth, width);
                } catch (final IndexOutOfBoundsException e) {
                    s_logger.info("[ignored] buffer overflow!?!", e);
                }
            }
            break;
        }

        default:
            throw new RuntimeException("Unsupported data buffer in buffered image: expected data buffer of type int (DataBufferInt). Actual data buffer type: " +
                    dataBuf.getClass().getSimpleName());
    }
}
 
Example 9
Source File: RawRect.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public void paint(BufferedImage image, Graphics2D graphics) {

    DataBuffer dataBuf = image.getRaster().getDataBuffer();

    switch (dataBuf.getDataType()) {

    case DataBuffer.TYPE_INT: {
        // We chose RGB888 model, so Raster will use DataBufferInt type
        DataBufferInt dataBuffer = (DataBufferInt)dataBuf;

        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        // Paint rectangle directly on buffer, line by line
        int[] imageBuffer = dataBuffer.getData();
        for (int srcLine = 0, dstLine = y; srcLine < height && dstLine < imageHeight; srcLine++, dstLine++) {
            try {
                System.arraycopy(buf, srcLine * width, imageBuffer, x + dstLine * imageWidth, width);
            } catch (IndexOutOfBoundsException e) {
                s_logger.info("[ignored] buffer overflow!?!", e);
            }
        }
        break;
    }

    default:
        throw new RuntimeException("Unsupported data buffer in buffered image: expected data buffer of type int (DataBufferInt). Actual data buffer type: " +
                dataBuf.getClass().getSimpleName());
    }
}
 
Example 10
Source File: MultipleGradientPaintContext.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 11
Source File: MultipleGradientPaintContext.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 12
Source File: MultipleGradientPaintContext.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 13
Source File: MultipleGradientPaintContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 14
Source File: MenuMisc.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
public static void WritePNGfile(String imagename, int[] linear, int width, int height) {
    BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    DataBufferInt sh = (DataBufferInt) buf.getRaster().getDataBuffer();
    int[] shd = sh.getData();
    System.arraycopy(linear, 0, shd, 0, Math.min(linear.length, shd.length));
    try {
        ImageIO.write(buf, "PNG", new File(imagename));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: MultipleGradientPaintContext.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 16
Source File: MultipleGradientPaintContext.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 17
Source File: MultipleGradientPaintContext.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 18
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private static int[] getData(BufferedImage image) {
  WritableRaster wr = image.getRaster();
  DataBufferInt dbi = (DataBufferInt) wr.getDataBuffer();
  return dbi.getData();
  // return ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
}
 
Example 19
Source File: PngWriter.java    From scrimage with Apache License 2.0 4 votes vote down vote up
@Override
public void write(AwtImage image, ImageMetadata metadata, OutputStream out) throws IOException {

    if (image.awt().getType() == BufferedImage.TYPE_INT_ARGB) {

        ImageInfo imi = new ImageInfo(image.width, image.height, 8, true);

        ar.com.hjg.pngj.PngWriter writer = new ar.com.hjg.pngj.PngWriter(out, imi);
        writer.setCompLevel(compressionLevel);
        writer.setFilterType(FilterType.FILTER_DEFAULT);

        DataBufferInt db = (DataBufferInt) image.awt().getRaster().getDataBuffer();
        if (db.getNumBanks() != 1) throw new RuntimeException("This method expects one bank");

        SinglePixelPackedSampleModel samplemodel = (SinglePixelPackedSampleModel) image.awt().getSampleModel();
        ImageLineInt line = new ImageLineInt(imi);
        int[] dbbuf = db.getData();

        for (int row = 0; row < imi.rows; row++) {
            int elem = samplemodel.getOffset(0, row);
            int j = 0;
            for (int col = 0; col < imi.cols; col++) {
                int sample = dbbuf[elem];
                elem = elem + 1;
                line.getScanline()[j] = (sample & 0xFF0000) >> 16; // R
                j = j + 1;
                line.getScanline()[j] = (sample & 0xFF00) >> 8; // G
                j = j + 1;
                line.getScanline()[j] = sample & 0xFF; // B
                j = j + 1;
                line.getScanline()[j] = ((sample & 0xFF000000) >> 24) & 0xFF; // A
                j = j + 1;
            }
            writer.writeRow(line, row);
        }
        writer.end();// end calls close

    } else {
        ImageIO.write(image.awt(), "png", out);
    }
}
 
Example 20
Source File: ImageUtils.java    From RemoteSupportTool with Apache License 2.0 4 votes vote down vote up
public static boolean equals(BufferedImage image1, BufferedImage image2) {
    final int image1Width = image1.getWidth();
    final int image1Height = image1.getHeight();

    final int image2Width = image2.getWidth();
    final int image2Height = image2.getHeight();

    if (image1Width != image2Width || image1Height != image2Height)
        return false;

    final DataBuffer image1Buffer = image1.getData().getDataBuffer();
    final DataBuffer image2Buffer = image2.getData().getDataBuffer();

    final int image1BufferSize = image1Buffer.getSize();
    final int image2BufferSize = image2Buffer.getSize();
    if (image1BufferSize != image2BufferSize)
        return false;

    if (image1Buffer instanceof DataBufferInt && image2Buffer instanceof DataBufferInt) {
        // compare according to https://stackoverflow.com/a/11006984
        final DataBufferInt image1BufferInt = (DataBufferInt) image1Buffer;
        final DataBufferInt image2BufferInt = (DataBufferInt) image2Buffer;
        if (image1BufferInt.getNumBanks() != image2BufferInt.getNumBanks())
            return false;

        for (int bank = 0; bank < image1BufferInt.getNumBanks(); bank++) {
            int[] actual = image1BufferInt.getData(bank);
            int[] expected = image2BufferInt.getData(bank);
            if (!Arrays.equals(actual, expected))
                return false;
        }
    } else {
        // compare according to https://stackoverflow.com/a/51497360
        for (int i = 0; i < image1BufferSize; i++) {
            if (image1Buffer.getElem(i) != image2Buffer.getElem(i)) {
                return false;
            }
        }
    }

    //for (int x = 0; x < image1Width; x++) {
    //    for (int y = 0; y < image1Height; y++) {
    //        if (image1.getRGB(x, y) != image2.getRGB(x, y)) {
    //            return false;
    //        }
    //    }
    //}

    return true;
}