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:
023: import java.util.StringTokenizer;
024:
025: import org.apache.avalon.framework.parameters.Parameters;
026:
027: import org.apache.cocoon.ProcessingException;
028:
029: public class ConvolveOperation implements ImageOperation {
030: private String m_Prefix;
031: private boolean m_Enabled;
032: private int m_ConvolveHeight;
033: private int m_ConvolveWidth;
034: private float[] m_Data;
035:
036: public void setPrefix(String prefix) {
037: m_Prefix = prefix;
038: }
039:
040: public void setup(Parameters params) throws ProcessingException {
041: m_Enabled = params.getParameterAsBoolean(m_Prefix + "enabled",
042: true);
043: m_ConvolveWidth = params.getParameterAsInteger(m_Prefix
044: + "width", 3);
045: m_ConvolveHeight = params.getParameterAsInteger(m_Prefix
046: + "height", 3);
047: String values = params.getParameter(m_Prefix + "data", "");
048: m_Data = getFloatArray(values);
049: if (m_Data.length != m_ConvolveWidth * m_ConvolveHeight)
050: throw new ProcessingException(
051: "The width*height must be equal to the number of data elements given: "
052: + (m_ConvolveWidth * m_ConvolveHeight)
053: + " is not compatible with '" + values
054: + "'");
055: }
056:
057: public WritableRaster apply(WritableRaster image) {
058: if (!m_Enabled)
059: return image;
060:
061: Kernel kernel = new Kernel(m_ConvolveWidth, m_ConvolveHeight,
062: m_Data);
063: ConvolveOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
064: null);
065: WritableRaster r = op.filter(image, null);
066: return r;
067: }
068:
069: private float[] getFloatArray(String values) {
070: float[] fvalues = new float[30];
071: int counter = 0;
072: StringTokenizer st = new StringTokenizer(values, ",", false);
073: for (int i = 0; st.hasMoreTokens(); i++) {
074: String value = st.nextToken().trim();
075: fvalues[i] = Float.parseFloat(value);
076: counter = counter + 1;
077: }
078: float[] result = new float[counter];
079: for (int i = 0; i < counter; i++)
080: result[i] = fvalues[i];
081: return result;
082: }
083:
084: private String getDataAsString() {
085: StringBuffer b = new StringBuffer();
086: for (int i = 0; i < m_Data.length; i++) {
087: if (i != 0)
088: b.append(",");
089: b.append(m_Data[i]);
090: }
091: String result = b.toString();
092: b.setLength(0);
093: return result;
094: }
095:
096: public String getKey() {
097: return "convolve:" + (m_Enabled ? "enable" : "disable") + ":"
098: + m_ConvolveWidth + ":" + m_ConvolveHeight + ":"
099: + getDataAsString() + ":" + m_Prefix;
100: }
101: }
|