Skip to content

Commit 3ecd816

Browse files
committed
Add utilities for converting LUTs to ColorTables
1 parent baa19e4 commit 3ecd816

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package net.imglib2.imagej;
2+
3+
import ij.process.LUT;
4+
import net.imglib2.util.Binning;
5+
import net.imglib2.display.ColorTable;
6+
7+
import java.awt.*;
8+
9+
/**
10+
* Provides convenience functions to wrap ImageJ {@link ij.process.LUT} objects into ImgLib2
11+
* {@link net.imglib2.display.ColorTable}s.
12+
*
13+
*
14+
* @author Stephan Preibisch
15+
* @author Stephan Saalfeld
16+
* @author Matthias Arzt
17+
* @author Gabriel Selzer
18+
*/
19+
public class LUTToColorTable {
20+
21+
public static ColorTable wrap(final LUT lut) {
22+
return new ColorTable() {
23+
@Override
24+
public int lookupARGB(double min, double max, double value) {
25+
final int bins = getLength();
26+
final int bin = Binning.valueToBin( bins, min, max, value );
27+
return lut.getRGB( bin );
28+
}
29+
30+
@Override
31+
public int getComponentCount() {
32+
return 4;
33+
}
34+
35+
@Override
36+
public int getLength() {
37+
return 256;
38+
}
39+
40+
@Override
41+
public int get(int comp, int bin) {
42+
int rgb = lut.getRGB(bin);
43+
int shift = comp == ColorTable.RED ? 16 :
44+
comp == ColorTable.GREEN ? 8 :
45+
comp == ColorTable.BLUE ? 0 :
46+
24;
47+
return (rgb >> shift) & 0xff;
48+
}
49+
50+
@Override
51+
public int getResampled(int comp, int bins, int bin) {
52+
final int newBin = ( int ) ( ( long ) getLength() * bin / bins );
53+
return get( comp, newBin );
54+
}
55+
};
56+
}
57+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.imglib2.imagej;
2+
3+
import ij.process.LUT;
4+
import net.imglib2.display.ColorTable;
5+
import org.junit.Test;
6+
7+
import java.util.Random;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
/**
12+
* Tests utilities in {@link LUTToColorTable}
13+
*
14+
* @author Gabriel Selzer
15+
*/
16+
public class LUTToColorTableTest {
17+
18+
@Test
19+
public void testWrapLUT() {
20+
Random r = new Random(0xdeadbeefL);
21+
byte[] reds = new byte[256];
22+
r.nextBytes(reds);
23+
byte[] blues = new byte[256];
24+
r.nextBytes(blues);
25+
byte[] greens = new byte[256];
26+
r.nextBytes(greens);
27+
LUT lut = new LUT(reds, greens, blues);
28+
ColorTable actual = LUTToColorTable.wrap(lut);
29+
assertEquals(256, actual.getLength());
30+
assertEquals(4, actual.getComponentCount());
31+
for(int i = 0; i < actual.getLength(); i++) {
32+
// Note ColorTable.get unsigned bytes as ints
33+
assertEquals(reds[i], (byte) actual.get(ColorTable.RED, i));
34+
assertEquals(greens[i], (byte) actual.get(ColorTable.GREEN, i));
35+
assertEquals(blues[i], (byte) actual.get(ColorTable.BLUE, i));
36+
assertEquals((byte) 255, (byte) actual.get(ColorTable.ALPHA, i));
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)