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.geom.AffineTransform;
020:
021: import java.awt.image.AffineTransformOp;
022: import java.awt.image.WritableRaster;
023:
024: import java.util.StringTokenizer;
025:
026: import org.apache.avalon.framework.parameters.Parameters;
027:
028: import org.apache.cocoon.ProcessingException;
029:
030: public class AffineTransformOperation implements ImageOperation {
031: private String m_Prefix;
032: private boolean m_Enabled;
033: private float[] m_Matrix;
034:
035: public void setPrefix(String prefix) {
036: m_Prefix = prefix;
037: }
038:
039: public void setup(Parameters params) throws ProcessingException {
040: m_Enabled = params.getParameterAsBoolean(m_Prefix + "enabled",
041: true);
042: int size = params.getParameterAsInteger(m_Prefix
043: + "matrix-size", 6);
044: String values = params.getParameter(m_Prefix + "values", null);
045:
046: if (size != 4 && size != 6)
047: throw new ProcessingException(
048: "Only matrices of 4 or 6 elements can be used.");
049:
050: if (values != null)
051: m_Matrix = getFloatArray(values);
052: else
053: m_Matrix = new float[size];
054:
055: if (m_Matrix.length != 4 && m_Matrix.length != 6)
056: throw new ProcessingException(
057: "Only matrices of 4 or 6 elements can be used.");
058:
059: float m00 = params.getParameterAsFloat(m_Prefix + "m00",
060: Float.NaN);
061: float m01 = params.getParameterAsFloat(m_Prefix + "m01",
062: Float.NaN);
063: float m02 = params.getParameterAsFloat(m_Prefix + "m02",
064: Float.NaN);
065: float m10 = params.getParameterAsFloat(m_Prefix + "m10",
066: Float.NaN);
067: float m11 = params.getParameterAsFloat(m_Prefix + "m11",
068: Float.NaN);
069: float m12 = params.getParameterAsFloat(m_Prefix + "m12",
070: Float.NaN);
071:
072: if (m_Matrix.length == 4) {
073: m_Matrix[0] = m00;
074: m_Matrix[1] = m01;
075: m_Matrix[2] = m10;
076: m_Matrix[3] = m11;
077: } else {
078: m_Matrix[0] = m00;
079: m_Matrix[1] = m01;
080: m_Matrix[2] = m02;
081: m_Matrix[3] = m10;
082: m_Matrix[4] = m11;
083: m_Matrix[5] = m12;
084: }
085: }
086:
087: public WritableRaster apply(WritableRaster image) {
088: if (!m_Enabled)
089: return image;
090: AffineTransform transform = new AffineTransform(m_Matrix);
091: AffineTransformOp op = new AffineTransformOp(transform,
092: AffineTransformOp.TYPE_BILINEAR);
093: WritableRaster scaledRaster = op.filter(image, null);
094: return scaledRaster;
095: }
096:
097: public String getKey() {
098: return "affine:" + (m_Enabled ? "enable" : "disable") + ":"
099: + getMatrixAsString() + ":" + m_Prefix;
100: }
101:
102: private float[] getFloatArray(String values) {
103: float[] fvalues = new float[30];
104: int counter = 0;
105: StringTokenizer st = new StringTokenizer(values, ",", false);
106: for (int i = 0; st.hasMoreTokens(); i++) {
107: String value = st.nextToken().trim();
108: fvalues[i] = Float.parseFloat(value);
109: counter = counter + 1;
110: }
111: float[] result = new float[counter];
112: for (int i = 0; i < counter; i++)
113: result[i] = fvalues[i];
114: return result;
115: }
116:
117: private String getMatrixAsString() {
118: StringBuffer b = new StringBuffer();
119: for (int i = 0; i < m_Matrix.length; i++) {
120: if (i != 0)
121: b.append(",");
122: b.append(m_Matrix[i]);
123: }
124: String result = b.toString();
125: b.setLength(0);
126: return result;
127: }
128:
129: }
|