Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.legend / org.gvsig.raster.lib.legend.impl / src / main / java / org / gvsig / raster / lib / legend / impl / io / GimpPaletteColorTableIO.java @ 6900

History | View | Annotate | Download (5.18 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.util.ArrayList;
11
import java.util.List;
12

    
13
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
14
import org.gvsig.raster.lib.legend.api.colortable.ColorTable;
15
import org.gvsig.raster.lib.legend.api.colortable.ColorTableIO;
16
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass;
17
import org.gvsig.raster.lib.legend.api.exceptions.ColorTableIOException;
18

    
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

    
22
/**
23
 * {@link ColorTableIO} to read Gimp palettes to {@link ColorTable} and write
24
 * {@link ColorTable} to Gimp palettes.
25
 * 
26
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
27
 *
28
 */
29
public class GimpPaletteColorTableIO implements ColorTableIO {
30

    
31
    private static final Logger LOG = LoggerFactory.getLogger(GimpPaletteColorTableIO.class);
32

    
33
    public static final String NAME = "GimpPaletteColorTableIO";
34

    
35
    public static final String DESCRIPTION = "ColorTableIO to read and write Gimp palettes";
36

    
37
    @Override
38
    public String getName() {
39
        return NAME;
40
    }
41

    
42
    @Override
43
    public String getDescription() {
44
        return DESCRIPTION;
45
    }
46

    
47
    @Override
48
    public ColorTable read(File file) throws ColorTableIOException {
49

    
50
        if (file.isDirectory()) {
51
            throw new IllegalArgumentException("File can not be a directory.");
52
        }
53

    
54
        if (!file.exists() || !file.canRead()) {
55
            throw new IllegalArgumentException("File does not exist or can be readed");
56
        }
57

    
58
        List<ColorTableClass> colorItems = new ArrayList<ColorTableClass>();
59
        ColorTable colorTable = RasterLegendLocator.getRasterLegendManager().createColorTable();
60
        try {
61
            BufferedReader reader = new BufferedReader(new FileReader(file));
62
            String currentLine;
63
            int cont = 0;
64
            while ((currentLine = reader.readLine()) != null) {
65
                if (currentLine.charAt(0) == '#'){
66
                    continue;
67
                }
68
                if (cont == 1) {
69
                    colorTable.setName(currentLine.substring(6));
70
                } else if (cont > 1) {
71
                    String[] strings = (" " + currentLine).split("\\s+");
72

    
73
                    ColorTableClass colorTableClass = null;
74
                    if (strings.length >= 4) {
75
                        colorTableClass = parseColorItem(strings);
76
                    } else {
77
                        continue;
78
                    }
79

    
80
                    strings = (" " + currentLine).split("\\s+\\d+\\s+\\d+\\s+\\d+\\s+");
81

    
82
                    if (!strings[1].equals("Untitled")) {
83
                        colorTableClass.setName(strings[1]);
84
                    } else {
85
                        colorTableClass.setName("");
86
                    }
87

    
88
                    colorTableClass.setValue(cont - 2);
89
                    colorItems.add(colorTableClass);
90
                }
91
                cont++;
92
            }
93
            reader.close();
94
        } catch (IOException ex) {
95
            LOG.error("Can not read file to get gimp palette", ex);
96
            throw new ColorTableIOException(ex);
97
        }
98

    
99
        colorTable.setClasses(colorItems, false);
100
        return colorTable;
101
    }
102

    
103
    private ColorTableClass parseColorItem(String[] strings) {
104
        ColorTableClass item = RasterLegendLocator.getRasterLegendManager().createColorTableClass();
105

    
106
        item.setInterpolated(100);
107

    
108
        item.setColor(new Color(Integer.valueOf(strings[1]).intValue(), Integer.valueOf(strings[2])
109
            .intValue(), Integer.valueOf(strings[3]).intValue()));
110
        return item;
111
    }
112

    
113
    @Override
114
    public void read(ColorTable colorTable, File file) throws ColorTableIOException {
115
        ColorTable tmpColorTable = this.read(file);
116
        colorTable.copyFrom(tmpColorTable);
117
    }
118

    
119
    @Override
120
    public void write(ColorTable colorTable, File file) throws ColorTableIOException {
121
        List<ColorTableClass> colorTableClasses = colorTable.getClasses();
122

    
123
        BufferedWriter writer;
124
        try {
125
            writer = new BufferedWriter(new FileWriter(file));
126
            writer.write("GIMP Palette\n");
127
            writer.write("Name: " + colorTable.getName() + "\n");
128
            writer.write("#\n");
129

    
130
            for (int i = 0; i < colorTableClasses.size(); i++) {
131
                String line = "";
132
                ColorTableClass item1 = colorTableClasses.get(i);
133
                Color color = item1.getColor();
134
                line += color.getRed() + " ";
135
                line += color.getGreen() + " ";
136
                line += color.getBlue() + "\t";
137
                if ((item1.getName() != null) && (item1.getName().length() > 0)) {
138
                    line += item1.getName() + "\n";
139
                } else {
140
                    line += "Untitled\n";
141
                }
142
                writer.write(line);
143
            }
144
            writer.close();
145
        } catch (IOException e) {
146
            LOG.error("Can not write to create new Gimp palette file from color table", e);
147
            throw new ColorTableIOException(e);
148
        }
149
    }
150
}