001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.arcsde.gce;
018:
019: import java.awt.Point;
020: import java.awt.Rectangle;
021: import java.awt.image.BufferedImage;
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.net.URL;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Properties;
030: import java.util.Vector;
031: import java.util.logging.Level;
032: import java.util.logging.Logger;
033:
034: import javax.imageio.ImageIO;
035:
036: import junit.framework.TestCase;
037:
038: import org.geotools.arcsde.data.ArcSDEQueryTest;
039: import org.geotools.arcsde.gce.ArcSDEPyramid;
040: import org.geotools.arcsde.gce.imageio.ArcSDERasterImageReadParam;
041: import org.geotools.arcsde.gce.imageio.ArcSDERasterReader;
042: import org.geotools.arcsde.gce.imageio.ArcSDERasterReaderSpi;
043: import org.geotools.arcsde.pool.ArcSDEConnectionConfig;
044: import org.geotools.arcsde.pool.ArcSDEConnectionPool;
045: import org.geotools.arcsde.pool.ArcSDEConnectionPoolFactory;
046: import org.geotools.arcsde.pool.ArcSDEPooledConnection;
047: import org.geotools.arcsde.pool.UnavailableArcSDEConnectionException;
048: import org.geotools.coverage.grid.GeneralGridRange;
049: import org.geotools.coverage.grid.GridCoverage2D;
050: import org.geotools.coverage.grid.GridGeometry2D;
051: import org.geotools.data.DataSourceException;
052: import org.geotools.data.coverage.grid.AbstractGridCoverage2DReader;
053: import org.geotools.data.coverage.grid.AbstractGridFormat;
054: import org.geotools.geometry.jts.ReferencedEnvelope;
055: import org.geotools.parameter.Parameter;
056: import org.geotools.referencing.CRS;
057: import org.opengis.coverage.grid.Format;
058: import org.opengis.coverage.grid.GridCoverage;
059: import org.opengis.coverage.grid.GridCoverageReader;
060: import org.opengis.coverage.grid.GridRange;
061: import org.opengis.parameter.GeneralParameterValue;
062: import org.opengis.referencing.crs.CoordinateReferenceSystem;
063:
064: import com.esri.sde.sdk.client.SeConnection;
065: import com.esri.sde.sdk.client.SeException;
066: import com.esri.sde.sdk.client.SeExtent;
067: import com.esri.sde.sdk.client.SeFilter;
068: import com.esri.sde.sdk.client.SeLayer;
069: import com.esri.sde.sdk.client.SeQuery;
070: import com.esri.sde.sdk.client.SeRaster;
071: import com.esri.sde.sdk.client.SeRasterAttr;
072: import com.esri.sde.sdk.client.SeRasterBand;
073: import com.esri.sde.sdk.client.SeRasterColumn;
074: import com.esri.sde.sdk.client.SeRasterConstraint;
075: import com.esri.sde.sdk.client.SeRasterTile;
076: import com.esri.sde.sdk.client.SeRow;
077: import com.esri.sde.sdk.client.SeShape;
078: import com.esri.sde.sdk.client.SeShapeFilter;
079: import com.esri.sde.sdk.client.SeSqlConstruct;
080: import com.esri.sde.sdk.client.SeTable;
081:
082: /**
083: * Tests the functionality of the ArcSDE raster-display package to read rasters
084: * from an ArcSDE database
085: *
086: * @author Saul Farber, (based on ArcSDEPoolTest by Gabriel Roldan)
087: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/test/java/org/geotools/arcsde/gce/ArcSDEImageIOReaderOutputFormatsTest.java $
088: * @version $Id: ArcSDEImageIOReaderOutputFormatsTest.java 27863 2007-11-12 20:34:34Z desruisseaux $
089: */
090: public class ArcSDEImageIOReaderOutputFormatsTest extends TestCase {
091:
092: private static Logger LOGGER = org.geotools.util.logging.Logging
093: .getLogger("org.geotools.arcsde.gce");
094:
095: private ArcSDEConnectionPool pool = null;
096:
097: private HashMap fourBandReaderProps, threeBandReaderProps;
098:
099: private SeRasterAttr rasterAttr;
100:
101: /**
102: * Creates a new ArcSDEConnectionPoolTest object.
103: *
104: */
105: public ArcSDEImageIOReaderOutputFormatsTest(String name)
106: throws Exception {
107: super (name);
108: }
109:
110: /**
111: * loads {@code test-data/testparams.properties} to get connection
112: * parameters and sets up an ArcSDEConnectionPool
113: *
114: * @throws Exception
115: * DOCUMENT ME!
116: * @throws IllegalStateException
117: * DOCUMENT ME!
118: */
119: protected void setUp() throws Exception {
120: super .setUp();
121:
122: //do setup one time only
123: if (pool != null)
124: return;
125:
126: Properties conProps = new Properties();
127: String propsFile = "raster-testparams.properties";
128: InputStream in = org.geotools.test.TestData.openStream(null,
129: propsFile);
130:
131: conProps.load(in);
132: in.close();
133:
134: ArcSDEConnectionConfig connectionConfig = new ArcSDEConnectionConfig(
135: conProps);
136: pool = ArcSDEConnectionPoolFactory.getInstance().createPool(
137: connectionConfig);
138:
139: ArcSDEPooledConnection scon = null;
140: SeQuery q = null;
141: ArcSDEPyramid pyramid;
142: SeRow r;
143: CoordinateReferenceSystem crs = CRS.decode("EPSG:26986");
144: String tableName;
145: try {
146:
147: // Set up a pyramid and readerprops for the four-band 2005 imagery
148: scon = pool.getConnection();
149: tableName = conProps.getProperty("fourbandtable");
150: q = new SeQuery(scon, new String[] { "RASTER" },
151: new SeSqlConstruct(tableName));
152: q.prepareQuery();
153: q.execute();
154: r = q.fetch();
155: rasterAttr = r.getRaster(0);
156: pyramid = new ArcSDEPyramid(rasterAttr, crs);
157:
158: fourBandReaderProps = new HashMap();
159: fourBandReaderProps.put(ArcSDERasterReaderSpi.PYRAMID,
160: pyramid);
161: fourBandReaderProps.put(ArcSDERasterReaderSpi.RASTER_TABLE,
162: tableName);
163: fourBandReaderProps.put(
164: ArcSDERasterReaderSpi.RASTER_COLUMN, "RASTER");
165: } catch (SeException se) {
166: LOGGER.log(Level.SEVERE, se.getSeError().getErrDesc(), se);
167: throw se;
168: } finally {
169: if (q != null)
170: q.close();
171: if (scon != null) {
172: scon.close();
173: }
174: }
175:
176: try {
177: // Set up a pyramid and readerprops for the three-band 2001 imagery
178: scon = pool.getConnection();
179: conProps.getProperty("threebandtable");
180: q = new SeQuery(scon, new String[] { "RASTER" },
181: new SeSqlConstruct(tableName));
182: q.prepareQuery();
183: q.execute();
184: r = q.fetch();
185: rasterAttr = r.getRaster(0);
186: pyramid = new ArcSDEPyramid(rasterAttr, crs);
187:
188: threeBandReaderProps = new HashMap();
189: threeBandReaderProps.put(ArcSDERasterReaderSpi.PYRAMID,
190: pyramid);
191: threeBandReaderProps.put(
192: ArcSDERasterReaderSpi.RASTER_TABLE, tableName);
193: threeBandReaderProps.put(
194: ArcSDERasterReaderSpi.RASTER_COLUMN, "RASTER");
195: } catch (SeException se) {
196: LOGGER.log(Level.SEVERE, se.getSeError().getErrDesc(), se);
197: throw se;
198: } finally {
199: if (q != null)
200: q.close();
201: if (scon != null) {
202: scon.close();
203: }
204: }
205: }
206:
207: /**
208: * closes the connection pool if it's still open
209: *
210: * @throws Exception
211: * DOCUMENT ME!
212: */
213: protected void tearDown() throws Exception {
214: }
215:
216: /**
217: * Tests reading the first three bands of a 4-band image (1 = RED, 2 =
218: * GREEN, 3 = BLUE, 4 = NEAR_INFRARED) into a TYPE_INT_RGB image.
219: *
220: * Bands are mapped as follows: rasterband 1 => image band 0 rasterband 2 =>
221: * image band 1 rasterband 3 => image band 2
222: *
223: */
224: public void testRead4BandIntoTYPE_INT_RGBImage() throws Exception {
225:
226: String imgPrefix = "type_int_rgb-fourband-image";
227: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
228: .createReaderInstance(fourBandReaderProps);
229:
230: ArcSDEPooledConnection scon = null;
231: try {
232: scon = pool.getConnection();
233:
234: SeRasterBand[] bands = rasterAttr.getBands();
235: HashMap bandMapper = new HashMap();
236: // red band
237: bandMapper.put(new Integer((int) bands[0].getId()
238: .longValue()), new Integer(0));
239: // blue band
240: bandMapper.put(new Integer((int) bands[1].getId()
241: .longValue()), new Integer(1));
242: // green band
243: bandMapper.put(new Integer((int) bands[2].getId()
244: .longValue()), new Integer(2));
245:
246: BufferedImage image;
247:
248: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
249: rParam.setSourceBands(new int[] { 1, 2, 3 });
250: rParam.setConnection(scon);
251: rParam.setSourceRegion(new Rectangle(0, 0, 1000, 1000));
252: image = new BufferedImage(1000, 1000,
253: BufferedImage.TYPE_INT_RGB);
254: rParam.setDestination(image);
255: rParam.setBandMapper(bandMapper);
256:
257: reader.read(9, rParam);
258: //ImageIO.write(image, "PNG", new File(imgPrefix + "1.png"));
259: assertTrue("Image from SDE isn't what we expected.",
260: RasterTestUtils.imageEquals(image, imgPrefix
261: + "1.png"));
262:
263: image = new BufferedImage(1000, 1000,
264: BufferedImage.TYPE_INT_RGB);
265: rParam.setDestination(image);
266: rParam.setDestinationOffset(new Point(100, 100));
267: reader.read(8, rParam);
268: //ImageIO.write(image, "PNG", new File(imgPrefix + "2.png"));
269: assertTrue("Image from SDE isn't what we expected.",
270: RasterTestUtils.imageEquals(image, imgPrefix
271: + "2.png"));
272:
273: image = new BufferedImage(1000, 1000,
274: BufferedImage.TYPE_INT_RGB);
275: rParam.setDestination(image);
276: rParam.setSourceRegion(new Rectangle(43, 30, 1000, 1000));
277: rParam.setDestinationOffset(new Point(0, 0));
278: reader.read(8, rParam);
279: //ImageIO.write(image, "PNG", new File(imgPrefix + "3.png"));
280: assertTrue("Image from SDE isn't what we expected.",
281: RasterTestUtils.imageEquals(image, imgPrefix
282: + "3.png"));
283: } catch (Exception e) {
284: throw e;
285: } finally {
286: if (scon != null && !scon.isClosed())
287: scon.close();
288: }
289: }
290:
291: /**
292: * Tests reading the first three bands of a 4-band image (1 = RED, 2 =
293: * GREEN, 3 = BLUE, 4 = NEAR_INFRARED) into a TYPE_INT_ARGB image.
294: *
295: * Bands are mapped as follows: rasterband 1 => image band 1 (red,
296: * hopefully!) rasterband 2 => image band 2 (green, hopefully!) rasterband 3 =>
297: * image band 3 (blue, hopefully!)
298: *
299: * Question: what do we do about image band 0 (the alpha band?) Ignoring it
300: * for now.
301: *
302: */
303: public void testRead4BandIntoTYPE_INT_ARGBImage() throws Exception {
304:
305: String imgPrefix = "type_int_argb-fourband-image";
306: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
307: .createReaderInstance(fourBandReaderProps);
308:
309: ArcSDEPooledConnection scon = null;
310: try {
311: scon = pool.getConnection();
312:
313: SeRasterBand[] bands = rasterAttr.getBands();
314: HashMap bandMapper = new HashMap();
315: // red band
316: bandMapper.put(new Integer((int) bands[0].getId()
317: .longValue()), new Integer(0));
318: // blue band
319: bandMapper.put(new Integer((int) bands[1].getId()
320: .longValue()), new Integer(1));
321: // green band
322: bandMapper.put(new Integer((int) bands[2].getId()
323: .longValue()), new Integer(2));
324:
325: BufferedImage image;
326:
327: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
328: rParam.setSourceBands(new int[] { 1, 2, 3 });
329: rParam.setConnection(scon);
330: rParam.setSourceRegion(new Rectangle(0, 0, 1000, 1000));
331: image = new BufferedImage(1000, 1000,
332: BufferedImage.TYPE_INT_ARGB);
333: int[] opaque = new int[image.getWidth() * image.getHeight()];
334: for (int i = 0; i < opaque.length; i++) {
335: opaque[i] = 0xff;
336: }
337: image.getSampleModel().setSamples(0, 0, image.getWidth(),
338: image.getHeight(), 3, opaque,
339: image.getRaster().getDataBuffer());
340: rParam.setDestination(image);
341: rParam.setBandMapper(bandMapper);
342:
343: reader.read(9, rParam);
344: //ImageIO.write(image, "PNG", new File(imgPrefix + "1.png"));
345: assertTrue("Image from SDE isn't what we expected.",
346: RasterTestUtils.imageEquals(image, imgPrefix
347: + "1.png"));
348:
349: image = new BufferedImage(1000, 1000,
350: BufferedImage.TYPE_INT_ARGB);
351: opaque = new int[image.getWidth() * image.getHeight()];
352: for (int i = 0; i < opaque.length; i++) {
353: opaque[i] = 0xff;
354: }
355: image.getSampleModel().setSamples(0, 0, image.getWidth(),
356: image.getHeight(), 3, opaque,
357: image.getRaster().getDataBuffer());
358: rParam.setDestination(image);
359: rParam.setDestinationOffset(new Point(100, 100));
360: reader.read(8, rParam);
361: //ImageIO.write(image, "PNG", new File(imgPrefix + "2.png"));
362: assertTrue("Image from SDE isn't what we expected.",
363: RasterTestUtils.imageEquals(image, imgPrefix
364: + "2.png"));
365:
366: image = new BufferedImage(1000, 1000,
367: BufferedImage.TYPE_INT_ARGB);
368: opaque = new int[image.getWidth() * image.getHeight()];
369: for (int i = 0; i < opaque.length; i++) {
370: opaque[i] = 0xff;
371: }
372: image.getSampleModel().setSamples(0, 0, image.getWidth(),
373: image.getHeight(), 3, opaque,
374: image.getRaster().getDataBuffer());
375: rParam.setDestination(image);
376: rParam.setSourceRegion(new Rectangle(43, 30, 1000, 1000));
377: rParam.setDestinationOffset(new Point(0, 0));
378: reader.read(8, rParam);
379: //ImageIO.write(image, "PNG", new File(imgPrefix + "3.png"));
380: assertTrue("Image from SDE isn't what we expected.",
381: RasterTestUtils.imageEquals(image, imgPrefix
382: + "3.png"));
383: } catch (Exception e) {
384: throw e;
385: } finally {
386: if (scon != null)
387: scon.close();
388: }
389: }
390:
391: /**
392: * Tests reading the first three bands of a 4-band image (1 = RED, 2 =
393: * GREEN, 3 = BLUE, 4 = NEAR_INFRARED) into a TYPE_INT_ARGB image.
394: *
395: * Bands are mapped as follows: rasterband 1 => image band 1 (red,
396: * hopefully!) rasterband 2 => image band 2 (green, hopefully!) rasterband 3 =>
397: * image band 3 (blue, hopefully!)
398: *
399: * Question: what do we do about image band 0 (the alpha band?) Ignoring it
400: * for now.
401: *
402: */
403: public void testRead3BandIntoTYPE_INT_RGBImage() throws Exception {
404:
405: String imgPrefix = "type_int_rgb-threeband-image";
406:
407: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
408: .createReaderInstance(threeBandReaderProps);
409:
410: ArcSDEPooledConnection scon = null;
411: try {
412: scon = pool.getConnection();
413:
414: SeRasterBand[] bands = rasterAttr.getBands();
415: HashMap bandMapper = new HashMap();
416: // red band
417: bandMapper.put(new Integer((int) bands[0].getId()
418: .longValue()), new Integer(0));
419: // blue band
420: bandMapper.put(new Integer((int) bands[1].getId()
421: .longValue()), new Integer(1));
422: // green band
423: bandMapper.put(new Integer((int) bands[2].getId()
424: .longValue()), new Integer(2));
425:
426: BufferedImage image;
427:
428: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
429: rParam.setSourceBands(new int[] { 1, 2, 3 });
430: rParam.setConnection(scon);
431: rParam.setSourceRegion(new Rectangle(0, 0, 1000, 1000));
432: image = new BufferedImage(1000, 1000,
433: BufferedImage.TYPE_INT_RGB);
434: rParam.setDestination(image);
435: rParam.setBandMapper(bandMapper);
436:
437: reader.read(9, rParam);
438: //ImageIO.write(image, "PNG", new File(imgPrefix + "1.png"));
439: assertTrue("Image from SDE isn't what we expected.",
440: RasterTestUtils.imageEquals(image, imgPrefix
441: + "1.png"));
442:
443: image = new BufferedImage(1000, 1000,
444: BufferedImage.TYPE_INT_RGB);
445: rParam.setDestination(image);
446: rParam.setDestinationOffset(new Point(100, 100));
447: reader.read(8, rParam);
448: //ImageIO.write(image, "PNG", new File(imgPrefix + "2.png"));
449: assertTrue("Image from SDE isn't what we expected.",
450: RasterTestUtils.imageEquals(image, imgPrefix
451: + "2.png"));
452:
453: image = new BufferedImage(1000, 1000,
454: BufferedImage.TYPE_INT_RGB);
455: rParam.setDestination(image);
456: rParam.setSourceRegion(new Rectangle(43, 30, 1000, 1000));
457: rParam.setDestinationOffset(new Point(0, 0));
458: reader.read(8, rParam);
459: //ImageIO.write(image, "PNG", new File(imgPrefix + "3.png"));
460: assertTrue("Image from SDE isn't what we expected.",
461: RasterTestUtils.imageEquals(image, imgPrefix
462: + "3.png"));
463: } catch (Exception e) {
464: throw e;
465: } finally {
466: if (scon != null)
467: scon.close();
468: }
469:
470: }
471:
472: /**
473: * Tests reading the first three bands of a 4-band image (1 = RED, 2 =
474: * GREEN, 3 = BLUE, 4 = NEAR_INFRARED) into a TYPE_INT_ARGB image.
475: *
476: * Bands are mapped as follows: rasterband 1 => image band 1 (red,
477: * hopefully!) rasterband 2 => image band 2 (green, hopefully!) rasterband 3 =>
478: * image band 3 (blue, hopefully!)
479: *
480: * Question: what do we do about image band 0 (the alpha band?) Ignoring it
481: * for now.
482: *
483: */
484: public void testRead3BandIntoTYPE_INT_ARGBImage() throws Exception {
485:
486: String imgPrefix = "type_int_argb-threeband-image";
487:
488: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
489: .createReaderInstance(threeBandReaderProps);
490:
491: ArcSDEPooledConnection scon = null;
492: try {
493: scon = pool.getConnection();
494: SeRasterBand[] bands = rasterAttr.getBands();
495: HashMap bandMapper = new HashMap();
496: // red band
497: bandMapper.put(new Integer((int) bands[0].getId()
498: .longValue()), new Integer(0));
499: // blue band
500: bandMapper.put(new Integer((int) bands[1].getId()
501: .longValue()), new Integer(1));
502: // green band
503: bandMapper.put(new Integer((int) bands[2].getId()
504: .longValue()), new Integer(2));
505:
506: BufferedImage image;
507:
508: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
509: rParam.setSourceBands(new int[] { 1, 2, 3 });
510: rParam.setConnection(scon);
511: rParam.setSourceRegion(new Rectangle(0, 0, 1000, 1000));
512: image = new BufferedImage(1000, 1000,
513: BufferedImage.TYPE_INT_ARGB);
514: int[] opaque = new int[image.getWidth() * image.getHeight()];
515: for (int i = 0; i < opaque.length; i++) {
516: opaque[i] = 0xff;
517: }
518: image.getSampleModel().setSamples(0, 0, image.getWidth(),
519: image.getHeight(), 3, opaque,
520: image.getRaster().getDataBuffer());
521: rParam.setDestination(image);
522: rParam.setBandMapper(bandMapper);
523:
524: reader.read(9, rParam);
525: //ImageIO.write(image, "PNG", new File(imgPrefix + "1.png"));
526: assertTrue("Image from SDE isn't what we expected.",
527: RasterTestUtils.imageEquals(image, imgPrefix
528: + "1.png"));
529:
530: image = new BufferedImage(1000, 1000,
531: BufferedImage.TYPE_INT_ARGB);
532: opaque = new int[image.getWidth() * image.getHeight()];
533: for (int i = 0; i < opaque.length; i++) {
534: opaque[i] = 0xff;
535: }
536: image.getSampleModel().setSamples(0, 0, image.getWidth(),
537: image.getHeight(), 3, opaque,
538: image.getRaster().getDataBuffer());
539: rParam.setDestination(image);
540: rParam.setDestinationOffset(new Point(100, 100));
541: reader.read(8, rParam);
542: //ImageIO.write(image, "PNG", new File(imgPrefix + "2.png"));
543: assertTrue("Image from SDE isn't what we expected.",
544: RasterTestUtils.imageEquals(image, imgPrefix
545: + "2.png"));
546:
547: image = new BufferedImage(1000, 1000,
548: BufferedImage.TYPE_INT_ARGB);
549: opaque = new int[image.getWidth() * image.getHeight()];
550: for (int i = 0; i < opaque.length; i++) {
551: opaque[i] = 0xff;
552: }
553: image.getSampleModel().setSamples(0, 0, image.getWidth(),
554: image.getHeight(), 3, opaque,
555: image.getRaster().getDataBuffer());
556: rParam.setDestination(image);
557: rParam.setSourceRegion(new Rectangle(43, 30, 1000, 1000));
558: rParam.setDestinationOffset(new Point(0, 0));
559: reader.read(8, rParam);
560: //ImageIO.write(image, "PNG", new File(imgPrefix + "3.png"));
561: assertTrue("Image from SDE isn't what we expected.",
562: RasterTestUtils.imageEquals(image, imgPrefix
563: + "3.png"));
564: } catch (Exception e) {
565: throw e;
566: } finally {
567: if (scon != null && !scon.isClosed())
568: scon.close();
569: }
570: }
571:
572: /**
573: * Tests reading the first three bands of a 4-band image (1 = RED, 2 =
574: * GREEN, 3 = BLUE, 4 = NEAR_INFRARED) into a TYPE_INT_ARGB image.
575: *
576: * Bands are mapped as follows: rasterband 1 => image band 1 (red,
577: * hopefully!) rasterband 2 => image band 2 (green, hopefully!) rasterband 3 =>
578: * image band 3 (blue, hopefully!)
579: *
580: * Question: what do we do about image band 0 (the alpha band?) Ignoring it
581: * for now.
582: *
583: */
584: public void testRead3BandIntoTYPE_3BYTE_BGRImage() throws Exception {
585:
586: String imgPrefix = "type_3byte_bgr-3band-image";
587:
588: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
589: .createReaderInstance(threeBandReaderProps);
590:
591: ArcSDEPooledConnection scon = null;
592: try {
593: scon = pool.getConnection();
594: SeRasterBand[] bands = rasterAttr.getBands();
595: HashMap bandMapper = new HashMap();
596: bandMapper.put(new Integer((int) bands[0].getId()
597: .longValue()), new Integer(0));
598: bandMapper.put(new Integer((int) bands[1].getId()
599: .longValue()), new Integer(1));
600: bandMapper.put(new Integer((int) bands[2].getId()
601: .longValue()), new Integer(2));
602:
603: BufferedImage image;
604:
605: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
606: rParam.setSourceBands(new int[] { 1, 2, 3 });
607: rParam.setConnection(scon);
608: rParam.setSourceRegion(new Rectangle(0, 0, 1000, 1000));
609: image = new BufferedImage(1000, 1000,
610: BufferedImage.TYPE_3BYTE_BGR);
611: rParam.setDestination(image);
612: rParam.setBandMapper(bandMapper);
613:
614: reader.read(9, rParam);
615: //ImageIO.write(image, "PNG", new File(imgPrefix + "1.png"));
616: assertTrue("Image from SDE isn't what we expected.",
617: RasterTestUtils.imageEquals(image, imgPrefix
618: + "1.png"));
619: } catch (Exception e) {
620: throw e;
621: } finally {
622: if (scon != null && !scon.isClosed())
623: scon.close();
624: }
625: }
626:
627: /**
628: * Tests reading the first three bands of a 4-band image (1 = RED, 2 =
629: * GREEN, 3 = BLUE, 4 = NEAR_INFRARED) into a TYPE_INT_ARGB image.
630: *
631: * Bands are mapped as follows: rasterband 1 => image band 1 (red,
632: * hopefully!) rasterband 2 => image band 2 (green, hopefully!) rasterband 3 =>
633: * image band 3 (blue, hopefully!)
634: *
635: * Question: what do we do about image band 0 (the alpha band?) Ignoring it
636: * for now.
637: *
638: */
639: public void testRead4BandIntoTYPE_3BYTE_BGRImage() throws Exception {
640:
641: String imgPrefix = "type_3byte_bgr-4band-image";
642:
643: ArcSDERasterReader reader = (ArcSDERasterReader) new ArcSDERasterReaderSpi()
644: .createReaderInstance(fourBandReaderProps);
645:
646: ArcSDEPooledConnection scon = null;
647: try {
648: scon = pool.getConnection();
649: SeRasterBand[] bands = rasterAttr.getBands();
650: HashMap bandMapper = new HashMap();
651: bandMapper.put(new Integer((int) bands[0].getId()
652: .longValue()), new Integer(0));
653: bandMapper.put(new Integer((int) bands[1].getId()
654: .longValue()), new Integer(1));
655: bandMapper.put(new Integer((int) bands[2].getId()
656: .longValue()), new Integer(2));
657:
658: BufferedImage image;
659:
660: ArcSDERasterImageReadParam rParam = new ArcSDERasterImageReadParam();
661: rParam.setSourceBands(new int[] { 1, 2, 3 });
662: rParam.setConnection(scon);
663: rParam.setSourceRegion(new Rectangle(0, 0, 1000, 1000));
664: image = new BufferedImage(1000, 1000,
665: BufferedImage.TYPE_4BYTE_ABGR);
666: int[] opaque = new int[image.getWidth() * image.getHeight()];
667: for (int i = 0; i < opaque.length; i++) {
668: opaque[i] = 0xff;
669: }
670: image.getSampleModel().setSamples(0, 0, image.getWidth(),
671: image.getHeight(), 3, opaque,
672: image.getRaster().getDataBuffer());
673: rParam.setDestination(image);
674: rParam.setBandMapper(bandMapper);
675:
676: reader.read(9, rParam);
677: //ImageIO.write(image, "PNG", new File(imgPrefix + "1.png"));
678: assertTrue("Image from SDE isn't what we expected.",
679: RasterTestUtils.imageEquals(image, imgPrefix
680: + "1.png"));
681: } catch (Exception e) {
682: throw e;
683: } finally {
684: if (scon != null && !scon.isClosed())
685: scon.close();
686: }
687: }
688: }
|