Search in sources :

Example 1 with JPEGCodec

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

the class NativeQTReader method uncompress.

/**
 * Uncompresses an image plane according to the the codec identifier.
 */
private byte[] uncompress(byte[] pixs, String code) throws FormatException, IOException {
    CodecOptions options = new MJPBCodecOptions();
    options.width = getSizeX();
    options.height = getSizeY();
    options.bitsPerSample = bitsPerPixel;
    options.channels = bitsPerPixel < 40 ? bitsPerPixel / 8 : (bitsPerPixel - 32) / 8;
    options.previousImage = canUsePrevious ? prevPixels : null;
    options.littleEndian = isLittleEndian();
    options.interleaved = isRGB();
    if (code.equals("raw "))
        return pixs;
    else if (code.equals("rle ")) {
        return new QTRLECodec().decompress(pixs, options);
    } else if (code.equals("rpza")) {
        return new RPZACodec().decompress(pixs, options);
    } else if (code.equals("mjpb")) {
        ((MJPBCodecOptions) options).interlaced = interlaced;
        return new MJPBCodec().decompress(pixs, options);
    } else if (code.equals("jpeg")) {
        return new JPEGCodec().decompress(pixs, options);
    } else {
        throw new UnsupportedCompressionException("Unsupported codec : " + code);
    }
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) MJPBCodecOptions(loci.formats.codec.MJPBCodecOptions) QTRLECodec(loci.formats.codec.QTRLECodec) MJPBCodec(loci.formats.codec.MJPBCodec) RPZACodec(loci.formats.codec.RPZACodec) UnsupportedCompressionException(loci.formats.UnsupportedCompressionException) MJPBCodecOptions(loci.formats.codec.MJPBCodecOptions) JPEGCodec(loci.formats.codec.JPEGCodec)

Example 2 with JPEGCodec

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

the class OMEXMLWriter method compress.

// -- Helper methods --
/**
 * Compress the given byte array using the current codec.
 * The compressed data is then base64-encoded.
 */
private byte[] compress(byte[] b) throws FormatException, IOException {
    MetadataRetrieve r = getMetadataRetrieve();
    String type = r.getPixelsType(series).toString();
    int pixelType = FormatTools.pixelTypeFromString(type);
    int bytes = FormatTools.getBytesPerPixel(pixelType);
    CodecOptions options = new CodecOptions();
    options.width = r.getPixelsSizeX(series).getValue().intValue();
    options.height = r.getPixelsSizeY(series).getValue().intValue();
    options.channels = 1;
    options.interleaved = false;
    options.signed = FormatTools.isSigned(pixelType);
    boolean littleEndian = false;
    if (r.getPixelsBigEndian(series) != null) {
        littleEndian = !r.getPixelsBigEndian(series).booleanValue();
    } else if (r.getPixelsBinDataCount(series) == 0) {
        littleEndian = !r.getPixelsBinDataBigEndian(series, 0).booleanValue();
    }
    options.littleEndian = littleEndian;
    options.bitsPerSample = bytes * 8;
    if (compression.equals("J2K")) {
        b = new JPEG2000Codec().compress(b, options);
    } else if (compression.equals("JPEG")) {
        b = new JPEGCodec().compress(b, options);
    } else if (compression.equals("zlib")) {
        b = new ZlibCodec().compress(b, options);
    }
    return new Base64Codec().compress(b, options);
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) CodecOptions(loci.formats.codec.CodecOptions) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) Base64Codec(loci.formats.codec.Base64Codec) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) JPEGCodec(loci.formats.codec.JPEGCodec)

Example 3 with JPEGCodec

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

the class PictReader 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 (jpegOffsets.size() > 0) {
        ByteArrayHandle v = new ByteArrayHandle();
        in.seek(jpegOffsets.get(0));
        byte[] b = new byte[(int) (in.length() - in.getFilePointer())];
        in.read(b);
        RandomAccessInputStream s = new RandomAccessInputStream(b);
        for (long jpegOffset : jpegOffsets) {
            s.seek(jpegOffset - jpegOffsets.get(0));
            CodecOptions options = new CodecOptions();
            options.interleaved = isInterleaved();
            options.littleEndian = isLittleEndian();
            v.write(new JPEGCodec().decompress(s, options));
        }
        s.close();
        s = new RandomAccessInputStream(v);
        s.seek(0);
        readPlane(s, x, y, w, h, buf);
        s.close();
        return buf;
    }
    if (legacy || strips.size() == 0) {
        in.seek(512);
        byte[] pix = new byte[(int) (in.length() - in.getFilePointer())];
        in.read(pix);
        byte[][] b = AWTImageTools.getBytes(AWTImageTools.makeBuffered(qtTools.pictToImage(pix)));
        pix = null;
        for (int i = 0; i < b.length; i++) {
            System.arraycopy(b[i], 0, buf, i * b[i].length, b[i].length);
        }
        b = null;
        return buf;
    }
    if ((getSizeY() * 4 < strips.size()) && (((strips.size() / 3) % getSizeY()) != 0)) {
        core.get(0).sizeY = strips.size();
    }
    int plane = w * h;
    if (lookup != null) {
        // 8 bit data
        byte[] row;
        for (int i = y; i < y + h; i++) {
            row = (byte[]) strips.get(i);
            int len = (int) Math.min(row.length, w);
            System.arraycopy(row, x, buf, (i - y) * w, len);
        }
    } else if (getSizeY() * 3 == strips.size() || getSizeY() * 4 == strips.size()) {
        // 24 or 32 bit data
        int nc = strips.size() / getSizeY();
        byte[] c0 = null;
        byte[] c1 = null;
        byte[] c2 = null;
        for (int i = y; i < h + y; i++) {
            c0 = (byte[]) strips.get(i * nc + nc - 3);
            c1 = (byte[]) strips.get(i * nc + nc - 2);
            c2 = (byte[]) strips.get(i * nc + nc - 1);
            int baseOffset = (i - y) * w;
            System.arraycopy(c0, x, buf, baseOffset, w);
            System.arraycopy(c1, x, buf, plane + baseOffset, w);
            System.arraycopy(c2, x, buf, 2 * plane + baseOffset, w);
        }
    } else {
        // RGB value is packed into a single short: xRRR RRGG GGGB BBBB
        int[] row = null;
        for (int i = y; i < h + y; i++) {
            row = (int[]) strips.get(i);
            for (int j = x; j < w + x; j++) {
                int base = (i - y) * w + (j - x);
                buf[base] = (byte) ((row[j] & 0x7c00) >> 10);
                buf[plane + base] = (byte) ((row[j] & 0x3e0) >> 5);
                buf[2 * plane + base] = (byte) (row[j] & 0x1f);
            }
        }
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle) JPEGCodec(loci.formats.codec.JPEGCodec)

Example 4 with JPEGCodec

use of loci.formats.codec.JPEGCodec 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 5 with JPEGCodec

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

the class ZeissZVIReader 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);
    lastPlane = no;
    if (poi == null) {
        initPOIService();
    }
    int bytes = FormatTools.getBytesPerPixel(getPixelType());
    int pixel = bytes * getRGBChannelCount();
    CodecOptions options = new CodecOptions();
    options.littleEndian = isLittleEndian();
    options.interleaved = isInterleaved();
    int index = -1;
    int[] coords = getZCTCoords(no);
    for (int q = 0; q < coordinates.length; q++) {
        if (coordinates[q][0] == coords[0] && coordinates[q][1] == coords[1] && coordinates[q][2] == coords[2] && coordinates[q][3] == getSeries()) {
            index = q;
            break;
        }
    }
    LOGGER.trace("no = " + no + ", index = " + index);
    if (index < 0 || index >= imageFiles.length) {
        return buf;
    }
    RandomAccessInputStream s = poi.getDocumentStream(imageFiles[index]);
    s.seek(offsets[index]);
    int len = w * pixel;
    int row = getSizeX() * pixel;
    if (isJPEG) {
        byte[] t = new JPEGCodec().decompress(s, options);
        for (int yy = 0; yy < h; yy++) {
            System.arraycopy(t, (yy + y) * row + x * pixel, buf, yy * len, len);
        }
    } else if (isZlib) {
        byte[] t = new ZlibCodec().decompress(s, options);
        for (int yy = 0; yy < h; yy++) {
            int src = (yy + y) * row + x * pixel;
            int dest = yy * len;
            if (src + len <= t.length && dest + len <= buf.length) {
                System.arraycopy(t, src, buf, dest, len);
            } else
                break;
        }
    } else {
        readPlane(s, x, y, w, h, buf);
    }
    s.close();
    if (isRGB() && !isJPEG) {
        // reverse bytes in groups of 3 to account for BGR storage
        byte[] bb = new byte[bytes];
        for (int i = 0; i < buf.length; i += bpp) {
            System.arraycopy(buf, i + 2 * bytes, bb, 0, bytes);
            System.arraycopy(buf, i, buf, i + 2 * bytes, bytes);
            System.arraycopy(bb, 0, buf, i, bytes);
        }
    }
    return buf;
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) CodecOptions(loci.formats.codec.CodecOptions) RandomAccessInputStream(loci.common.RandomAccessInputStream) JPEGCodec(loci.formats.codec.JPEGCodec)

Aggregations

CodecOptions (loci.formats.codec.CodecOptions)8 JPEGCodec (loci.formats.codec.JPEGCodec)8 JPEG2000Codec (loci.formats.codec.JPEG2000Codec)4 RandomAccessInputStream (loci.common.RandomAccessInputStream)3 UnsupportedCompressionException (loci.formats.UnsupportedCompressionException)3 ZlibCodec (loci.formats.codec.ZlibCodec)3 ByteArrayHandle (loci.common.ByteArrayHandle)2 Codec (loci.formats.codec.Codec)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 CBZip2InputStream (loci.common.CBZip2InputStream)1 IFormatReader (loci.formats.IFormatReader)1 Base64Codec (loci.formats.codec.Base64Codec)1 LosslessJPEGCodec (loci.formats.codec.LosslessJPEGCodec)1 MJPBCodec (loci.formats.codec.MJPBCodec)1 MJPBCodecOptions (loci.formats.codec.MJPBCodecOptions)1 MSRLECodec (loci.formats.codec.MSRLECodec)1 MSVideoCodec (loci.formats.codec.MSVideoCodec)1 PackbitsCodec (loci.formats.codec.PackbitsCodec)1 QTRLECodec (loci.formats.codec.QTRLECodec)1 RPZACodec (loci.formats.codec.RPZACodec)1