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 Ch21_Geometric_Operations;
010
011import ij.ImagePlus;
012import ij.plugin.PlugIn;
013import ij.process.ByteProcessor;
014import imagingbook.common.image.ImageGraphics;
015import imagingbook.core.jdoc.JavaDocHelp;
016
017import java.awt.Font;
018import java.awt.Graphics2D;
019
020/**
021 * <p>
022 * This ImageJ plugin draws a test grid in a new image. It uses anti-aliased drawing operations provided by
023 * imagingbook's {@link ImageGraphics} class. Used in [1] for testing geometric transformations (see Fig. 21.6) and
024 * interpolation methods (see Fig. 22.25).
025 * </p>
026 * <p>
027 * [1] W. Burger, M.J. Burge, <em>Digital Image Processing &ndash; An Algorithmic Introduction</em>, 3rd ed, Springer
028 * (2022).
029 * </p>
030 *
031 * @author WB
032 * @version 2022/11/28
033 * @see ImageGraphics
034 */
035public class Draw_Test_Grid implements PlugIn, JavaDocHelp {
036        
037        private static int W = 400;
038        private static int H = 400;
039        private static int xStep = 20;
040        private static int yStep = 20;
041        private static int xStart = 100;
042        private static int yStart = 100;
043        private static int xN = 10;
044        private static int yN = 10;
045        
046        private static int foreground = 0;
047        private static int background = 255;
048        
049    @Override
050        public void run(String arg) {
051        ByteProcessor ip = new ByteProcessor(W, H);
052        ip.setValue(background);
053        ip.fill();
054        
055        try (ImageGraphics ig = new ImageGraphics(ip)) {
056                        ig.setColor(foreground);
057                        ig.setLineWidth(1.0);
058                        
059                        int y = yStart;
060                int x1 = xStart;
061                int x2 = xStart + xN * xStep;
062                        for (int j = 0; j <= yN; j++) {
063                                ig.drawLine(x1, y, x2, y);
064                                y = y + yStep;
065                        }
066                        
067                        int x = xStart;
068                        int y1 = yStart;
069                        int y2 = yStart + yN * yStep;
070                        for (int i = 0; i <= xN; i++) {
071                                ig.drawLine(x, y1, x, y2);
072                                x = x + xStep;
073                        }
074                        
075                        ig.drawLine(0, 0, W - 1, H - 1);
076                        ig.drawOval(xStart, yStart, W/2, H/2);
077                        
078                        Graphics2D g = ig.getGraphics2D();
079                        g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 18));
080                        float xLeft = 8;
081                        float yTop  = 19;
082                        float xRight = W - 17;
083                        float yBot  = H - 10;
084                        g.drawString("1", xLeft, yTop);
085                        g.drawString("2", xRight, yTop);
086                        g.drawString("3", xRight, yBot);
087                        g.drawString("4", xLeft, yBot);
088        }
089        
090        new ImagePlus("Grid",ip).show();
091    }
092}