Revision 6325 org.gvsig.raster/branches/org.gvsig.raster.2.4/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.legend/org.gvsig.raster.lib.legend.impl/src/main/java/org/gvsig/raster/lib/legend/impl/DefaultRasterLegend.java
DefaultRasterLegend.java | ||
---|---|---|
7 | 7 |
import java.awt.image.BufferedImage; |
8 | 8 |
|
9 | 9 |
import org.cresques.cts.ICoordTrans; |
10 |
|
|
10 | 11 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
11 | 12 |
import org.gvsig.fmap.geom.GeometryLocator; |
12 | 13 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
14 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
|
15 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
|
13 | 16 |
import org.gvsig.fmap.geom.primitive.Envelope; |
14 | 17 |
import org.gvsig.fmap.geom.primitive.Point; |
15 | 18 |
import org.gvsig.fmap.mapcontext.ViewPort; |
16 | 19 |
import org.gvsig.raster.lib.buffer.api.Band; |
17 | 20 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
18 | 21 |
import org.gvsig.raster.lib.buffer.api.FilterList; |
22 |
import org.gvsig.raster.lib.buffer.api.NoData; |
|
19 | 23 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
20 | 24 |
import org.gvsig.raster.lib.legend.api.ColorInterpretation; |
21 | 25 |
import org.gvsig.raster.lib.legend.api.ColorTable; |
... | ... | |
28 | 32 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
29 | 33 |
import org.gvsig.tools.task.SimpleTaskStatus; |
30 | 34 |
import org.gvsig.tools.task.TaskStatusManager; |
35 |
|
|
31 | 36 |
import org.slf4j.Logger; |
32 | 37 |
import org.slf4j.LoggerFactory; |
33 | 38 |
|
... | ... | |
136 | 141 |
|
137 | 142 |
// Convert extension to check if envelopes intersect |
138 | 143 |
ICoordTrans coordTrans = viewPort.getProjection().getCT(buffer.getProjection()); |
144 |
ICoordTrans invertedCoordTrans = buffer.getProjection().getCT(viewPort.getProjection()); |
|
139 | 145 |
Envelope convertedEnvelope = viewPort.getAdjustedEnvelope().convert(coordTrans); |
146 |
double viewPortPixelSizeX = viewPort.getAdjustedEnvelope().getLength(0) / viewPort.getImageWidth(); |
|
147 |
double viewPortPixelSizeY = viewPort.getAdjustedEnvelope().getLength(1) / viewPort.getImageHeight(); |
|
140 | 148 |
if (!convertedEnvelope.intersects(buffer.getEnvelope())) { |
141 | 149 |
return; |
142 | 150 |
} |
143 | 151 |
try { |
144 | 152 |
// Clip buffer with doubled converted envelope |
145 | 153 |
bufferToDraw = buffer.clip(convertedEnvelope); |
154 |
Envelope bufferEnvelopeInViewPortCoords = bufferToDraw.getEnvelope().convert(invertedCoordTrans); |
|
146 | 155 |
|
156 |
double widthPixel =bufferEnvelopeInViewPortCoords.getLength(0)/viewPortPixelSizeX;; |
|
157 |
double heightPixel =bufferEnvelopeInViewPortCoords.getLength(1)/viewPortPixelSizeY;; |
|
158 |
|
|
159 |
bufferToDraw = |
|
160 |
bufferToDraw.createInterpolated((int) Math.floor(heightPixel), |
|
161 |
(int) Math.floor(widthPixel), Buffer.INTERPOLATION_NearestNeighbour, |
|
162 |
taskStatus); |
|
147 | 163 |
// Convert interpolated clipped buffer |
148 |
bufferToDraw = bufferToDraw.convert(coordTrans.getInverted(), taskStatus);
|
|
164 |
bufferToDraw = bufferToDraw.convert(invertedCoordTrans, taskStatus);
|
|
149 | 165 |
|
150 |
double widthPixel = |
|
151 |
getWidthPixel(bufferToDraw.getEnvelope(), viewPort.getAdjustedEnvelope().getLength(0) |
|
152 |
/ viewPort.getImageWidth()); |
|
153 |
double heightPixel = |
|
154 |
getHeightPixel(bufferToDraw.getEnvelope(), viewPort.getAdjustedEnvelope().getLength(1) |
|
155 |
/ viewPort.getImageHeight()); |
|
166 |
widthPixel = |
|
167 |
getWidthPixel(bufferToDraw.getEnvelope(), viewPortPixelSizeX); |
|
168 |
heightPixel = |
|
169 |
getHeightPixel(bufferToDraw.getEnvelope(), viewPortPixelSizeY); |
|
156 | 170 |
|
157 | 171 |
bufferToDraw = |
158 | 172 |
bufferToDraw.createInterpolated((int) Math.floor(heightPixel), |
159 | 173 |
(int) Math.floor(widthPixel), Buffer.INTERPOLATION_NearestNeighbour, |
160 | 174 |
taskStatus); |
161 | 175 |
|
176 |
|
|
162 | 177 |
} catch (BufferException e) { |
163 | 178 |
LOG.warn("Buffer can not be clipped, converted or interpolated", e); |
164 | 179 |
taskStatus.abort(); |
... | ... | |
200 | 215 |
return; |
201 | 216 |
} |
202 | 217 |
|
203 |
// Draw buffer |
|
204 |
Image image = null; |
|
205 |
if (this.colorInterpretation.isGray()){ |
|
206 |
// Draw buffer with gray scale |
|
207 |
image = drawGrayBuffer(graphics, bufferToDraw, transparency, filters); |
|
208 |
}else if ( this.colorInterpretation.isPalette() && this.colorTable != null ) { |
|
209 |
// Draw buffer with table color |
|
210 |
image = drawPaletteBuffer(graphics, bufferToDraw, colorTable, transparency, filters); |
|
211 |
} else if (this.colorInterpretation.isRGBA() || this.colorInterpretation.isRGB() |
|
212 |
|| this.colorInterpretation.isBGR()) { |
|
213 |
// Draw RGB, RGBA or BGR buffer without color table |
|
214 |
image = drawRGBBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
|
215 |
}else if (this.colorInterpretation.isHSL()) { |
|
216 |
// Draw HSL buffer without color table |
|
217 |
image = drawHSLBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
|
218 |
}else if (this.colorInterpretation.isCMYK()) { |
|
219 |
// Draw CMYK buffer without color table |
|
220 |
image = drawCMYKBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
|
221 |
}else if (this.colorInterpretation.isYCBCR()) { |
|
222 |
// Draw YCBCR buffer without color table |
|
223 |
image = drawYCBCRBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
|
224 |
} |
|
218 |
if (bufferToDraw != null) { |
|
225 | 219 |
|
226 |
// Calculate where image has to be drawn |
|
227 |
double x = bufferToDraw.getEnvelope().getMinimum(0); |
|
228 |
double y = bufferToDraw.getEnvelope().getMaximum(1); |
|
229 |
AffineTransform affineTransform = |
|
230 |
calculateAffineTransform(viewPort.getAdjustedEnvelope(), viewPort.getImageWidth(), |
|
231 |
viewPort.getImageHeight()); |
|
220 |
// Draw buffer |
|
221 |
Image image = null; |
|
222 |
if (this.colorInterpretation.isGray()) { |
|
223 |
// Draw buffer with gray scale |
|
224 |
image = drawGrayBuffer(graphics, bufferToDraw, transparency, filters); |
|
225 |
} else if (this.colorInterpretation.isPalette() && this.colorTable != null) { |
|
226 |
// Draw buffer with table color |
|
227 |
image = drawPaletteBuffer(graphics, bufferToDraw, colorTable, transparency, filters); |
|
228 |
} else if (this.colorInterpretation.isRGBA() || this.colorInterpretation.isRGB() |
|
229 |
|| this.colorInterpretation.isBGR()) { |
|
230 |
// Draw RGB, RGBA or BGR buffer without color table |
|
231 |
image = drawRGBBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
|
232 |
} else if (this.colorInterpretation.isHSL()) { |
|
233 |
// Draw HSL buffer without color table |
|
234 |
image = drawHSLBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
|
235 |
} else if (this.colorInterpretation.isCMYK()) { |
|
236 |
// Draw CMYK buffer without color table |
|
237 |
image = drawCMYKBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
|
238 |
} else if (this.colorInterpretation.isYCBCR()) { |
|
239 |
// Draw YCBCR buffer without color table |
|
240 |
image = drawYCBCRBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
|
241 |
} |
|
232 | 242 |
|
233 |
Point point; |
|
234 |
try { |
|
235 |
point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D); |
|
236 |
point.transform(affineTransform.createInverse()); |
|
237 |
graphics.drawImage(image, (int) Math.floor(point.getX()), |
|
238 |
(int) Math.floor(point.getY()), null); |
|
239 |
} catch (CreateGeometryException | NoninvertibleTransformException e) { |
|
240 |
LOG.warn("Can not calculate the point of buffer in viewport image", e); |
|
241 |
taskStatus.abort(); |
|
242 |
return; |
|
243 |
// Calculate where image has to be drawn |
|
244 |
double x = bufferToDraw.getEnvelope().getMinimum(0); |
|
245 |
double y = bufferToDraw.getEnvelope().getMaximum(1); |
|
246 |
AffineTransform affineTransform = |
|
247 |
calculateAffineTransform(viewPort.getAdjustedEnvelope(), viewPort.getImageWidth(), |
|
248 |
viewPort.getImageHeight()); |
|
249 |
|
|
250 |
Point point; |
|
251 |
try { |
|
252 |
point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D); |
|
253 |
point.transform(affineTransform.createInverse()); |
|
254 |
graphics.drawImage(image, (int) Math.floor(point.getX()), (int) Math.floor(point.getY()), null); |
|
255 |
} catch (CreateGeometryException | NoninvertibleTransformException e) { |
|
256 |
LOG.warn("Can not calculate the point of buffer in viewport image", e); |
|
257 |
taskStatus.abort(); |
|
258 |
return; |
|
259 |
} |
|
243 | 260 |
} |
244 | 261 |
|
245 | 262 |
if (!isMyTask) { |
... | ... | |
306 | 323 |
for (int i = 0; i < buffer.getRows(); i++) { |
307 | 324 |
for (int j = 0; j < buffer.getColumns(); j++) { |
308 | 325 |
Number redValue = (Number) redBand.get(i, j); |
326 |
Number greenValue = (Number) greenBand.get(i, j); |
|
309 | 327 |
Number blueValue = (Number) blueBand.get(i, j); |
310 |
Number greenValue = (Number) greenBand.get(i, j); |
|
311 | 328 |
|
312 | 329 |
Number alphaValue = 255; |
330 |
|
|
313 | 331 |
if (alphaBand != null) { |
314 | 332 |
alphaValue = (Number) alphaBand.get(i, j); |
315 | 333 |
} |
... | ... | |
710 | 728 |
this.setFilters((FilterList) state.get(FILTERS_PERSISTENCE_FIELD)); |
711 | 729 |
} |
712 | 730 |
|
731 |
@Override |
|
732 |
public void setTransparency(Transparency transparency) { |
|
733 |
this.transparency = transparency; |
|
734 |
} |
|
735 |
|
|
736 |
@Override |
|
737 |
public Transparency getTransparency() { |
|
738 |
return this.transparency; |
|
739 |
} |
|
740 |
|
|
713 | 741 |
} |
Also available in: Unified diff