/** ImageFormatter Class
   * @version 1.0  28 apr 2000
   * @author Lawrence Rodrigues
   **/
package app;
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.renderable.*;
import javax.media.jai.operator.BorderDescriptor;
import javax.media.jai.*;

/** An sample application that illustrates how to use the JAI "format" operator.
  * @version 1.0 28 Apr 2000
  * @author Lawrence Rodrigues
  **/
public class ImageFormatter extends app.JAISimpleViewer{
   protected Dimension border = new Dimension(10,10);

   public static void  main(String[] args){
      ImageFormatter ip = new ImageFormatter();
      if(args.length <1) {
        System.out.println("Enter a valid image file name");
        System.exit(0);
      }
      ip.loadAndDisplay(args[0]);
   }

   public void loadAndDisplay(String filename){
      PlanarImage img = readAsPlanarImage(filename);
      TileCache tc = JAI.getDefaultInstance().getTileCache();
      System.out.println("Default Memory Capacity = "+ tc.getMemoryCapacity());
      tc.setMemoryCapacity(2024*2024);
      System.out.println("Memory Capacity = "+ tc.getMemoryCapacity());

      System.out.println("Without Border");
      System.out.println("--------------");
      System.out.println("Size: "+
                          img.getWidth()+ " , "+  img.getHeight());
      System.out.println("ULHC  "+
                          img.getMinX()+ " ,"+  img.getMinY());

      RenderedOp borderImg  = com.vistech.jai.util.JAIUtil.setConstantBorder(img, border, 70);

      System.out.println("With Border");
      System.out.println("-----------");
      System.out.println("Size: "+
                          borderImg.getWidth()+ " ,"+  borderImg.getHeight());

      System.out.println("ULHC:  "+
                          borderImg.getMinX()+ " ,"+  borderImg.getMinY());

      RenderedOp opImage = reformatImage(borderImg, new Dimension(256, 256));
      displayImage(opImage);
   }

   public static RenderedOp reformatImage(PlanarImage img, Dimension tileDim) {
      ImageLayout tileLayout = new ImageLayout(img);
      tileLayout.setTileWidth(tileDim.width);
      tileLayout.setTileHeight(tileDim.width);

      HashMap map = new HashMap();
      map.put(JAI.KEY_IMAGE_LAYOUT, tileLayout);
      map.put(JAI.KEY_INTERPOLATION,
             Interpolation.getInstance(Interpolation.INTERP_BICUBIC));

      RenderingHints tileHints = new RenderingHints(map);
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(img);
      return JAI.create("format", pb, tileHints);
   }

   public void launchFrame(PlanarImage img, int width, int height) {
      setTitle("JAI Image Viewer");
      viewer = new BorderedPanel(img);
      viewer.setPreferredSize(new Dimension(width, height));
      Container cp = getContentPane();
      getContentPane().setLayout(new GridLayout(1,1));
      cp.add(viewer);
      pack();
      setSize(new Dimension(width, height));
      show();
      viewer.repaint();
   }

   protected class BorderedPanel extends ImagePanel {

      public BorderedPanel(PlanarImage img){ super(img);}

      public void paintComponent(Graphics gc){
         Graphics2D g = (Graphics2D)gc;
         Rectangle rect = this.getBounds();
         if((width != rect.width) || (height != rect.height)){
             double magx = rect.width/(double)width ;
             double magy = rect.height/(double)height ;
             atx.setToScale(magx, magy);
             atx.translate(border.width,border.height);
         }
         if(image != null) g.drawRenderedImage(image, atx);
      }
   }
 }
