Search in sources :

Example 1 with CodecOptions

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

the class NativeND2Reader 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);
    lastChannel = split ? no % getSizeC() : 0;
    int planeIndex = split ? no / getSizeC() : no;
    in.seek(offsets[getSeries()][planeIndex]);
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    int pixel = bpp * getRGBChannelCount();
    if (split)
        pixel *= getSizeC();
    int totalPlanes = split ? getImageCount() / getSizeC() : getImageCount();
    long maxFP = planeIndex == totalPlanes - 1 ? in.length() : offsets[getSeries()][planeIndex + 1];
    CodecOptions options = new CodecOptions();
    options.littleEndian = isLittleEndian();
    options.interleaved = isInterleaved();
    options.maxBytes = (int) maxFP;
    int scanlinePad = getScanlinePad();
    if (isJPEG || isLossless) {
        if (codec == null)
            codec = createCodec(isJPEG);
        byte[] t = null;
        try {
            t = codec.decompress(in, options);
        } catch (IOException e) {
            LOGGER.debug("Failed to decompress; plane may be corrupt", e);
            return buf;
        }
        if ((getSizeX() + scanlinePad) * getSizeY() * pixel > t.length) {
            // one padding pixel per row total, instead of one padding pixel
            // per channel per row
            int rowLength = getSizeX() * pixel + scanlinePad * bpp;
            int destLength = w * pixel;
            int p = rowLength * y + x * pixel;
            byte[] pix = new byte[destLength * h];
            for (int row = 0; row < h; row++) {
                if (p + destLength <= t.length) {
                    System.arraycopy(t, p, pix, row * destLength, destLength);
                    int skip = pixel * (getSizeX() - w - x) + scanlinePad * bpp;
                    p += destLength + skip;
                } else {
                    break;
                }
            }
            if (split) {
                pix = ImageTools.splitChannels(pix, lastChannel, getEffectiveSizeC(), bpp, false, true);
            }
            System.arraycopy(pix, 0, buf, 0, pix.length);
        } else {
            copyPixels(x, y, w, h, bpp, scanlinePad, t, buf, split);
        }
        t = null;
    } else if (split && (getSizeC() <= 4 || scanlinePad == 0) && nXFields == 1) {
        byte[] pix = new byte[(getSizeX() + scanlinePad) * getSizeY() * pixel];
        in.read(pix);
        copyPixels(x, y, w, h, bpp, scanlinePad, pix, buf, split);
        pix = null;
    } else if (split) {
        // one padding pixel per row total, instead of one padding pixel
        // per channel per row
        int rowLength = getSizeX() * pixel + scanlinePad * bpp;
        int destLength = w * pixel;
        in.skipBytes(rowLength * y);
        byte[] pix = new byte[destLength * h];
        for (int row = 0; row < h; row++) {
            in.skipBytes(x * pixel);
            in.read(pix, row * destLength, destLength);
            in.skipBytes(pixel * (getSizeX() - w - x) + scanlinePad * bpp);
        }
        pix = ImageTools.splitChannels(pix, lastChannel, getEffectiveSizeC(), bpp, false, true);
        System.arraycopy(pix, 0, buf, 0, pix.length);
    } else {
        // plane is not compressed
        readPlane(in, x, y, w, h, scanlinePad, buf);
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) IOException(java.io.IOException)

Example 2 with CodecOptions

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

the class PhotoshopTiffReader 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 {
    if (getSeries() == 0)
        return super.openBytes(no, buf, x, y, w, h);
    FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
    int offsetIndex = 0;
    for (int i = 1; i < getSeries(); i++) {
        offsetIndex += core.get(i).sizeC;
    }
    tag.seek(layerOffset[offsetIndex]);
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    if (compression[getSeries() - 1] == PACKBITS || compression[getSeries() - 1] == ZIP) {
        Codec codec = compression[getSeries() - 1] == ZIP ? new ZlibCodec() : new PackbitsCodec();
        CodecOptions options = new CodecOptions();
        options.maxBytes = FormatTools.getPlaneSize(this) / getSizeC();
        ByteArrayHandle pix = new ByteArrayHandle();
        for (int c = 0; c < getSizeC(); c++) {
            int index = channelOrder[getSeries() - 1][c];
            tag.seek(layerOffset[offsetIndex + index]);
            pix.write(codec.decompress(tag, options));
        }
        RandomAccessInputStream plane = new RandomAccessInputStream(pix);
        plane.seek(0);
        readPlane(plane, x, y, w, h, buf);
        plane.close();
        pix = null;
    } else
        readPlane(tag, x, y, w, h, buf);
    return buf;
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) PackbitsCodec(loci.formats.codec.PackbitsCodec) Codec(loci.formats.codec.Codec) PackbitsCodec(loci.formats.codec.PackbitsCodec) ZlibCodec(loci.formats.codec.ZlibCodec) CodecOptions(loci.formats.codec.CodecOptions) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 3 with CodecOptions

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

the class TargaReader 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 planeSize = FormatTools.getPlaneSize(this);
    TargaRLECodec codec = new TargaRLECodec();
    CodecOptions options = new CodecOptions();
    options.maxBytes = planeSize;
    options.bitsPerSample = bits;
    RandomAccessInputStream s = in;
    if (compressed) {
        byte[] b = codec.decompress(in, options);
        s = new RandomAccessInputStream(b);
        s.order(isLittleEndian());
    }
    int bpp = bits;
    while (bpp % 8 != 0) bpp++;
    bpp /= 8;
    int rowSkip = orientation < 2 ? (getSizeY() - h - y) : y;
    int colSkip = (orientation % 2) == 1 ? (getSizeX() - w - x) : x;
    s.skipBytes(rowSkip * getSizeX() * bpp);
    for (int row = 0; row < h; row++) {
        if (s.getFilePointer() >= s.length())
            break;
        s.skipBytes(colSkip * bpp);
        for (int col = 0; col < w; col++) {
            if (s.getFilePointer() >= s.length())
                break;
            int rowIndex = orientation < 2 ? h - row - 1 : row;
            int colIndex = (orientation % 2) == 1 ? w - col - 1 : col;
            int index = getSizeC() * (rowIndex * w + colIndex);
            if (bpp == 2) {
                int v = s.readShort();
                buf[index] = (byte) ((v & 0x7c00) >> 10);
                buf[index + 1] = (byte) ((v & 0x3e0) >> 5);
                buf[index + 2] = (byte) (v & 0x1f);
            } else if (bpp == 4) {
                buf[index + 2] = s.readByte();
                buf[index + 1] = s.readByte();
                buf[index] = s.readByte();
                s.skipBytes(1);
            } else {
                for (int c = getSizeC() - 1; c >= 0; c--) {
                    buf[index + c] = s.readByte();
                }
            }
        }
        s.skipBytes(bpp * (getSizeX() - w - colSkip));
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) TargaRLECodec(loci.formats.codec.TargaRLECodec) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 4 with CodecOptions

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

the class TiffCompressionCompressTest method testGROUP_4_FAX.

@Test(expectedExceptions = { FormatException.class })
public void testGROUP_4_FAX() throws FormatException, IOException {
    TiffCompression compression = TiffCompression.GROUP_3_FAX;
    CodecOptions options = compression.getCompressionCodecOptions(ifd);
    compression.compress(data, options);
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) JPEG2000CodecOptions(loci.formats.codec.JPEG2000CodecOptions) TiffCompression(loci.formats.tiff.TiffCompression) Test(org.testng.annotations.Test)

Example 5 with CodecOptions

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

the class TiffCompressionCompressTest method testJPEG_2000_ResetNumberDecompositionLevel.

@Test(enabled = true)
public void testJPEG_2000_ResetNumberDecompositionLevel() throws FormatException, IOException {
    TiffCompression compression = TiffCompression.JPEG_2000;
    JPEG2000CodecOptions opt = JPEG2000CodecOptions.getDefaultOptions();
    int v = 16;
    opt.numDecompositionLevels = v;
    CodecOptions options = compression.getCompressionCodecOptions(ifd, opt);
    assertTrue(options instanceof JPEG2000CodecOptions);
    JPEG2000CodecOptions j2k = (JPEG2000CodecOptions) options;
    assertEquals(j2k.numDecompositionLevels, opt.numDecompositionLevels);
    compression = TiffCompression.JPEG_2000_LOSSY;
    options = compression.getCompressionCodecOptions(ifd, opt);
    assertTrue(options instanceof JPEG2000CodecOptions);
    j2k = (JPEG2000CodecOptions) options;
    assertEquals(j2k.numDecompositionLevels, opt.numDecompositionLevels);
    compression = TiffCompression.ALT_JPEG2000;
    options = compression.getCompressionCodecOptions(ifd, opt);
    j2k = (JPEG2000CodecOptions) options;
    assertEquals(j2k.numDecompositionLevels, opt.numDecompositionLevels);
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) JPEG2000CodecOptions(loci.formats.codec.JPEG2000CodecOptions) TiffCompression(loci.formats.tiff.TiffCompression) JPEG2000CodecOptions(loci.formats.codec.JPEG2000CodecOptions) Test(org.testng.annotations.Test)

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