Search in sources :

Example 11 with DataBuffer

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

the class GetDataElementsTest method main.

public static void main(String[] args) {
    SampleModel sm = new ComponentSampleModel(dataType, width, height, 4, width * 4, new int[] { 0, 1, 2, 3 });
    DataBuffer db = sm.createDataBuffer();
    Object o = null;
    boolean testPassed = false;
    try {
        o = sm.getDataElements(Integer.MAX_VALUE, 0, 1, 1, o, db);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
        testPassed = true;
    }
    if (!testPassed) {
        throw new RuntimeException("Excpected excprion was not thrown.");
    }
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBuffer(java.awt.image.DataBuffer)

Example 12 with DataBuffer

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

the class IncorrectUnmanagedImageSourceOffset method makeUnmanagedBI.

private static BufferedImage makeUnmanagedBI(final int type) {
    final BufferedImage bi = new BufferedImage(511, 255, type);
    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 13 with DataBuffer

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

the class GetSamplesTest method doTest.

private static void doTest(Class<? extends SampleModel> c) {
    System.out.println("Test for: " + c.getName());
    SampleModel sm = createSampleModel(c);
    DataBuffer db = sm.createDataBuffer();
    int[] iArray = new int[width * height + numBands];
    float[] fArray = new float[width * height + numBands];
    double[] dArray = new double[width * height + numBands];
    boolean iOk = false;
    boolean fOk = false;
    boolean dOk = false;
    try {
        sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
        sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, iArray, db);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
        iOk = true;
    }
    try {
        sm.getSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
        sm.setSamples(Integer.MAX_VALUE, 0, 1, 1, 0, fArray, db);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
        fOk = true;
    }
    try {
        sm.getSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
        sm.setSamples(0, Integer.MAX_VALUE, 1, 1, 0, dArray, db);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
        dOk = true;
    }
    if (!iOk || !fOk || !dOk) {
        throw new RuntimeException("Test for " + c.getSimpleName() + " failed: iOk=" + iOk + "; fOk=" + fOk + "; dOk=" + dOk);
    }
}
Also used : BandedSampleModel(java.awt.image.BandedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) DataBuffer(java.awt.image.DataBuffer)

Example 14 with DataBuffer

use of java.awt.image.DataBuffer in project jmonkeyengine by jMonkeyEngine.

the class ShaderUtils method getImageDataFromImage.

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;
}
Also used : WritableRaster(java.awt.image.WritableRaster) DataBufferInt(java.awt.image.DataBufferInt) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) DataBuffer(java.awt.image.DataBuffer) Graphics2D(java.awt.Graphics2D)

Example 15 with DataBuffer

use of java.awt.image.DataBuffer in project jna by java-native-access.

the class GDI32Util method getScreenshot.

/**
	 * Takes a screenshot of the given window
	 * 
	 * @param target
	 *            The window to target
	 * @return the window captured as a screenshot, or null if the BufferedImage doesn't construct properly
	 * @throws IllegalStateException
	 *             if the rectangle from GetWindowRect has a width and/or height
	 *             of 0. <br>
	 *             if the device context acquired from the original HWND doesn't
	 *             release properly
	 */
public static BufferedImage getScreenshot(HWND target) {
    RECT rect = new RECT();
    if (!User32.INSTANCE.GetWindowRect(target, rect)) {
        throw new Win32Exception(Native.getLastError());
    }
    Rectangle jRectangle = rect.toRectangle();
    int windowWidth = jRectangle.width;
    int windowHeight = jRectangle.height;
    if (windowWidth == 0 || windowHeight == 0) {
        throw new IllegalStateException("Window width and/or height were 0 even though GetWindowRect did not appear to fail.");
    }
    HDC hdcTarget = User32.INSTANCE.GetDC(target);
    if (hdcTarget == null) {
        throw new Win32Exception(Native.getLastError());
    }
    Win32Exception we = null;
    // device context used for drawing
    HDC hdcTargetMem = null;
    // handle to the bitmap to be drawn to
    HBITMAP hBitmap = null;
    // original display surface associated with the device context
    HANDLE hOriginal = null;
    // final java image structure we're returning.
    BufferedImage image = null;
    try {
        hdcTargetMem = GDI32.INSTANCE.CreateCompatibleDC(hdcTarget);
        if (hdcTargetMem == null) {
            throw new Win32Exception(Native.getLastError());
        }
        hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcTarget, windowWidth, windowHeight);
        if (hBitmap == null) {
            throw new Win32Exception(Native.getLastError());
        }
        hOriginal = GDI32.INSTANCE.SelectObject(hdcTargetMem, hBitmap);
        if (hOriginal == null) {
            throw new Win32Exception(Native.getLastError());
        }
        // draw to the bitmap
        if (!GDI32.INSTANCE.BitBlt(hdcTargetMem, 0, 0, windowWidth, windowHeight, hdcTarget, 0, 0, GDI32.SRCCOPY)) {
            throw new Win32Exception(Native.getLastError());
        }
        BITMAPINFO bmi = new BITMAPINFO();
        bmi.bmiHeader.biWidth = windowWidth;
        bmi.bmiHeader.biHeight = -windowHeight;
        bmi.bmiHeader.biPlanes = 1;
        bmi.bmiHeader.biBitCount = 32;
        bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
        Memory buffer = new Memory(windowWidth * windowHeight * 4);
        int resultOfDrawing = GDI32.INSTANCE.GetDIBits(hdcTarget, hBitmap, 0, windowHeight, buffer, bmi, WinGDI.DIB_RGB_COLORS);
        if (resultOfDrawing == 0 || resultOfDrawing == WinError.ERROR_INVALID_PARAMETER) {
            throw new Win32Exception(Native.getLastError());
        }
        int bufferSize = windowWidth * windowHeight;
        DataBuffer dataBuffer = new DataBufferInt(buffer.getIntArray(0, bufferSize), bufferSize);
        WritableRaster raster = Raster.createPackedRaster(dataBuffer, windowWidth, windowHeight, windowWidth, SCREENSHOT_BAND_MASKS, null);
        image = new BufferedImage(SCREENSHOT_COLOR_MODEL, raster, false, null);
    } catch (Win32Exception e) {
        we = e;
    } finally {
        if (hOriginal != null) {
            // per MSDN, set the display surface back when done drawing
            HANDLE result = GDI32.INSTANCE.SelectObject(hdcTargetMem, hOriginal);
            // failure modes are null or equal to HGDI_ERROR
            if (result == null || WinGDI.HGDI_ERROR.equals(result)) {
                Win32Exception ex = new Win32Exception(Native.getLastError());
                if (we != null) {
                    ex.addSuppressed(we);
                }
                we = ex;
            }
        }
        if (hBitmap != null) {
            if (!GDI32.INSTANCE.DeleteObject(hBitmap)) {
                Win32Exception ex = new Win32Exception(Native.getLastError());
                if (we != null) {
                    ex.addSuppressed(we);
                }
                we = ex;
            }
        }
        if (hdcTargetMem != null) {
            // get rid of the device context when done
            if (!GDI32.INSTANCE.DeleteDC(hdcTargetMem)) {
                Win32Exception ex = new Win32Exception(Native.getLastError());
                if (we != null) {
                    ex.addSuppressed(we);
                }
                we = ex;
            }
        }
        if (hdcTarget != null) {
            if (0 == User32.INSTANCE.ReleaseDC(target, hdcTarget)) {
                throw new IllegalStateException("Device context did not release properly.");
            }
        }
    }
    if (we != null) {
        throw we;
    }
    return image;
}
Also used : RECT(com.sun.jna.platform.win32.WinDef.RECT) HDC(com.sun.jna.platform.win32.WinDef.HDC) Memory(com.sun.jna.Memory) Rectangle(java.awt.Rectangle) DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage) Win32Exception(com.sun.jna.platform.win32.Win32Exception) WritableRaster(java.awt.image.WritableRaster) BITMAPINFO(com.sun.jna.platform.win32.WinGDI.BITMAPINFO) HBITMAP(com.sun.jna.platform.win32.WinDef.HBITMAP) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) DataBuffer(java.awt.image.DataBuffer)

Aggregations

DataBuffer (java.awt.image.DataBuffer)28 DataBufferInt (java.awt.image.DataBufferInt)16 SampleModel (java.awt.image.SampleModel)14 DataBufferByte (java.awt.image.DataBufferByte)13 BufferedImage (java.awt.image.BufferedImage)11 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)10 DataBufferShort (java.awt.image.DataBufferShort)9 Point (java.awt.Point)8 WritableRaster (java.awt.image.WritableRaster)8 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 DataBufferUShort (java.awt.image.DataBufferUShort)5 DirectColorModel (java.awt.image.DirectColorModel)4 Graphics2D (java.awt.Graphics2D)3 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)3 Rectangle (java.awt.Rectangle)2 ColorSpace (java.awt.color.ColorSpace)2 ColorModel (java.awt.image.ColorModel)2 Raster (java.awt.image.Raster)2 ByteBuffer (java.nio.ByteBuffer)2