Search in sources :

Example 31 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)

Example 32 with DataBufferInt

use of java.awt.image.DataBufferInt in project android_frameworks_base by AOSPA.

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 33 with DataBufferInt

use of java.awt.image.DataBufferInt in project android_frameworks_base by ResurrectionRemix.

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 34 with DataBufferInt

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

the class HTMLCodec method platformImageBytesToImage.

/**
     * Translates either a byte array or an input stream which contain
     * platform-specific image data in the given format into an Image.
     */
@Override
protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException {
    String mimeType = null;
    if (format == CF_PNG) {
        mimeType = "image/png";
    } else if (format == CF_JFIF) {
        mimeType = "image/jpeg";
    }
    if (mimeType != null) {
        return standardImageBytesToImage(bytes, mimeType);
    }
    int[] imageData = platformImageBytesToImageData(bytes, format);
    if (imageData == null) {
        throw new IOException("data translation failed");
    }
    int len = imageData.length - 2;
    int width = imageData[len];
    int height = imageData[len + 1];
    DataBufferInt buffer = new DataBufferInt(imageData, len);
    WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandmasks, null);
    return new BufferedImage(directColorModel, raster, false, null);
}
Also used : WritableRaster(java.awt.image.WritableRaster) IOException(java.io.IOException) DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage)

Example 35 with DataBufferInt

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

the class IncorrectClipXorModeSW2Surface method getBufferedImage.

private static BufferedImage getBufferedImage(int sw) {
    final BufferedImage bi = new BufferedImage(sw, sw, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, sw, sw);
    g2d.dispose();
    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) Graphics2D(java.awt.Graphics2D) DataBuffer(java.awt.image.DataBuffer)

Aggregations

DataBufferInt (java.awt.image.DataBufferInt)47 BufferedImage (java.awt.image.BufferedImage)29 DataBuffer (java.awt.image.DataBuffer)16 DataBufferByte (java.awt.image.DataBufferByte)14 Graphics2D (java.awt.Graphics2D)11 Point (java.awt.Point)11 WritableRaster (java.awt.image.WritableRaster)11 DataBufferShort (java.awt.image.DataBufferShort)10 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)10 SampleModel (java.awt.image.SampleModel)8 Rectangle (java.awt.Rectangle)6 DataBufferUShort (java.awt.image.DataBufferUShort)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 ComponentSampleModel (java.awt.image.ComponentSampleModel)5 DirectColorModel (java.awt.image.DirectColorModel)5 IndexColorModel (java.awt.image.IndexColorModel)5 ColorModel (java.awt.image.ColorModel)4 ByteBuffer (java.nio.ByteBuffer)3 CartesianMesh (cbit.vcell.solvers.CartesianMesh)2 Dimension (java.awt.Dimension)2