Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / raster / lib / legend / impl / DefaultRasterLegendManager.java @ 43862

History | View | Annotate | Download (11.4 KB)

1
package org.gvsig.raster.lib.legend.impl;
2

    
3
import java.awt.Color;
4
import java.awt.image.BufferedImage;
5
import java.awt.image.ColorModel;
6
import java.awt.image.ComponentColorModel;
7
import java.awt.image.IndexColorModel;
8
import java.awt.image.WritableRaster;
9
import java.io.File;
10
import java.util.ArrayList;
11
import java.util.Collection;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15

    
16
import org.apache.commons.lang3.tuple.ImmutablePair;
17
import org.apache.commons.lang3.tuple.Pair;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

    
21
import org.gvsig.raster.lib.buffer.api.operations.OperationList;
22
import org.gvsig.raster.lib.legend.api.RasterLegend;
23
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
24
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
25
import org.gvsig.raster.lib.legend.api.Transparency;
26
import org.gvsig.raster.lib.legend.api.TransparencyRange;
27
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
28
import org.gvsig.raster.lib.legend.api.colortable.ColorTable;
29
import org.gvsig.raster.lib.legend.api.colortable.ColorTableIO;
30
import org.gvsig.raster.lib.legend.api.colortable.ColorTableIOFactory;
31
import org.gvsig.raster.lib.legend.api.colortable.MakeColorTable;
32
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass;
33
import org.gvsig.raster.lib.legend.impl.colortable.DefaultMakeColorTable;
34
import org.gvsig.raster.lib.legend.impl.io.gvSIGColorTableIO;
35

    
36
/**
37
 * Default implementation of {@link RasterLegendManager}.
38
 *
39
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
40
 *
41
 */
42
public class DefaultRasterLegendManager implements RasterLegendManagerServices {
43

    
44
    private static final Logger LOG = LoggerFactory.getLogger(DefaultRasterLegendManager.class);
45

    
46
    private Map<String, ColorTableIOFactory> colorTableIOFactories =
47
        new HashMap<String, ColorTableIOFactory>();
48

    
49
    private ColorManager colorManager;
50

    
51
    @Override
52
    public RasterLegend createLegend() {
53
        return new DefaultRasterLegend();
54
    }
55

    
56
    @Override
57
    public RasterLegend createLegend(ColorInterpretation colorInterpretation) {
58
        return new DefaultRasterLegend(colorInterpretation);
59
    }
60

    
61
    @Override
62
    public RasterLegend createLegend(ColorInterpretation colorInterpretation,
63
        Transparency transparency, OperationList filters) {
64
        return new DefaultRasterLegend(colorInterpretation, transparency, filters);
65
    }
66

    
67
    @Override
68
    public List<Pair<File, ColorTable>> getColorTables(File folder) {
69

    
70
        if (folder.isFile()) {
71
            throw new IllegalArgumentException("File object has to be a directory");
72
        }
73

    
74
        if (!folder.canRead()) {
75
            throw new IllegalArgumentException(
76
                "It is necessary read permissions to get files and load color tables");
77
        }
78

    
79
        List<Pair<File, ColorTable>> colorTables = new ArrayList<Pair<File, ColorTable>>();
80
        for (File file : listFilesForFolder(folder)) {
81

    
82
            Collection<ColorTableIOFactory> factories = this.colorTableIOFactories.values();
83
            for (ColorTableIOFactory colorTableIOFactory : factories) {
84

    
85
                if(colorTableIOFactory.accept(file)){
86
                    ColorTableIO colorTableIO = colorTableIOFactory.create();
87
                    try{
88
                        ColorTable colorTable = colorTableIO.read(file);
89
                        colorTables.add(new ImmutablePair<File, ColorTable>(file,colorTable));
90
                        break;
91
                    } catch (Exception e){
92
                        // Log warn but continue reading files.
93
                        LOG.warn(String.format(
94
                            "%1s factory accepts file but it can not get ColorTable from file",
95
                            colorTableIOFactory.getName()), e);
96
                    }
97
                }
98
            }
99
        }
100

    
101
        return colorTables;
102
    }
103

    
104
    private List<File> listFilesForFolder(File folder) {
105
        List<File> files = new ArrayList<File>();
106
        for (File fileEntry : folder.listFiles()) {
107
            if (fileEntry.isDirectory()) {
108
                listFilesForFolder(fileEntry);
109
            } else {
110
                files.add(fileEntry);
111
            }
112
        }
113
        return files;
114
    }
115

    
116
    @Override
117
    public ColorTable createColorTable() {
118
        return new DefaultColorTable();
119
    }
120

    
121
    @Override
122
    public ColorTable createColorTable(String name, List<ColorTableClass> colorTableClasses,
123
        boolean interpolated) {
124
        return new DefaultColorTable(name, colorTableClasses, interpolated);
125
    }
126

    
127
    @Override
128
    public List<ColorTableClass> createListColorTableClasses(double minimum, double maximum, int intervals, Color fromColor, Color toColor){
129
        double intervalSize = (maximum-minimum)/intervals;
130
        return createListColorTableClasses(minimum, maximum, intervalSize, fromColor, toColor);
131

    
132
    }
133

    
134
    @Override
135
    public List<ColorTableClass> createListColorTableClasses(double minimum, double maximum, double intervalSize, Color fromColor, Color toColor){
136
        List<ColorTableClass> colorTableClasses = new ArrayList<ColorTableClass>();
137

    
138
        int counter = 0;
139
        double value = minimum;
140

    
141
        Color color = fromColor;
142
        while(value<maximum){
143
            double proportion = (value-minimum)/(maximum-minimum);
144
            color = getInterpolatedColor(fromColor, toColor, proportion);
145
            ColorTableClass colorTableClass = this.createColorTableClass(new Integer(counter).toString(), value, 50.0, color);
146
            colorTableClasses.add(colorTableClass);
147
            value = value + intervalSize;
148
            counter++;
149
        }
150
        value = maximum;
151
        ColorTableClass colorTableClass = this.createColorTableClass(new Integer(counter).toString(), value, 50.0, toColor);
152
        colorTableClasses.add(colorTableClass);
153

    
154
        return colorTableClasses;
155
    }
156

    
157
    private Color getInterpolatedColor(Color fromColor, Color toColor, double proportion){
158
        int red = fromColor.getRed();
159
        int green = fromColor.getGreen();
160
        int blue = fromColor.getBlue();
161
        int alpha = fromColor.getAlpha();
162

    
163
            int rangeRed = toColor.getRed()-fromColor.getRed();
164
            red = (int)Math.round(fromColor.getRed()+(rangeRed*proportion));
165

    
166
            int rangeGreen = toColor.getGreen()-fromColor.getGreen();
167
            green = (int)Math.round(fromColor.getGreen()+(rangeGreen*proportion));
168

    
169
            int rangeBlue = toColor.getBlue()-fromColor.getBlue();
170
            blue = (int)Math.round(fromColor.getBlue()+(rangeBlue*proportion));
171

    
172
            int rangeAlpha = toColor.getAlpha()-fromColor.getAlpha();
173
            alpha = (int)Math.round(fromColor.getAlpha()+(rangeAlpha*proportion));
174

    
175
        return new Color(red, green, blue, alpha);
176
    }
177

    
178
    @Override
179
    public ColorTableIO getColorTableIO() {
180
        ColorTableIOFactory defaultFactory = this.colorTableIOFactories.get(gvSIGColorTableIO.NAME);
181
        if (defaultFactory == null) {
182
            if (this.colorTableIOFactories.isEmpty()) {
183
                LOG.warn("Any default Color Table IO factory registered");
184
                return null;
185
            }
186
        }
187
        return defaultFactory.create();
188
    }
189

    
190
    @Override
191
    public List<ColorTableIOFactory> getColorTableIOFactories() {
192
        return new ArrayList<ColorTableIOFactory>(this.colorTableIOFactories.values());
193
    }
194

    
195
    @Override
196
    public ColorTableIOFactory getColorTableIOFactory(String name) {
197
        return this.colorTableIOFactories.get(name);
198
    }
199

    
200
    @Override
201
    public void registerColorTableIOFactory(String name, ColorTableIOFactory colorTableIOFactory) {
202
        colorTableIOFactories.put(name, colorTableIOFactory);
203
    }
204

    
205
    @Override
206
    public ColorTableClass createColorTableClass() {
207
        return new DefaultColorTableClass();
208
    }
209

    
210
    @Override
211
    public ColorTableClass createColorTableClass(String className, double value,
212
        double interpolated, Color color) {
213
        return new DefaultColorTableClass(className, value, color, interpolated);
214
    }
215

    
216
    @Override
217
    public ColorInterpretation createColorInterpretation(String[] coloInterpretations) {
218
        return new DefaultColorInterpretation(coloInterpretations);
219
    }
220

    
221
    @Override
222
    public ColorInterpretation createColorInterpretation(List<String> coloInterpretations) {
223
        String[] x = coloInterpretations.toArray(new String[coloInterpretations.size()]);
224
        return new DefaultColorInterpretation(x);
225
    }
226

    
227
    @Override
228
    public ColorInterpretation createColorInterpretation(String definedColorInterpretation) {
229
        return new DefaultColorInterpretation(definedColorInterpretation);
230
    }
231
    
232
    @Override
233
    public ColorInterpretation createColorInterpretation(BufferedImage image, String prefix) {        
234
        ColorInterpretation colorInterpretation = null;
235
        ColorModel colorModel = image.getColorModel();
236

    
237
        if (colorModel instanceof ComponentColorModel) { //DirectColorModel) {
238
            WritableRaster raster = image.getRaster();
239
            if (raster.getNumBands() == 3) {
240
                colorInterpretation = this.createColorInterpretation(ColorInterpretation.RGB);
241
            } else if (raster.getNumBands() == 4) {
242
                colorInterpretation = this.createColorInterpretation(ColorInterpretation.ARGB);
243
            }
244
        } else if (colorModel instanceof IndexColorModel) {
245
            colorInterpretation = this.createColorInterpretation(ColorInterpretation.PALETTE);
246

    
247
            ColorTable colorTable;
248
            IndexColorModel indexColorModel = (IndexColorModel) colorModel;
249
            int[] rgbs = new int[indexColorModel.getMapSize()];
250
            boolean hasalpha = indexColorModel.hasAlpha();
251
            indexColorModel.getRGBs(rgbs);
252

    
253
            List<ColorTableClass> colorTableClasses = new ArrayList<>();
254

    
255
            for (int i = 0; i < rgbs.length; i++) {
256
                String className = i + "";
257
                double value = ((byte) i);
258
                double interpolation = 50.0;
259
                Color color = new Color(rgbs[i], hasalpha);
260
                ColorTableClass colorTableClass =
261
                    this.createColorTableClass(className, value, interpolation, color);
262
                colorTableClasses.add(colorTableClass);
263
            }
264
            String colorTableName = prefix + "_color_table";
265
            colorTable = this.createColorTable(colorTableName, colorTableClasses, true);
266

    
267
            colorInterpretation.setPalette(colorTable);
268
            colorInterpretation.setPaletteBand(0);
269

    
270
        }
271

    
272
        return colorInterpretation;
273
    }
274

    
275
    @Override
276
    public Transparency createTransparency() {
277
        return new DefaultTransparency();
278
    }
279

    
280
    @Override
281
    public Transparency createTransparency(int transparency,
282
        List<TransparencyRange> transparencyRanges) {
283
        return new DefaultTransparency(transparency, transparencyRanges);
284
    }
285

    
286
    @Override
287
    public TransparencyRange createTransparencyRange() {
288
        return new DefaultTransparencyRange();
289
    }
290

    
291
    @Override
292
    public TransparencyRange createTransparencyRange(int[] redRange, int[] greenRange, int[] blueRange, int alpha,
293
        boolean isAnd) {
294
        return new DefaultTransparencyRange(redRange, greenRange, blueRange, alpha, isAnd);
295
    }
296

    
297

    
298
    @Override
299
    public MakeColorTable createMakeColorTable() {
300
        return new DefaultMakeColorTable();
301
    }
302

    
303
    @Override
304
    public ColorManager getColorManager() {
305
        if( colorManager == null ) {
306
            colorManager = new DefaultColorManager();
307
        }
308
        return colorManager;
309
    }
310
}