Search in sources :

Example 6 with PackbitsCodec

use of loci.formats.codec.PackbitsCodec 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

CodecOptions (loci.formats.codec.CodecOptions)6 PackbitsCodec (loci.formats.codec.PackbitsCodec)6 RandomAccessInputStream (loci.common.RandomAccessInputStream)3 CoreMetadata (loci.formats.CoreMetadata)2 UnsupportedCompressionException (loci.formats.UnsupportedCompressionException)2 Codec (loci.formats.codec.Codec)2 ZlibCodec (loci.formats.codec.ZlibCodec)2 MetadataStore (loci.formats.meta.MetadataStore)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ByteArrayHandle (loci.common.ByteArrayHandle)1 Location (loci.common.Location)1 JPEG2000Codec (loci.formats.codec.JPEG2000Codec)1 JPEGCodec (loci.formats.codec.JPEGCodec)1 TiffIFDEntry (loci.formats.tiff.TiffIFDEntry)1 Length (ome.units.quantity.Length)1 Timestamp (ome.xml.model.primitives.Timestamp)1