root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / ColorTable.java @ 13022
History | View | Annotate | Download (21.5 KB)
1 | 12383 | nacho | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | */
|
||
19 | package org.gvsig.raster.datastruct; |
||
20 | |||
21 | 12504 | bsanchez | import java.awt.Color; |
22 | 12383 | nacho | import java.util.ArrayList; |
23 | 12707 | bsanchez | import java.util.Arrays; |
24 | 12383 | nacho | |
25 | 12504 | bsanchez | import org.gvsig.raster.RasterLibrary; |
26 | 12383 | nacho | |
27 | import es.gva.cit.jgdal.GdalColorEntry; |
||
28 | import es.gva.cit.jgdal.GdalColorTable; |
||
29 | import es.gva.cit.jgdal.GdalException; |
||
30 | /**
|
||
31 | * Paleta para raster. Esta consta de los valores RGB de la paleta que son
|
||
32 | 12504 | bsanchez | * almacenados en un vector donde cada elemento contiene en su interior el RGB
|
33 | * completo y del vector de rangos.
|
||
34 | 12383 | nacho | *
|
35 | 12504 | bsanchez | * @version 04/07/2007
|
36 | 12383 | nacho | * @author Nacho Brodin (nachobrodin@gmail.com)
|
37 | 12504 | bsanchez | * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
38 | 12383 | nacho | */
|
39 | public class ColorTable implements Cloneable { |
||
40 | 12707 | bsanchez | private static int hashCode(Object[] array) { |
41 | final int PRIME = 31; |
||
42 | if (array == null) |
||
43 | return 0; |
||
44 | int result = 1; |
||
45 | for (int index = 0; index < array.length; index++) { |
||
46 | result = PRIME * result + (array[index] == null ? 0 : array[index].hashCode()); |
||
47 | } |
||
48 | return result;
|
||
49 | } |
||
50 | |||
51 | private static int hashCode(double[] array) { |
||
52 | final int PRIME = 31; |
||
53 | if (array == null) |
||
54 | return 0; |
||
55 | int result = 1; |
||
56 | for (int index = 0; index < array.length; index++) { |
||
57 | long temp = Double.doubleToLongBits(array[index]); |
||
58 | result = PRIME * result + (int) (temp ^ (temp >>> 32)); |
||
59 | } |
||
60 | return result;
|
||
61 | } |
||
62 | |||
63 | 12383 | nacho | /**
|
64 | 12504 | bsanchez | * Lista de ColorItem donde estaran todos los valores de la paleta segun
|
65 | * el interfaz
|
||
66 | 12383 | nacho | */
|
67 | 12553 | bsanchez | protected ArrayList colorItems = null; |
68 | 12504 | bsanchez | |
69 | 12383 | nacho | /**
|
70 | 12504 | bsanchez | * Booleano que define si se interpolaran los valores de la paleta.
|
71 | 12383 | nacho | */
|
72 | 12504 | bsanchez | protected boolean interpolated = true; |
73 | |||
74 | 12383 | nacho | /**
|
75 | * Lista de rangos para paletas decimales
|
||
76 | */
|
||
77 | 12504 | bsanchez | |
78 | protected double[] range = null; |
||
79 | 12383 | nacho | /**
|
80 | * Lista de valores RGB
|
||
81 | */
|
||
82 | 12504 | bsanchez | protected byte[][] paletteByBand = null; |
83 | 12383 | nacho | |
84 | /**
|
||
85 | * Nombre de la clase asociada a la entrada
|
||
86 | */
|
||
87 | 12504 | bsanchez | protected String[] nameClass = null; |
88 | |||
89 | 12383 | nacho | /**
|
90 | 12504 | bsanchez | * Lista con todas las transparencias de la paleta
|
91 | */
|
||
92 | protected ArrayList transparencyRange = new ArrayList(); |
||
93 | |||
94 | /**
|
||
95 | 12383 | nacho | * Nombre de la paleta
|
96 | */
|
||
97 | 12504 | bsanchez | protected String name = null; |
98 | |||
99 | 12383 | nacho | /**
|
100 | 12504 | bsanchez | * Ruta del fichero a la cual se asocia la paleta. Las bandas de un
|
101 | * GeoRasterMultiFile han de saber a que paleta van asociadas.
|
||
102 | 12383 | nacho | */
|
103 | 12504 | bsanchez | protected String filePath = null; |
104 | 12383 | nacho | |
105 | 13022 | bsanchez | private int errorColor = 8; |
106 | |||
107 | 12504 | bsanchez | /**
|
108 | * Constructor vac?o.
|
||
109 | * @param name
|
||
110 | */
|
||
111 | public ColorTable() {
|
||
112 | this.name = ""; |
||
113 | } |
||
114 | 12383 | nacho | |
115 | /**
|
||
116 | * Constructor. Asigna el nombre de la paleta.
|
||
117 | * @param name
|
||
118 | */
|
||
119 | public ColorTable(String name) { |
||
120 | this.name = name;
|
||
121 | } |
||
122 | |||
123 | /**
|
||
124 | * Asigna el nombre de la paleta
|
||
125 | * @param Nombre de la paleta
|
||
126 | */
|
||
127 | public void setName(String name) { |
||
128 | this.name = name;
|
||
129 | } |
||
130 | |||
131 | /**
|
||
132 | * Obtiene el nombre de la paleta
|
||
133 | * @return Nombre de la paleta
|
||
134 | */
|
||
135 | public String getName() { |
||
136 | return name;
|
||
137 | } |
||
138 | |||
139 | 12567 | bsanchez | // static int total = 0;
|
140 | 12553 | bsanchez | public void createPaletteFromColorItems(ArrayList colorItems, boolean compress) { |
141 | 12504 | bsanchez | this.colorItems = colorItems;
|
142 | |||
143 | // Ordena la paleta
|
||
144 | 12553 | bsanchez | sortPalette(colorItems); |
145 | 12504 | bsanchez | /*
|
146 | 12567 | bsanchez | System.out.println(name);
|
147 | System.out.println("----- Sin comprimir (" + colorItems.size() + ") -----");
|
||
148 | */
|
||
149 | /*
|
||
150 | 12504 | bsanchez | if (name.equals("MDT1")) {
|
151 | for (int i=0; i<colorItems.size(); i++) {
|
||
152 | ColorItem c1 = ((ColorItem) colorItems.get(i));
|
||
153 | Color co = c1.getColor();
|
||
154 | System.out.println(c1.getValue() + ": " + co.getRed() + "," + co.getGreen() + "," + co.getBlue() + "," + co.getAlpha());
|
||
155 | 12383 | nacho | }
|
156 | }
|
||
157 | 12504 | bsanchez | */
|
158 | // Mira que valores se pueden descartar y asi dejamos la paleta reducida
|
||
159 | // para poder hacer interpolaciones
|
||
160 | 12553 | bsanchez | if (compress)
|
161 | compressPalette(colorItems); |
||
162 | 12504 | bsanchez | /*
|
163 | 12567 | bsanchez | System.out.println("------ Comprimido (" + colorItems.size() + ") ------");
|
164 | total += colorItems.size();
|
||
165 | System.out.println("------ Total (" + total + ")----");
|
||
166 | */
|
||
167 | /*
|
||
168 | 12504 | bsanchez | if (name.equals("MDT1")) {
|
169 | for (int i=0; i<colorItems.size(); i++) {
|
||
170 | ColorItem c1 = ((ColorItem) colorItems.get(i));
|
||
171 | Color co = c1.getColor();
|
||
172 | System.out.println(c1.getValue() + ": " + co.getRed() + "," + co.getGreen() + "," + co.getBlue() + "," + co.getAlpha());
|
||
173 | 12383 | nacho | }
|
174 | }
|
||
175 | 12504 | bsanchez | */
|
176 | // Genera la paleta final para poder ser usada
|
||
177 | 12553 | bsanchez | applyPalette(colorItems); |
178 | 12383 | nacho | } |
179 | |||
180 | 13022 | bsanchez | private boolean isEqualColor(Color c1, Color c2, int error) { |
181 | if ((c2 == null) && (c1 != null)) |
||
182 | return false; |
||
183 | if ((c1 == null) && (c2 != null)) |
||
184 | return false; |
||
185 | if (c2.getRed() < (c1.getRed() - error))
|
||
186 | return false; |
||
187 | if (c2.getGreen() < (c1.getGreen() - error))
|
||
188 | return false; |
||
189 | if (c2.getBlue() < (c1.getBlue() - error))
|
||
190 | return false; |
||
191 | if (c2.getAlpha() < (c1.getAlpha() - error))
|
||
192 | return false; |
||
193 | 12504 | bsanchez | |
194 | 13022 | bsanchez | if (c2.getRed() > (c1.getRed() + error))
|
195 | return false; |
||
196 | if (c2.getGreen() > (c1.getGreen() + error))
|
||
197 | return false; |
||
198 | if (c2.getBlue() > (c1.getBlue() + error))
|
||
199 | return false; |
||
200 | if (c2.getAlpha() > (c1.getAlpha() + error))
|
||
201 | return false; |
||
202 | 12504 | bsanchez | |
203 | return true; |
||
204 | 12383 | nacho | } |
205 | |||
206 | 12677 | bsanchez | /**
|
207 | * Informa de si el color c3 se encuentra en un rango valido entre c1 y c2.
|
||
208 | * Para colores con nombre de clase se devolver? false para no eliminar nunca
|
||
209 | * ese item.
|
||
210 | * @param c1
|
||
211 | * @param c2
|
||
212 | * @param c3
|
||
213 | * @return
|
||
214 | */
|
||
215 | 12504 | bsanchez | private boolean isCorrectColor(ColorItem c1, ColorItem c2, ColorItem c3) { |
216 | 12677 | bsanchez | if ((c3.getNameClass() != null) && (c3.getNameClass().length() > 0)) |
217 | return false; |
||
218 | |||
219 | double max = c2.getValue() - c1.getValue();
|
||
220 | 12504 | bsanchez | int r = c1.getColor().getRed() + (int) (((c2.getColor().getRed() - c1.getColor().getRed()) * (c3.getValue() - c1.getValue())) / max); |
221 | int g = c1.getColor().getGreen() + (int) (((c2.getColor().getGreen() - c1.getColor().getGreen()) * (c3.getValue() - c1.getValue())) / max); |
||
222 | int b = c1.getColor().getBlue() + (int) (((c2.getColor().getBlue() - c1.getColor().getBlue()) * (c3.getValue() - c1.getValue())) / max); |
||
223 | int a = c1.getColor().getAlpha() + (int) (((c2.getColor().getAlpha() - c1.getColor().getAlpha()) * (c3.getValue() - c1.getValue())) / max); |
||
224 | Color aux = new Color(r & 0xff, g & 0xff, b & 0xff, a & 0xff); |
||
225 | |||
226 | 13022 | bsanchez | return isEqualColor(c3.getColor(), aux, errorColor);
|
227 | 12504 | bsanchez | } |
228 | |||
229 | private boolean canDelete(int first, int last) { |
||
230 | ColorItem c1 = (ColorItem) colorItems.get(first); |
||
231 | ColorItem c2 = (ColorItem) colorItems.get(last); |
||
232 | for (int i = (first + 1); i < last; i++) { |
||
233 | if (!isCorrectColor(c1, c2, (ColorItem) colorItems.get(i)))
|
||
234 | return false; |
||
235 | } |
||
236 | return true; |
||
237 | } |
||
238 | |||
239 | 12553 | bsanchez | public void compressPalette() { |
240 | 13022 | bsanchez | System.out.println("Antes: " + colorItems.size()); |
241 | 12553 | bsanchez | compressPalette(colorItems); |
242 | 13022 | bsanchez | System.out.println("Despues: " + colorItems.size()); |
243 | 12553 | bsanchez | } |
244 | |||
245 | public boolean isCompressible() { |
||
246 | ArrayList cloneList = (ArrayList) colorItems.clone(); |
||
247 | compressPalette(cloneList); |
||
248 | return (cloneList.size() != colorItems.size());
|
||
249 | } |
||
250 | |||
251 | private void compressPalette(ArrayList colorItems) { |
||
252 | 12567 | bsanchez | int size = -1; |
253 | |||
254 | while (size != colorItems.size()) {
|
||
255 | int init = 0; |
||
256 | int posMax = 2; |
||
257 | |||
258 | size = colorItems.size(); |
||
259 | while (init < colorItems.size()) {
|
||
260 | if ((posMax < colorItems.size()) && canDelete(init, posMax)) {
|
||
261 | posMax++; |
||
262 | continue;
|
||
263 | } |
||
264 | if ((init + 2) < posMax) { |
||
265 | if (canDelete(init, posMax - 1)) |
||
266 | for (int i = (posMax - 2); i > init; i--) |
||
267 | if (i < colorItems.size())
|
||
268 | colorItems.remove(i); |
||
269 | } |
||
270 | init++; |
||
271 | posMax = init + 2;
|
||
272 | 12383 | nacho | } |
273 | } |
||
274 | } |
||
275 | |||
276 | /**
|
||
277 | 12504 | bsanchez | * Crea una paleta a partir de un objeto GdalColorTable. Esto es necesario
|
278 | * para los ficheros que tienen una paleta asignada, como los gif, y que son
|
||
279 | * tratados por Gdal. Se pasa la tabla de color le?da desde gdal y se crea
|
||
280 | * directamente un objeto Palette.
|
||
281 | 12383 | nacho | * @param table
|
282 | */
|
||
283 | public void createPaletteFromGdalColorTable(GdalColorTable table) { |
||
284 | 12504 | bsanchez | try {
|
285 | colorItems = new ArrayList(); |
||
286 | for (int iEntry = 0; iEntry < table.getColorEntryCount(); iEntry++) { |
||
287 | GdalColorEntry entry = table.getColorEntryAsRGB(iEntry); |
||
288 | 12383 | nacho | |
289 | 12504 | bsanchez | ColorItem colorItem = new ColorItem();
|
290 | colorItem.setNameClass("");
|
||
291 | colorItem.setValue(iEntry); |
||
292 | colorItem.setColor(new Color( (int) (entry.c1 & 0xff), |
||
293 | (int) (entry.c2 & 0xff), |
||
294 | (int) (entry.c3 & 0xff), |
||
295 | (int) (entry.c4 & 0xff))); |
||
296 | |||
297 | colorItems.add(colorItem); |
||
298 | } |
||
299 | } catch (GdalException ex) {
|
||
300 | // No se crea la paleta
|
||
301 | 12383 | nacho | } |
302 | 12553 | bsanchez | sortPalette(colorItems); |
303 | //compressPalette();
|
||
304 | applyPalette(colorItems); |
||
305 | 12383 | nacho | } |
306 | |||
307 | /**
|
||
308 | * Obtiene la transparencia por rangos asociada a la tabla de color actual.
|
||
309 | * @return Lista de objetos TransparencyRange
|
||
310 | */
|
||
311 | public ArrayList getTransparencyRanges() { |
||
312 | 12504 | bsanchez | return transparencyRange;
|
313 | 12383 | nacho | } |
314 | |||
315 | /**
|
||
316 | * Obtiene la tabla de color
|
||
317 | * @return Paleta
|
||
318 | */
|
||
319 | 12504 | bsanchez | /*
|
320 | 12383 | nacho | public int[] getColorTable() {
|
321 | return palette;
|
||
322 | }
|
||
323 | 12504 | bsanchez | */
|
324 | 12383 | nacho | |
325 | /**
|
||
326 | * Obtiene la tabla de color por banda
|
||
327 | * @return Paleta
|
||
328 | */
|
||
329 | public byte[][] getColorTableByBand() { |
||
330 | return paletteByBand;
|
||
331 | } |
||
332 | |||
333 | 12504 | bsanchez | |
334 | 12383 | nacho | /**
|
335 | * Asigna una paleta
|
||
336 | * @param palette Paleta
|
||
337 | */
|
||
338 | public void setColorTable(int[] palette) { |
||
339 | paletteByBand = new byte[palette.length][3]; |
||
340 | for (int i = 0; i < palette.length; i++) { |
||
341 | paletteByBand[i][0] = (byte)((palette[i] & 0x00ff0000) >> 16); |
||
342 | paletteByBand[i][1] = (byte)((palette[i] & 0x0000ff00) >> 8); |
||
343 | paletteByBand[i][2] = (byte)(palette[i] & 0x000000ff); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | /**
|
||
348 | * Obtiene los nombres de las clases de la paleta
|
||
349 | * @return Array de cadenas. Cada una corresponde con un nombre de clase
|
||
350 | * que corresponde a cada rango de tipos.
|
||
351 | */
|
||
352 | public String[] getNameClass() { |
||
353 | return nameClass;
|
||
354 | } |
||
355 | |||
356 | /**
|
||
357 | * Asigna los nombres de las clases de la paleta
|
||
358 | * @param names Array de cadenas. Cada una corresponde con un nombre de clase
|
||
359 | * que corresponde a cada rango de tipos.
|
||
360 | */
|
||
361 | public void setNameClass(String[] names) { |
||
362 | nameClass = names; |
||
363 | } |
||
364 | |||
365 | /**
|
||
366 | * Obtiene la ruta del fichero al que va asociada la paleta.
|
||
367 | * @return Ruta del fichero al que va asociada la paleta.
|
||
368 | */
|
||
369 | public String getFilePath() { |
||
370 | return filePath;
|
||
371 | } |
||
372 | |||
373 | /**
|
||
374 | * Asigna la ruta del fichero al que va asociada la paleta.
|
||
375 | * @param Ruta del fichero al que va asociada la paleta.
|
||
376 | */
|
||
377 | public void setFilePath(String filePath) { |
||
378 | this.filePath = filePath;
|
||
379 | } |
||
380 | |||
381 | /**
|
||
382 | * Obtiene los rangos de transparencia que tiene la paleta. Estos se obtienen
|
||
383 | * en un array de objetos TransparencyRange. Cada uno de ellos representa un
|
||
384 | * rango de pixeles
|
||
385 | * @return ArrayList
|
||
386 | */
|
||
387 | public ArrayList getTransparencyRange() { |
||
388 | 12504 | bsanchez | return transparencyRange;
|
389 | 12383 | nacho | } |
390 | |||
391 | /*
|
||
392 | * (non-Javadoc)
|
||
393 | * @see java.lang.Object#clone()
|
||
394 | */
|
||
395 | public Object clone() { |
||
396 | ColorTable clone = null;
|
||
397 | try {
|
||
398 | clone = (ColorTable) super.clone();
|
||
399 | } catch (CloneNotSupportedException e) { |
||
400 | } |
||
401 | |||
402 | 12567 | bsanchez | if (colorItems != null) { |
403 | clone.colorItems = new ArrayList(); |
||
404 | for (int i = 0; i < colorItems.size(); i++) { |
||
405 | clone.colorItems.add(((ColorItem) colorItems.get(i)).clone()); |
||
406 | } |
||
407 | } |
||
408 | |||
409 | 12383 | nacho | if (filePath != null) |
410 | clone.filePath = new String(filePath); |
||
411 | |||
412 | if (name != null) |
||
413 | clone.name = new String(name); |
||
414 | |||
415 | if (nameClass != null) { |
||
416 | clone.nameClass = new String[nameClass.length]; |
||
417 | for (int i = 0; i < nameClass.length; i++) |
||
418 | clone.nameClass[i] = new String(nameClass[i]); |
||
419 | } |
||
420 | |||
421 | if (paletteByBand != null) { |
||
422 | clone.paletteByBand = (byte[][]) paletteByBand.clone(); |
||
423 | for (int i = 0; i < paletteByBand.length; i++) |
||
424 | clone.paletteByBand[i] = (byte[]) paletteByBand[i].clone(); |
||
425 | } |
||
426 | |||
427 | 12567 | bsanchez | if (range != null) |
428 | clone.range = (double[]) range.clone(); |
||
429 | |||
430 | if (transparencyRange != null) { |
||
431 | clone.transparencyRange = new ArrayList(); |
||
432 | for (int i = 0; i < transparencyRange.size(); i++) { |
||
433 | clone.transparencyRange.add(((TransparencyRange) transparencyRange.get(i)).clone()); |
||
434 | } |
||
435 | } |
||
436 | |||
437 | 12504 | bsanchez | return clone;
|
438 | } |
||
439 | |||
440 | /**
|
||
441 | * Devuelve un color de interpolacion entre dos colores
|
||
442 | * @param value
|
||
443 | * @param pos
|
||
444 | * @return
|
||
445 | */
|
||
446 | private Color interpolatedColor(double value, int pos) { |
||
447 | 12899 | bsanchez | if (colorItems.size() <= 0) |
448 | return Color.black; |
||
449 | |||
450 | 12504 | bsanchez | if ((pos + 1) == colorItems.size()) |
451 | return ((ColorItem) colorItems.get(pos)).getColor();
|
||
452 | |||
453 | 12899 | bsanchez | if (value <= ((ColorItem) colorItems.get(0)).getValue()) |
454 | return ((ColorItem) colorItems.get(0)).getColor(); |
||
455 | |||
456 | 12504 | bsanchez | ColorItem item1 = (ColorItem) colorItems.get(pos); |
457 | ColorItem item2 = (ColorItem) colorItems.get(pos + 1);
|
||
458 | |||
459 | double percValue = ((value - item1.getValue()) * 100) / (item2.getValue() - item1.getValue()); |
||
460 | |||
461 | Color halfColor = new Color( |
||
462 | (item2.getColor().getRed() + item1.getColor().getRed()) >> 1,
|
||
463 | (item2.getColor().getGreen() + item1.getColor().getGreen()) >> 1,
|
||
464 | (item2.getColor().getBlue() + item1.getColor().getBlue()) >> 1,
|
||
465 | (item2.getColor().getAlpha() + item1.getColor().getAlpha()) >> 1);
|
||
466 | |||
467 | Color color1, color2;
|
||
468 | 13022 | bsanchez | double perc1, perc2;
|
469 | 12504 | bsanchez | |
470 | 13022 | bsanchez | if (percValue > item2.getInterpolated()) {
|
471 | 12504 | bsanchez | color1 = halfColor; |
472 | color2 = item2.getColor(); |
||
473 | 13022 | bsanchez | perc1 = item2.getInterpolated(); |
474 | 12504 | bsanchez | perc2 = 100;
|
475 | } else {
|
||
476 | color1 = item1.getColor(); |
||
477 | color2 = halfColor; |
||
478 | perc1 = 0;
|
||
479 | 13022 | bsanchez | perc2 = item2.getInterpolated(); |
480 | 12383 | nacho | } |
481 | |||
482 | 12504 | bsanchez | double percNew = (percValue - perc1) / (perc2 - perc1);
|
483 | |||
484 | Color newColor = new Color( |
||
485 | (int) (color1.getRed() + ((color2.getRed() - color1.getRed()) * percNew)) & 0xff, |
||
486 | (int) (color1.getGreen() + ((color2.getGreen() - color1.getGreen()) * percNew)) & 0xff, |
||
487 | (int) (color1.getBlue() + ((color2.getBlue() - color1.getBlue()) * percNew)) & 0xff, |
||
488 | (int) (color1.getAlpha() + ((color2.getAlpha() - color1.getAlpha()) * percNew)) & 0xff); |
||
489 | |||
490 | |||
491 | return newColor;
|
||
492 | 12383 | nacho | } |
493 | 12504 | bsanchez | |
494 | /*
|
||
495 | 12567 | bsanchez | * TODO: RENDIMIENTO: Incluir una heuristica que dado un valor se compare con
|
496 | * el valor de la mitad de la tabla y si es menor se empieza a recorrer desde
|
||
497 | * el principio sino se empieza a recorrer desde la mitad de la tabla hasta
|
||
498 | * abajo. Esto hace que se reduzca la tabla a la mitad de valores haciendo
|
||
499 | * solo una comparaci?n.
|
||
500 | 12504 | bsanchez | */
|
501 | /**
|
||
502 | * Obtiene el valor RGB para un clave entera pasada por par?metro
|
||
503 | * @param value clave de la cual se quiere obtener el valor RGB de la paleta
|
||
504 | * @return valor RGB
|
||
505 | */
|
||
506 | public byte[] getRGBByBand(double value) { |
||
507 | 12567 | bsanchez | for (int i = 1; i <= range.length; i++) { |
508 | 12504 | bsanchez | if (i < range.length) {
|
509 | if (value < range[i])
|
||
510 | return paletteByBand[i - 1]; |
||
511 | } else {
|
||
512 | return paletteByBand[i - 1]; |
||
513 | } |
||
514 | } |
||
515 | return new byte[4]; |
||
516 | } |
||
517 | |||
518 | 12567 | bsanchez | /*
|
519 | * TODO: Usar el m?todo Quicksort para ordenar
|
||
520 | */
|
||
521 | 12504 | bsanchez | /**
|
522 | * Ordena el ColorItems de manera ascendente. De momento se usa el m?todo de
|
||
523 | * ordenaci?n por burbuja.
|
||
524 | */
|
||
525 | 12553 | bsanchez | private void sortPalette(ArrayList colorItems) { |
526 | 12504 | bsanchez | for (int i = 0; i < colorItems.size(); i++) { |
527 | for (int j = i + 1; j < colorItems.size(); j++) { |
||
528 | if (((ColorItem) colorItems.get(j)).getValue() < ((ColorItem) colorItems.get(i)).getValue()) {
|
||
529 | Object aux = colorItems.get(i);
|
||
530 | colorItems.set(i, colorItems.get(j)); |
||
531 | colorItems.set(j, aux); |
||
532 | } |
||
533 | } |
||
534 | } |
||
535 | } |
||
536 | |||
537 | /**
|
||
538 | * Genera una paleta intermedia para acelerar los calculos.
|
||
539 | */
|
||
540 | 12553 | bsanchez | private void applyPalette(ArrayList colorItems) { |
541 | 12504 | bsanchez | ArrayList arrayColors = new ArrayList(); |
542 | |||
543 | paletteByBand = new byte[0][3]; |
||
544 | range = new double[0]; |
||
545 | nameClass = new String[0]; |
||
546 | |||
547 | if (colorItems.size()==0) |
||
548 | return;
|
||
549 | |||
550 | // Nos preparamos para hacer las particiones, sabiendo el minimo y maximo
|
||
551 | double min = ((ColorItem) colorItems.get(0)).getValue(); |
||
552 | double max = ((ColorItem) colorItems.get(colorItems.size() - 1)).getValue(); |
||
553 | |||
554 | if (min > max) {
|
||
555 | double aux = max;
|
||
556 | max = min; |
||
557 | min = aux; |
||
558 | } |
||
559 | |||
560 | Color color = Color.white; |
||
561 | Color colorOld = null; |
||
562 | |||
563 | // Hacemos las particiones, metiendo cada item calculado en un array
|
||
564 | int defaultColors = RasterLibrary.defaultNumberOfColors;
|
||
565 | for (int i = 0; i < defaultColors; i++) { |
||
566 | double value = min + ((i * (max - min)) / (defaultColors - 1)); |
||
567 | int pos = 0; |
||
568 | for (int j = 1; j <= colorItems.size(); j++) { |
||
569 | if (j < colorItems.size()) {
|
||
570 | if (value < ((ColorItem) colorItems.get(j)).getValue()) {
|
||
571 | pos = j - 1;
|
||
572 | break;
|
||
573 | } |
||
574 | } else {
|
||
575 | pos = j - 1;
|
||
576 | break;
|
||
577 | } |
||
578 | } |
||
579 | |||
580 | // Calculamos el color que corresponde, tanto interpolado como no
|
||
581 | 12553 | bsanchez | if (interpolated) {
|
582 | 12504 | bsanchez | color = interpolatedColor(value, pos); |
583 | 12553 | bsanchez | } else {
|
584 | if ((pos + 1) < colorItems.size()) { |
||
585 | 12899 | bsanchez | double min2 = ((ColorItem) colorItems.get(pos)).getValue();
|
586 | double max2 = ((ColorItem) colorItems.get(pos + 1)).getValue(); |
||
587 | if ((min2 + ((max2 - min2) * ((ColorItem) colorItems.get(pos + 1)).getInterpolated() / 100)) < value) |
||
588 | 12553 | bsanchez | pos++; |
589 | } |
||
590 | 12504 | bsanchez | color = ((ColorItem) colorItems.get(pos)).getColor(); |
591 | 12553 | bsanchez | } |
592 | 12504 | bsanchez | |
593 | 13022 | bsanchez | if (!isEqualColor(color, colorOld, 0)) { |
594 | 12504 | bsanchez | ColorItem colorItem = new ColorItem();
|
595 | colorItem.setNameClass("");
|
||
596 | colorItem.setValue(value); |
||
597 | colorItem.setColor(color); |
||
598 | arrayColors.add(colorItem); |
||
599 | } |
||
600 | |||
601 | colorOld = color; |
||
602 | } |
||
603 | |||
604 | // Una vez tenemos una paleta de 256 colores o inferior, rellenamos
|
||
605 | // los siguientes valores para hacer busquedas rapidas.
|
||
606 | |||
607 | paletteByBand = new byte[arrayColors.size()][3]; |
||
608 | range = new double[arrayColors.size()]; |
||
609 | nameClass = new String[arrayColors.size()]; |
||
610 | |||
611 | transparencyRange.clear(); |
||
612 | for (int i = 0; i < arrayColors.size(); i++) { |
||
613 | paletteByBand[i][0] = (byte) ((ColorItem) arrayColors.get(i)).getColor().getRed(); |
||
614 | paletteByBand[i][1] = (byte) ((ColorItem) arrayColors.get(i)).getColor().getGreen(); |
||
615 | paletteByBand[i][2] = (byte) ((ColorItem) arrayColors.get(i)).getColor().getBlue(); |
||
616 | range[i] = ((ColorItem) arrayColors.get(i)).getValue(); |
||
617 | nameClass[i] = ((ColorItem) arrayColors.get(i)).getNameClass(); |
||
618 | if (((ColorItem) arrayColors.get(i)).getColor().getAlpha() != 255) { |
||
619 | TransparencyRange r = new TransparencyRange();
|
||
620 | r.setRed(new int[] { ((ColorItem) arrayColors.get(i)).getColor().getRed(), ((ColorItem) arrayColors.get(i)).getColor().getRed() }); |
||
621 | r.setGreen(new int[] { ((ColorItem) arrayColors.get(i)).getColor().getGreen(), ((ColorItem) arrayColors.get(i)).getColor().getGreen() }); |
||
622 | r.setBlue(new int[] { ((ColorItem) arrayColors.get(i)).getColor().getBlue(), ((ColorItem) arrayColors.get(i)).getColor().getBlue() }); |
||
623 | r.setAlpha(((ColorItem) arrayColors.get(i)).getColor().getAlpha()); |
||
624 | r.setAnd(true);
|
||
625 | r.loadStrEntryFromValues(); |
||
626 | transparencyRange.add(r); |
||
627 | } |
||
628 | } |
||
629 | } |
||
630 | |||
631 | public double[] getRange() { |
||
632 | return range;
|
||
633 | } |
||
634 | |||
635 | public void setRange(double[] range) { |
||
636 | this.range = range;
|
||
637 | } |
||
638 | |||
639 | public ArrayList getColorItems() { |
||
640 | return colorItems;
|
||
641 | } |
||
642 | |||
643 | 12553 | bsanchez | /**
|
644 | * Nos indica si la paleta la ha generado con valores interpolados o no.
|
||
645 | * @return
|
||
646 | */
|
||
647 | 12504 | bsanchez | public boolean isInterpolated() { |
648 | return interpolated;
|
||
649 | } |
||
650 | |||
651 | 12553 | bsanchez | /**
|
652 | * Definir si la paleta tendra los valores interpolados o no
|
||
653 | * @param interpolated
|
||
654 | */
|
||
655 | 12504 | bsanchez | public void setInterpolated(boolean interpolated) { |
656 | this.interpolated = interpolated;
|
||
657 | 12553 | bsanchez | applyPalette(colorItems); |
658 | 12504 | bsanchez | } |
659 | 12707 | bsanchez | |
660 | /*
|
||
661 | * (non-Javadoc)
|
||
662 | * @see java.lang.Object#hashCode()
|
||
663 | */
|
||
664 | public int hashCode() { |
||
665 | final int PRIME = 31; |
||
666 | int result = 1; |
||
667 | result = PRIME * result + ((colorItems == null) ? 0 : colorItems.hashCode()); |
||
668 | 13022 | bsanchez | result = PRIME * result + errorColor; |
669 | 12707 | bsanchez | result = PRIME * result + ((filePath == null) ? 0 : filePath.hashCode()); |
670 | result = PRIME * result + (interpolated ? 1231 : 1237); |
||
671 | result = PRIME * result + ((name == null) ? 0 : name.hashCode()); |
||
672 | result = PRIME * result + ColorTable.hashCode(nameClass); |
||
673 | result = PRIME * result + ColorTable.hashCode(paletteByBand); |
||
674 | result = PRIME * result + ColorTable.hashCode(range); |
||
675 | result = PRIME * result + ((transparencyRange == null) ? 0 : transparencyRange.hashCode()); |
||
676 | return result;
|
||
677 | } |
||
678 | |||
679 | /*
|
||
680 | * (non-Javadoc)
|
||
681 | * @see java.lang.Object#equals(java.lang.Object)
|
||
682 | */
|
||
683 | public boolean equals(Object obj) { |
||
684 | if (this == obj) |
||
685 | return true; |
||
686 | if (obj == null) |
||
687 | return false; |
||
688 | if (getClass() != obj.getClass())
|
||
689 | return false; |
||
690 | final ColorTable other = (ColorTable) obj;
|
||
691 | |||
692 | if ( ((colorItems == null) && (other.colorItems != null)) || |
||
693 | ((colorItems != null) && (other.colorItems == null)) ) |
||
694 | return false; |
||
695 | |||
696 | if (colorItems != null) { |
||
697 | if (colorItems.size() != other.colorItems.size())
|
||
698 | return false; |
||
699 | for (int i = 0; i < colorItems.size(); i++) { |
||
700 | if (!((ColorItem) colorItems.get(i)).equals(other.colorItems.get(i)))
|
||
701 | return false; |
||
702 | } |
||
703 | } |
||
704 | |||
705 | if (filePath == null) { |
||
706 | if (other.filePath != null) |
||
707 | return false; |
||
708 | } else if (!filePath.equals(other.filePath)) |
||
709 | return false; |
||
710 | if (name == null) { |
||
711 | if (other.name != null) |
||
712 | return false; |
||
713 | } else if (!name.equals(other.name)) |
||
714 | return false; |
||
715 | |||
716 | 13022 | bsanchez | if (nameClass != null) { |
717 | if (nameClass.length != other.nameClass.length)
|
||
718 | return false; |
||
719 | |||
720 | for (int i = 0; i < nameClass.length; i++) { |
||
721 | if (!nameClass[i].equals(other.nameClass[i]))
|
||
722 | return false; |
||
723 | } |
||
724 | } |
||
725 | |||
726 | 12707 | bsanchez | if ( ((paletteByBand == null) && (other.paletteByBand != null)) || |
727 | ((paletteByBand != null) && (other.paletteByBand == null)) ) |
||
728 | return false; |
||
729 | |||
730 | if (paletteByBand != null) { |
||
731 | for (int i = 0; i < paletteByBand.length; i++) { |
||
732 | for (int j = 0; j < paletteByBand[i].length; j++) { |
||
733 | if (paletteByBand[i][j] != other.paletteByBand[i][j])
|
||
734 | return false; |
||
735 | } |
||
736 | } |
||
737 | } |
||
738 | |||
739 | if (!Arrays.equals(range, other.range)) |
||
740 | return false; |
||
741 | |||
742 | if ( ((colorItems == null) && (other.colorItems != null)) || |
||
743 | ((colorItems != null) && (other.colorItems == null)) ) |
||
744 | return false; |
||
745 | |||
746 | if (transparencyRange != null) { |
||
747 | if (transparencyRange.size() != other.transparencyRange.size())
|
||
748 | return false; |
||
749 | for (int i = 0; i < transparencyRange.size(); i++) { |
||
750 | if (!((TransparencyRange) transparencyRange.get(i)).equals(other.transparencyRange.get(i)))
|
||
751 | return false; |
||
752 | } |
||
753 | } |
||
754 | |||
755 | return true; |
||
756 | } |
||
757 | 12383 | nacho | } |