<html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><META name="Author" content="Novell GroupWise WebAccess"></head><body style='font-family: Helvetica, Arial, sans-serif; font-size: 13px; '>Hello all,<br><br>I am using the Bio-Formats Java library (trunk build) to write an OME-TIFF sequentially strip per strip.<br>It is basically the same approach I mentioned in a previous post (http://lists.openmicroscopy.org.uk/pipermail/ome-users/2012-August/003178.html),<br>but now I am reading the JPEG in stripes and store them strip per strip in an OME-TIFF. It works great for striped uncompressed OME-TIFFs.<br><br>However, when using LZW compression, it nearly ever results in an OME-TIFF which cannot be opened.<br>To locate the error I analyzed the IFD of the TIFF and observed that the tag 'StripByteCounts' doesn’t contain valid values.<br>In the case when there are just a few stripes in the OME-TIFF, the values of 'StripByteCounts' are too large, even exceeding the number of bytes when the strip would be uncompressed.<br>In the case when there are more stripes in the OME-TIFF, the values of 'StripByteCounts' are zero.<br>So I had a look at the IFD-class in the loci.formats.tiff package an encountered the following lines in the getStripByteCounts() method:<br><br>    if (getCompression() == TiffCompression.LZW) {<br>      for (int i=0; i<byteCounts.length; i++) {<br>        counts[i] = byteCounts[i] * 2;<br>      }<br>    }<br><br>I removed the multiplication (* 2) and it seems to solve the error with the wrong values of 'StripByteCounts'.<br>The OME-TIFFs can be opened now (even containing many stripes).<br>The multiplication increases the values of 'StripByteCounts' every time a strip gets stored in the OME-TIFF.<br>This would explain the very large numbers and, when more strips are added, the zeros because of an overflow.<br><br>Below, I added an example-code (TiffStripWriter.java) to reproduce this error.<br>Example settings:<br>TIFF_WIDTH = 2000, TIFF_HEIGHT = 800, STRIP_WIDTH=2000, STRIP_HEIGHT = 80 (= values of 'StripByteCounts' are very large)<br>TIFF_WIDTH = 2000, TIFF_HEIGHT = 800, STRIP_WIDTH=2000, STRIP_HEIGHT = 8 (= values of 'StripByteCounts' are zero)<br><br><br>Thanks in advance for having a look at it!<br><br>Regards,<br>Matthias<br><br><br><br>------------------------------------------------<br>TiffStripWriter.java<br>------------------------------------------------<br>import loci.common.services.ServiceFactory;<br>import loci.formats.FormatTools;<br>import loci.formats.MetadataTools;<br>import loci.formats.meta.IMetadata;<br>import loci.formats.out.OMETiffWriter;<br>import loci.formats.out.TiffWriter;<br>import loci.formats.services.OMEXMLService;<br>import loci.formats.tiff.IFD;<br><br>public class TiffStripWriter {<br><br>    static final int PIXEL_TYPE = FormatTools.UINT8;<br>    static final int COMPONENTS = 3;<br>    static final int TIFF_WIDTH = 2000;<br>    static final int TIFF_HEIGHT = 800;<br>    static final int STRIP_WIDTH = 2000;<br>    static final int STRIP_HEIGHT = 80;<br><br>    public static void main(String[] args) throws Exception {<br>        if (args.length < 1) {<br>            System.out.println("Please specify an output file name.");<br>            System.exit(1);<br>        }<br>        String id = args[0];<br><br>        // create metadata object with minimum required metadata fields<br>        ServiceFactory factory = new ServiceFactory();<br>        OMEXMLService service = factory.getInstance(OMEXMLService.class);<br>        IMetadata meta = service.createOMEXMLMetadata();<br>        MetadataTools.populateMetadata(meta, 0, null, false, "XYZCT", FormatTools.getPixelTypeString(PIXEL_TYPE), TIFF_WIDTH, TIFF_HEIGHT, 1, COMPONENTS, 1, COMPONENTS);<br><br>        // set up the writer for OME-TIFF<br>        TiffWriter writer = new OMETiffWriter();<br>        writer.setMetadataRetrieve(meta);<br>        writer.setInterleaved(Boolean.TRUE);<br>        writer.setBigTiff(false);<br>        writer.setId(id);<br>        writer.setCompression(TiffWriter.COMPRESSION_LZW);<br><br>        // set up IFD for creating a striped OME-TIFF<br>        IFD ifd = new IFD();<br>        ifd.put(IFD.ROWS_PER_STRIP, STRIP_HEIGHT);<br><br>        // create blank image<br>        byte[] img = new byte[STRIP_WIDTH * STRIP_HEIGHT * COMPONENTS * FormatTools.getBytesPerPixel(PIXEL_TYPE)];<br><br>        // write OME-TIFF<br>        for (int y = 0; y < (TIFF_HEIGHT / STRIP_HEIGHT); y++) {<br>            // create random image strip<br>            for (int i = 0; i < img.length; i++)<br>                img[i] = (byte) (256 * Math.random());<br><br>            // write strip<br>            writer.saveBytes(0, img, ifd, 0, y * STRIP_HEIGHT, STRIP_WIDTH, STRIP_HEIGHT);<br>        }<br><br>        writer.close();<br>    }<br><br>}<br></body></html>