Search in sources :

Example 6 with TiffIFDEntry

use of loci.formats.tiff.TiffIFDEntry in project bioformats by openmicroscopy.

the class OMETiffReader method isThisType.

/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
    TiffParser tp = new TiffParser(stream);
    tp.setDoCaching(false);
    boolean validHeader = tp.isValidHeader();
    if (!validHeader)
        return false;
    // look for OME-XML in first IFD's comment
    IFD ifd = tp.getFirstIFD();
    if (ifd == null)
        return false;
    Object description = ifd.get(IFD.IMAGE_DESCRIPTION);
    if (description == null) {
        return false;
    }
    String comment = null;
    if (description instanceof TiffIFDEntry) {
        Object value = tp.getIFDValue((TiffIFDEntry) description);
        if (value != null) {
            comment = value.toString();
        }
    } else if (description instanceof String) {
        comment = (String) description;
    }
    if (comment == null || comment.trim().length() == 0)
        return false;
    comment = comment.trim();
    // we are reasonably sure that the comment contains XML
    if (!comment.startsWith("<") || !comment.endsWith(">")) {
        return false;
    }
    try {
        if (service == null)
            setupService();
        meta = service.createOMEXMLMetadata(comment);
        try {
            metadataFile = meta.getBinaryOnlyMetadataFile();
            // referencing the current OME-TIFF
            if (metadataFile != null) {
                return true;
            }
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < meta.getImageCount(); i++) {
            meta.setPixelsBigEndian(Boolean.TRUE, i);
            if (meta.getPixelsBinDataCount(i) > 0) {
                for (int j = 0; j < meta.getPixelsBinDataCount(i); j++) {
                    meta.setPixelsBinDataBigEndian(Boolean.TRUE, i, j);
                }
            }
            MetadataTools.verifyMinimumPopulated(meta, i);
        }
        return meta.getImageCount() > 0;
    } catch (ServiceException se) {
        LOGGER.debug("OME-XML parsing failed", se);
    } catch (NullPointerException e) {
        LOGGER.debug("OME-XML parsing failed", e);
    } catch (FormatException e) {
        LOGGER.debug("OME-XML parsing failed", e);
    } catch (IndexOutOfBoundsException e) {
        LOGGER.debug("OME-XML parsing failed", e);
    }
    return false;
}
Also used : TiffIFDEntry(loci.formats.tiff.TiffIFDEntry) ServiceException(loci.common.services.ServiceException) IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) FormatException(loci.formats.FormatException)

Example 7 with TiffIFDEntry

use of loci.formats.tiff.TiffIFDEntry in project bioformats by openmicroscopy.

the class IonpathMIBITiffReader method initStandardMetadata.

// -- Internal BaseTiffReader API methods --
/* @see loci.formats.BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
    super.initStandardMetadata();
    ifds = tiffParser.getIFDs();
    int seriesIndex;
    String imageType = null;
    core.clear();
    for (int i = 0; i < ifds.size(); i++) {
        IFD ifd = ifds.get(i);
        Object description = ifd.get(IFD.IMAGE_DESCRIPTION);
        if (description == null) {
            throw new FormatException("Image description is mandatory.");
        }
        String imageDescription = null;
        if (description instanceof TiffIFDEntry) {
            Object value = tiffParser.getIFDValue((TiffIFDEntry) description);
            if (value != null) {
                imageDescription = value.toString();
            }
        } else if (description instanceof String) {
            imageDescription = (String) description;
        }
        JSONObject jsonDescription;
        try {
            jsonDescription = new JSONObject(imageDescription);
            imageType = jsonDescription.getString("image.type");
            if (imageType.equals("SIMS")) {
                String mass = jsonDescription.getString("channel.mass");
                if (mass == null) {
                    throw new FormatException("Channel masses are mandatory.");
                }
                String target = jsonDescription.getString("channel.target");
                channelIDs.add(mass);
                channelNames.add(target != null && target != "null" ? target : mass);
            }
        } catch (JSONException e) {
            throw new FormatException("Unexpected format in SIMS description JSON.");
        }
        if (seriesTypes.containsKey(imageType)) {
            if (!imageType.equals("SIMS")) {
                throw new FormatException("Only type 'SIMS' can have >1 image per file.");
            }
            seriesIndex = seriesTypes.get(imageType);
            CoreMetadata ms = core.get(seriesIndex);
            ms.sizeC += 1;
            ms.imageCount += 1;
        } else {
            seriesIndex = seriesTypes.size();
            seriesTypes.put(imageType, seriesIndex);
            seriesIFDs.add(i);
            core.add(new CoreMetadata());
            setSeries(seriesIndex);
            tiffParser.setDoCaching(true);
            tiffParser.fillInIFD(ifd);
            if (imageType.equals("SIMS")) {
                try {
                    Iterator<?> keySet = jsonDescription.keys();
                    while (keySet.hasNext()) {
                        String key = (String) keySet.next();
                        if (key.startsWith("mibi.")) {
                            simsDescription.put(key, jsonDescription.getString(key));
                        }
                    }
                } catch (JSONException e) {
                    throw new FormatException("Unexpected format in SIMS description JSON.");
                }
            }
            CoreMetadata ms = core.get(seriesIndex);
            PhotoInterp p = ifd.getPhotometricInterpretation();
            int samples = ifd.getSamplesPerPixel();
            ms.rgb = samples > 1 || p == PhotoInterp.RGB;
            ms.sizeX = (int) ifd.getImageWidth();
            ms.sizeY = (int) ifd.getImageLength();
            ms.sizeZ = 1;
            ms.sizeT = 1;
            ms.sizeC = ms.rgb ? samples : 1;
            ms.littleEndian = ifd.isLittleEndian();
            ms.indexed = p == PhotoInterp.RGB_PALETTE && (get8BitLookupTable() != null || get16BitLookupTable() != null);
            ms.imageCount = 1;
            ms.pixelType = ifd.getPixelType();
            ms.metadataComplete = true;
            ms.interleaved = false;
            ms.falseColor = false;
            ms.dimensionOrder = "XYCZT";
            ms.thumbnail = false;
            ms.imageCount = 1;
            if (imageType.equals("SIMS")) {
                ms.seriesMetadata = simsDescription;
            }
        }
    }
}
Also used : TiffIFDEntry(loci.formats.tiff.TiffIFDEntry) JSONObject(org.json.JSONObject) IFD(loci.formats.tiff.IFD) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) PhotoInterp(loci.formats.tiff.PhotoInterp) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 8 with TiffIFDEntry

use of loci.formats.tiff.TiffIFDEntry in project bioformats by openmicroscopy.

the class PhotoshopTiffReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    Object sourceData = ifds.get(0).getIFDValue(IMAGE_SOURCE_DATA);
    byte[] b = null;
    if (sourceData instanceof byte[]) {
        b = (byte[]) sourceData;
    } else if (sourceData instanceof TiffIFDEntry) {
        b = (byte[]) tiffParser.getIFDValue((TiffIFDEntry) sourceData);
    }
    if (b == null)
        return;
    tag = new RandomAccessInputStream(b);
    tag.order(isLittleEndian());
    String checkString = tag.readCString();
    String signature, type;
    int length;
    while (tag.getFilePointer() < tag.length() - 12 && tag.getFilePointer() > 0) {
        signature = tag.readString(4);
        type = tag.readString(4);
        length = tag.readInt();
        int skip = length % 4;
        if (skip != 0)
            skip = 4 - skip;
        if (type.equals("ryaL")) {
            int nLayers = (int) Math.abs(tag.readShort());
            compression = new int[nLayers];
            layerNames = new String[nLayers];
            channelOrder = new int[nLayers][];
            int[][] dataSize = new int[nLayers][];
            int offsetCount = 0;
            for (int layer = 0; layer < nLayers; layer++) {
                int top = tag.readInt();
                int left = tag.readInt();
                int bottom = tag.readInt();
                int right = tag.readInt();
                CoreMetadata layerCore = new CoreMetadata();
                layerCore = new CoreMetadata();
                layerCore.sizeX = right - left;
                layerCore.sizeY = bottom - top;
                layerCore.pixelType = getPixelType();
                layerCore.sizeC = tag.readShort();
                layerCore.sizeZ = 1;
                layerCore.sizeT = 1;
                layerCore.imageCount = 1;
                layerCore.rgb = isRGB();
                layerCore.interleaved = isInterleaved();
                layerCore.littleEndian = isLittleEndian();
                layerCore.dimensionOrder = getDimensionOrder();
                if (layerCore.sizeX == 0 || layerCore.sizeY == 0 || (layerCore.sizeC > 1 && !isRGB())) {
                    // Set size to 1
                    CoreMetadata ms0 = core.get(0);
                    core.clear();
                    core.add(ms0);
                    break;
                }
                offsetCount += layerCore.sizeC;
                channelOrder[layer] = new int[layerCore.sizeC];
                dataSize[layer] = new int[layerCore.sizeC];
                for (int c = 0; c < layerCore.sizeC; c++) {
                    int channelID = tag.readShort();
                    if (channelID < 0)
                        channelID = layerCore.sizeC - 1;
                    channelOrder[layer][channelID] = c;
                    dataSize[layer][c] = tag.readInt();
                }
                tag.skipBytes(12);
                int len = tag.readInt();
                long fp = tag.getFilePointer();
                int mask = tag.readInt();
                if (mask != 0)
                    tag.skipBytes(mask);
                int blending = tag.readInt();
                tag.skipBytes(blending);
                int nameLength = tag.read();
                int pad = nameLength % 4;
                if (pad != 0)
                    pad = 4 - pad;
                layerNames[layer] = tag.readString(nameLength + pad);
                layerNames[layer] = layerNames[layer].replaceAll("[^\\p{ASCII}]", "").trim();
                if (layerNames[layer].length() == nameLength + pad && !layerNames[layer].equalsIgnoreCase("Layer " + layer + "M")) {
                    addGlobalMetaList("Layer name", layerNames[layer]);
                    core.add(layerCore);
                }
                tag.skipBytes((int) (fp + len - tag.getFilePointer()));
            }
            nLayers = core.size() - 1;
            layerOffset = new long[offsetCount];
            int nextOffset = 0;
            for (int layer = 0; layer < nLayers; layer++) {
                for (int c = 0; c < core.get(layer + 1).sizeC; c++) {
                    long startFP = tag.getFilePointer();
                    compression[layer] = tag.readShort();
                    layerOffset[nextOffset] = tag.getFilePointer();
                    if (compression[layer] == ZIP) {
                        layerOffset[nextOffset] = tag.getFilePointer();
                        ZlibCodec codec = new ZlibCodec();
                        codec.decompress(tag, null);
                    } else if (compression[layer] == PACKBITS) {
                        if (layer == 0) {
                            tag.skipBytes(256 * 6 + 36);
                        } else
                            tag.skipBytes(192);
                        layerOffset[nextOffset] = tag.getFilePointer();
                        PackbitsCodec codec = new PackbitsCodec();
                        CodecOptions options = new CodecOptions();
                        options.maxBytes = core.get(layer + 1).sizeX * core.get(layer + 1).sizeY;
                        codec.decompress(tag, options);
                    }
                    tag.seek(startFP + dataSize[layer][c]);
                    nextOffset++;
                }
            }
        } else
            tag.skipBytes(length + skip);
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    store.setImageName("Merged", 0);
    if (layerNames != null) {
        int end = (int) Math.min(getSeriesCount() - 1, layerNames.length);
        for (int layer = 0; layer < end; layer++) {
            store.setImageName(layerNames[layer], layer + 1);
        }
    }
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) CoreMetadata(loci.formats.CoreMetadata) ZlibCodec(loci.formats.codec.ZlibCodec) PackbitsCodec(loci.formats.codec.PackbitsCodec) MetadataStore(loci.formats.meta.MetadataStore) TiffIFDEntry(loci.formats.tiff.TiffIFDEntry) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

TiffIFDEntry (loci.formats.tiff.TiffIFDEntry)8 IFD (loci.formats.tiff.IFD)7 RandomAccessInputStream (loci.common.RandomAccessInputStream)5 CoreMetadata (loci.formats.CoreMetadata)5 FormatException (loci.formats.FormatException)4 TiffParser (loci.formats.tiff.TiffParser)4 IOException (java.io.IOException)3 PhotoInterp (loci.formats.tiff.PhotoInterp)3 ArrayList (java.util.ArrayList)2 Location (loci.common.Location)2 MetadataStore (loci.formats.meta.MetadataStore)2 IFDList (loci.formats.tiff.IFDList)2 Timestamp (ome.xml.model.primitives.Timestamp)2 BigDecimal (java.math.BigDecimal)1 MathContext (java.math.MathContext)1 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1