Search in sources :

Example 91 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class AmiraReader method initFile.

/* (non-Javadoc)
   * @see loci.formats.FormatReader#initFile(java.lang.String)
   */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    AmiraParameters parameters = new AmiraParameters(in);
    offsetOfFirstStream = in.getFilePointer();
    LOGGER.info("Populating metadata hashtable");
    addGlobalMeta("Image width", parameters.width);
    addGlobalMeta("Image height", parameters.height);
    addGlobalMeta("Number of planes", parameters.depth);
    addGlobalMeta("Bits per pixel", 8);
    LOGGER.info("Populating core metadata");
    int channelIndex = 1;
    while (parameters.getStreams().get("@" + channelIndex) != null) {
        channelIndex++;
    }
    CoreMetadata m = core.get(0);
    m.sizeX = parameters.width;
    m.sizeY = parameters.height;
    m.sizeZ = parameters.depth;
    m.sizeT = 1;
    m.sizeC = channelIndex - 1;
    m.imageCount = getSizeZ() * getSizeC();
    m.littleEndian = parameters.littleEndian;
    m.dimensionOrder = "XYZCT";
    String streamType = parameters.streamTypes[0].toLowerCase();
    if (streamType.equals("byte")) {
        m.pixelType = FormatTools.UINT8;
    } else if (streamType.equals("short")) {
        m.pixelType = FormatTools.INT16;
        addGlobalMeta("Bits per pixel", 16);
    } else if (streamType.equals("ushort")) {
        m.pixelType = FormatTools.UINT16;
        addGlobalMeta("Bits per pixel", 16);
    } else if (streamType.equals("int")) {
        m.pixelType = FormatTools.INT32;
        addGlobalMeta("Bits per pixel", 32);
    } else if (streamType.equals("float")) {
        m.pixelType = FormatTools.FLOAT;
        addGlobalMeta("Bits per pixel", 32);
    } else {
        LOGGER.warn("Assuming data type is byte");
        m.pixelType = FormatTools.UINT8;
    }
    LOGGER.info("Populating metadata store");
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    // The bounding box is the range of the centre of the voxels
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        double pixelWidth = (double) (parameters.x1 - parameters.x0) / (parameters.width - 1);
        double pixelHeight = (double) (parameters.y1 - parameters.y0) / (parameters.height - 1);
        // TODO - what is correct setting if single slice?
        double pixelDepth = (double) (parameters.z1 - parameters.z0) / (parameters.depth - 1);
        // Amira does not have a standard form for encoding units, so we just
        // have to assume microns for microscopy data
        addGlobalMeta("Pixels per meter (X)", 1e6 / pixelWidth);
        addGlobalMeta("Pixels per meter (Y)", 1e6 / pixelHeight);
        addGlobalMeta("Pixels per meter (Z)", 1e6 / pixelDepth);
        Length sizeX = FormatTools.getPhysicalSizeX(pixelWidth);
        Length sizeY = FormatTools.getPhysicalSizeY(pixelHeight);
        Length sizeZ = FormatTools.getPhysicalSizeZ(pixelDepth);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
        if (sizeZ != null) {
            store.setPixelsPhysicalSizeZ(sizeZ, 0);
        }
    }
    ascii = parameters.ascii;
    ArrayList streamData = (ArrayList) parameters.getStreams().get("@1");
    if (streamData.size() > 2) {
        compression = (String) streamData.get(2);
    }
    initPlaneReader();
    hasPlaneReader = planeReader != null;
    addGlobalMeta("Compression", compression);
    Map params = (Map) parameters.getMap().get("Parameters");
    if (params != null) {
        Map materials = (Map) params.get("Materials");
        if (materials != null) {
            lut = getLookupTable(materials);
            m.indexed = true;
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) ArrayList(java.util.ArrayList) AmiraParameters(loci.formats.tools.AmiraParameters) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) Map(java.util.Map)

Example 92 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class BDReader method readMetaData.

private IniList readMetaData(String id) throws IOException {
    IniParser parser = new IniParser();
    RandomAccessInputStream idStream = new RandomAccessInputStream(id);
    IniList exp = parser.parseINI(new BufferedReader(new InputStreamReader(idStream, Constants.ENCODING)));
    IniList plate = null;
    IniList xyz = null;
    // Read Plate File
    for (String filename : metadataFiles) {
        if (checkSuffix(filename, "plt")) {
            RandomAccessInputStream stream = new RandomAccessInputStream(filename);
            plate = parser.parseINI(new BufferedReader(new InputStreamReader(stream, Constants.ENCODING)));
            stream.close();
        } else if (checkSuffix(filename, "xyz")) {
            RandomAccessInputStream stream = new RandomAccessInputStream(filename);
            xyz = parser.parseINI(new BufferedReader(new InputStreamReader(stream, Constants.ENCODING)));
            stream.close();
        } else if (filename.endsWith("RoiSummary.txt")) {
            roiFile = filename;
            if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
                RandomAccessInputStream s = new RandomAccessInputStream(filename);
                String line = s.readLine().trim();
                while (!line.endsWith(".adf\"")) {
                    line = s.readLine().trim();
                }
                plateName = line.substring(line.indexOf(':')).trim();
                plateName = plateName.replace('/', File.separatorChar);
                plateName = plateName.replace('\\', File.separatorChar);
                for (int i = 0; i < 3; i++) {
                    plateName = plateName.substring(0, plateName.lastIndexOf(File.separator));
                }
                plateName = plateName.substring(plateName.lastIndexOf(File.separator) + 1);
                s.close();
            }
        }
    }
    if (plate == null)
        throw new IOException("No Plate File");
    IniTable plateType = plate.getTable("PlateType");
    if (plateName == null) {
        plateName = plateType.get("Brand");
    }
    plateDescription = plateType.get("Brand") + " " + plateType.get("Description");
    int nWells = Integer.parseInt(plateType.get("Wells"));
    if (nWells == 96) {
        wellRows = 8;
        wellCols = 12;
    } else if (nWells == 384) {
        wellRows = 16;
        wellCols = 24;
    }
    for (String filename : rootList) {
        String name = new Location(filename).getName();
        if (name.startsWith("Well ")) {
            wellLabels.add(name.split("\\s|\\.")[1]);
        }
    }
    IniTable imageTable = exp.getTable("Image");
    boolean montage = imageTable.get("Montaged").equals("1");
    if (montage) {
        fieldRows = Integer.parseInt(imageTable.get("TilesY"));
        fieldCols = Integer.parseInt(imageTable.get("TilesX"));
    } else {
        fieldRows = 1;
        fieldCols = 1;
    }
    core.clear();
    int coresize = wellLabels.size() * fieldRows * fieldCols;
    CoreMetadata ms0 = new CoreMetadata();
    core.add(ms0);
    for (int i = 1; i < coresize; i++) {
        core.add(new CoreMetadata());
    }
    ms0.sizeC = Integer.parseInt(exp.getTable("General").get("Dyes"));
    ms0.bitsPerPixel = Integer.parseInt(exp.getTable("Camera").get("BitdepthUsed"));
    IniTable dyeTable = exp.getTable("Dyes");
    for (int i = 1; i <= getSizeC(); i++) {
        channelNames.add(dyeTable.get(Integer.toString(i)));
    }
    if (xyz != null) {
        IniTable zTable = xyz.getTable("Z1Axis");
        boolean zEnabled = "1".equals(zTable.get("Z1AxisEnabled")) && "1".equals(zTable.get("Z1AxisMode"));
        if (zEnabled) {
            ms0.sizeZ = (int) Double.parseDouble(zTable.get("Z1AxisValue")) + 1;
        } else {
            ms0.sizeZ = 1;
        }
    } else {
        ms0.sizeZ = 1;
    }
    // Count Images
    ms0.sizeT = 0;
    for (String channelName : channelNames) {
        int images = 0;
        for (String filename : wellList.get(1)) {
            if (filename.startsWith(channelName) && filename.endsWith(".tif")) {
                images++;
            }
        }
        if (images > getImageCount()) {
            ms0.sizeT = images / getSizeZ();
            ms0.imageCount = getSizeZ() * getSizeT() * channelNames.size();
        }
    }
    idStream.close();
    return exp;
}
Also used : IniParser(loci.common.IniParser) InputStreamReader(java.io.InputStreamReader) IniList(loci.common.IniList) IniTable(loci.common.IniTable) BufferedReader(java.io.BufferedReader) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) CoreMetadata(loci.formats.CoreMetadata) Location(loci.common.Location)

Example 93 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class BDReader method getTimestamp.

private long getTimestamp(String file) throws FormatException, IOException {
    RandomAccessInputStream s = new RandomAccessInputStream(file, 16);
    TiffParser parser = new TiffParser(s);
    parser.setDoCaching(false);
    IFD firstIFD = parser.getFirstIFD();
    if (firstIFD != null) {
        TiffIFDEntry timestamp = (TiffIFDEntry) firstIFD.get(IFD.DATE_TIME);
        if (timestamp != null) {
            String stamp = parser.getIFDValue(timestamp).toString();
            s.close();
            stamp = DateTools.formatDate(stamp, BaseTiffReader.DATE_FORMATS, ".");
            Timestamp t = Timestamp.valueOf(stamp);
            // NPE if invalid input.
            return t.asInstant().getMillis();
        }
    }
    s.close();
    return new Location(file).lastModified();
}
Also used : TiffIFDEntry(loci.formats.tiff.TiffIFDEntry) IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) Timestamp(ome.xml.model.primitives.Timestamp) Location(loci.common.Location)

Example 94 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class TiffPixelsTest method readSavedPlane.

// -- Helper method --
private byte[] readSavedPlane() throws FormatException, IOException {
    ByteArrayHandle savedData = new ByteArrayHandle();
    RandomAccessOutputStream out = new RandomAccessOutputStream(savedData);
    RandomAccessInputStream in = new RandomAccessInputStream(savedData);
    TiffSaver saver = new TiffSaver(out, savedData);
    // saver.setInputStream(in);
    saver.writeImage(data, ifd, 0, FormatTools.UINT16, false);
    out.close();
    TiffParser parser = new TiffParser(in);
    byte[] plane = new byte[data.length];
    parser.getSamples(ifd, plane);
    in.close();
    return plane;
}
Also used : RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 95 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class TiffComment method overwriteComment.

/**
 * Overwrites the comment.
 *
 * @param file The path to the file to handle.
 * @param comment The new comment to write.
 */
private static void overwriteComment(String file, String comment) {
    RandomAccessInputStream in = null;
    RandomAccessOutputStream out = null;
    try {
        in = new RandomAccessInputStream(file);
        out = new RandomAccessOutputStream(file);
        TiffSaver saver = new TiffSaver(out, file);
        saver.overwriteComment(in, comment);
    } catch (Exception e) {
        System.out.println(e.toString());
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (Exception e) {
        }
        try {
            if (out != null)
                out.close();
        } catch (Exception e) {
        }
    }
}
Also used : RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) RandomAccessInputStream(loci.common.RandomAccessInputStream) FormatException(loci.formats.FormatException) IOException(java.io.IOException)

Aggregations

RandomAccessInputStream (loci.common.RandomAccessInputStream)246 CoreMetadata (loci.formats.CoreMetadata)108 MetadataStore (loci.formats.meta.MetadataStore)97 FormatException (loci.formats.FormatException)75 TiffParser (loci.formats.tiff.TiffParser)56 IFD (loci.formats.tiff.IFD)51 Length (ome.units.quantity.Length)48 Location (loci.common.Location)47 IOException (java.io.IOException)46 ArrayList (java.util.ArrayList)30 Timestamp (ome.xml.model.primitives.Timestamp)28 Time (ome.units.quantity.Time)21 ByteArrayHandle (loci.common.ByteArrayHandle)18 IFDList (loci.formats.tiff.IFDList)16 CodecOptions (loci.formats.codec.CodecOptions)9 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)8 ServiceException (loci.common.services.ServiceException)7 PhotoInterp (loci.formats.tiff.PhotoInterp)7 TiffSaver (loci.formats.tiff.TiffSaver)7 BufferedReader (java.io.BufferedReader)6