001package plugins.tutorial.training;
002
003import icy.gui.dialog.MessageDialog;
004import icy.image.IcyBufferedImage;
005import icy.plugin.abstract_.PluginActionable;
006
007public class ModifyImagePlugin2 extends PluginActionable
008{
009    @Override
010    public void run()
011    {
012        IcyBufferedImage image = getActiveImage();
013
014        // check if an image is opened
015        if (image == null)
016        {
017            MessageDialog.showDialog("This plugin needs an opened image.");
018            return;
019        }
020
021        int w = image.getSizeX();
022        int h = image.getSizeY();
023
024        // inform that we start image modification
025        image.beginUpdate();
026        try
027        {
028            for (int x = 0; x < w; x++)
029            {
030                for (int y = 0; y < h; y++)
031                {
032                    // set pixel intensity to half of its original value
033                    image.setData(x, y, 0, image.getData(x, y, 0) / 2);
034                }
035            }
036        }
037        finally
038        {
039            // inform that we are done with image modification
040            image.endUpdate();
041        }
042    }
043}