Skip to content

Commit 08bb380

Browse files
committed
Add utilities for converting Color Tables to LUTs
1 parent 3ecd816 commit 08bb380

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.imglib2.imagej;
2+
3+
import ij.process.LUT;
4+
import net.imglib2.display.ColorTable;
5+
import net.imglib2.util.Binning;
6+
7+
/**
8+
* Provides convenience functions to convert ImgLib2 {@link ColorTable}s into
9+
* ImageJ {@link LUT} objects.
10+
*
11+
*
12+
* @author Gabriel Selzer
13+
*/
14+
public class ColorTableToLUT {
15+
16+
/**
17+
* Copies the data from {@code table} into a new {@link LUT}
18+
* @param table the {@link ColorTable} to convert
19+
* @return a {@link LUT} containing the same color mapping as {@code table}
20+
*/
21+
public static LUT convert(final ColorTable table) {
22+
byte[][] data = new byte[3][256];
23+
for (int bin = 0; bin < 256; bin++) {
24+
data[ColorTable.RED][bin] = (byte) table.get(ColorTable.RED, bin);
25+
data[ColorTable.GREEN][bin] = (byte) table.get(ColorTable.GREEN, bin);
26+
data[ColorTable.BLUE][bin] = (byte) table.get(ColorTable.BLUE, bin);
27+
}
28+
return new LUT(data[ColorTable.RED], data[ColorTable.GREEN], data[ColorTable.BLUE]);
29+
}
30+
}

src/main/java/net/imglib2/imagej/LUTToColorTable.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
* {@link net.imglib2.display.ColorTable}s.
1212
*
1313
*
14-
* @author Stephan Preibisch
15-
* @author Stephan Saalfeld
16-
* @author Matthias Arzt
1714
* @author Gabriel Selzer
1815
*/
1916
public class LUTToColorTable {
2017

18+
/**
19+
* Wraps (i.e. copyless) {@code lut} into a {@link ColorTable}.
20+
* @param lut the {@link LUT} to convert
21+
* @return a {@link ColorTable} enclosing {@code lut}
22+
*/
2123
public static ColorTable wrap(final LUT lut) {
2224
return new ColorTable() {
2325
@Override
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.imglib2.imagej;
2+
3+
import ij.process.LUT;
4+
import net.imglib2.display.ColorTable;
5+
import net.imglib2.display.ColorTable8;
6+
import org.junit.Test;
7+
8+
import java.util.Random;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class ColorTableToLUTTest {
13+
14+
@Test
15+
public void testConvertColorTable() {
16+
Random r = new Random(0xdeadbeefL);
17+
byte[] reds = new byte[256];
18+
r.nextBytes(reds);
19+
byte[] greens = new byte[256];
20+
r.nextBytes(greens);
21+
byte[] blues = new byte[256];
22+
r.nextBytes(blues);
23+
ColorTable table = new ColorTable8(reds, greens, blues);
24+
LUT actual = ColorTableToLUT.convert(table);
25+
for(int i = 0; i < 256; i++) {
26+
// Note ColorTable.get unsigned bytes as ints
27+
assertEquals(reds[i], (byte) actual.getRed(i));
28+
assertEquals(greens[i], (byte) actual.getGreen(i));
29+
assertEquals(blues[i], (byte) actual.getBlue(i));
30+
assertEquals((byte) 255, (byte) actual.getAlpha(i));
31+
}
32+
}
33+
}

src/test/java/net/imglib2/imagej/LUTToColorTableTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public void testWrapLUT() {
2020
Random r = new Random(0xdeadbeefL);
2121
byte[] reds = new byte[256];
2222
r.nextBytes(reds);
23-
byte[] blues = new byte[256];
24-
r.nextBytes(blues);
2523
byte[] greens = new byte[256];
2624
r.nextBytes(greens);
25+
byte[] blues = new byte[256];
26+
r.nextBytes(blues);
2727
LUT lut = new LUT(reds, greens, blues);
2828
ColorTable actual = LUTToColorTable.wrap(lut);
2929
assertEquals(256, actual.getLength());

0 commit comments

Comments
 (0)