Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit 08d33ead6e5292f4771b8e5919f331bf8df243ea @jdf committed Feb 16, 2010
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
@@ -0,0 +1,5 @@
+*.png
+*.tif
+*.jpg
+bin
+
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>allrgb</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,8 @@
+package allrgb;
+
+interface ColorIndex
+{
+
+ public int lookup(final int src);
+
+}
@@ -0,0 +1,63 @@
+package allrgb;
+
+class ColorList
+{
+ private int[] colors = new int[100];
+ private int size;
+
+ void add(final int c)
+ {
+ if (size == colors.length)
+ {
+ final int[] tmp = new int[colors.length * 2];
+ System.arraycopy(colors, 0, tmp, 0, colors.length);
+ colors = tmp;
+ }
+ colors[size++] = c;
+ }
+
+ boolean isEmpty()
+ {
+ return size == 0;
+ }
+
+ final int[] aLab = new int[3];
+ final int[] bLab = new int[3];
+
+ double distanceSquared(final int a, final int b)
+ {
+ LabTable.INSTANCE.get(a, aLab);
+ LabTable.INSTANCE.get(b, bLab);
+ final double dr = aLab[0] - bLab[0];
+ final double dg = aLab[1] - bLab[1];
+ final double db = aLab[2] - bLab[2];
+ return dr * dr + dg * dg + db * db;
+ }
+
+ int findAndRemoveClosest(final int target)
+ {
+ int bestIndex = -1;
+ double bestDistanceSquared = Integer.MAX_VALUE;
+ int numberSeen = 0;
+ int index = 0;
+ while (numberSeen < size)
+ {
+ while (index < colors.length && colors[index] == -1)
+ index++;
+ if (index >= colors.length)
+ throw new IllegalStateException("How?");
+ final double distanceSquared = distanceSquared(target, colors[index]);
+ if (distanceSquared < bestDistanceSquared)
+ {
+ bestDistanceSquared = distanceSquared;
+ bestIndex = index;
+ }
+ numberSeen++;
+ index++;
+ }
+ int color = colors[bestIndex];
+ colors[bestIndex] = -1;
+ size--;
+ return color;
+ }
+}
@@ -0,0 +1,86 @@
+/**
+ *
+ */
+package allrgb;
+
+class LabIndex1 implements ColorIndex
+{
+ final ColorList[][] colors = new ColorList[256][173];
+ // final ColorList[][] colors = new ColorList[256][205];
+ final int[] lab = new int[3];
+
+ public LabIndex1()
+ {
+ for (int c = 0; c <= 0xFFFFFF; c++)
+ {
+ LabTable.INSTANCE.get(c, lab);
+ final int lightness = lab[0];
+ final int a = lab[1];
+ if (colors[lightness][a] == null)
+ colors[lightness][a] = new ColorList();
+ colors[lightness][a].add(c);
+ }
+ }
+
+ public int lookup(final int src)
+ {
+ LabTable.INSTANCE.get(src, lab);
+ final int startL = lab[0];
+ final int startA = lab[1];
+ int delta = 0;
+ int br = startL;
+ ColorList[] lumTarget = colors[startL];
+ while (lumTarget == null)
+ {
+ delta++;
+ br = startL + delta;
+ if (br >= 0 && br < colors.length && colors[br] != null)
+ {
+ lumTarget = colors[br];
+ break;
+ }
+ br = startL - delta;
+ if (br >= 0 && br < colors.length && colors[br] != null)
+ {
+ lumTarget = colors[br];
+ break;
+ }
+ }
+
+ ColorList target = lumTarget[startA];
+ delta = 0;
+ int a = startA;
+ while (target == null)
+ {
+ delta++;
+ a = startA + delta;
+ if (a < lumTarget.length && lumTarget[a] != null)
+ {
+ target = lumTarget[a];
+ break;
+ }
+ a = startA - delta;
+ if (a >= 0 && lumTarget[a] != null)
+ {
+ target = lumTarget[a];
+ break;
+ }
+ }
+
+ final int result = target.findAndRemoveClosest(src);
+ if (target.isEmpty())
+ {
+ lumTarget[a] = null;
+ boolean empty = true;
+ for (ColorList element : lumTarget)
+ if (element != null)
+ {
+ empty = false;
+ break;
+ }
+ if (empty)
+ colors[br] = null;
+ }
+ return result;
+ }
+}
@@ -0,0 +1,52 @@
+/**
+ *
+ */
+package allrgb;
+
+class LabIndex2 implements ColorIndex
+{
+ final ColorList[] colors = new ColorList[256];
+ final int[] lab = new int[3];
+
+ public LabIndex2()
+ {
+ for (int c = 0; c <= 0xFFFFFF; c++)
+ {
+ LabTable.INSTANCE.get(c, lab);
+ final int lightness = lab[0];
+ if (colors[lightness] == null)
+ colors[lightness] = new ColorList();
+ colors[lightness].add(c);
+ }
+ }
+
+ public int lookup(final int src)
+ {
+ LabTable.INSTANCE.get(src, lab);
+ final int startL = lab[0];
+ int delta = 0;
+ int br = startL;
+ ColorList target = colors[startL];
+ while (target == null)
+ {
+ delta++;
+ br = startL + delta;
+ if (br >= 0 && br < colors.length && colors[br] != null)
+ {
+ target = colors[br];
+ break;
+ }
+ br = startL - delta;
+ if (br >= 0 && br < colors.length && colors[br] != null)
+ {
+ target = colors[br];
+ break;
+ }
+ }
+
+ final int result = target.findAndRemoveClosest(src);
+ if (target.isEmpty())
+ colors[br] = null;
+ return result;
+ }
+}
@@ -0,0 +1,90 @@
+/**
+ *
+ */
+package allrgb;
+
+class LabIndex3 implements ColorIndex
+{
+ final ColorList[][] laIndex = new ColorList[256][173];
+ final ColorList[][] lbIndex = new ColorList[256][205];
+ final int[] lab = new int[3];
+
+ public LabIndex3()
+ {
+ for (int c = 0; c <= 0xFFFFFF; c++)
+ {
+ LabTable.INSTANCE.get(c, lab);
+ final int lightness = lab[0];
+ final int a = lab[1];
+ final int b = lab[2];
+ if (laIndex[lightness][a] == null)
+ laIndex[lightness][a] = new ColorList();
+ laIndex[lightness][a].add(c);
+ if (lbIndex[lightness][b] == null)
+ lbIndex[lightness][b] = new ColorList();
+ lbIndex[lightness][b].add(c);
+ }
+ }
+
+ public int lookup(final int src)
+ {
+ LabTable.INSTANCE.get(src, lab);
+ final int startL = lab[0];
+ final int startA = lab[1];
+ int delta = 0;
+ int br = startL;
+ ColorList[] lumTarget = laIndex[startL];
+ while (lumTarget == null)
+ {
+ delta++;
+ br = startL + delta;
+ if (br >= 0 && br < laIndex.length && laIndex[br] != null)
+ {
+ lumTarget = laIndex[br];
+ break;
+ }
+ br = startL - delta;
+ if (br >= 0 && br < laIndex.length && laIndex[br] != null)
+ {
+ lumTarget = laIndex[br];
+ break;
+ }
+ }
+
+ ColorList target = lumTarget[startA];
+ delta = 0;
+ int a = startA;
+ while (target == null)
+ {
+ delta++;
+ a = startA + delta;
+ if (a < lumTarget.length && lumTarget[a] != null)
+ {
+ target = lumTarget[a];
+ break;
+ }
+ a = startA - delta;
+ if (a >= 0 && lumTarget[a] != null)
+ {
+ target = lumTarget[a];
+ break;
+ }
+ }
+
+ final int result = target.findAndRemoveClosest(src);
+ if (target.isEmpty())
+ {
+ lumTarget[a] = null;
+ boolean empty = true;
+ for (ColorList element : lumTarget)
+ if (element != null)
+ {
+ empty = false;
+ break;
+ }
+ if (empty)
+ laIndex[br] = null;
+ }
+ return result;
+ }
+}
Oops, something went wrong.

0 comments on commit 08d33ea

Please sign in to comment.