001/* Combine subimages into a larger image. 002 @Copyright (c) 1998-2014 The Regents of the University of California. 003 All rights reserved. 004 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the 008 above copyright notice and the following two paragraphs appear in all 009 copies of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION 2 025 COPYRIGHTENDKEY 026 */ 027package ptolemy.domains.sdf.lib.vq; 028 029import ptolemy.actor.lib.Transformer; 030import ptolemy.data.IntMatrixToken; 031import ptolemy.data.IntToken; 032import ptolemy.data.Token; 033import ptolemy.data.expr.Parameter; 034import ptolemy.data.type.BaseType; 035import ptolemy.kernel.CompositeEntity; 036import ptolemy.kernel.util.IllegalActionException; 037import ptolemy.kernel.util.NameDuplicationException; 038 039/////////////////////////////////////////////////////////////////// 040//// ImageUnpartition 041 042/** 043 Combine subimages into a larger image. Each input subimage 044 should have dimensions partitionColumns by partitionRows, and each output image 045 will have dimensions imageColumns by imageRows. The input images 046 will be placed in row-scanned order from top to bottom into the output image. 047 048 @author Steve Neuendorffer 049 @version $Id$ 050 @since Ptolemy II 0.2 051 @Pt.ProposedRating Green (neuendor) 052 @Pt.AcceptedRating Yellow (neuendor) 053 */ 054public class ImageUnpartition extends Transformer { 055 /** Construct an actor in the specified container with the specified 056 * name. 057 * @param container The container. 058 * @param name The name of this adder within the container. 059 * @exception IllegalActionException If the actor cannot be contained 060 * by the proposed container. 061 * @exception NameDuplicationException If the name coincides with 062 * an actor already in the container. 063 */ 064 public ImageUnpartition(CompositeEntity container, String name) 065 throws IllegalActionException, NameDuplicationException { 066 super(container, name); 067 068 imageColumns = new Parameter(this, "imageColumns", new IntToken("176")); 069 imageColumns.setTypeEquals(BaseType.INT); 070 imageRows = new Parameter(this, "imageRows", new IntToken("144")); 071 imageRows.setTypeEquals(BaseType.INT); 072 partitionColumns = new Parameter(this, "partitionColumns", 073 new IntToken("4")); 074 partitionColumns.setTypeEquals(BaseType.INT); 075 partitionRows = new Parameter(this, "partitionRows", new IntToken("2")); 076 partitionRows.setTypeEquals(BaseType.INT); 077 078 input_tokenConsumptionRate = new Parameter(input, 079 "tokenConsumptionRate"); 080 input_tokenConsumptionRate.setTypeEquals(BaseType.INT); 081 input_tokenConsumptionRate.setExpression( 082 "imageColumns * imageRows / partitionColumns / partitionRows"); 083 084 input.setTypeEquals(BaseType.INT_MATRIX); 085 output.setTypeEquals(BaseType.INT_MATRIX); 086 } 087 088 /////////////////////////////////////////////////////////////////// 089 //// ports and parameters //// 090 091 /** The width of the input matrices. */ 092 public Parameter imageColumns; 093 094 /** The height of the input matrices. */ 095 public Parameter imageRows; 096 097 /** The width of the input partitions. */ 098 public Parameter partitionColumns; 099 100 /** The height of the input partitions. */ 101 public Parameter partitionRows; 102 103 /** The input rate. */ 104 public Parameter input_tokenConsumptionRate; 105 106 /////////////////////////////////////////////////////////////////// 107 //// public methods //// 108 109 /** 110 * Initialize this actor. 111 * @exception IllegalActionException If a parameter does not contain a 112 * legal value. 113 */ 114 @Override 115 public void initialize() throws IllegalActionException { 116 super.initialize(); 117 118 _imageColumns = ((IntToken) imageColumns.getToken()).intValue(); 119 _imageRows = ((IntToken) imageRows.getToken()).intValue(); 120 _partitionColumns = ((IntToken) partitionColumns.getToken()).intValue(); 121 _partitionRows = ((IntToken) partitionRows.getToken()).intValue(); 122 123 if (_imageColumns % _partitionColumns != 0) { 124 throw new IllegalActionException(imageColumns, partitionColumns, 125 "Partition size must evenly divide image size"); 126 } 127 128 if (_imageRows % _partitionRows != 0) { 129 throw new IllegalActionException(imageRows, partitionRows, 130 "Partition size must evenly divide image size"); 131 } 132 133 _image = new int[_imageRows][_imageColumns]; 134 _partitionCount = _imageColumns * _imageRows / _partitionColumns 135 / _partitionRows; 136 } 137 138 /** 139 * Fire this actor. 140 * Consume IntMatrixTokens on the input port corresponding to the 141 * partitions of an image. Reassemble the image and produce a 142 * single IntMatrixToken on the output port. 143 * 144 * @exception IllegalActionException If the ports are not connected. 145 */ 146 @Override 147 public void fire() throws IllegalActionException { 148 super.fire(); 149 int i; 150 int j; 151 int y; 152 int partitionNumber; 153 154 Token[] _partitions = input.get(0, _partitionCount); 155 156 for (j = 0, partitionNumber = 0; j < _imageRows; j += _partitionRows) { 157 for (i = 0; i < _imageColumns; i += _partitionColumns, partitionNumber++) { 158 IntMatrixToken partition = (IntMatrixToken) _partitions[partitionNumber]; 159 160 if (partition.getRowCount() != _partitionRows 161 || partition.getColumnCount() != _partitionColumns) { 162 throw new IllegalActionException( 163 "input data must be partitionRows " 164 + "by partitionColumns"); 165 } 166 167 int[][] part = partition.intMatrix(); 168 169 for (y = 0; y < _partitionRows; y++) { 170 System.arraycopy(part[y], 0, _image[j + y], i, 171 _partitionColumns); 172 } 173 } 174 } 175 176 output.send(0, new IntMatrixToken(_image)); 177 } 178 179 /////////////////////////////////////////////////////////////////// 180 //// private variables //// 181 private int[][] _image; 182 183 private int _imageColumns; 184 185 private int _imageRows; 186 187 private int _partitionColumns; 188 189 private int _partitionRows; 190 191 // This is the input port consumption rate. 192 private int _partitionCount; 193}