changeset 435:67091b17462d

Added code to split palette interval into equal sized parts. gnv-artifacts/trunk@483 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 25 Dec 2009 12:00:14 +0000
parents 0eed5749fd63
children 6642ab6c583c
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java
diffstat 2 files changed, 71 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Wed Dec 23 15:28:40 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Fri Dec 25 12:00:14 2009 +0000
@@ -1,3 +1,12 @@
+2009-12-25	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/gnv/raster/Palette.java: Added
+	  method subdive(N) to Palette class which creates a new
+	  palette in which each interval is splitted into N
+	  equal sized intervals. Infinity sized intervals are not
+	  splitted. This is useful to fulfill the conditions of
+	  gnv/issue108.
+
 2009-12-23	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/gnv/math/Interpolation2D.java: 
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java	Wed Dec 23 15:28:40 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java	Fri Dec 25 12:00:14 2009 +0000
@@ -34,6 +34,14 @@
         public Entry() {
         }
 
+        public Entry(Entry other) {
+            index       = other.index;
+            from        = other.from;
+            to          = other.to;
+            description = other.description;
+            color       = other.color;
+        }
+
         public Entry(
             int    index, 
             double from, 
@@ -92,6 +100,11 @@
         public String getDescription() {
             return description;
         }
+
+        public boolean isInfinity() {
+            return from == -Double.MAX_VALUE
+              ||   to   ==  Double.MAX_VALUE;
+        }
     } // class Entry
 
     protected Entry [] entries;
@@ -140,6 +153,51 @@
         buildLookup();
     }
 
+    public Palette(Palette original, int N) {
+        if (N < 2)  {
+            throw new IllegalArgumentException("N < 2");
+        }
+
+        Entry [] origEntries = original.entries;
+
+        int newSize = 0;
+        for (int i = 0; i < origEntries.length; ++i) {
+            // cannot split infinity intervals
+            newSize += origEntries[i].isInfinity() ? 1 : N;
+        }
+
+        entries = new Entry[newSize];
+        rgbs    = new Color[newSize];
+
+        for (int i = 0, j = 0; i < origEntries.length; ++i) {
+            Entry origEntry = origEntries[i];
+            if (origEntry.isInfinity()) {
+                // infinity intervals are just copied
+                Entry nEntry = new Entry(origEntry);
+                entries[j] = nEntry;
+                rgbs[j]    = nEntry.color;
+                nEntry.index = j++;
+            }
+            else {
+                // split interval into N parts
+                double from  = origEntry.from;
+                double to    = origEntry.to;
+                double delta = (to - from)/N;
+                while (from < to) {
+                    Entry nEntry = new Entry(origEntry);
+                    nEntry.from = from;
+                    nEntry.to   = from + delta;
+                    from += delta;
+                    entries[j] = nEntry;
+                    rgbs[j]    = nEntry.color;
+                    nEntry.index = j++;
+                }
+            } // limited interval
+        } // for all original entries
+
+        buildLookup();
+    }
+
     private static final double doubleValue(String s) {
         if (s == null || (s = s.trim()).length() == 0) {
             return 0d;
@@ -168,6 +226,10 @@
         lookup = buildLookup(entries, 0, entries.length-1);
     }
 
+    public Palette subdivide(int N) {
+        return new Palette(this, N);
+    }
+
     public String getDescription() {
         return description;
     }

http://dive4elements.wald.intevation.org