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.plugin.abstract_.PluginActionable;
025import icy.sequence.Sequence;
026import icy.type.collection.array.Array1DUtil;
027
028/**
029 * This tutorial details how to create a simple image processing section without specific
030 * knowledge of the original data type. The image will be convert to a 1D array of double,
031 * then the process can be performed and stored back to the original image.
032 * 
033 * @author Fabrice de Chaumont
034 * @author Stephane Dallongeville
035 */
036public class TransparentlyProcessAnyImageDataType extends PluginActionable
037{
038    // This method is called as the plugin is launched
039    @Override
040    public void run()
041    {
042        // Display what this tutorial perform.
043        new AnnounceFrame(
044                "This tutorial fades to black the band 0 of the image, on the upper part of the image, regardless of the image dataType.");
045
046        // Get the current sequence having focus.
047        Sequence sequence = getActiveSequence();
048
049        // Check if sequence exists.
050        if (sequence == null)
051        {
052            MessageDialog.showDialog("Please open a sequence to use this plugin.", MessageDialog.WARNING_MESSAGE);
053            return;
054        }
055
056        // Get the image at t=0 and z=0
057        IcyBufferedImage image = sequence.getImage(0, 0);
058
059        // Check if the image exists
060        if (image == null)
061        {
062            MessageDialog.showDialog("No image is present at t=0 and z=0.", MessageDialog.WARNING_MESSAGE);
063            return;
064        }
065
066        // Get the data of the image for channel 0 as a linear buffer, regardless of the type.
067        Object imageData = image.getDataXY(0);
068
069        // Get a copy of the data in double.
070        double[] dataBuffer = Array1DUtil.arrayToDoubleArray(imageData, image.isSignedDataType());
071
072        // Fade the first half of the pixels.
073        for (int i = 0; i < dataBuffer.length / 2; i++)
074            dataBuffer[i] /= 2;
075
076        // Put the data back to the original image.
077        // Convert the double data automatically to the data type of the image.
078        Array1DUtil.doubleArrayToArray(dataBuffer, imageData);
079
080        // just to let the image know the data has changed (internal updates and view refresh) and also to update cache for volatile image
081        image.setDataXY(0, imageData);
082    }
083}