001: /*
002: * @(#) $Header: /cvs/jai-operators/src/main/ca/forklabs/media/jai/opimage/PipelineUtil.java,v 1.3 2007/07/05 18:26:59 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 java.util.Collection;
028: import javax.media.jai.CollectionImage;
029: import javax.media.jai.JAI;
030: import ca.forklabs.media.jai.operator.PipelineDescriptor;
031:
032: class PipelineUtil {
033:
034: //---------------------------
035: // Class variables
036: //---------------------------
037:
038: /**
039: * Specializes the call to the correct JAI factory method.
040: * @param <T> the type of image the specialization is for.
041: */
042: public static abstract class Specialization<T> {
043:
044: /**
045: * Extracts the source object from the parameter block.
046: * @param pb the parameter block.
047: * @return the source object.
048: */
049: public abstract T getSource(ParameterBlock pb);
050:
051: /**
052: * Calls the correct JAI factory method.
053: * @param operator the operator.
054: * @param pb the parameter block.
055: * @param hints rendering hints.
056: * @return the image created.
057: */
058: public abstract T create(String operator, ParameterBlock pb,
059: RenderingHints hints);
060:
061: }
062:
063: /** Specialization for the rendered pipeline. */
064: public static final Specialization<RenderedImage> RENDERED = new Specialization<RenderedImage>() {
065: @Override
066: @SuppressWarnings("unchecked")
067: public RenderedImage getSource(ParameterBlock pb) {
068: RenderedImage source = null;
069:
070: CollectionImage collection = (CollectionImage) pb
071: .getSource(0);
072: if (0 != collection.size()) {
073: source = (RenderedImage) collection.get(0);
074: }
075:
076: return source;
077: }
078:
079: @Override
080: public RenderedImage create(String operator, ParameterBlock pb,
081: RenderingHints hints) {
082: RenderedImage image = JAI.create(operator, pb, hints);
083: return image;
084: }
085: };
086:
087: /** Specialization for the renderable pipeline. */
088: public static final Specialization<RenderableImage> RENDERABLE = new Specialization<RenderableImage>() {
089: @Override
090: @SuppressWarnings("unchecked")
091: public RenderableImage getSource(ParameterBlock pb) {
092: RenderableImage source = null;
093:
094: CollectionImage collection = (CollectionImage) pb
095: .getSource(0);
096: if (0 != collection.size()) {
097: source = (RenderableImage) collection.get(0);
098: }
099:
100: return source;
101: }
102:
103: @Override
104: public RenderableImage create(String operator,
105: ParameterBlock pb, RenderingHints hints) {
106: RenderableImage image = JAI.createRenderable(operator, pb,
107: hints);
108: return image;
109: }
110: };
111:
112: /** Specialization for the collection pipeline. */
113: public static final Specialization<Collection<?>> COLLECTION = new Specialization<Collection<?>>() {
114: @Override
115: public Collection<?> getSource(ParameterBlock pb) {
116: Collection<?> source = (Collection<?>) pb.getSource(0);
117: return source;
118: }
119:
120: @Override
121: public Collection<?> create(String operator, ParameterBlock pb,
122: RenderingHints hints) {
123: Collection<?> image = JAI.createCollection(operator, pb,
124: hints);
125: return image;
126: }
127: };
128:
129: /** Specialization for the renderable collection pipeline. */
130: public static final Specialization<Collection<?>> RENDERABLE_COLLECTION = new Specialization<Collection<?>>() {
131: @Override
132: public Collection<?> getSource(ParameterBlock pb) {
133: Collection<?> source = (Collection<?>) pb.getSource(0);
134: return source;
135: }
136:
137: @Override
138: public Collection<?> create(String operator, ParameterBlock pb,
139: RenderingHints hints) {
140: Collection<?> image = JAI.createRenderableCollection(
141: operator, pb, hints);
142: return image;
143: }
144: };
145:
146: //---------------------------
147: // Constructor
148: //---------------------------
149:
150: /**
151: * Let no one instanciate.
152: */
153: private PipelineUtil() {
154: // nothing
155: }
156:
157: //---------------------------
158: // Class methods
159: //---------------------------
160:
161: /**
162: * Gets the name of the operations.
163: * @param pb the parameter block.
164: * @return the names.
165: */
166: public static String[] getOperators(ParameterBlock pb) {
167: String[] operators = (String[]) pb
168: .getObjectParameter(PipelineDescriptor.OPERATIONS_PARAMETER_INDEX);
169: return operators;
170: }
171:
172: /**
173: * Gets the parameter of the operations.
174: * @param pb the parameter block.
175: * @return the parameters.
176: */
177: public static ParameterBlock[] getParameters(ParameterBlock pb) {
178: ParameterBlock[] parameters = (ParameterBlock[]) pb
179: .getObjectParameter(PipelineDescriptor.PARAMETERS_PARAMETER_INDEX);
180: return parameters;
181: }
182:
183: /**
184: * Does the pipeline.
185: * @param pb the parameter block.
186: * @param hints rendering hints.
187: * @param specialization specialization depending on the mode.
188: *
189: * @param <T> the type of image.
190: *
191: * @return the result of the pipeline.
192: */
193: @SuppressWarnings("unchecked")
194: public static <T> T doPipeline(ParameterBlock pb,
195: RenderingHints hints, Specialization<T> specialization) {
196: T source = specialization.getSource(pb);
197: String[] operators = PipelineUtil.getOperators(pb);
198: ParameterBlock[] parameters = PipelineUtil.getParameters(pb);
199:
200: // for each operation, the source is
201: // the sink of the last operation
202: T sink = source;
203: for (int i = 0, len = operators.length; i < len; i++) {
204: String operator = operators[i];
205: ParameterBlock parameter = parameters[i];
206:
207: if (null != sink) {
208: parameter.addSource(sink);
209: }
210:
211: sink = specialization.create(operator, parameter, hints);
212: }
213:
214: return sink;
215: }
216:
217: }
218:
219: /*
220: * $Log: PipelineUtil.java,v $
221: * Revision 1.3 2007/07/05 18:26:59 forklabs
222: * Now uses CollectionImage instead of lists.
223: *
224: * Revision 1.2 2007/06/13 18:57:21 forklabs
225: * Changed parent to use CollectionDescriptor.
226: *
227: * Revision 1.1 2007/06/07 23:39:19 forklabs
228: * Operator pipeline is now on all four modes.
229: *
230: */
|