Skip to content

Commit

Permalink
Merge pull request glencoesoftware#31 from chris-allan/f32-f64-pix
Browse files Browse the repository at this point in the history
Add support for float and double pixel types
  • Loading branch information
chris-allan authored Apr 22, 2020
2 parents 0fd59e3 + 4f8bac0 commit 9968023
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
4 changes: 3 additions & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@

<!-- Checks for blocks. You know, those {}'s -->
<!-- See https://checkstyle.org/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="AvoidNestedBlocks">
<property name="allowInSwitchCase" value="true"/>
</module>
<module name="EmptyBlock"/>
<module name="LeftCurly">
<property name="option" value="nlow"/>
Expand Down
43 changes: 33 additions & 10 deletions src/main/java/com/glencoesoftware/bioformats2raw/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import org.janelia.saalfeldlab.n5.DataBlock;
import org.janelia.saalfeldlab.n5.DataType;
import org.janelia.saalfeldlab.n5.DatasetAttributes;
import org.janelia.saalfeldlab.n5.DoubleArrayDataBlock;
import org.janelia.saalfeldlab.n5.FloatArrayDataBlock;
import org.janelia.saalfeldlab.n5.GzipCompression;
import org.janelia.saalfeldlab.n5.Lz4Compression;
import org.janelia.saalfeldlab.n5.N5FSReader;
Expand Down Expand Up @@ -713,23 +715,38 @@ private void processTile(
return;
}

int bytesPerPixel = FormatTools.getBytesPerPixel(pixelType);
switch (bytesPerPixel) {
case 1:
ByteBuffer bb = ByteBuffer.wrap(tile);
if (resolution == 0 && isLittleEndian) {
bb = bb.order(ByteOrder.LITTLE_ENDIAN);
}
switch (pixelType) {
case FormatTools.INT8:
case FormatTools.UINT8: {
dataBlock = new ByteArrayDataBlock(size, gridPosition, tile);
break;
case 2:
}
case FormatTools.INT16:
case FormatTools.UINT16: {
short[] asShort = new short[tile.length / 2];
ByteBuffer bb = ByteBuffer.wrap(tile);
if (resolution == 0 && isLittleEndian) {
bb = bb.order(ByteOrder.LITTLE_ENDIAN);
}
bb.asShortBuffer().get(asShort);
dataBlock = new ShortArrayDataBlock(size, gridPosition, asShort);
break;
}
case FormatTools.FLOAT: {
float[] asFloat = new float[tile.length / 4];
bb.asFloatBuffer().get(asFloat);
dataBlock = new FloatArrayDataBlock(size, gridPosition, asFloat);
break;
}
case FormatTools.DOUBLE: {
double[] asDouble = new double[tile.length / 8];
bb.asDoubleBuffer().get(asDouble);
dataBlock = new DoubleArrayDataBlock(size, gridPosition, asDouble);
break;
}
default:
throw new FormatException(
"Unsupported bytes per pixel: " + bytesPerPixel);
throw new FormatException("Unsupported pixel type: "
+ FormatTools.getPixelTypeString(pixelType));
}
}
finally {
Expand Down Expand Up @@ -817,6 +834,12 @@ public void saveResolutions(int series)
case FormatTools.UINT16:
dataType = DataType.UINT16;
break;
case FormatTools.FLOAT:
dataType = DataType.FLOAT32;
break;
case FormatTools.DOUBLE:
dataType = DataType.FLOAT64;
break;
default:
throw new FormatException("Unsupported pixel type: "
+ FormatTools.getPixelTypeString(pixelType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import loci.formats.in.FakeReader;
import picocli.CommandLine;

import org.janelia.saalfeldlab.n5.DataType;
import org.janelia.saalfeldlab.n5.DatasetAttributes;
import org.janelia.saalfeldlab.n5.zarr.N5ZarrReader;
import org.junit.Assert;
Expand Down Expand Up @@ -371,4 +372,50 @@ public void testMultiT() throws Exception {
Assert.assertArrayEquals(new int[] {0, 1, 0, 0, 1}, seriesPlaneNumberZCT);
}

/**
* Test float pixel type.
*/
@Test
public void testFloatPixelType() throws Exception {
input = fake("pixelType", "float");
assertTool();
N5ZarrReader z =
new N5ZarrReader(output.resolve("data.zarr").toString());

// Check series dimensions and special pixels
DatasetAttributes da = z.getDatasetAttributes("/0/0");
Assert.assertEquals(DataType.FLOAT32, da.getDataType());
Assert.assertArrayEquals(
new long[] {512, 512, 1, 1, 1}, da.getDimensions());
Assert.assertArrayEquals(
new int[] {512, 512, 1, 1, 1}, da.getBlockSize());
ByteBuffer tile = z.readBlock("/0/0", da, new long[] {0, 0, 0, 0, 0})
.toByteBuffer();
int[] seriesPlaneNumberZCT = FakeReader.readSpecialPixels(tile.array());
Assert.assertArrayEquals(new int[] {0, 0, 0, 0, 0}, seriesPlaneNumberZCT);
}

/**
* Test double pixel type.
*/
@Test
public void testDoublePixelType() throws Exception {
input = fake("pixelType", "double");
assertTool();
N5ZarrReader z =
new N5ZarrReader(output.resolve("data.zarr").toString());

// Check series dimensions and special pixels
DatasetAttributes da = z.getDatasetAttributes("/0/0");
Assert.assertEquals(DataType.FLOAT64, da.getDataType());
Assert.assertArrayEquals(
new long[] {512, 512, 1, 1, 1}, da.getDimensions());
Assert.assertArrayEquals(
new int[] {512, 512, 1, 1, 1}, da.getBlockSize());
ByteBuffer tile = z.readBlock("/0/0", da, new long[] {0, 0, 0, 0, 0})
.toByteBuffer();
int[] seriesPlaneNumberZCT = FakeReader.readSpecialPixels(tile.array());
Assert.assertArrayEquals(new int[] {0, 0, 0, 0, 0}, seriesPlaneNumberZCT);
}

}

0 comments on commit 9968023

Please sign in to comment.