Search in sources :

Example 36 with CodecOptions

use of loci.formats.codec.CodecOptions in project bioformats by openmicroscopy.

the class PSDReader method openBytes.

/**
 * @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
 */
@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
    in.seek(offset);
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    int plane = getSizeX() * getSizeY() * bpp;
    if (compressed) {
        PackbitsCodec codec = new PackbitsCodec();
        CodecOptions options = new CodecOptions();
        options.maxBytes = getSizeX() * bpp;
        byte[] b = null;
        int index = 0;
        for (int c = 0; c < getSizeC(); c++) {
            for (int row = 0; row < getSizeY(); row++) {
                if (row < y || row >= y + h) {
                    in.skipBytes(lens[c][row]);
                } else {
                    b = new byte[lens[c][row]];
                    in.read(b);
                    b = codec.decompress(b, options);
                    System.arraycopy(b, x * bpp, buf, index, w * bpp);
                    index += w * bpp;
                }
            }
        }
    } else {
        readPlane(in, x, y, w, h, buf);
    }
    return buf;
}
Also used : PackbitsCodec(loci.formats.codec.PackbitsCodec) CodecOptions(loci.formats.codec.CodecOptions)

Example 37 with CodecOptions

use of loci.formats.codec.CodecOptions 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)37 JPEG2000CodecOptions (loci.formats.codec.JPEG2000CodecOptions)18 TiffCompression (loci.formats.tiff.TiffCompression)18 Test (org.testng.annotations.Test)18 RandomAccessInputStream (loci.common.RandomAccessInputStream)9 JPEGCodec (loci.formats.codec.JPEGCodec)8 PackbitsCodec (loci.formats.codec.PackbitsCodec)6 ByteArrayHandle (loci.common.ByteArrayHandle)5 ZlibCodec (loci.formats.codec.ZlibCodec)5 UnsupportedCompressionException (loci.formats.UnsupportedCompressionException)4 JPEG2000Codec (loci.formats.codec.JPEG2000Codec)4 IOException (java.io.IOException)3 CoreMetadata (loci.formats.CoreMetadata)3 Codec (loci.formats.codec.Codec)3 FormatException (loci.formats.FormatException)2 IFormatReader (loci.formats.IFormatReader)2 MetadataStore (loci.formats.meta.MetadataStore)2 Length (ome.units.quantity.Length)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1