Example usage for java.awt.image PixelGrabber grabPixels

List of usage examples for java.awt.image PixelGrabber grabPixels

Introduction

In this page you can find the example usage for java.awt.image PixelGrabber grabPixels.

Prototype

public boolean grabPixels() throws InterruptedException 

Source Link

Document

Request the Image or ImageProducer to start delivering pixels and wait for all of the pixels in the rectangle of interest to be delivered.

Usage

From source file:com.xebia.visualreview.PixelComparator.java

public static DiffReport processImage(File beforeFile, File afterFile, java.util.Map maskInfo,
        String compareSettings) {
    try {/*from www  .j  ava  2 s.co  m*/
        int precision = getPrecision(compareSettings);

        PixelGrabber beforeGrab = grabImage(beforeFile);
        PixelGrabber afterGrab = grabImage(afterFile);

        int[] beforeData = null;
        int[] afterData = null;
        int beforeWidth = 0;
        int afterWidth = 0;
        int y1 = 0;
        int y2 = 0;

        if (beforeGrab.grabPixels()) {
            beforeWidth = beforeGrab.getWidth();
            y1 = beforeGrab.getHeight();
            beforeData = (int[]) beforeGrab.getPixels();
        }

        if (afterGrab.grabPixels()) {
            afterWidth = afterGrab.getWidth();
            y2 = afterGrab.getHeight();
            afterData = (int[]) afterGrab.getPixels();
        }

        int minX = Math.min(beforeWidth, afterWidth);
        int minY = Math.min(y1, y2);
        int diffWidth = Math.max(beforeWidth, afterWidth);
        int diffHeight = Math.max(y1, y2);
        int[] diffData = new int[diffWidth * diffHeight];
        int differentPixels = 0;
        boolean hasMask = (maskInfo != null);
        BufferedImage maskImage = generateMask(maskInfo, diffWidth, diffHeight);
        for (int y = 0; y < diffHeight; y++) {
            for (int x = 0; x < diffWidth; x++) {
                if (maskImage.getRGB(x, y) != DIFFCOLOUR) {
                    if (x >= minX || y >= minY || hasDifference(beforeData[y * beforeWidth + x],
                            afterData[y * afterWidth + x], precision)) {
                        diffData[y * diffWidth + x] = DIFFCOLOUR;
                        differentPixels++;
                    }
                }
            }
        }

        BufferedImage diffImage = new BufferedImage(diffWidth, diffHeight, BufferedImage.TYPE_INT_ARGB);
        diffImage.setRGB(0, 0, diffWidth, diffHeight, diffData, 0, diffWidth);

        DiffReport report = new DiffReport(beforeFile, afterFile, diffImage, differentPixels);
        if (hasMask) {
            report.setMaskImage(maskImage);
        }
        return report;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:MainClass.java

public void init() {
    MediaTracker mt = new MediaTracker(this);
    i = getImage(getDocumentBase(), "ora-icon.gif");
    mt.addImage(i, 0);// w  ww  . j  a v a2s  . co  m
    try {
        mt.waitForAll();
        int width = i.getWidth(this);
        int height = i.getHeight(this);
        int pixels[] = new int[width * height];
        PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, pixels, 0, width);
        if (pg.grabPixels() && ((pg.status() & ImageObserver.ALLBITS) != 0)) {
            j = createImage(
                    new MemoryImageSource(width, height, rowFlipPixels(pixels, width, height), 0, width));
            k = createImage(
                    new MemoryImageSource(width, height, colFlipPixels(pixels, width, height), 0, width));
            l = createImage(
                    new MemoryImageSource(height, width, rot90Pixels(pixels, width, height), 0, height));
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:GrabandFadewithRasters.java

public void init() {
    URL url;//from www  .j  av a2  s.  co m
    try {
        url = new URL(imageURLString);
        originalImage = getImage(url);
    } catch (MalformedURLException me) {
        showStatus("Malformed URL: " + me.getMessage());
    }

    try {
        PixelGrabber grabber = new PixelGrabber(originalImage, 0, 0, -1, -1, true);
        if (grabber.grabPixels()) {
            width = grabber.getWidth();
            height = grabber.getHeight();
            originalPixelArray = (int[]) grabber.getPixels();

            mis = new MemoryImageSource(width, height, originalPixelArray, 0, width);
            mis.setAnimated(true);
            newImage = createImage(mis);
        } else {
            System.err.println("Grabbing Failed");
        }
    } catch (InterruptedException ie) {
        System.err.println("Pixel Grabbing Interrupted");
    }

    DataBufferInt dbi = new DataBufferInt(originalPixelArray, width * height);

    int bandmasks[] = { 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff };
    SampleModel sm;
    sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, bandmasks);

    raster = Raster.createWritableRaster(sm, dbi, null);
}

From source file:GrabandFade.java

public void init() {
    URL url;//from w w  w .  java2 s. co  m
    try {
        // set imageURLString here
        url = new URL(imageURLString);
        originalImage = getImage(url);
    } catch (MalformedURLException me) {
        showStatus("Malformed URL: " + me.getMessage());
    }

    /*
     * Create PixelGrabber and use it to fill originalPixelArray with image
     * pixel data. This array will then by used by the MemoryImageSource.
     */
    try {
        PixelGrabber grabber = new PixelGrabber(originalImage, 0, 0, -1, -1, true);
        if (grabber.grabPixels()) {
            width = grabber.getWidth();
            height = grabber.getHeight();
            originalPixelArray = (int[]) grabber.getPixels();

            mis = new MemoryImageSource(width, height, originalPixelArray, 0, width);
            mis.setAnimated(true);
            newImage = createImage(mis);
        } else {
            System.err.println("Grabbing Failed");
        }
    } catch (InterruptedException ie) {
        System.err.println("Pixel Grabbing Interrupted");
    }
}

From source file:com.jcraft.weirdx.TransparentFilter.java

TransparentFilter(int cx, int cy, XPixmap pixmap) {
    this.cx = cx;
    this.cy = cy;

    this.width = pixmap.width;
    this.height = pixmap.height;

    pixels = new byte[pixmap.width * pixmap.height];
    if (pixmap.data != null) {
        for (int i = 0; i < pixmap.height; i++) {
            for (int j = 0; j < pixmap.width; j++) {
                if (pixmap.data[i * pixmap.width + j] == 0) {
                    pixels[i * pixmap.width + j] = 0;
                } else {
                    pixels[i * pixmap.width + j] = 1;
                }/* w  w w .jav  a2  s.  co  m*/
            }
        }
    } else {
        int[] ipixels = new int[pixmap.width * pixmap.height];
        Image img = pixmap.img;
        PixelGrabber pg = null;
        pg = new PixelGrabber(img, 0, 0, pixmap.width, pixmap.height, ipixels, 0, pixmap.width);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            //System.err.println("interrupted waiting for pixels!");
            return;
        }

        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            LOG.error("image fetch aborted or errored");
            return;
        }

        for (int i = 0; i < pixmap.height; i++) {
            for (int j = 0; j < pixmap.width; j++) {
                if (ipixels[i * pixmap.width + j] == 0xff000000) {
                    pixels[i * pixmap.width + j] = 0;
                } else {
                    pixels[i * pixmap.width + j] = 1;
                }
            }
        }
        ipixels = null;
    }
}

From source file:net.pms.medialibrary.commons.helpers.FileImportHelper.java

private static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }/*from  w ww . j a  va2s . com*/

    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

From source file:com.aurel.track.attachment.AttachBL.java

private static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }//from   w ww. j  av  a  2 s. c  o m

    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

From source file:GIFEncoder.java

/**
 * Construct a GIFEncoder. The constructor will convert the image to
 * an indexed color array. <B>This may take some time.</B><P>
 * //from www. jav a2s . co m
 * @param image The image to encode. The image <B>must</B> be
 * completely loaded.
 * @exception AWTException Will be thrown if the pixel grab fails. This
 * can happen if Java runs out of memory. It may also indicate that the image
 * contains more than 256 colors.
 * */
public GIFEncoder(Image image) throws AWTException {
    width_ = (short) image.getWidth(null);
    height_ = (short) image.getHeight(null);

    int values[] = new int[width_ * height_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, width_, height_, values, 0, width_);

    try {
        if (grabber.grabPixels() != true)
            throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException e) {
        ;
    }

    byte r[][] = new byte[width_][height_];
    byte g[][] = new byte[width_][height_];
    byte b[][] = new byte[width_][height_];
    int index = 0;
    for (int y = 0; y < height_; ++y)
        for (int x = 0; x < width_; ++x) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index]) & 0xFF);
            ++index;
        }
    ToIndexedColor(r, g, b);
}

From source file:no.geosoft.cc.io.GifEncoder.java

/**
 * Constructing a GIF encoder.//from   w  w w .java 2  s .  c o m
 *
 * @param image  The image to encode. The image must be
 *               completely loaded.
 * @throws AWTException  If memory is exhausted or image contains
 *                       more than 256 colors.
 */
public GifEncoder(Image image) throws AWTException {
    imageWidth_ = (short) image.getWidth(null);
    imageHeight_ = (short) image.getHeight(null);

    int values[] = new int[imageWidth_ * imageHeight_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, imageWidth_, imageHeight_, values, 0, imageWidth_);

    try {
        if (grabber.grabPixels() != true)
            throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException exception) {
    }

    byte[][] r = new byte[imageWidth_][imageHeight_];
    byte[][] g = new byte[imageWidth_][imageHeight_];
    byte[][] b = new byte[imageWidth_][imageHeight_];

    int index = 0;

    for (int y = 0; y < imageHeight_; y++) {
        for (int x = 0; x < imageWidth_; x++, index++) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index] >> 0) & 0xFF);
        }
    }

    toIndexColor(r, g, b);
}

From source file:GifEncoder.java

/** Constructs a new GifEncoder using an 8-bit AWT Image.
 The image is assumed to be fully loaded. */
public GifEncoder(Image img) {
    width = img.getWidth(null);/*from  w  w  w  . ja v a  2  s.  co  m*/
    height = img.getHeight(null);
    pixels = new byte[width * height];
    PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println(e);
    }
    ColorModel cm = pg.getColorModel();
    if (cm instanceof IndexColorModel) {
        pixels = (byte[]) (pg.getPixels());
        // hpm
        IndexColorModel icm = (IndexColorModel) cm;
        setTransparentPixel(icm.getTransparentPixel());
    } else
        throw new IllegalArgumentException("IMAGE_ERROR");
    IndexColorModel m = (IndexColorModel) cm;
    int mapSize = m.getMapSize();
    r = new byte[mapSize];
    g = new byte[mapSize];
    b = new byte[mapSize];
    m.getReds(r);
    m.getGreens(g);
    m.getBlues(b);
    interlace = false;
    pixelIndex = 0;
    numPixels = width * height;
}