Search in sources :

Example 36 with ServiceFactory

use of loci.common.services.ServiceFactory 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 37 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class FormatWriterTest method testWriterConsistency.

// -- Tests --
@Test(groups = { "all" }, dataProvider = "getWriterList")
public void testWriterConsistency(IFormatWriter writer) {
    String testName = TestTools.shortClassName(writer) + " " + writer.getCompression() + " testWriterConsistency";
    boolean success = true;
    String msg = null;
    try {
        reader.close();
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        reader.setMetadataStore(service.createOMEXMLMetadata());
        reader.setId(id);
        int type = reader.getPixelType();
        if (!writer.isSupportedType(type)) {
            success = true;
            result(testName, success, msg);
            return;
        }
        config = configTree.get(id);
        String prefix = id.substring(id.lastIndexOf(File.separator) + 1, id.lastIndexOf("."));
        // will throw an exception
        while (prefix.length() < 3) prefix = "x" + prefix;
        String suffix = "." + writer.getSuffixes()[0];
        File tmpFile = File.createTempFile(prefix, suffix);
        tmpFile.deleteOnExit();
        String convertedFile = tmpFile.getAbsolutePath();
        IMetadata meta = (IMetadata) reader.getMetadataStore();
        writer.close();
        writer.setMetadataRetrieve((MetadataRetrieve) meta);
        // convert the input file
        writer.setId(convertedFile);
        int seriesCount = writer.canDoStacks() ? reader.getSeriesCount() : 1;
        for (int series = 0; series < seriesCount; series++) {
            reader.setSeries(series);
            writer.setSeries(series);
            int imageCount = writer.canDoStacks() ? reader.getImageCount() : 1;
            for (int image = 0; image < imageCount; image++) {
                writer.saveBytes(image, reader.openBytes(image));
            }
        }
        writer.close();
        // verify that the dimensions are accurate
        convertedReader.setId(convertedFile);
        boolean seriesMatch = convertedReader.getSeriesCount() == config.getSeriesCount();
        boolean expectRGB = config.isRGB();
        int expectedCount = config.getSizeZ() * config.getSizeT() * (expectRGB ? 1 : config.getSizeC());
        boolean imageMatch = convertedReader.getImageCount() == expectedCount;
        if (!seriesMatch && writer.canDoStacks()) {
            int totalImages = 0;
            for (int i = 0; i < reader.getSeriesCount(); i++) {
                reader.setSeries(i);
                totalImages += reader.getImageCount();
            }
            reader.setSeries(0);
            if (convertedReader.getImageCount() != totalImages) {
                success = false;
                msg = "Series counts do not match (found " + convertedReader.getSeriesCount() + ", expected " + config.getSeriesCount() + ")";
            } else
                imageMatch = true;
        }
        if (success) {
            for (int series = 0; series < seriesCount; series++) {
                if (series >= convertedReader.getSeriesCount()) {
                    break;
                }
                convertedReader.setSeries(series);
                config.setSeries(series);
                int expectedX = config.getSizeX();
                int expectedY = config.getSizeY();
                expectRGB = config.isRGB();
                if (TestTools.shortClassName(writer).equals("OMEXMLWriter")) {
                    expectRGB = false;
                } else if (TestTools.shortClassName(writer).equals("JPEGWriter")) {
                    expectRGB = expectRGB || config.isIndexed();
                }
                int expectedPixelType = FormatTools.pixelTypeFromString(config.getPixelType());
                expectedCount = config.getSizeZ() * config.getSizeT() * (expectRGB ? 1 : config.getSizeC());
                String expectedMD5 = config.getMD5();
                int x = convertedReader.getSizeX();
                int y = convertedReader.getSizeY();
                int count = convertedReader.getImageCount();
                boolean rgb = convertedReader.isRGB();
                int pixelType = convertedReader.getPixelType();
                boolean isQuicktime = TestTools.shortClassName(writer).equals("QTWriter");
                String md5 = TestTools.md5(convertedReader.openBytes(0));
                if (msg == null)
                    msg = checkMismatch(x, expectedX, series, "X");
                if (msg == null)
                    msg = checkMismatch(y, expectedY, series, "Y");
                if (msg == null && writer.canDoStacks() && !imageMatch) {
                    msg = checkMismatch(count, expectedCount, series, "Image count");
                }
                if (msg == null && !isQuicktime) {
                    msg = checkMismatch(rgb, expectRGB, series, "RGB");
                }
                if (msg == null && !isQuicktime) {
                    msg = checkMismatch(pixelType, expectedPixelType, series, "Pixel type");
                }
                if (msg == null && isLosslessWriter(writer) && config.isRGB() == expectRGB) {
                    msg = checkMismatch(md5, expectedMD5, series, "Pixels hash");
                }
                success = msg == null;
                if (!success)
                    break;
            }
        }
        convertedReader.close();
    } catch (Throwable t) {
        LOGGER.info("", t);
        success = false;
    }
    result(testName, success, msg);
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) File(java.io.File) OMEXMLService(loci.formats.services.OMEXMLService) Test(org.testng.annotations.Test)

Example 38 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class Jpeg2000GrindTest method initializeWriter.

/**
 * Initializes the writer.
 * @param output The file where to write the compressed data.
 * @param compression The compression to use.
 * @param bigTiff Pass <code>true</code> to set the <code>bigTiff</code>
 * flag, <code>false</code> otherwise.
 * @throws Exception Thrown if an error occurred.
 */
private void initializeWriter(String output, String compression, boolean bigTiff) throws Exception {
    ServiceFactory sf = new ServiceFactory();
    OMEXMLService service = sf.getInstance(OMEXMLService.class);
    IMetadata metadata = service.createOMEXMLMetadata();
    metadata.setImageID("Image:0", 0);
    metadata.setPixelsID("Pixels:0", 0);
    metadata.setPixelsBinDataBigEndian(true, 0, 0);
    metadata.setPixelsDimensionOrder(DimensionOrder.XYZCT, 0);
    metadata.setPixelsType(ome.xml.model.enums.PixelType.fromString(PIXEL_TYPE), 0);
    metadata.setPixelsSizeX(new PositiveInteger(SIZE_X), 0);
    metadata.setPixelsSizeY(new PositiveInteger(SIZE_Y), 0);
    metadata.setPixelsSizeZ(new PositiveInteger(1), 0);
    metadata.setPixelsSizeC(new PositiveInteger(1), 0);
    metadata.setPixelsSizeT(new PositiveInteger(SIZE_Z * SIZE_C * SIZE_T), 0);
    metadata.setChannelID("Channel:0", 0, 0);
    metadata.setChannelSamplesPerPixel(new PositiveInteger(1), 0, 0);
    writer = new TiffWriter();
    writer.setMetadataRetrieve(metadata);
    writer.setCompression(compression);
    writer.setWriteSequentially(false);
    writer.setInterleaved(true);
    writer.setBigTiff(bigTiff);
    writer.setId(output);
    bytesPerPixel = FormatTools.getBytesPerPixel(PIXEL_TYPE);
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) IMetadata(loci.formats.meta.IMetadata) TiffWriter(loci.formats.out.TiffWriter) ServiceFactory(loci.common.services.ServiceFactory) OMEXMLService(loci.formats.services.OMEXMLService)

Example 39 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class MetadataConfigurableTest method testSetId.

@Test
public void testSetId() throws FormatException, IOException {
    long t0 = System.currentTimeMillis();
    pixelsOnly.setId(id);
    assertEquals(MetadataLevel.MINIMUM, pixelsOnly.getMetadataOptions().getMetadataLevel());
    long t1 = System.currentTimeMillis();
    all.setId(id);
    assertEquals(MetadataLevel.ALL, all.getMetadataOptions().getMetadataLevel());
    assertFalse(0 == all.getSeriesMetadata().size() + all.getGlobalMetadata().size());
    long t2 = System.currentTimeMillis();
    System.err.println(String.format("Pixels only: %d -- All: %d", t1 - t0, t2 - t1));
    IMetadata metadata = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        metadata = service.createOMEXMLMetadata();
        noOverlays.setMetadataStore(metadata);
    } catch (Exception e) {
        throw new FormatException("Cannot initialize OMEXML metadata store");
    }
    noOverlays.setId(id);
    assertEquals(MetadataLevel.NO_OVERLAYS, noOverlays.getMetadataOptions().getMetadataLevel());
    assertEquals(metadata.getROICount(), 0);
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException) SkipException(org.testng.SkipException) IOException(java.io.IOException) FormatException(loci.formats.FormatException) Test(org.testng.annotations.Test)

Example 40 with ServiceFactory

use of loci.common.services.ServiceFactory in project bioformats by openmicroscopy.

the class FileConvert method initialize.

/**
 * Set up the file reader and writer, ensuring that the input file is
 * associated with the reader and the output file is associated with the
 * writer.
 *
 * @return true if the reader and writer were successfully set up, or false
 *   if an error occurred
 */
private boolean initialize() {
    Exception exception = null;
    try {
        // construct the object that stores OME-XML metadata
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        IMetadata omexml = service.createOMEXMLMetadata();
        // set up the reader and associate it with the input file
        reader = new ImageReader();
        reader.setMetadataStore(omexml);
        reader.setId(inputFile);
        // set up the writer and associate it with the output file
        writer = new ImageWriter();
        writer.setMetadataRetrieve(omexml);
        writer.setInterleaved(reader.isInterleaved());
        writer.setId(outputFile);
    } catch (FormatException e) {
        exception = e;
    } catch (IOException e) {
        exception = e;
    } catch (DependencyException e) {
        exception = e;
    } catch (ServiceException e) {
        exception = e;
    }
    if (exception != null) {
        System.err.println("Failed to initialize files.");
        exception.printStackTrace();
    }
    return exception == null;
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) ImageWriter(loci.formats.ImageWriter) IOException(java.io.IOException) ImageReader(loci.formats.ImageReader) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) ServiceException(loci.common.services.ServiceException) IOException(java.io.IOException) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Aggregations

ServiceFactory (loci.common.services.ServiceFactory)94 OMEXMLService (loci.formats.services.OMEXMLService)73 DependencyException (loci.common.services.DependencyException)49 FormatException (loci.formats.FormatException)38 IMetadata (loci.formats.meta.IMetadata)35 ServiceException (loci.common.services.ServiceException)30 ImageReader (loci.formats.ImageReader)23 MissingLibraryException (loci.formats.MissingLibraryException)15 BeforeMethod (org.testng.annotations.BeforeMethod)15 MetadataStore (loci.formats.meta.MetadataStore)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)14 IOException (java.io.IOException)13 CoreMetadata (loci.formats.CoreMetadata)13 Length (ome.units.quantity.Length)12 Location (loci.common.Location)11 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)10 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)8 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)8 BeforeClass (org.testng.annotations.BeforeClass)8