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