001: /*
002: * @(#) $Header: /cvs/jai-operators/src/main/ca/forklabs/media/jai/opimage/ApplyToCollectionRCIF.java,v 1.4 2007/07/17 16:39:33 forklabs Exp $
003: *
004: * Copyright (C) 2007 Forklabs Daniel Léonard
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program 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 this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package ca.forklabs.media.jai.opimage;
022:
023: import java.awt.RenderingHints;
024: import java.awt.image.RenderedImage;
025: import java.awt.image.renderable.ParameterBlock;
026: import java.awt.image.renderable.RenderableImage;
027: import javax.media.jai.CollectionImage;
028: import javax.media.jai.CollectionImageFactory;
029: import javax.media.jai.CollectionOp;
030: import javax.media.jai.JAI;
031: import javax.media.jai.RenderableCollectionImageFactory;
032: import ca.forklabs.media.jai.SimpleCollectionImage;
033: import ca.forklabs.media.jai.operator.ApplyToCollectionDescriptor;
034:
035: /**
036: * Class {@code ApplyToToCollectionRCIF} is a {@link CollectionImageFactory} and
037: * {@link RenderableCollectionImageFactory} supporting the
038: * <em>applytocollection</em> operation.
039: *
040: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.opimage.ApplyToCollectionRCIF">Daniel Léonard</a>
041: * @version $Revision: 1.4 $
042: */
043: public class ApplyToCollectionRCIF implements CollectionImageFactory,
044: RenderableCollectionImageFactory {
045:
046: //---------------------------
047: // Constructor
048: //---------------------------
049:
050: /**
051: * Constructor.
052: */
053: public ApplyToCollectionRCIF() {
054: // nothing
055: }
056:
057: //---------------------------
058: // Instance methods
059: //---------------------------
060:
061: /**
062: * Gets the name of the operation parameter within the parameter block.
063: * @param pb the parameter block.
064: * @return the name of the operation.
065: */
066: protected String getOperation(ParameterBlock pb) {
067: int index = ApplyToCollectionDescriptor.OPERATION_PARAMETER_INDEX;
068: String operation = (String) pb.getObjectParameter(index);
069: return operation;
070: }
071:
072: /**
073: * Gets the parameter block parameter within the parameter block.
074: * @param pb the parameter block.
075: * @return the parameter block parameter.
076: */
077: protected ParameterBlock getParameters(ParameterBlock pb) {
078: int index = ApplyToCollectionDescriptor.PARAMETERS_PARAMETER_INDEX;
079: ParameterBlock parameters = (ParameterBlock) pb
080: .getObjectParameter(index);
081: return parameters;
082: }
083:
084: /**
085: * Creates a {@code CollectionImage} that represents the result of applying
086: * the requested operation once to each image in the collection.
087: * @param pb the parameter block.
088: * @param hints the rendering hints.
089: * @return the collection image containing the results.
090: */
091: @SuppressWarnings("unchecked")
092: protected CollectionImage apply(ParameterBlock pb,
093: RenderingHints hints) {
094: CollectionImage sources = (CollectionImage) pb.getSource(0);
095:
096: CollectionImage sinks = new SimpleCollectionImage();
097:
098: String operation = this .getOperation(pb);
099: ParameterBlock parameters = this .getParameters(pb);
100:
101: for (Object source : sources) {
102: parameters.removeSources();
103: parameters.addSource(source);
104:
105: Object sink;
106: if (source instanceof RenderedImage) {
107: sink = JAI.create(operation, parameters, hints);
108: } else if (source instanceof RenderableImage) {
109: sink = JAI.createRenderable(operation, parameters,
110: hints);
111: }
112: // TODO : remove this, only support rendered and renderable image
113: else if (source instanceof CollectionImage) {
114: sink = JAI.createCollection(operation, parameters,
115: hints);
116: } else {
117: Class<?> clazz = source.getClass();
118: String message = this
119: .getNeitherRenderedNorRenderableErrorMessage(clazz);
120: throw new IllegalStateException(message);
121: }
122: sinks.add(sink);
123: }
124:
125: return sinks;
126: }
127:
128: /**
129: * Gets the error message saying that a source image is neither a rendered
130: * nor a renderable image.
131: * @param clazz the class of the offending image.
132: * @return the error message.
133: */
134: protected String getNeitherRenderedNorRenderableErrorMessage(
135: Class<?> clazz) {
136: String key = Resources.NEITHER_RENDERED_NOR_RENDERABLE;
137: String message = Resources.getLocalizedString(key, clazz);
138: return message;
139: }
140:
141: //---------------------------
142: // Implemented methods from javax.media.jai.CollectionImageFactory
143: //---------------------------
144:
145: /**
146: * Calls {@link #apply(ParameterBlock, RenderingHints)}.
147: * @param pb the parameter block.
148: * @param hints the rendering hints.
149: * @return the collection image containing the results.
150: */
151: public CollectionImage create(ParameterBlock pb,
152: RenderingHints hints) {
153: CollectionImage image = this .apply(pb, hints);
154: return image;
155: }
156:
157: /**
158: * It is impratical to perform the update.
159: * @param old_pb ignored
160: * @param old_hints ignored
161: * @param new_pb ignored
162: * @param new_hints ignored
163: * @param old_image ignored
164: * @param op ignored
165: * @return always {@code null}.
166: */
167: public CollectionImage update(ParameterBlock old_pb,
168: RenderingHints old_hints, ParameterBlock new_pb,
169: RenderingHints new_hints, CollectionImage old_image,
170: CollectionOp op) {
171: // it is impracticable to perform the update
172: CollectionImage new_image = null;
173: return new_image;
174: }
175:
176: //---------------------------
177: // Implemented methods from javax.media.jai.RenderableCollectionImageFactory
178: //---------------------------
179:
180: /**
181: * Calls {@link #apply(ParameterBlock, RenderingHints)}.
182: * @param pb the parameter block.
183: * @return the collection image containing the results.
184: */
185: @Override
186: public CollectionImage create(ParameterBlock pb) {
187: RenderingHints hints = null;
188: CollectionImage image = this .apply(pb, hints);
189: return image;
190: }
191:
192: }
193:
194: /*
195: * $Log: ApplyToCollectionRCIF.java,v $
196: * Revision 1.4 2007/07/17 16:39:33 forklabs
197: * clean up.
198: *
199: * Revision 1.3 2007/07/05 18:23:21 forklabs
200: * Now uses CollectionImage instead of lists.
201: *
202: * Revision 1.2 2007/06/13 18:57:21 forklabs
203: * Changed parent to use CollectionDescriptor.
204: *
205: * Revision 1.1 2007/06/05 21:26:59 forklabs
206: * applytocollection now supports mode renderablecollection.
207: *
208: * Revision 1.1 2007/05/25 21:20:08 forklabs
209: * Operator "applytocollection"
210: *
211: */
|