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: import java.awt.image.AffineTransformOp;
21: import java.awt.image.WritableRaster;
22: import org.apache.avalon.framework.parameters.Parameters;
23: import org.apache.cocoon.ProcessingException;
24:
25: public class ResizeOperation implements ImageOperation {
26:
27: private String prefix;
28: private boolean enabled;
29: private int height;
30: private int width;
31: private boolean preserveRatio;
32: private boolean adjustX;
33:
34: public void setPrefix(String prefix) {
35: this .prefix = prefix;
36: }
37:
38: public void setup(Parameters params) throws ProcessingException {
39: enabled = params
40: .getParameterAsBoolean(prefix + "enabled", true);
41: height = params.getParameterAsInteger(prefix + "height", 200);
42: if (height < 0) {
43: throw new ProcessingException(
44: "Negative Height is not allowed: " + height);
45: }
46: width = params.getParameterAsInteger(prefix + "width", 300);
47: if (width < 0) {
48: throw new ProcessingException(
49: "Negative Width is not allowed: " + width);
50: }
51: preserveRatio = params.getParameterAsBoolean(prefix
52: + "preserve-ratio", false);
53: adjustX = params.getParameterAsBoolean(prefix + "adjust-x",
54: false);
55: }
56:
57: public WritableRaster apply(WritableRaster image) {
58: if (!enabled) {
59: return image;
60: }
61: double height = image.getHeight();
62: double width = image.getWidth();
63: double xScale = this .width / width;
64: double yScale = this .height / height;
65: if (preserveRatio) {
66: if (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:" + (enabled ? "enable" : "disable") + ":"
81: + width + ":" + height + ":" + prefix;
82: }
83: }
|