001/* An actor that reads in a java.awt.Image and rotates it a certain number of
002 degrees
003
004 @Copyright (c) 2001-2014 The Regents of the University of California.
005 All rights reserved.
006
007 Permission is hereby granted, without written agreement and without
008 license or royalty fees, to use, copy, modify, and distribute this
009 software and its documentation for any purpose, provided that the
010 above copyright notice and the following two paragraphs appear in all
011 copies of this software.
012
013 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017 SUCH DAMAGE.
018
019 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 ENHANCEMENTS, OR MODIFICATIONS.
025
026 PT_COPYRIGHT_VERSION 2
027 COPYRIGHTENDKEY
028 */
029package ptolemy.actor.lib.image;
030
031import java.awt.Image;
032
033import ptolemy.actor.lib.Transformer;
034import ptolemy.data.AWTImageToken;
035import ptolemy.data.ImageToken;
036import ptolemy.data.IntToken;
037import ptolemy.data.expr.Parameter;
038import ptolemy.data.type.BaseType;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042
043///////////////////////////////////////////////////////////////////
044//// ImageRotate
045
046/**
047 This actor reads an ObjectToken that is a java.awt.Image from the input,
048 rotates it a certain number of degrees and writes the resulting
049 image to the output port as an ObjectToken that is a java.awt.Image.
050
051 @author  Christopher Hylands
052 @version $Id$
053 @since Ptolemy II 3.0
054 @Pt.ProposedRating Red (cxh)
055 @Pt.AcceptedRating Red (cxh)
056 */
057public class ImageRotate extends Transformer {
058    /** Construct an actor with the given container and name.
059     *  @param container The container.
060     *  @param name The name of this actor.
061     *  @exception IllegalActionException If the actor cannot be contained
062     *   by the proposed container.
063     *  @exception NameDuplicationException If the container already has an
064     *   actor with this name.
065     */
066    public ImageRotate(CompositeEntity container, String name)
067            throws IllegalActionException, NameDuplicationException {
068        super(container, name);
069        input.setTypeEquals(BaseType.OBJECT);
070        output.setTypeEquals(BaseType.OBJECT);
071        rotationInDegrees = new Parameter(this, "rotationInDegrees",
072                new IntToken(90));
073        rotationInDegrees.setTypeEquals(BaseType.INT);
074    }
075
076    ///////////////////////////////////////////////////////////////////
077    ////                     ports and parameters                  ////
078
079    /** The amount of of rotation in degrees.
080     *  This parameter contains an IntegerToken, initially with value 90.
081     */
082    public Parameter rotationInDegrees;
083
084    ///////////////////////////////////////////////////////////////////
085    ////                         public methods                    ////
086
087    /** Read one java.awt.Image from each channel and rotate each Image
088     *  the number of degrees indicated by the rotationInDegrees parameter.
089     *
090     *  @exception IllegalActionException If there is no director.
091     */
092    @Override
093    public void fire() throws IllegalActionException {
094        super.fire();
095        int width = input.getWidth();
096        int rotation = ((IntToken) rotationInDegrees.getToken()).intValue();
097
098        for (int i = 0; i < width; i++) {
099            if (input.hasToken(i)) {
100                ImageToken imageToken = (ImageToken) input.get(i);
101                Image image = imageToken.asAWTImage();
102                Image rotatedImage = Transform.rotate(image, rotation);
103                output.broadcast(new AWTImageToken(rotatedImage));
104            }
105        }
106    }
107}