X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=ImageUtils.java;h=c0e123280cd5aa846bd57e08324a81df253c9de4;hb=b3424395dbdc159c445aea4c465131a269b61535;hp=fb869294f2029bd1c0fd7f4a64680259ca4bc4b0;hpb=143d16e3caae370e6108aeb77680d11308d10e08;p=nikiroo-utils.git diff --git a/ImageUtils.java b/ImageUtils.java index fb86929..c0e1232 100644 --- a/ImageUtils.java +++ b/ImageUtils.java @@ -41,6 +41,53 @@ public abstract class ImageUtils { public abstract void saveAsImage(Image img, File target, String format) throws IOException; + /** + * 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 zoom + * the zoom factor, or -1 for snap size + * @param zoomSnapWidth + * if snap size, TRUE to snap to width (and FALSE, 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) { + int width; + int height; + if (zoom > 0) { + width = (int) Math.round(imageHeight * zoom); + height = (int) Math.round(imageHeight * zoom); + } else { + if (zoomSnapWidth) { + width = areaWidth; + height = (int) Math.round( + (((double) areaWidth) / imageWidth) * imageHeight); + } else { + height = areaHeight; + width = (int) Math.round( + (((double) areaHeight) / imageHeight) * imageWidth); + + } + } + + if (width < 1) + width = 1; + if (height < 1) + height = 1; + + return new Integer[] { width, height }; + } + /** * Return the EXIF transformation flag of this image if any. *