/*
 * @(#) JAIUtil.java 1.0 2000/1/21
 * Copyright (c) 2000 Larry Rodrigues
 */
package com.vistech.jai.util;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.image.renderable.*;
import java.io.*;
import javax.media.jai.*;
import javax.media.jai.operator.BorderDescriptor;

/** A utility class that has statis methods to create TiledImage objects.
  * The createTiledImage() methods creates a TiledImage object from a PlanarImage object.
  * The aggregateImages() method aggregates multiple planar images into a single TiledImage object.
  * @version 1.0  21 Jan 2000
  * @author Lawrence Rodrigues
  **/
public class JAIUtil {
  public static TiledImage createTiledImage(PlanarImage image){
      SampleModel sampleModel = image.getSampleModel();
      ColorModel colorModel = image.getColorModel();
      TiledImage tlImage = new TiledImage(image.getMinX(), image.getMinY(),
                                    image.getWidth(), image.getHeight(),
                                    image.getTileGridXOffset(), image.getTileGridYOffset(),
                                    sampleModel, colorModel);
      tlImage.setData(image.copyData());
      return tlImage;
   }
   public static TiledImage aggregateImages(PlanarImage[] imageset,
                                             Dimension celldim, int cols, int rows) {
       int imWid = cols*celldim.width;
       int imHt =  rows*celldim.height;
       PlanarImage img = imageset[0];
       TiledImage ti = new TiledImage(0,0,(int)imWid,(int)(imHt), 0,0,
                                         img.getSampleModel(), img.getColorModel());

       Graphics2D tg2d = ti.createGraphics();
       int x = 0; int y=0;
       for(int i=0; i< rows; i++) {
           for(int j=0;j<cols; j++) {
               if(i*cols+j >= imageset.length) break;
               img = imageset[i*cols+j];
               if(img != null){
                  double magx = celldim.width/(double)img.getWidth() ;
                  double magy = celldim.height/(double)img.getHeight() ;
                  AffineTransform atx = new AffineTransform();
                  atx.setToTranslation((double)x,(double)y);
                  atx.scale(magx, magy);
                  tg2d.drawRenderedImage(img,atx);
                  x += celldim.width;
               }
           }
           x =0;
           y += celldim.height;
        }
       return ti;
    }

    public static RenderedOp setConstantBorder(PlanarImage img,
                                               Dimension border, double constVal) {
       ParameterBlock pb = new ParameterBlock();
       pb.addSource(img);
       pb.add(border.width);
       pb.add(border.height);
       pb.add(border.width);
       pb.add(border.height);
       //pb.add(BorderDescriptor.BORDER_CONST_FILL);
       int numbands = img.getSampleModel().getNumBands();
       double[] fillValue = new double[numbands];
       for (int i=0;i<numbands;i++) {
            fillValue[i] = constVal;
       }
       pb.add(new BorderExtenderConstant(fillValue));
       //pb.add( fillValue);
       return JAI.create("border", pb);
    }
 }
