Search in sources :

Example 26 with ByteArrayHandle

use of loci.common.ByteArrayHandle 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)

Aggregations

ByteArrayHandle (loci.common.ByteArrayHandle)26 RandomAccessInputStream (loci.common.RandomAccessInputStream)18 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)7 IOException (java.io.IOException)6 FormatException (loci.formats.FormatException)6 CodecOptions (loci.formats.codec.CodecOptions)5 CoreMetadata (loci.formats.CoreMetadata)4 TiffSaver (loci.formats.tiff.TiffSaver)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Location (loci.common.Location)3 DependencyException (loci.common.services.DependencyException)3 ServiceFactory (loci.common.services.ServiceFactory)3 IFD (loci.formats.tiff.IFD)3 TiffParser (loci.formats.tiff.TiffParser)3 Length (ome.units.quantity.Length)3 Test (org.testng.annotations.Test)3 ArrayList (java.util.ArrayList)2 IFormatReader (loci.formats.IFormatReader)2 Codec (loci.formats.codec.Codec)2 JPEGCodec (loci.formats.codec.JPEGCodec)2