Java BufferedImage Operation scrapeColors(BufferedImage image)

Here you can find the source of scrapeColors(BufferedImage image)

Description

Get's all of the unique colors in an image.

License

Open Source License

Parameter

Parameter Description
image The image.

Declaration

public static byte[][] scrapeColors(BufferedImage image) 

Method Source Code


//package com.java2s;
/*/*www .  ja v a2s. c  o m*/
 * RHQ Management Platform
 * Copyright (C) 2005-2008 Red Hat, Inc.
 * All rights reserved.
 *
 * 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 version 2 of the License.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.awt.image.BufferedImage;

import java.awt.image.Raster;

public class Main {
    /**
     * Get's all of the unique colors in an image.
     *
     * @param image The image.
     */
    public static byte[][] scrapeColors(BufferedImage image) {
        int i;
        Raster raster = image.getRaster();
        byte[][] clrs = new byte[raster.getNumBands()][256];
        int[][] pixels = new int[raster.getNumBands()][];
        int nextClr = 0;

        for (int band = 0; band < raster.getNumBands(); band++) {
            pixels[band] = raster.getSamples(0, 0, raster.getWidth(), raster.getHeight(), band, (int[]) null);
        }

        for (int pixel = 0; pixel < pixels[0].length; pixel++) {
            byte red = 0;
            byte green = 0;
            byte blue = 0;

            // Add the clr if it doesn't already exist in the index
            for (i = 0; i < clrs[0].length; i++) {
                red = (byte) pixels[0][pixel];
                green = (byte) pixels[1][pixel];
                blue = (byte) pixels[2][pixel];

                if ((red == clrs[0][i]) && (green == clrs[1][i]) && (blue == clrs[2][i])) {
                    break;
                }
            }

            if ((i == clrs[0].length) && (nextClr < 256)) {
                clrs[0][nextClr] = red;
                clrs[1][nextClr] = green;
                clrs[2][nextClr++] = blue;

                //                System.out.println("{"+Integer.toHexString(red & 0x000000FF)+','+
                //                                   Integer.toHexString(green & 0x0000FF00 >> 8)+','+
                //                                   Integer.toHexString(blue & 0x00FF0000 >> 16)+'}');
            }
        }

        //        System.out.println("Colors: " + nextClr);

        return clrs;
    }
}

Related

  1. resampleWithAffineTransformOp(BufferedImage srcImage, double sx, double sy)
  2. revertBlackAndWhite(BufferedImage img)
  3. roate90(BufferedImage src)
  4. sample9Points(BufferedImage src, int x, int y)
  5. scanFill(BufferedImage sourceImage)
  6. setArrayAlpha(BufferedImage image, int[] array)
  7. setArrayColor(BufferedImage image, int[][] array)
  8. setBackgroud(BufferedImage image, Color backgroundColor)
  9. setBGRPixels(byte[] bgrPixels, BufferedImage img, int x, int y, int w, int h)