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:com.thalespf.dip.DeblurringTest.java

private static void blurTest() throws IOException {
    String imagePath = "image.jpg";
    IImageIO imageIO = ImageIODesktop.createImageIO(imagePath);
    Object imageObject = imageIO.getImageObject();
    if (imageObject == null || !(imageObject instanceof BufferedImage)) {
        throw new IllegalStateException("Nao foi possivel criar a imagem.");
    }/*from w ww . j  a v a  2s . c  om*/

    BufferedImage bufferedImage = (BufferedImage) imageObject;
    WritableRaster raster = bufferedImage.getRaster();

    int width = bufferedImage.getWidth();
    int height = bufferedImage.getHeight();

    double[] fft = new double[2 * width * height];
    FFTForwardInverseTest.fftForward(raster, width, height, fft);

    Complex[] ci = new Complex[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {

            Complex c = new Complex(fft[2 * (x + y * width)], fft[2 * (x + y * width) + 1]);
            ci[x + y * width] = c;

        }
    }

    normalizeToMaxAbsValue(ci, width, height, 1);

    double[] degradation = new double[2 * width * height];
    //motionBlur (funcao de transferencia)
    Complex[] complexPsf = motionBlur(degradation, width, height);

    normalizeToMaxAbsValue(complexPsf, width, height, 1);

    //convolve data
    double[] convoluted = new double[2 * width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double realImg = fft[2 * (x + y * width)];
            double imgImg = fft[2 * (x + y * width) + 1];
            Complex complexImage = new Complex(realImg, imgImg);

            double realDeg = degradation[2 * (x + y * width)];
            double imgDeg = degradation[2 * (x + y * width) + 1];
            Complex complexDeg = new Complex(realDeg, imgDeg);

            Complex m = complexImage.multiply(complexDeg);
            convoluted[2 * (x + y * width)] = m.getReal();
            convoluted[2 * (x + y * width) + 1] = m.getImaginary();
        }
    }

    double[] imageResult = new double[width * height];
    FFTForwardInverseTest.fftInverse(convoluted, imageResult, width, height);

    int[] image = new int[imageResult.length];
    Expansion.globalExpansion2(imageResult, image, width, height);

    ImageIODesktop imageIODesktop = new ImageIODesktop();
    imageIODesktop.savePixels(image, width, height, null, "image_blurred");
}

From source file:org.apache.fop.visual.BitmapComparator.java

/**
 * Loads an image from a URL/*from  w w w.ja  va  2s  .  c  o  m*/
 * @param bitmapFile the bitmap file
 * @return the bitmap as BufferedImage
 */
public static BufferedImage getImage(File bitmapFile) {
    try {
        InputStream in = new java.io.FileInputStream(bitmapFile);
        try {
            in = new java.io.BufferedInputStream(in);

            ImageTagRegistry reg = ImageTagRegistry.getRegistry();
            Filter filt = reg.readStream(in);
            if (filt == null) {
                return null;
            }

            RenderedImage red = filt.createDefaultRendering();
            if (red == null) {
                return null;
            }

            BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB);
            red.copyData(img.getRaster());
            return img;
        } finally {
            IOUtils.closeQuietly(in);
        }
    } catch (IOException e) {
        return null;
    }
}

From source file:Snippet156.java

static BufferedImage convertToAWT(ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;/* w w w.j  a  v a 2  s .  c  om*/
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        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++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                pixelArray[0] = rgb.red;
                pixelArray[1] = rgb.green;
                pixelArray[2] = rgb.blue;
                raster.setPixels(x, y, 1, 1, pixelArray);
            }
        }
        return bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel,
                colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        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++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        return bufferedImage;
    }
}

From source file:ml.hsv.java

public static void HSV2File(hsv hsvImage[][], int width, int height) throws IOException //store img output as hsv2file.png
{
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int RGB = Color.HSBtoRGB(hsvImage[i][j].h, hsvImage[i][j].s, hsvImage[i][j].v);
            Color c = new Color(RGB);
            int temp[] = { c.getRed(), c.getGreen(), c.getBlue() };
            raster.setPixel(j, i, temp);
        }/*www. j a v a 2 s  .  c  o m*/
    }
    ImageIO.write(image, "PNG",
            new File("/home/shinchan/FinalProject/PaperImplementation/Eclipse/ML/output/hsv2file.png"));
}

From source file:org.shaman.terrain.vegetation.ImpositorCreator.java

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();/*  ww  w  .  jav a2s .  c  o m*/
    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] = r1;//b1;
            cpuArray[outPtr + 2] = g1;
            cpuArray[outPtr + 3] = b1;//r1;

            cpuArray[inPtr + 0] = a2;
            cpuArray[inPtr + 1] = r2;//b2;
            cpuArray[inPtr + 2] = g2;
            cpuArray[inPtr + 3] = b2;//r2;
        }
    }
}

From source file:org.apache.fop.visual.BitmapComparator.java

/**
 * Builds a new BufferedImage that is the difference between the two input images
 * @param ref the reference bitmap//  w ww  .  j av  a2s  . c  o  m
 * @param gen the newly generated bitmap
 * @return the diff bitmap
 */
public static BufferedImage buildDiffImage(BufferedImage ref, BufferedImage gen) {
    BufferedImage diff = new BufferedImage(ref.getWidth(), ref.getHeight(), BufferedImage.TYPE_INT_ARGB);
    WritableRaster refWR = ref.getRaster();
    WritableRaster genWR = gen.getRaster();
    WritableRaster dstWR = diff.getRaster();

    boolean refPre = ref.isAlphaPremultiplied();
    if (!refPre) {
        ColorModel cm = ref.getColorModel();
        cm = GraphicsUtil.coerceData(refWR, cm, true);
        ref = new BufferedImage(cm, refWR, true, null);
    }
    boolean genPre = gen.isAlphaPremultiplied();
    if (!genPre) {
        ColorModel cm = gen.getColorModel();
        cm = GraphicsUtil.coerceData(genWR, cm, true);
        gen = new BufferedImage(cm, genWR, true, null);
    }

    int w = ref.getWidth();
    int h = ref.getHeight();

    int y, i, val;
    int[] refPix = null;
    int[] genPix = null;
    for (y = 0; y < h; y++) {
        refPix = refWR.getPixels(0, y, w, 1, refPix);
        genPix = genWR.getPixels(0, y, w, 1, genPix);
        for (i = 0; i < refPix.length; i++) {
            // val = ((genPix[i] - refPix[i]) * 5) + 128;
            val = ((refPix[i] - genPix[i]) * 10) + 128;
            if ((val & 0xFFFFFF00) != 0) {
                if ((val & 0x80000000) != 0) {
                    val = 0;
                } else {
                    val = 255;
                }
            }
            genPix[i] = val;
        }
        dstWR.setPixels(0, y, w, 1, genPix);
    }

    if (!genPre) {
        ColorModel cm = gen.getColorModel();
        cm = GraphicsUtil.coerceData(genWR, cm, false);
    }

    if (!refPre) {
        ColorModel cm = ref.getColorModel();
        cm = GraphicsUtil.coerceData(refWR, cm, false);
    }

    return diff;
}

From source file:com.vaadin.testbench.screenshot.ImageUtil.java

/**
 * Extract magical image properties used by the getBlock function.
 * //from   w w  w  . j  a va2  s. c o  m
 * @param image
 *            a BufferedImage
 * @return an ImageProperties descriptor object
 */
public static final ImageProperties getImageProperties(final BufferedImage image) {
    final int imageType = image.getType();
    ImageProperties p = new ImageProperties();
    p.image = image;
    p.raster = image.getRaster();
    p.alpha = imageType == TYPE_INT_ARGB || imageType == BufferedImage.TYPE_4BYTE_ABGR;
    boolean rgb = imageType == TYPE_INT_ARGB || imageType == TYPE_INT_RGB;
    boolean bgr = imageType == BufferedImage.TYPE_INT_BGR || imageType == BufferedImage.TYPE_3BYTE_BGR
            || imageType == BufferedImage.TYPE_4BYTE_ABGR;
    p.width = image.getWidth();
    p.height = image.getHeight();
    p.fallback = !(rgb || bgr);
    return p;
}

From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static BufferedImage makeTransparent(BufferedImage image, int x, int y) {
    ColorModel cm = image.getColorModel();
    if (!(cm instanceof IndexColorModel))
        return image; // sorry...
    IndexColorModel icm = (IndexColorModel) cm;
    WritableRaster raster = image.getRaster();
    int pixel = raster.getSample(x, y, 0); // pixel is offset in ICM's
    // palette/*from www . j  a v  a2s. c o  m*/
    int size = icm.getMapSize();
    byte[] reds = new byte[size];
    byte[] greens = new byte[size];
    byte[] blues = new byte[size];
    icm.getReds(reds);
    icm.getGreens(greens);
    icm.getBlues(blues);
    IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
    return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null);
}

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

/**
 * Convert image from Cmyk to Rgb profile
 * @param cmykRaster/*from  w  ww.j  a va  2 s . c  o m*/
 * @param cmykProfile
 * @return The BufferedImage obj
 * @throws IOException
 */
private static BufferedImage convertCmykToRgb(Raster cmykRaster, ICC_Profile cmykProfile) throws IOException {
    if (cmykProfile == null) {
        cmykProfile = ICC_Profile.getInstance(
                CustomExtraImgReader.class.getResourceAsStream(Consts.RESOURCEPATH + Consts.GENERICICCFILE));
    }
    if (cmykProfile.getProfileClass() != ICC_Profile.CLASS_DISPLAY) {
        byte[] profileData = cmykProfile.getData();
        if (profileData[ICC_Profile.icHdrRenderingIntent] == ICC_Profile.icPerceptual) {
            intToBigEndian(ICC_Profile.icSigDisplayClass, profileData, ICC_Profile.icHdrDeviceClass);
            cmykProfile = ICC_Profile.getInstance(profileData);
        }
    }

    ICC_ColorSpace cmykCS = new ICC_ColorSpace(cmykProfile);
    BufferedImage rgbImage = new BufferedImage(cmykRaster.getWidth(), cmykRaster.getHeight(),
            BufferedImage.TYPE_INT_RGB);
    WritableRaster rgbRaster = rgbImage.getRaster();
    ColorSpace rgbCS = rgbImage.getColorModel().getColorSpace();
    ColorConvertOp cmykToRgb = new ColorConvertOp(cmykCS, rgbCS, null);
    cmykToRgb.filter(cmykRaster, rgbRaster);
    return rgbImage;
}

From source file:oct.util.Util.java

/**
 * Convert the supplied image to a 2D pixel array such that an (X,Y) value
 * indexes as array[x][y].//  ww w . j  a va 2  s  .c o  m
 *
 * Credit for this method goes to Stack Overflow user Mota and their post
 * here:
 * http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image
 * for this implementation.
 *
 * This method will return the red, green and blue values directly for each
 * pixel, and if there is an alpha channel it will add the alpha value.
 * Using this method is harder in terms of calculating indices, but is much
 * faster than using getRGB to build this same array.
 *
 * @param image
 * @return
 */
public static int[][] convertTo2D(BufferedImage image) {

    final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    final int width = image.getWidth();
    final int height = image.getHeight();
    final boolean hasAlphaChannel = image.getAlphaRaster() != null;

    int[][] result = new int[width][height];
    if (hasAlphaChannel) {
        final int pixelLength = 4;
        for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
            argb += ((int) pixels[pixel + 1] & 0xff); // blue
            argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
            argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
            result[col][row] = argb;
            col++;
            if (col == width) {
                col = 0;
                row++;
            }
        }
    } else {
        final int pixelLength = 3;
        for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            argb += -16777216; // 255 alpha
            argb += ((int) pixels[pixel] & 0xff); // blue
            argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
            argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
            result[col][row] = argb;
            col++;
            if (col == width) {
                col = 0;
                row++;
            }
        }
    }

    return result;
}