Search in sources :

Example 46 with TiffParser

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

the class XMLValidate method main.

public static void main(String[] args) throws Exception {
    CommandLineTools.runUpgradeCheck(args);
    if (args.length == 0) {
        // read from stdin
        process("<stdin>", new BufferedReader(new InputStreamReader(System.in, Constants.ENCODING)));
    } else {
        // read from file(s)
        for (int i = 0; i < args.length; i++) {
            if (args[i].toLowerCase().endsWith("tif") || args[i].toLowerCase().endsWith("tiff")) {
                String comment = new TiffParser(args[i]).getComment();
                process(args[i], new BufferedReader(new StringReader(comment)));
            } else {
                process(args[i], new BufferedReader(new InputStreamReader(new FileInputStream(args[i]), Constants.ENCODING)));
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) TiffParser(loci.formats.tiff.TiffParser) FileInputStream(java.io.FileInputStream)

Example 47 with TiffParser

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

the class FlowSightReader method isThisType.

/* (non-Javadoc)
   * @see loci.formats.FormatReader#isThisType(loci.common.RandomAccessInputStream)
   */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
    TiffParser tiffParser;
    try {
        tiffParser = new TiffParser(stream);
    } catch (java.lang.IllegalArgumentException iae) {
        return false;
    }
    if (!tiffParser.isValidHeader())
        return false;
    IFD ifd = tiffParser.getFirstIFD();
    if (ifd == null)
        return false;
    tiffParser.fillInIFD(ifd);
    for (int tag : MINIMAL_TAGS) {
        try {
            if (ifd.getIFDStringValue(tag) == null)
                return false;
        } catch (FormatException e) {
            return false;
        }
    }
    return true;
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) FormatException(loci.formats.FormatException)

Example 48 with TiffParser

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

the class OMETiffReader method openBytes.

/*
   * @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
   */
@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
    int series = getSeries();
    lastPlane = no;
    int i = info[series][no].ifd;
    if (!info[series][no].exists) {
        Arrays.fill(buf, (byte) 0);
        return buf;
    }
    MinimalTiffReader r = (MinimalTiffReader) info[series][no].reader;
    if (r.getCurrentFile() == null) {
        r.setId(info[series][no].id);
    }
    r.lastPlane = i;
    IFDList ifdList = r.getIFDs();
    if (i >= ifdList.size()) {
        LOGGER.warn("Error untangling IFDs; the OME-TIFF file may be malformed (IFD #{} missing).", i);
        return buf;
    }
    IFD ifd = ifdList.get(i);
    RandomAccessInputStream s = new RandomAccessInputStream(info[series][no].id, 16);
    TiffParser p = new TiffParser(s);
    p.getSamples(ifd, buf, x, y, w, h);
    s.close();
    // lower-right-most tile from a single plane file has been read
    if (r.getImageCount() == 1 && w + x == getSizeX() && h + y == getSizeY()) {
        r.close();
    }
    return buf;
}
Also used : IFD(loci.formats.tiff.IFD) IFDList(loci.formats.tiff.IFDList) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 49 with TiffParser

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

the class OMETiffReader method isSingleFile.

// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isSingleFile(String) */
@Override
public boolean isSingleFile(String id) throws FormatException, IOException {
    // companion files in a binary-only dataset should always have additional files
    if (checkSuffix(id, "companion.ome")) {
        return false;
    }
    // parse and populate OME-XML metadata
    String fileName = new Location(id).getAbsoluteFile().getAbsolutePath();
    RandomAccessInputStream ras = new RandomAccessInputStream(fileName, 16);
    TiffParser tp = new TiffParser(ras);
    IFD ifd = tp.getFirstIFD();
    long[] ifdOffsets = tp.getIFDOffsets();
    ras.close();
    String xml = ifd.getComment();
    if (service == null)
        setupService();
    OMEXMLMetadata meta;
    try {
        meta = service.createOMEXMLMetadata(xml);
        metaFile = new Location(id).getAbsolutePath();
    } catch (ServiceException se) {
        throw new FormatException(se);
    }
    if (meta.getRoot() == null) {
        throw new FormatException("Could not parse OME-XML from TIFF comment");
    }
    int nImages = 0;
    for (int i = 0; i < meta.getImageCount(); i++) {
        int nChannels = meta.getChannelCount(i);
        if (nChannels == 0)
            nChannels = 1;
        int z = meta.getPixelsSizeZ(i).getValue().intValue();
        int t = meta.getPixelsSizeT(i).getValue().intValue();
        nImages += z * t * nChannels;
    }
    return nImages > 0 && nImages <= ifdOffsets.length;
}
Also used : ServiceException(loci.common.services.ServiceException) IFD(loci.formats.tiff.IFD) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) FormatException(loci.formats.FormatException) Location(loci.common.Location)

Example 50 with TiffParser

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

the class TiffComment method main.

public static void main(String[] args) throws FormatException, IOException {
    CommandLineTools.runUpgradeCheck(args);
    if (args.length == 0) {
        System.out.println("Usage:");
        System.out.println("tiffcomment [-set comment] [-edit] file1 [file2 ...]");
        System.out.println();
        System.out.println("This tool requires an ImageDescription tag to be " + "present in the TIFF file. ");
        System.out.println();
        System.out.println("If using the '-set' option, the new TIFF comment " + "must be specified and may take any of the following forms:");
        System.out.println();
        System.out.println("  * the text of the comment, e.g. 'new comment!'");
        System.out.println("  * the name of the file containing the text of " + "the comment, e.g. 'file.xml'");
        System.out.println("  * '-', to enter the comment using stdin.  " + "Entering a blank line will");
        System.out.println("    terminate reading from stdin.");
        return;
    }
    // parse flags
    boolean edit = false;
    String newComment = null;
    ArrayList<String> files = new ArrayList<String>();
    for (int i = 0; i < args.length; i++) {
        if (!args[i].startsWith("-")) {
            files.add(args[i]);
            continue;
        }
        if (args[i].equals("-edit"))
            edit = true;
        else if (args[i].equals("-set")) {
            newComment = args[++i];
            if (new File(newComment).exists()) {
                newComment = DataTools.readFile(newComment);
            } else if (newComment.equals("-")) {
                newComment = null;
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Constants.ENCODING));
                String line = reader.readLine();
                while (line != null && line.length() > 0) {
                    if (newComment == null)
                        newComment = line;
                    else {
                        newComment += "\n" + line;
                    }
                    line = reader.readLine();
                }
            }
        } else
            System.out.println("Warning: unknown flag: " + args[i]);
    }
    // process files
    for (String file : files) {
        if (edit)
            EditTiffG.openFile(file);
        else if (newComment != null) {
            overwriteComment(file, newComment);
        } else {
            String comment = new TiffParser(file).getComment();
            System.out.println(comment == null ? file + ": no TIFF comment found." : comment);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) TiffParser(loci.formats.tiff.TiffParser) File(java.io.File)

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