001: package com.vividsolutions.jump.workbench.imagery.ecw;
002:
003: /*
004: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
005: * for visualizing and manipulating spatial features with geometry and attributes.
006: *
007: * Copyright (C) 2003 Vivid Solutions
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: import com.ermapper.ecw.JNCSException;
035: import com.ermapper.ecw.JNCSFileOpenFailedException;
036: import com.ermapper.ecw.JNCSInvalidSetViewException;
037: import com.ermapper.ecw.JNCSRenderer;
038: import com.vividsolutions.jts.geom.Envelope;
039: import com.vividsolutions.jump.JUMPException;
040: import com.vividsolutions.jump.feature.Feature;
041: import com.vividsolutions.jump.workbench.imagery.ReferencedImage;
042: import com.vividsolutions.jump.workbench.ui.Viewport;
043:
044: /**
045: * A {@link ReferencedImage} for ECW files
046: */
047: public class ECWImage implements ReferencedImage {
048:
049: private Envelope imageEnv = new Envelope();
050: private JNCSRenderer ecwRenderer;
051: private int[] bandlist;
052: private boolean validSetView = false;
053: private Envelope lastViewportEnv = new Envelope();
054:
055: // debugging only
056: int count = 0;
057:
058: public ECWImage(String location) throws JUMPException {
059: init(location);
060: }
061:
062: private void init(String location) throws JUMPException {
063: try {
064: ecwRenderer = new JNCSRenderer(location, false);
065: double xm = ecwRenderer.originX;
066: double yM = ecwRenderer.originY;
067: double xM = ecwRenderer.originX
068: + (double) (ecwRenderer.width - 1)
069: * ecwRenderer.cellIncrementX;
070: double ym = ecwRenderer.originY
071: + (double) (ecwRenderer.height - 1)
072: * ecwRenderer.cellIncrementY;
073: imageEnv = new Envelope(xm, xM, ym, yM);
074:
075: // use all bands
076: bandlist = new int[ecwRenderer.numBands];
077: for (int i = 0; i < ecwRenderer.numBands; i++) {
078: bandlist[i] = i;
079: }
080: } catch (JNCSFileOpenFailedException e) {
081: throw new JUMPException(e.getMessage());
082: } catch (JNCSException e) {
083: throw new JUMPException(e.getMessage());
084: }
085: }
086:
087: public Envelope getEnvelope() {
088: return imageEnv;
089: }
090:
091: public void paint(Feature f, java.awt.Graphics2D g,
092: Viewport viewport) throws JUMPException {
093: Envelope viewportEnv = viewport.getEnvelopeInModelCoordinates();
094: if (!imageEnv.intersects(viewportEnv)) {
095: System.out.println("image not visible");
096: return;
097: }
098:
099: // only set view if viewport has changed
100: if (!viewportEnv.equals(lastViewportEnv)) {
101: validSetView = false;
102: lastViewportEnv = viewportEnv;
103: }
104:
105: try {
106: // Set the view
107: int width = (int) viewport.toViewRectangle(viewportEnv)
108: .getWidth();
109: int height = (int) viewport.toViewRectangle(viewportEnv)
110: .getHeight();
111: double dWorldTLX = viewportEnv.getMinX();
112: double dWorldTLY = viewportEnv.getMaxY();
113: double dWorldBRX = viewportEnv.getMaxX();
114: double dWorldBRY = viewportEnv.getMinY();
115:
116: // only set view if viewport has changed
117: if (!validSetView) {
118: ecwRenderer.setView(ecwRenderer.numBands, bandlist,
119: dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY,
120: width, height);
121: validSetView = true;
122: //System.out.println("setView called");
123: }
124: ecwRenderer.drawImage(g, 0, 0, width, height, dWorldTLX,
125: dWorldTLY, dWorldBRX, dWorldBRY, viewport
126: .getPanel());
127: } catch (JNCSInvalidSetViewException e) {
128: // this catches the "Supersampling not supported" exception
129: validSetView = false;
130: throw new JUMPException(e.getMessage());
131: } catch (Exception e) {
132: validSetView = false;
133: throw new JUMPException(e.getMessage());
134: }
135: }
136:
137: public void close() {
138: ecwRenderer.close(true);
139: }
140:
141: public String getType() {
142: return "ECW";
143: }
144:
145: }
|