X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fgui%2FImageUtils.java;h=d152ecca5b1c78549d6fcb7ea0f65878984a5436;hb=4d5796d02a15808311c09448d79e6e7d1de9d636;hp=4b46959f55c575d46fe0670851925fd91580cdf6;hpb=5625a1abadb5f2ca5f017fe7dbda1d5141cb637b;p=GpsPrune.git diff --git a/tim/prune/gui/ImageUtils.java b/tim/prune/gui/ImageUtils.java index 4b46959..d152ecc 100644 --- a/tim/prune/gui/ImageUtils.java +++ b/tim/prune/gui/ImageUtils.java @@ -2,6 +2,7 @@ package tim.prune.gui; import java.awt.Dimension; import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.ConvolveOp; @@ -69,18 +70,67 @@ public abstract class ImageUtils */ public static Dimension getThumbnailSize(int inOrigWidth, int inOrigHeight, int inMaxWidth, int inMaxHeight) { - if (inMaxWidth <= 0 || inMaxHeight <= 0) - { - //System.out.println("Can't do it - maxwidth=" + inMaxWidth + ", maxheight=" + inMaxHeight); - return new Dimension(0,0); - } + if (inMaxWidth <= 0 || inMaxHeight <= 0) {return new Dimension(1, 1);} // work out maximum zoom ratio available so that thumbnail isn't too big double xZoom = inMaxWidth * 1.0 / inOrigWidth; double yZoom = inMaxHeight * 1.0 / inOrigHeight; double zoom = (xZoom > yZoom?yZoom:xZoom); // Don't make thumbnail bigger than picture if (zoom > 1.0) {return new Dimension(inOrigWidth, inOrigHeight);} + // calculate new width and height - return new Dimension ((int) (zoom * inOrigWidth), (int) (zoom * inOrigHeight)); + final int xSize = (int) (zoom * inOrigWidth); + final int ySize = (int) (zoom * inOrigHeight); + if (xSize <= 0 || ySize <= 0) {return new Dimension(1, 1);} + return new Dimension (xSize, ySize); + } + + + /** + * Create a new image by rotating and scaling the given one + * @param inImage input image + * @param inMaxWidth maximum width of output image + * @param inMaxHeight maximum height of output image + * @param inRotationDegrees number of degrees to rotate clockwise (0, 90, 180 or 270) + * @return rotated, scaled image + */ + public static BufferedImage rotateImage(Image inImage, int inMaxWidth, int inMaxHeight, int inRotationDegrees) + { + // Create scaled image of suitable size + boolean isRotated = (inRotationDegrees % 180 != 0); + int origWidth = inImage.getWidth(null); + int origHeight = inImage.getHeight(null); + int thumbWidth = isRotated?origHeight:origWidth; + int thumbHeight = isRotated?origWidth:origHeight; + Dimension scaledSize = getThumbnailSize(thumbWidth, thumbHeight, inMaxWidth, inMaxHeight); + BufferedImage result = new BufferedImage(scaledSize.width, scaledSize.height, BufferedImage.TYPE_INT_RGB); + // Do different things according to rotation angle (a bit messy, sorry!) + if (inRotationDegrees == 0) + { + // Not rotated, so just copy image directly + result.getGraphics().drawImage(inImage, 0, 0, scaledSize.width, scaledSize.height, null); + } + else + { + // Need to use Graphics2D for rotation, not Graphics + Graphics2D g2d = result.createGraphics(); + switch (inRotationDegrees) + { + case 90: + g2d.rotate(Math.PI / 2, 0.0, 0.0); + g2d.drawImage(inImage, 0, -scaledSize.width, scaledSize.height, scaledSize.width, null); + break; + case 180: + g2d.rotate(Math.PI, scaledSize.width/2.0, scaledSize.height/2.0); + g2d.drawImage(inImage, 0, 0, scaledSize.width, scaledSize.height, null); + break; + case 270: + g2d.rotate(Math.PI * 3/2, 0.0, 0.0); + g2d.drawImage(inImage, -scaledSize.height, 0, scaledSize.height, scaledSize.width, null); + } + // Clear up memory + g2d.dispose(); + } + return result; } }