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:tilt.image.Blob.java

/**
 * Test with small image//from ww w.  j  a va  2 s  .  c o  m
 * @param args ignored
 */
public static void main(String[] args) {
    try {
        Options opts = new Options(new JSONObject());
        String url = "http://ecdosis.net/test.png";
        Double[][] cc = { { 0.0, 0.0 }, { 100.0, 0.0 }, { 100.0, 100.0 }, { 0.0, 100.0 } };
        Picture p = new Picture(opts, url, new TextIndex("", ""), cc, InetAddress.getByName("127.0.0.1"));
        p.convertToTwoTone();
        BufferedImage bandw = ImageIO.read(p.twotone);
        WritableRaster wr = bandw.getRaster();
        Blob largest = null;
        int max = 0;
        int[] iArray = new int[1];
        WritableRaster darkRegions = bandw.copyData(null);
        Blob.setToWhite(darkRegions);
        for (int y = 0; y < wr.getHeight(); y++) {
            for (int x = 0; x < wr.getWidth(); x++) {
                Blob b = new Blob(darkRegions, opts, null);
                wr.getPixel(x, y, iArray);
                if (iArray[0] == 0) {
                    b.expandArea(wr, new Point(x, y));
                    if (b.size() > max) {
                        System.out.println("Found new blob at " + x + "," + y + " size=" + b.size());
                        largest = b;
                        max = b.size();
                    }
                }
            }
        }
        if (largest != null) {
            WritableRaster dirt = bandw.copyData(null);
            Blob.setToWhite(dirt);
            largest.save(dirt, wr, largest.firstBlackPixel);
            System.out.println(largest.toString());
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
}

From source file:Main.java

public static double meanValue(BufferedImage image) {
    Raster raster = image.getRaster();
    double sum = 0.0;

    for (int y = 0; y < image.getHeight(); ++y) {
        for (int x = 0; x < image.getWidth(); ++x) {
            sum += raster.getSample(x, y, 0);
        }//ww  w  .jav a 2  s  .c om
    }
    return sum / (image.getWidth() * image.getHeight());
}

From source file:Main.java

private static IntBuffer getImageAsARGBIntBuffer(BufferedImage image) {
    DataBuffer buffer = image.getRaster().getDataBuffer();

    if (buffer instanceof DataBufferInt) {
        return IntBuffer.wrap(((DataBufferInt) buffer).getData());
    } else if (buffer instanceof DataBufferByte) {
        return ByteBuffer.wrap(((DataBufferByte) buffer).getData()).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
    } else {//from   w ww  . j a v  a  2 s. c  o m
        int width = image.getWidth();
        int height = image.getHeight();
        int[] pixels = new int[width * height];
        image.getRGB(0, 0, width, height, pixels, 0, width);
        return IntBuffer.wrap(pixels);
    }
}

From source file:Main.java

private static BufferedImage colorImage(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();
    WritableRaster raster = image.getRaster();

    for (int xx = 0; xx < width; xx++) {
        for (int yy = 0; yy < height; yy++) {
            int[] pixels = raster.getPixel(xx, yy, (int[]) null);
            pixels[0] = 0;/*from  w w  w  .  j av  a2  s  .c o  m*/
            pixels[1] = 255;
            pixels[2] = 255;
            raster.setPixel(xx, yy, pixels);
        }
    }
    return image;
}

From source file:Main.java

static BufferedImage average(BufferedImage[] images) {
    BufferedImage average = new BufferedImage(images[0].getWidth(), images[0].getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);
    WritableRaster raster = average.getRaster().createCompatibleWritableRaster();
    for (int k = 0; k < images[0].getHeight(); ++k) {
        for (int j = 0; j < images[0].getWidth(); ++j) {
            float sum = 0.0f;
            for (int i = 0; i < images.length; ++i) {
                sum = sum + images[i].getRaster().getSample(j, k, 0);
            }/*from w  w  w . j a  v a2s.  c o m*/
            raster.setSample(j, k, 0, Math.round(sum / images.length));
        }
    }
    average.setData(raster);
    return average;
}

From source file:Main.java

/**
 * Quickly copies an image.//from w w  w  . j a va2  s  . com
 * @param src The source image.
 * @return The replicated image.
 */
public static BufferedImage imgUtilFastCopy(BufferedImage src) {
    if (src == null)
        return null;
    BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
    b.setData(src.getRaster());
    return b;
}

From source file:Main.java

/**
 * Trims the transparent pixels from the given {@link BufferedImage} (returns a sub-image).
 *
 * @param source The source image.//from  w  ww  . j  a v  a2s  .c  om
 * @return A new, trimmed image, or the source image if no trim is performed.
 */
public static BufferedImage trimmedImage(BufferedImage source) {
    final int minAlpha = 1;
    final int srcWidth = source.getWidth();
    final int srcHeight = source.getHeight();
    Raster raster = source.getRaster();
    int l = srcWidth, t = srcHeight, r = 0, b = 0;

    int alpha, x, y;
    int[] pixel = new int[4];
    for (y = 0; y < srcHeight; y++) {
        for (x = 0; x < srcWidth; x++) {
            raster.getPixel(x, y, pixel);
            alpha = pixel[3];
            if (alpha >= minAlpha) {
                l = Math.min(x, l);
                t = Math.min(y, t);
                r = Math.max(x, r);
                b = Math.max(y, b);
            }
        }
    }

    if (l > r || t > b) {
        // No pixels, couldn't trim
        return source;
    }

    return source.getSubimage(l, t, r - l + 1, b - t + 1);
}

From source file:arlocros.Imshow.java

/**
 * @param opencvImage/*from  ww  w  .  ja v a 2  s. c om*/
 */
public static void show(Mat opencvImage) {

    Dimension frameSize = new Dimension(opencvImage.rows(), opencvImage.cols());
    if (frame == null) {
        frame = new Imshow("", frameSize.height, frameSize.width);
        frame.Window.setVisible(true);

        frame.Window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        if (frame.SizeCustom) {
            Imgproc.resize(opencvImage, opencvImage, new Size(frame.Height, frame.Width));
        }
    }
    BufferedImage bufImage = null;
    try {

        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (opencvImage.channels() > 1) {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = opencvImage.channels() * opencvImage.cols() * opencvImage.rows();
        byte[] b = new byte[bufferSize];
        opencvImage.get(0, 0, b);
        BufferedImage bufferedImage = new BufferedImage(opencvImage.cols(), opencvImage.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length);
        bufImage = bufferedImage;
        frame.image.setImage(bufImage);
        frame.Window.pack();
        frame.label.updateUI();
        //frame.Window.setVisible(true);
    } catch (RuntimeException e) {
        logger.info("Exception while visualizing.", e);
    }
}

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

/**
 * Loads an image from a URL/*www  .j a  v a 2s .c o  m*/
 * @param url the URL to the image
 * @return the bitmap as BufferedImage
 * TODO This method doesn't close the InputStream opened by the URL.
 */
public static BufferedImage getImage(URL url) {
    ImageTagRegistry reg = ImageTagRegistry.getRegistry();
    Filter filt = reg.readURL(new ParsedURL(url));
    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;
}

From source file:com.thalespf.dip.DeblurringTest.java

private static void deblurTest() throws IOException {
    String imagePath = "image_blurred.png";
    IImageIO imageIO = ImageIODesktop.createImageIO(imagePath);
    Object imageObject = imageIO.getImageObject();
    if (imageObject == null || !(imageObject instanceof BufferedImage)) {
        throw new IllegalStateException("Nao foi possivel criar a imagem.");
    }//  w  w  w.ja  v  a  2  s . co  m

    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[] complexImage = 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]);
            complexImage[x + y * width] = c;

        }
    }

    normalizeToMaxAbsValue(complexImage, 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);

    //deconvolve data
    double[] convoluted = new double[2 * width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            Complex complexImg = complexImage[x + y * width];

            Complex complexDeg = complexPsf[x + y * width];

            Complex m = deconvolutionByWiener(complexImg, 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_deblurred");
}