Search in sources :

Example 21 with DataBufferInt

use of java.awt.image.DataBufferInt 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 22 with DataBufferInt

use of java.awt.image.DataBufferInt 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)

Example 23 with DataBufferInt

use of java.awt.image.DataBufferInt in project platform_frameworks_base by android.

the class ShadowPainter method createDropShadow.

/**
     * Creates a drop shadow of a given image and returns a new image which shows the input image on
     * top of its drop shadow.
     * <p/>
     * <b>NOTE: If the shape is rectangular and opaque, consider using {@link
     * #drawRectangleShadow(Graphics2D, int, int, int, int)} instead.</b>
     *
     * @param source the source image to be shadowed
     * @param shadowSize the size of the shadow in pixels
     * @param shadowOpacity the opacity of the shadow, with 0=transparent and 1=opaque
     * @param shadowRgb the RGB int to use for the shadow color
     *
     * @return a new image with the source image on top of its shadow when shadowSize > 0 or the
     * source image otherwise
     */
// Imported code
@SuppressWarnings({ "SuspiciousNameCombination", "UnnecessaryLocalVariable" })
public static BufferedImage createDropShadow(BufferedImage source, int shadowSize, float shadowOpacity, int shadowRgb) {
    if (shadowSize <= 0) {
        return source;
    }
    // This code is based on
    //      http://www.jroller.com/gfx/entry/non_rectangular_shadow
    BufferedImage image;
    int width = source.getWidth();
    int height = source.getHeight();
    image = new BufferedImage(width + SHADOW_SIZE, height + SHADOW_SIZE, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    g2.drawImage(image, shadowSize, shadowSize, null);
    int dstWidth = image.getWidth();
    int dstHeight = image.getHeight();
    int left = (shadowSize - 1) >> 1;
    int right = shadowSize - left;
    int xStart = left;
    int xStop = dstWidth - right;
    int yStart = left;
    int yStop = dstHeight - right;
    shadowRgb &= 0x00FFFFFF;
    int[] aHistory = new int[shadowSize];
    int historyIdx;
    int aSum;
    int[] dataBuffer = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    int lastPixelOffset = right * dstWidth;
    float sumDivider = shadowOpacity / shadowSize;
    // horizontal pass
    for (int y = 0, bufferOffset = 0; y < dstHeight; y++, bufferOffset = y * dstWidth) {
        aSum = 0;
        historyIdx = 0;
        for (int x = 0; x < shadowSize; x++, bufferOffset++) {
            int a = dataBuffer[bufferOffset] >>> 24;
            aHistory[x] = a;
            aSum += a;
        }
        bufferOffset -= right;
        for (int x = xStart; x < xStop; x++, bufferOffset++) {
            int a = (int) (aSum * sumDivider);
            dataBuffer[bufferOffset] = a << 24 | shadowRgb;
            // subtract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];
            // get the latest pixel
            a = dataBuffer[bufferOffset + right] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;
            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }
    // vertical pass
    for (int x = 0, bufferOffset = 0; x < dstWidth; x++, bufferOffset = x) {
        aSum = 0;
        historyIdx = 0;
        for (int y = 0; y < shadowSize; y++, bufferOffset += dstWidth) {
            int a = dataBuffer[bufferOffset] >>> 24;
            aHistory[y] = a;
            aSum += a;
        }
        bufferOffset -= lastPixelOffset;
        for (int y = yStart; y < yStop; y++, bufferOffset += dstWidth) {
            int a = (int) (aSum * sumDivider);
            dataBuffer[bufferOffset] = a << 24 | shadowRgb;
            // subtract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];
            // get the latest pixel
            a = dataBuffer[bufferOffset + lastPixelOffset] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;
            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }
    g2.drawImage(source, null, 0, 0);
    g2.dispose();
    return image;
}
Also used : DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 24 with DataBufferInt

use of java.awt.image.DataBufferInt in project processing by processing.

the class PSurfaceJOGL method setCursor.

public void setCursor(PImage image, int hotspotX, int hotspotY) {
    Display disp = window.getScreen().getDisplay();
    BufferedImage bimg = (BufferedImage) image.getNative();
    DataBufferInt dbuf = (DataBufferInt) bimg.getData().getDataBuffer();
    int[] ipix = dbuf.getData();
    ByteBuffer pixels = ByteBuffer.allocate(ipix.length * 4);
    pixels.asIntBuffer().put(ipix);
    PixelFormat format = PixelFormat.ARGB8888;
    final Dimension size = new Dimension(bimg.getWidth(), bimg.getHeight());
    PixelRectangle pixelrect = new PixelRectangle.GenericPixelRect(format, size, 0, false, pixels);
    final PointerIcon pi = disp.createPointerIcon(pixelrect, hotspotX, hotspotY);
    display.getEDTUtil().invoke(false, new Runnable() {

        @Override
        public void run() {
            window.setPointerIcon(pi);
        }
    });
}
Also used : PixelRectangle(com.jogamp.nativewindow.util.PixelRectangle) PixelFormat(com.jogamp.nativewindow.util.PixelFormat) DataBufferInt(java.awt.image.DataBufferInt) Dimension(com.jogamp.nativewindow.util.Dimension) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) PointerIcon(com.jogamp.newt.Display.PointerIcon) Display(com.jogamp.newt.Display)

Example 25 with DataBufferInt

use of java.awt.image.DataBufferInt in project playn by threerings.

the class JavaGLContext method updateTexture.

void updateTexture(int tex, BufferedImage image) {
    // Convert the image into a format for quick uploading
    image = convertImage(image);
    DataBuffer dbuf = image.getRaster().getDataBuffer();
    ByteBuffer bbuf;
    int format, type;
    if (image.getType() == BufferedImage.TYPE_INT_ARGB_PRE) {
        DataBufferInt ibuf = (DataBufferInt) dbuf;
        int iSize = ibuf.getSize() * 4;
        bbuf = checkGetImageBuffer(iSize);
        bbuf.asIntBuffer().put(ibuf.getData());
        bbuf.position(bbuf.position() + iSize);
        bbuf.flip();
        format = GL12.GL_BGRA;
        type = GL12.GL_UNSIGNED_INT_8_8_8_8_REV;
    } else if (image.getType() == BufferedImage.TYPE_4BYTE_ABGR) {
        DataBufferByte dbbuf = (DataBufferByte) dbuf;
        bbuf = checkGetImageBuffer(dbbuf.getSize());
        bbuf.put(dbbuf.getData());
        bbuf.flip();
        format = GL11.GL_RGBA;
        type = GL12.GL_UNSIGNED_INT_8_8_8_8;
    } else {
        // except we don't know how to deal with it
        throw new RuntimeException("Image type wasn't converted to usable: " + image.getType());
    }
    bindTexture(tex);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, format, type, bbuf);
    checkGLError("updateTexture");
}
Also used : DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) ByteBuffer(java.nio.ByteBuffer) DataBuffer(java.awt.image.DataBuffer)

Aggregations

DataBufferInt (java.awt.image.DataBufferInt)37 BufferedImage (java.awt.image.BufferedImage)22 DataBuffer (java.awt.image.DataBuffer)15 DataBufferByte (java.awt.image.DataBufferByte)12 DataBufferShort (java.awt.image.DataBufferShort)10 Graphics2D (java.awt.Graphics2D)9 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)8 WritableRaster (java.awt.image.WritableRaster)8 SampleModel (java.awt.image.SampleModel)7 DataBufferUShort (java.awt.image.DataBufferUShort)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 Point (java.awt.Point)5 ComponentSampleModel (java.awt.image.ComponentSampleModel)5 Rectangle (java.awt.Rectangle)4 IndexColorModel (java.awt.image.IndexColorModel)4 ColorModel (java.awt.image.ColorModel)3 DirectColorModel (java.awt.image.DirectColorModel)3 ByteBuffer (java.nio.ByteBuffer)3 Raster (java.awt.image.Raster)2 IOException (java.io.IOException)2