Java BufferedImage Histogram histogram(BufferedImage img, boolean gray)

Here you can find the source of histogram(BufferedImage img, boolean gray)

Description

Generates a histogram for each of the R, G and B channels.

License

Open Source License

Parameter

Parameter Description
img the image to analyze
gray whether to use (A)RGB or grayscale

Return

the histogram, if ARGB then 0 = R, 1 = G, 2 = B, 3 = A or 0 = gray in case of grayscale

Declaration

public static int[][] histogram(BufferedImage img, boolean gray) 

Method Source Code

//package com.java2s;
/*/*  w ww.ja  va 2 s .  co  m*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class Main {
    /**
     * Generates a histogram for each of the R, G and B channels.
     * 
     * @param img      the image to analyze
     * @param gray   whether to use (A)RGB or grayscale
     * @return      the histogram, if ARGB then 0 = R, 1 = G, 2 = B, 3 = A
     *          or 0 = gray in case of grayscale
     */
    public static int[][] histogram(BufferedImage img, boolean gray) {
        int[][] result;
        int width;
        int height;
        int x;
        int y;
        int[] split;
        int i;

        if (gray) {
            result = new int[1][256];
            img = convert(img, BufferedImage.TYPE_BYTE_GRAY);
            height = img.getHeight();
            width = img.getWidth();
            split = new int[1];

            for (y = 0; y < height; y++) {
                for (x = 0; x < width; x++) {
                    split[0] = (img.getRGB(x, y) >> 8) & 0xFF;
                    result[0][split[0]]++;
                }
            }
        } else {
            result = new int[4][256];
            img = convert(img, BufferedImage.TYPE_4BYTE_ABGR);
            height = img.getHeight();
            width = img.getWidth();

            for (y = 0; y < height; y++) {
                for (x = 0; x < width; x++) {
                    split = split(img.getRGB(x, y));
                    for (i = 0; i < split.length; i++)
                        result[i][split[i]]++;
                }
            }
        }

        return result;
    }

    /**
     * Converts the image, if necessary to the specified type.
     * 
     * @param img      the image to convert
     * @param type   the required type
     * @return      the (potentially) converted image
     */
    public static BufferedImage convert(BufferedImage img, int type) {
        BufferedImage result;
        Graphics2D g2d;

        if (img.getType() != type) {
            result = new BufferedImage(img.getWidth(), img.getHeight(), type);
            g2d = result.createGraphics();
            g2d.drawImage(img, 0, 0, null);
            g2d.dispose();
        } else {
            result = img;
        }

        return result;
    }

    /**
     * Splits the RGBA value into R,G,B,A.
     * 
     * @param pixel   the RGB value to split
     * @return      the array with R,G,B,A
     */
    public static int[] split(int pixel) {
        int[] result;

        result = new int[4];
        result[0] = (pixel >> 16) & 0xFF; // R
        result[1] = (pixel >> 8) & 0xFF; // G
        result[2] = (pixel >> 0) & 0xFF; // B
        result[3] = (pixel >> 24) & 0xFF; // A

        return result;
    }
}

Related

  1. generateBWHistogram(BufferedImage bitmap, int bucketSize)
  2. histogram(BufferedImage bufferedImage)
  3. imageHistogram(BufferedImage input)
  4. imageHistogram(BufferedImage input)
  5. sectorHistorgramCompare(BufferedImage a, BufferedImage b)