Search in sources :

Example 11 with ImageWriter

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

the class FileWriteSPW method initializeWriter.

/**
 * Set up the file writer.
 *
 * @param omexml the IMetadata object that is to be associated with the writer
 * @return true if the file writer was successfully initialized; false if an
 *   error occurred
 */
private boolean initializeWriter(IMetadata omexml) {
    // create the file writer and associate the OME-XML metadata with it
    writer = new ImageWriter();
    writer.setMetadataRetrieve(omexml);
    Exception exception = null;
    try {
        writer.setId(outputFile);
    } catch (FormatException | IOException e) {
        exception = e;
    }
    if (exception != null) {
        System.err.println("Failed to initialize file writer.");
    }
    return exception == null;
}
Also used : ImageWriter(loci.formats.ImageWriter) IOException(java.io.IOException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) FormatException(loci.formats.FormatException)

Example 12 with ImageWriter

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

the class MultiFileExportExample method main.

public static void main(String[] args) throws FormatException, IOException {
    if (args.length < 2) {
        System.out.println("Usage: java MultiFileExportExample <infile> <output file extension>");
        System.exit(1);
    }
    ImageReader reader = new ImageReader();
    IMetadata metadata;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        metadata = service.createOMEXMLMetadata();
    } catch (DependencyException exc) {
        throw new FormatException("Could not create OME-XML store.", exc);
    } catch (ServiceException exc) {
        throw new FormatException("Could not create OME-XML store.", exc);
    }
    reader.setMetadataStore(metadata);
    reader.setId(args[0]);
    ImageWriter writer = new ImageWriter();
    writer.setMetadataRetrieve(metadata);
    String baseFile = args[0].substring(0, args[0].lastIndexOf("."));
    writer.setId(baseFile + "_s0_z0" + args[1]);
    for (int series = 0; series < reader.getSeriesCount(); series++) {
        reader.setSeries(series);
        writer.setSeries(series);
        int planesPerFile = reader.getImageCount() / reader.getSizeZ();
        for (int z = 0; z < reader.getSizeZ(); z++) {
            String file = baseFile + "_s" + series + "_z" + z + args[1];
            writer.changeOutputFile(file);
            for (int image = 0; image < planesPerFile; image++) {
                int[] zct = FormatTools.getZCTCoords(reader.getDimensionOrder(), 1, reader.getEffectiveSizeC(), reader.getSizeT(), planesPerFile, image);
                int index = FormatTools.getIndex(reader, z, zct[1], zct[2]);
                writer.saveBytes(image, reader.openBytes(index));
            }
        }
    }
    reader.close();
    writer.close();
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) ImageWriter(loci.formats.ImageWriter) ImageReader(loci.formats.ImageReader) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Example 13 with ImageWriter

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

the class ConversionTest method testCompressDecompress.

private void testCompressDecompress(String ext, String compression, boolean lossy, int rgbChannels, int seriesCount, String pixelType, boolean littleEndian) throws Exception {
    File tmp = File.createTempFile("conversionTest", ext);
    tmp.deleteOnExit();
    ImageWriter writer = new ImageWriter();
    writer.setMetadataRetrieve(createMetadata(pixelType, rgbChannels, seriesCount, littleEndian));
    writer.setId(tmp.getAbsolutePath());
    int bytes = FormatTools.getBytesPerPixel(pixelType);
    byte[] plane = getPlane(WIDTH, HEIGHT, bytes * rgbChannels);
    Plane originalPlane = new Plane(plane, littleEndian, !writer.isInterleaved(), rgbChannels, pixelType);
    for (int s = 0; s < seriesCount; s++) {
        writer.setSeries(s);
        writer.saveBytes(0, plane);
    }
    writer.close();
    ImageReader reader = new ImageReader();
    reader.setId(tmp.getAbsolutePath());
    assertEquals(reader.getSeriesCount(), seriesCount);
    for (int s = 0; s < seriesCount; s++) {
        reader.setSeries(s);
        assertEquals(reader.getSizeC(), rgbChannels);
        assertTrue(reader.getImageCount() == rgbChannels || reader.isRGB());
        byte[] readPlane = reader.openBytes(0);
        if (!lossy) {
            Plane newPlane = new Plane(readPlane, reader.isLittleEndian(), !reader.isInterleaved(), reader.getRGBChannelCount(), FormatTools.getPixelTypeString(reader.getPixelType()));
            assertTrue(originalPlane.equals(newPlane), tmp.getAbsolutePath());
        }
    }
    reader.close();
}
Also used : ImageWriter(loci.formats.ImageWriter) ImageReader(loci.formats.ImageReader) File(java.io.File)

Example 14 with ImageWriter

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

the class FormatWriterTest method getWriterList.

// -- Data provider --
@DataProvider(name = "getWriterList")
public Object[][] getWriterList() {
    IFormatWriter[] writers = new ImageWriter().getWriters();
    List tmp = new ArrayList();
    for (int i = 0; i < writers.length; i++) {
        String[] compressionTypes = writers[i].getCompressionTypes();
        if (compressionTypes == null) {
            try {
                IFormatWriter w = (IFormatWriter) writers[i].getClass().newInstance();
                tmp.add(w);
            } catch (InstantiationException ie) {
            } catch (IllegalAccessException iae) {
            }
            continue;
        }
        for (int q = 0; q < compressionTypes.length; q++) {
            try {
                IFormatWriter w = (IFormatWriter) writers[i].getClass().newInstance();
                if (DataTools.containsValue(w.getPixelTypes(compressionTypes[q]), reader.getPixelType())) {
                    w.setCompression(compressionTypes[q]);
                    tmp.add(w);
                }
            } catch (FormatException fe) {
            } catch (InstantiationException ie) {
            } catch (IllegalAccessException iae) {
            }
        }
    }
    IFormatWriter[][] writersToUse = new IFormatWriter[tmp.size()][1];
    for (int i = 0; i < tmp.size(); i++) {
        writersToUse[i][0] = (IFormatWriter) tmp.get(i);
    }
    return writersToUse;
}
Also used : IFormatWriter(loci.formats.IFormatWriter) ImageWriter(loci.formats.ImageWriter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) FormatException(loci.formats.FormatException) DataProvider(org.testng.annotations.DataProvider)

Example 15 with ImageWriter

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

the class TiledExportExample method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("Usage: java TiledExportExample <infile> <outfile>");
        System.exit(1);
    }
    ImageReader reader = new ImageReader();
    ImageWriter writer = new ImageWriter();
    IMetadata meta;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.createOMEXMLMetadata();
    } catch (DependencyException exc) {
        throw new FormatException("Could not create OME-XML store.", exc);
    } catch (ServiceException exc) {
        throw new FormatException("Could not create OME-XML store.", exc);
    }
    reader.setMetadataStore(meta);
    reader.setId(args[0]);
    writer.setMetadataRetrieve(meta);
    writer.setId(args[1]);
    for (int series = 0; series < reader.getSeriesCount(); series++) {
        reader.setSeries(series);
        writer.setSeries(series);
        for (int image = 0; image < reader.getImageCount(); image++) {
            for (int row = 0; row < 2; row++) {
                for (int col = 0; col < 2; col++) {
                    int w = reader.getSizeX() / 2;
                    int h = reader.getSizeY() / 2;
                    int x = col * w;
                    int y = row * h;
                    /* debug */
                    System.out.println("[" + x + ", " + y + ", " + w + ", " + h + "]");
                    byte[] buf = reader.openBytes(image, x, y, w, h);
                    writer.saveBytes(image, buf, x, y, w, h);
                }
            }
        }
    }
    reader.close();
    writer.close();
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) ImageWriter(loci.formats.ImageWriter) ImageReader(loci.formats.ImageReader) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Aggregations

ImageWriter (loci.formats.ImageWriter)16 FormatException (loci.formats.FormatException)10 DependencyException (loci.common.services.DependencyException)8 ServiceException (loci.common.services.ServiceException)8 ImageReader (loci.formats.ImageReader)7 IOException (java.io.IOException)6 ServiceFactory (loci.common.services.ServiceFactory)6 IMetadata (loci.formats.meta.IMetadata)6 OMEXMLService (loci.formats.services.OMEXMLService)6 IFormatWriter (loci.formats.IFormatWriter)5 EnumerationException (ome.xml.model.enums.EnumerationException)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 PositiveInteger (ome.xml.model.primitives.PositiveInteger)3 List (java.util.List)2 FileFilter (javax.swing.filechooser.FileFilter)2 FileStitcher (loci.formats.FileStitcher)2 IFormatReader (loci.formats.IFormatReader)2 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)2 MetadataStore (loci.formats.meta.MetadataStore)2