Search in sources :

Example 56 with IMetadata

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

the class ReadWriteInMemory method main.

public static void main(String[] args) throws DependencyException, FormatException, IOException, ServiceException {
    if (args.length < 1) {
        System.out.println("Please specify a (small) image file.");
        System.exit(1);
    }
    String path = args[0];
    /* file-read-start */
    // read in entire file
    System.out.println("Reading file into memory from disk...");
    File inputFile = new File(path);
    int fileSize = (int) inputFile.length();
    DataInputStream in = new DataInputStream(new FileInputStream(inputFile));
    byte[] inBytes = new byte[fileSize];
    in.readFully(inBytes);
    System.out.println(fileSize + " bytes read.");
    /* file-read-end */
    /* mapping-start */
    // determine input file suffix
    String fileName = inputFile.getName();
    int dot = fileName.lastIndexOf(".");
    String suffix = dot < 0 ? "" : fileName.substring(dot);
    // map input id string to input byte array
    String inId = "inBytes" + suffix;
    Location.mapFile(inId, new ByteArrayHandle(inBytes));
    /* mapping-end */
    // read data from byte array using ImageReader
    System.out.println();
    System.out.println("Reading image data from memory...");
    /* read-start */
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    IMetadata omeMeta = service.createOMEXMLMetadata();
    ImageReader reader = new ImageReader();
    reader.setMetadataStore(omeMeta);
    reader.setId(inId);
    /* read-end */
    int seriesCount = reader.getSeriesCount();
    int imageCount = reader.getImageCount();
    int sizeX = reader.getSizeX();
    int sizeY = reader.getSizeY();
    int sizeZ = reader.getSizeZ();
    int sizeC = reader.getSizeC();
    int sizeT = reader.getSizeT();
    // output some details
    System.out.println("Series count: " + seriesCount);
    System.out.println("First series:");
    System.out.println("\tImage count = " + imageCount);
    System.out.println("\tSizeX = " + sizeX);
    System.out.println("\tSizeY = " + sizeY);
    System.out.println("\tSizeZ = " + sizeZ);
    System.out.println("\tSizeC = " + sizeC);
    System.out.println("\tSizeT = " + sizeT);
    /* out—mapping-start */
    // map output id string to output byte array
    String outId = fileName + ".ome.tif";
    ByteArrayHandle outputFile = new ByteArrayHandle();
    Location.mapFile(outId, outputFile);
    /* out—mapping-end */
    /* write—init-start */
    // write data to byte array using ImageWriter
    System.out.println();
    System.out.print("Writing planes to destination in memory: ");
    ImageWriter writer = new ImageWriter();
    writer.setMetadataRetrieve(omeMeta);
    writer.setId(outId);
    /* write—init-end */
    /* write-start */
    byte[] plane = null;
    for (int i = 0; i < imageCount; i++) {
        if (plane == null) {
            // allow reader to allocate a new byte array
            plane = reader.openBytes(i);
        } else {
            // reuse previously allocated byte array
            reader.openBytes(i, plane);
        }
        writer.saveBytes(i, plane);
        System.out.print(".");
    }
    reader.close();
    writer.close();
    System.out.println();
    byte[] outBytes = outputFile.getBytes();
    outputFile.close();
    /* write-end */
    /* flush-start */
    // flush output byte array to disk
    System.out.println();
    System.out.println("Flushing image data to disk...");
    File outFile = new File(fileName + ".ome.tif");
    DataOutputStream out = new DataOutputStream(new FileOutputStream(outFile));
    out.write(outBytes);
    out.close();
/* flush-end */
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) OMEXMLService(loci.formats.services.OMEXMLService) IMetadata(loci.formats.meta.IMetadata) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 57 with IMetadata

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

the class TiledReaderWriter 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
 * @throws DependencyException thrown if failed to create an OMEXMLService
 * @throws IOException thrown if unable to setup input or output stream for reader or writer
 * @throws FormatException thrown if invalid ID set for reader or writer or invalid tile size set
 * @throws ServiceException thrown if unable to create OME-XML meta data
 */
private void initialize() throws DependencyException, FormatException, IOException, ServiceException {
    // 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 OMETiffWriter();
    writer.setMetadataRetrieve(omexml);
    writer.setInterleaved(reader.isInterleaved());
    // set the tile size height and width for writing
    this.tileSizeX = writer.setTileSizeX(tileSizeX);
    this.tileSizeY = writer.setTileSizeY(tileSizeY);
    writer.setId(outputFile);
}
Also used : OMETiffWriter(loci.formats.out.OMETiffWriter) IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) ImageReader(loci.formats.ImageReader) OMEXMLService(loci.formats.services.OMEXMLService)

Example 58 with IMetadata

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

the class SewTiffs method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("Usage: java SewTiffs base_name channel_num [time_count]");
        System.exit(1);
    }
    String base = args[0];
    int c = Integer.parseInt(args[1]);
    int num;
    if (args.length < 3) {
        FilePattern fp = new FilePattern(new Location(base + "_C" + c + "_TP1.tiff"));
        int[] count = fp.getCount();
        num = count[count.length - 1];
    } else
        num = Integer.parseInt(args[2]);
    System.out.println("Fixing " + base + "_C" + c + "_TP<1-" + num + ">.tiff");
    TiffReader in = new TiffReader();
    TiffWriter out = new TiffWriter();
    String outId = base + "_C" + c + ".tiff";
    System.out.println("Writing " + outId);
    out.setId(outId);
    System.out.print("   ");
    boolean comment = false;
    for (int t = 0; t < num; t++) {
        String inId = base + "_C" + c + "_TP" + (t + 1) + ".tiff";
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        IMetadata meta = service.createOMEXMLMetadata();
        in.setMetadataStore(meta);
        in.setId(inId);
        out.setMetadataRetrieve(meta);
        // read first image plane
        byte[] image = in.openBytes(0);
        in.close();
        if (t == 0) {
            // read first IFD
            RandomAccessInputStream ras = new RandomAccessInputStream(inId);
            TiffParser parser = new TiffParser(ras);
            IFD ifd = parser.getFirstIFD();
            ras.close();
            // preserve TIFF comment
            String desc = ifd.getComment();
            if (desc != null) {
                ifd = new IFD();
                ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, desc);
                comment = true;
                out.saveBytes(t, image, ifd);
                System.out.print(".");
                continue;
            }
        }
        // write image plane
        out.saveBytes(t, image);
        // update status
        System.out.print(".");
        if (t % DOTS == DOTS - 1) {
            System.out.println(" " + (t + 1));
            System.out.print("   ");
        }
    }
    System.out.println();
    if (comment)
        System.out.println("OME-TIFF comment saved.");
    else
        System.out.println("No OME-TIFF comment found.");
}
Also used : TiffWriter(loci.formats.out.TiffWriter) ServiceFactory(loci.common.services.ServiceFactory) IFD(loci.formats.tiff.IFD) OMEXMLService(loci.formats.services.OMEXMLService) TiffReader(loci.formats.in.TiffReader) IMetadata(loci.formats.meta.IMetadata) TiffParser(loci.formats.tiff.TiffParser) FilePattern(loci.formats.FilePattern) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Example 59 with IMetadata

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

Example 60 with IMetadata

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

the class WriteRGBMovie method main.

public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.out.println("Please specify an output file name.");
        System.exit(1);
    }
    String id = args[0];
    // create 20 blank 512x512 image planes
    System.out.println("Creating random image planes...");
    int w = 511, h = 507, numFrames = 20, numChannels = 3;
    int pixelType = FormatTools.UINT8;
    int bpp = FormatTools.getBytesPerPixel(pixelType);
    int planeSize = h * w * numChannels * bpp;
    byte[][] img = new byte[numFrames][planeSize];
    // fill with random data
    for (int t = 0; t < numFrames; t++) {
        for (int i = 0; i < img[t].length; i += numChannels) {
            for (int c = 0; c < numChannels; c++) {
                img[t][i + c] = (byte) (256 * Math.random());
            }
        }
    }
    // create metadata object with required metadata fields
    System.out.println("Populating metadata...");
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    IMetadata meta = service.createOMEXMLMetadata();
    MetadataTools.populateMetadata(meta, 0, null, false, "XYZCT", FormatTools.getPixelTypeString(pixelType), w, h, 1, numChannels, numFrames, numChannels);
    // write image planes to disk
    System.out.print("Writing planes to '" + id + "'");
    IFormatWriter writer = new ImageWriter();
    writer.setMetadataRetrieve(meta);
    writer.setId(id);
    for (int t = 0; t < numFrames; t++) {
        System.out.print(".");
        writer.saveBytes(t, img[t]);
    }
    writer.close();
    System.out.println("Done.");
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) OMEXMLService(loci.formats.services.OMEXMLService)

Aggregations

IMetadata (loci.formats.meta.IMetadata)62 ServiceFactory (loci.common.services.ServiceFactory)35 OMEXMLService (loci.formats.services.OMEXMLService)35 FormatException (loci.formats.FormatException)19 ImageReader (loci.formats.ImageReader)19 ServiceException (loci.common.services.ServiceException)17 Test (org.testng.annotations.Test)17 DependencyException (loci.common.services.DependencyException)15 SkipException (org.testng.SkipException)15 Length (ome.units.quantity.Length)13 IOException (java.io.IOException)9 ImageWriter (loci.formats.ImageWriter)6 Time (ome.units.quantity.Time)6 PositiveInteger (ome.xml.model.primitives.PositiveInteger)6 File (java.io.File)5 IFormatReader (loci.formats.IFormatReader)5 IFormatWriter (loci.formats.IFormatWriter)5 OMETiffWriter (loci.formats.out.OMETiffWriter)5 TiffWriter (loci.formats.out.TiffWriter)5 EnumerationException (ome.xml.model.enums.EnumerationException)4