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.GenericDialog;
015import ij.plugin.PlugIn;
016import imagingbook.common.ij.GuiTools;
017import imagingbook.core.jdoc.JavaDocHelp;
018
019/**
020 * ImageJ plugin, resizes the window of the given image to fit an arbitrary, user-specified magnification factor. The
021 * resulting window size is limited by the current screen size. The window size is reduced if too large but the given
022 * magnification factor remains always unchanged.
023 *
024 * @author WB
025 * @version 2020/10/08
026 */
027public class Zoom_Exact implements PlugIn, JavaDocHelp {
028        
029        private static boolean LOG_OUTPUT = false;
030        
031        @Override
032        public void run(String arg) {
033                ImagePlus im = WindowManager.getCurrentImage();
034                if (im == null) {
035                        IJ.showMessage("No image open");
036                        return;
037                }
038                
039                GenericDialog gd = new GenericDialog("Zoom Exact");
040                gd.addHelp(getJavaDocUrl());
041                gd.addNumericField("Magnification (%): ", GuiTools.getMagnification(im) * 100, 2);
042                gd.addCheckbox("Log output", LOG_OUTPUT);
043                gd.showDialog();
044                if (gd.wasCanceled())
045                        return;
046                
047                double mag = gd.getNextNumber() / 100.0;
048                LOG_OUTPUT = gd.getNextBoolean();
049                
050                if (mag < 0.001) {
051                        IJ.showMessage(String.format("Out of range magnification:\n%.3f", mag));
052                        return;
053                }
054                
055                if (GuiTools.zoomExact(im, mag)) {
056                        if (LOG_OUTPUT) {
057                                IJ.log(String.format("new magnification: %.3f",  GuiTools.getMagnification(im)));
058                        }
059                }
060                else {
061                        IJ.showMessage(Zoom_Exact.class.getSimpleName() + " failed");
062                }
063        }
064}