001package plugins.tutorial.training;
002
003import icy.gui.dialog.MessageDialog;
004import icy.image.IcyBufferedImage;
005import icy.math.MathUtil;
006import icy.plugin.abstract_.PluginActionable;
007import icy.type.collection.array.Array1DUtil;
008
009public class ModifyImagePlugin3 extends PluginActionable
010{
011    @Override
012    public void run()
013    {
014        IcyBufferedImage image = getActiveImage();
015
016        // check if an image is opened
017        if (image == null)
018        {
019            MessageDialog.showDialog("This plugin needs an opened image.");
020            return;
021        }
022
023        // get a direct reference on the XY planar image data array
024        Object dataArray = image.getDataXY(0);
025
026        // convert the data array in double type array for precise mathematical operations
027        double[] doubleDataArray = Array1DUtil.arrayToDoubleArray(dataArray, image.isSignedDataType());
028
029        // divide contents of the double data array by 2
030        MathUtil.divide(doubleDataArray, 2d);
031
032        // convert the double data array back to the original image data type
033        Array1DUtil.doubleArrayToSafeArray(doubleDataArray, dataArray, image.isSignedDataType());
034
035        // just to let the image know the data has changed (internal updates and view refresh) and also to update cache for volatile image
036        image.setDataXY(0, dataArray);
037    }
038}