001: /*
002: * @(#) $Header: /cvs/jai-operators/src/main/ca/forklabs/media/jai/opimage/PeriodicShift3DRCIF.java,v 1.2 2007/07/05 18:24:45 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.ArrayList;
028: import java.util.Collections;
029: import java.util.List;
030: import javax.media.jai.CollectionImage;
031: import javax.media.jai.CollectionImageFactory;
032: import javax.media.jai.CollectionOp;
033: import javax.media.jai.JAI;
034: import javax.media.jai.RenderableCollectionImageFactory;
035: import ca.forklabs.media.jai.ParameterBlockUtil;
036: import ca.forklabs.media.jai.SimpleCollectionImage;
037: import ca.forklabs.media.jai.operator.PeriodicShift3DDescriptor;
038:
039: /**
040: * Class {@code PeriodicShift3DRCIF} is a {@link CollectionImageFactory} and a
041: * {@link RenderableCollectionImageFactory} supporting the
042: * <em>periodicshift3d</em> operation.
043: *
044: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.opimage.PeriodicShift3DRCIF">Daniel Léonard</a>
045: * @version $Revision: 1.2 $
046: */
047: public class PeriodicShift3DRCIF implements CollectionImageFactory,
048: RenderableCollectionImageFactory {
049:
050: //---------------------------
051: // Constructor
052: //---------------------------
053:
054: /**
055: * Constructor.
056: */
057: public PeriodicShift3DRCIF() {
058: // nothing
059: }
060:
061: //---------------------------
062: // Instance methods
063: //---------------------------
064:
065: /**
066: * Gets the diplacement in <em>x</em> within the parameter block.
067: * @param pb the parameter block.
068: * @return the displacement.
069: */
070: protected Integer getShiftX(ParameterBlock pb) {
071: int index = PeriodicShift3DDescriptor.SHIFT_X_PARAMETER_INDEX;
072: Integer shift_x = (Integer) pb.getObjectParameter(index);
073: return shift_x;
074: }
075:
076: /**
077: * Gets the diplacement in <em>y</em> within the parameter block.
078: * @param pb the parameter block.
079: * @return the displacement.
080: */
081: protected Integer getShiftY(ParameterBlock pb) {
082: int index = PeriodicShift3DDescriptor.SHIFT_Y_PARAMETER_INDEX;
083: Integer shift_y = (Integer) pb.getObjectParameter(index);
084: return shift_y;
085: }
086:
087: /**
088: * Gets the diplacement in <em>z</em> within the parameter block.
089: * @param pb the parameter block.
090: * @return the displacement.
091: */
092: @SuppressWarnings("boxing")
093: protected Integer getShiftZ(ParameterBlock pb) {
094: int index = PeriodicShift3DDescriptor.SHIFT_Z_PARAMETER_INDEX;
095: Integer shift_z = (Integer) pb.getObjectParameter(index);
096: return shift_z;
097: }
098:
099: /**
100: * Shifts the cubic image.
101: * @param pb the parameter block.
102: * @param hints the rendering hints.
103: * @return the shifted cubic image.
104: */
105: @SuppressWarnings({"unchecked","nls","boxing"})
106: protected CollectionImage shift(ParameterBlock pb,
107: RenderingHints hints) {
108: CollectionImage sources = (CollectionImage) pb.getSource(0);
109:
110: Integer shift_x = this .getShiftX(pb);
111: Integer shift_y = this .getShiftY(pb);
112: Integer shift_z = this .getShiftZ(pb);
113:
114: int len = sources.size();
115: List sinks = new ArrayList(len);
116:
117: for (Object source : sources) {
118: Object sink;
119: if (source instanceof RenderedImage) {
120: RenderedImage image = (RenderedImage) source;
121: ParameterBlock parameters = ParameterBlockUtil
122: .createPeriodicShiftParameterBlock(image,
123: shift_x, shift_y);
124: sink = JAI.create("periodicshift", parameters, hints);
125: } else if (source instanceof RenderableImage) {
126: RenderableImage image = (RenderableImage) source;
127: ParameterBlock parameters = ParameterBlockUtil
128: .createPeriodicShiftParameterBlock(image,
129: shift_x, shift_y);
130: sink = JAI.createRenderable("periodicshift",
131: parameters, hints);
132: } else {
133: Class<?> clazz = source.getClass();
134: String messsage = this
135: .getNeitherRenderedNorRenderableErrorMessage(clazz);
136: throw new IllegalStateException(messsage);
137: }
138: sinks.add(sink);
139: }
140:
141: Collections.rotate(sinks, shift_z);
142:
143: CollectionImage collection_image = new SimpleCollectionImage(
144: sinks);
145: return collection_image;
146: }
147:
148: /**
149: * Gets the error message saying that a source image is neither a rendered
150: * nor a renderable image.
151: * @param clazz the class of the offending image.
152: * @return the error message.
153: */
154: protected String getNeitherRenderedNorRenderableErrorMessage(
155: Class<?> clazz) {
156: String key = Resources.NEITHER_RENDERED_NOR_RENDERABLE;
157: String message = Resources.getLocalizedString(key, clazz);
158: return message;
159: }
160:
161: //---------------------------
162: // Implemented methods from javax.media.jai.CollectionImageFactory
163: //---------------------------
164:
165: /**
166: * Calls {@link #shift(ParameterBlock, RenderingHints)}.
167: * @param pb the parameter block.
168: * @param hints the rendering hints.
169: * @return the shifted cubic image.
170: */
171: @SuppressWarnings("unchecked")
172: public CollectionImage create(ParameterBlock pb,
173: RenderingHints hints) {
174: CollectionImage collection_image = this .shift(pb, hints);
175: return collection_image;
176: }
177:
178: /**
179: * It is impratical to perform the update.
180: * @param old_pb ignored
181: * @param old_hints ignored
182: * @param new_pb ignored
183: * @param new_hints ignored
184: * @param old_image ignored
185: * @param op ignored
186: * @return always {@code null}.
187: */
188: public CollectionImage update(ParameterBlock old_pb,
189: RenderingHints old_hints, ParameterBlock new_pb,
190: RenderingHints new_hints, CollectionImage old_image,
191: CollectionOp op) {
192: // it is impracticable to perform the update
193: CollectionImage new_image = null;
194: return new_image;
195: }
196:
197: //---------------------------
198: // Implemented methods from javax.media.jai.RenderableCollectionImageFactory
199: //---------------------------
200:
201: /**
202: * Calls {@link #shift(ParameterBlock, RenderingHints)}.
203: * @param pb the parameter block.
204: * @return the shifted cubic image.
205: */
206: @Override
207: public CollectionImage create(ParameterBlock pb) {
208: RenderingHints hints = null;
209: CollectionImage sinks = this .shift(pb, hints);
210: return sinks;
211: }
212:
213: }
214:
215: /*
216: * $Log: PeriodicShift3DRCIF.java,v $
217: * Revision 1.2 2007/07/05 18:24:45 forklabs
218: * Now uses CollectionImage instead of lists.
219: *
220: * Revision 1.1 2007/06/05 20:29:59 forklabs
221: * Operator periodicshift3d.
222: *
223: */
|