| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.imaging; |
| 16 | |
| 17 | import java.awt.Graphics; |
| 18 | import java.awt.image.BufferedImage; |
| 19 | |
| 20 | /** |
| 21 | * Provides miscellaneous utility functions for handling images. |
| 22 | * There is no need to instantiate this class since it only provides |
| 23 | * static methods. |
| 24 | * @author Jean-Francois Poilpret |
| 25 | */ |
| 26 | public class ImageTools |
| 27 | { |
| 28 | protected ImageTools() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Clone an image. |
| 34 | * @param source original image to clone |
| 35 | * @param sourceType type of the source image (as defined in BufferedImage) |
| 36 | * @return a new image which is an exact copy of the source image |
| 37 | */ |
| 38 | public static BufferedImage cloneImage(BufferedImage source, int sourceType) |
| 39 | { |
| 40 | BufferedImage target = new BufferedImage( source.getWidth(), |
| 41 | source.getHeight(), |
| 42 | sourceType); |
| 43 | Graphics g = target.getGraphics(); |
| 44 | g.drawImage(source, 0, 0, null); |
| 45 | g.dispose(); |
| 46 | return target; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Scale an Image to a new size. The method keeps the aspect ratio of the |
| 51 | * original Image. |
| 52 | * @param source original image to scale down |
| 53 | * @param sourceType type of the source image (as defined in BufferedImage) |
| 54 | * @param newSize new size to which the image should be scaled |
| 55 | * @return a new image which is a scaled copy of the source image |
| 56 | */ |
| 57 | public static BufferedImage scaleImage(BufferedImage source, int sourceType, int newSize) |
| 58 | { |
| 59 | int width = source.getWidth(null); |
| 60 | int height = source.getHeight(null); |
| 61 | if (width <= height) |
| 62 | { |
| 63 | width = newSize * width / height; |
| 64 | height = newSize; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | height = newSize * height / width; |
| 69 | width = newSize; |
| 70 | } |
| 71 | BufferedImage target = new BufferedImage(width, height, sourceType); |
| 72 | Graphics g = target.getGraphics(); |
| 73 | g.drawImage(source, 0, 0, width, height, null); |
| 74 | g.dispose(); |
| 75 | return target; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Scale an Image to a new size. The method keeps the aspect ratio of the |
| 80 | * original Image. |
| 81 | * @param source original image to scale down |
| 82 | * @param sourceType type of the source image (as defined in BufferedImage) |
| 83 | * @param ratio scaling ratio |
| 84 | * @return a new image which is a scaled copy of the source image |
| 85 | */ |
| 86 | public static BufferedImage scaleImage(BufferedImage source, int sourceType, double ratio) |
| 87 | { |
| 88 | int size = Math.max(source.getWidth(null), source.getHeight(null)); |
| 89 | int newSize = (int) (ratio * size); |
| 90 | return scaleImage(source, sourceType, newSize); |
| 91 | } |
| 92 | } |