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 / GimpGradients.java @ 162

History | View | Annotate | Download (6.25 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.text.NumberFormat;
32
import java.util.ArrayList;
33

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

    
47
        /**
48
         * Devuelve el ColorItem asociado a una linea de un fichero de gradientes de
49
         * Gimp
50
         * @param strings
51
         * @param pos
52
         * @return
53
         */
54
        private ColorItem parseColorItem(String[] strings, int pos) {
55
                ColorItem item = new ColorItemImpl();
56

    
57
                double pos1 = Double.valueOf(strings[0]).doubleValue();
58
                double pos2 = Double.valueOf(strings[1]).doubleValue();
59
                double pos3 = Double.valueOf(strings[2]).doubleValue();
60
                if (pos == 0)
61
                        item.setValue(pos1 * 255);
62
                else
63
                        item.setValue(pos3 * 255);
64
                if (pos == 0)
65
                        item.setInterpolated((100 * (pos2 - pos1)) / (pos3 - pos1));
66
                if (pos == 0)
67
                        item.setColor(new Color(
68
                                (int) (Double.valueOf(strings[3]).doubleValue() * 255.0),
69
                                (int) (Double.valueOf(strings[4]).doubleValue() * 255.0),
70
                                (int) (Double.valueOf(strings[5]).doubleValue() * 255.0),
71
                                (int) (Double.valueOf(strings[6]).doubleValue() * 255.0)
72
                                ));
73
                else
74
                        item.setColor(new Color(
75
                                        (int) (Double.valueOf(strings[7]).doubleValue() * 255.0),
76
                                        (int) (Double.valueOf(strings[8]).doubleValue() * 255.0),
77
                                        (int) (Double.valueOf(strings[9]).doubleValue() * 255.0),
78
                                        (int) (Double.valueOf(strings[10]).doubleValue() * 255.0)
79
                                        ));
80
                return item;
81
        }
82

    
83
        /*
84
         * (non-Javadoc)
85
         * @see org.gvsig.raster.datastruct.io.RasterLegendIO#read(java.io.File)
86
         */
87
        public ColorTable read(File input) throws IOException {
88
                ArrayList<ColorItem> colorItems = new ArrayList<ColorItem>();
89
                ColorTable colorTable = new DataStoreColorTable();
90
                try {
91
                        BufferedReader reader = new BufferedReader(new FileReader(input));
92
                        String currentLine;
93
                        int cont = 0;
94
                        while ((currentLine = reader.readLine()) != null) {
95
                                if (cont == 1)
96
                                        colorTable.setName(currentLine.substring(6));
97

    
98
                                if (cont > 1) {
99
                                        String[] strings = currentLine.split("\\s+");
100

    
101
                                        if (strings.length < 13)
102
                                                continue;
103

    
104
                                        colorItems.add(parseColorItem(strings, 0));
105
                                        colorItems.add(parseColorItem(strings, 1));
106
                                }
107
                                cont++;
108
                        }
109
                        reader.close();
110
                } catch (IOException ex) {
111
                        throw new IOException();
112
                }
113

    
114
                colorTable.createPaletteFromColorItems(colorItems, true);
115
                return colorTable;
116
        }
117

    
118
        /*
119
         * (non-Javadoc)
120
         * @see org.gvsig.raster.datastruct.io.RasterLegendIO#write(org.gvsig.raster.datastruct.ColorTable, java.io.File)
121
         */
122
        public void write(ColorTable colorTable, File output) throws IOException {
123
                ArrayList<ColorItem> colorItems = colorTable.getColorItems();
124
                try {
125
                        BufferedWriter writer = new BufferedWriter(new FileWriter(output));
126
                        writer.write("GIMP Gradient\n");
127
                        writer.write("Name: " + colorTable.getName() + "\n");
128
                        writer.write((colorItems.size() - 1) + "\n");
129
                        String line;
130

    
131
                        for (int i = 1; i < colorItems.size(); i++) {
132
                                double min = ((ColorItem) colorItems.get(0)).getValue();
133
                                double max = ((ColorItem) colorItems.get(colorItems.size() - 1)).getValue();
134

    
135
                                line = "";
136
                                ColorItem item1 = (ColorItem) colorItems.get(i - 1);
137
                                ColorItem item2 = (ColorItem) colorItems.get(i);
138

    
139
                                double pos1 = (item1.getValue() - min) / (max - min);
140
                                double pos2 = (item2.getValue() - min) / (max - min);
141

    
142
                                NumberFormat format = NumberFormat.getNumberInstance();
143
                                format.setMaximumFractionDigits(6);
144
                                format.setMinimumFractionDigits(6);
145
                                line += format.format(pos1).replaceAll(",", ".") + " ";
146
                                double interp = pos1 + (((pos2 - pos1) * item1.getInterpolated()) / 100.0);
147
                                line += format.format(interp).replaceAll(",", ".") + " ";
148
                                line += format.format(pos2).replaceAll(",", ".") + " ";
149

    
150
                                Color color = item1.getColor();
151
                                line += format.format(color.getRed() / 255.0).replaceAll(",", ".") + " ";
152
                                line += format.format(color.getGreen() / 255.0).replaceAll(",", ".") + " ";
153
                                line += format.format(color.getBlue() / 255.0).replaceAll(",", ".") + " ";
154
                                line += format.format(color.getAlpha() / 255.0).replaceAll(",", ".") + " ";
155

    
156
                                color = item2.getColor();
157
                                line += format.format(color.getRed() / 255.0).replaceAll(",", ".") + " ";
158
                                line += format.format(color.getGreen() / 255.0).replaceAll(",", ".") + " ";
159
                                line += format.format(color.getBlue() / 255.0).replaceAll(",", ".") + " ";
160
                                line += format.format(color.getAlpha() / 255.0).replaceAll(",", ".") + " ";
161

    
162
                                line += "0 0\n";
163
                                writer.write(line);
164
                        }
165
                        writer.close();
166
                } catch (IOException ex) {
167
                        throw new IOException();
168
                }
169
        }
170

    
171
        /*
172
         * (non-Javadoc)
173
         * @see org.gvsig.raster.datastruct.io.RasterLegendIO#getDescription()
174
         */
175
        public String getDescription() {
176
                return "Gimp Gradients";
177
        }
178
}