/** 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[] {imageDepth}, false, false,
                          Transparency.OPAQUE,
                          DataBuffer.TYPE_USHORT);
      ComponentSampleModel csm = new ComponentSampleModel(
                                    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[wid*ht*3];
      for(int i=0; i<ht; i++){
          for(int j=0; j<wid; j++){
              for(int k=0;k<3;k++)
              data1[i*ht+j*wid+k] = (short)j;
          }
      }
      BufferedImage bi = gs.createBandedImage(64,64,8,data1);
      if(bi != null) JpegUtil.saveImageAsJPEG(bi, "rgb.jpg"); */
   }
}
