root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / io / formats / GimpPalettes.java @ 17178
History | View | Annotate | Download (4.44 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 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.io.formats; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.io.BufferedReader; |
23 |
import java.io.BufferedWriter; |
24 |
import java.io.File; |
25 |
import java.io.FileReader; |
26 |
import java.io.FileWriter; |
27 |
import java.io.IOException; |
28 |
import java.util.ArrayList; |
29 |
|
30 |
import org.gvsig.raster.datastruct.ColorItem; |
31 |
import org.gvsig.raster.datastruct.ColorTable; |
32 |
import org.gvsig.raster.datastruct.io.RasterLegendIO; |
33 |
/**
|
34 |
* Clase GimpPalettes sirve para importar y exportar tablas de color de Gimp.
|
35 |
* Los ficheros de Paletas de gimp tienen la extension .gpl
|
36 |
*
|
37 |
* @version 03/12/2007
|
38 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
39 |
*/
|
40 |
public class GimpPalettes extends RasterLegendIO { |
41 |
|
42 |
/**
|
43 |
* Devuelve el ColorItem asociado a una linea de un fichero de tablas de color
|
44 |
* de Gimp
|
45 |
* @param strings
|
46 |
* @param pos
|
47 |
* @return
|
48 |
*/
|
49 |
private ColorItem parseColorItem(String[] strings) { |
50 |
if (strings.length < 4) |
51 |
return null; |
52 |
|
53 |
ColorItem item = new ColorItem();
|
54 |
|
55 |
item.setInterpolated(100);
|
56 |
|
57 |
item.setColor(new Color( |
58 |
Integer.valueOf(strings[1]).intValue(), |
59 |
Integer.valueOf(strings[2]).intValue(), |
60 |
Integer.valueOf(strings[3]).intValue() |
61 |
)); |
62 |
return item;
|
63 |
} |
64 |
|
65 |
/*
|
66 |
* (non-Javadoc)
|
67 |
* @see org.gvsig.raster.datastruct.io.RasterLegendIO#read(java.io.File)
|
68 |
*/
|
69 |
public ColorTable read(File input) throws IOException { |
70 |
ArrayList colorItems = new ArrayList(); |
71 |
ColorTable colorTable = new ColorTable();
|
72 |
try {
|
73 |
BufferedReader reader = new BufferedReader(new FileReader(input)); |
74 |
String currentLine;
|
75 |
int cont = 0; |
76 |
while ((currentLine = reader.readLine()) != null) { |
77 |
if (currentLine.charAt(0) == '#') |
78 |
continue;
|
79 |
if (cont == 1) |
80 |
colorTable.setName(currentLine.substring(6));
|
81 |
if (cont > 1) { |
82 |
String[] strings = (" " + currentLine).split("\\s+"); |
83 |
|
84 |
ColorItem colorItem = parseColorItem(strings); |
85 |
if (colorItem == null) |
86 |
continue;
|
87 |
|
88 |
strings = (" " + currentLine).split("\\s+\\d+\\s+\\d+\\s+\\d+\\s+"); |
89 |
|
90 |
if (!strings[1].equals("Untitled")) |
91 |
colorItem.setNameClass(strings[1]);
|
92 |
else
|
93 |
colorItem.setNameClass("");
|
94 |
|
95 |
colorItem.setValue(cont - 2);
|
96 |
colorItems.add(colorItem); |
97 |
} |
98 |
cont++; |
99 |
} |
100 |
reader.close(); |
101 |
} catch (IOException ex) { |
102 |
throw new IOException(); |
103 |
} |
104 |
|
105 |
colorTable.createPaletteFromColorItems(colorItems, false);
|
106 |
return colorTable;
|
107 |
} |
108 |
|
109 |
/*
|
110 |
* (non-Javadoc)
|
111 |
* @see org.gvsig.raster.datastruct.io.RasterLegendIO#write(org.gvsig.raster.datastruct.ColorTable, java.io.File)
|
112 |
*/
|
113 |
public void write(ColorTable colorTable, File output) throws IOException { |
114 |
ArrayList colorItems = colorTable.getColorItems();
|
115 |
|
116 |
try {
|
117 |
BufferedWriter writer = new BufferedWriter(new FileWriter(output)); |
118 |
writer.write("GIMP Palette\n");
|
119 |
writer.write("Name: " + colorTable.getName() + "\n"); |
120 |
writer.write("#\n");
|
121 |
|
122 |
for (int i = 0; i < colorItems.size(); i++) { |
123 |
String line = ""; |
124 |
ColorItem item1 = (ColorItem) colorItems.get(i); |
125 |
Color color = item1.getColor();
|
126 |
line += color.getRed() + " ";
|
127 |
line += color.getGreen() + " ";
|
128 |
line += color.getBlue() + "\t";
|
129 |
if ((item1.getNameClass() != null) && (item1.getNameClass().length() > 0)) |
130 |
line += item1.getNameClass() + "\n";
|
131 |
else
|
132 |
line += "Untitled\n";
|
133 |
writer.write(line); |
134 |
} |
135 |
writer.close(); |
136 |
} catch (IOException ex) { |
137 |
throw new IOException(); |
138 |
} |
139 |
} |
140 |
|
141 |
/*
|
142 |
* (non-Javadoc)
|
143 |
* @see org.gvsig.raster.datastruct.io.RasterLegendIO#getDescription()
|
144 |
*/
|
145 |
public String getDescription() { |
146 |
return "Gimp Palettes"; |
147 |
} |
148 |
} |