Returns an array of pixels, stored as integers, from a BufferedImage. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Pixel

Description

Returns an array of pixels, stored as integers, from a BufferedImage.

Demo Code

/*//from   w  w w . jav a  2 s  .  c  om
 * @(#)EffectUtils.java   1.2 07/12/12
 *
 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
//package com.java2s;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;

public class Main {
    /**
     * <p>
     * Returns an array of pixels, stored as integers, from a
     * <code>BufferedImage</code>. The pixels are grabbed from a rectangular
     * area defined by a location and two dimensions. Calling this method on an
     * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and
     * <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.
     * </p>
     * 
     * @param img
     *            the source image
     * @param x
     *            the x location at which to start grabbing pixels
     * @param y
     *            the y location at which to start grabbing pixels
     * @param w
     *            the width of the rectangle of pixels to grab
     * @param h
     *            the height of the rectangle of pixels to grab
     * @param pixels
     *            a pre-allocated array of pixels of size w*h; can be null
     * @return <code>pixels</code> if non-null, a new array of integers
     *         otherwise
     * @throws IllegalArgumentException
     *             is <code>pixels</code> is non-null and of length &lt; w*h
     */
    static byte[] getPixels(BufferedImage img, int x, int y, int w, int h,
            byte[] pixels) {
        if (w == 0 || h == 0) {
            return new byte[0];
        }

        if (pixels == null) {
            pixels = new byte[w * h];
        } else if (pixels.length < w * h) {
            throw new IllegalArgumentException(
                    "pixels array must have a length >= w*h");
        }

        int imageType = img.getType();
        if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
            Raster raster = img.getRaster();
            return (byte[]) raster.getDataElements(x, y, w, h, pixels);
        } else {
            throw new IllegalArgumentException(
                    "Only type BYTE_GRAY is supported");
        }
    }

    /**
     * <p>
     * Returns an array of pixels, stored as integers, from a
     * <code>BufferedImage</code>. The pixels are grabbed from a rectangular
     * area defined by a location and two dimensions. Calling this method on an
     * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and
     * <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.
     * </p>
     * 
     * @param img
     *            the source image
     * @param x
     *            the x location at which to start grabbing pixels
     * @param y
     *            the y location at which to start grabbing pixels
     * @param w
     *            the width of the rectangle of pixels to grab
     * @param h
     *            the height of the rectangle of pixels to grab
     * @param pixels
     *            a pre-allocated array of pixels of size w*h; can be null
     * @return <code>pixels</code> if non-null, a new array of integers
     *         otherwise
     * @throws IllegalArgumentException
     *             is <code>pixels</code> is non-null and of length &lt; w*h
     */
    public static int[] getPixels(BufferedImage img, int x, int y, int w,
            int h, int[] pixels) {
        if (w == 0 || h == 0) {
            return new int[0];
        }

        if (pixels == null) {
            pixels = new int[w * h];
        } else if (pixels.length < w * h) {
            throw new IllegalArgumentException(
                    "pixels array must have a length" + " >= w*h");
        }

        int imageType = img.getType();
        if (imageType == BufferedImage.TYPE_INT_ARGB
                || imageType == BufferedImage.TYPE_INT_RGB) {
            Raster raster = img.getRaster();
            return (int[]) raster.getDataElements(x, y, w, h, pixels);
        }

        // Unmanages the image
        return img.getRGB(x, y, w, h, pixels, 0, w);
    }
}

Related Tutorials