001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.reading.imageop;
018:
019: import java.awt.image.ConvolveOp;
020: import java.awt.image.Kernel;
021: import java.awt.image.WritableRaster;
022: import java.util.StringTokenizer;
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.cocoon.ProcessingException;
025:
026: public class ConvolveOperation implements ImageOperation {
027:
028: private String prefix;
029: private boolean enabled;
030: private int convolveHeight;
031: private int convolveWidth;
032: private float[] data;
033:
034: public void setPrefix(String prefix) {
035: this .prefix = prefix;
036: }
037:
038: public void setup(Parameters params) throws ProcessingException {
039: enabled = params
040: .getParameterAsBoolean(prefix + "enabled", true);
041: convolveWidth = params.getParameterAsInteger(prefix + "width",
042: 3);
043: convolveHeight = params.getParameterAsInteger(
044: prefix + "height", 3);
045: String values = params.getParameter(prefix + "data", "");
046: data = getFloatArray(values);
047: if (data.length != convolveWidth * convolveHeight) {
048: throw new ProcessingException(
049: "The width*height must be equal to the number of data elements given: "
050: + (convolveWidth * convolveHeight)
051: + " is not compatible with '" + values
052: + "'");
053: }
054: }
055:
056: public WritableRaster apply(WritableRaster image) {
057: if (!enabled) {
058: return image;
059: }
060: Kernel kernel = new Kernel(convolveWidth, convolveHeight, data);
061: ConvolveOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
062: null);
063: WritableRaster r = op.filter(image, null);
064: return r;
065: }
066:
067: private float[] getFloatArray(String values) {
068: float[] fvalues = new float[30];
069: int counter = 0;
070: StringTokenizer st = new StringTokenizer(values, ",", false);
071: for (int i = 0; st.hasMoreTokens(); i++) {
072: String value = st.nextToken().trim();
073: fvalues[i] = Float.parseFloat(value);
074: counter = counter + 1;
075: }
076: float[] result = new float[counter];
077: for (int i = 0; i < counter; i++) {
078: result[i] = fvalues[i];
079: }
080: return result;
081: }
082:
083: private String getDataAsString() {
084: StringBuffer b = new StringBuffer();
085: for (int i = 0; i < data.length; i++) {
086: if (i != 0) {
087: b.append(",");
088: }
089: b.append(data[i]);
090: }
091: String result = b.toString();
092: b.setLength(0);
093: return result;
094: }
095:
096: public String getKey() {
097: return "convolve:" + (enabled ? "enable" : "disable") + ":"
098: + convolveWidth + ":" + convolveHeight + ":"
099: + getDataAsString() + ":" + prefix;
100: }
101: }
|