Search in sources :

Example 26 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class DisplayHandler method displayDataBrowser.

public void displayDataBrowser(ImagePlus imp) {
    IFormatReader r = process.getReader();
    int[] subC;
    String[] subCTypes;
    Modulo moduloC = r.getModuloC();
    if (moduloC.length() > 1) {
        subC = new int[] { r.getSizeC() / moduloC.length(), moduloC.length() };
        subCTypes = new String[] { moduloC.parentType, moduloC.type };
    } else {
        subC = new int[] { r.getSizeC() };
        subCTypes = new String[] { FormatTools.CHANNEL };
    }
    new DataBrowser(imp, null, subCTypes, subC, xmlWindow);
}
Also used : DataBrowser(loci.plugins.util.DataBrowser) IFormatReader(loci.formats.IFormatReader) Modulo(loci.formats.Modulo)

Example 27 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class DisplayHandler method displayImage5D.

public void displayImage5D(ImagePlus imp) {
    WindowManager.setTempCurrentImage(imp);
    IFormatReader r = process.getReader();
    ReflectedUniverse ru = new ReflectedUniverse();
    try {
        ru.exec("import i5d.Image5D");
        ru.setVar("title", imp.getTitle());
        ru.setVar("stack", imp.getStack());
        ru.setVar("sizeC", imp.getNChannels());
        ru.setVar("sizeZ", imp.getNSlices());
        ru.setVar("sizeT", imp.getNFrames());
        ru.exec("i5d = new Image5D(title, stack, sizeC, sizeZ, sizeT)");
        ru.setVar("cal", imp.getCalibration());
        ru.setVar("fi", imp.getOriginalFileInfo());
        ru.exec("i5d.setCalibration(cal)");
        ru.exec("i5d.setFileInfo(fi)");
        // ru.exec("i5d.setDimensions(sizeC, sizeZ, sizeT)");
        ru.exec("i5d.show()");
    } catch (ReflectException exc) {
        WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem interfacing with Image5D");
    }
}
Also used : IFormatReader(loci.formats.IFormatReader) ReflectedUniverse(loci.common.ReflectedUniverse) ReflectException(loci.common.ReflectException)

Example 28 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class ImportProcess method createBaseReader.

/**
 * Initializes an {@link loci.formats.IFormatReader}
 * according to the current configuration.
 */
private void createBaseReader() throws FormatException, IOException {
    if (options.isLocal() || options.isHTTP()) {
        BF.status(options.isQuiet(), "Identifying " + idName);
        imageReader = LociPrefs.makeImageReader();
        baseReader = imageReader.getReader(options.isUsingPatternIds() ? new FilePattern(options.getId()).getFiles()[0] : options.getId());
    } else if (options.isOMERO()) {
        BF.status(options.isQuiet(), "Establishing server connection");
        try {
            ReflectedUniverse r = new ReflectedUniverse();
            r.exec("import loci.ome.io.OmeroReader");
            r.exec("baseReader = new OmeroReader()");
            ClassList<IFormatReader> classes = new ClassList<IFormatReader>(IFormatReader.class);
            r.setVar("classes", classes);
            r.exec("class = baseReader.getClass()");
            r.exec("classes.addClass(class)");
            imageReader = new ImageReader(classes);
            baseReader = imageReader.getReader(options.getId());
        } catch (Exception exc) {
            WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem communicating with the server.");
            cancel();
            return;
        }
    } else {
        WindowTools.reportException(null, options.isQuiet(), "Sorry, there has been an internal error: unknown data source");
        cancel();
        return;
    }
    // attach OME-XML metadata store
    Exception exc = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.createOMEXMLMetadata();
        omeXML = null;
    } catch (DependencyException de) {
        exc = de;
    } catch (ServiceException se) {
        exc = se;
    }
    if (exc != null) {
        WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem constructing the OME-XML metadata store");
        throw new FormatException(exc);
    }
    baseReader.setMetadataStore(meta);
    BF.status(options.isQuiet(), "");
    DebugTools.enableIJLogging(IJ.debugMode);
}
Also used : IFormatReader(loci.formats.IFormatReader) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) FilePattern(loci.formats.FilePattern) ReflectedUniverse(loci.common.ReflectedUniverse) ClassList(loci.formats.ClassList) ImageReader(loci.formats.ImageReader) DependencyException(loci.common.services.DependencyException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Example 29 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class OMETiffReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    // normalize file name
    super.initFile(normalizeFilename(null, id));
    id = currentId;
    String dir = new File(id).getParent();
    // parse and populate OME-XML metadata
    String fileName = new Location(id).getAbsoluteFile().getAbsolutePath();
    if (!new File(fileName).exists()) {
        fileName = currentId;
    }
    String xml;
    IFD firstIFD = null;
    boolean companion = false;
    if (checkSuffix(fileName, "companion.ome")) {
        xml = DataTools.readFile(fileName);
        companion = true;
    } else {
        RandomAccessInputStream ras = new RandomAccessInputStream(fileName, 16);
        try {
            TiffParser tp = new TiffParser(ras);
            firstIFD = tp.getFirstIFD();
            xml = firstIFD.getComment();
        } finally {
            ras.close();
        }
    }
    if (service == null)
        setupService();
    try {
        if (meta == null || !metaFile.equals(currentId)) {
            meta = service.createOMEXMLMetadata(xml);
            metaFile = currentId;
        }
        if (companion) {
            String firstTIFF = meta.getUUIDFileName(0, 0);
            initFile(new Location(dir, firstTIFF).getAbsolutePath());
            return;
        }
    } catch (ServiceException se) {
        throw new FormatException(se);
    }
    String metadataPath = null;
    try {
        metadataPath = meta.getBinaryOnlyMetadataFile();
    } catch (NullPointerException e) {
    }
    if (metadataPath != null) {
        // this is a binary-only file
        // overwrite XML with what is in the companion OME-XML file
        Location path = new Location(dir, metadataPath);
        if (path.exists()) {
            metadataFile = path.getAbsolutePath();
            xml = readMetadataFile();
            try {
                meta = service.createOMEXMLMetadata(xml);
            } catch (ServiceException se) {
                throw new FormatException(se);
            } catch (NullPointerException e) {
                metadataFile = null;
                metadataPath = null;
            }
        }
    }
    hasSPW = meta.getPlateCount() > 0;
    for (int i = 0; i < meta.getImageCount(); i++) {
        int sizeC = meta.getPixelsSizeC(i).getValue().intValue();
        service.removeChannels(meta, i, sizeC);
    }
    Hashtable originalMetadata = service.getOriginalMetadata(meta);
    if (originalMetadata != null)
        metadata = originalMetadata;
    LOGGER.trace(xml);
    if (meta.getRoot() == null) {
        throw new FormatException("Could not parse OME-XML from TIFF comment");
    }
    String[] acquiredDates = new String[meta.getImageCount()];
    for (int i = 0; i < acquiredDates.length; i++) {
        Timestamp acquisitionDate = meta.getImageAcquisitionDate(i);
        if (acquisitionDate != null) {
            acquiredDates[i] = acquisitionDate.getValue();
        }
    }
    String currentUUID = meta.getUUID();
    if (!isGroupFiles() && !isSingleFile(currentId)) {
        IFormatReader reader = new MinimalTiffReader();
        reader.setId(currentId);
        core.set(0, reader.getCoreMetadataList().get(0));
        int ifdCount = reader.getImageCount();
        reader.close();
        int maxSeries = 0;
        info = new OMETiffPlane[meta.getImageCount()][];
        ArrayList<Integer> imagesToRemove = new ArrayList<Integer>();
        ArrayList<int[]> cBounds = new ArrayList<int[]>();
        for (int i = 0; i < meta.getImageCount(); i++) {
            int maxZ = 0;
            int maxC = 0;
            int maxT = 0;
            int minZ = Integer.MAX_VALUE;
            int minC = Integer.MAX_VALUE;
            int minT = Integer.MAX_VALUE;
            int sizeZ = meta.getPixelsSizeZ(i).getValue();
            int sizeC = meta.getChannelCount(i);
            int sizeT = meta.getPixelsSizeT(i).getValue();
            String order = meta.getPixelsDimensionOrder(i).getValue();
            int num = sizeZ * sizeC * sizeT;
            CoreMetadata m = i < core.size() ? core.get(i) : new CoreMetadata(core.get(0));
            m.dimensionOrder = order;
            info[i] = new OMETiffPlane[meta.getTiffDataCount(i)];
            int next = 0;
            for (int td = 0; td < meta.getTiffDataCount(i); td++) {
                String uuid = null;
                try {
                    uuid = meta.getUUIDValue(i, td);
                } catch (NullPointerException e) {
                }
                String filename = null;
                try {
                    filename = meta.getUUIDFileName(i, td);
                } catch (NullPointerException e) {
                }
                if ((uuid == null || !uuid.equals(currentUUID)) && (filename == null || !currentId.endsWith(filename))) {
                    // this plane doesn't appear to be in the current file
                    continue;
                }
                if (i > maxSeries) {
                    maxSeries = i;
                }
                NonNegativeInteger ifd = meta.getTiffDataIFD(i, td);
                NonNegativeInteger count = meta.getTiffDataPlaneCount(i, td);
                NonNegativeInteger firstZ = meta.getTiffDataFirstZ(i, td);
                NonNegativeInteger firstC = meta.getTiffDataFirstC(i, td);
                NonNegativeInteger firstT = meta.getTiffDataFirstT(i, td);
                int realCount = count == null ? 1 : count.getValue();
                if (ifd == null && count == null) {
                    realCount = ifdCount;
                }
                for (int q = 0; q < realCount; q++) {
                    OMETiffPlane p = new OMETiffPlane();
                    p.id = currentId;
                    p.ifd = q;
                    if (ifd != null) {
                        p.ifd += ifd.getValue();
                    }
                    p.reader = reader;
                    info[i][next++] = p;
                    int z = firstZ == null ? 0 : firstZ.getValue();
                    int c = firstC == null ? 0 : firstC.getValue();
                    int t = firstT == null ? 0 : firstT.getValue();
                    if (q > 0) {
                        int index = FormatTools.getIndex(order, sizeZ, sizeC, sizeT, num, z, c, t);
                        int[] add = FormatTools.getZCTCoords(order, sizeZ, sizeC, sizeT, num, q);
                        z += add[0];
                        c += add[1];
                        t += add[2];
                    }
                    if (z > maxZ) {
                        maxZ = z;
                    }
                    if (c > maxC) {
                        maxC = c;
                    }
                    if (t > maxT) {
                        maxT = t;
                    }
                    if (z < minZ) {
                        minZ = z;
                    }
                    if (c < minC) {
                        minC = c;
                    }
                    if (t < minT) {
                        minT = t;
                    }
                }
            }
            if (i <= maxSeries) {
                m.sizeZ = (maxZ - minZ) + 1;
                m.sizeC = (maxC - minC) + 1;
                m.sizeT = (maxT - minT) + 1;
                m.imageCount = m.sizeZ * m.sizeC * m.sizeT;
                m.sizeC *= meta.getChannelSamplesPerPixel(i, 0).getValue();
                if (i >= core.size()) {
                    core.add(m);
                }
                cBounds.add(new int[] { minC, maxC });
            } else {
                imagesToRemove.add(i);
            }
        }
        // remove extra Images, Channels, and Planes
        meta.resolveReferences();
        OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) meta.getRoot();
        List<Image> images = root.copyImageList();
        for (int i = imagesToRemove.size() - 1; i >= 0; i--) {
            images.remove(imagesToRemove.get(i));
        }
        for (int i = 0; i < images.size(); i++) {
            Image img = images.get(i);
            Pixels pix = img.getPixels();
            List<Plane> planes = pix.copyPlaneList();
            for (int p = 0; p < planes.size(); p++) {
                Plane plane = planes.get(p);
                if (plane.getTheZ().getValue() >= core.get(i).sizeZ || plane.getTheC().getValue() >= core.get(i).sizeC || plane.getTheT().getValue() >= core.get(i).sizeT) {
                    pix.removePlane(planes.get(p));
                }
            }
            pix.setMetadataOnly(null);
            List<Channel> channels = pix.copyChannelList();
            for (int c = 0; c < channels.size(); c++) {
                if (c < cBounds.get(i)[0] || c > cBounds.get(i)[1]) {
                    pix.removeChannel(channels.get(c));
                }
            }
        }
        meta.setRoot(root);
        service.convertMetadata(meta, metadataStore);
        MetadataTools.populatePixels(metadataStore, this);
        return;
    }
    service.convertMetadata(meta, metadataStore);
    // determine series count from Image and Pixels elements
    int seriesCount = meta.getImageCount();
    core.clear();
    for (int i = 0; i < seriesCount; i++) {
        core.add(new CoreMetadata());
    }
    info = new OMETiffPlane[seriesCount][];
    tileWidth = new int[seriesCount];
    tileHeight = new int[seriesCount];
    // compile list of file/UUID mappings
    Hashtable<String, String> files = new Hashtable<String, String>();
    boolean needSearch = false;
    for (int i = 0; i < seriesCount; i++) {
        int tiffDataCount = meta.getTiffDataCount(i);
        for (int td = 0; td < tiffDataCount; td++) {
            String uuid = null;
            try {
                uuid = meta.getUUIDValue(i, td);
            } catch (NullPointerException e) {
            }
            String filename = null;
            if (uuid == null) {
                // no UUID means that TiffData element refers to this file
                uuid = "";
                filename = id;
            } else {
                filename = meta.getUUIDFileName(i, td);
                if (!new Location(dir, filename).exists())
                    filename = null;
                if (filename == null) {
                    if (uuid.equals(currentUUID) || currentUUID == null) {
                        // UUID references this file
                        filename = id;
                    } else {
                        // will need to search for this UUID
                        filename = "";
                        needSearch = true;
                    }
                } else
                    filename = normalizeFilename(dir, filename);
            }
            String existing = files.get(uuid);
            if (existing == null)
                files.put(uuid, filename);
            else if (!existing.equals(filename)) {
                throw new FormatException("Inconsistent UUID filenames");
            }
        }
    }
    // search for missing filenames
    if (needSearch) {
        Enumeration en = files.keys();
        while (en.hasMoreElements()) {
            String uuid = (String) en.nextElement();
            String filename = files.get(uuid);
            if (filename.equals("")) {
                // to make this work with OME server may be a little tricky?
                throw new FormatException("Unmatched UUID: " + uuid);
            }
        }
    }
    // build list of used files
    Enumeration en = files.keys();
    int numUUIDs = files.size();
    // ensure no duplicate filenames
    HashSet fileSet = new HashSet();
    for (int i = 0; i < numUUIDs; i++) {
        String uuid = (String) en.nextElement();
        String filename = files.get(uuid);
        fileSet.add(filename);
    }
    used = new String[fileSet.size()];
    Iterator iter = fileSet.iterator();
    for (int i = 0; i < used.length; i++) used[i] = (String) iter.next();
    // process TiffData elements
    Hashtable<String, IFormatReader> readers = new Hashtable<String, IFormatReader>();
    boolean adjustedSamples = false;
    for (int i = 0; i < seriesCount; i++) {
        int s = i;
        LOGGER.debug("Image[{}] {", i);
        LOGGER.debug("  id = {}", meta.getImageID(i));
        String order = meta.getPixelsDimensionOrder(i).toString();
        PositiveInteger samplesPerPixel = null;
        if (meta.getChannelCount(i) > 0) {
            samplesPerPixel = meta.getChannelSamplesPerPixel(i, 0);
        }
        int samples = samplesPerPixel == null ? -1 : samplesPerPixel.getValue();
        int tiffSamples = firstIFD.getSamplesPerPixel();
        if (adjustedSamples || (samples != tiffSamples && (i == 0 || samples < 0))) {
            LOGGER.warn("SamplesPerPixel mismatch: OME={}, TIFF={}", samples, tiffSamples);
            samples = tiffSamples;
            adjustedSamples = true;
        } else {
            adjustedSamples = false;
        }
        if (adjustedSamples && meta.getChannelCount(i) <= 1) {
            adjustedSamples = false;
        }
        int effSizeC = meta.getPixelsSizeC(i).getValue().intValue();
        if (!adjustedSamples) {
            effSizeC /= samples;
        }
        if (effSizeC == 0)
            effSizeC = 1;
        if (effSizeC * samples != meta.getPixelsSizeC(i).getValue().intValue()) {
            effSizeC = meta.getPixelsSizeC(i).getValue().intValue();
        }
        int sizeT = meta.getPixelsSizeT(i).getValue().intValue();
        int sizeZ = meta.getPixelsSizeZ(i).getValue().intValue();
        int num = effSizeC * sizeT * sizeZ;
        OMETiffPlane[] planes = new OMETiffPlane[num];
        for (int no = 0; no < num; no++) planes[no] = new OMETiffPlane();
        int tiffDataCount = meta.getTiffDataCount(i);
        Boolean zOneIndexed = null;
        Boolean cOneIndexed = null;
        Boolean tOneIndexed = null;
        for (int td = 0; td < tiffDataCount; td++) {
            NonNegativeInteger firstC = meta.getTiffDataFirstC(i, td);
            NonNegativeInteger firstT = meta.getTiffDataFirstT(i, td);
            NonNegativeInteger firstZ = meta.getTiffDataFirstZ(i, td);
            int c = firstC == null ? 0 : firstC.getValue();
            int t = firstT == null ? 0 : firstT.getValue();
            int z = firstZ == null ? 0 : firstZ.getValue();
            if (c >= effSizeC && cOneIndexed == null) {
                cOneIndexed = true;
            } else if (c == 0) {
                cOneIndexed = false;
            }
            if (z >= sizeZ && zOneIndexed == null) {
                zOneIndexed = true;
            } else if (z == 0) {
                zOneIndexed = false;
            }
            if (t >= sizeT && tOneIndexed == null) {
                tOneIndexed = true;
            } else if (t == 0) {
                tOneIndexed = false;
            }
        }
        for (int td = 0; td < tiffDataCount; td++) {
            LOGGER.debug("    TiffData[{}] {", td);
            // extract TiffData parameters
            String filename = null;
            String uuid = null;
            try {
                filename = meta.getUUIDFileName(i, td);
            } catch (NullPointerException e) {
                LOGGER.debug("Ignoring null UUID object when retrieving filename.");
            }
            try {
                uuid = meta.getUUIDValue(i, td);
            } catch (NullPointerException e) {
                LOGGER.debug("Ignoring null UUID object when retrieving value.");
            }
            NonNegativeInteger tdIFD = meta.getTiffDataIFD(i, td);
            int ifd = tdIFD == null ? 0 : tdIFD.getValue();
            NonNegativeInteger numPlanes = meta.getTiffDataPlaneCount(i, td);
            NonNegativeInteger firstC = meta.getTiffDataFirstC(i, td);
            NonNegativeInteger firstT = meta.getTiffDataFirstT(i, td);
            NonNegativeInteger firstZ = meta.getTiffDataFirstZ(i, td);
            int c = firstC == null ? 0 : firstC.getValue();
            int t = firstT == null ? 0 : firstT.getValue();
            int z = firstZ == null ? 0 : firstZ.getValue();
            // NB: some writers index FirstC, FirstZ and FirstT from 1
            if (cOneIndexed != null && cOneIndexed)
                c--;
            if (zOneIndexed != null && zOneIndexed)
                z--;
            if (tOneIndexed != null && tOneIndexed)
                t--;
            if (z >= sizeZ || c >= effSizeC || t >= sizeT) {
                LOGGER.warn("Found invalid TiffData: Z={}, C={}, T={}", new Object[] { z, c, t });
                break;
            }
            int index = FormatTools.getIndex(order, sizeZ, effSizeC, sizeT, num, z, c, t);
            int count = numPlanes == null ? 1 : numPlanes.getValue();
            if (count == 0) {
                core.set(s, null);
                break;
            }
            // get reader object for this filename
            if (filename == null) {
                if (uuid == null)
                    filename = id;
                else
                    filename = files.get(uuid);
            } else
                filename = normalizeFilename(dir, filename);
            IFormatReader r = readers.get(filename);
            if (r == null) {
                r = new MinimalTiffReader();
                readers.put(filename, r);
            }
            Location file = new Location(filename);
            boolean exists = true;
            if (!file.exists()) {
                // if this is an absolute file name, try using a relative name
                // old versions of OMETiffWriter wrote an absolute path to
                // UUID.FileName, which causes problems if the file is moved to
                // a different directory
                filename = filename.substring(filename.lastIndexOf(File.separator) + 1);
                filename = dir + File.separator + filename;
                if (!new Location(filename).exists()) {
                    filename = currentId;
                    // if only one file is defined, we have to assume that it
                    // corresponds to the current file
                    exists = fileSet.size() == 1;
                }
            }
            // populate plane index -> IFD mapping
            for (int q = 0; q < count; q++) {
                int no = index + q;
                planes[no].reader = r;
                planes[no].id = filename;
                planes[no].ifd = ifd + q;
                planes[no].certain = true;
                planes[no].exists = exists;
                LOGGER.debug("      Plane[{}]: file={}, IFD={}", new Object[] { no, planes[no].id, planes[no].ifd });
            }
            if (numPlanes == null) {
                // unknown number of planes; fill down
                for (int no = index + 1; no < num; no++) {
                    if (planes[no].certain)
                        break;
                    planes[no].reader = r;
                    planes[no].id = filename;
                    planes[no].ifd = planes[no - 1].ifd + 1;
                    planes[no].exists = exists;
                    LOGGER.debug("      Plane[{}]: FILLED", no);
                }
            } else {
                // known number of planes; clear anything subsequently filled
                for (int no = index + count; no < num; no++) {
                    if (planes[no].certain)
                        break;
                    planes[no].reader = null;
                    planes[no].id = null;
                    planes[no].ifd = -1;
                    LOGGER.debug("      Plane[{}]: CLEARED", no);
                }
            }
            LOGGER.debug("    }");
        }
        if (core.get(s) == null)
            continue;
        // verify that all planes are available
        LOGGER.debug("    --------------------------------");
        for (int no = 0; no < num; no++) {
            LOGGER.debug("    Plane[{}]: file={}, IFD={}", new Object[] { no, planes[no].id, planes[no].ifd });
            if (planes[no].reader == null) {
                LOGGER.warn("Image ID '{}': missing plane #{}.  " + "Using TiffReader to determine the number of planes.", meta.getImageID(i), no);
                TiffReader r = new TiffReader();
                r.setId(currentId);
                try {
                    planes = new OMETiffPlane[r.getImageCount()];
                    for (int plane = 0; plane < planes.length; plane++) {
                        planes[plane] = new OMETiffPlane();
                        planes[plane].id = currentId;
                        planes[plane].reader = r;
                        planes[plane].ifd = plane;
                    }
                    num = planes.length;
                } finally {
                    r.close();
                }
            }
        }
        LOGGER.debug("  }");
        // populate core metadata
        CoreMetadata m = core.get(s);
        info[s] = planes;
        try {
            RandomAccessInputStream testFile = new RandomAccessInputStream(info[s][0].id, 16);
            String firstFile = info[s][0].id;
            if (!info[s][0].reader.isThisType(testFile)) {
                LOGGER.warn("{} is not a valid OME-TIFF", info[s][0].id);
                info[s][0].id = currentId;
                info[s][0].exists = false;
            }
            testFile.close();
            for (int plane = 1; plane < info[s].length; plane++) {
                if (info[s][plane].id.equals(firstFile)) {
                    // don't repeat slow type checking if the files are the same
                    if (!info[s][0].exists) {
                        info[s][plane].id = info[s][0].id;
                        info[s][plane].exists = false;
                    }
                    continue;
                }
                testFile = new RandomAccessInputStream(info[s][plane].id, 16);
                if (!info[s][plane].reader.isThisType(testFile)) {
                    LOGGER.warn("{} is not a valid OME-TIFF", info[s][plane].id);
                    info[s][plane].id = info[s][0].id;
                    info[s][plane].exists = false;
                }
                testFile.close();
            }
            info[s][0].reader.setId(info[s][0].id);
            tileWidth[s] = info[s][0].reader.getOptimalTileWidth();
            tileHeight[s] = info[s][0].reader.getOptimalTileHeight();
            m.sizeX = meta.getPixelsSizeX(i).getValue().intValue();
            int tiffWidth = (int) firstIFD.getImageWidth();
            if (m.sizeX != tiffWidth && s == 0) {
                LOGGER.warn("SizeX mismatch: OME={}, TIFF={}", m.sizeX, tiffWidth);
            }
            m.sizeY = meta.getPixelsSizeY(i).getValue().intValue();
            int tiffHeight = (int) firstIFD.getImageLength();
            if (m.sizeY != tiffHeight && s == 0) {
                LOGGER.warn("SizeY mismatch: OME={}, TIFF={}", m.sizeY, tiffHeight);
            }
            m.sizeZ = meta.getPixelsSizeZ(i).getValue().intValue();
            m.sizeC = meta.getPixelsSizeC(i).getValue().intValue();
            m.sizeT = meta.getPixelsSizeT(i).getValue().intValue();
            m.pixelType = FormatTools.pixelTypeFromString(meta.getPixelsType(i).toString());
            int tiffPixelType = firstIFD.getPixelType();
            if (m.pixelType != tiffPixelType && (s == 0 || adjustedSamples)) {
                LOGGER.warn("PixelType mismatch: OME={}, TIFF={}", m.pixelType, tiffPixelType);
                m.pixelType = tiffPixelType;
            }
            m.imageCount = num;
            m.dimensionOrder = meta.getPixelsDimensionOrder(i).toString();
            // hackish workaround for files exported by OMERO that have an
            // incorrect dimension order
            String uuidFileName = "";
            try {
                if (meta.getTiffDataCount(i) > 0) {
                    uuidFileName = meta.getUUIDFileName(i, 0);
                }
            } catch (NullPointerException e) {
            }
            if (meta.getChannelCount(i) > 0 && meta.getChannelName(i, 0) == null && meta.getTiffDataCount(i) > 0 && uuidFileName.indexOf("__omero_export") != -1) {
                m.dimensionOrder = "XYZCT";
            }
            m.orderCertain = true;
            PhotoInterp photo = firstIFD.getPhotometricInterpretation();
            m.rgb = samples > 1 || photo == PhotoInterp.RGB;
            if ((samples != m.sizeC && (samples % m.sizeC) != 0 && (m.sizeC % samples) != 0) || m.sizeC == 1 || adjustedSamples) {
                m.sizeC *= samples;
            }
            if (m.sizeZ * m.sizeT * m.sizeC > m.imageCount && !m.rgb) {
                if (m.sizeZ == m.imageCount) {
                    m.sizeT = 1;
                    m.sizeC = 1;
                } else if (m.sizeT == m.imageCount) {
                    m.sizeZ = 1;
                    m.sizeC = 1;
                } else if (m.sizeC == m.imageCount) {
                    m.sizeT = 1;
                    m.sizeZ = 1;
                }
            }
            if (meta.getPixelsBinDataCount(i) > 1) {
                LOGGER.warn("OME-TIFF Pixels element contains BinData elements! " + "Ignoring.");
            }
            m.littleEndian = firstIFD.isLittleEndian();
            m.interleaved = false;
            m.indexed = photo == PhotoInterp.RGB_PALETTE && firstIFD.getIFDValue(IFD.COLOR_MAP) != null;
            if (m.indexed) {
                m.rgb = false;
            }
            m.falseColor = true;
            m.metadataComplete = true;
            if (meta.getPixelsSignificantBits(i) != null) {
                m.bitsPerPixel = meta.getPixelsSignificantBits(i).getValue();
            }
        } catch (NullPointerException exc) {
            throw new FormatException("Incomplete Pixels metadata", exc);
        }
    }
    // remove null CoreMetadata entries
    ArrayList<CoreMetadata> series = new ArrayList<CoreMetadata>();
    final List<OMETiffPlane[]> planeInfo = new ArrayList<OMETiffPlane[]>();
    for (int i = 0; i < core.size(); i++) {
        if (core.get(i) != null) {
            series.add(core.get(i));
            planeInfo.add(info[i]);
        }
    }
    core = series;
    info = planeInfo.toArray(new OMETiffPlane[0][0]);
    if (getImageCount() == 1) {
        CoreMetadata ms0 = core.get(0);
        ms0.sizeZ = 1;
        if (!ms0.rgb) {
            ms0.sizeC = 1;
        }
        ms0.sizeT = 1;
    }
    for (int i = 0; i < core.size(); i++) {
        CoreMetadata m = core.get(i);
        Modulo z = service.getModuloAlongZ(meta, i);
        if (z != null) {
            m.moduloZ = z;
        }
        Modulo c = service.getModuloAlongC(meta, i);
        if (c != null) {
            m.moduloC = c;
        }
        Modulo t = service.getModuloAlongT(meta, i);
        if (t != null) {
            m.moduloT = t;
        }
    }
    MetadataTools.populatePixels(metadataStore, this, false, false);
    for (int i = 0; i < meta.getImageCount(); i++) {
        // TheT values are not changed
        for (int p = 0; p < meta.getPlaneCount(i); p++) {
            NonNegativeInteger z = meta.getPlaneTheZ(i, p);
            NonNegativeInteger c = meta.getPlaneTheC(i, p);
            NonNegativeInteger t = meta.getPlaneTheT(i, p);
            if (z == null) {
                z = new NonNegativeInteger(0);
                metadataStore.setPlaneTheZ(z, i, p);
            }
            if (c == null) {
                c = new NonNegativeInteger(0);
                metadataStore.setPlaneTheC(c, i, p);
            }
            if (t == null) {
                t = new NonNegativeInteger(0);
                metadataStore.setPlaneTheT(t, i, p);
            }
        }
    }
    for (int i = 0; i < acquiredDates.length; i++) {
        if (acquiredDates[i] != null) {
            metadataStore.setImageAcquisitionDate(new Timestamp(acquiredDates[i]), i);
        }
    }
}
Also used : IFormatReader(loci.formats.IFormatReader) IFD(loci.formats.tiff.IFD) ArrayList(java.util.ArrayList) Image(ome.xml.model.Image) Timestamp(ome.xml.model.primitives.Timestamp) Pixels(ome.xml.model.Pixels) Iterator(java.util.Iterator) HashSet(java.util.HashSet) PositiveInteger(ome.xml.model.primitives.PositiveInteger) Enumeration(java.util.Enumeration) Plane(ome.xml.model.Plane) Hashtable(java.util.Hashtable) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Modulo(loci.formats.Modulo) Channel(ome.xml.model.Channel) PhotoInterp(loci.formats.tiff.PhotoInterp) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) ServiceException(loci.common.services.ServiceException) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) File(java.io.File) Location(loci.common.Location)

Example 30 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class OpenlabReader 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);
    lastPlane = no;
    PlaneInfo planeInfo = null;
    if (specialPlateNames) {
        planeInfo = getPlane(getZCTCoords(no));
    } else if (no < planeOffsets[getSeries()].length) {
        int index = planeOffsets[getSeries()][no];
        planeInfo = planes[index];
    }
    if (planeInfo == null)
        return buf;
    long first = planeInfo.planeOffset;
    long last = first + FormatTools.getPlaneSize(this) * 2;
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    if (!planeInfo.pict) {
        if (version == 2) {
            in.seek(first);
            readPlane(in, x, y, w, h, buf);
        } else {
            in.seek(first + 16);
            int bytes = bpp * getRGBChannelCount();
            byte[] b = new byte[(int) (last - first)];
            in.read(b);
            CodecOptions options = new CodecOptions();
            options.width = getSizeX();
            options.height = getSizeY();
            options.bitsPerSample = bytes * 8;
            options.maxBytes = getSizeX() * getSizeY() * bytes;
            b = new LZOCodec().decompress(b, options);
            if (getSizeX() * getSizeY() * 4 <= b.length) {
                for (int yy = y; yy < h + y; yy++) {
                    for (int xx = x; xx < w + x; xx++) {
                        System.arraycopy(b, (yy * (getSizeX() + 4) + xx) * 4 + 1, buf, ((yy - y) * w + xx - x) * 3, 3);
                    }
                }
            } else {
                int src = b.length / getSizeY();
                if (src - (getSizeX() * bytes) != 16)
                    src = getSizeX() * bytes;
                int dest = w * bytes;
                for (int row = 0; row < h; row++) {
                    System.arraycopy(b, (row + y) * src + x * bytes, buf, row * dest, dest);
                }
            }
            b = null;
        }
        if (planeInfo.volumeType == MAC_256_GREYS || planeInfo.volumeType == MAC_256_COLORS) {
            for (int i = 0; i < buf.length; i++) {
                buf[i] = (byte) (~buf[i] & 0xff);
            }
        }
    } else {
        // PICT plane
        Exception exc = null;
        if (getPixelType() == FormatTools.UINT8) {
            in.seek(first);
            byte[] b = new byte[(int) (last - first) + 512];
            in.read(b, 512, b.length - 512);
            IFormatReader r = getRGBChannelCount() == 1 ? new ChannelSeparator(pict) : pict;
            try {
                Location.mapFile("OPENLAB_PICT", new ByteArrayHandle(b));
                r.setId("OPENLAB_PICT");
                if (getPixelType() != r.getPixelType()) {
                    throw new FormatException("Pixel type of inner PICT does not " + "match pixel type of Openlab file");
                }
                if (isIndexed()) {
                    int index = no;
                    if (getSeries() < planeOffsets.length) {
                        index = planeOffsets[getSeries()][lastPlane];
                    }
                    luts.set(index, pict.get8BitLookupTable());
                }
                r.openBytes(0, buf, x, y, w, h);
            } catch (FormatException e) {
                exc = e;
            } catch (IOException e) {
                exc = e;
            } finally {
                r.close();
                // remove file from map
                Location.mapFile("OPENLAB_PICT", null);
            }
            b = null;
        }
        if (exc != null || getPixelType() != FormatTools.UINT8) {
            LOGGER.debug("", exc);
            in.seek(planeInfo.planeOffset - 298);
            if (in.readByte() == 1)
                in.skipBytes(297);
            else
                in.skipBytes(169);
            int size = 0, expectedBlock = 0, totalBlocks = -1, pixPos = 0;
            byte[] plane = new byte[FormatTools.getPlaneSize(this)];
            while (expectedBlock != totalBlocks && pixPos < plane.length && in.getFilePointer() + 32 < last) {
                findNextBlock();
                if (in.getFilePointer() + 4 >= in.length())
                    break;
                int num = in.readInt();
                if (num != expectedBlock) {
                    throw new FormatException("Expected iPic block not found");
                }
                expectedBlock++;
                if (totalBlocks == -1) {
                    totalBlocks = in.readInt();
                    in.skipBytes(8);
                } else
                    in.skipBytes(12);
                size = in.readInt();
                in.skipBytes(4);
                if (size + pixPos > plane.length)
                    size = plane.length - pixPos;
                in.read(plane, pixPos, size);
                pixPos += size;
            }
            RandomAccessInputStream pix = new RandomAccessInputStream(plane);
            readPlane(pix, x, y, w, h, buf);
            pix.close();
            plane = null;
        }
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) IFormatReader(loci.formats.IFormatReader) LZOCodec(loci.formats.codec.LZOCodec) IOException(java.io.IOException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) ChannelSeparator(loci.formats.ChannelSeparator) FormatException(loci.formats.FormatException) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Aggregations

IFormatReader (loci.formats.IFormatReader)38 ImageReader (loci.formats.ImageReader)20 FormatException (loci.formats.FormatException)15 IOException (java.io.IOException)10 FileStitcher (loci.formats.FileStitcher)10 Test (org.testng.annotations.Test)8 Location (loci.common.Location)6 ChannelSeparator (loci.formats.ChannelSeparator)6 ServiceException (loci.common.services.ServiceException)5 ServiceFactory (loci.common.services.ServiceFactory)5 ReaderWrapper (loci.formats.ReaderWrapper)5 IMetadata (loci.formats.meta.IMetadata)5 RandomAccessInputStream (loci.common.RandomAccessInputStream)4 ChannelFiller (loci.formats.ChannelFiller)4 CoreMetadata (loci.formats.CoreMetadata)4 DimensionSwapper (loci.formats.DimensionSwapper)4 MinMaxCalculator (loci.formats.MinMaxCalculator)4 BufferedImageReader (loci.formats.gui.BufferedImageReader)4 OMEXMLService (loci.formats.services.OMEXMLService)4 DependencyException (loci.common.services.DependencyException)3