001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009
010package imagingbook.lib.ij;
011import java.awt.Graphics;
012import java.awt.Graphics2D;
013import java.awt.RenderingHints;
014import java.awt.image.BufferedImage;
015
016import ij.ImagePlus;
017import ij.gui.ImageCanvas;
018import ij.gui.Overlay;
019import ij.plugin.frame.Recorder;
020import ij.process.ColorProcessor;
021import ij.process.ImageProcessor;
022
023/**
024 * This class extends ImagePlus to allow flattening of overlays with antialiasing.
025 * As of version 1.47o (26 April 2013) implemented in ImageJ and controllable
026 * by ImagePlus.setAntialiasRendering(boolean). Thus not needed any longer.
027 * @author WB
028 *
029 */
030
031@Deprecated
032public class ImagePlusForAntialiasedFlattening extends ImagePlus {
033        
034        private boolean antialiasRendering = true;
035        
036        public void setAntialiasRendering(boolean antialiasRendering) {
037                this.antialiasRendering = antialiasRendering;
038        }
039        
040        public ImagePlusForAntialiasedFlattening(String title, ImageProcessor ip) {
041                super(title, ip);
042        }
043
044    /** Returns a "flattened" version of this image, in RGB format. */
045        @Override
046    public ImagePlus flatten() {
047        ImagePlus imp2 = createImagePlus();
048        String title = "Flat_"+getTitle();
049        ImageCanvas ic2 = new ImageCanvas(imp2);
050        //imp2.flatteningCanvas = ic2;
051        
052        imp2.setRoi(getRoi());
053        if (getStackSize()>1) {
054            imp2.setStack(getStack());
055            imp2.setSlice(getCurrentSlice());
056            if (isHyperStack()) {
057                imp2.setDimensions(getNChannels(),getNSlices(),getNFrames());
058                imp2.setPosition(getChannel(),getSlice(),getFrame());
059                imp2.setOpenAsHyperStack(true);
060            }
061        }
062        ImageCanvas ic = getCanvas();
063        Overlay overlay2 = getOverlay();
064        ic2.setOverlay(overlay2);
065        if (ic!=null) {
066            ic2.setShowAllROIs(ic.getShowAllROIs());
067            //double mag = ic.getMagnification();
068            //if (mag<1.0) ic2.setMagnification(mag);
069        }
070        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
071        Graphics g = bi.getGraphics();
072        
073        // start of Wilhelm's changes: ------------------------------------------------
074        Graphics2D gg = (Graphics2D)g;
075        gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
076                        (antialiasRendering) ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
077        gg.drawImage(getImage(), 0, 0, null);
078        ic2.paint(gg);
079        // end of Wilhelm's changes ------------------------------------------------
080        
081        //imp2.flatteningCanvas = null;
082        if (Recorder.record) Recorder.recordCall("imp = IJ.getImage().flatten();");
083        return new ImagePlus(title, new ColorProcessor(bi));
084    }
085
086}