Search in sources :

Example 1 with SeekableStream

use of uk.ac.sussex.gdsc.core.ij.io.SeekableStream in project GDSC-SMLM by aherbert.

the class SeriesImageSource method openImage.

private TiffImage openImage(int id, String path) {
    TiffImage tiffImage = null;
    // We only need the meta data for the first image. We then assume check
    // all other images in the series match the pixel type and width of the first.
    final boolean pixelInfoOnly = id != 0;
    // Open using specialised TIFF reader for better non-sequential support
    SeekableStream ss = null;
    try {
        ss = createSeekableStream(path, imageData[id].fileSize);
        final FastTiffDecoder td = FastTiffDecoder.create(ss, path);
        td.setTrackProgress(trackProgress);
        // Try and use the index map
        final IndexMap indexMap = td.getIndexMap();
        // Check the image map is the correct size (only if we have sizes)
        if (indexMap != null && (imageSize == null || indexMap.getSize() == getImageSize(id))) {
            // We need the first IFD to define the image pixel type and width/height
            final ExtendedFileInfo fi = td.getTiffInfo(indexMap, 0, pixelInfoOnly);
            // A byte array seekable stream will ignore the close() method so we can re-use it
            tiffImage = new TiffImage(indexMap, fi, (ss instanceof ByteArraySeekableStream) ? ss : null);
        } else {
            // Reset after reading the index map
            td.reset();
            // Read all the IFDs
            final ExtendedFileInfo[] info = td.getTiffInfo(pixelInfoOnly);
            if (info != null) {
                // A byte array seekable stream will ignore the close() method so we can re-use it
                tiffImage = new TiffImage(info, (ss instanceof ByteArraySeekableStream) ? ss : null);
            }
        }
    } catch (final IOException ioe) {
    // Ignore
    } finally {
        closeInputStream(ss);
    }
    if (tiffImage == null) {
        // Prevent reading again. Skip the storeTiffImage(...) method
        // as that may be optional and we don't want to repeat the error.
        imageData[id].tiffImage = new TiffImage();
    } else {
        storeTiffImage(id, tiffImage);
    }
    return tiffImage;
}
Also used : FastTiffDecoder(uk.ac.sussex.gdsc.core.ij.io.FastTiffDecoder) IndexMap(uk.ac.sussex.gdsc.core.ij.io.FastTiffDecoder.IndexMap) ByteArraySeekableStream(uk.ac.sussex.gdsc.core.ij.io.ByteArraySeekableStream) ByteArraySeekableStream(uk.ac.sussex.gdsc.core.ij.io.ByteArraySeekableStream) SeekableStream(uk.ac.sussex.gdsc.core.ij.io.SeekableStream) FileSeekableStream(uk.ac.sussex.gdsc.core.ij.io.FileSeekableStream) ExtendedFileInfo(uk.ac.sussex.gdsc.core.ij.io.ExtendedFileInfo) IOException(java.io.IOException)

Example 2 with SeekableStream

use of uk.ac.sussex.gdsc.core.ij.io.SeekableStream in project GDSC-SMLM by aherbert.

the class SeriesImageSource method initialise.

/**
 * Initialise the TIFF image sizes and data structures.
 */
private void initialise() {
    if (imageData == null) {
        trackProgress.status("Reading images sizes");
        final Ticker ticker = Ticker.createStarted(trackProgress, images.size(), false);
        // All images are TIFF. Get the size of each and count the total frames.
        imageData = new ImageData[images.size()];
        imageSize = new int[images.size()];
        final String[] names = new String[images.size()];
        frames = 0;
        int ok = 0;
        // We can guess for sequential read
        final boolean estimate = getReadHint() == ReadHint.SEQUENTIAL;
        boolean exact = true;
        for (int i = 0; i < names.length; i++) {
            final String path = images.get(i);
            final File file = new File(path);
            // Get the size of each file so we can determine if
            // they can fit into memory. We only use pre-loading for
            // sequential reading if all images fit into memory.
            final long size = getSize(file);
            try (SeekableStream ss = createSeekableStream(path)) {
                final FastTiffDecoder td = FastTiffDecoder.create(ss, path);
                NumberOfImages numberOfImages;
                if (estimate) {
                    numberOfImages = td.getNumberOfImages(() -> size);
                } else {
                    numberOfImages = td.getNumberOfImages();
                }
                if (numberOfImages.isExact()) {
                    trackProgress.log("%s : images=%d (%d bytes)", path, numberOfImages.getImageCount(), size);
                } else {
                    trackProgress.log("%s : images=%d (approx) (%d bytes)", path, numberOfImages.getImageCount(), size);
                }
                if (estimate) {
                    // Track if this is exact
                    exact = exact && numberOfImages.isExact();
                } else if (numberOfImages.getImageCount() <= 0) {
                    // using the cumulative size array so remove the image.
                    continue;
                }
                frames += numberOfImages.getImageCount();
                imageSize[ok] = frames;
                imageData[ok] = new ImageData(size);
                names[ok] = path;
                ok++;
            } catch (final Throwable ex) {
                if (estimate) {
                    // This is an untested method so log the error
                    ex.printStackTrace();
                }
            }
            ticker.tick();
        }
        trackProgress.status("");
        ticker.stop();
        if (ok < images.size()) {
            imageSize = Arrays.copyOf(imageSize, ok);
            imageData = Arrays.copyOf(imageData, ok);
            images.clear();
            images.addAll(Arrays.asList(names));
        }
        // No support for non-sequential access
        if (!exact) {
            imageSize = null;
        }
    }
}
Also used : FastTiffDecoder(uk.ac.sussex.gdsc.core.ij.io.FastTiffDecoder) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) NumberOfImages(uk.ac.sussex.gdsc.core.ij.io.FastTiffDecoder.NumberOfImages) ByteArraySeekableStream(uk.ac.sussex.gdsc.core.ij.io.ByteArraySeekableStream) SeekableStream(uk.ac.sussex.gdsc.core.ij.io.SeekableStream) FileSeekableStream(uk.ac.sussex.gdsc.core.ij.io.FileSeekableStream) File(java.io.File)

Aggregations

ByteArraySeekableStream (uk.ac.sussex.gdsc.core.ij.io.ByteArraySeekableStream)2 FastTiffDecoder (uk.ac.sussex.gdsc.core.ij.io.FastTiffDecoder)2 FileSeekableStream (uk.ac.sussex.gdsc.core.ij.io.FileSeekableStream)2 SeekableStream (uk.ac.sussex.gdsc.core.ij.io.SeekableStream)2 File (java.io.File)1 IOException (java.io.IOException)1 ExtendedFileInfo (uk.ac.sussex.gdsc.core.ij.io.ExtendedFileInfo)1 IndexMap (uk.ac.sussex.gdsc.core.ij.io.FastTiffDecoder.IndexMap)1 NumberOfImages (uk.ac.sussex.gdsc.core.ij.io.FastTiffDecoder.NumberOfImages)1 Ticker (uk.ac.sussex.gdsc.core.logging.Ticker)1