Example usage for java.awt.image BufferedImage getRaster

List of usage examples for java.awt.image BufferedImage getRaster

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getRaster.

Prototype

public WritableRaster getRaster() 

Source Link

Document

Returns the WritableRaster .

Usage

From source file:Snippet156.java

static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }// www.j av  a 2s . com
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

From source file:Main.java

private static void applyShadow(BufferedImage image, int shadowSize, Color shadowColor, float shadowOpacity) {
    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;

    int shadowRgb = shadowColor.getRGB() & 0x00FFFFFF;

    int[] aHistory = new int[shadowSize];
    int historyIdx = 0;

    int aSum;/*ww  w . j  a v  a  2 s .  c  o m*/

    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;

            // substract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];

            // get the lastest 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;

            // substract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];

            // get the lastest pixel
            a = dataBuffer[bufferOffset + lastPixelOffset] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;

            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }
}

From source file:SWTUtils.java

/**
 * Converts a buffered image to SWT <code>ImageData</code>.
 *
 * @param bufferedImage  the buffered image (<code>null</code> not
 *         permitted).//  w  ww  .j  av a 2  s .c  om
 *
 * @return The image data.
 */
public static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

From source file:VASSAL.tools.image.tilecache.TileUtils.java

/**
 * Write a tile image to a stream./*  w  w w .j a va2 s  .  c om*/
 *
 * @param tile the image
 * @param out the stream
 *
 * @throws ImageIOException if the write fails
 */
public static void write(BufferedImage tile, OutputStream out) throws IOException {
    ByteBuffer bb;

    // write the header
    bb = ByteBuffer.allocate(18);

    bb.put("VASSAL".getBytes()).putInt(tile.getWidth()).putInt(tile.getHeight()).putInt(tile.getType());

    out.write(bb.array());

    // write the tile data
    final DataBufferInt db = (DataBufferInt) tile.getRaster().getDataBuffer();
    final int[] data = db.getData();

    bb = ByteBuffer.allocate(4 * data.length);
    bb.asIntBuffer().put(data);

    final GZIPOutputStream zout = new GZIPOutputStream(out);
    zout.write(bb.array());
    zout.finish();
}

From source file:org.openossad.util.ImageUtil.java

public static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }/*from  w w  w  . jav a 2s .c  o  m*/
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

public static int[][] imageToPixels(BufferedImage image) {
    if (image == null) {
        return null;
    }/*from  w w  w . ja  v a2  s .  co m*/
    int w = image.getWidth();
    int h = image.getHeight();
    int[][] pixels = new int[w][h];
    Raster raster = image.getRaster();
    if (raster.getTransferType() == DataBuffer.TYPE_BYTE) {
        byte[] bytes = (byte[]) raster.getDataElements(0, 0, w, h, null);
        int bytesPerPixel = (bytes.length / (w * h));
        ColorModel colorModel = image.getColorModel();
        byte[] buf = new byte[bytesPerPixel];
        for (int x = 0; x < w; ++x) {
            for (int y = 0; y < h; ++y) {
                System.arraycopy(bytes, (x + y * w) * bytesPerPixel, buf, 0, bytesPerPixel);
                pixels[x][y] = colorModel.getRGB(buf) & 0xFFFFFF;
            }
        }
        return pixels;
    } else {
        throw new RuntimeException("transfer type " + raster.getTransferType() + " not implemented yet");
    }
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Can be used to verify impact of different encoding algorithms. Compares two BufferedImages and outputs differences between those.
 *
 * @param image//from w w w .  jav  a2 s  .c  o  m
 * @param image2
 * @return
 */
public static int[] calculateBufferedImageDifferencesPxByPx(BufferedImage image, BufferedImage image2) {
    int width = image.getWidth();
    int height = image.getHeight();

    int width2 = image2.getWidth();
    int height2 = image2.getHeight();

    WritableRaster raster1 = image.getRaster();
    WritableRaster raster2 = image2.getRaster();

    if (width != width2 || height != height2)
        throw new IllegalArgumentException(
                "Please insert two identical images that were treated with different image algorithms");

    int[] diff = new int[width * height];

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster1.getNumBands(); band++) {
                int p1 = raster1.getSample(col, row, band);
                int p2 = raster2.getSample(col, row, band);

                diff[((row * width) + col)] = p2 - p1;
            }
        }
    }

    return diff;
}

From source file:lucee.runtime.img.ImageUtil.java

public static BufferedImage createBufferedImage(BufferedImage image, int columns, int rows) {
    ColorModel colormodel = image.getColorModel();
    BufferedImage newImage;//w w  w .  j  a  va2 s.co  m
    if (colormodel instanceof IndexColorModel) {
        if (colormodel.getTransparency() != 1)
            newImage = new BufferedImage(columns, rows, 2);
        else
            newImage = new BufferedImage(columns, rows, 1);
    } else {
        newImage = new BufferedImage(colormodel,
                image.getRaster().createCompatibleWritableRaster(columns, rows),
                colormodel.isAlphaPremultiplied(), null);
    }
    return newImage;
}

From source file:ro.nextreports.designer.util.ImageUtil.java

private static void applyShadow(BufferedImage image) {
    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;

    int shadowRgb = shadowColor.getRGB() & 0x00FFFFFF;

    int[] aHistory = new int[shadowSize];
    int historyIdx = 0;

    int aSum;// www  . ja  va 2s.com

    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;

            // substract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];

            // get the lastest 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;

            // substract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];

            // get the lastest pixel
            a = dataBuffer[bufferOffset + lastPixelOffset] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;

            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.java

/**
 * Returns an ARGB image filled with the given paint and using the given image as a mask.
 * @param paint the paint to fill the visible portions of the image with
 * @return a masked image filled with the given paint
 * @throws IOException if the image cannot be read
 * @throws IllegalStateException if the image is not a stencil.
 *///www  .  j  ava 2 s . c om
public static BufferedImage getStencilImage(PDImage pdImage, Paint paint) throws IOException {
    // get mask (this image)
    BufferedImage mask = getRGBImage(pdImage, null);

    // compose to ARGB
    BufferedImage masked = new BufferedImage(mask.getWidth(), mask.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = masked.createGraphics();

    // draw the mask
    //g.drawImage(mask, 0, 0, null);

    // fill with paint using src-in
    //g.setComposite(AlphaComposite.SrcIn);
    g.setPaint(paint);
    g.fillRect(0, 0, mask.getWidth(), mask.getHeight());
    g.dispose();

    // set the alpha
    int width = masked.getWidth();
    int height = masked.getHeight();
    WritableRaster raster = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    final float[] transparent = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            if (alphaPixel[0] == 255) {
                raster.setPixel(x, y, transparent);
            }
        }
    }

    return masked;
}