Skip to content

Instantly share code, notes, and snippets.

@emilroz
Last active January 19, 2017 13:55
Show Gist options
  • Save emilroz/1c894ce866b78cee73e96ea4dd91decf to your computer and use it in GitHub Desktop.
Save emilroz/1c894ce866b78cee73e96ea4dd91decf to your computer and use it in GitHub Desktop.
Basic ome.tif tile writer
public void dummyTiledTiff(
String directory, String imageName)
throws DependencyException, ServiceException,
FormatException, IOException
{
Integer sizeX = 1024;
Integer sizeY = 1024;
Integer sizeC = 4;
Integer bpp = 2;
Integer tileSizeX = 256;
Integer tileSizeY = 256;
Integer numberOfTilesX = sizeX / tileSizeX;
Integer numberOfTilesY = sizeY / tileSizeY;
String pref = directory;
String fileName;
fileName = imageName + "_tiled_test.ome.tif";
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata meta = service.createOMEXMLMetadata();
MetadataTools.populateMetadata(
meta, 0, null, true, "XYZCT",
FormatTools.getPixelTypeString(FormatTools.UINT16),
sizeX, sizeY, 1, sizeC, 1, sizeC);
OMETiffWriter writer = new OMETiffWriter();
writer.setMetadataRetrieve(meta);
writer.setTileSizeX(tileSizeX);
writer.setTileSizeY(tileSizeY);
writer.setBigTiff(true);
writer.setId(pref + fileName);
Integer offsetC = 0;
Integer position = 0;
Integer numberOfBytes;
for (Integer tileY = 0; tileY < numberOfTilesY; tileY++) {
Integer y = tileY * tileSizeY;
for (Integer tileX = 0; tileX < numberOfTilesX; tileX++) {
Integer x = tileX * tileSizeX;
int writeSizeX =
(x + tileSizeX) < sizeX ? tileSizeX : sizeX - x;
int writeSizeY =
(y + tileSizeY) < sizeY ? tileSizeY : sizeY - y;
byte[] byteBuffer = new byte[
sizeC * writeSizeX * writeSizeY * bpp];
numberOfBytes = writeSizeX * writeSizeY * bpp;
for (Integer c = 0; c < sizeC; c++) {
offsetC = c * writeSizeX * writeSizeY * bpp;
for (Integer i = 0; i < numberOfBytes; i++) {
position = i + offsetC;
byteBuffer[position] = (byte) ((offsetC + 1) * 50);
}
}
writer.saveBytes(0, byteBuffer, x, y, writeSizeX, writeSizeY);
}
}
writer.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment