Search in sources :

Example 6 with JPEG2000Codec

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

the class JPEG2000Writer method compressBuffer.

/**
 * Compresses the buffer.
 *
 * @param no the image index within the current file, starting from 0.
 * @param buf the byte array that represents the image tile.
 * @param x the X coordinate of the upper-left corner of the image tile.
 * @param y the Y coordinate of the upper-left corner of the image tile.
 * @param w the width (in pixels) of the image tile.
 * @param h the height (in pixels) of the image tile.
 * @throws FormatException if one of the parameters is invalid.
 * @throws IOException if there was a problem writing to the file.
 */
public byte[] compressBuffer(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    checkParams(no, buf, x, y, w, h);
    MetadataRetrieve retrieve = getMetadataRetrieve();
    boolean littleEndian = false;
    if (retrieve.getPixelsBigEndian(series) != null) {
        littleEndian = !retrieve.getPixelsBigEndian(series).booleanValue();
    } else if (retrieve.getPixelsBinDataCount(series) == 0) {
        littleEndian = !retrieve.getPixelsBinDataBigEndian(series, 0).booleanValue();
    }
    int bytesPerPixel = FormatTools.getBytesPerPixel(FormatTools.pixelTypeFromString(retrieve.getPixelsType(series).toString()));
    int nChannels = getSamplesPerPixel();
    // To be on the save-side
    if (options == null)
        options = JPEG2000CodecOptions.getDefaultOptions();
    options = new JPEG2000CodecOptions(options);
    options.width = w;
    options.height = h;
    options.channels = nChannels;
    options.bitsPerSample = bytesPerPixel * 8;
    options.littleEndian = littleEndian;
    options.interleaved = interleaved;
    options.lossless = compression == null || compression.equals(CompressionType.J2K.getCompression());
    options.colorModel = getColorModel();
    return new JPEG2000Codec().compress(buf, options);
}
Also used : JPEG2000Codec(loci.formats.codec.JPEG2000Codec) JPEG2000CodecOptions(loci.formats.codec.JPEG2000CodecOptions) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 7 with JPEG2000Codec

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

the class JPXReader 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);
    if (lastSeries == getSeries() && lastPlane == no && lastSeriesPlane != null) {
        RandomAccessInputStream s = new RandomAccessInputStream(lastSeriesPlane);
        readPlane(s, x, y, w, h, buf);
        s.close();
        return buf;
    }
    JPEG2000CodecOptions options = JPEG2000CodecOptions.getDefaultOptions();
    options.interleaved = isInterleaved();
    options.littleEndian = isLittleEndian();
    if (resolutionLevels != null) {
        options.resolution = Math.abs(getSeries() - resolutionLevels);
    } else if (getSeriesCount() > 1) {
        options.resolution = getSeries();
    }
    in.seek(pixelOffsets.get(no));
    lastSeriesPlane = new JPEG2000Codec().decompress(in, options);
    RandomAccessInputStream s = new RandomAccessInputStream(lastSeriesPlane);
    readPlane(s, x, y, w, h, buf);
    s.close();
    lastSeries = getSeries();
    lastPlane = no;
    return buf;
}
Also used : JPEG2000Codec(loci.formats.codec.JPEG2000Codec) JPEG2000CodecOptions(loci.formats.codec.JPEG2000CodecOptions) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

JPEG2000Codec (loci.formats.codec.JPEG2000Codec)7 CodecOptions (loci.formats.codec.CodecOptions)4 JPEGCodec (loci.formats.codec.JPEGCodec)4 RandomAccessInputStream (loci.common.RandomAccessInputStream)3 JPEG2000CodecOptions (loci.formats.codec.JPEG2000CodecOptions)3 Codec (loci.formats.codec.Codec)2 ZlibCodec (loci.formats.codec.ZlibCodec)2 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayHandle (loci.common.ByteArrayHandle)1 CBZip2InputStream (loci.common.CBZip2InputStream)1 IFormatReader (loci.formats.IFormatReader)1 UnsupportedCompressionException (loci.formats.UnsupportedCompressionException)1 Base64Codec (loci.formats.codec.Base64Codec)1 LosslessJPEGCodec (loci.formats.codec.LosslessJPEGCodec)1 PackbitsCodec (loci.formats.codec.PackbitsCodec)1