001/*
002 * Copyright 2010, 2011 Institut Pasteur.
003 * 
004 * This file is part of ICY.
005 * 
006 * ICY is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * ICY is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
018 */
019package plugins.tutorial.basics;
020
021import icy.gui.dialog.MessageDialog;
022import icy.gui.frame.progress.AnnounceFrame;
023import icy.image.IcyBufferedImage;
024import icy.math.MathUtil;
025import icy.plugin.abstract_.PluginActionable;
026import icy.type.collection.array.Array1DUtil;
027
028/**
029 * This plugin do a simple intensify operation on current opened image
030 * 
031 * @author Stephane
032 */
033public class SimpleIntensify extends PluginActionable
034{
035    @Override
036    public void run()
037    {
038        // get focused image
039        IcyBufferedImage image = getActiveImage();
040
041        // check if the image exists
042        if (image == null)
043        {
044            MessageDialog.showDialog("This plugin need a valid opened image.", MessageDialog.WARNING_MESSAGE);
045            return;
046        }
047
048        // display what this tutorial perform.
049        new AnnounceFrame("This tutorial multiply image intensity by a factor of 2, regardless the image data type.");
050
051        // get first channel image data array regardless of the data type
052        Object dataArray = image.getDataXY(0);
053
054        // transform data array to double data array for easy processing
055        double[] doubleDataArray = Array1DUtil.arrayToDoubleArray(dataArray, image.isSignedDataType());
056
057        // multiply by a factor of 2
058        MathUtil.mul(doubleDataArray, 2d);
059
060        // transform data back to original data type.
061        // 'Safe' methods take care of overflow with the destination data type.
062        Array1DUtil.doubleArrayToSafeArray(doubleDataArray, dataArray, image.isSignedDataType());
063
064        // just to let the image know the data has changed (internal updates and view refresh) and also to update cache for volatile image
065        image.setDataXY(0, dataArray);
066    }
067}