<commit_before>package ijx.etc;

import ij.IJ;
import ij.ImageJ;

/**
 *
 * @author GBH
 */
public class Ij1PluginRunner {

    public static void main(String[] args) {
        runPlugin("PluginClass");
    }
    public static boolean runPlugin(String plugin) {
        if (IJ.getInstance() == null) {
            new ImageJ(ImageJ.NO_SHOW);
            if (IJ.getInstance() == null) {
                System.err.println("ImageJ failed to start.");
                return false;
            } else {
                IJ.showMessage("OK -- ImageJ Started.");
            }
        }
        // create the ImagePlus
        // ?? How do we handle the need for multiple image selections ??
        //    We would need to mirror the open images in IJ2 to the IJ1 WindowManager, no?
        
        // Set as the current image in ImageJ
        IJ.getImage();
        return true;
    }<commit_msg>Ij1PluginRunner with test (main) that loads an Image<T>, creates a BridgeStack, and executes an IJ1 plugin (Flip vertically).<commit_after>package ijx.etc;

import ij.IJ;
import ij.ImageJ;
import ijx.IjxImagePlus;
import ijx.IjxImageStack;
import imagej.ij1bridge.BridgeStack;
import imagej.ij1bridge.ImgLibDataset;
import imagej.ij1bridge.ImgLibProcessorFactory;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import loci.formats.FormatException;
import mpicbg.imglib.image.Image;
import mpicbg.imglib.io.ImageOpener;
import mpicbg.imglib.type.numeric.RealType;

/**
 *
 * @author GBH
 */
public class Ij1PluginRunner {

    public static <T extends RealType<T>> void main(String[] args) {
        String filename = "testpattern.tif";
        final ImageOpener imageOpener = new ImageOpener();
        try {
            Image<T> img = imageOpener.openImage(filename);
            String command = "Flip Vertically";
            String plugin = "ij.plugin.filter.Transformer(\"flipv\")";
            String imgName = "TestPattern";
            runPlugin(command, plugin, imgName, img);
            //runPlugin("PluginClass", );
        } catch (FormatException ex) {
            Logger.getLogger(Ij1PluginRunner.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Ij1PluginRunner.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static <T extends RealType<T>> boolean runPlugin(String command, String plugin, String imgName, Image<T> img) {

        // Start ImageJ, NO_SHOW
        if (IJ.getInstance() == null) {
            new ImageJ(ImageJ.NO_SHOW);
            if (IJ.getInstance() == null) {
                System.err.println("ImageJ failed to start.");
                return false;
            } else {
                IJ.showMessage("OK -- ImageJ Started.");
            }
        }

        // create the ImagePlus
        ImgLibDataset<T> dataset = new ImgLibDataset<T>((Image<T>) img);
        BridgeStack stack = new BridgeStack(dataset, new ImgLibProcessorFactory(img));
        ij.ImagePlus imp = new ij.ImagePlus(imgName, (ij.ImageStack) stack);
        // ?? How do we handle the need for multiple image selections ??
        //    We would need to mirror the open images in IJ2 to the IJ1 WindowManager, no?
        // Perhaps pass (Image<T>[] imgs)
        imp.show();
        runCommand(command, plugin, false);
        return true;
    }

    public static Object runCommand(String commandName, String className, boolean createNewLoader) {
        if (className != null) {
            String arg = "";
            if (className.endsWith("\")")) {
                // extract string argument (e.g. className("arg"))
                int argStart = className.lastIndexOf("(\"");
                if (argStart > 0) {
                    arg = className.substring(argStart + 2, className.length() - 2);
                    className = className.substring(0, argStart);
                }
            }
            Object output = runUserPlugIn(commandName, className, arg, false);
            return output;
        }
        return null;
    }

    public static Object runUserPlugIn(String commandName, String className, String arg, boolean createNewLoader) {
        if (createNewLoader) {
            // ??  classLoader = null;
        }
        ClassLoader loader = ij.IJ.getClassLoader();
        Object thePlugIn = null;
        try {
            thePlugIn = (loader.loadClass(className)).newInstance();
            if (thePlugIn instanceof ij.plugin.PlugIn) {
                ((ij.plugin.PlugIn) thePlugIn).run(arg);
            } else if (thePlugIn instanceof ij.plugin.filter.PlugInFilter) {
                new ij.plugin.filter.PlugInFilterRunner(thePlugIn, commandName, arg);
            }
        } catch (ClassNotFoundException e) {
            if (className.indexOf('_') != -1 /*&& !suppressPluginNotFoundError*/) {
                ij.IJ.error("Plugin or class not found: \"" + className + "\"\n(" + e + ")");
            }
        } catch (NoClassDefFoundError e) {
            int dotIndex = className.indexOf('.');
            String cause = e.getMessage();
            int parenIndex = cause.indexOf('(');
            if (parenIndex >= 1) {
                cause = cause.substring(0, parenIndex - 1);
            }
            boolean correctClass = cause.endsWith(dotIndex < 0
                    ? className : className.substring(dotIndex + 1));
            if (!correctClass /*&& !suppressPluginNotFoundError*/) {
                ij.IJ.error("Plugin " + className
                        + " did not find required class: "
                        + e.getMessage());
            }
            if (correctClass && dotIndex >= 0) {
                return runUserPlugIn(commandName, className.substring(dotIndex + 1), arg, createNewLoader);
            }
            if (className.indexOf('_') != -1 /*&& !suppressPluginNotFoundError*/) {
                ij.IJ.error("Plugin or class not found: \"" + className + "\"\n(" + e + ")");
            }
        } catch (InstantiationException e) {
            ij.IJ.error("Unable to load plugin (ins)");
        } catch (IllegalAccessException e) {
            ij.IJ.error("Unable to load plugin, possibly \nbecause it is not public.");
        }
//        if (redirectErrorMessages && !"HandleExtraFileTypes".equals(className)) {
//            redirectErrorMessages = false;
//        }
//        suppressPluginNotFoundError = false;
        return thePlugIn;
    }
{'fraction_non_alphanumeric': 0.1175378905041757, 'fraction_numerical': 0.004021033096195484, 'mean_word_length': 2.0375763269140443, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5705602', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1757, 'n_words': 455}