Search in sources :

Example 86 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class CompressDecompressTest method assertCompression.

/**
 * Tests the writing of the tiles.
 * @param compression The compression to use.
 * @param lossy whether or not this is a lossy compression type
 */
private void assertCompression(TiffCompression compression, boolean lossy) throws Exception {
    IFD ifd = new IFD();
    int w = 64;
    int h = 64;
    int bpp = 8;
    ifd.put(IFD.IMAGE_WIDTH, w);
    ifd.put(IFD.IMAGE_LENGTH, h);
    ifd.put(IFD.BITS_PER_SAMPLE, new int[] { bpp });
    ifd.put(IFD.SAMPLES_PER_PIXEL, 1);
    ifd.put(IFD.LITTLE_ENDIAN, Boolean.TRUE);
    byte[] plane = new byte[w * h * (bpp / 8)];
    for (int i = 0; i < plane.length; i++) {
        plane[i] = (byte) i;
    }
    String beforeCompression, afterCompression, afterDecompression;
    CodecOptions options = compression.getCompressionCodecOptions(ifd);
    byte[] compressed;
    beforeCompression = Hashing.md5().hashBytes(plane).toString();
    compressed = compression.compress(plane, options);
    afterCompression = Hashing.md5().hashBytes(compressed).toString();
    if (compression.equals(TiffCompression.UNCOMPRESSED)) {
        if (!beforeCompression.equals(afterCompression)) {
            fail("Compression: " + compression.getCodecName() + " " + String.format("Compression MD5 %s != %s", beforeCompression, afterCompression));
        }
        afterDecompression = Hashing.md5().hashBytes(compression.decompress(compressed, options)).toString();
        if (!beforeCompression.equals(afterDecompression)) {
            fail("Compression: " + compression.getCodecName() + " " + String.format("Decompression MD5 %s != %s", beforeCompression, afterDecompression));
        }
    } else {
        if (beforeCompression.equals(afterCompression)) {
            fail("Compression: " + compression.getCodecName() + " " + String.format("Compression MD5 %s != %s", beforeCompression, afterCompression));
        }
        afterDecompression = Hashing.md5().hashBytes(compression.decompress(compressed, options)).toString();
        if (!lossy && !beforeCompression.equals(afterDecompression)) {
            fail("Compression: " + compression.getCodecName() + " " + String.format("Decompression MD5 %s != %s", beforeCompression, afterDecompression));
        }
    }
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) IFD(loci.formats.tiff.IFD)

Example 87 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class MinimalTiffReader method get16BitLookupTable.

/* @see loci.formats.IFormatReader#get16BitLookupTable() */
@Override
public short[][] get16BitLookupTable() throws FormatException, IOException {
    FormatTools.assertId(currentId, true, 1);
    if (ifds == null || lastPlane < 0 || lastPlane >= ifds.size())
        return null;
    IFD lastIFD = ifds.get(lastPlane);
    int[] bits = lastIFD.getBitsPerSample();
    if (bits[0] <= 16 && bits[0] > 8) {
        int[] colorMap = tiffParser.getColorMap(lastIFD);
        if (colorMap == null || colorMap.length < 65536 * 3) {
            // it's possible that the LUT is only present in the first IFD
            if (lastPlane != 0) {
                lastIFD = ifds.get(0);
                colorMap = tiffParser.getColorMap(lastIFD);
                if (colorMap == null || colorMap.length < 65536 * 3)
                    return null;
            } else
                return null;
        }
        short[][] table = new short[3][colorMap.length / 3];
        int next = 0;
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table[0].length; j++) {
                table[i][j] = (short) (colorMap[next++] & 0xffff);
            }
        }
        return table;
    }
    return null;
}
Also used : IFD(loci.formats.tiff.IFD)

Example 88 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class TiffWriter method saveBytes.

/**
 * Saves the given image to the specified series in the current file.
 * The IFD hashtable allows specification of TIFF parameters such as bit
 * depth, compression and units.
 */
public void saveBytes(int no, byte[] buf, IFD ifd, int x, int y, int w, int h) throws IOException, FormatException {
    if (checkParams)
        checkParams(no, buf, x, y, w, h);
    if (ifd == null)
        ifd = new IFD();
    MetadataRetrieve retrieve = getMetadataRetrieve();
    int type = FormatTools.pixelTypeFromString(retrieve.getPixelsType(series).toString());
    int index = no;
    int imageWidth = retrieve.getPixelsSizeX(series).getValue().intValue();
    int imageHeight = retrieve.getPixelsSizeY(series).getValue().intValue();
    int currentTileSizeX = getTileSizeX();
    int currentTileSizeY = getTileSizeY();
    if (currentTileSizeX != imageWidth || currentTileSizeY != imageHeight) {
        ifd.put(new Integer(IFD.TILE_WIDTH), new Long(currentTileSizeX));
        ifd.put(new Integer(IFD.TILE_LENGTH), new Long(currentTileSizeY));
    }
    if (currentTileSizeX < w || currentTileSizeY < h) {
        int numTilesX = (w + (x % currentTileSizeX) + currentTileSizeX - 1) / currentTileSizeX;
        int numTilesY = (h + (y % currentTileSizeY) + currentTileSizeY - 1) / currentTileSizeY;
        for (int yTileIndex = 0; yTileIndex < numTilesY; yTileIndex++) {
            for (int xTileIndex = 0; xTileIndex < numTilesX; xTileIndex++) {
                Region tileParams = new Region();
                tileParams.width = xTileIndex < numTilesX - 1 ? currentTileSizeX - (x % currentTileSizeX) : w - (currentTileSizeX * xTileIndex);
                tileParams.height = yTileIndex < numTilesY - 1 ? currentTileSizeY - (y % currentTileSizeY) : h - (currentTileSizeY * yTileIndex);
                tileParams.x = x + (xTileIndex * currentTileSizeX) - (xTileIndex > 0 ? (x % currentTileSizeX) : 0);
                tileParams.y = y + (yTileIndex * currentTileSizeY) - (yTileIndex > 0 ? (y % currentTileSizeY) : 0);
                byte[] tileBuf = getTile(buf, tileParams, new Region(x, y, w, h));
                // This operation is synchronized
                synchronized (this) {
                    // This operation is synchronized against the TIFF saver.
                    synchronized (tiffSaver) {
                        index = prepareToWriteImage(no, tileBuf, ifd, tileParams.x, tileParams.y, tileParams.width, tileParams.height);
                        if (index == -1) {
                            return;
                        }
                    }
                }
                tiffSaver.writeImage(tileBuf, ifd, index, type, tileParams.x, tileParams.y, tileParams.width, tileParams.height, no == getPlaneCount() - 1 && getSeries() == retrieve.getImageCount() - 1);
            }
        }
    } else {
        // This operation is synchronized
        synchronized (this) {
            // This operation is synchronized against the TIFF saver.
            synchronized (tiffSaver) {
                index = prepareToWriteImage(no, buf, ifd, x, y, w, h);
                if (index == -1) {
                    return;
                }
            }
        }
        tiffSaver.writeImage(buf, ifd, index, type, x, y, w, h, no == getPlaneCount() - 1 && getSeries() == retrieve.getImageCount() - 1);
    }
}
Also used : IFD(loci.formats.tiff.IFD) Region(loci.common.Region) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Example 89 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class TiffWriter method saveBytes.

// -- IFormatWriter API methods --
/**
 * @see loci.formats.IFormatWriter#saveBytes(int, byte[], int, int, int, int)
 */
@Override
public void saveBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    IFD ifd = new IFD();
    if (!sequential) {
        TiffParser parser = new TiffParser(currentId);
        try {
            long[] ifdOffsets = parser.getIFDOffsets();
            if (no < ifdOffsets.length) {
                ifd = parser.getIFD(ifdOffsets[no]);
            }
            saveBytes(no, buf, ifd, x, y, w, h);
        } finally {
            RandomAccessInputStream tiffParserStream = parser.getStream();
            if (tiffParserStream != null) {
                tiffParserStream.close();
            }
        }
    } else {
        saveBytes(no, buf, ifd, x, y, w, h);
    }
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 90 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class LeicaReader method isThisType.

/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
    TiffParser tp = new TiffParser(stream);
    IFD ifd = tp.getFirstIFD();
    if (ifd == null)
        return false;
    return ifd.containsKey(new Integer(LEICA_MAGIC_TAG));
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser)

Aggregations

IFD (loci.formats.tiff.IFD)121 TiffParser (loci.formats.tiff.TiffParser)74 RandomAccessInputStream (loci.common.RandomAccessInputStream)51 CoreMetadata (loci.formats.CoreMetadata)33 FormatException (loci.formats.FormatException)32 MetadataStore (loci.formats.meta.MetadataStore)21 IFDList (loci.formats.tiff.IFDList)21 PhotoInterp (loci.formats.tiff.PhotoInterp)18 IOException (java.io.IOException)17 Location (loci.common.Location)15 ArrayList (java.util.ArrayList)14 Timestamp (ome.xml.model.primitives.Timestamp)11 Length (ome.units.quantity.Length)10 Time (ome.units.quantity.Time)8 TiffIFDEntry (loci.formats.tiff.TiffIFDEntry)7 TiffRational (loci.formats.tiff.TiffRational)6 File (java.io.File)5 TiffReader (loci.formats.in.TiffReader)5 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)5 PositiveInteger (ome.xml.model.primitives.PositiveInteger)5