# HG changeset patch
# User robcast
# Date 1052165948 -7200
# Node ID 78f52a1876fed55c9f46aa41577e42bcad8d36f3
# Parent cc6a0b9ac78ee2f01f1980bb12206ccc2e2406a2
digilib V1.9b1
- ongoing work with rotation (still doesn't work as I want)
diff -r cc6a0b9ac78e -r 78f52a1876fe servlet/src/digilib/image/DocuImage.java
--- 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 x
,y
by angle
+ * around the center by the angle
* 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 angle
* degrees. Currently only horizontal and vertical mirroring (0 and 90
- * degree) are supported.
+ * degree) are supported.
*
* @param angle angle of mirror axis
* @throws ImageOpException
diff -r cc6a0b9ac78e -r 78f52a1876fe servlet/src/digilib/image/ImageLoaderDocuImage.java
--- 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 {
diff -r cc6a0b9ac78e -r 78f52a1876fe servlet/src/digilib/image/JAIDocuImage.java
--- 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;
}
}