Search in sources :

Example 66 with RandomAccessInputStream

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

the class TCSReader method isThisType.

/* @see loci.formats.IFormatReader#isThisType(String, boolean) */
@Override
public boolean isThisType(String name, boolean open) {
    // not allowed to touch the file system
    if (!open)
        return false;
    // check that there is no LEI file
    String prefix = name;
    if (prefix.indexOf('.') != -1) {
        prefix = prefix.substring(0, prefix.lastIndexOf("."));
    }
    Location lei = new Location(prefix + ".lei");
    if (!lei.exists()) {
        lei = new Location(prefix + ".LEI");
        while (!lei.exists() && prefix.indexOf('_') != -1) {
            prefix = prefix.substring(0, prefix.lastIndexOf("_"));
            lei = new Location(prefix + ".lei");
            if (!lei.exists())
                lei = new Location(prefix + ".LEI");
        }
    }
    if (lei.exists())
        return false;
    try {
        RandomAccessInputStream s = new RandomAccessInputStream(name);
        boolean isThisType = isThisType(s);
        s.close();
        return isThisType;
    } catch (IOException e) {
        LOGGER.debug("", e);
        return false;
    }
}
Also used : RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) Location(loci.common.Location)

Example 67 with RandomAccessInputStream

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

the class ZeissZVIReader method parseROIs.

/**
 * Parse ROI data from the given RandomAccessInputStream and store it in the
 * given MetadataStore.
 */
private void parseROIs(int imageNum, String name, MetadataStore store) throws IOException {
    MetadataLevel level = getMetadataOptions().getMetadataLevel();
    if (level == MetadataLevel.MINIMUM || level == MetadataLevel.NO_OVERLAYS) {
        return;
    }
    RandomAccessInputStream s = poi.getDocumentStream(name);
    s.setEncoding("UTF-16LE");
    s.order(true);
    // scan stream for offsets to each ROI
    final List<Long> roiOffsets = new ArrayList<Long>();
    // Bytes 0x0-1 == 0x3
    // Bytes 0x2-5 == Layer version (04100010)
    // Byte 0x10 == Shape count
    s.seek(2);
    int layerversion = s.readInt();
    LOGGER.debug("LAYER@{} version={}", s.getFilePointer() - 4, layerversion);
    // Layer name (assumed).  This is usually NULL in most files, so
    // we need to explicitly check whether it's a string prior to
    // parsing it.  The only file seen with a non-null string here did
    // not contain any shapes in the layer (purpose of the empty layer
    // unknown).
    String layername = null;
    {
        long tmp = s.getFilePointer();
        if (s.readShort() == 8) {
            s.seek(tmp);
            layername = parseROIString(s);
        }
        if (layername != null)
            LOGGER.debug("  Name={}", layername);
        else
            LOGGER.debug("  Name=NULL");
    }
    s.skipBytes(8);
    int roiCount = s.readShort();
    int roiFound = 0;
    LOGGER.debug("  ShapeCount@{} count={}", s.getFilePointer() - 2, roiCount);
    // Add new layer for this set of shapes.
    Layer nlayer = new Layer();
    nlayer.name = layername;
    layers.add(nlayer);
    while (roiFound < roiCount && s.getFilePointer() < s.length() - 8) {
        // find next ROI signature
        long signature = s.readLong() & 0xffffffffffffffffL;
        while (signature != ROI_SIGNATURE) {
            if (s.getFilePointer() >= s.length())
                break;
            s.seek(s.getFilePointer() - 6);
            signature = s.readLong() & 0xffffffffffffffffL;
        }
        if (s.getFilePointer() >= s.length()) {
            break;
        }
        long roiOffset = s.getFilePointer() - 8;
        roiOffsets.add(roiOffset);
        LOGGER.debug("ROI@{}", roiOffset);
        // Found ROI; now fill out the shape details and add to the
        // layer.
        s.seek(roiOffset + 26);
        int length = s.readInt();
        Shape nshape = new Shape();
        s.skipBytes(length + 6);
        long shapeAttrOffset = s.getFilePointer();
        int shapeAttrLength = s.readInt();
        nshape.type = FeatureType.get(s.readInt());
        LOGGER.debug("  ShapeAttrs@{} len={}", shapeAttrOffset, shapeAttrLength);
        if (// Broken attrs.
        shapeAttrLength < 32)
            break;
        // read the bounding box
        s.skipBytes(8);
        nshape.x1 = s.readInt();
        nshape.y1 = s.readInt();
        nshape.x2 = s.readInt();
        nshape.y2 = s.readInt();
        nshape.width = nshape.x2 - nshape.x1;
        nshape.height = nshape.y2 - nshape.y1;
        LOGGER.debug("    Bounding Box");
        if (shapeAttrLength >= 72) {
            // Basic shape styling
            s.skipBytes(16);
            nshape.fillColour = s.readInt();
            nshape.textColour = s.readInt();
            nshape.drawColour = s.readInt();
            nshape.lineWidth = s.readInt();
            nshape.drawStyle = DrawStyle.get(s.readInt());
            nshape.fillStyle = FillStyle.get(s.readInt());
            nshape.strikeout = (s.readInt() != 0);
            LOGGER.debug("    Shape styles");
        }
        if (shapeAttrLength >= 100) {
            // Font styles
            // Windows TrueType font weighting.
            nshape.fontWeight = s.readInt();
            nshape.bold = (nshape.fontWeight >= 600);
            nshape.fontSize = s.readInt();
            nshape.italic = (s.readInt() != 0);
            nshape.underline = (s.readInt() != 0);
            nshape.textAlignment = TextAlignment.get(s.readInt());
            LOGGER.debug("    Font styles");
        }
        if (shapeAttrLength >= 148) {
            // Line styles
            s.skipBytes(36);
            nshape.lineEndStyle = BaseZeissReader.LineEndStyle.get(s.readInt());
            nshape.pointStyle = BaseZeissReader.PointStyle.get(s.readInt());
            nshape.lineEndSize = s.readInt();
            nshape.lineEndPositions = BaseZeissReader.LineEndPositions.get(s.readInt());
            LOGGER.debug("    Line styles");
        }
        if (shapeAttrLength >= 152) {
            nshape.displayTag = (s.readInt() != 0);
            LOGGER.debug("    Tag display");
        }
        if (shapeAttrLength >= 152) {
            nshape.charset = Charset.get(s.readInt());
            LOGGER.debug("    Charset");
        }
        // Label (text).  This label can be NULL in some files, so we
        // need to explicitly check whether it's a string prior to
        // parsing it.  It can also be present 0 or 2 bytes after the
        // ShapeAttrs block, so check for both eventualities.
        {
            long tmp = s.getFilePointer();
            for (int i = 0; i < 2; ++i) {
                if (s.readShort() == 8) {
                    s.seek(tmp);
                    nshape.text = parseROIString(s);
                    break;
                }
            }
            if (nshape.text != null)
                LOGGER.debug("  Text={}", nshape.text);
            else
                LOGGER.debug("  Text=NULL");
        }
        // Tag ID
        if (s.getFilePointer() + 8 > s.length())
            break;
        s.skipBytes(4);
        LOGGER.debug("  Tag@{}", s.getFilePointer());
        nshape.tagID = new Tag(s.readInt(), BaseZeissReader.Context.MAIN);
        LOGGER.debug("    TagID={}", nshape.tagID);
        // Font name
        nshape.fontName = parseROIString(s);
        if (nshape.fontName == null)
            break;
        LOGGER.debug("  Font name={}", nshape.fontName);
        // Label (name).
        nshape.name = parseROIString(s);
        if (nshape.name == null)
            break;
        LOGGER.debug("  Name={}", nshape.name);
        // Handle size and point count.
        if (s.getFilePointer() + 20 > s.length())
            break;
        s.skipBytes(4);
        nshape.handleSize = s.readInt();
        s.skipBytes(2);
        nshape.pointCount = s.readInt();
        s.skipBytes(6);
        LOGGER.debug("  Handle size={}", nshape.handleSize);
        LOGGER.debug("  Point count={}", nshape.pointCount);
        if (s.getFilePointer() + (8 * 2 * nshape.pointCount) > s.length())
            break;
        nshape.points = new double[nshape.pointCount * 2];
        for (int p = 0; p < nshape.pointCount; p++) {
            nshape.points[(p * 2)] = s.readDouble();
            nshape.points[(p * 2) + 1] = s.readDouble();
        }
        nlayer.shapes.add(nshape);
        ++roiFound;
    }
    if (roiCount != roiFound) {
        LOGGER.warn("Found {} ROIs, but {} ROIs expected", roiFound, roiCount);
    }
    s.close();
}
Also used : ArrayList(java.util.ArrayList) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 68 with RandomAccessInputStream

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

the class ZeissZVIReader method fillMetadataPass1.

/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void fillMetadataPass1(MetadataStore store) throws FormatException, IOException {
    super.fillMetadataPass1(store);
    // parse each embedded file
    for (String name : files) {
        String relPath = name.substring(name.lastIndexOf(File.separator) + 1);
        if (!relPath.toUpperCase().equals("CONTENTS"))
            continue;
        String dirName = name.substring(0, name.lastIndexOf(File.separator));
        if (dirName.indexOf(File.separator) != -1) {
            dirName = dirName.substring(dirName.lastIndexOf(File.separator) + 1);
        }
        if (name.indexOf("Scaling") == -1 && dirName.equals("Tags")) {
            int imageNum = getImageNumber(name, -1);
            if (imageNum == -1) {
                parseTags(imageNum, name, new DummyMetadata());
            } else
                tagsToParse.add(name);
        } else if (dirName.equals("Shapes") && name.indexOf("Item") != -1) {
            int imageNum = getImageNumber(name, -1);
            if (imageNum != -1) {
                try {
                    parseROIs(imageNum, name, store);
                } catch (IOException e) {
                    LOGGER.debug("Could not parse all ROIs.", e);
                }
            }
        } else if (dirName.equals("Image") || dirName.toUpperCase().indexOf("ITEM") != -1) {
            int imageNum = getImageNumber(dirName, getImageCount() == 1 ? 0 : -1);
            if (imageNum == -1)
                continue;
            // found a valid image stream
            RandomAccessInputStream s = poi.getDocumentStream(name);
            s.order(true);
            if (s.length() <= 1024) {
                s.close();
                continue;
            }
            for (int q = 0; q < 11; q++) {
                getNextTag(s);
            }
            s.skipBytes(2);
            int len = s.readInt() - 20;
            s.skipBytes(8);
            int zidx = s.readInt();
            int cidx = s.readInt();
            int tidx = s.readInt();
            s.skipBytes(4);
            int tileIndex = s.readInt();
            zIndices.add(zidx);
            timepointIndices.add(tidx);
            channelIndices.add(cidx);
            tileIndices.add(tileIndex);
            s.skipBytes(len - 8);
            for (int q = 0; q < 5; q++) {
                getNextTag(s);
            }
            s.skipBytes(4);
            core.get(0).sizeX = s.readInt();
            core.get(0).sizeY = s.readInt();
            s.skipBytes(4);
            if (bpp == 0) {
                bpp = s.readInt();
            } else
                s.skipBytes(4);
            s.skipBytes(4);
            int valid = s.readInt();
            String check = s.readString(4).trim();
            isZlib = (valid == 0 || valid == 1) && check.equals("WZL");
            isJPEG = (valid == 0 || valid == 1) && !isZlib;
            // save the offset to the pixel data
            offsets[imageNum] = (int) s.getFilePointer() - 4;
            if (isZlib)
                offsets[imageNum] += 8;
            coordinates[imageNum][0] = zidx;
            coordinates[imageNum][1] = cidx;
            coordinates[imageNum][2] = tidx;
            coordinates[imageNum][3] = tileIndex;
            LOGGER.trace("imageNum = {}, coordinate = {}", imageNum, coordinates[imageNum]);
            imageFiles[imageNum] = name;
            s.close();
        }
    }
}
Also used : DummyMetadata(loci.formats.meta.DummyMetadata) IOException(java.io.IOException) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 69 with RandomAccessInputStream

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

the class ZeissZVIReader method parseTags.

/**
 * Parse all of the tags in a stream.
 */
private void parseTags(int image, String file, MetadataStore store) throws FormatException, IOException {
    ArrayList<Tag> tags = new ArrayList<Tag>();
    RandomAccessInputStream s = poi.getDocumentStream(file);
    s.order(true);
    s.seek(8);
    int count = s.readInt();
    for (int i = 0; i < count; i++) {
        if (s.getFilePointer() + 2 >= s.length())
            break;
        String value = DataTools.stripString(getNextTag(s));
        s.skipBytes(2);
        int tagID = s.readInt();
        s.skipBytes(6);
        if (// Use 1025 only for ZVI.
        tagID != 1047)
            tags.add(new Tag(tagID, value, Context.MAIN));
    }
    parseMainTags(image, store, tags);
    s.close();
}
Also used : ArrayList(java.util.ArrayList) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 70 with RandomAccessInputStream

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

the class EditTiffComment method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.out.println("Usage: java EditTiffComment file1 file2 ...");
        return;
    }
    BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    for (int i = 0; i < args.length; i++) {
        String f = args[i];
        // read comment
        System.out.println("Reading " + f + " ");
        String comment = new TiffParser(f).getComment();
        // or if you already have the file open for random access, you can use:
        // RandomAccessInputStream fin = new RandomAccessInputStream(f);
        // TiffParser tiffParser = new TiffParser(fin);
        // String comment = tiffParser.getComment();
        // fin.close();
        System.out.println("[done]");
        // display comment, and prompt for changes
        System.out.println("Comment =");
        System.out.println(comment);
        System.out.println("Enter new comment (no line breaks):");
        String xml = cin.readLine();
        System.out.print("Saving " + f);
        // save results back to the TIFF file
        TiffSaver saver = new TiffSaver(f);
        RandomAccessInputStream in = new RandomAccessInputStream(f);
        saver.overwriteComment(in, xml);
        in.close();
        saver.close();
        System.out.println(" [done]");
    }
}
Also used : RandomAccessInputStream(loci.common.RandomAccessInputStream)

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