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-2023 Wilhelm Burger, Mark J. Burge. All rights reserved.
007 * Visit https://imagingbook.com for additional details.
008 ******************************************************************************/
009package Tools_;
010
011import ij.IJ;
012import ij.ImagePlus;
013import ij.WindowManager;
014import ij.gui.Roi;
015import ij.plugin.PlugIn;
016import imagingbook.common.ij.GuiTools;
017import imagingbook.core.jdoc.JavaDocHelp;
018
019import java.awt.Rectangle;
020
021/**
022 * <p>
023 * ImageJ plugin, zooms the current image such that the given selection (ROI) fits the image window. The top-left anchor
024 * corner of the new view is set to the top-left corner of the current ROI (which may be of any type). The magnification
025 * factor is determined from the width/height of the ROI's bounding rectangle, such that the selected box always fits
026 * entirely into the current image window. If the resulting view has no complete coverage by the source image, the
027 * plugin does nothing. The size of the existing image window is never changed.
028 * </p>
029 * <p>
030 * The functionality is similar to ImageJ's "Image &rarr; Zoom &rarr; To Selection" operator which, however, only
031 * supports a limited number of predefined zoom factors.
032 * </p>
033 *
034 * @author WB
035 * @version 2020/12/17
036 */
037public class Zoom_To_Selection implements PlugIn, JavaDocHelp {
038        
039        private static boolean LogOutput = false;
040        
041        @Override
042        public void run(String arg) {
043                
044                ImagePlus im = WindowManager.getCurrentImage();
045                if (im == null) {
046                        IJ.showMessage("No image open");
047                        return;
048                }
049                Roi roi = im.getRoi();
050                if (roi == null) {
051                        IJ.showMessage("Selection required");
052                        return;
053                }
054                
055                Rectangle bounds = roi.getBounds();
056                if (bounds.width == 0 || bounds.height == 0) {
057                        IJ.showMessage("Selected width and height must not be zero");
058                        return;
059                }
060                
061                double xmag = (double) im.getWidth() / bounds.width;
062                double ymag = (double) im.getHeight() / bounds.height;  
063                double mag =  Math.min(xmag, ymag);
064                
065                Rectangle srcRect = GuiTools.setImageView(im, mag, bounds.x, bounds.y);
066                
067                if (srcRect == null) {
068                        IJ.showMessage("Failed to set view");
069                }
070                else {
071                        if (LogOutput) {
072                                IJ.log(String.format("new magnification: %.3f",  GuiTools.getMagnification(im)));
073                        }
074                }
075        }
076        
077}