01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
05: * (C) 2003, Institut de Recherche pour le Développement
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: */
17: package org.geotools.image.jai;
18:
19: // J2SE dependencies
20: import java.awt.RenderingHints;
21: import java.awt.image.RenderedImage;
22: import java.awt.image.renderable.ParameterBlock;
23: import java.util.List;
24: import java.util.Vector;
25:
26: // JAI dependencies
27: import javax.media.jai.CRIFImpl;
28:
29: /**
30: * The image factory for the {@link Combine} operation.
31: *
32: * @since 2.1
33: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/main/java/org/geotools/image/jai/CombineCRIF.java $
34: * @version $Id: CombineCRIF.java 20970 2006-08-11 07:53:22Z jgarnett $
35: * @author Remi Eve
36: * @author Martin Desruisseaux
37: */
38: public class CombineCRIF extends CRIFImpl {
39: /**
40: * Constructs a default factory.
41: */
42: public CombineCRIF() {
43: }
44:
45: /**
46: * Creates a {@link RenderedImage} representing the results of an imaging
47: * operation for a given {@link ParameterBlock} and {@link RenderingHints}.
48: */
49: public RenderedImage create(final ParameterBlock param,
50: final RenderingHints hints) {
51: final Vector sources = param.getSources();
52: final double[][] matrix = (double[][]) param
53: .getObjectParameter(0);
54: final CombineTransform transform = (CombineTransform) param
55: .getObjectParameter(1);
56: return transform == null && isDyadic(sources, matrix) ? new Combine.Dyadic(
57: sources, matrix, hints)
58: : new Combine(sources, matrix, transform, hints);
59: }
60:
61: /**
62: * Returns {@code true} if the combine operation could be done through
63: * the optimized {@code Combine.Dyadic} class.
64: */
65: private static boolean isDyadic(final List sources,
66: final double[][] matrix) {
67: if (sources.size() != 2) {
68: return false;
69: }
70: final RenderedImage src0 = (RenderedImage) sources.get(0);
71: final RenderedImage src1 = (RenderedImage) sources.get(1);
72: final int numBands0 = src0.getSampleModel().getNumBands();
73: final int numBands1 = src1.getSampleModel().getNumBands();
74: final int numBands = matrix.length;
75: if (numBands != numBands0 || numBands != numBands1) {
76: return false;
77: }
78: for (int i = 0; i < numBands; i++) {
79: final double[] row = matrix[i];
80: for (int j = numBands0 + numBands1; --j >= 0;) {
81: if (j != i && j != i + numBands0 && row[j] != 0) {
82: return false;
83: }
84: }
85: }
86: return true;
87: }
88: }
|