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.gui.GenericDialog;
014import ij.plugin.PlugIn;
015import imagingbook.common.util.DirectoryWalker;
016import imagingbook.core.jdoc.JavaDocHelp;
017
018import java.io.File;
019import java.util.Collection;
020
021/**
022 * This is a simple tool for converting images of a specific type in a given directory to a new type using ImageJ. There
023 * is no safety net except the option for performing a dry run. Converted files are placed in the same directory as the
024 * original files. Note that nothing is polished, this is intended for quick use only.
025 *
026 * @author WB
027 * @version 2022/12/27
028 */
029public class Convert_Images_To_PNG implements PlugIn, JavaDocHelp {
030        private static String SourceExtension = ".pgm";
031        private static String TargetExtension = ".png";
032        private static String CurrentDirectory =  IJ.getDirectory("current");
033        private static boolean DoDryRun = true;
034        private static boolean InvertImages = false;
035        private static boolean InvertLookupTables = false;
036        
037        public void run(String arg0) {
038                GenericDialog gd = new GenericDialog(this.getClass().getSimpleName());
039                gd.addHelp(getJavaDocUrl());
040                gd.addStringField("Source file extension", SourceExtension);
041                gd.addStringField("Target file extension", TargetExtension);
042                gd.addDirectoryField("Select directory", CurrentDirectory);
043                gd.addCheckbox("Invert images", InvertImages);
044                gd.addCheckbox("Invert lookup tables", InvertLookupTables);
045                gd.addCheckbox("Dry run", DoDryRun);
046
047                gd.showDialog();
048                if (gd.wasCanceled()) {
049                        return;
050                }
051
052                SourceExtension = gd.getNextString();
053                TargetExtension = gd.getNextString();
054                CurrentDirectory = gd.getNextString();
055                InvertImages = gd.getNextBoolean();
056                InvertLookupTables = gd.getNextBoolean();
057                DoDryRun = gd.getNextBoolean();
058
059                // -------------------
060
061                String dir = CurrentDirectory;
062                if (dir == null)
063                        return;
064
065                if (DoDryRun) {
066                        IJ.log("Doing a dry run only, nothing will be changed!");
067                }
068                IJ.log("Scanning directory " + dir + " ...");
069                Collection<String> paths =  new DirectoryWalker(SourceExtension).collectFiles(dir);
070
071                int cnt = 0;
072                for (String p : paths) {
073                        ImagePlus im = IJ.openImage(p);
074                        if (im == null) {
075                                IJ.log("WARNING: could not read/open image" + p);
076                                continue;
077                        }
078                        File f = new File(dir + im.getShortTitle() + TargetExtension);
079                        String savepath = f.getAbsolutePath();
080                        IJ.log("Converting " + p + " --> " + savepath);
081
082                        if (!DoDryRun) {
083                                if (InvertImages)
084                                        IJ.run(im, "Invert", "");       // im.getProcessor().invert();
085                                if (InvertLookupTables)
086                                        IJ.run(im, "Invert LUT", "");
087
088                                IJ.save(im, savepath);
089                        }
090                        im.close();
091                        cnt++;
092                }
093
094                IJ.log("Processed " + cnt + " files.");
095        }
096}
097