Skip to content

Instantly share code, notes, and snippets.

@sbesson
Forked from emilroz/TiledWriter.java
Last active January 23, 2017 13:51
Show Gist options
  • Save sbesson/7ccf71072ae028b214dd9448a2b131da to your computer and use it in GitHub Desktop.
Save sbesson/7ccf71072ae028b214dd9448a2b131da to your computer and use it in GitHub Desktop.
Basic ome.tif tile writer
import java.io.IOException;
import loci.common.services.DependencyException;
import loci.common.services.ServiceException;
import loci.common.services.ServiceFactory;
import loci.formats.services.OMEXMLService;
import loci.formats.FormatException;
import loci.formats.FormatTools;
import loci.formats.meta.IMetadata;
import loci.formats.MetadataTools;
import loci.formats.in.FakeReader;
import loci.formats.out.OMETiffWriter;
public class TiledWriter {
public static void writeImage(Integer sizeX, Integer sizeY, boolean bigTiff, boolean writeSequentially, String pixelType)
throws DependencyException, ServiceException,
FormatException, IOException
{
Integer sizeC = 4;
Integer tileSizeX = 256;
Integer tileSizeY = 256;
Integer numberOfTilesX = sizeX / tileSizeX;
Integer numberOfTilesY = sizeY / tileSizeY;
String pref = "/tmp/";
String imageName = "test";
String fileName = imageName + "_tiled_" + sizeX + "_" + sizeY + "_" + bigTiff + "_" + writeSequentially + "_" + pixelType + ".ome.tif";
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
IMetadata meta = service.createOMEXMLMetadata();
MetadataTools.populateMetadata(
meta, 0, null, true, "XYZCT", pixelType,
sizeX, sizeY, 1, sizeC, 1, 1);
OMETiffWriter writer = new OMETiffWriter();
FakeReader reader = new FakeReader();
writer.setMetadataRetrieve(meta);
writer.setTileSizeX(tileSizeX);
writer.setTileSizeY(tileSizeY);
writer.setBigTiff(bigTiff);
writer.setWriteSequentially(writeSequentially);
writer.setId(pref + fileName);
reader.setId("tiles&sizeX=" + sizeX + "&sizeY=" + sizeY + "&sizeC=" + sizeC + "&pixelType=" + pixelType + ".fake");
byte[] byteBuffer;
for (int image=0; image<reader.getImageCount(); image++) {
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;
byteBuffer = reader.openBytes(image, x, y, writeSizeX, writeSizeY);
writer.saveBytes(image, byteBuffer, x, y, writeSizeX, writeSizeY);
}
}
}
writer.close();
reader.close();
}
public static void main(String[] args)
throws DependencyException, ServiceException,
FormatException, IOException
{
writeImage(1200, 1024, true, true, "uint8");
writeImage(1200, 1024, true, false, "uint8");
writeImage(1200, 1024, false, true, "uint8");
writeImage(1200, 1024, false, false, "uint8");
writeImage(1200, 1024, true, true, "uint16");
writeImage(1200, 1024, true, false, "uint16");
writeImage(1200, 1024, false, true, "uint16");
writeImage(1200, 1024, false, false, "uint16");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment