diff --git a/src/org/forkalsrud/album/web/PictureScaler.java b/src/org/forkalsrud/album/web/PictureScaler.java index d8a0bc0..dbeed20 100644 --- a/src/org/forkalsrud/album/web/PictureScaler.java +++ b/src/org/forkalsrud/album/web/PictureScaler.java @@ -172,19 +172,41 @@ public class PictureScaler { reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); BufferedImage img = reader.read(0, param); - + + // The orientation is about flipping and rotating. Here is what an 'F' looks like + // on pictures with each orientation. + // + // 1 2 3 4 5 6 7 8 + // + // 888888 888888 88 88 8888888888 88 88 8888888888 + // 88 88 88 88 88 88 88 88 88 88 88 88 + // 8888 8888 8888 8888 88 8888888888 8888888888 88 + // 88 88 88 88 + // 88 88 888888 888888 + // + // The first four are obtained with only flipping X and/or Y + // The last four are obtained by rotating 90 degrees and then flipping X and/or Y. + + double[] flipX = new double[] { +1, -1, -1, +1, -1, +1, +1, -1 }; + double[] flipY = new double[] { +1, +1, -1, -1, +1, +1, -1, -1 }; + int idx = thumbnail.getOrientation() - 1; + // Recalculate scale after sub-sampling was applied double scale; - AffineTransform xform; - if (thumbnail.getOrientation() == 6) { - xform = AffineTransform.getTranslateInstance(intermediate.getWidth() / 2d, intermediate.getHeight() / 2d); + AffineTransform xform = new AffineTransform(); + boolean rotate = idx >= 4; + + if (rotate) { + xform.translate(intermediate.getWidth() / 2d, intermediate.getHeight() / 2d); xform.rotate(Math.PI / 2); - scale = (double)intermediate.getHeight() / img.getWidth(); - xform.scale(scale, scale); + scale = (double)intermediate.getHeight() / img.getWidth(); + } else { + scale = (double)intermediate.getWidth() / img.getWidth(); + } + xform.scale(scale, scale); + xform.scale(flipX[idx], flipY[idx]); + if (rotate) { xform.translate(-img.getWidth() / 2d, -img.getHeight() / 2d); - } else { - scale = (double)intermediate.getWidth() / img.getWidth(); - xform = AffineTransform.getScaleInstance(scale, scale); } int imgType = img.getType();