Search in sources :

Example 11 with PixelGrabber

use of java.awt.image.PixelGrabber in project Lucee by lucee.

the class ImageCombiningFilter method filter.

public ImageProducer filter(Image image1, Image image2, int x, int y, int w, int h) {
    int[] pixels1 = new int[w * h];
    int[] pixels2 = new int[w * h];
    int[] pixels3 = new int[w * h];
    PixelGrabber pg1 = new PixelGrabber(image1, x, y, w, h, pixels1, 0, w);
    PixelGrabber pg2 = new PixelGrabber(image2, x, y, w, h, pixels2, 0, w);
    try {
        pg1.grabPixels();
        pg2.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        return null;
    }
    if ((pg1.status() & ImageObserver.ABORT) != 0) {
        System.err.println("image fetch aborted or errored");
        return null;
    }
    if ((pg2.status() & ImageObserver.ABORT) != 0) {
        System.err.println("image fetch aborted or errored");
        return null;
    }
    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            int k = j * w + i;
            pixels3[k] = filterRGB(x + i, y + j, pixels1[k], pixels2[k]);
        }
    }
    return new MemoryImageSource(w, h, pixels3, 0, w);
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) MemoryImageSource(java.awt.image.MemoryImageSource)

Example 12 with PixelGrabber

use of java.awt.image.PixelGrabber in project Lucee by lucee.

the class ImageUtils method createImage.

/**
 * Cretae a BufferedImage from an ImageProducer.
 * @param producer the ImageProducer
 * @return a new TYPE_INT_ARGB BufferedImage
 */
public static BufferedImage createImage(ImageProducer producer) {
    PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        throw new RuntimeException("Image fetch interrupted");
    }
    if ((pg.status() & ImageObserver.ABORT) != 0)
        throw new RuntimeException("Image fetch aborted");
    if ((pg.status() & ImageObserver.ERROR) != 0)
        throw new RuntimeException("Image fetch error");
    BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB);
    p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[]) pg.getPixels(), 0, pg.getWidth());
    return p;
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) BufferedImage(java.awt.image.BufferedImage)

Aggregations

PixelGrabber (java.awt.image.PixelGrabber)12 BufferedImage (java.awt.image.BufferedImage)6 Image (java.awt.Image)3 IOException (java.io.IOException)3 Color (java.awt.Color)2 ColorModel (java.awt.image.ColorModel)2 DirectColorModel (java.awt.image.DirectColorModel)2 IndexColorModel (java.awt.image.IndexColorModel)2 MemoryImageSource (java.awt.image.MemoryImageSource)2 AWTException (java.awt.AWTException)1 Dimension (java.awt.Dimension)1 MediaTracker (java.awt.MediaTracker)1 ComponentColorModel (java.awt.image.ComponentColorModel)1 PackedColorModel (java.awt.image.PackedColorModel)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DatagramPacket (java.net.DatagramPacket)1 JTextArea (javax.swing.JTextArea)1 Nullable (org.jetbrains.annotations.Nullable)1