
package org.bonej.wrapperPlugins.wrapperUtils;

import net.imagej.ImgPlus;
import net.imagej.axis.CalibratedAxis;
import net.imagej.ops.OpService;
import net.imglib2.img.Img;
import net.imglib2.type.logic.BitType;
import net.imglib2.type.numeric.ComplexType;

import ij.ImagePlus;

/**
 * Miscellaneous utility methods
 *
 * @author Richard Domander
 */
public class Common {

	/**
	 * Duplicates the image without changing the title of the copy, or cropping it
	 * to the ROI.
	 * <p>
	 * Circumvents the default behaviour of {@link ImagePlus#duplicate()}.
	 * </p>
	 * 
	 * @param image an ImageJ1 style ImagePlus.
	 * @return an unchanged copy of the image.
	 */
	public static ImagePlus cleanDuplicate(final ImagePlus image) {
		image.killRoi();
		final ImagePlus copy = image.duplicate();
		image.restoreRoi();
		copy.setTitle(image.getTitle());
		return copy;
	}

	/**
	 * Converts the {@link ImgPlus} to a new ImgPlus with {@link BitType}
	 * elements.
	 * <p>
	 * Also copies ImgPlus metadata.
	 * </p>
	 * 
	 * @param ops an {@link OpService} to find the necessary ops for conversion.
	 * @param imgPlus an image.
	 * @param <C> type of the elements in the input image.
	 * @return the image converted to bit type.
	 */
	public static <C extends ComplexType<C>> ImgPlus<BitType> toBitTypeImgPlus(
		OpService ops, final ImgPlus<C> imgPlus)
	{
		final Img<BitType> convertedImg = ops.convert().bit(imgPlus.getImg());
		final ImgPlus<BitType> convertedImgPlus = new ImgPlus<>(convertedImg);
		copyMetadata(imgPlus, convertedImgPlus);

		return convertedImgPlus;
	}

	/**
	 * Copies image metadata such as name, axis types and calibrations from source
	 * to target.
     *
     * @param source source of metadata.
     * @param target target of metadata.
	 */
	private static void copyMetadata(ImgPlus<?> source, ImgPlus<?> target) {
		target.setName(source.getName());

		final int dimensions = source.numDimensions();
		for (int d = 0; d < dimensions; d++) {
			final CalibratedAxis axis = source.axis(d);
			target.setAxis(axis, d);
		}
	}
}

@@ -42,16 +42,219 @@
 Utils;%0A%0A
+import static org.scijava.ui.DialogPrompt.MessageType.WARNING_MESSAGE;%0Aimport static org.scijava.ui.DialogPrompt.OptionType.OK_CANCEL_OPTION;%0Aimport static org.scijava.ui.DialogPrompt.Result.OK_OPTION;%0A%0A
 import n
@@ -457,16 +457,92 @@
 xType;%0A%0A
+import org.bonej.utilities.ImagePlusUtil;%0Aimport org.scijava.ui.UIService;%0A%0A
 import i
@@ -591,16 +591,17 @@
  methods
+.
 %0A *%0A * @
@@ -1843,24 +1843,828 @@
 mgPlus;%0A%09%7D%0A%0A
+%09/**%0A%09 * Shows a warning dialog about image anisotropy, and asks if the user wants%0A%09 * to continue.%0A%09 *%0A%09 * @param image the current image open in ImageJ.%0A%09 * @param uiService used to display the warning dialog.%0A%09 * @return true if user chose OK_OPTION, or image is not anisotropic. False if%0A%09 *         user chose 'cancel' or they closed the dialog.%0A%09 */%0A%09public static boolean warnAnisotropy(final ImagePlus image,%0A%09%09final UIService uiService)%0A%09%7B%0A%09%09final double anisotropy = ImagePlusUtil.anisotropy(image);%0A%09%09if (anisotropy %3C 1E-3) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%09%09final String anisotropyPercent = String.format(%22(%25.1f %25%25)%22, anisotropy *%0A%09%09%09100.0);%0A%09%09return uiService.showDialog(%22The image is anisotropic %22 +%0A%09%09%09anisotropyPercent + %22. Continue anyway?%22, WARNING_MESSAGE,%0A%09%09%09OK_CANCEL_OPTION) == OK_OPTION;%0A%09%7D%0A%0A
 %09/**%0A%09 * Cop

Add command utility method