Search in sources :

Example 11 with DataBufferByte

use of java.awt.image.DataBufferByte in project nutz by nutzam.

the class Images method createJPEG4.

/**
     * Java's ImageIO can't process 4-component images and Java2D can't apply
     * AffineTransformOp either, so convert raster data to RGB. Technique due to
     * MArk Stephens. Free for any use.
     */
private static BufferedImage createJPEG4(Raster raster) {
    int w = raster.getWidth();
    int h = raster.getHeight();
    byte[] rgb = new byte[w * h * 3];
    float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
    float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
    float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
    float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
    for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
        float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
        double val = y + 1.402 * (cr - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
        val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
        val = y + 1.772 * (cb - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
    }
    raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[] { 0, 1, 2 }, null);
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    return new BufferedImage(cm, (WritableRaster) raster, true, null);
}
Also used : ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage)

Example 12 with DataBufferByte

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

the class Raster method drawImage.

public void drawImage(Image image, int x, int y) {
    BufferedImage buf;
    // a new one:
    if (image instanceof BufferedImage && getColorModel().isCompatibleSampleModel(((BufferedImage) image).getSampleModel())) {
        buf = (BufferedImage) image;
    } else {
        buf = createCompatibleImage(image.getWidth(null), image.getHeight(null));
        buf.createGraphics().drawImage(image, x, y, null);
    }
    WritableRaster raster = buf.getRaster();
    byte[] data = ((DataBufferByte) raster.getDataBuffer()).getData();
    nativeSetPixels(data, raster.getNumBands(), x, y, buf.getWidth(), buf.getHeight());
}
Also used : WritableRaster(java.awt.image.WritableRaster) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage)

Example 13 with DataBufferByte

use of java.awt.image.DataBufferByte 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 14 with DataBufferByte

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

Example 15 with DataBufferByte

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

Aggregations

DataBufferByte (java.awt.image.DataBufferByte)28 BufferedImage (java.awt.image.BufferedImage)16 WritableRaster (java.awt.image.WritableRaster)14 DataBuffer (java.awt.image.DataBuffer)12 DataBufferInt (java.awt.image.DataBufferInt)12 DataBufferShort (java.awt.image.DataBufferShort)10 Point (java.awt.Point)9 SampleModel (java.awt.image.SampleModel)8 DataBufferUShort (java.awt.image.DataBufferUShort)7 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 Graphics2D (java.awt.Graphics2D)6 Rectangle (java.awt.Rectangle)6 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)5 ColorModel (java.awt.image.ColorModel)4 Raster (java.awt.image.Raster)4 ColorSpace (java.awt.color.ColorSpace)3 AffineTransform (java.awt.geom.AffineTransform)3 IndexColorModel (java.awt.image.IndexColorModel)3 RenderedImage (java.awt.image.RenderedImage)3