Search in sources :

Example 96 with TiffParser

use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.

the class SewTiffs method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("Usage: java SewTiffs base_name channel_num [time_count]");
        System.exit(1);
    }
    String base = args[0];
    int c = Integer.parseInt(args[1]);
    int num;
    if (args.length < 3) {
        FilePattern fp = new FilePattern(new Location(base + "_C" + c + "_TP1.tiff"));
        int[] count = fp.getCount();
        num = count[count.length - 1];
    } else
        num = Integer.parseInt(args[2]);
    System.out.println("Fixing " + base + "_C" + c + "_TP<1-" + num + ">.tiff");
    TiffReader in = new TiffReader();
    TiffWriter out = new TiffWriter();
    String outId = base + "_C" + c + ".tiff";
    System.out.println("Writing " + outId);
    out.setId(outId);
    System.out.print("   ");
    boolean comment = false;
    for (int t = 0; t < num; t++) {
        String inId = base + "_C" + c + "_TP" + (t + 1) + ".tiff";
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        IMetadata meta = service.createOMEXMLMetadata();
        in.setMetadataStore(meta);
        in.setId(inId);
        out.setMetadataRetrieve(meta);
        // read first image plane
        byte[] image = in.openBytes(0);
        in.close();
        if (t == 0) {
            // read first IFD
            RandomAccessInputStream ras = new RandomAccessInputStream(inId);
            TiffParser parser = new TiffParser(ras);
            IFD ifd = parser.getFirstIFD();
            ras.close();
            // preserve TIFF comment
            String desc = ifd.getComment();
            if (desc != null) {
                ifd = new IFD();
                ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, desc);
                comment = true;
                out.saveBytes(t, image, ifd);
                System.out.print(".");
                continue;
            }
        }
        // write image plane
        out.saveBytes(t, image);
        // update status
        System.out.print(".");
        if (t % DOTS == DOTS - 1) {
            System.out.println(" " + (t + 1));
            System.out.print("   ");
        }
    }
    System.out.println();
    if (comment)
        System.out.println("OME-TIFF comment saved.");
    else
        System.out.println("No OME-TIFF comment found.");
}
Also used : TiffWriter(loci.formats.out.TiffWriter) ServiceFactory(loci.common.services.ServiceFactory) IFD(loci.formats.tiff.IFD) OMEXMLService(loci.formats.services.OMEXMLService) TiffReader(loci.formats.in.TiffReader) IMetadata(loci.formats.meta.IMetadata) TiffParser(loci.formats.tiff.TiffParser) FilePattern(loci.formats.FilePattern) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Aggregations

TiffParser (loci.formats.tiff.TiffParser)96 IFD (loci.formats.tiff.IFD)74 RandomAccessInputStream (loci.common.RandomAccessInputStream)56 FormatException (loci.formats.FormatException)26 CoreMetadata (loci.formats.CoreMetadata)19 IOException (java.io.IOException)18 IFDList (loci.formats.tiff.IFDList)16 Location (loci.common.Location)15 MetadataStore (loci.formats.meta.MetadataStore)15 ArrayList (java.util.ArrayList)12 Timestamp (ome.xml.model.primitives.Timestamp)9 Length (ome.units.quantity.Length)8 PhotoInterp (loci.formats.tiff.PhotoInterp)6 File (java.io.File)5 HashMap (java.util.HashMap)5 ServiceException (loci.common.services.ServiceException)4 Time (ome.units.quantity.Time)4 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)4 List (java.util.List)3 ByteArrayHandle (loci.common.ByteArrayHandle)3