Search in sources :

Example 56 with OMEXMLService

use of loci.formats.services.OMEXMLService 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 57 with OMEXMLService

use of loci.formats.services.OMEXMLService 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 58 with OMEXMLService

use of loci.formats.services.OMEXMLService 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)

Example 59 with OMEXMLService

use of loci.formats.services.OMEXMLService in project digilib by robcast.

the class BioFormatsDocuImage method loadImage.

@Override
public void loadImage(ImageInput ii) throws FileOpException {
    logger.debug("loadImage: " + ii);
    this.input = ii;
    reader = new ImageReader();
    try {
        // construct the object that stores OME-XML metadata
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.createOMEXMLMetadata();
        // set up the reader and associate it with the input file
        reader = new ImageReader();
        reader.setMetadataStore(meta);
        reader.setId(ii.getFile().getAbsolutePath());
        BufferedImageReader biReader = BufferedImageReader.makeBufferedImageReader(reader);
        img = biReader.openImage(0);
        logger.debug("image loaded: " + img);
    } catch (FormatException e) {
        throw new FileOpException("Unable to load image format: " + e);
    } catch (IOException e) {
        throw new FileOpException("Unable to load image file: " + e);
    } catch (ServiceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DependencyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) FileOpException(digilib.io.FileOpException) BufferedImageReader(loci.formats.gui.BufferedImageReader) IOException(java.io.IOException) ImageReader(loci.formats.ImageReader) BufferedImageReader(loci.formats.gui.BufferedImageReader) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Aggregations

OMEXMLService (loci.formats.services.OMEXMLService)59 ServiceFactory (loci.common.services.ServiceFactory)57 IMetadata (loci.formats.meta.IMetadata)35 DependencyException (loci.common.services.DependencyException)28 ServiceException (loci.common.services.ServiceException)26 FormatException (loci.formats.FormatException)23 ImageReader (loci.formats.ImageReader)21 PositiveInteger (ome.xml.model.primitives.PositiveInteger)12 IOException (java.io.IOException)11 MetadataStore (loci.formats.meta.MetadataStore)7 File (java.io.File)6 Location (loci.common.Location)6 ImageWriter (loci.formats.ImageWriter)6 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)6 Length (ome.units.quantity.Length)6 BeforeClass (org.testng.annotations.BeforeClass)6 IFormatWriter (loci.formats.IFormatWriter)5 MissingLibraryException (loci.formats.MissingLibraryException)5 OMETiffWriter (loci.formats.out.OMETiffWriter)5 ArrayList (java.util.ArrayList)4