/** The GrayscaleImageFactory class creates grayscale images.
    * @version 1.0  18 Feb 2000
    * @author Lawrence Rodrigues
    **/
package com.vistech.util;
import java.awt.*;
import java.awt.image.*;
import java.awt.color.*;
import java.util.*;

public class GrayscaleImageFactory {
   short pixels[];
   BufferedImage grayscaleImage;
   int imageWidth =256, imageHeight = 256;
   int imageDepth = 8; 
   Object data;
   public GrayscaleImageFactory() {


   }

    public GrayscaleImageFactory(int width, int height, int depth, short data[]) {
       this.imageWidth = width;
       this.imageHeight = height;
       this.imageDepth = depth;
       this.data = data;
       grayscaleImage = createImage();
   }
   public void setImageDimension(int width, int height) {
      this.imageWidth = width;
      this.imageHeight = height;
   }

   public void setImageDepth(int depth){
      this.imageDepth = depth;
   }
  
   public void setData(short data[]){
      this.data = data;
      grayscaleImage = createImage();
   }

   public BufferedImage  getGrayscaleImage() {
      return grayscaleImage;

   }

   public BufferedImage createImage() {
      ComponentColorModel ccm = new ComponentColorModel(
                          ColorSpace.getInstance(ColorSpace.CS_GRAY),
                          new int[] {imageDepth}, false, false,
                          Transparency.OPAQUE,
                          DataBuffer.TYPE_USHORT);
      ComponentSampleModel csm = new ComponentSampleModel(
                                    DataBuffer.TYPE_USHORT,
                                    imageWidth, imageHeight,1,imageWidth, new int[] {0});
      DataBuffer dataBuf = new DataBufferUShort((short[])data, imageWidth);
      WritableRaster wr = Raster.createWritableRaster(csm, dataBuf, new Point(0,0));
      Hashtable ht = new Hashtable();
      ht.put("owner", "Lawrence Rodrigues");

      return  new BufferedImage(ccm, wr, true, ht);
   }

   public static BufferedImage createImage(int imageWidth, int imageHeight, int imageDepth, short data[]){
      ComponentColorModel ccm = new ComponentColorModel(
                          ColorSpace.getInstance(ColorSpace.CS_GRAY),
                          new int[] {imageDepth}, false, false,
                          Transparency.OPAQUE,
                          DataBuffer.TYPE_USHORT);
      ComponentSampleModel csm = new ComponentSampleModel(
                                    DataBuffer.TYPE_USHORT,
                                    imageWidth, imageHeight,1,imageWidth, new int[] {0});
      DataBuffer dataBuf = new DataBufferUShort((short[])data, imageWidth);
      WritableRaster wr = Raster.createWritableRaster(csm, dataBuf, new Point(0,0));
      Hashtable ht = new Hashtable();
      ht.put("owner", "Lawrence Rodrigues");
      return  new BufferedImage(ccm, wr, true, ht);
   }

    public static BufferedImage createBandedImage(int imageWidth, int imageHeight, int imageDepth, short data[][]){
      ComponentColorModel ccm = new ComponentColorModel(
                          ColorSpace.getInstance(ColorSpace.CS_sRGB),
                          new int[] {8,8,8}, false, false,
                          Transparency.OPAQUE,
                          DataBuffer.TYPE_USHORT);
      BandedSampleModel csm = new BandedSampleModel(
                                    DataBuffer.TYPE_USHORT,
                                    imageWidth, imageHeight,3); //,imageWidth, new int[] {0,1,2});
      DataBuffer dataBuf = new DataBufferUShort((short[][])data, imageWidth);
      WritableRaster wr = Raster.createWritableRaster(csm, dataBuf, new Point(0,0));
      Hashtable ht = new Hashtable();
      ht.put("owner", "Lawrence Rodrigues");
      return  new BufferedImage(ccm, wr, true, ht);
   }
   
   public static void  main(String[] args){
      GrayscaleImageFactory gs = new GrayscaleImageFactory();
      int wid = 64, ht =64;
      short data[] = new short[wid*ht];
      for(int i=0; i<ht; i++){
          for(int j=0; j<wid; j++){
              data[i*ht+j] = (short)j;
          }
      }
      gs.setImageDimension(wid,ht);
      gs.setData(data);
      BufferedImage bi = gs.createImage();

      wid = 64; ht =64;
      short data1[][] = new short[3][wid*ht];
      for(int k=0;k<3;k++) {
      for(int i=0; i<ht; i++){
          for(int j=0; j<wid; j++){
                data1[k][i*ht+j] = (short)(j*k);
          }
       }
      }
      BufferedImage bi1 = gs.createBandedImage(64,64,8,data1);
      if(bi1 != null) JpegUtil.saveImageAsJPEG(bi1, "rgb.jpg");
   }
}
