Search in sources :

Example 1 with NumberOfImages

use of uk.ac.sussex.gdsc.core.ij.io.FastTiffDecoder.NumberOfImages 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

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