changeset 101:78f52a1876fe

digilib V1.9b1 - ongoing work with rotation (still doesn't work as I want)
author robcast
date Mon, 05 May 2003 22:19:08 +0200
parents cc6a0b9ac78e
children 5fdf109d31c2
files servlet/src/digilib/image/DocuImage.java servlet/src/digilib/image/ImageLoaderDocuImage.java servlet/src/digilib/image/JAIDocuImage.java
diffstat 3 files changed, 74 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/image/DocuImage.java	Mon May 05 22:18:37 2003 +0200
+++ b/servlet/src/digilib/image/DocuImage.java	Mon May 05 22:19:08 2003 +0200
@@ -161,7 +161,7 @@
 	/** Rotates the current image.
 	 * 
 	 * Replaces the current image with a rotated image. The image is rotated
-	 * around the point <code>x</code>,<code>y</code> by <code>angle</code> 
+	 * around the center by the <code>angle</code> 
 	 * given in degrees [0, 360] clockwise.
 	 * Image size and aspect ratio are likely to change.
 	 * 
@@ -174,7 +174,7 @@
 	 * Replaces  the current image with a mirrored image. The mirror axis goes
 	 * through the center of the image and is rotated by <code>angle</code>
 	 * degrees. Currently only horizontal and vertical mirroring (0 and 90
-	 * degree) are supported.
+	 * degree) are supported. 
 	 * 
 	 * @param angle angle of mirror axis
 	 * @throws ImageOpException
--- a/servlet/src/digilib/image/ImageLoaderDocuImage.java	Mon May 05 22:18:37 2003 +0200
+++ b/servlet/src/digilib/image/ImageLoaderDocuImage.java	Mon May 05 22:19:08 2003 +0200
@@ -23,6 +23,7 @@
 import java.awt.Dimension;
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
 import java.awt.image.AffineTransformOp;
 import java.awt.image.BufferedImage;
 import java.awt.image.RescaleOp;
@@ -278,22 +279,39 @@
 		return fb;
 	}
 
-	public void rotate(double angle) throws ImageOpException {
+	public void rotate(double angle)
+		throws ImageOpException {
 		// setup rotation
 		double rangle = Math.toRadians(angle);
-		double x = getWidth() / 2;
-		double y = getHeight() / 2;
-		AffineTransformOp rotOp =
-			new AffineTransformOp(
-				AffineTransform.getRotateInstance(rangle, x, y),
-				interpol);
+		// create offset to make shure the rotated image has no negative coordinates
+		double w = img.getWidth();
+		double h = img.getHeight();
+		double xoff = (w / 2) * Math.sin(rangle);
+		double yoff = (h / 2) * Math.sin(rangle);
+		double off = Math.max(xoff, yoff)+10;
+		// move image
+		AffineTransform trafo = AffineTransform.getTranslateInstance(off, off);
+		// new center of rotation 
+		double x = (w / 2) + off;
+		double y = (h / 2) + off;
+		trafo.rotate(rangle, x, y);
+		// transform image
+		AffineTransformOp rotOp = new AffineTransformOp(trafo, interpol);
 		BufferedImage rotImg = rotOp.filter(img, null);
+		// calculate new bounding box
+		Rectangle2D bounds = rotOp.getBounds2D(img);
 
 		if (rotImg == null) {
 			util.dprintln(2, "ERROR: error in rotate");
 			throw new ImageOpException("Unable to rotate");
 		}
-		img = rotImg;
+		// crop new image (with self-made rounding)
+		img =
+			rotImg.getSubimage(
+				(int) (bounds.getX()+0.5),
+				(int) (bounds.getY()+0.5),
+				(int) (bounds.getWidth()+0.5),
+				(int) (bounds.getHeight()+0.5));
 	}
 
 	public void mirror(double angle) throws ImageOpException {
--- a/servlet/src/digilib/image/JAIDocuImage.java	Mon May 05 22:18:37 2003 +0200
+++ b/servlet/src/digilib/image/JAIDocuImage.java	Mon May 05 22:19:08 2003 +0200
@@ -126,7 +126,7 @@
 
 	/* scales the current image */
 	public void scale(double scale) throws ImageOpException {
-		float sf = (float)scale;
+		float sf = (float) scale;
 		// setup scale
 		ParameterBlock param = new ParameterBlock();
 		param.addSource(img);
@@ -200,8 +200,8 @@
 		RenderedImage rotImg;
 		// convert degrees to radians
 		double rangle = Math.toRadians(angle);
-		float x = getWidth()/2;
-		float y = getHeight()/2;
+		double x = img.getWidth() / 2;
+		double y = img.getHeight() / 2;
 
 		// optimize rotation by right angles
 		TransposeType rotOp = null;
@@ -231,17 +231,12 @@
 			// setup "normal" rotation
 			ParameterBlock param = new ParameterBlock();
 			param.addSource(img);
-			param.add(x);
-			param.add(y);
+			param.add((float) x);
+			param.add((float) y);
 			param.add((float) rangle);
 			param.add(interpol);
-			// hint with border extender
-			RenderingHints hint =
-				new RenderingHints(
-					JAI.KEY_BORDER_EXTENDER,
-					BorderExtender.createInstance(BorderExtender.BORDER_COPY));
 
-			rotImg = JAI.create("rotate", param, hint);
+			rotImg = JAI.create("rotate", param);
 		}
 
 		util.dprintln(
@@ -273,10 +268,9 @@
 	 */
 	public void mirror(double angle) throws ImageOpException {
 		RenderedImage mirImg;
-
 		// only mirroring by right angles
 		TransposeType rotOp = null;
-		if (Math.abs(angle - 0) < epsilon) {
+		if (Math.abs(angle) < epsilon) {
 			// 0 degree
 			rotOp = TransposeDescriptor.FLIP_HORIZONTAL;
 		} else if (Math.abs(angle - 90) < epsilon) {
@@ -292,29 +286,17 @@
 			// 360 degree
 			rotOp = TransposeDescriptor.FLIP_HORIZONTAL;
 		}
-		if (rotOp != null) {
-			// use Transpose operation
-			ParameterBlock param = new ParameterBlock();
-			param.addSource(img);
-			param.add(rotOp);
-			mirImg = JAI.create("transpose", param);
+		// use Transpose operation
+		ParameterBlock param = new ParameterBlock();
+		param.addSource(img);
+		param.add(rotOp);
+		mirImg = JAI.create("transpose", param);
 
-			util.dprintln(
-				3,
-				"MIRROR: "
-					+ angle
-					+ " ->"
-					+ mirImg.getWidth()
-					+ "x"
-					+ mirImg.getHeight());
-			//DEBUG
-
-			if (mirImg == null) {
-				util.dprintln(2, "ERROR(mirror): error in mirror");
-				throw new ImageOpException("Unable to mirror");
-			}
-			img = mirImg;
+		if (mirImg == null) {
+			util.dprintln(2, "ERROR(mirror): error in flip");
+			throw new ImageOpException("Unable to flip");
 		}
+		img = mirImg;
 	}
 
 	/* contrast and brightness enhancement */
@@ -348,44 +330,43 @@
 		img = enhImg;
 	}
 
-
 	/* (non-Javadoc)
 	 * @see digilib.image.DocuImage#enhanceRGB(float[], float[])
 	 */
 	public void enhanceRGB(float[] rgbm, float[] rgba)
 		throws ImageOpException {
-			RenderedImage enhImg;
-			int nb = rgbm.length;
-			double[] ma = new double[nb];
-			double[] aa = new double[nb];
-			for (int i = 0; i < nb; i++) {
-				ma[i] = rgbm[i];
-				aa[i] = rgba[i];
-			}
-			// use Rescale operation
-			ParameterBlock param = new ParameterBlock();
-			param.addSource(img);
-			param.add(ma);
-			param.add(aa);
-			enhImg = JAI.create("rescale", param);
+		RenderedImage enhImg;
+		int nb = rgbm.length;
+		double[] ma = new double[nb];
+		double[] aa = new double[nb];
+		for (int i = 0; i < nb; i++) {
+			ma[i] = rgbm[i];
+			aa[i] = rgba[i];
+		}
+		// use Rescale operation
+		ParameterBlock param = new ParameterBlock();
+		param.addSource(img);
+		param.add(ma);
+		param.add(aa);
+		enhImg = JAI.create("rescale", param);
 
-			util.dprintln(
-				3,
-				"ENHANCE_RGB: *"
-					+ rgbm
-					+ ", +"
-					+ rgba
-					+ " ->"
-					+ enhImg.getWidth()
-					+ "x"
-					+ enhImg.getHeight());
-			//DEBUG
+		util.dprintln(
+			3,
+			"ENHANCE_RGB: *"
+				+ rgbm
+				+ ", +"
+				+ rgba
+				+ " ->"
+				+ enhImg.getWidth()
+				+ "x"
+				+ enhImg.getHeight());
+		//DEBUG
 
-			if (enhImg == null) {
-				util.dprintln(2, "ERROR(enhance): error in enhanceRGB");
-				throw new ImageOpException("Unable to enhanceRGB");
-			}
-			img = enhImg;
+		if (enhImg == null) {
+			util.dprintln(2, "ERROR(enhance): error in enhanceRGB");
+			throw new ImageOpException("Unable to enhanceRGB");
+		}
+		img = enhImg;
 	}
 
 }