Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / legend / GimpPalettes.java @ 162

History | View | Annotate | Download (4.63 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.datastruct.legend;
23

    
24
import java.awt.Color;
25
import java.io.BufferedReader;
26
import java.io.BufferedWriter;
27
import java.io.File;
28
import java.io.FileReader;
29
import java.io.FileWriter;
30
import java.io.IOException;
31
import java.util.ArrayList;
32

    
33
import org.gvsig.fmap.dal.coverage.datastruct.ColorItem;
34
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
35
import org.gvsig.raster.impl.datastruct.ColorItemImpl;
36
import org.gvsig.raster.impl.store.properties.DataStoreColorTable;
37
/**
38
 * Clase GimpPalettes sirve para importar y exportar tablas de color de Gimp.
39
 * Los ficheros de Paletas de gimp tienen la extension .gpl
40
 *
41
 * @version 03/12/2007
42
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
43
 */
44
public class GimpPalettes extends RasterLegendIO {
45

    
46
        /**
47
         * Devuelve el ColorItem asociado a una linea de un fichero de tablas de color
48
         * de Gimp
49
         * @param strings
50
         * @param pos
51
         * @return
52
         */
53
        private ColorItem parseColorItem(String[] strings) {
54
                if (strings.length < 4)
55
                        return null;
56

    
57
                ColorItem item = new ColorItemImpl();
58

    
59
                item.setInterpolated(100);
60

    
61
                item.setColor(new Color(
62
                                Integer.valueOf(strings[1]).intValue(),
63
                                Integer.valueOf(strings[2]).intValue(),
64
                                Integer.valueOf(strings[3]).intValue()
65
                        ));
66
                return item;
67
        }
68

    
69
        /*
70
         * (non-Javadoc)
71
         * @see org.gvsig.raster.datastruct.io.RasterLegendIO#read(java.io.File)
72
         */
73
        public ColorTable read(File input) throws IOException {
74
                ArrayList<ColorItem> colorItems = new ArrayList<ColorItem>();
75
                ColorTable colorTable = new DataStoreColorTable();
76
                try {
77
                        BufferedReader reader = new BufferedReader(new FileReader(input));
78
                        String currentLine;
79
                        int cont = 0;
80
                        while ((currentLine = reader.readLine()) != null) {
81
                                if (currentLine.charAt(0) == '#')
82
                                        continue;
83
                                if (cont == 1)
84
                                        colorTable.setName(currentLine.substring(6));
85
                                if (cont > 1) {
86
                                        String[] strings = (" " + currentLine).split("\\s+");
87

    
88
                                        ColorItem colorItem = parseColorItem(strings);
89
                                        if (colorItem == null)
90
                                                continue;
91

    
92
                                        strings = (" " + currentLine).split("\\s+\\d+\\s+\\d+\\s+\\d+\\s+");
93

    
94
                                        if (!strings[1].equals("Untitled"))
95
                                                colorItem.setNameClass(strings[1]);
96
                                        else
97
                                                colorItem.setNameClass("");
98

    
99
                                        colorItem.setValue(cont - 2);
100
                                        colorItems.add(colorItem);
101
                                }
102
                                cont++;
103
                        }
104
                        reader.close();
105
                } catch (IOException ex) {
106
                        throw new IOException();
107
                }
108

    
109
                colorTable.createPaletteFromColorItems(colorItems, false);
110
                return colorTable;
111
        }
112

    
113
        /*
114
         * (non-Javadoc)
115
         * @see org.gvsig.raster.datastruct.io.RasterLegendIO#write(org.gvsig.raster.datastruct.ColorTable, java.io.File)
116
         */
117
        public void write(ColorTable colorTable, File output) throws IOException {
118
                ArrayList<ColorItem> colorItems = colorTable.getColorItems();
119

    
120
                try {
121
                        BufferedWriter writer = new BufferedWriter(new FileWriter(output));
122
                        writer.write("GIMP Palette\n");
123
                        writer.write("Name: " + colorTable.getName() + "\n");
124
                        writer.write("#\n");
125

    
126
                        for (int i = 0; i < colorItems.size(); i++) {
127
                                String line = "";
128
                                ColorItem item1 = (ColorItem) colorItems.get(i);
129
                                Color color = item1.getColor();
130
                                line += color.getRed() + " ";
131
                                line += color.getGreen() + " ";
132
                                line += color.getBlue() + "\t";
133
                                if ((item1.getNameClass() != null) && (item1.getNameClass().length() > 0))
134
                                        line += item1.getNameClass() + "\n";
135
                                else
136
                                        line += "Untitled\n";
137
                                writer.write(line);
138
                        }
139
                        writer.close();
140
                } catch (IOException ex) {
141
                        throw new IOException();
142
                }
143
        }
144

    
145
        /*
146
         * (non-Javadoc)
147
         * @see org.gvsig.raster.datastruct.io.RasterLegendIO#getDescription()
148
         */
149
        public String getDescription() {
150
                return "Gimp Palettes";
151
        }
152
}