001: /*
002: * @(#) $Header: /cvs/jai-operators/src/main/ca/forklabs/media/jai/operator/PeriodicShift3DDescriptor.java,v 1.4 2007/09/07 18:08:15 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.operator;
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.JAI;
029: import javax.media.jai.OperationDescriptor;
030: import javax.media.jai.ParameterBlockJAI;
031: import javax.media.jai.ParameterListDescriptor;
032: import javax.media.jai.registry.CollectionRegistryMode;
033: import javax.media.jai.registry.RenderableCollectionRegistryMode;
034: import ca.forklabs.media.jai.CollectionDescriptor;
035:
036: /**
037: * Class {@code PeriodicShift3DDescriptor} is an {@link OperationDescriptor}
038: * describing the <em>periodicshift</em> operation. This operation is an
039: * extension of JAI operator <em>periodicshift</em>.
040: * <p>
041: * The <em>periodicshift</em> operation takes three parameters: the shift in
042: * <em>x</em> (the width of the image), the shift in <em>y</em> (the height of
043: * the image) and the shift in <em>z</em> (the depth of the image).
044: * <p>
045: * It is assumed that all the images in the collection are of the same size,
046: * otherwise surprising results may occurs especially with default values.
047: *
048: * <table border=1>
049: * <caption>Resource List</caption>
050: * <tr><th>Name</th> <th>Value</th></tr>
051: * <tr><td>GlobalName</td> <td>PeriodicShift3D</td></tr>
052: * <tr><td>LocalName</td> <td>PeriodicShift3D</td></tr>
053: * <tr><td>Vendor</td> <td>ca.forklabs.media.jai.opimage</td></tr>
054: * <tr><td>Description</td> <td>Periodic shift on a 3D image</td></tr>
055: * <tr><td>DocURL</td> <td>n/a</td></tr>
056: * <tr><td>Version</td> <td>$Version$</td></tr>
057: * <tr><td>Arg0Desct</td> <td>The displacement in the X direction (optional)</td></tr>
058: * <tr><td>Arg1Desct</td> <td>The displacement in the Y direction (optional)</td></tr>
059: * <tr><td>Arg2Desct</td> <td>The displacement in the Z direction (optional)</td></tr>
060: * </table>
061: *
062: * <table border=1>
063: * <caption>Parameter List</caption>
064: * <tr><th>Name</th> <th>Class Type</th> <th>Default Value</th></tr>
065: * <tr><td>shiftX</td> <td>Integer</td> <td>sourceWidth/2</td>
066: * <tr><td>shiftY</td> <td>Integer</td> <td>sourceHeight/2</td>
067: * <tr><td>shiftZ</td> <td>Integer</td> <td>sourceDepth/2</td>
068: * </table>
069: *
070: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.PeriodicShift3DDescriptor">Daniel Léonard</a>
071: * @version $Revision: 1.4 $
072: */
073: public class PeriodicShift3DDescriptor extends CollectionDescriptor {
074:
075: //---------------------------
076: // Class variables
077: //---------------------------
078:
079: /** <em>serialVersionUID</em>. */
080: private static final long serialVersionUID = 846175619312050342L;
081:
082: /** The name of this operator. */
083: @SuppressWarnings("nls")
084: public static final String NAME = "PeriodicShift";
085:
086: /** The name of the <em>shiftX</em> parameter. */
087: @SuppressWarnings("nls")
088: public static final String SHIFT_X_PARAMETER_NAME = "shiftX";
089: /** The name of the <em>shiftY</em> parameter. */
090: @SuppressWarnings("nls")
091: public static final String SHIFT_Y_PARAMETER_NAME = "shiftY";
092: /** The name of the <em>shiftZ</em> parameter. */
093: @SuppressWarnings("nls")
094: public static final String SHIFT_Z_PARAMETER_NAME = "shiftZ";
095:
096: /** The index in the parameter block of the <em>shifX</em> parameter. */
097: public static final int SHIFT_X_PARAMETER_INDEX = 0;
098: /** The index in the parameter block of the <em>shifY</em> parameter. */
099: public static final int SHIFT_Y_PARAMETER_INDEX = 1;
100: /** The index in the parameter block of the <em>shifZ</em> parameter. */
101: public static final int SHIFT_Z_PARAMETER_INDEX = 2;
102:
103: /**
104: * The resource strings that provide the general documentation and specify
105: * the parameter list for this operation.
106: */
107: @SuppressWarnings("nls")
108: private static final String[][] RESOURCES = {
109: { "GlobalName", PeriodicShift3DDescriptor.NAME, },
110: { "LocalName", PeriodicShift3DDescriptor.NAME, },
111: { "Vendor", "ca.forklabs.media.jai.opimage", },
112: { "Description",
113: PeriodicShift3DDescriptor.getDescription(), },
114: { "DocURL", "n/a", },
115: { "Version", "$Version$", },
116: { "arg0Desc",
117: PeriodicShift3DDescriptor.getArg0Description(), },
118: { "arg1Desc",
119: PeriodicShift3DDescriptor.getArg1Description(), },
120: { "arg2Desc",
121: PeriodicShift3DDescriptor.getArg2Description(), }, };
122:
123: /** The supported modes. */
124: private static final String[] SUPPORTED_MODES = {
125: CollectionRegistryMode.MODE_NAME,
126: RenderableCollectionRegistryMode.MODE_NAME, };
127:
128: /** The name of the source, use default. */
129: private static final String[] SOURCE_NAMES = null;
130:
131: /** The type of source for each mode. */
132: private static final Class<?>[][] SOURCE_CLASSES = new Class<?>[][] {
133: { Collection.class, }, { Collection.class, }, };
134:
135: /** The parameter list descriptor for all modes. */
136: private static final ParameterListDescriptor PARAMETER_LIST_DESCRIPTOR = new CollectionDescriptor.EmptyParameterListDescriptor() {
137:
138: @Override
139: public int getNumParameters() {
140: int num_parameters = 3;
141: return num_parameters;
142: }
143:
144: @Override
145: public Class<?>[] getParamClasses() {
146: Class<?>[] clazzes = new Class<?>[] { Integer.class,
147: Integer.class, Integer.class, };
148: return clazzes;
149: }
150:
151: @Override
152: public Object getParamDefaultValue(String name) {
153: // the default values are calculated
154: Object value = null;
155: return value;
156: }
157:
158: @Override
159: @SuppressWarnings("nls")
160: public Object[] getParamDefaults() {
161: // the default values are calculated
162: Object[] defaults = new Object[] {
163: this
164: .getParamDefaultValue(PeriodicShift3DDescriptor.SHIFT_X_PARAMETER_NAME),
165: this
166: .getParamDefaultValue(PeriodicShift3DDescriptor.SHIFT_Y_PARAMETER_NAME),
167: this
168: .getParamDefaultValue(PeriodicShift3DDescriptor.SHIFT_Z_PARAMETER_NAME), };
169: return defaults;
170: }
171:
172: @Override
173: public String[] getParamNames() {
174: String[] names = {
175: PeriodicShift3DDescriptor.SHIFT_X_PARAMETER_NAME,
176: PeriodicShift3DDescriptor.SHIFT_Y_PARAMETER_NAME,
177: PeriodicShift3DDescriptor.SHIFT_Z_PARAMETER_NAME, };
178: return names;
179: }
180:
181: @Override
182: public boolean isParameterValueValid(String name, Object value) {
183: // all values are valid
184: boolean is_valid = true;
185: return is_valid;
186: }
187:
188: };
189:
190: /** Description of the parameters. */
191: private static final ParameterListDescriptor[] PARAMETER_LIST_DESCRIPTORS = new ParameterListDescriptor[] {
192: PeriodicShift3DDescriptor.PARAMETER_LIST_DESCRIPTOR,
193: PeriodicShift3DDescriptor.PARAMETER_LIST_DESCRIPTOR, };
194:
195: //---------------------------
196: // Constructor
197: //---------------------------
198:
199: /**
200: * Constructor.
201: */
202: public PeriodicShift3DDescriptor() {
203: super (RESOURCES, SUPPORTED_MODES, SOURCE_NAMES, SOURCE_CLASSES,
204: PARAMETER_LIST_DESCRIPTORS);
205: }
206:
207: //---------------------------
208: // Overriden methods from javax.media.jai.OperationDescriptorImpl
209: //---------------------------
210:
211: /**
212: * Adds the <em>shiftZ</em> parameter if it is missing then makes sure that
213: * all the image in the collection are of the same type.
214: * @param mode the rendering mode.
215: * @param pb the parameter block.
216: * @param sb the error message buffer.
217: * @return {@code true} if all is OK, {@code false} otherwise.
218: */
219: @Override
220: @SuppressWarnings("boxing")
221: public boolean validateParameters(String mode, ParameterBlock pb,
222: StringBuffer sb) {
223: boolean all_ok = super .validateParameters(mode, pb, sb);
224: if (false == all_ok) {
225: return all_ok;
226: }
227:
228: // we deal only with the shift_z parameter, the other two
229: // will be dealt in the implementation when operator
230: // "periodicshift" is called
231: Integer shift_z = (Integer) pb
232: .getObjectParameter(PeriodicShift3DDescriptor.SHIFT_Z_PARAMETER_INDEX);
233: if (null == shift_z) {
234: Collection<?> sources = (Collection<?>) pb.getSource(0);
235: int depth = sources.size();
236: shift_z = depth / 2;
237: pb.set(shift_z,
238: PeriodicShift3DDescriptor.SHIFT_Z_PARAMETER_INDEX);
239: }
240:
241: return all_ok;
242: }
243:
244: //---------------------------
245: // Class methods
246: //---------------------------
247:
248: /**
249: * Creates and fills a new parameter block.
250: * @param mode the rendering mode.
251: * @param source the source image.
252: * @param shift_x the displacement in <em>x</em>.
253: * @param shift_y the displacement in <em>y</em>.
254: * @param shift_z the displacement in <em>z</em>.
255: * @return a new parameter block.
256: */
257: protected static ParameterBlockJAI createParameterBlock(
258: String mode, Object source, int shift_x, int shift_y,
259: int shift_z) {
260: String name = PeriodicShift3DDescriptor.NAME;
261: ParameterBlockJAI pb = new ParameterBlockJAI(name, mode);
262: if (null != source) {
263: pb.addSource(source);
264: }
265: pb.setParameter(
266: PeriodicShift3DDescriptor.SHIFT_X_PARAMETER_NAME,
267: shift_x);
268: pb.setParameter(
269: PeriodicShift3DDescriptor.SHIFT_Y_PARAMETER_NAME,
270: shift_y);
271: pb.setParameter(
272: PeriodicShift3DDescriptor.SHIFT_Z_PARAMETER_NAME,
273: shift_z);
274: return pb;
275: }
276:
277: /**
278: * Performs the operation on a collection of rendered image.
279: * @param sources the source images.
280: * @param shift_x the displacement in <em>x</em>.
281: * @param shift_y the displacement in <em>y</em>.
282: * @param shift_z the displacement in <em>z</em>.
283: * @param hints the rendering hints, may be {@code null}.
284: * @return the resulting images.
285: */
286: @SuppressWarnings("unchecked")
287: public static Collection<RenderedImage> createCollection(
288: Collection<RenderedImage> sources, int shift_x,
289: int shift_y, int shift_z, RenderingHints hints) {
290: String mode = CollectionRegistryMode.MODE_NAME;
291: String name = PeriodicShift3DDescriptor.NAME;
292: ParameterBlockJAI parameter_block = PeriodicShift3DDescriptor
293: .createParameterBlock(mode, sources, shift_x, shift_y,
294: shift_z);
295: Collection<RenderedImage> sinks = JAI.createCollection(name,
296: parameter_block, hints);
297: return sinks;
298: }
299:
300: /**
301: * Performs the operation on a collection of renderable image.
302: * @param sources the source images.
303: * @param shift_x the displacement in <em>x</em>.
304: * @param shift_y the displacement in <em>y</em>.
305: * @param shift_z the displacement in <em>z</em>.
306: * @param hints the rendering hints, may be {@code null}.
307: * @return the resulting images.
308: */
309: @SuppressWarnings("unchecked")
310: public static Collection<RenderableImage> createRenderableCollection(
311: Collection<RenderableImage> sources, int shift_x,
312: int shift_y, int shift_z, RenderingHints hints) {
313: String mode = RenderableCollectionRegistryMode.MODE_NAME;
314: String name = PeriodicShift3DDescriptor.NAME;
315: ParameterBlockJAI parameter_block = PeriodicShift3DDescriptor
316: .createParameterBlock(mode, sources, shift_x, shift_y,
317: shift_z);
318: Collection<RenderableImage> sinks = JAI
319: .createRenderableCollection(name, parameter_block,
320: hints);
321: return sinks;
322: }
323:
324: //---------------------------
325: // External resources methods
326: //---------------------------
327:
328: /**
329: * Gets the description of this operation.
330: * @return the description message.
331: */
332: protected static String getDescription() {
333: String key = Resources.PERIODIC_SHIFT_3D_DESCRIPTION;
334: String message = Resources.getLocalizedString(key);
335: return message;
336: }
337:
338: /**
339: * Gets the description for the first argument, the displacement in
340: * <em>x</em>.
341: * @return the description message.
342: */
343: protected static String getArg0Description() {
344: String key = Resources.PERIODIC_SHIFT_3D_ARG0_DESCRIPTION;
345: String message = Resources.getLocalizedString(key);
346: return message;
347: }
348:
349: /**
350: * Gets the description for the second argument, the displacement in
351: * <em>y</em>.
352: * @return the description message.
353: */
354: protected static String getArg1Description() {
355: String key = Resources.PERIODIC_SHIFT_3D_ARG1_DESCRIPTION;
356: String message = Resources.getLocalizedString(key);
357: return message;
358: }
359:
360: /**
361: * Gets the description for the third argument, the displacement in
362: * <em>z</em>.
363: * @return the description message.
364: */
365: protected static String getArg2Description() {
366: String key = Resources.PERIODIC_SHIFT_3D_ARG2_DESCRIPTION;
367: String message = Resources.getLocalizedString(key);
368: return message;
369: }
370:
371: }
372:
373: /*
374: * $Log: PeriodicShift3DDescriptor.java,v $
375: * Revision 1.4 2007/09/07 18:08:15 forklabs
376: * Updated JavaDoc.
377: *
378: * Revision 1.3 2007/07/17 16:32:13 forklabs
379: * Operation renamed from periodicshift3d to periodicshift.
380: *
381: * Revision 1.2 2007/06/13 18:57:21 forklabs
382: * Changed parent to use CollectionDescriptor.
383: *
384: * Revision 1.1 2007/06/05 20:37:39 forklabs
385: * Operator periodicshift3d.
386: *
387: */
|