01: /* ***** BEGIN LICENSE BLOCK *****
02: * Version: MPL 1.1
03: * The contents of this file are subject to the Mozilla Public License Version
04: * 1.1 (the "License"); you may not use this file except in compliance with
05: * the License. You may obtain a copy of the License at
06: * http://www.mozilla.org/MPL/
07: *
08: * Software distributed under the License is distributed on an "AS IS" basis,
09: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10: * for the specific language governing rights and limitations under the
11: * License.
12: *
13: * The Original Code is Riot.
14: *
15: * The Initial Developer of the Original Code is
16: * Neteye GmbH.
17: * Portions created by the Initial Developer are Copyright (C) 2006
18: * the Initial Developer. All Rights Reserved.
19: *
20: * Contributor(s):
21: * Felix Gnass [fgnass at neteye dot de]
22: *
23: * ***** END LICENSE BLOCK ***** */
24: package org.riotfamily.common.image;
25:
26: import java.awt.Graphics2D;
27: import java.awt.RenderingHints;
28: import java.awt.image.BufferedImage;
29: import java.io.File;
30: import java.io.IOException;
31:
32: /**
33: * Thumbnailer that uses the Java ImageIO API.
34: *
35: * @author Felix Gnass [fgnass at neteye dot de]
36: */
37: public class ImageIOThumbnailer implements Thumbnailer {
38:
39: private int maxCrop = 0;
40:
41: public void setMaxCrop(int maxCrop) {
42: this .maxCrop = maxCrop;
43: }
44:
45: public void renderThumbnail(File source, File dest, int width,
46: int height) throws IOException {
47:
48: BufferedImage originalImage = ImageUtils.readImage(source);
49: boolean alpha = originalImage.getColorModel().hasAlpha();
50:
51: int imageWidth = originalImage.getWidth(null);
52: int imageHeight = originalImage.getHeight(null);
53:
54: int thumbWidth;
55: int thumbHeight;
56:
57: double scaleX = (double) width / (double) imageWidth;
58: double scaleY = (double) height / (double) imageHeight;
59:
60: double scale = Math.min(Math.max(scaleX, scaleY), 1);
61: thumbWidth = (int) (imageWidth * scale);
62: thumbHeight = (int) (imageHeight * scale);
63: double cropFactor = (double) maxCrop / 100;
64:
65: if ((thumbWidth - width > cropFactor * thumbWidth)
66: || (thumbHeight - height > cropFactor * thumbHeight)) {
67:
68: scaleX = width / (imageWidth - cropFactor * imageWidth);
69: scaleY = height / (imageHeight - cropFactor * imageHeight);
70: scale = Math.min(Math.min(scaleX, scaleY), 1);
71: }
72:
73: thumbWidth = (int) (imageWidth * scale);
74: thumbHeight = (int) (imageHeight * scale);
75:
76: BufferedImage thumbImage = new BufferedImage(thumbWidth,
77: thumbHeight, alpha ? BufferedImage.TYPE_INT_ARGB
78: : BufferedImage.TYPE_INT_RGB);
79:
80: Graphics2D graphics2D = thumbImage.createGraphics();
81: graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
82: RenderingHints.VALUE_INTERPOLATION_BICUBIC);
83:
84: graphics2D.drawImage(originalImage, 0, 0, thumbWidth,
85: thumbHeight, null);
86:
87: int x = Math.max((thumbWidth - width) / 2, 0);
88: int y = Math.max((thumbHeight - height) / 2, 0);
89: if (x > 0 || y > 0) {
90: thumbWidth = Math.min(thumbWidth, width);
91: thumbHeight = Math.min(thumbHeight, height);
92: thumbImage = thumbImage.getSubimage(x, y, thumbWidth,
93: thumbHeight);
94: }
95:
96: ImageUtils.write(thumbImage, dest);
97: }
98:
99: }
|