Search in sources :

Example 71 with WritableRaster

use of java.awt.image.WritableRaster in project scriptographer by scriptographer.

the class Raster method getSubImage.

public BufferedImage getSubImage(int x, int y, int width, int height) {
    if (width == -1 || height == -1) {
        Size size = getSize();
        if (width == -1)
            width = (int) size.width;
        if (height == -1)
            height = (int) size.height;
    }
    BufferedImage img = createCompatibleImage(width, height);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(java.awt.Color.WHITE);
    //		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0));
    g2d.fillRect(0, 0, width, height);
    g2d.dispose();
    WritableRaster raster = img.getRaster();
    byte[] data = ((DataBufferByte) raster.getDataBuffer()).getData();
    nativeGetPixels(data, raster.getNumBands(), x, y, width, height);
    return img;
}
Also used : WritableRaster(java.awt.image.WritableRaster) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 72 with WritableRaster

use of java.awt.image.WritableRaster in project intellij-code-outline by sitano.

the class CodeOutlineImage method moveDataDown.

/**
     * Copies data between the given images, starting at the bottom of the
     * images and copying then moving upwards line by line. This method is
     * suitable for when the two given images are subimages of the same image,
     * and their data may overlap.
     *
     * @param from the source image
     * @param to the destination image
     */
private static void moveDataDown(BufferedImage from, BufferedImage to) {
    Raster raster = from.getRaster();
    WritableRaster outRaster = to.getRaster();
    int width = outRaster.getWidth();
    int height = outRaster.getHeight();
    int startX = outRaster.getMinX();
    int startY = outRaster.getMinY();
    Object tdata = null;
    for (int i = startY + height - 1; i >= startY; i--) {
        tdata = raster.getDataElements(startX, i, width, 1, tdata);
        outRaster.setDataElements(startX, i, width, 1, tdata);
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster)

Example 73 with WritableRaster

use of java.awt.image.WritableRaster in project lwjgl by LWJGL.

the class TextureLoader method convertImageData.

/**
     * Convert the buffered image to a texture
     *
     * @param bufferedImage The image to convert to a texture
     * @param texture The texture to store the data into
     * @return A buffer containing the data
     */
private ByteBuffer convertImageData(BufferedImage bufferedImage, Texture texture) {
    ByteBuffer imageBuffer;
    WritableRaster raster;
    BufferedImage texImage;
    int texWidth = 2;
    int texHeight = 2;
    // of the produced texture
    while (texWidth < bufferedImage.getWidth()) {
        texWidth *= 2;
    }
    while (texHeight < bufferedImage.getHeight()) {
        texHeight *= 2;
    }
    texture.setTextureHeight(texHeight);
    texture.setTextureWidth(texWidth);
    // for a texture
    if (bufferedImage.getColorModel().hasAlpha()) {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
        texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable());
    } else {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
        texImage = new BufferedImage(glColorModel, raster, false, new Hashtable());
    }
    // copy the source image into the produced image
    Graphics g = texImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, texWidth, texHeight);
    g.drawImage(bufferedImage, 0, 0, null);
    // build a byte buffer from the temporary image
    // that be used by OpenGL to produce a texture.
    byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
    imageBuffer = ByteBuffer.allocateDirect(data.length);
    imageBuffer.order(ByteOrder.nativeOrder());
    imageBuffer.put(data, 0, data.length);
    imageBuffer.flip();
    return imageBuffer;
}
Also used : Graphics(java.awt.Graphics) WritableRaster(java.awt.image.WritableRaster) Hashtable(java.util.Hashtable) Color(java.awt.Color) DataBufferByte(java.awt.image.DataBufferByte) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage)

Example 74 with WritableRaster

use of java.awt.image.WritableRaster in project cloudstack by apache.

the class AwtCanvasAdapter method handleBitmap.

private void handleBitmap(BitmapOrder order, ByteBuffer buf) {
    // Draw rectangle on offline buffer
    BufferedImage image = canvas.getOfflineImage();
    Graphics2D g = (Graphics2D) image.getGraphics();
    for (BitmapRectangle rectangle : order.rectangles) {
        // *DEBUG*/System.out.println("["+this+"] DEBUG: Rectangle: " +
        // rectangle.toString());
        int x = rectangle.x;
        int y = rectangle.y;
        int width = rectangle.width;
        int height = rectangle.height;
        int bufferWidth = rectangle.bufferWidth;
        int bufferHeight = rectangle.bufferHeight;
        BufferedImage rectImage;
        switch(rectangle.colorDepth) {
            case 8:
                {
                    rectImage = new BufferedImage(bufferWidth, height, BufferedImage.TYPE_BYTE_INDEXED, screen.colorMap);
                    WritableRaster raster = rectImage.getRaster();
                    raster.setDataElements(0, 0, bufferWidth, bufferHeight, rectangle.bitmapDataStream.toByteArray());
                    break;
                }
            case 15:
                {
                    rectImage = new BufferedImage(bufferWidth, height, BufferedImage.TYPE_USHORT_555_RGB);
                    WritableRaster raster = rectImage.getRaster();
                    raster.setDataElements(0, 0, bufferWidth, bufferHeight, rectangle.bitmapDataStream.toShortArray());
                    break;
                }
            case 16:
                {
                    rectImage = new BufferedImage(bufferWidth, height, BufferedImage.TYPE_USHORT_565_RGB);
                    WritableRaster raster = rectImage.getRaster();
                    raster.setDataElements(0, 0, bufferWidth, bufferHeight, rectangle.bitmapDataStream.toShortArray());
                    break;
                }
            case 24:
            case 32:
                {
                    rectImage = new BufferedImage(bufferWidth, height, BufferedImage.TYPE_INT_RGB);
                    WritableRaster raster = rectImage.getRaster();
                    raster.setDataElements(0, 0, bufferWidth, bufferHeight, rectangle.bitmapDataStream.toIntLEArray());
                    break;
                }
            default:
                throw new RuntimeException("Unsupported color depth: " + rectangle.colorDepth + ".");
        }
        g.setClip(x, y, width, height);
        g.drawImage(rectImage, x, y, null);
        // Request update of repainted area
        canvas.updateFrameBuffer(x, y, width, height);
        canvas.repaint(x, y, width, height);
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster) BitmapRectangle(common.BitmapRectangle) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 75 with WritableRaster

use of java.awt.image.WritableRaster in project ChatGameFontificator by GlitchCog.

the class Sprite method copyImage.

private static BufferedImage copyImage(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    WritableRaster raster = bi.copyData(null);
    return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
}
Also used : ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage)

Aggregations

WritableRaster (java.awt.image.WritableRaster)110 BufferedImage (java.awt.image.BufferedImage)74 ColorModel (java.awt.image.ColorModel)33 DirectColorModel (java.awt.image.DirectColorModel)20 IndexColorModel (java.awt.image.IndexColorModel)19 DataBufferByte (java.awt.image.DataBufferByte)17 Point (java.awt.Point)15 Raster (java.awt.image.Raster)15 Rectangle (java.awt.Rectangle)13 Graphics2D (java.awt.Graphics2D)12 ComponentColorModel (java.awt.image.ComponentColorModel)12 DataBufferInt (java.awt.image.DataBufferInt)10 SampleModel (java.awt.image.SampleModel)9 DataBuffer (java.awt.image.DataBuffer)8 IOException (java.io.IOException)8 ColorSpace (java.awt.color.ColorSpace)7 Color (java.awt.Color)6 Iterator (java.util.Iterator)6 AffineTransform (java.awt.geom.AffineTransform)5 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)5