001/**
002 * 
003 */
004package plugins.kernel.roi.descriptor.intensity;
005
006import icy.roi.ROI;
007import icy.roi.ROIDescriptor;
008import icy.sequence.Sequence;
009import icy.sequence.SequenceEvent;
010import icy.sequence.SequenceEvent.SequenceEventSourceType;
011
012/**
013 * Maximum intensity ROI descriptor class (see {@link ROIDescriptor})
014 * 
015 * @author Stephane
016 */
017public class ROIMaxIntensityDescriptor extends ROIDescriptor
018{
019    public static final String ID = "Max intensity";
020
021    public ROIMaxIntensityDescriptor()
022    {
023        super(ID, "Max Intensity", Double.class);
024    }
025
026    @Override
027    public String getDescription()
028    {
029        return "Maximum intensity";
030    }
031
032    @Override
033    public boolean separateChannel()
034    {
035        return true;
036    }
037
038    @Override
039    public boolean needRecompute(SequenceEvent change)
040    {
041        return (change.getSourceType() == SequenceEventSourceType.SEQUENCE_DATA);
042    }
043
044    @Override
045    public Object compute(ROI roi, Sequence sequence) throws UnsupportedOperationException
046    {
047        return Double.valueOf(computeMaxIntensity(roi, sequence));
048    }
049
050    /**
051     * Computes and returns the maximum intensity for the specified ROI on given sequence.<br>
052     * It may returns <code>Double.Nan</code> if the operation is not supported for that ROI.
053     * 
054     * @param roi
055     *        the ROI on which we want to compute the maximum intensity
056     * @param sequence
057     *        the sequence used to compute the pixel intensity
058     * @throws UnsupportedOperationException
059     *         if the operation is not supported for this ROI
060     */
061    public static double computeMaxIntensity(ROI roi, Sequence sequence) throws UnsupportedOperationException
062    {
063        try
064        {
065            return ROIIntensityDescriptorsPlugin.computeIntensityDescriptors(roi, sequence, false).max;
066        }
067        catch (Exception e)
068        {
069            throw new UnsupportedOperationException(ROIMaxIntensityDescriptor.class.getSimpleName()
070                    + ": cannot compute descriptors for '" + roi.getName() + "'", e);
071        }
072    }
073}