X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=ui%2FImageUtilsAwt.java;h=c273e0d45879628543dcab75f2c7c88f133224d9;hb=712ddafb749aada41daab85c36ac12f657b2307e;hp=9412c93cd93b52c00d4fbc33cc93dc8c9860c17a;hpb=b3424395dbdc159c445aea4c465131a269b61535;p=fanfix.git diff --git a/ui/ImageUtilsAwt.java b/ui/ImageUtilsAwt.java index 9412c93..c273e0d 100644 --- a/ui/ImageUtilsAwt.java +++ b/ui/ImageUtilsAwt.java @@ -37,6 +37,7 @@ public class ImageUtilsAwt extends ImageUtils { /** Rotate the image by 180° */ UTURN } + @Override protected boolean check() { // Will not work if ImageIO is not available @@ -96,7 +97,7 @@ public class ImageUtilsAwt extends ImageUtils { public static BufferedImage fromImage(Image img) throws IOException { return fromImage(img, Rotation.NONE); } - + /** * Convert the given {@link Image} into a {@link BufferedImage} object, * respecting the EXIF transformations if any. @@ -112,7 +113,8 @@ public class ImageUtilsAwt extends ImageUtils { * @throws IOException * in case of IO error */ - public static BufferedImage fromImage(Image img, Rotation rotation) throws IOException { + public static BufferedImage fromImage(Image img, Rotation rotation) + throws IOException { InputStream in = img.newInputStream(); BufferedImage image; try { @@ -146,8 +148,7 @@ public class ImageUtilsAwt extends ImageUtils { } finally { inData.close(); } - extra = ", content: " - + new String(data, "UTF-8"); + extra = ", content: " + new String(data, "UTF-8"); } catch (Exception e) { extra = ", content unavailable"; } @@ -202,24 +203,34 @@ public class ImageUtilsAwt extends ImageUtils { affineTransform = null; break; } - + if (rotation == null) rotation = Rotation.NONE; - + switch (rotation) { - case LEFT: + case RIGHT: if (affineTransform == null) { affineTransform = new AffineTransform(); } affineTransform.translate(height, 0); affineTransform.rotate(Math.PI / 2); + + int tmp = width; + width = height; + height = tmp; + break; - case RIGHT: + case LEFT: if (affineTransform == null) { affineTransform = new AffineTransform(); } affineTransform.translate(0, width); affineTransform.rotate(3 * Math.PI / 2); + + int temp = width; + width = height; + height = temp; + break; case UTURN: if (affineTransform == null) { @@ -251,36 +262,73 @@ public class ImageUtilsAwt extends ImageUtils { return image; } + /** + * Scale a dimension. + * + * @param imageSize + * the actual image size + * @param areaSize + * the base size of the target to get snap sizes for + * @param zoom + * the zoom factor (ignored on snap mode) + * @param snapMode + * NULL for no snap mode, TRUE to snap to width and FALSE for + * snap to height) + * + * @return the scaled (minimum is 1x1) + */ + public static Dimension scaleSize(Dimension imageSize, Dimension areaSize, + double zoom, Boolean snapMode) { + Integer[] sz = scaleSize(imageSize.width, imageSize.height, + areaSize.width, areaSize.height, zoom, snapMode); + return new Dimension(sz[0], sz[1]); + } + /** * Resize the given image. * + * @param image + * the image to resize * @param areaSize * the base size of the target dimension for snap sizes + * @param zoom + * the zoom factor (ignored on snap mode) + * @param snapMode + * NULL for no snap mode, TRUE to snap to width and FALSE for + * snap to height) + * + * @return a new, resized image + */ + public static BufferedImage scaleImage(BufferedImage image, + Dimension areaSize, double zoom, Boolean snapMode) { + Dimension scaledSize = scaleSize( + new Dimension(image.getWidth(), image.getHeight()), areaSize, + zoom, snapMode); + + return scaleImage(image, scaledSize); + } + + /** + * Resize the given image. + * * @param image * the image to resize - * @param zoom - * the zoom factor or -1 for snap size - * @param zoomSnapWidth - * if snap size, TRUE to snap to width (and FALSE, snap to - * height) + * @param targetSize + * the target size * * @return a new, resized image */ - public static BufferedImage scaleImage(Dimension areaSize, - BufferedImage image, double zoom, boolean zoomSnapWidth) { - Integer scaledSize[] = scaleSize(areaSize.width, areaSize.height, - image.getWidth(), image.getHeight(), zoom, zoomSnapWidth); - int width = scaledSize[0]; - int height = scaledSize[1]; - BufferedImage resizedImage = new BufferedImage(width, height, - BufferedImage.TYPE_4BYTE_ABGR); + public static BufferedImage scaleImage(BufferedImage image, + Dimension targetSize) { + BufferedImage resizedImage = new BufferedImage(targetSize.width, + targetSize.height, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g = resizedImage.createGraphics(); try { - g.drawImage(image, 0, 0, width, height, null); + g.drawImage(image, 0, 0, targetSize.width, targetSize.height, null); } finally { g.dispose(); } - + return resizedImage; } }