Search in sources :

Example 11 with ChannelFiller

use of loci.formats.ChannelFiller 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)

Example 12 with ChannelFiller

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

the class BaseModelReaderTest method testSetId.

@Test
public void testSetId() throws Exception {
    reader = new MinMaxCalculator(new ChannelSeparator(new ChannelFiller(new ImageReader())));
    metadata = new OMEXMLMetadataImpl();
    reader.setMetadataStore(metadata);
    reader.setId(temporaryFile.getAbsolutePath());
}
Also used : MinMaxCalculator(loci.formats.MinMaxCalculator) ChannelFiller(loci.formats.ChannelFiller) ImageReader(loci.formats.ImageReader) ChannelSeparator(loci.formats.ChannelSeparator) OMEXMLMetadataImpl(loci.formats.ome.OMEXMLMetadataImpl) Test(org.testng.annotations.Test)

Aggregations

ChannelFiller (loci.formats.ChannelFiller)12 ChannelSeparator (loci.formats.ChannelSeparator)11 ImageReader (loci.formats.ImageReader)11 MinMaxCalculator (loci.formats.MinMaxCalculator)11 DimensionSwapper (loci.formats.DimensionSwapper)6 ChannelMerger (loci.formats.ChannelMerger)5 FormatException (loci.formats.FormatException)5 Test (org.testng.annotations.Test)5 FileStitcher (loci.formats.FileStitcher)4 IFormatReader (loci.formats.IFormatReader)4 OMEXMLMetadataImpl (loci.formats.ome.OMEXMLMetadataImpl)4 IOException (java.io.IOException)3 DataProvider (org.testng.annotations.DataProvider)3 Location (loci.common.Location)2 DependencyException (loci.common.services.DependencyException)2 ServiceException (loci.common.services.ServiceException)2 ServiceFactory (loci.common.services.ServiceFactory)2 Memoizer (loci.formats.Memoizer)2 MissingLibraryException (loci.formats.MissingLibraryException)2 OMEXMLService (loci.formats.services.OMEXMLService)2