-
Notifications
You must be signed in to change notification settings - Fork 0
ImageJ LUT <-> ImgLib2 ColorTable conversion #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+166
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ecd816
Add utilities for converting LUTs to ColorTables
gselzer 08bb380
Add utilities for converting Color Tables to LUTs
gselzer 24730b9
Remove unused imports
gselzer 026e17a
Check ColorTable length before converting
gselzer 8acaca8
Add test class javadoc
gselzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package net.imglib2.imagej; | ||
|
|
||
| import ij.process.LUT; | ||
| import net.imglib2.display.ColorTable; | ||
|
|
||
| /** | ||
| * Provides convenience functions to convert ImgLib2 {@link ColorTable}s into | ||
| * ImageJ {@link LUT} objects. | ||
| * | ||
| * | ||
| * @author Gabriel Selzer | ||
| */ | ||
| public class ColorTableToLUT { | ||
|
|
||
| /** | ||
| * Copies the data from {@code table} into a new {@link LUT} | ||
| * @param table the {@link ColorTable} to convert | ||
| * @return a {@link LUT} containing the same color mapping as {@code table} | ||
| */ | ||
| public static LUT convert(final ColorTable table) { | ||
| if (table.getLength() != 256) { | ||
| throw new IllegalArgumentException("Can only convert ColorTables of length 256 to LUTs"); | ||
| } | ||
| byte[][] data = new byte[3][256]; | ||
| for (int bin = 0; bin < 256; bin++) { | ||
| data[ColorTable.RED][bin] = (byte) table.get(ColorTable.RED, bin); | ||
| data[ColorTable.GREEN][bin] = (byte) table.get(ColorTable.GREEN, bin); | ||
| data[ColorTable.BLUE][bin] = (byte) table.get(ColorTable.BLUE, bin); | ||
| } | ||
| return new LUT(data[ColorTable.RED], data[ColorTable.GREEN], data[ColorTable.BLUE]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package net.imglib2.imagej; | ||
|
|
||
| import ij.process.LUT; | ||
| import net.imglib2.display.ColorTable; | ||
| import net.imglib2.util.Binning; | ||
|
|
||
| /** | ||
| * Provides convenience functions to wrap ImageJ {@link ij.process.LUT} objects into ImgLib2 | ||
| * {@link net.imglib2.display.ColorTable}s. | ||
| * | ||
| * | ||
| * @author Gabriel Selzer | ||
| */ | ||
| public class LUTToColorTable { | ||
|
|
||
| /** | ||
| * Wraps (i.e. copyless) {@code lut} into a {@link ColorTable}. | ||
| * @param lut the {@link LUT} to convert | ||
| * @return a {@link ColorTable} enclosing {@code lut} | ||
| */ | ||
| public static ColorTable wrap(final LUT lut) { | ||
| return new ColorTable() { | ||
| @Override | ||
| public int lookupARGB(double min, double max, double value) { | ||
| final int bins = getLength(); | ||
| final int bin = Binning.valueToBin( bins, min, max, value ); | ||
| return lut.getRGB( bin ); | ||
| } | ||
|
|
||
| @Override | ||
| public int getComponentCount() { | ||
| return 4; | ||
| } | ||
|
|
||
| @Override | ||
| public int getLength() { | ||
| return 256; | ||
| } | ||
|
|
||
| @Override | ||
| public int get(int comp, int bin) { | ||
| int rgb = lut.getRGB(bin); | ||
| int shift = comp == ColorTable.RED ? 16 : | ||
| comp == ColorTable.GREEN ? 8 : | ||
| comp == ColorTable.BLUE ? 0 : | ||
| 24; | ||
| return (rgb >> shift) & 0xff; | ||
| } | ||
|
|
||
| @Override | ||
| public int getResampled(int comp, int bins, int bin) { | ||
| final int newBin = ( int ) ( ( long ) getLength() * bin / bins ); | ||
| return get( comp, newBin ); | ||
| } | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package net.imglib2.imagej; | ||
|
|
||
| import ij.process.LUT; | ||
| import net.imglib2.display.ColorTable; | ||
| import net.imglib2.display.ColorTable8; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| /** | ||
| * Tests conversions from {@link ColorTable}s to ImageJ {@link LUT}s. | ||
| * | ||
| * @author Gabriel Selzer | ||
| */ | ||
| public class ColorTableToLUTTest { | ||
|
|
||
| @Test | ||
| public void testConvertColorTable() { | ||
| Random r = new Random(0xdeadbeefL); | ||
| byte[] reds = new byte[256]; | ||
| r.nextBytes(reds); | ||
| byte[] greens = new byte[256]; | ||
| r.nextBytes(greens); | ||
| byte[] blues = new byte[256]; | ||
| r.nextBytes(blues); | ||
| ColorTable table = new ColorTable8(reds, greens, blues); | ||
| LUT actual = ColorTableToLUT.convert(table); | ||
| for(int i = 0; i < 256; i++) { | ||
| // Note ColorTable.get unsigned bytes as ints | ||
| assertEquals(reds[i], (byte) actual.getRed(i)); | ||
| assertEquals(greens[i], (byte) actual.getGreen(i)); | ||
| assertEquals(blues[i], (byte) actual.getBlue(i)); | ||
| assertEquals((byte) 255, (byte) actual.getAlpha(i)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package net.imglib2.imagej; | ||
|
|
||
| import ij.process.LUT; | ||
| import net.imglib2.display.ColorTable; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| /** | ||
| * Tests utilities in {@link LUTToColorTable} | ||
| * | ||
| * @author Gabriel Selzer | ||
| */ | ||
| public class LUTToColorTableTest { | ||
|
|
||
| @Test | ||
| public void testWrapLUT() { | ||
| Random r = new Random(0xdeadbeefL); | ||
| byte[] reds = new byte[256]; | ||
| r.nextBytes(reds); | ||
| byte[] greens = new byte[256]; | ||
| r.nextBytes(greens); | ||
| byte[] blues = new byte[256]; | ||
| r.nextBytes(blues); | ||
| LUT lut = new LUT(reds, greens, blues); | ||
| ColorTable actual = LUTToColorTable.wrap(lut); | ||
| assertEquals(256, actual.getLength()); | ||
| assertEquals(4, actual.getComponentCount()); | ||
| for(int i = 0; i < actual.getLength(); i++) { | ||
| // Note ColorTable.get unsigned bytes as ints | ||
| assertEquals(reds[i], (byte) actual.get(ColorTable.RED, i)); | ||
| assertEquals(greens[i], (byte) actual.get(ColorTable.GREEN, i)); | ||
| assertEquals(blues[i], (byte) actual.get(ColorTable.BLUE, i)); | ||
| assertEquals((byte) 255, (byte) actual.get(ColorTable.ALPHA, i)); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.