Here you can find the source of isGrayscale(ImageIcon icon)
Parameter | Description |
---|---|
icon | the image icon |
public static boolean isGrayscale(ImageIcon icon)
//package com.java2s; // Distributed under the GNU Lesser General Public License import java.awt.image.*; import javax.swing.*; public class Main { /**/* w w w . j av a 2s .co m*/ * Determine whether the given image is grayscale * * @param rgb the RGB data * @return true if the image is grayscale */ public static boolean isGrayscale(int[] rgb) { for (int i = 0; i < rgb.length; i++) { int pixel = rgb[i]; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; if ((red != blue) || (red != green) || (blue != green)) return false; } return true; } /** * Determine whether the given image icon is grayscale * * @param icon the image icon * @return true if the image is grayscale */ public static boolean isGrayscale(ImageIcon icon) { return isGrayscale(grabRGB(icon)); } /** * Grabs a RGB values of the given image icon * * @param icon the image icon * @return an array of RGB values */ public static int[] grabRGB(ImageIcon icon) { int width = icon.getIconWidth(); int height = icon.getIconHeight(); int[] rgb = new int[width * height]; PixelGrabber pg = new PixelGrabber(icon.getImage(), 0, 0, width, height, rgb, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("Interrupted waiting for pixels!"); } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { throw new RuntimeException("Image fetch aborted or errored"); } for (int i = 0; i < rgb.length; i++) rgb[i] &= 0xffffff; return rgb; } }