From: Niki Roo Date: Thu, 7 May 2020 17:24:10 +0000 (+0200) Subject: ImageUtils + awt: change scale parameters X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=7547fcd3bedafff17d3cecd87081b5ed9d06fbeb;p=fanfix-swing.git ImageUtils + awt: change scale parameters --- diff --git a/src/be/nikiroo/utils/ImageUtils.java b/src/be/nikiroo/utils/ImageUtils.java index 2b8ff8fa..877c8fa8 100644 --- a/src/be/nikiroo/utils/ImageUtils.java +++ b/src/be/nikiroo/utils/ImageUtils.java @@ -44,40 +44,39 @@ public abstract class ImageUtils { /** * Scale a dimension. * - * @param areaWidth - * the base width of the target dimension for snap sizes - * @param areaHeight - * the base height of the target dimension for snap sizes + * * @param imageWidth * the actual image width * @param imageHeight * the actual image height + * @param areaWidth + * the base width of the target dimension for snap sizes + * @param areaHeight + * the base height of the target dimension for snap sizes * @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) + * 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 size, width is [0] and height is [1] (minimum is 1x1) */ - protected static Integer[] scaleSize(int areaWidth, int areaHeight, - int imageWidth, int imageHeight, double zoom, boolean zoomSnapWidth) { + protected static Integer[] scaleSize(int imageWidth, int imageHeight, + int areaWidth, int areaHeight, double zoom, Boolean snapMode) { int width; int height; - if (zoom > 0) { + if (snapMode == null) { width = (int) Math.round(imageWidth * zoom); height = (int) Math.round(imageHeight * zoom); + } else if (snapMode) { + width = areaWidth; + height = (int) Math + .round((((double) areaWidth) / imageWidth) * imageHeight); } else { - if (zoomSnapWidth) { - width = areaWidth; - height = (int) Math.round( - (((double) areaWidth) / imageWidth) * imageHeight); - } else { - height = areaHeight; - width = (int) Math.round( - (((double) areaHeight) / imageHeight) * imageWidth); + height = areaHeight; + width = (int) Math + .round((((double) areaHeight) / imageHeight) * imageWidth); - } } if (width < 1) diff --git a/src/be/nikiroo/utils/ui/ImageUtilsAwt.java b/src/be/nikiroo/utils/ui/ImageUtilsAwt.java index 19c16a01..c273e0d4 100644 --- a/src/be/nikiroo/utils/ui/ImageUtilsAwt.java +++ b/src/be/nikiroo/utils/ui/ImageUtilsAwt.java @@ -262,32 +262,69 @@ 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(); }