svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / fmap / raster / lib / legend / impl / io / GimpGradientColorTableIO.java @ 44831
History | View | Annotate | Download (6.67 KB)
1 |
package org.gvsig.raster.lib.legend.impl.io; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.io.BufferedReader; |
5 |
import java.io.BufferedWriter; |
6 |
import java.io.File; |
7 |
import java.io.FileReader; |
8 |
import java.io.FileWriter; |
9 |
import java.io.IOException; |
10 |
import java.text.NumberFormat; |
11 |
import java.util.ArrayList; |
12 |
import java.util.List; |
13 |
|
14 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
15 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
16 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTableIO; |
17 |
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass; |
18 |
import org.gvsig.raster.lib.legend.api.exceptions.ColorTableIOException; |
19 |
|
20 |
import org.slf4j.Logger; |
21 |
import org.slf4j.LoggerFactory; |
22 |
|
23 |
/**
|
24 |
* {@link ColorTableIO} to read Gimp gradients to {@link ColorTable} and write
|
25 |
* {@link ColorTable} to Gimp gradients.
|
26 |
*
|
27 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
28 |
*
|
29 |
*/
|
30 |
public class GimpGradientColorTableIO implements ColorTableIO { |
31 |
|
32 |
private static final Logger LOG = LoggerFactory.getLogger(GimpGradientColorTableIO.class); |
33 |
|
34 |
public static final String NAME = "GimpGradientColorTableIO"; |
35 |
|
36 |
public static final String DESCRIPTION = "ColorTableIO to read and write Gimp gradients"; |
37 |
|
38 |
@Override
|
39 |
public String getName() { |
40 |
return NAME;
|
41 |
} |
42 |
|
43 |
@Override
|
44 |
public String getDescription() { |
45 |
return DESCRIPTION;
|
46 |
} |
47 |
|
48 |
@Override
|
49 |
public ColorTable read(File file) throws ColorTableIOException { |
50 |
List<ColorTableClass> colorTableClasses = new ArrayList<ColorTableClass>(); |
51 |
ColorTable colorTable = RasterLegendLocator.getRasterLegendManager().createColorTable(); |
52 |
try {
|
53 |
BufferedReader reader = new BufferedReader(new FileReader(file)); |
54 |
String currentLine;
|
55 |
int cont = 0; |
56 |
while ((currentLine = reader.readLine()) != null) { |
57 |
if (cont == 1) { |
58 |
colorTable.setName(currentLine.substring(6));
|
59 |
} else if (cont > 1) { |
60 |
String[] strings = currentLine.split("\\s+"); |
61 |
|
62 |
if (strings.length < 13) { |
63 |
continue;
|
64 |
} |
65 |
|
66 |
colorTableClasses.add(parseColorItem(strings, 0));
|
67 |
colorTableClasses.add(parseColorItem(strings, 1));
|
68 |
} |
69 |
cont++; |
70 |
} |
71 |
reader.close(); |
72 |
} catch (IOException ex) { |
73 |
LOG.error("Error reading file to create color table", ex);
|
74 |
throw new ColorTableIOException(ex); |
75 |
} |
76 |
|
77 |
colorTable.setClasses(colorTableClasses, true);
|
78 |
return colorTable;
|
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void read(ColorTable colorTable, File file) throws ColorTableIOException { |
83 |
ColorTable tmpColorTable = this.read(file);
|
84 |
colorTable.copyFrom(tmpColorTable); |
85 |
} |
86 |
|
87 |
@Override
|
88 |
public void write(ColorTable colorTable, File file) throws ColorTableIOException { |
89 |
List<ColorTableClass> colorTableClass = colorTable.getClasses();
|
90 |
try {
|
91 |
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); |
92 |
writer.write("GIMP Gradient\n");
|
93 |
writer.write("Name: " + colorTable.getName() + "\n"); |
94 |
writer.write((colorTableClass.size() - 1) + "\n"); |
95 |
String line;
|
96 |
|
97 |
for (int i = 1; i < colorTableClass.size(); i++) { |
98 |
double min = colorTableClass.get(0).getValue(); |
99 |
double max = colorTableClass.get(colorTableClass.size() - 1).getValue(); |
100 |
|
101 |
line = "";
|
102 |
ColorTableClass item1 = (ColorTableClass) colorTableClass.get(i - 1);
|
103 |
ColorTableClass item2 = (ColorTableClass) colorTableClass.get(i); |
104 |
|
105 |
double pos1 = (item1.getValue() - min) / (max - min);
|
106 |
double pos2 = (item2.getValue() - min) / (max - min);
|
107 |
|
108 |
NumberFormat format = NumberFormat.getNumberInstance(); |
109 |
format.setMaximumFractionDigits(6);
|
110 |
format.setMinimumFractionDigits(6);
|
111 |
line += format.format(pos1).replaceAll(",", ".") + " "; |
112 |
double interp = pos1 + (((pos2 - pos1) * item1.getInterpolated()) / 100.0); |
113 |
line += format.format(interp).replaceAll(",", ".") + " "; |
114 |
line += format.format(pos2).replaceAll(",", ".") + " "; |
115 |
|
116 |
Color color = item1.getColor();
|
117 |
line += format.format(color.getRed() / 255.0).replaceAll(",", ".") + " "; |
118 |
line += format.format(color.getGreen() / 255.0).replaceAll(",", ".") + " "; |
119 |
line += format.format(color.getBlue() / 255.0).replaceAll(",", ".") + " "; |
120 |
line += format.format(color.getAlpha() / 255.0).replaceAll(",", ".") + " "; |
121 |
|
122 |
color = item2.getColor(); |
123 |
line += format.format(color.getRed() / 255.0).replaceAll(",", ".") + " "; |
124 |
line += format.format(color.getGreen() / 255.0).replaceAll(",", ".") + " "; |
125 |
line += format.format(color.getBlue() / 255.0).replaceAll(",", ".") + " "; |
126 |
line += format.format(color.getAlpha() / 255.0).replaceAll(",", ".") + " "; |
127 |
|
128 |
line += "0 0\n";
|
129 |
writer.write(line); |
130 |
} |
131 |
writer.close(); |
132 |
} catch (IOException ex) { |
133 |
LOG.error("Error writting file to create Gimp gradient", ex);
|
134 |
throw new ColorTableIOException(ex); |
135 |
} |
136 |
} |
137 |
|
138 |
private ColorTableClass parseColorItem(String[] strings, int pos) { |
139 |
ColorTableClass item = RasterLegendLocator.getRasterLegendManager().createColorTableClass(); |
140 |
|
141 |
double pos1 = Double.valueOf(strings[0]).doubleValue(); |
142 |
double pos2 = Double.valueOf(strings[1]).doubleValue(); |
143 |
double pos3 = Double.valueOf(strings[2]).doubleValue(); |
144 |
if (pos == 0) { |
145 |
item.setValue(pos1 * 255);
|
146 |
} else {
|
147 |
item.setValue(pos3 * 255);
|
148 |
} |
149 |
if (pos == 0) { |
150 |
item.setInterpolated((100 * (pos2 - pos1)) / (pos3 - pos1));
|
151 |
} |
152 |
if (pos == 0) { |
153 |
item.setColor(new Color((int) (Double.valueOf(strings[3]).doubleValue() * 255.0), |
154 |
(int) (Double.valueOf(strings[4]).doubleValue() * 255.0), (int) (Double.valueOf( |
155 |
strings[5]).doubleValue() * 255.0), (int) (Double.valueOf(strings[6]) |
156 |
.doubleValue() * 255.0)));
|
157 |
} else {
|
158 |
item.setColor(new Color((int) (Double.valueOf(strings[7]).doubleValue() * 255.0), |
159 |
(int) (Double.valueOf(strings[8]).doubleValue() * 255.0), (int) (Double.valueOf( |
160 |
strings[9]).doubleValue() * 255.0), (int) (Double.valueOf(strings[10]) |
161 |
.doubleValue() * 255.0)));
|
162 |
} |
163 |
return item;
|
164 |
} |
165 |
} |