Here you can find the source of scrapeColors(BufferedImage image)
Parameter | Description |
---|---|
image | The image. |
public static byte[][] scrapeColors(BufferedImage image)
//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; } }