Search in sources :

Example 6 with MetadataRetrieve

use of loci.formats.meta.MetadataRetrieve 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)

Example 7 with MetadataRetrieve

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

the class ImageInfo method printOMEXML.

public void printOMEXML() throws MissingLibraryException, ServiceException {
    LOGGER.info("");
    MetadataStore ms = reader.getMetadataStore();
    if (baseReader instanceof ImageReader) {
        baseReader = ((ImageReader) baseReader).getReader();
    }
    OMEXMLService service;
    try {
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(OMEXMLService.class);
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    }
    String version = service.getOMEXMLVersion(ms);
    if (version == null)
        LOGGER.info("Generating OME-XML");
    else {
        LOGGER.info("Generating OME-XML (schema version {})", version);
    }
    if (ms instanceof MetadataRetrieve) {
        if (omexmlOnly) {
            DebugTools.setRootLevel("INFO");
        }
        String xml = service.getOMEXML((MetadataRetrieve) ms);
        LOGGER.info("{}", XMLTools.indentXML(xml, xmlSpaces, true));
        if (omexmlOnly) {
            DebugTools.setRootLevel("OFF");
        }
    } else {
        LOGGER.info("The metadata could not be converted to OME-XML.");
        if (omexmlVersion == null) {
            LOGGER.info("The OME-XML Java library is probably not available.");
        } else {
            LOGGER.info("{} is probably not a legal schema version.", omexmlVersion);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) ServiceFactory(loci.common.services.ServiceFactory) MissingLibraryException(loci.formats.MissingLibraryException) ImageReader(loci.formats.ImageReader) BufferedImageReader(loci.formats.gui.BufferedImageReader) DependencyException(loci.common.services.DependencyException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService)

Example 8 with MetadataRetrieve

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

the class FormatTools method convert.

/**
 * Convenience method for writing all of the images and metadata obtained
 * from the specified IFormatReader into the specified IFormatWriter.
 *
 * It is required that setId(String) be called on the IFormatReader
 * object before it is passed to convert(...).  setMetadataStore(...)
 * should also have been called with an appropriate instance of IMetadata.
 *
 * The setId(String) method must not be called on the IFormatWriter
 * object; this is taken care of internally.  Additionally, the
 * setMetadataRetrieve(...) method in IFormatWriter should not be called.
 *
 * @param input the pre-initialized IFormatReader used for reading data.
 * @param output the uninitialized IFormatWriter used for writing data.
 * @param outputFile the full path name of the output file to be created.
 * @throws FormatException if there is a general problem reading from or
 * writing to one of the files.
 * @throws IOException if there is an I/O-related error.
 */
public static void convert(IFormatReader input, IFormatWriter output, String outputFile) throws FormatException, IOException {
    MetadataStore store = input.getMetadataStore();
    MetadataRetrieve meta = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.asRetrieve(store);
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    }
    output.setMetadataRetrieve(meta);
    output.setId(outputFile);
    for (int series = 0; series < input.getSeriesCount(); series++) {
        input.setSeries(series);
        output.setSeries(series);
        byte[] buf = new byte[getPlaneSize(input)];
        for (int image = 0; image < input.getImageCount(); image++) {
            input.openBytes(image, buf);
            output.saveBytes(image, buf);
        }
    }
    input.close();
    output.close();
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService)

Example 9 with MetadataRetrieve

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

the class LociFunctions method getPixelsPhysicalSizeX.

public void getPixelsPhysicalSizeX(Double[] sizeX) {
    int imageIndex = r.getSeries();
    MetadataRetrieve retrieve = (MetadataRetrieve) r.getMetadataStore();
    Length x = retrieve.getPixelsPhysicalSizeX(imageIndex);
    if (x != null) {
        sizeX[0] = x.value(UNITS.MICROMETER).doubleValue();
    }
    if (sizeX[0] == null)
        sizeX[0] = new Double(Double.NaN);
}
Also used : Length(ome.units.quantity.Length) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 10 with MetadataRetrieve

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

the class LociFunctions method getPixelsTimeIncrement.

public void getPixelsTimeIncrement(Double[] sizeT) {
    int imageIndex = r.getSeries();
    MetadataRetrieve retrieve = (MetadataRetrieve) r.getMetadataStore();
    sizeT[0] = retrieve.getPixelsTimeIncrement(imageIndex).value(UNITS.SECOND).doubleValue();
    if (sizeT[0] == null)
        sizeT[0] = new Double(Double.NaN);
}
Also used : MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Aggregations

MetadataRetrieve (loci.formats.meta.MetadataRetrieve)64 FormatException (loci.formats.FormatException)11 MetadataStore (loci.formats.meta.MetadataStore)11 Length (ome.units.quantity.Length)10 ServiceFactory (loci.common.services.ServiceFactory)8 DependencyException (loci.common.services.DependencyException)7 OMEXMLService (loci.formats.services.OMEXMLService)6 RandomAccessInputStream (loci.common.RandomAccessInputStream)5 Time (ome.units.quantity.Time)5 PositiveInteger (ome.xml.model.primitives.PositiveInteger)5 ServiceException (loci.common.services.ServiceException)4 MissingLibraryException (loci.formats.MissingLibraryException)4 Test (org.testng.annotations.Test)4 IndexColorModel (java.awt.image.IndexColorModel)3 Location (loci.common.Location)3 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)3 ImageReader (loci.formats.ImageReader)3 BufferedImage (java.awt.image.BufferedImage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2