/** ImageSaverJAI Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.jai.imageio;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.image.RenderedImage;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
//import com.sun.media.jai.codec.*;
import java.awt.image.renderable.*;
import javax.imageio.*;

public class ImageSaverJAI {
   public static void saveAsBMP(RenderedImage image, String file)
      throws java.io.IOException {
      String filename = file;
      if(!filename.endsWith(".bmp"))filename = new String(file+".bmp");
      OutputStream out = new FileOutputStream(filename);
      BMPEncodeParam param = new BMPEncodeParam();
      ImageEncoder encoder = ImageCodec.createImageEncoder("BMP", out, param);
      encoder.encode(image);
      out.close();
   }

   public static void saveAsTIFF(RenderedImage image, String file )
      throws java.io.IOException{
      String filename = file;
      if(!filename.endsWith(".tiff"))filename = new String(file+".tiff");
      OutputStream out = new FileOutputStream(filename);
      TIFFEncodeParam param = new TIFFEncodeParam();
      ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out, param);
      encoder.encode(image);
      out.close();
   }

   public static void saveAsPNGRGB(RenderedImage image, String file)
      throws java.io.IOException, IIOException {
      String filename = file;
      if(!filename.endsWith(".png"))filename = new String(file+".png");
      OutputStream out = new FileOutputStream(filename);
      //PNGEncodeParam.RGB param = new PNGEncodeParam.RGB();
      //ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out, param);
      //encoder.encode(image);
      ImageIO.write(image, "png", out);
      out.close();
   }

   public static void saveAsPNGGray(RenderedImage image, String file)
          throws java.io.IOException {
      String filename = file;
      if(!filename.endsWith(".png"))filename = new String(file+".png");
      OutputStream out = new FileOutputStream(filename);
      PNGEncodeParam.Gray param = new PNGEncodeParam.Gray();
      ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out, param);
      encoder.encode(image);
      out.close();
   }

   public static void saveAsPNGPalette(RenderedImage image, String file)
      throws java.io.IOException, IIOException {
          String filename = file;
      if(!filename.endsWith(".png"))filename = new String(file+".png");
      OutputStream out = new FileOutputStream(filename);
      //PNGEncodeParam.Palette param = new PNGEncodeParam.Palette();
      //ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out, param);
      //encoder.encode(image);
      ImageIO.write(image, "png", out);
      out.close();
   }

   public static void saveAsJPEG(RenderedImage image, String file)
           throws java.io.IOException {
      String filename = file;
      if(!filename.endsWith(".jpg"))filename = new String(file+".jpg");
      OutputStream out = new FileOutputStream(filename);
      //JPEGEncodeParam param = new JPEGEncodeParam();
      //ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", out, param);
      //encoder.encode(image);
      ImageIO.write(image, "jpg", out);
      out.close();
   }

   public static void saveAsPNM(RenderedImage image, String file)
      throws java.io.IOException, IIOException {
      String filename = file;
      if(!filename.endsWith(".pnm"))filename = new String(file+".pnm");
      OutputStream out = new FileOutputStream(filename);
      //PNMEncodeParam param = new PNMEncodeParam();
      //ImageEncoder encoder = ImageCodec.createImageEncoder("PNM", out, param);
      //encoder.encode(image);
      ImageIO.write(image, "pnm", out);
      out.close();
  }

   public static void saveAsPNM(RenderedImage image, String file, boolean raw)
      throws java.io.IOException {
      String filename = file;
      if(!filename.endsWith(".pnm"))filename = new String(file+".pnm");
      PNMEncodeParam param = new PNMEncodeParam();
      param.setRaw(raw);
      ParameterBlock pb =  new ParameterBlock();
      pb.addSource(image);
      pb.add(filename);
      pb.add("pnm");
      pb.add(param);
      JAI.create("filestore", pb);
   }
}
