Search in sources :

Example 21 with CodecOptions

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

the class CellSensReader method decodeTile.

private byte[] decodeTile(int no, int row, int col) throws FormatException, IOException {
    if (tileMap.get(getCoreIndex()) == null) {
        return new byte[getTileSize()];
    }
    int[] zct = getZCTCoords(no);
    TileCoordinate t = new TileCoordinate(nDimensions.get(getCoreIndex()));
    t.coordinate[0] = col;
    t.coordinate[1] = row;
    int resIndex = getResolution();
    int pyramidIndex = getSeries();
    if (hasFlattenedResolutions()) {
        int index = 0;
        pyramidIndex = 0;
        for (int i = 0; i < core.size(); ) {
            if (index + core.get(i).resolutionCount <= getSeries()) {
                index += core.get(i).resolutionCount;
                i += core.get(i).resolutionCount;
                pyramidIndex++;
            } else {
                resIndex = getSeries() - index;
                break;
            }
        }
    }
    Pyramid pyramid = pyramids.get(pyramidIndex);
    for (String dim : pyramid.dimensionOrdering.keySet()) {
        int index = pyramid.dimensionOrdering.get(dim) + 2;
        if (dim.equals("Z")) {
            t.coordinate[index] = zct[0];
        } else if (dim.equals("C")) {
            t.coordinate[index] = zct[1];
        } else if (dim.equals("T")) {
            t.coordinate[index] = zct[2];
        }
    }
    if (resIndex > 0) {
        t.coordinate[t.coordinate.length - 1] = resIndex;
    }
    ArrayList<TileCoordinate> map = tileMap.get(getCoreIndex());
    Integer index = map.indexOf(t);
    if (index == null || index < 0) {
        // fill in the tile with the stored background color
        // usually this is either black or white
        byte[] tile = new byte[getTileSize()];
        byte[] color = backgroundColor.get(getCoreIndex());
        if (color != null) {
            for (int q = 0; q < getTileSize(); q += color.length) {
                for (int i = 0; i < color.length; i++) {
                    tile[q + i] = color[i];
                }
            }
        }
        return tile;
    }
    Long offset = tileOffsets.get(getCoreIndex())[index];
    RandomAccessInputStream ets = new RandomAccessInputStream(fileMap.get(getCoreIndex()));
    ets.seek(offset);
    CodecOptions options = new CodecOptions();
    options.interleaved = isInterleaved();
    options.littleEndian = isLittleEndian();
    int tileSize = getTileSize();
    if (tileSize == 0) {
        tileSize = tileX.get(getCoreIndex()) * tileY.get(getCoreIndex()) * 10;
    }
    options.maxBytes = (int) (offset + tileSize);
    byte[] buf = null;
    long end = index < tileOffsets.get(getCoreIndex()).length - 1 ? tileOffsets.get(getCoreIndex())[index + 1] : ets.length();
    IFormatReader reader = null;
    String file = null;
    switch(compressionType.get(getCoreIndex())) {
        case RAW:
            buf = new byte[tileSize];
            ets.read(buf);
            break;
        case JPEG:
            Codec codec = new JPEGCodec();
            buf = codec.decompress(ets, options);
            break;
        case JPEG_2000:
            codec = new JPEG2000Codec();
            buf = codec.decompress(ets, options);
            break;
        case JPEG_LOSSLESS:
            codec = new LosslessJPEGCodec();
            buf = codec.decompress(ets, options);
            break;
        case PNG:
            file = "tile.png";
            reader = new APNGReader();
        case BMP:
            if (reader == null) {
                file = "tile.bmp";
                reader = new BMPReader();
            }
            byte[] b = new byte[(int) (end - offset)];
            ets.read(b);
            Location.mapFile(file, new ByteArrayHandle(b));
            reader.setId(file);
            buf = reader.openBytes(0);
            Location.mapFile(file, null);
            break;
    }
    if (reader != null) {
        reader.close();
    }
    ets.close();
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) IFormatReader(loci.formats.IFormatReader) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) JPEGCodec(loci.formats.codec.JPEGCodec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) Codec(loci.formats.codec.Codec) JPEGCodec(loci.formats.codec.JPEGCodec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 22 with CodecOptions

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

the class TiffCompressionCompressTest method testTHUNDERSCAN.

@Test(expectedExceptions = { FormatException.class })
public void testTHUNDERSCAN() throws FormatException, IOException {
    TiffCompression compression = TiffCompression.THUNDERSCAN;
    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 23 with CodecOptions

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

the class TiffCompressionCompressTest method testJPEG_2000_ResetQuality.

@Test(enabled = true)
public void testJPEG_2000_ResetQuality() throws FormatException, IOException {
    TiffCompression compression = TiffCompression.JPEG_2000;
    JPEG2000CodecOptions opt = JPEG2000CodecOptions.getDefaultOptions();
    opt.quality = 1.0f;
    CodecOptions options = compression.getCompressionCodecOptions(ifd, opt);
    assertEquals(options.quality, opt.quality);
    compression = TiffCompression.JPEG_2000_LOSSY;
    options = compression.getCompressionCodecOptions(ifd, opt);
    assertEquals(options.quality, opt.quality);
    compression = TiffCompression.ALT_JPEG2000;
    options = compression.getCompressionCodecOptions(ifd, opt);
    assertEquals(options.quality, opt.quality);
}
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)

Example 24 with CodecOptions

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

the class TiffCompressionCompressTest method testJPEG_2000_LOSSY.

@Test
public void testJPEG_2000_LOSSY() throws FormatException, IOException {
    TiffCompression compression = TiffCompression.JPEG_2000_LOSSY;
    CodecOptions options = compression.getCompressionCodecOptions(ifd);
    byte[] compressed = compression.compress(data, options);
    assertNotNull(compressed);
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) JPEG2000CodecOptions(loci.formats.codec.JPEG2000CodecOptions) TiffCompression(loci.formats.tiff.TiffCompression) Test(org.testng.annotations.Test)

Example 25 with CodecOptions

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

the class TiffCompressionCompressTest method testNIKON.

@Test(expectedExceptions = { FormatException.class })
public void testNIKON() throws FormatException, IOException {
    TiffCompression compression = TiffCompression.NIKON;
    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)

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