package org.bonej.wrapperPlugins;

import ij.ImagePlus;
import net.imagej.patcher.LegacyInjector;
import org.bonej.utilities.ImagePlusCheck;
import org.scijava.ItemIO;
import org.scijava.command.Command;
import org.scijava.command.ContextCommand;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import sc.fiji.skeletonize3D.Skeletonize3D_;

import static org.bonej.wrapperPlugins.CommonMessages.*;

/**
 * A wrapper plugin to bundle Skeletonize3D into BoneJ2
 *
 * @author Richard Domander
 */
@Plugin(type = Command.class, menuPath = "Plugins>BoneJ>Skeletonise")
public class SkeletoniseWrapper extends ContextCommand {
    static {
        LegacyInjector.preinit();
    }

    /** @implNote Use ImagePlus because of conversion issues of composite images */
    @Parameter(initializer = "initializeImage")
    private ImagePlus inputImage;

    /** @implNote Use ImagePlus because a (converted) Dataset has display issues with a composite image */
    @Parameter(type = ItemIO.OUTPUT)
    private ImagePlus skeleton;

    @Override
    public void run() {
        skeleton = inputImage.duplicate();
        skeleton.setTitle("Skeleton of " + inputImage.getTitle());

        final Skeletonize3D_ skeletoniser = new Skeletonize3D_();
        skeletoniser.setup("", skeleton);
        skeletoniser.run(null);

        skeleton.show();
    }

    @SuppressWarnings("unused")
    private void initializeImage() {
        if (inputImage == null) {
            cancel(NO_IMAGE_OPEN);
            return;
        }

        if (inputImage.getType() != ImagePlus.GRAY8 || !ImagePlusCheck.isBinaryColour(inputImage)) {
            cancel(NOT_8_BIT_BINARY_IMAGE);
        }
    }
}

@@ -49,16 +49,44 @@
 gePlus;%0A
+import ij.plugin.LutLoader;%0A
 import n
@@ -1374,24 +1374,247 @@
 ton.show();%0A
+        if (inputImage.isInvertedLut() != skeleton.isInvertedLut()) %7B%0A            // FIXME Does *not* work in headless mode!%0A            LutLoader lutLoader = new LutLoader();%0A            lutLoader.run(%22invert%22);%0A        %7D%0A
     %7D%0A%0A    @

Add LUT inversion