package org.bonej.wrapperPlugins;

import ij.ImagePlus;
import ij.ImageStack;
import net.imagej.Dataset;
import net.imagej.patcher.LegacyInjector;
import net.imglib2.IterableInterval;
import org.bonej.utilities.AxisUtils;
import org.bonej.utilities.ElementUtil;
import org.scijava.command.Command;
import org.scijava.command.ContextCommand;
import org.scijava.convert.ConvertService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;
import sc.fiji.analyzeSkeleton.AnalyzeSkeleton_;
import sc.fiji.analyzeSkeleton.Graph;
import sc.fiji.skeletonize3D.Skeletonize3D_;

import static org.bonej.wrapperPlugins.CommonMessages.*;
import static org.scijava.ui.DialogPrompt.MessageType.INFORMATION_MESSAGE;

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

    /** @implNote Use Dataset because it has a conversion to ImagePlus */
    @Parameter(initializer = "initializeImage")
    private Dataset inputImage;

    @Parameter
    private ConvertService convertService;

    @Parameter
    private UIService uiService;

    @Override
    public void run() {
        final ImagePlus skeleton = convertService.convert(inputImage, ImagePlus.class);

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

        final int iterations = skeletoniser.getThinningIterations();
        if (iterations > 1) {
            skeleton.show();
            uiService.showDialog(GOT_SKELETONISED, INFORMATION_MESSAGE);
        }

        final AnalyzeSkeleton_ analyser = new AnalyzeSkeleton_();

        analyser.setup("", skeleton);
        analyser.run(null);
        final Graph[] graphs = analyser.getGraphs();
        // Get resultImage to check that the plugin actually ran, and wasn't cancelled by the user
        final ImageStack resultImage = analyser.getResultImage(false);

        if ((graphs == null || graphs.length == 0) && resultImage != null) {
            uiService.showDialog(NO_SKELETONS, INFORMATION_MESSAGE);
        }
    }

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

        final long spatialDimensions = AxisUtils.countSpatialDimensions(inputImage);
        if (spatialDimensions < 2 || spatialDimensions > 3) {
            cancel(NOT_2D_OR_3D_IMAGE);
            return;
        }

        IterableInterval interval = inputImage;
        if (inputImage.getValidBits() != 8 || !ElementUtil.isColorsBinary(interval)) {
            cancel(NOT_8_BIT_BINARY_IMAGE);
            return;
        }

        if (!convertService.supports(inputImage, ImagePlus.class)) {
            cancel(CANNOT_CONVERT_TO_IMAGE_PLUS);
        }
    }
}

@@ -75,35 +75,8 @@
 ck;%0A
-import net.imagej.Dataset;%0A
 impo
@@ -124,114 +124,42 @@
 ort 
-net.imglib2.IterableInterval;%0Aimport org.bonej.utilities.AxisUtils;%0Aimport org.bonej.utilities.ElementUtil
+org.bonej.utilities.ImagePlusCheck
 ;%0Aim
@@ -239,51 +239,8 @@
 nd;%0A
-import org.scijava.convert.ConvertService;%0A
 impo
@@ -915,23 +915,25 @@
 ote Use 
-Dataset
+ImagePlus
  because
@@ -937,16 +937,10 @@
 use 
-it has a
+of
  con
@@ -951,19 +951,33 @@
 ion 
-to I
+issues of composite i
 mage
-Plu
 s */
@@ -1041,15 +1041,17 @@
 ate 
-Dataset
+ImagePlus
  inp
@@ -1064,67 +1064,8 @@
 e;%0A%0A
-    @Parameter%0A    private ConvertService convertService;%0A%0A
     
@@ -1186,58 +1186,29 @@
 n = 
-convertService.convert(inputImage, ImagePlus.class
+inputImage.duplicate(
 );%0A%0A
@@ -2261,492 +2261,96 @@
     
-final long spatialDimensions = AxisUtils.countSpatialDimensions(inputImage);%0A        if (spatialDimensions %3C 2 %7C%7C spatialDimensions %3E 3) %7B%0A            cancel(NOT_2D_OR_3D_IMAGE);%0A            return;%0A        %7D%0A%0A        IterableInterval interval = inputImage;%0A        if (inputImage.getValidBits() != 8 %7C%7C !ElementUtil.isColorsBinary(interval)) %7B%0A            cancel(NOT_8_BIT_BINARY_IMAGE);%0A            return;%0A        %7D%0A%0A        if (!convertService.supports(inputImage, ImagePlus.class
+if (inputImage.getType() != ImagePlus.GRAY8 %7C%7C !ImagePlusCheck.isBinaryColour(inputImage
 )) %7B
@@ -2373,36 +2373,30 @@
 cel(
-CAN
 NOT_
-CONVERT_TO
+8_BIT_BINARY
 _IMAGE
-_PLUS
 );%0A 

Revert to ImagePlus