01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.reading.imageop;
18:
19: import java.awt.geom.AffineTransform;
20:
21: import java.awt.image.AffineTransformOp;
22: import java.awt.image.WritableRaster;
23:
24: import org.apache.avalon.framework.parameters.Parameters;
25:
26: import org.apache.cocoon.ProcessingException;
27:
28: public class ResizeOperation implements ImageOperation {
29: private String m_Prefix;
30: private boolean m_Enabled;
31: private int m_Height;
32: private int m_Width;
33: private boolean m_PreserveRatio;
34: private boolean m_AdjustX;
35:
36: public void setPrefix(String prefix) {
37: m_Prefix = prefix;
38: }
39:
40: public void setup(Parameters params) throws ProcessingException {
41: m_Enabled = params.getParameterAsBoolean(m_Prefix + "enabled",
42: true);
43: m_Height = params.getParameterAsInteger(m_Prefix + "height",
44: 200);
45: if (m_Height < 0)
46: throw new ProcessingException(
47: "Negative Height is not allowed: " + m_Height);
48: m_Width = params.getParameterAsInteger(m_Prefix + "width", 300);
49: if (m_Width < 0)
50: throw new ProcessingException(
51: "Negative Width is not allowed: " + m_Width);
52: m_PreserveRatio = params.getParameterAsBoolean(m_Prefix
53: + "preserve-ratio", false);
54: m_AdjustX = params.getParameterAsBoolean(m_Prefix + "adjust-x",
55: false);
56: }
57:
58: public WritableRaster apply(WritableRaster image) {
59: if (!m_Enabled)
60: return image;
61: double height = image.getHeight();
62: double width = image.getWidth();
63: double xScale = m_Width / width;
64: double yScale = m_Height / height;
65: if (m_PreserveRatio) {
66: if (m_AdjustX)
67: xScale = yScale;
68: else
69: yScale = xScale;
70: }
71: AffineTransform scale = AffineTransform.getScaleInstance(
72: xScale, yScale);
73: AffineTransformOp op = new AffineTransformOp(scale,
74: AffineTransformOp.TYPE_BILINEAR);
75: WritableRaster scaledRaster = op.filter(image, null);
76: return scaledRaster;
77: }
78:
79: public String getKey() {
80: return "resize:" + (m_Enabled ? "enable" : "disable") + ":"
81: + m_Width + ":" + m_Height + ":" + m_Prefix;
82: }
83: }
|