public static Spectrum readSpectrum(BufferedImage image, Rectangle area, int precision) throws IOException {

        if (precision < 8) throw new IllegalArgumentException("Color size should not be less then 8");
        if (precision > 256) throw new IllegalArgumentException("Color size should not be bigger then 256");

        int spectrum[][][] = new int[precision][precision][precision];

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

        int[] a = new int[width * height];

        image.getRGB(0, 0, width, height, a, 0, width);

        int spectrumWidth = width;
        int spectrumHeight = height;

        if (area == null) {
            area = new Rectangle(0, 0, width, height);
        }
        else {
            spectrumWidth = area.width;
            spectrumHeight = area.height;
        }

        int k = 0;
        int r,g,b;

        for (int y = area.y; y < area.y + area.height; y++) {
            for (int x = area.x; x < area.x + area.width; x++) {
                k = y * width + x;

                r = ((a[k] >> 16) & 0xff) * precision / 256;
                g = ((a[k] >> 8) & 0xff) * precision / 256;
                b = ((a[k]) & 0xff) * precision / 256;

                spectrum[Math.min(r, precision - 1)][Math.min(g, precision - 1)][Math.min(b, precision - 1)] += 1;
            }
        }

        return new Spectrum(spectrum, spectrumWidth, spectrumHeight);
    }
public static Spectrum readSpectrum ( BufferedImage image , Rectangle area , int precision ) throws IOException { if ( precision < 8 ) throw new IllegalArgumentException ( "Color size should not be less then 8" ) ; if ( precision > 256 ) throw new IllegalArgumentException ( "Color size should not be bigger then 256" ) ; int spectrum [ ] [ ] [ ] = new int [ precision ] [ precision ] [ precision ] ; int width = image . getWidth ( ) ; int height = image . getHeight ( ) ; int [ ] a = new int [ width * height ] ; image . getRGB ( 0 , 0 , width , height , a , 0 , width ) ; int spectrumWidth = width ; int spectrumHeight = height ; if ( area == null ) { area = new Rectangle ( 0 , 0 , width , height ) ; } else { spectrumWidth = area . width ; spectrumHeight = area . height ; } int k = 0 ; int r , g , b ; for ( int y = area . y ; y < area . y + area . height ; y ++ ) { for ( int x = area . x ; x < area . x + area . width ; x ++ ) { k = y * width + x ; r = ( ( a [ k ] > > 16 ) & 0xff ) * precision / 256 ; g = ( ( a [ k ] > > 8 ) & 0xff ) * precision / 256 ; b = ( ( a [ k ] ) & 0xff ) * precision / 256 ; spectrum [ Math . min ( r , precision - 1 ) ] [ Math . min ( g , precision - 1 ) ] [ Math . min ( b , precision - 1 ) ] += 1 ; } } return new Spectrum ( spectrum , spectrumWidth , spectrumHeight ) ; }
@param image an image for calculating the color spectrum
@param precision 8 to 256 value for spectrum accuracy. The bigger value - the better precision, but the more memory it takes
@return
@throws IOException