Example usage for java.awt.image PixelGrabber PixelGrabber

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

Introduction

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

Prototype

public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix, int off, int scansize) 

Source Link

Document

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the image produced by the specified ImageProducer into the given array.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

    Graphics g = img.getGraphics();
    g.setColor(Color.red);//from   w ww  . ja  v a 2 s .co  m
    g.setFont(new Font("Arial", Font.BOLD, 14));
    g.drawString("Reference", 10, 80);

    int w = 100;
    int h = 100;
    int x = 1;
    int y = 1;

    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);

    pg.grabPixels();
    BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            bimg.setRGB(x + i, y + j, pixels[j * w + i]);
        }
    }

    FileOutputStream fout = new FileOutputStream("jpg.jpg");

    JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(fout);
    JPEGEncodeParam enParam = jencoder.getDefaultJPEGEncodeParam(bimg);

    enParam.setQuality(1.0F, true);
    jencoder.setJPEGEncodeParam(enParam);
    jencoder.encode(bimg);

    fout.close();

}

From source file:Util.java

/**
 * Converts a java.awt.Image into an array of pixels
 *///from w  w  w. java 2  s.  co m
public static int[] convertToPixels(Image img) {
    int width = img.getWidth(null);
    int height = img.getHeight(null);
    int[] pixel = new int[width * height];

    PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        throw new IllegalStateException("Error: Interrupted Waiting for Pixels");
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        throw new IllegalStateException("Error: Image Fetch Aborted");
    }
    return pixel;
}

From source file:Main.java

private static int[] makeGradientPallet() {
    BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    Point2D start = new Point2D.Float(0f, 0f);
    Point2D end = new Point2D.Float(99f, 0f);
    float[] dist = { 0.0f, 0.5f, 1.0f };
    Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
    g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
    g2.fillRect(0, 0, 100, 1);//w w w. j  av a  2s.c  om
    g2.dispose();

    int width = image.getWidth(null);
    int[] pallet = new int[width];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
    try {
        pg.grabPixels();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pallet;
}

From source file:MainClass.java

public void init() {
    MediaTracker mt = new MediaTracker(this);
    i = getImage(getDocumentBase(), "ora-icon.gif");
    mt.addImage(i, 0);//from  ww w . ja  va2 s. c om
    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:net.imglib2.script.analysis.ChartUtils.java

public static final Img<ARGBType> asImage(final JFreeChart chart, int width, int height) {
    final ChartPanel panel = new ChartPanel(chart);
    final Dimension d = panel.getPreferredSize();
    if (-1 == width && -1 == height) {
        width = d.width;//from   w w  w  .  j  a  va2 s.  c  om
        height = d.height;
        panel.setSize(d);
    } else {
        panel.setSize(width, height);
    }
    layoutComponent(panel);
    final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = bi.createGraphics();
    if (!panel.isOpaque()) {
        g.setColor(panel.getBackground());
        g.fillRect(0, 0, width, height);
    }
    panel.paint(g);
    final int[] pixels = new int[width * height];
    final PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
    try {
        pg.grabPixels();
    } catch (final InterruptedException e) {
    }
    g.dispose();

    final ArrayImg<ARGBType, IntArray> a = new ArrayImg<ARGBType, IntArray>(new IntArray(pixels),
            new long[] { width, height }, 1);

    // create a Type that is linked to the container
    final ARGBType linkedType = new ARGBType(a);
    // pass it to the DirectAccessContainer
    a.setLinkedType(linkedType);

    return a;
}

From source file:script.imglib.analysis.ChartUtils.java

public static final Image<RGBALegacyType> asImage(final JFreeChart chart, int width, int height) {
    ChartPanel panel = new ChartPanel(chart);
    Dimension d = panel.getPreferredSize();
    if (-1 == width && -1 == height) {
        width = d.width;/*from   w  ww  .  j  a v a  2  s  .co  m*/
        height = d.height;
        panel.setSize(d);
    } else {
        panel.setSize(width, height);
    }
    layoutComponent(panel);
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    if (!panel.isOpaque()) {
        g.setColor(panel.getBackground());
        g.fillRect(0, 0, width, height);
    }
    panel.paint(g);
    int[] pixels = new int[width * height];
    PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }
    g.dispose();

    Array<RGBALegacyType, IntAccess> a = new Array<RGBALegacyType, IntAccess>(new ArrayContainerFactory(),
            new IntArray(pixels), new int[] { width, height }, 1);
    // create a Type that is linked to the container
    final RGBALegacyType linkedType = new RGBALegacyType(a);
    // pass it to the DirectAccessContainer
    a.setLinkedType(linkedType);

    return new Image<RGBALegacyType>(a, new RGBALegacyType());
}

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;
                }/*from   ww  w .j  av a2s.c  om*/
            }
        }
    } 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:no.geosoft.cc.io.GifEncoder.java

/**
 * Constructing a GIF encoder.//from   www.  j a va 2  s  . co  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

/**
 * Construct a GIFEncoder. The constructor will convert the image to
 * an indexed color array. <B>This may take some time.</B><P>
 * //from   ww w . j  a v  a 2  s. c o  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:com.greenline.guahao.biz.manager.image.codes.gif.GifEncoder.java

/**
 * Convenience constructor for class <CODE>GIFEncoder</CODE>. The argument
 * will be converted to an indexed color array. <B>This may take some
 * time.</B>//from  w ww. j ava 2 s  .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 {
    this.imageWidth = (short) image.getWidth(null);
    this.imageHeight = (short) image.getHeight(null);

    int values[] = new int[this.imageWidth * this.imageHeight];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, this.imageWidth, this.imageHeight, values, 0,
            this.imageWidth);

    try {
        if (grabber.grabPixels() != true) {
            log.error("GifEncoder#GifEncoder Grabber returned false: " + grabber.status());
            throw new AWTException("Grabber returned false: " + grabber.status());
        }
    } // ends try
    catch (InterruptedException ie) {
        log.error("GifEncoder#GifEncoder " + ie.getMessage(), ie);
    }

    byte[][] r = new byte[this.imageWidth][this.imageHeight];
    byte[][] g = new byte[this.imageWidth][this.imageHeight];
    byte[][] b = new byte[this.imageWidth][this.imageHeight];
    int index = 0;

    for (int y = 0; y < this.imageHeight; y++) {
        for (int x = 0; x < this.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]) & 0xFF);
        } // ends for

    } // ends for

    this.toIndexColor(r, g, b);
}