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.image.WritableRaster;
20: import org.apache.avalon.framework.parameters.Parameters;
21: import org.apache.cocoon.ProcessingException;
22:
23: /**
24: * The Crop operation crops the image according to the specified aspect ratio.
25: * A ratio of 2 means, the height of the image is twice the width, a ratio of 0.5
26: * means the height of the image is half of the width.
27: * Add summary documentation here.
28: *
29: * @version $Id: CropOperation.java 509607 2007-02-20 15:32:23Z cziegeler $
30: */
31: public class CropOperation implements ImageOperation {
32:
33: private String prefix;
34: private boolean enabled;
35:
36: /**
37: * ratio = height : width
38: */
39: private float ratio;
40:
41: public void setPrefix(String prefix) {
42: this .prefix = prefix;
43: }
44:
45: public void setup(Parameters params) throws ProcessingException {
46: enabled = params
47: .getParameterAsBoolean(prefix + "enabled", true);
48: ratio = params.getParameterAsFloat(prefix + "ratio", 200);
49: if (ratio < 0) {
50: throw new ProcessingException(
51: "Negative Height is not allowed: " + ratio);
52: }
53: }
54:
55: public WritableRaster apply(WritableRaster image) {
56: if (!enabled) {
57: return image;
58: }
59: //maximum new height or width
60: int max = Math.min(image.getHeight(), image.getWidth());
61: int height;
62: int width;
63: if (ratio > 1) {
64: height = max;
65: width = (int) (max / ratio);
66: } else {
67: width = max;
68: height = (int) (max * ratio);
69: }
70:
71: int hdelta = (image.getWidth() - width) / 2;
72: int vdelta = (image.getHeight() - height) / 2;
73:
74: WritableRaster result = image.createWritableChild(hdelta,
75: vdelta, width, height, hdelta, vdelta, null);
76:
77: return result;
78: }
79:
80: public String getKey() {
81: return "crop:" + (enabled ? "enable" : "disable") + ":" + ratio
82: + ":" + prefix;
83: }
84: }
|