Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java @ 424:21fbd254db71
Added support for converting 2D rasters into polygons.
gnv-artifacts/trunk@472 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 21 Dec 2009 18:00:54 +0000 |
parents | |
children | 3a0c0ad113d9 |
comparison
equal
deleted
inserted
replaced
423:2402173a1490 | 424:21fbd254db71 |
---|---|
1 package de.intevation.gnv.raster; | |
2 | |
3 import org.w3c.dom.Document; | |
4 import org.w3c.dom.Element; | |
5 import org.w3c.dom.NodeList; | |
6 | |
7 import java.util.Arrays; | |
8 | |
9 import java.awt.Color; | |
10 | |
11 import de.intevation.gnv.raster.Raster.ValueToIndex; | |
12 | |
13 /** | |
14 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | |
15 */ | |
16 public class Palette | |
17 implements ValueToIndex | |
18 { | |
19 public static final class Entry | |
20 implements Comparable | |
21 { | |
22 private Entry left; | |
23 private Entry right; | |
24 | |
25 private int index; | |
26 | |
27 private double from; | |
28 private double to; | |
29 | |
30 private String description; | |
31 | |
32 private Color color; | |
33 | |
34 public Entry() { | |
35 } | |
36 | |
37 public Entry( | |
38 int index, | |
39 double from, | |
40 double to, | |
41 Color color, | |
42 String description | |
43 ) { | |
44 this.index = index; | |
45 this.from = from; | |
46 this.to = to; | |
47 this.color = color; | |
48 this.description = description; | |
49 } | |
50 | |
51 public int compareTo(Object other) { | |
52 Entry e = (Entry)other; | |
53 if (from < e.from) return -1; | |
54 if (from > e.from) return +1; | |
55 return 0; | |
56 } | |
57 | |
58 public Entry locateEntry(double value) { | |
59 Entry current = this; | |
60 while (current != null) { | |
61 boolean b = value >= current.from; | |
62 if (b && value <= current.to) { | |
63 return current; | |
64 } | |
65 current = b | |
66 ? current.right | |
67 : current.left; | |
68 } | |
69 return null; | |
70 } | |
71 | |
72 public int locate(double value) { | |
73 Entry entry = locateEntry(value); | |
74 return entry != null | |
75 ? entry.index | |
76 : -1; | |
77 } | |
78 | |
79 public Entry findByIndex(int index) { | |
80 Entry current = this; | |
81 while (current != null) { | |
82 if (current.index == index) { | |
83 break; | |
84 } | |
85 current = index < current.index | |
86 ? current.left | |
87 : current.right; | |
88 } | |
89 return current; | |
90 } | |
91 | |
92 public String getDescription() { | |
93 return description; | |
94 } | |
95 } // class Entry | |
96 | |
97 protected Entry [] entries; | |
98 protected Entry lookup; | |
99 protected Color [] rgbs; | |
100 | |
101 private Entry buildLookup(Entry [] entries, int lo, int hi) { | |
102 if (lo > hi) { | |
103 return null; | |
104 } | |
105 int mid = (lo + hi)/2; | |
106 Entry entry = entries[mid]; | |
107 entry.left = buildLookup(entries, lo, mid-1); | |
108 entry.right = buildLookup(entries, mid+1, hi); | |
109 return entry; | |
110 } | |
111 | |
112 public Palette() { | |
113 } | |
114 | |
115 public Palette(Document document) { | |
116 | |
117 NodeList rangeNodes = document.getElementsByTagName("range"); | |
118 | |
119 entries = new Entry[rangeNodes.getLength()]; | |
120 rgbs = new Color[entries.length]; | |
121 | |
122 for (int i = 0; i < entries.length; ++i) { | |
123 Element rangeElement = (Element)rangeNodes.item(i); | |
124 double from = doubleValue(rangeElement.getAttribute("from")); | |
125 double to = doubleValue(rangeElement.getAttribute("to")); | |
126 Color color = color(rangeElement.getAttribute("rgb")); | |
127 String description = rangeElement.getAttribute("description"); | |
128 if (from > to) { double t = from; from = to; to = t; } | |
129 entries[i] = new Entry(i, from, to, color, description); | |
130 rgbs[i] = color; | |
131 } | |
132 | |
133 buildLookup(); | |
134 } | |
135 | |
136 private static final double doubleValue(String s) { | |
137 if (s == null || (s = s.trim()).length() == 0) { | |
138 return 0d; | |
139 } | |
140 if ((s = s.toLowerCase()).startsWith("-inf")) { | |
141 return -Double.MAX_VALUE; // XXX: Not using Double.NEGATIVE_INFINITY! | |
142 } | |
143 | |
144 if (s.startsWith("inf")) { | |
145 return Double.MAX_VALUE; // XXX: Not using Double.NEGATIVE_INFINITY! | |
146 } | |
147 | |
148 return Double.parseDouble(s); | |
149 } | |
150 | |
151 private static final Color color(String s) { | |
152 if (s == null || (s = s.trim()).length() == 0) { | |
153 return null; | |
154 } | |
155 return Color.decode(s); | |
156 } | |
157 | |
158 | |
159 protected void buildLookup() { | |
160 Arrays.sort(entries); | |
161 lookup = buildLookup(entries, 0, entries.length-1); | |
162 } | |
163 | |
164 public int getSize() { | |
165 return rgbs.length; | |
166 } | |
167 | |
168 public Color getColor(int index) { | |
169 return rgbs[index]; | |
170 } | |
171 | |
172 public int indexToRGB(int index) { | |
173 return rgbs[index].getRGB(); | |
174 } | |
175 | |
176 public int toIndex(double value) { | |
177 return lookup.locate(value); | |
178 } | |
179 | |
180 public Entry getEntry(double value) { | |
181 return lookup.locateEntry(value); | |
182 } | |
183 | |
184 public Entry getEntryByIndex(int index) { | |
185 return lookup.findByIndex(index); | |
186 } | |
187 } | |
188 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |