import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;

public class ScalableColor {
	
	public static HistogramSet getHistogram(BufferedImage input, int colorSpace) {		
		// preparing histogramSet for filling
		int length=360; // prepare for maximum value (HSV=360)
		int countHistogram=3; // three chrominant component
		double quantization=256;
		Vector vTemp = new Vector();
		vTemp.generate(countHistogram); // generate vector for three histograms
		HistogramSet hs = new HistogramSet();
		for (int i=0;i<countHistogram;i++) {
			Histogram h = new Histogram();
			for (int j=0;j<length;j++) {
				h.add(0);
			}
			hs.add(h);
		}
	 
	    for(int i=0; i<input.getWidth(); i++) { // get RGB or HSV histogram
	        for(int j=0; j<input.getHeight(); j++) {
	 
	            int red = new Color(input.getRGB (i, j)).getRed();
	            int green = new Color(input.getRGB (i, j)).getGreen();
	            int blue = new Color(input.getRGB (i, j)).getBlue();
	            vTemp.set((double) red, (double) green, (double) blue);
	            if (colorSpace==Main.HSV) {
		           vTemp.convertTo(Main.HSV);
	            }
	            if (colorSpace==Main.HSVDC) {
			           vTemp.convertTo(Main.HSV);
		            }
	            
	            for (int k=0;k<vTemp.getLength();k++) { //filling values to histogram
	            	hs.get(k).increase((int) vTemp.get(k)); // increase histogram value on color position and on specific - k - histogram
	            }          
	        } 
	    } // end of image
	    
	    if (colorSpace==Main.HSV) {
			
	    	for (int i=0;i<16;i++) { // normalization to 16 H bins according to mpeg-7
	    	    double bin = 0;
	    	    	for (int j=0;j<23;j++) { 
	    		    	if (i*j<360) {
	    		    		bin += hs.get(0).getValue(i*j);
	    		    	}	    		
	    		    }
	    	    hs.get(0).setValue(i, bin);
	    	    }
	    	    
	    	    for (int i=0;i<4;i++) { // normalization 4 S bins and 4 V bins according to mpeg-7
	    		    double binS = 0;
	    		    double binV = 0;
	    		    	for (int j=0;j<25;j++) { 
	    			    	binS += hs.get(1).getValue(i*j);
	    			    	binV += hs.get(2).getValue(i*j);
	    			    }
	    		    hs.get(1).setValue(i, binS);
	    		    hs.get(2).setValue(i, binV);
	    		    }
	    	    for (int k=0;k<hs.getLength();k++) { //normalization to quantized(16) values	           
	            	double max = Collections.max(hs.get(k).getHistogram());
	            	for (int m=0;m<hs.get(k).getLength();m++) {
	            		double temp = hs.get(k).getValue(m);
	            		temp = temp/max*16;
	            		hs.get(k).setValue(m, Math.round(temp));
	            	}
	            }
	    }
	    
		return hs;
	} // end of method
	
	public static HistogramSet HaarTransform (HistogramSet hs, int bitRes) {
		int max=bitRes/16;
		double sum=0;
		double diff = 0;
		for (int i=0;i<hs.getLength();i++) {
			for (int j=1;j<max;j++) {
				sum = hs.get(i).getValue(2*j-1) + hs.get(i).getValue(2*j);
				diff = Math.abs(hs.get(i).getValue(2*j-1) - hs.get(i).getValue(2*j));
				hs.get(i).setValue(j, sum);
			}
		}
		
		for (int k=0;k<hs.getLength();k++) { //normalization to quantized(16) values	           
        	double maxq = Collections.max(hs.get(k).getHistogram());
        	for (int m=0;m<hs.get(k).getLength();m++) {
        		double temp = hs.get(k).getValue(m);
        		temp = temp/maxq*16;
        		hs.get(k).setValue(m, Math.round(temp));
        	}
		}
		return hs;
	}

}
