Search in sources :

Example 66 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class AmiraReader method initFile.

/* (non-Javadoc)
   * @see loci.formats.FormatReader#initFile(java.lang.String)
   */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    AmiraParameters parameters = new AmiraParameters(in);
    offsetOfFirstStream = in.getFilePointer();
    LOGGER.info("Populating metadata hashtable");
    addGlobalMeta("Image width", parameters.width);
    addGlobalMeta("Image height", parameters.height);
    addGlobalMeta("Number of planes", parameters.depth);
    addGlobalMeta("Bits per pixel", 8);
    LOGGER.info("Populating core metadata");
    int channelIndex = 1;
    while (parameters.getStreams().get("@" + channelIndex) != null) {
        channelIndex++;
    }
    CoreMetadata m = core.get(0);
    m.sizeX = parameters.width;
    m.sizeY = parameters.height;
    m.sizeZ = parameters.depth;
    m.sizeT = 1;
    m.sizeC = channelIndex - 1;
    m.imageCount = getSizeZ() * getSizeC();
    m.littleEndian = parameters.littleEndian;
    m.dimensionOrder = "XYZCT";
    String streamType = parameters.streamTypes[0].toLowerCase();
    if (streamType.equals("byte")) {
        m.pixelType = FormatTools.UINT8;
    } else if (streamType.equals("short")) {
        m.pixelType = FormatTools.INT16;
        addGlobalMeta("Bits per pixel", 16);
    } else if (streamType.equals("ushort")) {
        m.pixelType = FormatTools.UINT16;
        addGlobalMeta("Bits per pixel", 16);
    } else if (streamType.equals("int")) {
        m.pixelType = FormatTools.INT32;
        addGlobalMeta("Bits per pixel", 32);
    } else if (streamType.equals("float")) {
        m.pixelType = FormatTools.FLOAT;
        addGlobalMeta("Bits per pixel", 32);
    } else {
        LOGGER.warn("Assuming data type is byte");
        m.pixelType = FormatTools.UINT8;
    }
    LOGGER.info("Populating metadata store");
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    // The bounding box is the range of the centre of the voxels
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        double pixelWidth = (double) (parameters.x1 - parameters.x0) / (parameters.width - 1);
        double pixelHeight = (double) (parameters.y1 - parameters.y0) / (parameters.height - 1);
        // TODO - what is correct setting if single slice?
        double pixelDepth = (double) (parameters.z1 - parameters.z0) / (parameters.depth - 1);
        // Amira does not have a standard form for encoding units, so we just
        // have to assume microns for microscopy data
        addGlobalMeta("Pixels per meter (X)", 1e6 / pixelWidth);
        addGlobalMeta("Pixels per meter (Y)", 1e6 / pixelHeight);
        addGlobalMeta("Pixels per meter (Z)", 1e6 / pixelDepth);
        Length sizeX = FormatTools.getPhysicalSizeX(pixelWidth);
        Length sizeY = FormatTools.getPhysicalSizeY(pixelHeight);
        Length sizeZ = FormatTools.getPhysicalSizeZ(pixelDepth);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
        if (sizeZ != null) {
            store.setPixelsPhysicalSizeZ(sizeZ, 0);
        }
    }
    ascii = parameters.ascii;
    ArrayList streamData = (ArrayList) parameters.getStreams().get("@1");
    if (streamData.size() > 2) {
        compression = (String) streamData.get(2);
    }
    initPlaneReader();
    hasPlaneReader = planeReader != null;
    addGlobalMeta("Compression", compression);
    Map params = (Map) parameters.getMap().get("Parameters");
    if (params != null) {
        Map materials = (Map) params.get("Materials");
        if (materials != null) {
            lut = getLookupTable(materials);
            m.indexed = true;
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) ArrayList(java.util.ArrayList) AmiraParameters(loci.formats.tools.AmiraParameters) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) Map(java.util.Map)

Example 67 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class BDReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    // make sure we have the experiment file
    id = locateExperimentFile(id);
    super.initFile(id);
    Location dir = new Location(id).getAbsoluteFile().getParentFile();
    rootList = dir.list(true);
    Arrays.sort(rootList);
    for (int i = 0; i < rootList.length; i++) {
        String file = rootList[i];
        Location f = new Location(dir, file);
        rootList[i] = f.getAbsolutePath();
        if (!f.isDirectory()) {
            if (checkSuffix(file, META_EXT) && !f.isDirectory()) {
                metadataFiles.add(f.getAbsolutePath());
            }
        } else {
            String[] wells = f.list(true);
            Arrays.sort(wells);
            wellList.add(wells);
            for (String well : wells) {
                Location wellFile = new Location(f, well);
                if (!wellFile.isDirectory()) {
                    if (checkSuffix(well, META_EXT)) {
                        metadataFiles.add(wellFile.getAbsolutePath());
                    }
                }
            }
        }
    }
    // parse Experiment metadata
    IniList experiment = readMetaData(id);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        objective = experiment.getTable("Geometry").get("Name");
        IniTable camera = experiment.getTable("Camera");
        binning = camera.get("BinX") + "x" + camera.get("BinY");
        parseChannelData(dir);
        addGlobalMeta("Objective", objective);
        addGlobalMeta("Camera binning", binning);
    }
    final List<String> uniqueRows = new ArrayList<String>();
    final List<String> uniqueColumns = new ArrayList<String>();
    for (String well : wellLabels) {
        String row = well.substring(0, 1).trim();
        String column = well.substring(1).trim();
        if (!uniqueRows.contains(row) && row.length() > 0)
            uniqueRows.add(row);
        if (!uniqueColumns.contains(column) && column.length() > 0) {
            uniqueColumns.add(column);
        }
    }
    int nSlices = getSizeZ() == 0 ? 1 : getSizeZ();
    int nTimepoints = getSizeT();
    int nWells = wellLabels.size();
    int nChannels = getSizeC() == 0 ? channelNames.size() : getSizeC();
    if (nChannels == 0)
        nChannels = 1;
    tiffs = getTiffs();
    reader = new MinimalTiffReader();
    reader.setId(tiffs[0][0]);
    int sizeX = reader.getSizeX();
    int sizeY = reader.getSizeY();
    int pixelType = reader.getPixelType();
    boolean rgb = reader.isRGB();
    boolean interleaved = reader.isInterleaved();
    boolean indexed = reader.isIndexed();
    boolean littleEndian = reader.isLittleEndian();
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        IniParser parser = new IniParser();
        for (String metadataFile : metadataFiles) {
            String filename = new Location(metadataFile).getName();
            if (!checkSuffix(metadataFile, new String[] { "txt", "bmp", "adf", "roi" })) {
                String data = DataTools.readFile(metadataFile);
                IniList ini = parser.parseINI(new BufferedReader(new StringReader(data)));
                HashMap<String, String> h = ini.flattenIntoHashMap();
                for (String key : h.keySet()) {
                    addGlobalMeta(filename + " " + key, h.get(key));
                }
            }
        }
    }
    int coresize = core.size();
    core.clear();
    for (int i = 0; i < coresize; i++) {
        CoreMetadata ms = new CoreMetadata();
        core.add(ms);
        ms.sizeC = nChannels;
        ms.sizeZ = nSlices;
        ms.sizeT = nTimepoints;
        ms.sizeX = sizeX / fieldCols;
        ms.sizeY = sizeY / fieldRows;
        ms.pixelType = pixelType;
        ms.rgb = rgb;
        ms.interleaved = interleaved;
        ms.indexed = indexed;
        ms.littleEndian = littleEndian;
        ms.dimensionOrder = "XYZTC";
        ms.imageCount = nSlices * nTimepoints * nChannels;
    }
    MetadataStore store = makeFilterMetadata();
    boolean populatePlanes = getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM;
    MetadataTools.populatePixels(store, this, populatePlanes);
    String plateAcqID = MetadataTools.createLSID("PlateAcquisition", 0, 0);
    store.setPlateAcquisitionID(plateAcqID, 0, 0);
    PositiveInteger fieldCount = FormatTools.getMaxFieldCount(fieldRows * fieldCols);
    if (fieldCount != null) {
        store.setPlateAcquisitionMaximumFieldCount(fieldCount, 0, 0);
    }
    for (int row = 0; row < wellRows; row++) {
        for (int col = 0; col < wellCols; col++) {
            int index = row * wellCols + col;
            store.setWellID(MetadataTools.createLSID("Well", 0, index), 0, index);
            store.setWellRow(new NonNegativeInteger(row), 0, index);
            store.setWellColumn(new NonNegativeInteger(col), 0, index);
        }
    }
    for (int i = 0; i < getSeriesCount(); i++) {
        int well = i / (fieldRows * fieldCols);
        int field = i % (fieldRows * fieldCols);
        MetadataTools.setDefaultCreationDate(store, tiffs[well][0], i);
        String name = wellLabels.get(well);
        String row = name.substring(0, 1);
        Integer col = Integer.parseInt(name.substring(1));
        int index = (row.charAt(0) - 'A') * wellCols + col - 1;
        String wellSampleID = MetadataTools.createLSID("WellSample", 0, index, field);
        store.setWellSampleID(wellSampleID, 0, index, field);
        store.setWellSampleIndex(new NonNegativeInteger(i), 0, index, field);
        String imageID = MetadataTools.createLSID("Image", i);
        store.setWellSampleImageRef(imageID, 0, index, field);
        store.setImageID(imageID, i);
        store.setImageName(name + " Field #" + (field + 1), i);
        store.setPlateAcquisitionWellSampleRef(wellSampleID, 0, 0, i);
    }
    MetadataLevel level = getMetadataOptions().getMetadataLevel();
    if (level != MetadataLevel.MINIMUM) {
        String instrumentID = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrumentID, 0);
        String objectiveID = MetadataTools.createLSID("Objective", 0, 0);
        store.setObjectiveID(objectiveID, 0, 0);
        if (objective != null) {
            String[] tokens = objective.split(" ");
            String mag = tokens[0].replaceAll("[xX]", "");
            String na = null;
            int naIndex = 0;
            for (int i = 0; i < tokens.length; i++) {
                if (tokens[i].equals("NA")) {
                    naIndex = i + 1;
                    na = tokens[naIndex];
                    break;
                }
            }
            Double magnification = new Double(mag);
            store.setObjectiveNominalMagnification(magnification, 0, 0);
            if (na != null) {
                na = na.substring(0, 1) + "." + na.substring(1);
                store.setObjectiveLensNA(new Double(na), 0, 0);
            }
            if (naIndex + 1 < tokens.length) {
                store.setObjectiveManufacturer(tokens[naIndex + 1], 0, 0);
            }
        }
        // populate LogicalChannel data
        for (int i = 0; i < getSeriesCount(); i++) {
            store.setImageInstrumentRef(instrumentID, i);
            store.setObjectiveSettingsID(objectiveID, i);
            for (int c = 0; c < getSizeC(); c++) {
                store.setChannelName(channelNames.get(c), i, c);
                Length emission = FormatTools.getEmissionWavelength(emWave[c]);
                Length excitation = FormatTools.getExcitationWavelength(exWave[c]);
                if (emission != null) {
                    store.setChannelEmissionWavelength(emission, i, c);
                }
                if (excitation != null) {
                    store.setChannelExcitationWavelength(excitation, i, c);
                }
                String detectorID = MetadataTools.createLSID("Detector", 0, c);
                store.setDetectorID(detectorID, 0, c);
                store.setDetectorSettingsID(detectorID, i, c);
                store.setDetectorSettingsGain(gain[c], i, c);
                store.setDetectorSettingsOffset(offset[c], i, c);
                store.setDetectorSettingsBinning(getBinning(binning), i, c);
            }
            long firstPlane = 0;
            for (int p = 0; p < getImageCount(); p++) {
                int[] zct = getZCTCoords(p);
                store.setPlaneExposureTime(new Time(exposure[zct[1]], UNITS.SECOND), i, p);
                String file = getFilename(i, p);
                if (file != null) {
                    long plane = getTimestamp(file);
                    if (p == 0) {
                        firstPlane = plane;
                    }
                    double timestamp = (plane - firstPlane) / 1000.0;
                    store.setPlaneDeltaT(new Time(timestamp, UNITS.SECOND), i, p);
                }
            }
        }
        store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
        store.setPlateRowNamingConvention(getNamingConvention("Letter"), 0);
        store.setPlateColumnNamingConvention(getNamingConvention("Number"), 0);
        store.setPlateName(plateName, 0);
        store.setPlateDescription(plateDescription, 0);
        if (level != MetadataLevel.NO_OVERLAYS) {
            parseROIs(store);
        }
    }
}
Also used : IniParser(loci.common.IniParser) IniList(loci.common.IniList) ArrayList(java.util.ArrayList) Time(ome.units.quantity.Time) StringReader(java.io.StringReader) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) CoreMetadata(loci.formats.CoreMetadata) MetadataStore(loci.formats.meta.MetadataStore) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Length(ome.units.quantity.Length) IniTable(loci.common.IniTable) BufferedReader(java.io.BufferedReader) Location(loci.common.Location)

Example 68 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class TileStitcher method setId.

/* @see IFormatReader#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
    super.setId(id);
    MetadataStore store = getMetadataStore();
    if (!(store instanceof IMetadata) || reader.getSeriesCount() == 1) {
        tileX = 1;
        tileY = 1;
        return;
    }
    IMetadata meta = (IMetadata) store;
    // enough to just ignore HCS data with multiple plates and/or wells
    if (meta.getPlateCount() > 1 || (meta.getPlateCount() == 1 && meta.getWellCount(0) > 1)) {
        tileX = 1;
        tileY = 1;
        return;
    }
    // now make sure that all of the series have the same dimensions
    boolean equalDimensions = true;
    for (int i = 1; i < meta.getImageCount(); i++) {
        if (!meta.getPixelsSizeX(i).equals(meta.getPixelsSizeX(0))) {
            equalDimensions = false;
        }
        if (!meta.getPixelsSizeY(i).equals(meta.getPixelsSizeY(0))) {
            equalDimensions = false;
        }
        if (!meta.getPixelsSizeZ(i).equals(meta.getPixelsSizeZ(0))) {
            equalDimensions = false;
        }
        if (!meta.getPixelsSizeC(i).equals(meta.getPixelsSizeC(0))) {
            equalDimensions = false;
        }
        if (!meta.getPixelsSizeT(i).equals(meta.getPixelsSizeT(0))) {
            equalDimensions = false;
        }
        if (!meta.getPixelsType(i).equals(meta.getPixelsType(0))) {
            equalDimensions = false;
        }
        if (!equalDimensions)
            break;
    }
    if (!equalDimensions) {
        tileX = 1;
        tileY = 1;
        return;
    }
    ArrayList<TileCoordinate> tiles = new ArrayList<TileCoordinate>();
    final List<Length> uniqueX = new ArrayList<Length>();
    final List<Length> uniqueY = new ArrayList<Length>();
    boolean equalZs = true;
    final Length firstZ = meta.getPlanePositionZ(0, meta.getPlaneCount(0) - 1);
    for (int i = 0; i < reader.getSeriesCount(); i++) {
        TileCoordinate coord = new TileCoordinate();
        coord.x = meta.getPlanePositionX(i, meta.getPlaneCount(i) - 1);
        coord.y = meta.getPlanePositionY(i, meta.getPlaneCount(i) - 1);
        tiles.add(coord);
        if (coord.x != null && !uniqueX.contains(coord.x)) {
            uniqueX.add(coord.x);
        }
        if (coord.y != null && !uniqueY.contains(coord.y)) {
            uniqueY.add(coord.y);
        }
        final Length zPos = meta.getPlanePositionZ(i, meta.getPlaneCount(i) - 1);
        if (firstZ == null) {
            if (zPos != null) {
                equalZs = false;
            }
        } else {
            if (!firstZ.equals(zPos)) {
                equalZs = false;
            }
        }
    }
    tileX = uniqueX.size();
    tileY = uniqueY.size();
    if (!equalZs) {
        LOGGER.warn("Z positions not equal");
    }
    tileMap = new Integer[tileY][tileX];
    final Length[] xCoordinates = uniqueX.toArray(new Length[tileX]);
    Arrays.sort(xCoordinates);
    final Length[] yCoordinates = uniqueY.toArray(new Length[tileY]);
    Arrays.sort(yCoordinates);
    for (int row = 0; row < tileMap.length; row++) {
        for (int col = 0; col < tileMap[row].length; col++) {
            TileCoordinate coordinate = new TileCoordinate();
            coordinate.x = xCoordinates[col];
            coordinate.y = yCoordinates[row];
            for (int tile = 0; tile < tiles.size(); tile++) {
                if (tiles.get(tile).equals(coordinate)) {
                    tileMap[row][col] = tile;
                }
            }
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) IMetadata(loci.formats.meta.IMetadata) Length(ome.units.quantity.Length) ArrayList(java.util.ArrayList)

Example 69 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class ImageConverter method testConvert.

// -- Utility methods --
/**
 * A utility method for converting a file from the command line.
 */
public boolean testConvert(IFormatWriter writer, String[] args) throws FormatException, IOException {
    nextOutputIndex.clear();
    options.setValidate(validate);
    writer.setMetadataOptions(options);
    firstTile = true;
    boolean success = parseArgs(args);
    if (!success) {
        return false;
    }
    if (printVersion) {
        CommandLineTools.printVersion();
        return true;
    }
    CommandLineTools.runUpgradeCheck(args);
    if (in == null || out == null) {
        printUsage();
        return false;
    }
    if (new Location(out).exists()) {
        if (overwrite == null) {
            LOGGER.warn("Output file {} exists.", out);
            LOGGER.warn("Do you want to overwrite it? ([y]/n)");
            BufferedReader r = new BufferedReader(new InputStreamReader(System.in, Constants.ENCODING));
            String choice = r.readLine().trim().toLowerCase();
            overwrite = !choice.startsWith("n");
        }
        if (!overwrite) {
            LOGGER.warn("Exiting; next time, please specify an output file that " + "does not exist.");
            return false;
        } else {
            new Location(out).delete();
        }
    }
    if (map != null)
        Location.mapId(in, map);
    long start = System.currentTimeMillis();
    LOGGER.info(in);
    reader = new ImageReader();
    if (stitch) {
        reader = new FileStitcher(reader);
        Location f = new Location(in);
        String pat = null;
        if (!f.exists()) {
            pat = in;
        } else {
            pat = FilePattern.findPattern(f);
        }
        if (pat != null)
            in = pat;
    }
    if (separate)
        reader = new ChannelSeparator(reader);
    if (merge)
        reader = new ChannelMerger(reader);
    if (fill)
        reader = new ChannelFiller(reader);
    minMax = null;
    if (autoscale) {
        reader = new MinMaxCalculator(reader);
        minMax = (MinMaxCalculator) reader;
    }
    reader.setMetadataOptions(options);
    reader.setGroupFiles(group);
    reader.setMetadataFiltered(true);
    reader.setOriginalMetadataPopulated(true);
    OMEXMLService service = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(OMEXMLService.class);
        reader.setMetadataStore(service.createOMEXMLMetadata());
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    } catch (ServiceException se) {
        throw new FormatException(se);
    }
    reader.setId(in);
    MetadataStore store = reader.getMetadataStore();
    MetadataTools.populatePixels(store, reader, false, false);
    boolean dimensionsSet = true;
    if (width == 0 || height == 0) {
        // otherwise default to series 0
        if (series >= 0) {
            reader.setSeries(series);
        }
        width = reader.getSizeX();
        height = reader.getSizeY();
        dimensionsSet = false;
    }
    if (channel >= reader.getEffectiveSizeC()) {
        throw new FormatException("Invalid channel '" + channel + "' (" + reader.getEffectiveSizeC() + " channels in source file)");
    }
    if (timepoint >= reader.getSizeT()) {
        throw new FormatException("Invalid timepoint '" + timepoint + "' (" + reader.getSizeT() + " timepoints in source file)");
    }
    if (zSection >= reader.getSizeZ()) {
        throw new FormatException("Invalid Z section '" + zSection + "' (" + reader.getSizeZ() + " Z sections in source file)");
    }
    if (store instanceof MetadataRetrieve) {
        try {
            String xml = service.getOMEXML(service.asRetrieve(store));
            OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) store.getRoot();
            IMetadata meta = service.createOMEXMLMetadata(xml);
            if (series >= 0) {
                Image exportImage = new Image(root.getImage(series));
                Pixels exportPixels = new Pixels(root.getImage(series).getPixels());
                exportImage.setPixels(exportPixels);
                OMEXMLMetadataRoot newRoot = (OMEXMLMetadataRoot) meta.getRoot();
                while (newRoot.sizeOfImageList() > 0) {
                    newRoot.removeImage(newRoot.getImage(0));
                }
                newRoot.addImage(exportImage);
                meta.setRoot(newRoot);
                meta.setPixelsSizeX(new PositiveInteger(width), 0);
                meta.setPixelsSizeY(new PositiveInteger(height), 0);
                if (autoscale) {
                    store.setPixelsType(PixelType.UINT8, 0);
                }
                if (channel >= 0) {
                    meta.setPixelsSizeC(new PositiveInteger(1), 0);
                }
                if (zSection >= 0) {
                    meta.setPixelsSizeZ(new PositiveInteger(1), 0);
                }
                if (timepoint >= 0) {
                    meta.setPixelsSizeT(new PositiveInteger(1), 0);
                }
                writer.setMetadataRetrieve((MetadataRetrieve) meta);
            } else {
                for (int i = 0; i < reader.getSeriesCount(); i++) {
                    meta.setPixelsSizeX(new PositiveInteger(width), 0);
                    meta.setPixelsSizeY(new PositiveInteger(height), 0);
                    if (autoscale) {
                        store.setPixelsType(PixelType.UINT8, i);
                    }
                    if (channel >= 0) {
                        meta.setPixelsSizeC(new PositiveInteger(1), 0);
                    }
                    if (zSection >= 0) {
                        meta.setPixelsSizeZ(new PositiveInteger(1), 0);
                    }
                    if (timepoint >= 0) {
                        meta.setPixelsSizeT(new PositiveInteger(1), 0);
                    }
                }
                writer.setMetadataRetrieve((MetadataRetrieve) meta);
            }
        } catch (ServiceException e) {
            throw new FormatException(e);
        }
    }
    writer.setWriteSequentially(true);
    if (writer instanceof TiffWriter) {
        ((TiffWriter) writer).setBigTiff(bigtiff);
    } else if (writer instanceof ImageWriter) {
        IFormatWriter w = ((ImageWriter) writer).getWriter(out);
        if (w instanceof TiffWriter) {
            ((TiffWriter) w).setBigTiff(bigtiff);
        }
    }
    String format = writer.getFormat();
    LOGGER.info("[{}] -> {} [{}]", new Object[] { reader.getFormat(), out, format });
    long mid = System.currentTimeMillis();
    int total = 0;
    int num = writer.canDoStacks() ? reader.getSeriesCount() : 1;
    long read = 0, write = 0;
    int first = series == -1 ? 0 : series;
    int last = series == -1 ? num : series + 1;
    long timeLastLogged = System.currentTimeMillis();
    for (int q = first; q < last; q++) {
        reader.setSeries(q);
        firstTile = true;
        if (!dimensionsSet) {
            width = reader.getSizeX();
            height = reader.getSizeY();
        }
        int writerSeries = series == -1 ? q : 0;
        writer.setSeries(writerSeries);
        writer.setInterleaved(reader.isInterleaved() && !autoscale);
        writer.setValidBitsPerPixel(reader.getBitsPerPixel());
        int numImages = writer.canDoStacks() ? reader.getImageCount() : 1;
        int startPlane = (int) Math.max(0, firstPlane);
        int endPlane = (int) Math.min(numImages, lastPlane);
        numImages = endPlane - startPlane;
        if (channel >= 0) {
            numImages /= reader.getEffectiveSizeC();
        }
        if (zSection >= 0) {
            numImages /= reader.getSizeZ();
        }
        if (timepoint >= 0) {
            numImages /= reader.getSizeT();
        }
        total += numImages;
        int count = 0;
        for (int i = startPlane; i < endPlane; i++) {
            int[] coords = reader.getZCTCoords(i);
            if ((zSection >= 0 && coords[0] != zSection) || (channel >= 0 && coords[1] != channel) || (timepoint >= 0 && coords[2] != timepoint)) {
                continue;
            }
            String outputName = FormatTools.getFilename(q, i, reader, out, zeroPadding);
            if (outputName.equals(FormatTools.getTileFilename(0, 0, 0, outputName))) {
                writer.setId(outputName);
                if (compression != null)
                    writer.setCompression(compression);
            } else {
                int tileNum = outputName.indexOf(FormatTools.TILE_NUM);
                int tileX = outputName.indexOf(FormatTools.TILE_X);
                int tileY = outputName.indexOf(FormatTools.TILE_Y);
                if (tileNum < 0 && (tileX < 0 || tileY < 0)) {
                    throw new FormatException("Invalid file name pattern; " + FormatTools.TILE_NUM + " or both of " + FormatTools.TILE_X + " and " + FormatTools.TILE_Y + " must be specified.");
                }
            }
            int outputIndex = 0;
            if (nextOutputIndex.containsKey(outputName)) {
                outputIndex = nextOutputIndex.get(outputName);
            }
            long s = System.currentTimeMillis();
            long m = convertPlane(writer, i, outputIndex, outputName);
            long e = System.currentTimeMillis();
            read += m - s;
            write += e - m;
            nextOutputIndex.put(outputName, outputIndex + 1);
            if (i == endPlane - 1) {
                nextOutputIndex.remove(outputName);
            }
            // log number of planes processed every second or so
            if (count == numImages - 1 || (e - timeLastLogged) / 1000 > 0) {
                int current = (count - startPlane) + 1;
                int percent = 100 * current / numImages;
                StringBuilder sb = new StringBuilder();
                sb.append("\t");
                int numSeries = last - first;
                if (numSeries > 1) {
                    sb.append("Series ");
                    sb.append(q);
                    sb.append(": converted ");
                } else
                    sb.append("Converted ");
                LOGGER.info(sb.toString() + "{}/{} planes ({}%)", new Object[] { current, numImages, percent });
                timeLastLogged = e;
            }
            count++;
        }
    }
    writer.close();
    long end = System.currentTimeMillis();
    LOGGER.info("[done]");
    // output timing results
    float sec = (end - start) / 1000f;
    long initial = mid - start;
    float readAvg = (float) read / total;
    float writeAvg = (float) write / total;
    LOGGER.info("{}s elapsed ({}+{}ms per plane, {}ms overhead)", new Object[] { sec, readAvg, writeAvg, initial });
    return true;
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) ChannelMerger(loci.formats.ChannelMerger) ImageWriter(loci.formats.ImageWriter) ChannelFiller(loci.formats.ChannelFiller) Image(ome.xml.model.Image) OMEXMLService(loci.formats.services.OMEXMLService) Pixels(ome.xml.model.Pixels) IMetadata(loci.formats.meta.IMetadata) ImageReader(loci.formats.ImageReader) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) PositiveInteger(ome.xml.model.primitives.PositiveInteger) TiffWriter(loci.formats.out.TiffWriter) InputStreamReader(java.io.InputStreamReader) DependencyException(loci.common.services.DependencyException) ChannelSeparator(loci.formats.ChannelSeparator) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) IFormatWriter(loci.formats.IFormatWriter) FileStitcher(loci.formats.FileStitcher) ServiceException(loci.common.services.ServiceException) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) BufferedReader(java.io.BufferedReader) MinMaxCalculator(loci.formats.MinMaxCalculator) MissingLibraryException(loci.formats.MissingLibraryException) Location(loci.common.Location)

Example 70 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class ImageConverter method convertTilePlane.

/**
 * Convert the specified plane as a set of tiles, using the specified writer.
 * @param writer the {@link loci.formats.IFormatWriter} to use for writing the plane
 * @param index the index of the plane to convert in the input file
 * @param outputIndex the index of the plane to convert in the output file
 * @param currentFile the file name or pattern being written to
 * @return the time at which conversion started, in milliseconds
 * @throws FormatException
 * @throws IOException
 */
private long convertTilePlane(IFormatWriter writer, int index, int outputIndex, String currentFile) throws FormatException, IOException {
    int w = reader.getOptimalTileWidth();
    int h = reader.getOptimalTileHeight();
    if (saveTileWidth > 0 && saveTileWidth <= width) {
        w = saveTileWidth;
    }
    if (saveTileHeight > 0 && saveTileHeight <= height) {
        h = saveTileHeight;
    }
    if (firstTile) {
        LOGGER.info("Tile size = {} x {}", w, h);
        firstTile = false;
    }
    int nXTiles = width / w;
    int nYTiles = height / h;
    if (nXTiles * w != width) {
        nXTiles++;
    }
    if (nYTiles * h != height) {
        nYTiles++;
    }
    IFD ifd = new IFD();
    ifd.put(IFD.TILE_WIDTH, w);
    ifd.put(IFD.TILE_LENGTH, h);
    Long m = null;
    for (int y = 0; y < nYTiles; y++) {
        for (int x = 0; x < nXTiles; x++) {
            int tileX = xCoordinate + x * w;
            int tileY = yCoordinate + y * h;
            int tileWidth = x < nXTiles - 1 ? w : width - (w * x);
            int tileHeight = y < nYTiles - 1 ? h : height - (h * y);
            byte[] buf = reader.openBytes(index, tileX, tileY, tileWidth, tileHeight);
            String tileName = FormatTools.getTileFilename(x, y, y * nXTiles + x, currentFile);
            if (!currentFile.equals(tileName)) {
                int nTileRows = getTileRows(currentFile);
                int nTileCols = getTileColumns(currentFile);
                int sizeX = nTileCols == 1 ? width : tileWidth;
                int sizeY = nTileRows == 1 ? height : tileHeight;
                MetadataRetrieve retrieve = writer.getMetadataRetrieve();
                if (retrieve instanceof MetadataStore) {
                    ((MetadataStore) retrieve).setPixelsSizeX(new PositiveInteger(sizeX), reader.getSeries());
                    ((MetadataStore) retrieve).setPixelsSizeY(new PositiveInteger(sizeY), reader.getSeries());
                }
                writer.close();
                writer.setMetadataRetrieve(retrieve);
                writer.setId(tileName);
                if (compression != null)
                    writer.setCompression(compression);
                outputIndex = 0;
                if (nextOutputIndex.containsKey(tileName)) {
                    outputIndex = nextOutputIndex.get(tileName);
                }
                nextOutputIndex.put(tileName, outputIndex + 1);
                if (nTileRows > 1) {
                    tileY = 0;
                }
                if (nTileCols > 1) {
                    tileX = 0;
                }
            }
            autoscalePlane(buf, index);
            applyLUT(writer);
            if (m == null) {
                m = System.currentTimeMillis();
            }
            if (writer instanceof TiffWriter) {
                ((TiffWriter) writer).saveBytes(outputIndex, buf, ifd, tileX, tileY, tileWidth, tileHeight);
            } else if (writer instanceof ImageWriter) {
                IFormatWriter baseWriter = ((ImageWriter) writer).getWriter(out);
                if (baseWriter instanceof TiffWriter) {
                    ((TiffWriter) baseWriter).saveBytes(outputIndex, buf, ifd, tileX, tileY, tileWidth, tileHeight);
                }
            }
        }
    }
    return m;
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) IFormatWriter(loci.formats.IFormatWriter) PositiveInteger(ome.xml.model.primitives.PositiveInteger) TiffWriter(loci.formats.out.TiffWriter) IFD(loci.formats.tiff.IFD) ImageWriter(loci.formats.ImageWriter) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Aggregations

MetadataStore (loci.formats.meta.MetadataStore)180 CoreMetadata (loci.formats.CoreMetadata)130 RandomAccessInputStream (loci.common.RandomAccessInputStream)97 Length (ome.units.quantity.Length)82 FormatException (loci.formats.FormatException)66 Timestamp (ome.xml.model.primitives.Timestamp)54 Location (loci.common.Location)51 Time (ome.units.quantity.Time)41 ArrayList (java.util.ArrayList)34 IFD (loci.formats.tiff.IFD)21 DependencyException (loci.common.services.DependencyException)16 IOException (java.io.IOException)15 TiffParser (loci.formats.tiff.TiffParser)15 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)15 ServiceFactory (loci.common.services.ServiceFactory)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 IFDList (loci.formats.tiff.IFDList)12 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)11 ServiceException (loci.common.services.ServiceException)10 Map (java.util.Map)9