package com.vistech.util;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.jar.*;

public class ImageUtil{
   /** Filters file names.
     * @param name the file name.
     * @param filter an array of file name extensions
     */
   public static boolean filterFileNames(String name, String filter[]){
      if(filter == null) return true;
      for(int i=0; i< filter.length;i++){
          if(name.endsWith(filter[i])) return true;
      }
      return false;
   }
   public static boolean isImage(String fl){
      String[] filter = {"jpeg", "jpg", "JPEG", "JPG", "gif", "GIF"};
      return filterFileNames(fl, filter);
   }

   /**
     * Filters file names.
     * @param name the file name.
     * @param filter an array of file name extensions
     */
   public static boolean compareFileNames(String name, String filelist[]){
      if(filelist == null) return false;
      for(int i=0; i< filelist.length;i++){
          if(name.equals(filelist[i])) return true;
      }
      return false;
   }

   /**
     * Reads images from a jar file.
     * @param jarpath the complete path of the jar file including its name.
     * @return an array of images.
     */
   public static Image[] readImages(String jarpath){
      Image[] imageSet= null;
      try {
        FileInputStream fis = new FileInputStream(jarpath);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ZipInputStream zstream = new ZipInputStream(bis);
        ZipEntry zentry = null;
        Vector imageVect = new Vector();
        while ((zentry = zstream.getNextEntry())!=null) {
                String flName = zentry.getName();
                if(filterFileNames(flName, new String[] {".jpeg", ".gif",".jpg", ".JPEG", ".GIF"})){
                   byte[] imageBuf = new byte[1024];
                   ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
                   for(;;){
                       int bytesread = zstream.read(imageBuf);
                       if (bytesread <0) {break;}
                       boutstream.write(imageBuf,0,bytesread);
                   }
                   byte[] pixmap =boutstream.toByteArray();
                   Image img = Toolkit.getDefaultToolkit().createImage(pixmap);
                   imageVect.addElement(img);
               }
        }
        zstream.close();
        fis.close();
        imageSet = new Image[imageVect.size()];
        imageVect.copyInto(imageSet);
      }catch(Exception e){System.out.println("exception"+e);}
      return imageSet;
   }

  /**
     * Reads images from a jar file.
     * @param jarpath the complete path of the jar file including its name.
     * @return an array of images.
     */
   public static Image[] readImages(InputStream is){
      if(is == null) return null;
      Image[] imageSet= null;
      try {
        BufferedInputStream bis = new BufferedInputStream(is);
        ZipInputStream zstream = new ZipInputStream(bis);
        ZipEntry zentry = null;
        Vector imageVect = new Vector();
        while ((zentry = zstream.getNextEntry())!=null) {
                String flName = zentry.getName();
                if(filterFileNames(flName, new String[] {".jpeg", ".gif"})){
                   byte[] imageBuf = new byte[1024];
                   ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
                   for(;;){
                       int bytesread = zstream.read(imageBuf);
                       if (bytesread <0) {break;}
                       boutstream.write(imageBuf,0,bytesread);
                   }
                   byte[] pixmap =boutstream.toByteArray();
                   Image img = Toolkit.getDefaultToolkit().createImage(pixmap);
                   imageVect.addElement(img);
               }
        }
        zstream.close();
        //fis.close();
        imageSet = new Image[imageVect.size()];
        imageVect.copyInto(imageSet);
      }catch(Exception e){System.out.println("exception"+e);}
      return imageSet;
   }


   public static Image[] readImages(String jarpath, String[] filelist){
      Image[] imageSet= null;
      try {
        ZipFile zipfile = new ZipFile(new File(jarpath));
        Vector imageVect = new Vector();
        for(int i=0; i<filelist.length;i++){
            if(filelist[i] == null) continue;
            ZipEntry zentry = zipfile.getEntry(filelist[i]);
            if(zentry == null) continue;
            InputStream zstream = zipfile.getInputStream(zentry);
            String flName = zentry.getName();

            if(filterFileNames(flName, new String[] {".jpeg", ".gif"})){
               byte[] imageBuf = new byte[128];
               ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
               for(;;){
                   int bytesread = zstream.read(imageBuf);
                   if (bytesread <0) {break;}
                   boutstream.write(imageBuf,0,bytesread);
               }
               byte[] pixmap =boutstream.toByteArray();
               boutstream.flush();
               Image img = Toolkit.getDefaultToolkit().createImage(pixmap);
               imageVect.addElement(img);
           }
        }
        zipfile.close();
        imageSet = new Image[imageVect.size()];
        imageVect.copyInto(imageSet);
      }catch(Exception e){System.out.println(e);}
      return imageSet;
   }

   public static Image readImage(String jarpath, String filename){
      Image image = null;
      try {
        ZipFile zipfile = new ZipFile(new File(jarpath));
        Vector imageVect = new Vector();
        ZipEntry zentry = zipfile.getEntry(filename);
        //if(zentry == null) return;
        InputStream zstream = zipfile.getInputStream(zentry);
        String flName = zentry.getName();
        if(filterFileNames(flName, new String[] {".jpeg", ".gif", ".jpg", ".JPG", ".GIF", ".JPEG"})){
           byte[] imageBuf = new byte[128];
           ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
           for(;;){
               int bytesread = zstream.read(imageBuf);
               if (bytesread <0) {break;}
               boutstream.write(imageBuf,0,bytesread);
           }
           byte[] pixmap =boutstream.toByteArray();
           boutstream.flush();
           image = Toolkit.getDefaultToolkit().createImage(pixmap);
        }
        zipfile.close();
      }catch(Exception e){System.out.println(e);}
      return image;
   }

   public static Image readImageFromJar(String jarpath,
                                 String filename){
      Image image = null;
      try {
        ZipFile zipfile = new ZipFile(new File(jarpath));
        Vector imageVect = new Vector();
        ZipEntry zentry = zipfile.getEntry(filename);
        InputStream zstream = zipfile.getInputStream(zentry);
        String flName = zentry.getName();

        byte[] imageBuf = new byte[128];
        ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
        for(;;){
           int bytesread = zstream.read(imageBuf);
           if (bytesread <0) {break;}
           boutstream.write(imageBuf,0,bytesread);
        }
        byte[] pixmap =boutstream.toByteArray();
        boutstream.flush();
        image = Toolkit.getDefaultToolkit().createImage(pixmap);
        zipfile.close();
      }catch(Exception e){System.out.println(e);}
      return image;
   }

   public static Image[] readImages(JarFile jarfile, String[] filelist){
      Image[] imageSet= null;
      if((filelist == null) ||(jarfile == null)) return null;
      //System.out.println("read images1");
      try {
        Vector imageVect = new Vector();
        for(int i=0; i<filelist.length;i++){
            if(filelist[i] == null) continue;
            JarEntry jentry = jarfile.getJarEntry(filelist[i]);
            if(jentry == null) continue;
            String flName = jentry.getName();
            if(flName == null) return null;
             System.out.println(flName);
            if(filterFileNames(flName, new String[] {".jpeg", ".gif"})){
               System.out.println("in"+flName);
               if(jentry == null || jarfile ==null) continue;
               InputStream zstream = jarfile.getInputStream(jentry);
               if(zstream == null) continue;
               System.out.println(flName);
               byte[] imageBuf = new byte[128];
               ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
               for(;;){
                   int bytesread = zstream.read(imageBuf);
                   if (bytesread <0) {break;}
                   boutstream.write(imageBuf,0,bytesread);
               }
               byte[] pixmap =boutstream.toByteArray();
               boutstream.flush();
               Image img = Toolkit.getDefaultToolkit().createImage(pixmap);
               imageVect.addElement(img);
           }
        }
        jarfile.close();
        imageSet = new Image[imageVect.size()];
        imageVect.copyInto(imageSet);
      }catch(Exception e){System.out.println(e);}
      return imageSet;
   }
}
