001: /*
002: * $RCSfile: AddConstToCollectionCIF.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:56:12 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.opimage;
013:
014: import java.awt.RenderingHints;
015: import java.awt.image.renderable.ParameterBlock;
016: import java.util.ArrayList;
017: import java.util.Collection;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import javax.media.jai.CollectionImage;
021: import javax.media.jai.CollectionImageFactory;
022: import javax.media.jai.CollectionOp;
023: import javax.media.jai.ImageLayout;
024: import javax.media.jai.PlanarImage;
025: import javax.media.jai.RenderedOp;
026:
027: /**
028: * A <code>CIF</code> supporting the "AddConstToCollection" operation.
029: *
030: * @see javax.media.jai.operator.AddConstToCollectionDescriptor
031: * @see AddConstToCollectionOpImage
032: *
033: *
034: * @since EA4
035: */
036: public class AddConstToCollectionCIF implements CollectionImageFactory {
037:
038: /** Constructor. */
039: public AddConstToCollectionCIF() {
040: }
041:
042: /**
043: * Creates a new instance of <code>AddConstToCollectionOpImage</code>.
044: *
045: * @param args Input source collection and constants
046: * @param hints Optionally contains destination image layout.
047: */
048: public CollectionImage create(ParameterBlock args,
049: RenderingHints hints) {
050: return new AddConstToCollectionOpImage((Collection) args
051: .getSource(0), hints, (double[]) args
052: .getObjectParameter(0));
053: }
054:
055: /**
056: * Updates an instance of <code>AddConstToCollectionOpImage</code>.
057: */
058: public CollectionImage update(ParameterBlock oldParamBlock,
059: RenderingHints oldHints, ParameterBlock newParamBlock,
060: RenderingHints newHints, CollectionImage oldRendering,
061: CollectionOp op) {
062: CollectionImage updatedCollection = null;
063:
064: if (oldParamBlock.getObjectParameter(0).equals(
065: newParamBlock.getObjectParameter(0))
066: && (oldHints == null ? newHints == null : oldHints
067: .equals(newHints))) {
068:
069: // Retrieve the old and new sources and the parameters.
070: Collection oldSource = (Collection) oldParamBlock
071: .getSource(0);
072: Collection newSource = (Collection) newParamBlock
073: .getSource(0);
074: double[] constants = (double[]) oldParamBlock
075: .getObjectParameter(0);
076:
077: // Construct a Collection of common sources.
078: Collection commonSources = new ArrayList();
079: Iterator it = oldSource.iterator();
080: while (it.hasNext()) {
081: Object oldElement = it.next();
082: if (newSource.contains(oldElement)) {
083: commonSources.add(oldElement);
084: }
085: }
086:
087: if (commonSources.size() != 0) {
088: // Construct a Collection of the RenderedOp nodes that
089: // will be retained in the new CollectionImage.
090: ArrayList commonNodes = new ArrayList(commonSources
091: .size());
092: it = oldRendering.iterator();
093: while (it.hasNext()) {
094: RenderedOp node = (RenderedOp) it.next();
095: PlanarImage source = (PlanarImage) node
096: .getSourceImage(0);
097: if (commonSources.contains(source)) {
098: commonNodes.add(node);
099: }
100: }
101:
102: // Create a new CollectionImage.
103: updatedCollection = new AddConstToCollectionOpImage(
104: newSource, newHints, constants);
105:
106: // Remove from the new CollectionImage all nodes that
107: // are common with the old CollectionImage.
108: ArrayList newNodes = new ArrayList(oldRendering.size()
109: - commonSources.size());
110: it = updatedCollection.iterator();
111: while (it.hasNext()) {
112: RenderedOp node = (RenderedOp) it.next();
113: PlanarImage source = (PlanarImage) node
114: .getSourceImage(0);
115: if (commonSources.contains(source)) {
116: it.remove();
117: }
118: }
119:
120: // Add all the common nodes to the new CollectionImage.
121: it = commonNodes.iterator();
122: while (it.hasNext()) {
123: updatedCollection.add(it.next());
124: }
125: }
126: }
127:
128: return updatedCollection;
129: }
130: }
|