minor fix: fixed rotation to be exactly 0 when user chooses: set roation to none, in advanced: rotate 0 and variance 0

This commit is contained in:
Balázs Szücs 2025-05-17 18:18:32 +02:00
parent e2d01f0fea
commit 283fec6ea3
2 changed files with 53 additions and 41 deletions

View File

@ -168,51 +168,62 @@ public class FakeScanController {
if (baseRotation != 0 || rotateVariance != 0) { if (baseRotation != 0 || rotateVariance != 0) {
pageRotation += (RANDOM.nextDouble() * 2 - 1) * rotateVariance; pageRotation += (RANDOM.nextDouble() * 2 - 1) * rotateVariance;
} }
double radians = Math.toRadians(pageRotation);
double sin = Math.abs(Math.sin(radians)); BufferedImage rotated;
double cos = Math.abs(Math.cos(radians));
int w = composed.getWidth(); int w = composed.getWidth();
int h = composed.getHeight(); int h = composed.getHeight();
int rotW = (int) Math.floor(w * cos + h * sin); int rotW = w;
int rotH = (int) Math.floor(h * cos + w * sin); int rotH = h;
BufferedImage rotatedBg = new BufferedImage(rotW, rotH, composed.getType());
Graphics2D gBgRot = rotatedBg.createGraphics(); // Skip rotation entirely if no rotation is needed
for (int y = 0; y < rotH; y++) { if (pageRotation == 0) {
for (int x = 0; x < rotW; x++) { rotated = composed;
float frac = vertical ? (float) y / (rotH - 1) : (float) x / (rotW - 1); } else {
int r = double radians = Math.toRadians(pageRotation);
Math.round( double sin = Math.abs(Math.sin(radians));
startColor.getRed() double cos = Math.abs(Math.cos(radians));
+ (endColor.getRed() - startColor.getRed()) * frac); rotW = (int) Math.floor(w * cos + h * sin);
int g = rotH = (int) Math.floor(h * cos + w * sin);
Math.round( BufferedImage rotatedBg = new BufferedImage(rotW, rotH, composed.getType());
startColor.getGreen() Graphics2D gBgRot = rotatedBg.createGraphics();
+ (endColor.getGreen() - startColor.getGreen()) for (int y = 0; y < rotH; y++) {
* frac); for (int x = 0; x < rotW; x++) {
int b = float frac = vertical ? (float) y / (rotH - 1) : (float) x / (rotW - 1);
Math.round( int r =
startColor.getBlue() Math.round(
+ (endColor.getBlue() - startColor.getBlue()) startColor.getRed()
* frac); + (endColor.getRed() - startColor.getRed())
rotatedBg.setRGB(x, y, new Color(r, g, b).getRGB()); * frac);
int g =
Math.round(
startColor.getGreen()
+ (endColor.getGreen() - startColor.getGreen())
* frac);
int b =
Math.round(
startColor.getBlue()
+ (endColor.getBlue() - startColor.getBlue())
* frac);
rotatedBg.setRGB(x, y, new Color(r, g, b).getRGB());
}
} }
gBgRot.dispose();
rotated = new BufferedImage(rotW, rotH, composed.getType());
Graphics2D g2d = rotated.createGraphics();
g2d.drawImage(rotatedBg, 0, 0, null);
AffineTransform at = new AffineTransform();
at.translate((rotW - w) / 2.0, (rotH - h) / 2.0);
at.rotate(radians, w / 2.0, h / 2.0);
g2d.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(
RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(composed, at, null);
g2d.dispose();
} }
gBgRot.dispose();
BufferedImage rotated = new BufferedImage(rotW, rotH, composed.getType());
Graphics2D g2d = rotated.createGraphics();
g2d.drawImage(rotatedBg, 0, 0, null);
AffineTransform at = new AffineTransform();
at.translate((rotW - w) / 2.0, (rotH - h) / 2.0);
at.rotate(radians, w / 2.0, h / 2.0);
g2d.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(
RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(composed, at, null);
g2d.dispose();
// 4. Scale and center the rotated image to cover the original page size // 4. Scale and center the rotated image to cover the original page size
PDRectangle origPageSize = document.getPage(i).getMediaBox(); PDRectangle origPageSize = document.getPage(i).getMediaBox();

View File

@ -85,6 +85,7 @@ public class FakeScanRequest {
public int getRotationValue() { public int getRotationValue() {
return switch (rotation.toLowerCase()) { return switch (rotation.toLowerCase()) {
case "none" -> 0;
case "slight" -> 2; case "slight" -> 2;
case "moderate" -> 5; case "moderate" -> 5;
case "severe" -> 8; case "severe" -> 8;