Search in sources :

Example 6 with DimensionSwapper

use of loci.formats.DimensionSwapper in project bioformats by openmicroscopy.

the class WrapperTest method createWrappers.

@DataProvider(name = "wrappers")
public Object[][] createWrappers() {
    Location.mapId(TEST_FILE, TEST_FILE);
    Object[][] wrappers = new Object[][] { { new ChannelFiller() }, { new ChannelMerger() }, { new ChannelSeparator() }, { new DimensionSwapper() }, { new FileStitcher() }, { new ImageReader() }, { new MinMaxCalculator() }, { new Memoizer() } };
    for (int i = 0; i < wrappers.length; i++) {
        IFormatReader reader = (IFormatReader) wrappers[i][0];
        try {
            reader.setId(TEST_FILE);
        } catch (FormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return wrappers;
}
Also used : IFormatReader(loci.formats.IFormatReader) Memoizer(loci.formats.Memoizer) ChannelMerger(loci.formats.ChannelMerger) ChannelFiller(loci.formats.ChannelFiller) IOException(java.io.IOException) ChannelSeparator(loci.formats.ChannelSeparator) FormatException(loci.formats.FormatException) FileStitcher(loci.formats.FileStitcher) MinMaxCalculator(loci.formats.MinMaxCalculator) DimensionSwapper(loci.formats.DimensionSwapper) ImageReader(loci.formats.ImageReader) DataProvider(org.testng.annotations.DataProvider)

Example 7 with DimensionSwapper

use of loci.formats.DimensionSwapper in project bioformats by openmicroscopy.

the class Colorizer method applyColors.

// -- Colorizer methods --
public List<ImagePlus> applyColors(List<ImagePlus> imps) {
    final ImporterOptions options = process.getOptions();
    final ImageProcessorReader reader = process.getReader();
    final DimensionSwapper dimSwapper = process.getDimensionSwapper();
    final ChannelFiller channelFiller = process.getChannelFiller();
    final ImageReader imageReader = process.getImageReader();
    for (int i = 0; i < imps.size(); i++) {
        ImagePlus imp = imps.get(i);
        final int series = (Integer) imp.getProperty(ImagePlusReader.PROP_SERIES);
        reader.setSeries(series);
        // get LUT for each channel
        final String stackOrder = dimSwapper.getDimensionOrder();
        final int zSize = imp.getNSlices();
        final int cSize = imp.getNChannels();
        final int tSize = imp.getNFrames();
        final int stackSize = imp.getStackSize();
        final LUT[] channelLUTs = new LUT[cSize];
        boolean hasChannelLUT = false;
        for (int c = 0; c < cSize; c++) {
            final int index = FormatTools.getIndex(stackOrder, zSize, cSize, tSize, stackSize, 0, c, 0);
            channelLUTs[c] = (LUT) imp.getProperty(ImagePlusReader.PROP_LUT + index);
            if (channelLUTs[c] != null)
                hasChannelLUT = true;
        }
        // compute color mode and LUTs to use
        int mode = -1;
        LUT[] luts;
        if (options.isColorModeDefault()) {
            // NB: Default color mode behavior depends on the situation.
            final boolean isRGB = reader.isRGB() || imageReader.isRGB();
            if (isRGB || channelFiller.isFilled()) {
                // NB: The original data had more than one channel per plane
                // (e.g., RGB image planes), so we use the composite display mode.
                mode = CompositeImage.COMPOSITE;
                // preserve original LUTs
                luts = makeLUTs(channelLUTs, true);
            } else if (hasChannelLUT) {
                // NB: The original data had only one channel per plane,
                // but had at least one lookup table defined. We use the color
                // display mode, with missing LUTs as grayscale.
                mode = CompositeImage.COLOR;
                // preserve original LUTs
                luts = makeLUTs(channelLUTs, true);
            } else {
                // NB: The original data had only one channel per plane,
                // and had no lookup tables defined, so we use the grayscale mode.
                mode = CompositeImage.GRAYSCALE;
                luts = null;
            }
        } else if (options.isColorModeComposite()) {
            mode = CompositeImage.COMPOSITE;
            // preserve existing channel LUTs
            luts = makeLUTs(channelLUTs, true);
        } else if (options.isColorModeColorized()) {
            mode = CompositeImage.COLOR;
            // preserve existing channel LUTs
            luts = makeLUTs(channelLUTs, true);
        } else if (options.isColorModeGrayscale()) {
            mode = CompositeImage.GRAYSCALE;
            // use default (grayscale) channel LUTs
            luts = null;
        } else if (options.isColorModeCustom()) {
            mode = CompositeImage.COLOR;
            // override any existing channel LUTs
            luts = makeLUTs(series);
        } else {
            throw new IllegalStateException("Invalid color mode: " + options.getColorMode());
        }
        // apply color mode and LUTs
        final boolean doComposite = !options.isViewStandard() && mode != -1 && cSize > 1 && cSize <= 7;
        if (doComposite) {
            final ImagePlus toClose = imp;
            CompositeImage compImage = new CompositeImage(imp, mode) {

                @Override
                public void close() {
                    super.close();
                    toClose.close();
                }

                @Override
                public void show(String message) {
                    super.show(message);
                    // see ticket #12267
                    if (toClose instanceof VirtualImagePlus) {
                        int channel = getChannel();
                        double min = getDisplayRangeMin();
                        double max = getDisplayRangeMax();
                        for (int c = 0; c < cSize; c++) {
                            setPositionWithoutUpdate(c + 1, getSlice(), getFrame());
                            setDisplayRange(min, max);
                        }
                        reset();
                        setPosition(channel, getSlice(), getFrame());
                    }
                }
            };
            compImage.setProperty(ImagePlusReader.PROP_SERIES, series);
            if (luts != null)
                compImage.setLuts(luts);
            imps.set(i, compImage);
            imp = compImage;
        } else {
            // NB: Cannot use CompositeImage for some reason.
            if (luts != null && luts.length > 0 && luts[0] != null) {
                if (imp instanceof VirtualImagePlus) {
                    ((VirtualImagePlus) imp).setLUTs(luts);
                } else if (cSize == 1)
                    imp.getProcessor().setColorModel(luts[0]);
            }
            if (mode != -1 && cSize > 7) {
                // NB: Cannot use CompositeImage with more than seven channels.
                BF.warn(options.isQuiet(), "Data has too many channels for " + options.getColorMode() + " color mode");
            }
        }
        applyDisplayRanges(imp, series);
    }
    return imps;
}
Also used : ImageProcessorReader(loci.plugins.util.ImageProcessorReader) VirtualImagePlus(loci.plugins.util.VirtualImagePlus) ChannelFiller(loci.formats.ChannelFiller) LUT(ij.process.LUT) VirtualImagePlus(loci.plugins.util.VirtualImagePlus) ImagePlus(ij.ImagePlus) CompositeImage(ij.CompositeImage) DimensionSwapper(loci.formats.DimensionSwapper) ImageReader(loci.formats.ImageReader)

Aggregations

DimensionSwapper (loci.formats.DimensionSwapper)7 ChannelFiller (loci.formats.ChannelFiller)6 ChannelSeparator (loci.formats.ChannelSeparator)5 FormatException (loci.formats.FormatException)5 ImageReader (loci.formats.ImageReader)5 MinMaxCalculator (loci.formats.MinMaxCalculator)5 IOException (java.io.IOException)4 ChannelMerger (loci.formats.ChannelMerger)4 IFormatReader (loci.formats.IFormatReader)4 DataProvider (org.testng.annotations.DataProvider)4 FileStitcher (loci.formats.FileStitcher)3 Memoizer (loci.formats.Memoizer)2 ImageProcessorReader (loci.plugins.util.ImageProcessorReader)2 CompositeImage (ij.CompositeImage)1 ImagePlus (ij.ImagePlus)1 LUT (ij.process.LUT)1 File (java.io.File)1 Location (loci.common.Location)1 DependencyException (loci.common.services.DependencyException)1 ServiceException (loci.common.services.ServiceException)1