gvsig-raster / 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 @ 6899
History | View | Annotate | Download (47.8 KB)
1 |
package org.gvsig.raster.lib.legend.impl; |
---|---|
2 |
|
3 |
import java.awt.Graphics; |
4 |
import java.awt.Image; |
5 |
import java.awt.geom.AffineTransform; |
6 |
import java.awt.geom.NoninvertibleTransformException; |
7 |
import java.awt.image.BufferedImage; |
8 |
|
9 |
import org.cresques.cts.ICoordTrans; |
10 |
import org.slf4j.Logger; |
11 |
import org.slf4j.LoggerFactory; |
12 |
|
13 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
14 |
import org.gvsig.fmap.geom.GeometryLocator; |
15 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
16 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
17 |
import org.gvsig.fmap.geom.primitive.Envelope; |
18 |
import org.gvsig.fmap.geom.primitive.Point; |
19 |
import org.gvsig.fmap.mapcontext.ViewPort; |
20 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
21 |
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendContentsChangedListener; |
22 |
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent; |
23 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
24 |
import org.gvsig.raster.lib.buffer.api.Band; |
25 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
26 |
import org.gvsig.raster.lib.buffer.api.FilterList; |
27 |
import org.gvsig.raster.lib.buffer.api.NoData; |
28 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
29 |
import org.gvsig.raster.lib.legend.api.ColorInterpretation; |
30 |
import org.gvsig.raster.lib.legend.api.RasterLegend; |
31 |
import org.gvsig.raster.lib.legend.api.Transparency; |
32 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
33 |
import org.gvsig.tools.ToolsLocator; |
34 |
import org.gvsig.tools.dispose.DisposeUtils; |
35 |
import org.gvsig.tools.dynobject.DynStruct; |
36 |
import org.gvsig.tools.locator.LocatorException; |
37 |
import org.gvsig.tools.persistence.PersistenceManager; |
38 |
import org.gvsig.tools.persistence.PersistentState; |
39 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
40 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
41 |
import org.gvsig.tools.task.SimpleTaskStatus; |
42 |
import org.gvsig.tools.task.TaskStatusManager; |
43 |
|
44 |
/**
|
45 |
* Default implementation of {@link RasterLegend}. This object can draw buffers
|
46 |
* with a {@link ColorInterpretation}, {@link ColorTable} and {@link FilterList}
|
47 |
*
|
48 |
*
|
49 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
50 |
*
|
51 |
*/
|
52 |
public class DefaultRasterLegend implements RasterLegend { |
53 |
|
54 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultRasterLegend.class); |
55 |
|
56 |
private ColorInterpretation colorInterpretation;
|
57 |
private ColorTable colorTable;
|
58 |
private Transparency transparency; |
59 |
private FilterList filters;
|
60 |
private boolean transparentNoData; |
61 |
|
62 |
/**
|
63 |
* Persistence definition name
|
64 |
*/
|
65 |
public static final String PERSISTENT_NAME = "RasterLegendPersistence"; |
66 |
/**
|
67 |
* Description of persistence definition
|
68 |
*/
|
69 |
public static final String PERSISTENT_DESCRIPTION = "Persistence definition of raster legend"; |
70 |
|
71 |
private final static String COLOR_TABLE_PERSISTENCE_FIELD = "colorTable"; |
72 |
private final static String COLOR_INTERPRETATION_PERSISTENCE_FIELD = "colorInterpretation"; |
73 |
private final static String FILTERS_PERSISTENCE_FIELD = "filters"; |
74 |
|
75 |
/**
|
76 |
* Empty constructor
|
77 |
*/
|
78 |
public DefaultRasterLegend() {
|
79 |
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* Default {@link RasterLegend} constructor
|
84 |
*
|
85 |
* @param colorInterpretation
|
86 |
* Color interpretation of legend
|
87 |
*/
|
88 |
public DefaultRasterLegend(ColorInterpretation colorInterpretation) {
|
89 |
this.colorInterpretation = colorInterpretation;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* Default {@link RasterLegend} constructor
|
94 |
*
|
95 |
* @param colorTable
|
96 |
* Color table of legend
|
97 |
* @param colorInterpretation
|
98 |
* Color interpretation of legend
|
99 |
* @param transparency
|
100 |
* Transparency of legend
|
101 |
* @param filters
|
102 |
* Filters of legend
|
103 |
*/
|
104 |
public DefaultRasterLegend(ColorInterpretation colorInterpretation, ColorTable colorTable,
|
105 |
Transparency transparency, FilterList filters) {
|
106 |
this.colorInterpretation = colorInterpretation;
|
107 |
this.colorTable = colorTable;
|
108 |
this.transparency = transparency;
|
109 |
this.filters = filters;
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public void draw(Graphics graphics, Buffer buffer, ViewPort viewPort, |
114 |
SimpleTaskStatus taskStatus) { |
115 |
|
116 |
boolean isMyTask = false; |
117 |
if (taskStatus == null) { |
118 |
TaskStatusManager taskStatusManager = ToolsLocator.getTaskStatusManager(); |
119 |
taskStatus = |
120 |
taskStatusManager.createDefaultSimpleTaskStatus("_drawing_buffer_XxellipsisxX");
|
121 |
taskStatus.setAutoremove(true);
|
122 |
taskStatus.add(); |
123 |
isMyTask = true;
|
124 |
} else {
|
125 |
taskStatus.push(); |
126 |
} |
127 |
|
128 |
taskStatus.setIndeterminate(); |
129 |
|
130 |
if (this.colorInterpretation == null || !this.colorInterpretation.hasInterpretation() |
131 |
|| this.colorInterpretation.isUndefined()) {
|
132 |
taskStatus.abort(); |
133 |
throw new IllegalStateException( |
134 |
"To draw buffer, raster legend has to have a color interpretation");
|
135 |
} |
136 |
|
137 |
if ((this.colorInterpretation.isRGB() || this.colorInterpretation.isBGR() || this.colorInterpretation |
138 |
.isRGBA()|| this.colorInterpretation.isGray() || this.colorInterpretation.hasAnyRGBBand()) && this.colorTable != null) { |
139 |
taskStatus.abort(); |
140 |
throw new IllegalStateException( |
141 |
"If color interpretation is RGB, RGBA or BGR, raster legend can not have a color table");
|
142 |
} |
143 |
|
144 |
// Check if viewport projection is the same as buffer projection
|
145 |
Buffer clip = null; |
146 |
Buffer interpolated = null; |
147 |
Buffer converted = null; |
148 |
Buffer interpolated2 = null; |
149 |
Buffer bufferToDraw = buffer;
|
150 |
try {
|
151 |
if (bufferToDraw != null && bufferToDraw.getColumns()>0 && bufferToDraw.getRows()>0) { |
152 |
|
153 |
if (!viewPort.getProjection().equals(bufferToDraw.getProjection())) {
|
154 |
|
155 |
// Convert extension to check if envelopes intersect
|
156 |
ICoordTrans coordTrans = viewPort.getProjection().getCT(bufferToDraw.getProjection()); |
157 |
ICoordTrans invertedCoordTrans = bufferToDraw.getProjection().getCT(viewPort.getProjection()); |
158 |
Envelope convertedEnvelope = viewPort.getAdjustedEnvelope().convert(coordTrans); |
159 |
double viewPortPixelSizeX = viewPort.getAdjustedEnvelope().getLength(0) / viewPort.getImageWidth(); |
160 |
double viewPortPixelSizeY = viewPort.getAdjustedEnvelope().getLength(1) / viewPort.getImageHeight(); |
161 |
if (!convertedEnvelope.intersects(bufferToDraw.getEnvelope())) {
|
162 |
return;
|
163 |
} |
164 |
try {
|
165 |
// Clip buffer with doubled converted envelope
|
166 |
clip = bufferToDraw.clip(convertedEnvelope); |
167 |
Envelope bufferEnvelopeInViewPortCoords = clip.getEnvelope().convert(invertedCoordTrans); |
168 |
|
169 |
double widthPixel = bufferEnvelopeInViewPortCoords.getLength(0) / viewPortPixelSizeX; |
170 |
double heightPixel = bufferEnvelopeInViewPortCoords.getLength(1) / viewPortPixelSizeY; |
171 |
|
172 |
interpolated = |
173 |
clip.createInterpolated((int) Math.floor(heightPixel), (int) Math.floor(widthPixel), |
174 |
Buffer.INTERPOLATION_NearestNeighbour, taskStatus);
|
175 |
// Convert interpolated clipped buffer
|
176 |
converted = interpolated.convert(invertedCoordTrans, taskStatus); |
177 |
|
178 |
widthPixel = getWidthPixel(converted.getEnvelope(), viewPortPixelSizeX); |
179 |
heightPixel = getHeightPixel(converted.getEnvelope(), viewPortPixelSizeY); |
180 |
|
181 |
interpolated2 = |
182 |
converted.createInterpolated((int) Math.floor(heightPixel), (int) Math.floor(widthPixel), |
183 |
Buffer.INTERPOLATION_NearestNeighbour, taskStatus);
|
184 |
|
185 |
bufferToDraw = interpolated2; |
186 |
|
187 |
} catch (BufferException | LocatorException | CreateEnvelopeException e) {
|
188 |
LOG.warn("Buffer can not be clipped, converted or interpolated", e);
|
189 |
taskStatus.abort(); |
190 |
return;
|
191 |
} |
192 |
} else if (viewPort.getAdjustedEnvelope().intersects(bufferToDraw.getEnvelope())) { |
193 |
|
194 |
double widthPixel = 0; |
195 |
double heightPixel = 0; |
196 |
try {
|
197 |
// Clip and interpolate buffer with view port envelope
|
198 |
if (!bufferToDraw.getEnvelope().equals(viewPort.getAdjustedEnvelope())) {
|
199 |
clip = bufferToDraw.clip(viewPort.getAdjustedEnvelope()); |
200 |
bufferToDraw = clip; |
201 |
} |
202 |
widthPixel = |
203 |
getWidthPixel(bufferToDraw.getEnvelope(), viewPort.getAdjustedEnvelope().getLength(0)
|
204 |
/ viewPort.getImageWidth()); |
205 |
heightPixel = |
206 |
getHeightPixel(bufferToDraw.getEnvelope(), viewPort.getAdjustedEnvelope().getLength(1)
|
207 |
/ viewPort.getImageHeight()); |
208 |
|
209 |
interpolated = |
210 |
bufferToDraw.createInterpolated((int) Math.floor(heightPixel), |
211 |
(int) Math.floor(widthPixel), Buffer.INTERPOLATION_NearestNeighbour, taskStatus); |
212 |
bufferToDraw = interpolated; |
213 |
} catch (BufferException e) {
|
214 |
LOG.warn( |
215 |
"Buffer can not be interpolated with [rows: {} , columns: {}, method: {}]",
|
216 |
new String[] { String.valueOf((int) Math.floor(heightPixel)), |
217 |
String.valueOf((int) Math.floor(widthPixel)), |
218 |
String.valueOf(Buffer.INTERPOLATION_NearestNeighbour) }); |
219 |
taskStatus.abort(); |
220 |
return;
|
221 |
} |
222 |
} else {
|
223 |
// Do nothing view port envelope does not intersect with
|
224 |
// buffer
|
225 |
return;
|
226 |
} |
227 |
} |
228 |
|
229 |
if (bufferToDraw != null && bufferToDraw.getColumns()>0 && bufferToDraw.getRows()>0) { |
230 |
|
231 |
// Draw buffer
|
232 |
Image image = null; |
233 |
if (this.colorInterpretation.hasAnyGrayBand()) { |
234 |
// Draw buffer with gray scale
|
235 |
image = drawGrayBuffer(graphics, bufferToDraw, transparency, filters); |
236 |
} else if (this.colorInterpretation.hasAnyPaletteBand() && this.colorTable != null) { |
237 |
// Draw buffer with table color
|
238 |
image = drawPaletteBuffer(graphics, bufferToDraw, colorTable, transparency, filters); |
239 |
// } else if (this.colorInterpretation.isRGBA() || this.colorInterpretation.isRGB()
|
240 |
// || this.colorInterpretation.isBGR() || this.colorInterpretation.hasRGBBands()) {
|
241 |
} else if (this.colorInterpretation.hasAnyRGBBand()) { |
242 |
// Draw RGB, RGBA or BGR buffer without color table
|
243 |
image = drawRGBBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
244 |
} else if (this.colorInterpretation.hasAnyHSLBand()) { |
245 |
// Draw HSL buffer without color table
|
246 |
image = drawHSLBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
247 |
} else if (this.colorInterpretation.hasAnyCMYKBand()) { |
248 |
// Draw CMYK buffer without color table
|
249 |
image = drawCMYKBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
250 |
} else if (this.colorInterpretation.hasAnyYCBCRBand()) { |
251 |
// Draw YCBCR buffer without color table
|
252 |
image = drawYCBCRBuffer(graphics, bufferToDraw, colorInterpretation, transparency, filters); |
253 |
} |
254 |
|
255 |
// Calculate where image has to be drawn
|
256 |
double x = bufferToDraw.getEnvelope().getMinimum(0); |
257 |
double y = bufferToDraw.getEnvelope().getMaximum(1); |
258 |
AffineTransform affineTransform =
|
259 |
calculateAffineTransform(viewPort.getAdjustedEnvelope(), viewPort.getImageWidth(), |
260 |
viewPort.getImageHeight()); |
261 |
|
262 |
Point point;
|
263 |
try {
|
264 |
point = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D); |
265 |
point.transform(affineTransform.createInverse()); |
266 |
graphics.drawImage(image, (int) Math.floor(point.getX()), (int) Math.floor(point.getY()), null); |
267 |
} catch (CreateGeometryException | NoninvertibleTransformException e) { |
268 |
LOG.warn("Can not calculate the point of buffer in viewport image", e);
|
269 |
taskStatus.abort(); |
270 |
return;
|
271 |
} |
272 |
} |
273 |
} catch (LocatorException | CreateEnvelopeException e1) {
|
274 |
LOG.warn("Can not calculate the envelope of buffer", e1);
|
275 |
taskStatus.abort(); |
276 |
return;
|
277 |
} finally {
|
278 |
if (clip != null) { |
279 |
DisposeUtils.dispose(clip); |
280 |
clip = null;
|
281 |
} |
282 |
if (interpolated != null) { |
283 |
DisposeUtils.dispose(interpolated); |
284 |
interpolated = null;
|
285 |
} |
286 |
if (converted != null) { |
287 |
DisposeUtils.dispose(converted); |
288 |
converted = null;
|
289 |
} |
290 |
if (interpolated2 != null) { |
291 |
DisposeUtils.dispose(interpolated2); |
292 |
interpolated2 = null;
|
293 |
} |
294 |
if (bufferToDraw == null && bufferToDraw != buffer) { |
295 |
DisposeUtils.dispose(bufferToDraw); |
296 |
bufferToDraw = null;
|
297 |
} |
298 |
|
299 |
if (!isMyTask) {
|
300 |
taskStatus.pop(); |
301 |
} |
302 |
} |
303 |
} |
304 |
|
305 |
private AffineTransform calculateAffineTransform(Envelope envelope, double imageWidth, |
306 |
double imageHeight) {
|
307 |
double pixelSizeX = envelope.getLength(0) / imageWidth; |
308 |
double rotationX = 0; |
309 |
double x = envelope.getMinimum(0); |
310 |
double rotationY = 0; |
311 |
double pixelSizeY = -envelope.getLength(1) / imageHeight; |
312 |
double y = envelope.getMaximum(1); |
313 |
return new AffineTransform(pixelSizeX, rotationY, rotationX, pixelSizeY, x, y); |
314 |
} |
315 |
|
316 |
private double getWidthPixel(Envelope envelope, double dist1pixel) { |
317 |
double widthEnvelope = envelope.getLength(0); |
318 |
return widthEnvelope / dist1pixel;
|
319 |
} |
320 |
|
321 |
private double getHeightPixel(Envelope envelope, double dist1pixel) { |
322 |
double heightEnvelope = envelope.getLength(1); |
323 |
return heightEnvelope / dist1pixel;
|
324 |
} |
325 |
|
326 |
|
327 |
|
328 |
private Image drawRGBBuffer(Graphics graphics, Buffer buffer, |
329 |
ColorInterpretation colorInterpretation, Transparency transparency, FilterList filters) {
|
330 |
|
331 |
BufferedImage image = null; |
332 |
|
333 |
if (colorInterpretation.isRGB() || colorInterpretation.isRGBA() || colorInterpretation.hasAnyRGBBand()) {
|
334 |
image = ToolsSwingLocator.getToolsSwingManager().createBufferedImage(buffer.getColumns(), buffer.getRows(), |
335 |
BufferedImage.TYPE_INT_ARGB);
|
336 |
} else if (colorInterpretation.isBGR()) { |
337 |
image = ToolsSwingLocator.getToolsSwingManager().createBufferedImage(buffer.getColumns(), buffer.getRows(), |
338 |
BufferedImage.TYPE_INT_BGR);
|
339 |
} |
340 |
|
341 |
if (image == null) { |
342 |
throw new IllegalStateException( |
343 |
"Color interpretation is not RGB,RGBA or BGR and buffer was be drawn without color table");
|
344 |
} |
345 |
|
346 |
int redBandIndex = colorInterpretation.getBand(ColorInterpretation.RED_BAND);
|
347 |
int greenBandIndex = colorInterpretation.getBand(ColorInterpretation.GREEN_BAND);
|
348 |
int blueBandIndex = colorInterpretation.getBand(ColorInterpretation.BLUE_BAND);
|
349 |
|
350 |
Band redBand = null;
|
351 |
if(redBandIndex>=0){ |
352 |
redBand = buffer.getBand(redBandIndex); |
353 |
} |
354 |
Band greenBand = null;
|
355 |
if(greenBandIndex>=0){ |
356 |
greenBand = buffer.getBand(greenBandIndex); |
357 |
} |
358 |
Band blueBand = null;
|
359 |
if(blueBandIndex>=0){ |
360 |
blueBand = buffer.getBand(blueBandIndex); |
361 |
} |
362 |
Band alphaBand = null;
|
363 |
if (colorInterpretation.hasAlphaBand()) {
|
364 |
int alphaBandIndex =
|
365 |
colorInterpretation.getBand(ColorInterpretation.ALPHA_BAND); |
366 |
alphaBand = buffer.getBand(alphaBandIndex); |
367 |
} |
368 |
// Statistics statistics = buffer.getStatistics(null);
|
369 |
// double[] max = statistics.getMax(); //Para pruebas con el rango de cada banda
|
370 |
// double[] min = statistics.getMin(); //Para pruebas con el rango de cada banda
|
371 |
// double maximum = statistics.getMaximun(); //Para pruebas con el rango conjunto de todas las bandas
|
372 |
// double minimum = statistics.getMinimun(); //Para pruebas con el rango conjunto de todas las bandas
|
373 |
|
374 |
|
375 |
for (int i = 0; i < buffer.getRows(); i++) { |
376 |
for (int j = 0; j < buffer.getColumns(); j++) { |
377 |
// Number redValue = (Number) redBand.get(i, j);
|
378 |
// Number greenValue = (Number) greenBand.get(i, j);
|
379 |
// Number blueValue = (Number) blueBand.get(i, j);
|
380 |
|
381 |
/*FIXME:
|
382 |
* Aqu? necesitamos pasar los valores originales de la capa al rango de valores del tipo byte.
|
383 |
* Para hacer esto tendr?amos varias opciones:
|
384 |
*
|
385 |
* 1.- Como hace el raster viejo, una conversi?n lineal entre el rango de valores de la capa [MIN, MAX]
|
386 |
* y el rango de valores de tipo byte [0 - 255] para lo cual necesitar?amos llamar aqu? a las estad?sticas.
|
387 |
*
|
388 |
* 2.- Una conversi?n lineal entre el rango de valores del tipo del Number concreto (p. ej. Short [-32768 - 32767],
|
389 |
* Int [-2^31 - 2^31-1], etc...) y el rango de valores de tipo byte [0 - 255], pero cuando los valores
|
390 |
* fueran relativamente peque?os se concentrar?an todos en el 0 y la capa saldr?a completamente negra.
|
391 |
*
|
392 |
* 3.- Una conversi?n logaritmica entre el rango de valores del tipo del Number concreto (p. ej. Short [-32768 - 32767],
|
393 |
* Int [-2^31 - 2^31-1], etc...) y el rango de valores de tipo byte [0 - 255], esto evitar?a la
|
394 |
* concentraci?n de la mayor?a de valores en el 0.
|
395 |
*
|
396 |
* 4.- Y una mejor opci?n ser?a la conversi?n (linear o logaritm?tca, por decidir) entre el rango de valores
|
397 |
* del "sensor" con el que se han tomado los datos y el rango de valores de tipo byte [0 - 255], pero
|
398 |
* , por lo menos por ahora, no tenemos informaci?n sobre el sensor.
|
399 |
*
|
400 |
* Hago pruebas de las tres primeras opciones, comentarizando dos y dejando activa la otra,
|
401 |
* pero es algo a pensar con detenimiento.
|
402 |
*/
|
403 |
|
404 |
int alphaByteValue = 255; |
405 |
int redByteValue = 0; |
406 |
if(redBand!=null){ |
407 |
Number redValue = (Number) redBand.get(i, j); |
408 |
NoData noData = redBand.getNoData(); |
409 |
if(noData != null){ |
410 |
if(redValue.equals(noData.getValue())){
|
411 |
alphaByteValue = 0;
|
412 |
redByteValue = 0;
|
413 |
} else {
|
414 |
alphaByteValue = 255;
|
415 |
redByteValue = logarithmicConversionToByteRange(redValue); |
416 |
} |
417 |
} else {
|
418 |
redByteValue = logarithmicConversionToByteRange(redValue); |
419 |
} |
420 |
// byte redByteValue = logarithmicConversionToByte(redValue,max[redBandIndex]);
|
421 |
// byte redByteValue = linearConversionToByte(redValue);
|
422 |
// redByteValue = proportionalConversionToByte(redValue, max[redBandIndex], min[redBandIndex]);
|
423 |
// byte redByteValue = proportionalConversionToByte(redValue, maximum, minimum); //1
|
424 |
} |
425 |
int greenByteValue = 0; |
426 |
if(greenBand!=null){ |
427 |
Number greenValue = (Number) greenBand.get(i, j); |
428 |
NoData noData = greenBand.getNoData(); |
429 |
if(noData != null){ |
430 |
if(greenValue.equals(noData.getValue())){
|
431 |
alphaByteValue = 0;
|
432 |
greenByteValue = 0;
|
433 |
} else {
|
434 |
alphaByteValue = 255;
|
435 |
greenByteValue = logarithmicConversionToByteRange(greenValue); |
436 |
} |
437 |
} else {
|
438 |
greenByteValue = logarithmicConversionToByteRange(greenValue); |
439 |
} |
440 |
// byte greenByteValue = logarithmicConversionToByte(greenValue,max[greenBandIndex]);
|
441 |
// byte greenByteValue = linearConversionToByte(greenValue);
|
442 |
// greenByteValue = proportionalConversionToByte(greenValue, max[greenBandIndex], min[greenBandIndex]);
|
443 |
// byte greenByteValue = proportionalConversionToByte(greenValue, maximum, minimum);
|
444 |
} |
445 |
int blueByteValue = 0; |
446 |
if(blueBand!=null){ |
447 |
Number blueValue = (Number) blueBand.get(i, j); |
448 |
NoData noData = blueBand.getNoData(); |
449 |
if(noData != null){ |
450 |
if(blueValue.equals(noData.getValue())){
|
451 |
alphaByteValue = 0;
|
452 |
blueByteValue = 0;
|
453 |
} else {
|
454 |
alphaByteValue = 255;
|
455 |
blueByteValue = logarithmicConversionToByteRange(blueValue); |
456 |
} |
457 |
} else {
|
458 |
blueByteValue = logarithmicConversionToByteRange(blueValue); |
459 |
} |
460 |
|
461 |
|
462 |
// byte blueByteValue = logarithmicConversionToByte(blueValue,max[blueBandIndex]);
|
463 |
// byte blueByteValue = linearConversionToByte(blueValue);
|
464 |
// blueByteValue = proportionalConversionToByte(blueValue, max[blueBandIndex], min[blueBandIndex]);
|
465 |
// byte blueByteValue = proportionalConversionToByte(blueValue, maximum, minimum);
|
466 |
} |
467 |
|
468 |
|
469 |
if (alphaByteValue!=0 && alphaBand != null) { |
470 |
Number alphaValue = (Number) alphaBand.get(i, j); |
471 |
int alphaBandIndex = colorInterpretation.getBand(ColorInterpretation.ALPHA_BAND);
|
472 |
// alphaByteValue = logarithmicConversionToByte(alphaValue);
|
473 |
NoData noData = alphaBand.getNoData(); |
474 |
if(noData != null){ |
475 |
if(alphaValue.equals(noData.getValue())){
|
476 |
alphaByteValue = 0;
|
477 |
} else {
|
478 |
alphaByteValue = logarithmicConversionToByteRange(alphaValue); |
479 |
} |
480 |
} else {
|
481 |
alphaByteValue = logarithmicConversionToByteRange(alphaValue); |
482 |
} |
483 |
|
484 |
// alphaByteValue = logarithmicConversionToByte(alphaValue,max[alphaBandIndex]);
|
485 |
// alphaByteValue = linearConversionToByte(alphaValue);
|
486 |
// alphaByteValue = proportionalConversionToByte(alphaValue, max[alphaBandIndex], min[alphaBandIndex]);
|
487 |
// alphaByteValue = proportionalConversionToByte(alphaValue, maximum, minimum);
|
488 |
// LOG.info("alphaValue = "+alphaValue+ " alphaByteValue = "+alphaByteValue);
|
489 |
} |
490 |
|
491 |
if (alphaByteValue!=0 && transparency != null) { |
492 |
// Apply defined transparency ranges
|
493 |
|
494 |
int newAlpha =
|
495 |
transparency.getTransparencyRangeAlpha(redByteValue, |
496 |
greenByteValue, blueByteValue); |
497 |
alphaByteValue = getNewAlpha(alphaByteValue, newAlpha); |
498 |
|
499 |
// Apply general transparency
|
500 |
int transparencyValue = transparency.getAlpha();
|
501 |
alphaByteValue = getNewAlpha(alphaByteValue, transparencyValue); |
502 |
} |
503 |
|
504 |
if (filters != null) { |
505 |
// TODO Apply filters
|
506 |
} |
507 |
|
508 |
int intRGB = 0; |
509 |
if (colorInterpretation.hasAnyRGBBand()) {//colorInterpretation.isRGB() || colorInterpretation.isRGBA()) { |
510 |
intRGB = (((byte)alphaByteValue & 0xFF) << 24) | // alpha |
511 |
(((byte)redByteValue & 0xFF) << 16) | // red |
512 |
(((byte)greenByteValue & 0xFF) << 8) | // green |
513 |
(((byte)blueByteValue & 0xFF)); // blue String.format("%X", b) |
514 |
// intRGB = ((alphaByteValue & 0xFF) << 24) | // alpha
|
515 |
// ((redByteValue & 0xFF) << 16) | // red
|
516 |
// ((greenByteValue & 0xFF) << 8) | // green
|
517 |
// ((blueByteValue & 0xFF) << 0); // blue String.format("%X", b)
|
518 |
} else if (colorInterpretation.isBGR()) { |
519 |
intRGB = (((byte)blueByteValue & 0xFF) << 16 | // blue |
520 |
(((byte)greenByteValue & 0xFF) << 8) | // green |
521 |
(((byte)redByteValue & 0xFF))); // red |
522 |
|
523 |
} |
524 |
image.setRGB(j, i, intRGB); |
525 |
} |
526 |
} |
527 |
return image;
|
528 |
} |
529 |
|
530 |
/**
|
531 |
* Conversi?n logaritmica de los valores del dominio de entrada a valores del dominio de BYTE
|
532 |
* para poder ser representados en RGB.
|
533 |
*
|
534 |
* @param n
|
535 |
* @return
|
536 |
*/
|
537 |
private int logarithmicConversionToByteRange(Number n) { |
538 |
int b = 0; |
539 |
if (n instanceof Byte) { |
540 |
b = n.byteValue(); |
541 |
if(b<0){ |
542 |
b=256+b;
|
543 |
} |
544 |
} else if (n instanceof Short) { |
545 |
// Because Math.log(Short.MAX_VALUE-Short.MIN_VALUE)/Math.log(256) = 2;
|
546 |
b = (new Double(Math.round(Math.pow(n.shortValue(), 1d / 2d)))).intValue(); |
547 |
} else if (n instanceof Integer) { |
548 |
// Because
|
549 |
// Math.log(Integer.MAX_VALUE/256-Integer.MIN_VALUE/256)/Math.log(256) = 4;
|
550 |
b = (new Double(Math.round(Math.pow(n.intValue(), 1d / 4d)))).intValue(); |
551 |
} else if (n instanceof Float) { |
552 |
// Because Math.log(Float.MAX_VALUE-Float.MIN_VALUE)/Math.log(256) = 16;
|
553 |
double root = 16d; // Math.log(Float.MAX_VALUE-Float.MIN_VALUE)/Math.log(256); |
554 |
b = (new Double(Math.round(Math.pow(n.floatValue(), 1d / root)))).intValue(); |
555 |
} else if (n instanceof Double) { |
556 |
// Because Math.log(Double.MAX_VALUE-Double.MIN_VALUE)/Math.log(256) = 128;
|
557 |
double root = 128d; |
558 |
b = (new Double(Math.round(Math.pow(n.doubleValue(), 1d / root)))).intValue(); |
559 |
} |
560 |
return b;
|
561 |
} |
562 |
|
563 |
/**
|
564 |
* Conversi?n logaritmica de los valores del dominio de entrada a valores del dominio de BYTE
|
565 |
* para poder ser representados en RGB.
|
566 |
* Aplica un factor de correcci?n que depende de los valores de entrada.
|
567 |
*
|
568 |
* @param n
|
569 |
* @param max
|
570 |
* @return
|
571 |
*/
|
572 |
private int logarithmicConversionToByteRange(Number n, double max) { |
573 |
int b = 0; |
574 |
if (n instanceof Byte) { |
575 |
b = n.byteValue(); |
576 |
if(b<0){ |
577 |
b=256+b;
|
578 |
} |
579 |
} else if (n instanceof Short) { |
580 |
short value = n.shortValue();
|
581 |
double factor = Short.MAX_VALUE / max; |
582 |
// Because Math.log(Short.MAX_VALUE-Short.MIN_VALUE)/Math.log(256) = 2;
|
583 |
double root = 2d; |
584 |
b = (new Double(Math.round(Math.pow(value, 1d / root)*factor))).intValue(); |
585 |
} else if (n instanceof Integer) { |
586 |
int value = n.intValue();
|
587 |
double factor = Integer.MAX_VALUE / max; |
588 |
// Because Math.log(Integer.MAX_VALUE/256-Integer.MIN_VALUE/256)/Math.log(256) = 4;
|
589 |
double root = 4d; |
590 |
b = (new Double(Math.round(Math.pow(value, 1d / root)*factor))).intValue(); |
591 |
} else if (n instanceof Float) { |
592 |
float value = n.floatValue();
|
593 |
double factor = Float.MAX_VALUE / max; |
594 |
// Because Math.log(Float.MAX_VALUE-Float.MIN_VALUE)/Math.log(256) = 16;
|
595 |
double root = 16d; |
596 |
b = (new Double(Math.round(Math.pow(value, 1d / root)*factor))).intValue(); |
597 |
} else if (n instanceof Double) { |
598 |
double value = n.doubleValue();
|
599 |
double factor = Double.MAX_VALUE / max; |
600 |
// Because Math.log(Double.MAX_VALUE-Double.MIN_VALUE)/Math.log(256) = 128;
|
601 |
double root = 128d; |
602 |
b = (new Double(Math.round(Math.pow(value, 1d / root)*factor))).intValue(); |
603 |
} |
604 |
return b;
|
605 |
} |
606 |
|
607 |
/**
|
608 |
* Conversi?n lineal de los valores del dominio de entrada a valores del dominio de BYTE
|
609 |
* para poder ser representados en RGB.
|
610 |
*
|
611 |
* @param n
|
612 |
* @return
|
613 |
*/
|
614 |
private int linearConversionToByteRange(Number n){ |
615 |
int b=0; |
616 |
if(n instanceof Byte){ |
617 |
b = n.byteValue(); |
618 |
if(b<0){ |
619 |
b=256+b;
|
620 |
} |
621 |
|
622 |
} else if(n instanceof Short){ |
623 |
// 65536/256 = 256
|
624 |
b = (new Double(n.shortValue()/256)).intValue(); |
625 |
} else if(n instanceof Integer){ |
626 |
// (Integer.MAX_VALUE-Integer.MIN_VALUE)/256 = 16777215
|
627 |
b = (new Double(n.intValue()/16777215)).intValue(); |
628 |
} else if(n instanceof Float){ |
629 |
// (Float.MAX_VALUE-Float.MIN_VALUE)/256 = 1.3292279E36
|
630 |
b = (new Double(n.floatValue()/1.3292279E36)).intValue(); |
631 |
} else if(n instanceof Double){ |
632 |
// (Double.MAX_VALUE-Double.MIN_VALUE)/256 = 7.022238808055921E305;
|
633 |
b = (new Double(n.doubleValue()/7.022238808055921E305)).intValue(); |
634 |
} |
635 |
return b;
|
636 |
} |
637 |
|
638 |
/**
|
639 |
* Conversi?n lineal de los valores de entrada a valores del dominio de BYTE
|
640 |
* para poder ser representados en RGB.
|
641 |
* Depende del rango de valores del buffer a representar.
|
642 |
*
|
643 |
* @param n
|
644 |
* @param maximum
|
645 |
* @param minimum
|
646 |
* @return
|
647 |
*/
|
648 |
private int proportionalConversionToByteRange(Number n, double maximum, double minimum){ |
649 |
int b=0; |
650 |
double ratio = (maximum-minimum)/256; |
651 |
if(n instanceof Byte){ |
652 |
b = n.byteValue(); |
653 |
if(b<0){ |
654 |
b=256+b;
|
655 |
} |
656 |
} else if(n instanceof Short){ |
657 |
// 65536/256 = 256
|
658 |
b = (new Double((n.shortValue()-minimum)/ratio)).intValue(); |
659 |
} else if(n instanceof Integer){ |
660 |
// (Integer.MAX_VALUE-Integer.MIN_VALUE)/256 = 16777215
|
661 |
b = (new Double((n.intValue()-minimum)/ratio)).intValue(); |
662 |
} else if(n instanceof Float){ |
663 |
// (Float.MAX_VALUE-Float.MIN_VALUE)/256 = 1.3292279E36
|
664 |
b = (new Double((n.floatValue()-minimum)/ratio)).intValue(); |
665 |
} else if(n instanceof Double){ |
666 |
// (Double.MAX_VALUE-Double.MIN_VALUE)/256 = 7.022238808055921E305;
|
667 |
b = (new Double((n.doubleValue()-minimum)/ratio)).intValue(); |
668 |
} |
669 |
return b;
|
670 |
} |
671 |
|
672 |
|
673 |
private Image drawHSLBuffer(Graphics graphics, Buffer buffer, |
674 |
ColorInterpretation colorInterpretation,Transparency transparency,
|
675 |
FilterList filters) { |
676 |
|
677 |
BufferedImage image = null; |
678 |
|
679 |
image = ToolsSwingLocator.getToolsSwingManager().createBufferedImage(buffer.getColumns(), buffer.getRows(), |
680 |
BufferedImage.TYPE_INT_ARGB);
|
681 |
|
682 |
int hueBandIndex = colorInterpretation.getBand(ColorInterpretation.HUE_BAND);
|
683 |
int saturationBandIndex = colorInterpretation.getBand(ColorInterpretation.SATURATION_BAND);
|
684 |
int lightBandIndex = colorInterpretation.getBand(ColorInterpretation.LIGHTNESS_BAND);
|
685 |
|
686 |
|
687 |
Band hueBand = buffer.getBand(hueBandIndex); |
688 |
Band saturationBand = buffer.getBand(saturationBandIndex); |
689 |
Band lightBand = buffer.getBand(lightBandIndex); |
690 |
|
691 |
|
692 |
for (int i = 0; i < buffer.getRows(); i++) { |
693 |
for (int j = 0; j < buffer.getColumns(); j++) { |
694 |
Number hueValue = (Number) hueBand.get(i, j); |
695 |
Number saturationValue = (Number) saturationBand.get(i, j); |
696 |
Number lightValue = (Number) lightBand.get(i, j); |
697 |
|
698 |
Number[] rgbaValues=ColorUtils.fromHSLtoRGBA(hueValue.floatValue(), saturationValue.floatValue(), lightValue.floatValue()); |
699 |
|
700 |
Integer alphaValue =rgbaValues[3].intValue(); |
701 |
if (transparency != null) { |
702 |
// Apply defined transparency ranges
|
703 |
|
704 |
alphaValue = alphaValue.byteValue() & 0xFF;
|
705 |
int newAlpha =
|
706 |
transparency.getTransparencyRangeAlpha(rgbaValues[0].byteValue(),
|
707 |
rgbaValues[1].byteValue(), rgbaValues[2].byteValue()); |
708 |
alphaValue = getNewAlpha(alphaValue.intValue(), newAlpha); |
709 |
|
710 |
// Apply general transparency
|
711 |
int transparencyValue = transparency.getAlpha();
|
712 |
alphaValue = getNewAlpha(alphaValue.intValue(), transparencyValue); |
713 |
} |
714 |
|
715 |
if (filters != null) { |
716 |
// TODO Apply filters
|
717 |
} |
718 |
|
719 |
int intRGB = 0; |
720 |
intRGB = ((alphaValue.byteValue() & 0xFF) << 24) | // alpha |
721 |
((rgbaValues[0].byteValue() & 0xFF) << 16) | // red |
722 |
((rgbaValues[1].byteValue() & 0xFF) << 8) | // green |
723 |
((rgbaValues[2].byteValue() & 0xFF) << 0); // blue |
724 |
image.setRGB(j, i, intRGB); |
725 |
} |
726 |
} |
727 |
return image;
|
728 |
} |
729 |
|
730 |
private Image drawCMYKBuffer(Graphics graphics, Buffer buffer, |
731 |
ColorInterpretation colorInterpretation,Transparency transparency,
|
732 |
FilterList filters) { |
733 |
|
734 |
BufferedImage image = null; |
735 |
|
736 |
image = ToolsSwingLocator.getToolsSwingManager().createBufferedImage(buffer.getColumns(), buffer.getRows(), |
737 |
BufferedImage.TYPE_INT_ARGB);
|
738 |
|
739 |
int cyanBandIndex = colorInterpretation.getBand(ColorInterpretation.CYAN_BAND);
|
740 |
int magentaBandIndex = colorInterpretation.getBand(ColorInterpretation.MAGENTA_BAND);
|
741 |
int yellowBandIndex = colorInterpretation.getBand(ColorInterpretation.YELLOW_BAND);
|
742 |
int blackBandIndex = colorInterpretation.getBand(ColorInterpretation.BLACK_BAND);
|
743 |
|
744 |
|
745 |
Band cyanBand = buffer.getBand(cyanBandIndex); |
746 |
Band magentaBand = buffer.getBand(magentaBandIndex); |
747 |
Band yellowBand = buffer.getBand(yellowBandIndex); |
748 |
Band blackBand = buffer.getBand(blackBandIndex); |
749 |
|
750 |
for (int i = 0; i < buffer.getRows(); i++) { |
751 |
for (int j = 0; j < buffer.getColumns(); j++) { |
752 |
Number cyanValue = (Number) cyanBand.get(i, j); |
753 |
Number magentaValue = (Number) magentaBand.get(i, j); |
754 |
Number yellowValue = (Number) yellowBand.get(i, j); |
755 |
Number blackValue = (Number) blackBand.get(i, j); |
756 |
|
757 |
|
758 |
|
759 |
Number[] rgbValues=ColorUtils.fromCMYKtoRGB( |
760 |
cyanValue.floatValue(), |
761 |
magentaValue.floatValue(), |
762 |
yellowValue.floatValue(), |
763 |
blackValue.floatValue() |
764 |
); |
765 |
|
766 |
Integer alphaValue =255; |
767 |
if (transparency != null) { |
768 |
// Apply defined transparency ranges
|
769 |
|
770 |
alphaValue = alphaValue.byteValue() & 0xFF;
|
771 |
int newAlpha =
|
772 |
transparency.getTransparencyRangeAlpha(rgbValues[0].byteValue(),
|
773 |
rgbValues[1].byteValue(), rgbValues[2].byteValue()); |
774 |
alphaValue = getNewAlpha(alphaValue.intValue(), newAlpha); |
775 |
|
776 |
// Apply general transparency
|
777 |
int transparencyValue = transparency.getAlpha();
|
778 |
alphaValue = getNewAlpha(alphaValue.intValue(), transparencyValue); |
779 |
} |
780 |
|
781 |
if (filters != null) { |
782 |
// TODO Apply filters
|
783 |
} |
784 |
|
785 |
int intRGB = 0; |
786 |
intRGB = ((alphaValue.byteValue() & 0xFF) << 24) | // alpha |
787 |
((rgbValues[0].byteValue() & 0xFF) << 16) | // red |
788 |
((rgbValues[1].byteValue() & 0xFF) << 8) | // green |
789 |
((rgbValues[2].byteValue() & 0xFF) << 0); // blue |
790 |
image.setRGB(j, i, intRGB); |
791 |
} |
792 |
} |
793 |
return image;
|
794 |
} |
795 |
|
796 |
private Image drawYCBCRBuffer(Graphics graphics, Buffer buffer, |
797 |
ColorInterpretation colorInterpretation,Transparency transparency,
|
798 |
FilterList filters) { |
799 |
|
800 |
BufferedImage image = null; |
801 |
|
802 |
image = ToolsSwingLocator.getToolsSwingManager().createBufferedImage(buffer.getColumns(), buffer.getRows(), |
803 |
BufferedImage.TYPE_INT_ARGB);
|
804 |
|
805 |
int yIndex = colorInterpretation.getBand(ColorInterpretation.YCBCR_Y_BAND);
|
806 |
int cbBandIndex = colorInterpretation.getBand(ColorInterpretation.YCBCR_CB_BAND);
|
807 |
int crBandIndex = colorInterpretation.getBand(ColorInterpretation.YCBCR_CR_BAND);
|
808 |
|
809 |
|
810 |
Band yBand = buffer.getBand(yIndex); |
811 |
Band cbBand = buffer.getBand(cbBandIndex); |
812 |
Band crBand = buffer.getBand(crBandIndex); |
813 |
|
814 |
for (int i = 0; i < buffer.getRows(); i++) { |
815 |
for (int j = 0; j < buffer.getColumns(); j++) { |
816 |
Number yValue = (Number) yBand.get(i, j); |
817 |
Number cbValue = (Number) cbBand.get(i, j); |
818 |
Number crValue = (Number) crBand.get(i, j); |
819 |
|
820 |
Number[] rgbValues=ColorUtils.fromYCBCRtoRGB( |
821 |
yValue.floatValue(), |
822 |
cbValue.floatValue(), |
823 |
crValue.floatValue() |
824 |
); |
825 |
|
826 |
Integer alphaValue =255; |
827 |
if (transparency != null) { |
828 |
// Apply defined transparency ranges
|
829 |
|
830 |
alphaValue = alphaValue.byteValue() & 0xFF;
|
831 |
int newAlpha =
|
832 |
transparency.getTransparencyRangeAlpha(rgbValues[0].byteValue(),
|
833 |
rgbValues[1].byteValue(), rgbValues[2].byteValue()); |
834 |
alphaValue = getNewAlpha(alphaValue.intValue(), newAlpha); |
835 |
|
836 |
// Apply general transparency
|
837 |
int transparencyValue = transparency.getAlpha();
|
838 |
alphaValue = getNewAlpha(alphaValue.intValue(), transparencyValue); |
839 |
} |
840 |
|
841 |
if (filters != null) { |
842 |
// TODO Apply filters
|
843 |
} |
844 |
|
845 |
int intRGB = 0; |
846 |
intRGB = ((alphaValue.byteValue() & 0xFF) << 24) | // alpha |
847 |
((rgbValues[0].byteValue() & 0xFF) << 16) | // red |
848 |
((rgbValues[1].byteValue() & 0xFF) << 8) | // green |
849 |
((rgbValues[2].byteValue() & 0xFF) << 0); // blue |
850 |
image.setRGB(j, i, intRGB); |
851 |
} |
852 |
} |
853 |
return image;
|
854 |
} |
855 |
|
856 |
/**
|
857 |
* Method to draw gray buffers
|
858 |
* @param graphics
|
859 |
* @param buffer
|
860 |
* @param filters
|
861 |
* @return
|
862 |
*/
|
863 |
private Image drawGrayBuffer(Graphics graphics, Buffer buffer, Transparency transparency, FilterList filters) { |
864 |
|
865 |
BufferedImage image = null; |
866 |
|
867 |
image = ToolsSwingLocator.getToolsSwingManager().createBufferedImage(buffer.getColumns(), buffer.getRows(), |
868 |
BufferedImage.TYPE_INT_ARGB);
|
869 |
|
870 |
Band greyBand = buffer.getBand(0);
|
871 |
|
872 |
// Statistics statistics = buffer.getStatistics(null);
|
873 |
// double maximum = statistics.getMaximun();// ver drawRGBBuffer
|
874 |
// double minimum = statistics.getMinimun();
|
875 |
|
876 |
|
877 |
for (int i = 0; i < buffer.getRows(); i++) { |
878 |
for (int j = 0; j < buffer.getColumns(); j++) { |
879 |
int greyByteValue = 0; |
880 |
int alphaByteValue = (byte)255; |
881 |
Number greyValue = (Number) greyBand.get(i, j); |
882 |
NoData noData = greyBand.getNoData(); |
883 |
if(noData != null){ |
884 |
if(greyValue.equals(noData.getValue())){
|
885 |
alphaByteValue = 0;
|
886 |
greyByteValue = 0;
|
887 |
} else {
|
888 |
alphaByteValue = 255;
|
889 |
greyByteValue = logarithmicConversionToByteRange(greyValue); |
890 |
} |
891 |
} else {
|
892 |
greyByteValue = logarithmicConversionToByteRange(greyValue); |
893 |
} |
894 |
// byte greyByteValue = linearConversionToByte(greyValue);
|
895 |
// byte greyByteValue = proportionalConversionToByte(greyValue, maximum, minimum); // 1
|
896 |
// byte greyByteValue = logarithmicConversionToByte(greyValue);
|
897 |
// byte greyByteValue = logarithmicConversionToByte(greyValue, maximum);
|
898 |
|
899 |
if (alphaByteValue != 0 && transparency != null) { |
900 |
// Apply defined transparency ranges
|
901 |
|
902 |
// alphaByteValue = alphaValue.byteValue() & 0xFF;
|
903 |
int newAlpha = transparency.getTransparencyRangeAlpha(greyByteValue, greyByteValue, greyByteValue);
|
904 |
alphaByteValue = getNewAlpha(alphaByteValue, newAlpha); |
905 |
|
906 |
// Apply general transparency
|
907 |
int transparencyValue = transparency.getAlpha();
|
908 |
alphaByteValue = getNewAlpha(alphaByteValue, transparencyValue); |
909 |
} |
910 |
|
911 |
if (filters != null) { |
912 |
// TODO Apply filters
|
913 |
} |
914 |
|
915 |
int intARGB = (((byte)alphaByteValue & 0xFF) << 24) | // alpha |
916 |
(((byte)greyByteValue & 0xFF)<< 16) | // red |
917 |
(((byte)greyByteValue & 0xFF)<< 8) | // green |
918 |
(((byte)greyByteValue & 0xFF)); // blue |
919 |
|
920 |
image.setRGB(j, i, intARGB); |
921 |
} |
922 |
} |
923 |
return image;
|
924 |
} |
925 |
|
926 |
private Image drawPaletteBuffer(Graphics graphics, Buffer buffer, ColorTable colorTable, |
927 |
Transparency transparency, FilterList filters) {
|
928 |
|
929 |
BufferedImage image = ToolsSwingLocator.getToolsSwingManager().createBufferedImage(buffer.getColumns(), buffer.getRows(),
|
930 |
BufferedImage.TYPE_INT_ARGB);
|
931 |
|
932 |
for (int i = 0; i < buffer.getRows(); i++) { |
933 |
for (int j = 0; j < buffer.getColumns(); j++) { |
934 |
|
935 |
Object value = buffer.getBand(0).get(i, j); |
936 |
byte[] rgba = colorTable.getRGBA(value); |
937 |
|
938 |
if (transparency != null) { |
939 |
// Apply defined transparency ranges
|
940 |
int alpha = rgba[3] & 0xFF; |
941 |
int newAlpha =
|
942 |
transparency.getTransparencyRangeAlpha(rgba[0], rgba[1], rgba[2]); |
943 |
alpha = getNewAlpha(alpha, newAlpha); |
944 |
|
945 |
// Apply general transparency
|
946 |
int transparencyValue = transparency.getAlpha();
|
947 |
rgba[3] = (byte) getNewAlpha(alpha, transparencyValue); |
948 |
} |
949 |
|
950 |
if (filters != null) { |
951 |
// TODO Apply filters
|
952 |
} |
953 |
|
954 |
int intARGB = ((rgba[3] & 0xFF) << 24) | // alpha |
955 |
((rgba[0] & 0xFF) << 16) | // red |
956 |
((rgba[1] & 0xFF) << 8) | // green |
957 |
((rgba[2] & 0xFF) << 0); // blue |
958 |
|
959 |
image.setRGB(j, i, intARGB); |
960 |
} |
961 |
} |
962 |
return image;
|
963 |
} |
964 |
|
965 |
private int getNewAlpha(int alpha, int newAlpha) { |
966 |
// if(alpha<0){
|
967 |
// alpha = 256+alpha;
|
968 |
// }
|
969 |
// if(newAlpha<0){
|
970 |
// newAlpha = 256+newAlpha;
|
971 |
// }
|
972 |
return (int) Math.round(alpha * (newAlpha / 255d)); |
973 |
} |
974 |
|
975 |
public ColorTable getColorTable() {
|
976 |
return this.colorTable; |
977 |
} |
978 |
|
979 |
@Override
|
980 |
public void setColorTable(ColorTable colorTable) { |
981 |
ColorInterpretation colorInterpretation = this.getColorInterpretation();
|
982 |
|
983 |
if (colorInterpretation != null |
984 |
&& (colorInterpretation.isRGB() || colorInterpretation.isBGR() || colorInterpretation |
985 |
.isRGBA())) { |
986 |
throw new IllegalArgumentException( |
987 |
"Can not set color table when color interpretation is RGB, BGR or RGBA");
|
988 |
} |
989 |
|
990 |
this.colorTable = colorTable;
|
991 |
} |
992 |
|
993 |
@Override
|
994 |
public ColorInterpretation getColorInterpretation() {
|
995 |
return this.colorInterpretation; |
996 |
} |
997 |
|
998 |
@Override
|
999 |
public void setColorInterpretation(ColorInterpretation colorInterpretation) { |
1000 |
|
1001 |
if (this.colorTable != null |
1002 |
&& (colorInterpretation.isRGB() || colorInterpretation.isBGR() || colorInterpretation |
1003 |
.isRGBA())) { |
1004 |
throw new IllegalArgumentException( |
1005 |
"Can not set color interpreation RGB, RGBA or BGR if legend has a color table specifed");
|
1006 |
} |
1007 |
|
1008 |
this.colorInterpretation = colorInterpretation;
|
1009 |
} |
1010 |
|
1011 |
@Override
|
1012 |
public FilterList getFilters() {
|
1013 |
return this.filters; |
1014 |
} |
1015 |
|
1016 |
@Override
|
1017 |
public void setFilters(FilterList filterList) { |
1018 |
this.filters = filterList;
|
1019 |
} |
1020 |
|
1021 |
/**
|
1022 |
*
|
1023 |
*/
|
1024 |
public static void registerPersistence() { |
1025 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
1026 |
DynStruct definition = manager.getDefinition(PERSISTENT_NAME); |
1027 |
if (definition == null) { |
1028 |
definition = |
1029 |
manager.addDefinition(DefaultRasterLegend.class, PERSISTENT_NAME, |
1030 |
PERSISTENT_DESCRIPTION, null, null); |
1031 |
definition.addDynFieldObject(COLOR_TABLE_PERSISTENCE_FIELD).setMandatory(false)
|
1032 |
.setClassOfValue(ColorTable.class); |
1033 |
definition.addDynFieldObject(COLOR_INTERPRETATION_PERSISTENCE_FIELD).setMandatory(false)
|
1034 |
.setClassOfValue(ColorInterpretation.class); |
1035 |
definition.addDynFieldObject(FILTERS_PERSISTENCE_FIELD).setMandatory(false)
|
1036 |
.setClassOfValue(FilterList.class); |
1037 |
} |
1038 |
} |
1039 |
|
1040 |
@Override
|
1041 |
public void saveToState(PersistentState state) throws PersistenceException { |
1042 |
state.set(COLOR_TABLE_PERSISTENCE_FIELD, this.getColorTable());
|
1043 |
state.set(COLOR_INTERPRETATION_PERSISTENCE_FIELD, this.getColorInterpretation());
|
1044 |
state.set(FILTERS_PERSISTENCE_FIELD, this.filters);
|
1045 |
} |
1046 |
|
1047 |
@Override
|
1048 |
public void loadFromState(PersistentState state) throws PersistenceException { |
1049 |
this.setColorTable((ColorTable) state.get(COLOR_TABLE_PERSISTENCE_FIELD));
|
1050 |
this.setColorInterpretation((ColorInterpretation) state
|
1051 |
.get(COLOR_INTERPRETATION_PERSISTENCE_FIELD)); |
1052 |
this.setFilters((FilterList) state.get(FILTERS_PERSISTENCE_FIELD));
|
1053 |
} |
1054 |
|
1055 |
@Override
|
1056 |
public void setTransparency(Transparency transparency) { |
1057 |
this.transparency = transparency;
|
1058 |
} |
1059 |
|
1060 |
@Override
|
1061 |
public Transparency getTransparency() { |
1062 |
return this.transparency; |
1063 |
} |
1064 |
|
1065 |
@Override
|
1066 |
public ISymbol getDefaultSymbol() {
|
1067 |
return null; |
1068 |
} |
1069 |
|
1070 |
@Override
|
1071 |
public Object clone() throws CloneNotSupportedException { |
1072 |
// TODO Auto-generated method stub
|
1073 |
return super.clone(); |
1074 |
} |
1075 |
|
1076 |
@Override
|
1077 |
public ILegend cloneLegend() {
|
1078 |
try {
|
1079 |
return (RasterLegend)this.clone(); |
1080 |
} catch (Exception e) { |
1081 |
throw new RuntimeException("Can't clone the legend", e); |
1082 |
} |
1083 |
} |
1084 |
|
1085 |
@Override
|
1086 |
public void addLegendListener(LegendContentsChangedListener listener) { |
1087 |
// TODO Auto-generated method stub
|
1088 |
|
1089 |
} |
1090 |
|
1091 |
@Override
|
1092 |
public void removeLegendListener(LegendContentsChangedListener listener) { |
1093 |
// TODO Auto-generated method stub
|
1094 |
|
1095 |
} |
1096 |
|
1097 |
@Override
|
1098 |
public void fireDefaultSymbolChangedEvent(SymbolLegendEvent event) { |
1099 |
// TODO Auto-generated method stub
|
1100 |
|
1101 |
} |
1102 |
|
1103 |
@Override
|
1104 |
public LegendContentsChangedListener[] getListeners() { |
1105 |
return null; |
1106 |
} |
1107 |
|
1108 |
@Override
|
1109 |
public void setTransparentNoData(boolean noDataTransparent) { |
1110 |
this.transparentNoData=noDataTransparent;
|
1111 |
|
1112 |
} |
1113 |
|
1114 |
@Override
|
1115 |
public boolean areTransparentNoData() { |
1116 |
return this.transparentNoData; |
1117 |
} |
1118 |
|
1119 |
} |