/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

import java.io.FileNotFoundException;
import java.io.IOException;

import ilog.views.IlvGraphic;
import ilog.views.IlvManager;
import ilog.views.IlvRect;
import ilog.views.maps.IlvCoordinate;
import ilog.views.maps.IlvFeatureRenderer;
import ilog.views.maps.IlvMapFeature;
import ilog.views.maps.IlvMapRenderException;
import ilog.views.maps.format.IlvMapFormatException;
import ilog.views.maps.format.image.IlvImageReader;
import ilog.views.maps.format.image.IlvImageTileLoader;
import ilog.views.maps.srs.coordsys.IlvGeographicCoordinateSystem;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;
import ilog.views.maps.srs.coordtrans.IlvMapAffineTransform;
import ilog.views.tiling.IlvTiledLayer;
import ilog.views.util.IlvProductUtil;

/**
 * Example showing how to use the Image reader package.
 */
public class ImageReader {
  IlvManager manager = new IlvManager();
  
  /**
   * Load a single image. Upper left and lower right coordinates have to be provided.
   */
  public void loadImage(String imageName, IlvCoordinate ul, IlvCoordinate lr) 
  throws IlvMapFormatException, 
         FileNotFoundException, 
         IOException, 
         IlvMapRenderException,
         IlvCoordinateTransformationException {
    // Create image reader.
    IlvImageReader reader = new IlvImageReader(imageName, ul, lr);
    // Retreive the unique feature.
    IlvMapFeature feature = reader.getNextFeature();
    // Retreive the default renderer.
    IlvFeatureRenderer renderer = reader.getDefaultFeatureRenderer();
    // No reprojection for images.
    IlvCoordinateTransformation tr = 
      new IlvCoordinateTransformation(IlvGeographicCoordinateSystem.WGS84,
                                      IlvGeographicCoordinateSystem.WGS84,
                                      new IlvMapAffineTransform());
    // Create the graphic object.
    IlvGraphic graphic = renderer.makeGraphic(feature, tr);
  }
  
  /**
   * Create a load on demand image layer.
   * The pattern, colFmt, rowFmt are used to retrieve the image file name 
   * from the tile coordinates 
   */
  public void loadLOD(IlvRect tileOrigin, String pattern, String colFmt, String rowFmt) {
    // Create the tile loader.
    IlvImageTileLoader tileLoader = new IlvImageTileLoader(pattern, colFmt, rowFmt);
    // Create the tiled layer.
    IlvTiledLayer tiledLayer = new IlvTiledLayer(tileOrigin);
    // Affect the tile loader to the tiled layer.
    tiledLayer.setTileLoader(tileLoader);
    // Add the layer into the manager.
    manager.addLayer(tiledLayer, -1);
  }
  
  public static void main(String a[]) {
    // This sample uses JViews Maps features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Rogue Wave JViews Maps Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(
        IlvProductUtil.JViews_Maps_Deployment);

    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    javax.swing.SwingUtilities.invokeLater(
      new Runnable() {
        public void run() {
          ImageReader reader = new ImageReader();
          IlvCoordinate ul = new IlvCoordinate(77.5, 10);
          IlvCoordinate lr = new IlvCoordinate(82.5, 5);
          try {
            reader.loadImage("../../data/srilanka.jpg", ul, lr);
          } catch (IlvMapFormatException e) {
            e.printStackTrace();
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          } catch (IlvMapRenderException e) {
            e.printStackTrace();
          } catch (IlvCoordinateTransformationException e) {
            e.printStackTrace();
          }
        }
      }
      );
  }
  
}