Search code examples
javabufferedimagedpi

Reliable way to check if image is Grey scale


I am currently working on one use case where i need to determine if uploaded image is Grey Scale or RGB. I found couple of ways to identify this, but not sure if they are reliable and can be used collectively to confirm image is grey scale or not.

Part 1: Read Image and get NumberDataElements using Raster.

BufferedImage image = ImageIO.read(file);
        Raster ras = image.getRaster();
        int elem = ras.getNumDataElements();

I observed value of elem is "1" in some cases, but not in all.

Part 2: Check RGB value of each pixel. If R , G, B value is same of given pixel.

BufferedImage image = ImageIO.read(file);
        Raster ras = image.getRaster();

        //Number of Color elements
        int elem = ras.getNumDataElements();

        int width = image.getWidth();
        int height = image.getHeight();

        int pixel,red, green, blue;

        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++) {
                //scan through each pixel
                pixel = image.getRGB(i, j);
                red = (pixel >> 16) & 0xff;
                green = (pixel >> 8) & 0xff;
                blue = (pixel) & 0xff;

                //check if R=G=B
                if (red != green || green != blue ) {
                    flag = true;
                    break;
                }


            }

Here i check R, G,B values are same for any given pixel and this behavior is consistent across all pixels.

I am using these 2 approaches, but not sure how accurate they are. Kindly suggest..


Solution

  • Below approach is worked for me. Thanks guys for help.

    BufferedImage image = ImageIO.read(file);
            Raster ras = image.getRaster();
    
            //Number of Color elements
            int elem = ras.getNumDataElements();
    
            int width = image.getWidth();
            int height = image.getHeight();
    
            int pixel,red, green, blue;
    
            for (int i = 0; i < width; i++)
                for (int j = 0; j < height; j++) {
                    //scan through each pixel
                    pixel = image.getRGB(i, j);
                    red = (pixel >> 16) & 0xff;
                    green = (pixel >> 8) & 0xff;
                    blue = (pixel) & 0xff;
    
                    //check if R=G=B
                    if (red != green || green != blue ) {
                        flag = true;
                        break;
                    }
    
    
                }