/*
 * @(#) JpegUtil.java 1.0 99/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
package com.vistech.util;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
 /** Provides utility methods for saving as JPEG image.
   * @version 1.0  10 April 1999
   * @author Lawrence Rodrigues
   */
public class JpegUtil {
   public static void saveComponentAsJPEG(Component cmp, Container cont, String jpegfile) {
       Rectangle d = cmp.getBounds();
       BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
       Graphics2D g2d = bi.createGraphics();
       SwingUtilities.paintComponent(g2d,cmp,cont, 0,0,d.width,d.height);
       saveImageAsJPEG(bi, jpegfile);
   }

   public static void restoreComponent(Component cmp, Container cont){
       cont.add(cmp);
       cont.validate();
   }

   public static String saveImageAsJPEG(BufferedImage bi,
                                      float quality) {
         String filename = launchFileDialog();
         saveImageAsJPEG(bi,quality,filename);
         return filename;
   }


   public static void saveImageAsJPEG(BufferedImage bi,
                                      float quality,
                                      String filename){
      try {
         ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
         JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(boutstream);
         JPEGEncodeParam enparam = JPEGCodec.getDefaultJPEGEncodeParam(bi);
         enparam.setQuality(quality, true);
         enc.encode(bi, enparam);
         FileOutputStream fimage = new FileOutputStream(new File(filename));
         boutstream.writeTo(fimage);
         fimage.close();
      }
      catch (Exception e) { System.out.println(e); }
   }


   public static void saveImageAsJPEG(BufferedImage bi, String filename) {
      try {
         ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
         JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(boutstream);
         enc.encode(bi);
         FileOutputStream fimage = new FileOutputStream(new File(filename));
         boutstream.writeTo(fimage);
         fimage.close();
      } catch (Exception e) { System.out.println(e); }
   }

   public static String saveImageAsJPEG(BufferedImage bi) {
       try {
         String filename = launchFileDialog();
         ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
         JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(boutstream);
         enc.encode(bi);
         FileOutputStream fimage = new FileOutputStream(new File(filename));
         boutstream.writeTo(fimage);
         fimage.close();
         return filename;
         } catch (Exception e) {
         System.out.println(e);
         return null;
       }
   }

   public static void saveImageAsJPEG(Image img,
                                      int imwidth,
                                      int imheight,
                                      ImageObserver obs,
                                      String filename) {
      BufferedImage bi = new BufferedImage(imwidth, imheight, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2d = bi.createGraphics();
      g2d.drawImage(img, 0,0,imwidth, imheight, obs);
      saveImageAsJPEG(bi, filename);
   }

   public static String saveImageAsJPEG(Image img,
                                      int imwidth,
                                      int imheight,
                                      ImageObserver obs) {
      BufferedImage bi = new BufferedImage(imwidth, imheight, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2d = bi.createGraphics();
      g2d.drawImage(img, 0,0,imwidth, imheight, obs);
      return saveImageAsJPEG(bi);
   }

   public static BufferedImage readAsBufferedImage(String filename) {
      try {
         FileInputStream fis = new FileInputStream(filename);
         JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
         BufferedImage bi = decoder.decodeAsBufferedImage();
         return bi;
      } catch(Exception e) {return null;}
   }


   public static String launchFileDialog(){
        Frame fr = new Frame();
        FileDialog fd = new FileDialog(fr,"", FileDialog.SAVE);
        fd.show();
        String dir = fd.getDirectory();
        String file = fd.getFile();
        if((dir == null) || (file == null)) return null;
        String path = dir+file;
        int lastindex = path.lastIndexOf(".");
        if(lastindex <0) path += ".jpg";
        else {
          String sb = path.substring(lastindex);
          if(sb.equals(".jpg") || sb.equals(".jpeg")) return path;
          else {
            System.out.println(path + " enter extension .jpg or .jpeg");
          }
        }
        return path;
   }
}