Skip to content

Commit

Permalink
Merge pull request #29 from chris-allan/fix-uint16-padding
Browse files Browse the repository at this point in the history
Fix issues with uint16 tile padding on the East and South edges
  • Loading branch information
chris-allan authored May 20, 2020
2 parents f26b41d + 1e4dc7d commit edc3619
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ private void convertPyramid(PyramidSeries s)
}

int rgbChannels = s.rgb ? s.c : 1;

int bytesPerPixel = FormatTools.getBytesPerPixel(s.pixelType);
try {
for (int resolution=0; resolution<s.numberOfResolutions; resolution++) {
LOG.info("Converting resolution #{}", resolution);
Expand Down Expand Up @@ -718,12 +718,14 @@ private void convertPyramid(PyramidSeries s)
else {
// padded tile, use descriptor X and Y tile size
byte[] realTile =
new byte[descriptor.tileSizeX * descriptor.tileSizeY];
new byte[descriptor.tileSizeX * descriptor.tileSizeY
* bytesPerPixel];
int totalRows = region.height;
int inRowLen = tileBytes.length / totalRows;
int inRowLen = region.width * bytesPerPixel;
int outRowLen = descriptor.tileSizeX * bytesPerPixel;
for (int row=0; row<totalRows; row++) {
System.arraycopy(tileBytes, row * inRowLen,
realTile, row * descriptor.tileSizeX, inRowLen);
realTile, row * outRowLen, inRowLen);
}
writeTile(s, currentPlane, realTile,
currentIndex, currentResolution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -21,6 +23,8 @@
import com.glencoesoftware.pyramid.PyramidFromDirectoryWriter;

import loci.formats.ImageReader;
import loci.formats.tiff.IFDList;
import loci.formats.tiff.TiffParser;
import picocli.CommandLine;

import org.junit.Assert;
Expand Down Expand Up @@ -190,4 +194,72 @@ public void testDefaults() throws Exception {
}
}

/**
* Test South and East edge padding.
*/
@Test
public void testSouthEastEdgePadding() throws Exception {
input = fake();
assertBioFormats2Raw("-w", "240", "-h", "240");
assertTool("--compression", "raw");
try (ImageReader reader = new ImageReader()) {
reader.setFlattenedResolutions(false);
reader.setId(outputOmeTiff.toString());
Assert.assertEquals(2, reader.getResolutionCount());
Assert.assertEquals(240, reader.getOptimalTileWidth());
Assert.assertEquals(240, reader.getOptimalTileHeight());
ByteBuffer plane = ByteBuffer.wrap(reader.openBytes(0));
Assert.assertEquals(512 * 512, plane.capacity());
int offset = 0;
for (int y = 0; y < reader.getSizeY(); y++) {
offset = (y * 512) + 511;
Assert.assertEquals(255, Byte.toUnsignedInt(plane.get(offset)));
}
}
try (TiffParser tiffParser = new TiffParser(outputOmeTiff.toString())) {
IFDList mainIFDs = tiffParser.getMainIFDs();
Assert.assertEquals(1, mainIFDs.size());
int tileSize = 240 * 240;
Assert.assertArrayEquals(new long[] {
tileSize, tileSize, tileSize, // Row 1
tileSize, tileSize, tileSize, // Row 2
tileSize, tileSize, tileSize // Row 3
}, mainIFDs.get(0).getStripByteCounts());
}
}

/**
* Test edge padding uint16.
*/
@Test
public void testEdgePaddingUint16() throws Exception {
input = fake("pixelType", "uint16");
assertBioFormats2Raw("-w", "240", "-h", "240");
assertTool("--compression", "raw");
try (ImageReader reader = new ImageReader()) {
reader.setFlattenedResolutions(false);
reader.setId(outputOmeTiff.toString());
Assert.assertEquals(2, reader.getResolutionCount());
Assert.assertEquals(240, reader.getOptimalTileWidth());
Assert.assertEquals(240, reader.getOptimalTileHeight());
ShortBuffer plane = ByteBuffer.wrap(reader.openBytes(0)).asShortBuffer();
Assert.assertEquals(512 * 512, plane.capacity());
int offset = 0;
for (int y = 0; y < reader.getSizeY(); y++) {
offset = (y * 512) + 511;
Assert.assertEquals(511, Short.toUnsignedInt(plane.get(offset)));
}
}
try (TiffParser tiffParser = new TiffParser(outputOmeTiff.toString())) {
IFDList mainIFDs = tiffParser.getMainIFDs();
Assert.assertEquals(1, mainIFDs.size());
int tileSize = 240 * 240 * 2;
Assert.assertArrayEquals(new long[] {
tileSize, tileSize, tileSize, // Row 1
tileSize, tileSize, tileSize, // Row 2
tileSize, tileSize, tileSize // Row 3
}, mainIFDs.get(0).getStripByteCounts());
}
}

}

0 comments on commit edc3619

Please sign in to comment.