
package test;

// J2SE dependencies
import java.awt.Color;
import java.awt.Frame;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
import java.awt.image.RenderedImage;
import java.awt.image.WritableRaster;
import java.net.URL;
import java.util.List;

import javax.media.jai.ColorModelFactory;
import javax.media.jai.widget.ImageCanvas;
import javax.media.jai.widget.ScrollingImagePanel;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.geotools.coverage.FactoryFinder;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageFactory;
import org.geotools.data.coverage.grid.stream.StreamGridCoverageExchange;
import org.geotools.gce.arcgrid.ArcGridFormat;
import org.geotools.gce.arcgrid.ArcGridRaster;
import org.geotools.gui.swing.StyledMapPane;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.parameter.Parameter;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.renderer.j2d.RenderedMapScale;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;
import org.opengis.coverage.grid.Format;
import org.opengis.coverage.grid.GridCoverage;
import org.opengis.coverage.grid.GridCoverageExchange;
import org.opengis.coverage.grid.GridCoverageReader;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.spatialschema.geometry.Envelope;

import sun.awt.image.SunWritableRaster;

import com.sun.media.jai.widget.DisplayJAI;

public class FloatRasterDemo {
    
    public void test()throws Exception{
        URL url = getClass().getResource(
            "test.asc");
        GridCoverageExchange gce = new StreamGridCoverageExchange();
        Format format = new ArcGridFormat();
        GridCoverageReader reader = ((ArcGridFormat)format).getReader(url);
        
        List list = format.getReadParameters().values();
        GeneralParameterValue[] values = new Parameter[] {(Parameter) list.get(2)} ; 
//        GridCoverage coverage = reader.read(values);
        
        CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
//        Envelope envelope = coverage.getEnvelope();
        
//        ((GridCoverage2D) coverage).show();
        
        MapContext map = new DefaultMapContext();
        
        Style style = new StyleBuilder().getStyleFactory().getDefaultStyle();
//        map.addLayer(coverage, style);
        URL monterey = getClass().getResource("");
        ArcGridRaster raster = new ArcGridRaster(monterey);
//        GridCoverage montereyCoverage = getCoverage(monterey);
        Color[] colors = new Color[] {Color.BLUE, Color.CYAN, Color.WHITE, Color.YELLOW, Color.RED};
//        GridCoverageFactory factory = FactoryFinder.getGridCoverageFactory(null);
//        GridCoverage2D montereyGrid = factory.create("Test", raster.readRaster(), montereyCoverage.getEnvelope(), null, null, null, new Color[][] {colors}, null);
//        RenderedImage image = montereyGrid.getRenderedImage();
        WritableRaster image = raster.readRaster();
        
//      Create a grayscale color space
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        
        ColorModel cm = new ComponentColorModel(cs, false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_FLOAT);
        BufferedImage bimage = new BufferedImage(cm, image, false, null);
        DisplayJAI jai = new DisplayJAI(bimage);
        jai.setVisible(true);
        
//        map.addLayer(montereyGrid, style);
//        StyledMapPane mapPane = new StyledMapPane();
//        mapPane.setMapContext(map);
//        mapPane.getRenderer().addLayer(new RenderedMapScale());
        JFrame frame = new JFrame();
        frame.setTitle("test map");
//        frame.setContentPane(mapPane.createScrollPane());
        frame.getContentPane().add(jai);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setVisible(true);
    }
    
    public GridCoverage getCoverage(URL url) throws Exception{
        GridCoverageExchange gce = new StreamGridCoverageExchange();
        Format format = new ArcGridFormat();
        GridCoverageReader reader = ((ArcGridFormat)format).getReader(url);
        
        List list = format.getReadParameters().values();
        GeneralParameterValue[] values = new Parameter[] {(Parameter) list.get(2)} ; 
        GridCoverage coverage = reader.read(values);
        return coverage;
    }

    /**
     * Run the demo.
     */
    public static void main(String[] args) throws Exception {
        FloatRasterDemo demo = new FloatRasterDemo();
        demo.test();
    }
}