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