/* * #%L * OME Bio-Formats package for reading and converting biological file formats. * %% * Copyright (C) 2005 - 2016 Open Microscopy Environment: * - Board of Regents of the University of Wisconsin-Madison * - Glencoe Software, Inc. * - University of Dundee * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * . * #L% */ import java.io.IOException; import loci.common.services.ServiceFactory; import loci.formats.FormatException; import loci.formats.ImageReader; import loci.formats.FormatTools; import loci.formats.meta.IMetadata; import loci.formats.out.OMETiffWriter; import loci.formats.services.OMEXMLService; /** * Example class for reading and writing a file in a tiled OME Tiff format. * * @author David Gault dgault at dundee.ac.uk */ public class TiledReaderWriter { /** The file format reader. */ private ImageReader reader; /** The file format writer. */ private OMETiffWriter writer; /** The file to be read. */ private String inputFile; /** The file to be written. */ private String outputFile; /** The tile width to be used. */ private int tileSizeX; /** The tile height to be used. */ private int tileSizeY; /** * Construct a new TiledReaderWriter to read the specified input file * and write the given output file using the tile sizes provided. * * @param inputFile the file to be read * @param outputFile the file to be written * @param tileSizeX the width of tile to attempt to use * @param tileSizeY the height of tile to attempt to use */ public TiledReaderWriter(String inputFile, String outputFile, int tileX, int tileY) { this.inputFile = inputFile; this.outputFile = outputFile; this.tileSizeX = tileSizeX; this.tileSizeY = tileSizeY; } /** * Set up the file reader and writer, ensuring that the input file is * associated with the reader and the output file is associated with the * writer. * * @return true if the reader and writer were successfully set up, or false * if an error occurred */ private boolean initialize() { Exception exception = null; try { // construct the object that stores OME-XML metadata ServiceFactory factory = new ServiceFactory(); OMEXMLService service = factory.getInstance(OMEXMLService.class); IMetadata omexml = service.createOMEXMLMetadata(); // set up the reader and associate it with the input file reader = new ImageReader(); reader.setMetadataStore(omexml); reader.setId(inputFile); // set up the writer and associate it with the output file writer = new OMETiffWriter(); writer.setMetadataRetrieve(omexml); writer.setInterleaved(reader.isInterleaved()); // set the tile size height and width for writing this.tileSizeX = writer.setTileSizeX(tileSizeX); this.tileSizeY = writer.setTileSizeY(tileSizeY); writer.setId(outputFile); } catch (Exception e) { exception = e; System.err.println("Failed to initialize files."); exception.printStackTrace(); } return exception == null; } /** Read tiles from input file and write tiles to output OME Tiff. * @throws IOException * @throws FormatException */ public void readWriteTiles() throws FormatException, IOException { int bpp = FormatTools.getBytesPerPixel(reader.getPixelType()); int tilePlaneSize = tileSizeX * tileSizeY * reader.getRGBChannelCount() * bpp; byte[] buf = new byte[tilePlaneSize]; for (int series=0; series