
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class ImageStats implements ImageLoadedListener{
    static final int RED =  1;
    static final int GREEN =  2;
    static final int BLUE = 3;
    static final int ALPHA = 4;

    public static void main(String argv[]){
       if(argv.length == 0) return;
       ImageStats stats = new ImageStats();
       for (int i=0;i<argv.length;i++) {
            stats.loadImage(argv[i]);
       }
       System.exit(0);
   }

    public void loadImage(String image) {
       File img= new File(image);
       ImageLoader im = new ImageLoader();
       im.setFileInfo(".", image, 0);
       im.load();
       loadPixels(im);
    }

    public void loadPixels(ImageLoader il){
        il.addImageLoadedListener(this); 
        try{
           Thread.sleep(100);
        } catch(InterruptedException e){}
        il.fetchPixels();
    }

        
     public synchronized void imageLoaded(ImageLoadedEvent e){
        int status = e.getLoadStatus();
        ImageLoader il = (ImageLoader)e.getSource();
        ImageBuffer img = (ImageBuffer)il.getImageBuffer();
        if((status & ImageLoadedListener.PIXELS_AVAILABLE) != 0){
           generateAndDisplayStats(img);
           il.removeImageLoadedListener(this);
        }
    }

    private void generateAndDisplayStats(ImageBuffer img){
        int[] stats = computeStats(img.origPixels,RED);  
        System.out.println("-------------------------------");
        System.out.println("Color \tMin \tMax \tAverage");
        System.out.println("-------------------------------");
        System.out.print("Red");
            
        System.out.print("\t"+Integer.toString(stats[0]));
        System.out.print("\t"+Integer.toString(stats[1]));
        System.out.println("\t"+Integer.toString(stats[2]));
        
        stats = computeStats(img.origPixels,GREEN); 
        System.out.print("Green");     
        System.out.print("\t"+Integer.toString(stats[0]));
        System.out.print("\t"+Integer.toString(stats[1]));
        System.out.println("\t"+Integer.toString(stats[2]));

        stats = computeStats(img.origPixels,BLUE); 
        System.out.print("Blue");
             
        System.out.print("\t"+Integer.toString(stats[0]));
        System.out.print("\t"+Integer.toString(stats[1]));
        System.out.print("\t"+Integer.toString(stats[2]));
        System.out.println();
    }

    public int[] computeStats(int inp[], int clr){
       if(inp == null) return null;
       int pix;
       int stats[] =  new int[3];
       int max = Integer.MIN_VALUE;
       int min = Integer.MAX_VALUE;
       ColorModel cm = ColorModel.getRGBdefault();
       int sum =0;
       for(int i=0; i<inp.length;i++){
           switch(clr){
               case RED:
                  pix = cm.getRed(inp[i]);
                  break;
               case GREEN:
                  pix = cm.getGreen(inp[i]);
                  break;
               case BLUE:
                  pix = cm.getBlue(inp[i]);
                  break;
               case ALPHA:
                  pix = cm.getAlpha(inp[i]);
                  break;
               default:
                  pix =0;
           } 
           if(pix > max) max = pix;
           if(pix < min ) min = pix;
           sum += pix;  
       }
       double average = sum/inp.length;
       stats[0] = min; stats[1] = max; stats[2] = (int)average;
       return stats;
    }

  }
