001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/coverage/grid/ByteGridCoverage.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2008 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53115 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: klaus.greve@uni-bonn.de 041 042 043 ---------------------------------------------------------------------------*/ 044 package org.deegree.model.coverage.grid; 045 046 import java.awt.Rectangle; 047 import java.awt.image.BandedSampleModel; 048 import java.awt.image.BufferedImage; 049 import java.awt.image.DataBuffer; 050 import java.awt.image.DataBufferByte; 051 import java.awt.image.Raster; 052 import java.awt.image.SampleModel; 053 import java.awt.image.renderable.RenderableImage; 054 055 import org.deegree.model.spatialschema.Envelope; 056 import org.deegree.model.spatialschema.GeometryFactory; 057 import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering; 058 import org.opengis.pt.PT_Envelope; 059 060 /** 061 * GridCoverage implementation for holding grids stored in a raw byte matrix (byte[][]) or in a set 062 * of <tt>ByteGridCoverage</tt>s 063 * 064 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 065 * @author last edited by: $Author: apoth $ 066 * 067 * @version $Revision: 9343 $, $Date: 2007-12-27 14:30:32 +0100 (Do, 27 Dez 2007) $ 068 */ 069 public class ByteGridCoverage extends AbstractGridCoverage { 070 071 private static final long serialVersionUID = 5612511572056707069L; 072 073 private byte[][][] data = null; 074 075 /** 076 * @param coverageOffering 077 * @param envelope 078 * @param data 079 */ 080 public ByteGridCoverage( CoverageOffering coverageOffering, Envelope envelope, byte[][][] data ) { 081 this( coverageOffering, envelope, false, data ); 082 } 083 084 /** 085 * @param coverageOffering 086 * @param envelope 087 * @param isEditable 088 * @param data 089 */ 090 public ByteGridCoverage( CoverageOffering coverageOffering, Envelope envelope, boolean isEditable, byte[][][] data ) { 091 super( coverageOffering, envelope, isEditable ); 092 this.data = data; 093 } 094 095 /** 096 * @param coverageOffering 097 * @param envelope 098 * @param sources 099 */ 100 public ByteGridCoverage( CoverageOffering coverageOffering, Envelope envelope, ByteGridCoverage[] sources ) { 101 super( coverageOffering, envelope, sources ); 102 } 103 104 /** 105 * The number of sample dimensions in the coverage. For grid coverages, a sample dimension is a 106 * band. 107 * 108 * @return The number of sample dimensions in the coverage. 109 * @UML mandatory numSampleDimensions 110 */ 111 public int getNumSampleDimensions() { 112 if ( data != null ) { 113 return data.length; 114 } 115 return sources[0].getNumSampleDimensions(); 116 } 117 118 /** 119 * Returns 2D view of this coverage as a renderable image. This optional operation allows 120 * interoperability with <A HREF="http://java.sun.com/products/java-media/2D/">Java2D</A>. If 121 * this coverage is a {@link "org.opengis.coverage.grid.GridCoverage"} backed by a 122 * {@link java.awt.image.RenderedImage}, the underlying image can be obtained with: 123 * 124 * <code>getRenderableImage(0,1).{@linkplain RenderableImage#createDefaultRendering() 125 * createDefaultRendering()}</code> 126 * 127 * @param xAxis 128 * Dimension to use for the <var>x</var> axis. 129 * @param yAxis 130 * Dimension to use for the <var>y</var> axis. 131 * @return A 2D view of this coverage as a renderable image. 132 * @throws UnsupportedOperationException 133 * if this optional operation is not supported. 134 * @throws IndexOutOfBoundsException 135 * if <code>xAxis</code> or <code>yAxis</code> is out of bounds. 136 */ 137 public RenderableImage getRenderableImage( int xAxis, int yAxis ) 138 throws UnsupportedOperationException, IndexOutOfBoundsException { 139 if ( data != null ) { 140 141 return null; 142 } 143 // TODO if multi images -> sources.length > 0 144 return null; 145 } 146 147 /** 148 * this is a deegree convenience method which returns the source image of an 149 * <tt>ImageGridCoverage</tt>. In procipal the same can be done with the 150 * getRenderableImage(int xAxis, int yAxis) method. but creating a <tt>RenderableImage</tt> 151 * image is very slow. I xAxis or yAxis <= 0 then the size of the returned image will be 152 * calculated from the source images of the coverage. 153 * 154 * @param xAxis 155 * Dimension to use for the <var>x</var> axis. 156 * @param yAxis 157 * Dimension to use for the <var>y</var> axis. 158 * @return the source image of an <tt>ImageGridCoverage</tt>. 159 */ 160 public BufferedImage getAsImage( int xAxis, int yAxis ) { 161 162 if ( xAxis <= 0 || yAxis <= 0 ) { 163 // get default size if passed target size is <= 0 164 Rectangle rect = calculateOriginalSize(); 165 xAxis = rect.width; 166 yAxis = rect.height; 167 } 168 BufferedImage bi = createBufferedImage( xAxis, yAxis ); 169 if ( data != null ) { 170 bi = createBufferedImage( data[0][0].length, data[0].length ); 171 // total number of fields for one band; it is assumed that each 172 // band has the same number of fiels 173 int numOfFields = data[0].length * data[0][0].length; 174 byte[][] bb = new byte[data.length][]; 175 for ( int z = 0; z < data.length; z++ ) { 176 bb[z] = new byte[numOfFields]; 177 } 178 int c = 0; 179 for ( int i = 0; i < data[0].length; i++ ) { 180 for ( int j = 0; j < data[0][i].length; j++ ) { 181 for ( int z = 0; z < data.length; z++ ) { 182 bb[z][c] = data[z][i][j]; 183 } 184 c++; 185 } 186 } 187 DataBuffer db = new DataBufferByte( bb, numOfFields ); 188 SampleModel sm = new BandedSampleModel( DataBuffer.TYPE_BYTE, data[0][0].length, data[0].length, 189 data.length ); 190 Raster raster = Raster.createWritableRaster( sm, db, null ); 191 bi.setData( raster ); 192 } else { 193 // it's a complex ByteGridCoverage made up of different 194 // source coverages 195 for ( int i = 0; i < sources.length; i++ ) { 196 PT_Envelope env = sources[i].getEnvelope(); 197 Envelope sourceEnv = GeometryFactory.createEnvelope( env.minCP.ord[0], env.minCP.ord[1], 198 env.maxCP.ord[0], env.maxCP.ord[1], null ); 199 BufferedImage sourceImg = ( (AbstractGridCoverage) sources[i] ).getAsImage( -1, -1 ); 200 env = this.getEnvelope(); 201 Envelope targetEnv = GeometryFactory.createEnvelope( env.minCP.ord[0], env.minCP.ord[1], 202 env.maxCP.ord[0], env.maxCP.ord[1], null ); 203 bi = paintImage( bi, targetEnv, sourceImg, sourceEnv ); 204 } 205 } 206 207 return bi; 208 } 209 210 private BufferedImage createBufferedImage( int xAxis, int yAxis ) { 211 int sampleDim = getNumSampleDimensions(); 212 switch ( sampleDim ) { 213 case 1: 214 return new BufferedImage( xAxis, yAxis, BufferedImage.TYPE_BYTE_GRAY ); 215 case 3: 216 return new BufferedImage( xAxis, yAxis, BufferedImage.TYPE_INT_RGB ); 217 default: 218 return new BufferedImage( xAxis, yAxis, BufferedImage.TYPE_INT_ARGB ); 219 } 220 } 221 222 /** 223 * calculates the original size of a gridcoverage based on its resolution and the envelope(s) of 224 * its source(s). 225 * 226 * @return 227 */ 228 private Rectangle calculateOriginalSize() { 229 if ( data != null ) { 230 return new Rectangle( data[0].length, data.length ); 231 } 232 BufferedImage bi = ( (ImageGridCoverage) sources[0] ).getAsImage( -1, -1 ); 233 PT_Envelope env = sources[0].getEnvelope(); 234 double dx = ( env.maxCP.ord[0] - env.minCP.ord[0] ) / bi.getWidth(); 235 double dy = ( env.maxCP.ord[1] - env.minCP.ord[1] ) / bi.getHeight(); 236 env = this.getEnvelope(); 237 int w = (int) Math.round( ( env.maxCP.ord[0] - env.minCP.ord[0] ) / dx ); 238 int h = (int) Math.round( ( env.maxCP.ord[1] - env.minCP.ord[1] ) / dy ); 239 return new Rectangle( w, h ); 240 } 241 }