Search in sources :

Example 26 with DataBufferByte

use of java.awt.image.DataBufferByte in project commons-gdx by gemserk.

the class ScreenshotSaver method saveScreenshot.

public static void saveScreenshot(File file, byte[] pixels, int width, int height, boolean hasAlpha) throws IOException {
    DataBufferByte dataBuffer = new DataBufferByte(pixels, pixels.length);
    PixelInterleavedSampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, 4, 4 * width, getOffsets(hasAlpha));
    WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
    BufferedImage img = new BufferedImage(getColorModel(hasAlpha), raster, false, null);
    ImageIO.write(img, "png", file);
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) WritableRaster(java.awt.image.WritableRaster) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage)

Example 27 with DataBufferByte

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

the class Screenshots method convertScreenShot.

/**
     * Flips the image along the Y axis and converts BGRA to ABGR
     * 
     * @param bgraBuf
     * @param out 
     */
public static void convertScreenShot(ByteBuffer bgraBuf, BufferedImage out) {
    WritableRaster wr = out.getRaster();
    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
    byte[] cpuArray = db.getData();
    // copy native memory to java memory
    bgraBuf.clear();
    bgraBuf.get(cpuArray);
    bgraBuf.clear();
    int width = wr.getWidth();
    int height = wr.getHeight();
    // flip the components the way AWT likes them
    // calcuate half of height such that all rows of the array are written to
    // e.g. for odd heights, write 1 more scanline
    int heightdiv2ceil = height % 2 == 1 ? (height / 2) + 1 : height / 2;
    for (int y = 0; y < heightdiv2ceil; y++) {
        for (int x = 0; x < width; x++) {
            int inPtr = (y * width + x) * 4;
            int outPtr = ((height - y - 1) * width + x) * 4;
            byte b1 = cpuArray[inPtr + 0];
            byte g1 = cpuArray[inPtr + 1];
            byte r1 = cpuArray[inPtr + 2];
            byte a1 = cpuArray[inPtr + 3];
            byte b2 = cpuArray[outPtr + 0];
            byte g2 = cpuArray[outPtr + 1];
            byte r2 = cpuArray[outPtr + 2];
            byte a2 = cpuArray[outPtr + 3];
            cpuArray[outPtr + 0] = a1;
            cpuArray[outPtr + 1] = b1;
            cpuArray[outPtr + 2] = g1;
            cpuArray[outPtr + 3] = r1;
            cpuArray[inPtr + 0] = a2;
            cpuArray[inPtr + 1] = b2;
            cpuArray[inPtr + 2] = g2;
            cpuArray[inPtr + 3] = r2;
        }
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster) DataBufferByte(java.awt.image.DataBufferByte)

Example 28 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 29 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 30 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)

Aggregations

DataBufferByte (java.awt.image.DataBufferByte)64 BufferedImage (java.awt.image.BufferedImage)38 Point (java.awt.Point)36 WritableRaster (java.awt.image.WritableRaster)18 DataBufferInt (java.awt.image.DataBufferInt)14 DataBuffer (java.awt.image.DataBuffer)13 DataBufferShort (java.awt.image.DataBufferShort)10 UShortImage (cbit.vcell.VirtualMicroscopy.UShortImage)8 Rectangle (java.awt.Rectangle)8 SampleModel (java.awt.image.SampleModel)8 DataBufferUShort (java.awt.image.DataBufferUShort)7 IndexColorModel (java.awt.image.IndexColorModel)7 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 Graphics2D (java.awt.Graphics2D)6 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 UserCancelException (org.vcell.util.UserCancelException)6 UtilCancelException (org.vcell.util.UtilCancelException)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)5 Hashtable (java.util.Hashtable)5 VCImage (cbit.image.VCImage)4