diff --git a/src/main/java/be/quodlibet/boxable/image/Image.java b/src/main/java/be/quodlibet/boxable/image/Image.java index d5067ee9..14e2c3cb 100644 --- a/src/main/java/be/quodlibet/boxable/image/Image.java +++ b/src/main/java/be/quodlibet/boxable/image/Image.java @@ -1,6 +1,5 @@ package be.quodlibet.boxable.image; -import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.IOException; @@ -9,64 +8,137 @@ import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; +import be.quodlibet.boxable.Row; import be.quodlibet.boxable.utils.ImageUtils; public class Image { private final BufferedImage image; - + private float width; - + private float height; + // standard DPI + private float[] dpi = {72,72}; + /** *

* Constructor for default images *

* * @param image + * @return + * @throws IOException */ public Image(final BufferedImage image) { this.image = image; - width = image.getWidth(); - height = image.getHeight(); + this.width = image.getWidth(); + this.height = image.getHeight(); + } + + public Image(final BufferedImage image, float dpi) { + this(image, dpi, dpi); + } + + public Image(final BufferedImage image, float dpiX, float dpiY) { + this.image = image; + this.width = image.getWidth(); + this.height = image.getHeight(); + this.dpi[0] = dpiX; + this.dpi[1] = dpiY; + scaleImageFromPixelToPoints(); } /** - *

Drawing simple {@link Image} in {@link PDPageContentStream}.

- * @param doc {@link PDDocument} where drawing will be applied - * @param stream {@link PDPageContentStream} where drawing will be applied - * @param x X coordinate for image drawing - * @param y Y coordinate for image drawing + *

+ * Drawing simple {@link Image} in {@link PDPageContentStream}. + *

+ * + * @param doc + * {@link PDDocument} where drawing will be applied + * @param stream + * {@link PDPageContentStream} where drawing will be applied + * @param x + * X coordinate for image drawing + * @param y + * Y coordinate for image drawing * @throws IOException */ public void draw(final PDDocument doc, final PDPageContentStream stream, float x, float y) throws IOException { PDImageXObject imageXObject = LosslessFactory.createFromImage(doc, image); stream.drawImage(imageXObject, x, y - height, width, height); } + /** - *

Method which scale {@link Image} with designated width - * @param width Maximal width where {@link Image} needs to be scaled + *

+ * Method which scale {@link Image} with designated width + *

+ * @deprecated Use {@link #scaleByWidth(width)} + * @param width + * Maximal height where {@link Image} needs to be scaled * @return Scaled {@link Image} */ public Image scale(float width) { + return scaleByWidth(width); + } + + + /** + *

+ * Method which scale {@link Image} with designated width + *

+ * @param width + * Maximal width where {@link Image} needs to be scaled + * @return Scaled {@link Image} + */ + public Image scaleByWidth(float width) { float factorWidth = width / this.width; return scale(width, this.height * factorWidth); } + private void scaleImageFromPixelToPoints() { + float dpiX = dpi[0]; + float dpiY = dpi[1]; + scale(getImageWidthInPoints(dpiX),getImageHeightInPoints(dpiY)); + } + /** - *

Method which scale {@link Image} with designated width und height - * @param width Maximal width where {@link Image} needs to be scaled - * @param height Maximal height where {@link Image} needs to be scaled + *

+ * Method which scale {@link Image} with designated height + * + * @param width + * Maximal height where {@link Image} needs to be scaled + * @return Scaled {@link Image} + */ + public Image scaleByHeight(float height) { + float factorHeight = height / this.height; + return scale(this.width * factorHeight, height); + } + + public float getImageWidthInPoints(float dpiX) { + return this.width*72f/dpiX; + } + + public float getImageHeightInPoints(float dpiY) { + return this.height*72f/dpiY; + } + + /** + *

+ * Method which scale {@link Image} with designated width und height + * + * @param width + * Maximal width where {@link Image} needs to be scaled + * @param height + * Maximal height where {@link Image} needs to be scaled * @return */ - public Image scale(float width, float height) { - Dimension imageDim = new Dimension((int) image.getWidth(), (int) image.getHeight()); - Dimension newImageDim = new Dimension((int) width, (int) height); - Dimension scaledImageDim = ImageUtils.getScaledDimension(imageDim, newImageDim); - this.width = scaledImageDim.width; - this.height = scaledImageDim.height; + public Image scale(float boundWidth, float boundHeight) { + float[] imageDimension = ImageUtils.getScaledDimension(this.width, this.height, boundWidth, boundHeight); + this.width = imageDimension[0]; + this.height = imageDimension[1]; return this; } @@ -77,5 +149,4 @@ public float getHeight() { public float getWidth() { return width; } - } diff --git a/src/main/java/be/quodlibet/boxable/utils/ImageUtils.java b/src/main/java/be/quodlibet/boxable/utils/ImageUtils.java index 11cdaaf6..23b787da 100644 --- a/src/main/java/be/quodlibet/boxable/utils/ImageUtils.java +++ b/src/main/java/be/quodlibet/boxable/utils/ImageUtils.java @@ -50,27 +50,25 @@ public static Image readImage(File imageFile) throws IOException { * @return Appropriate scaled image {@link Dimension} based on boundary * {@link Dimension} */ - public static Dimension getScaledDimension(Dimension imgDim, Dimension boundary) { - int imgWidth = imgDim.width; - int imgHeight = imgDim.height; - int boundWidth = boundary.width; - int boundHeight = boundary.height; - int newImgWidth = imgWidth; - int newImgHeight = imgHeight; - + public static float[] getScaledDimension(float imageWidth, float imageHeight, float boundWidth, float boundHeight) { + float newImageWidth = imageWidth; + float newImageHeight = imageHeight; + // first check if we need to scale width - if (imgWidth > boundWidth) { - newImgWidth = boundWidth; + if (imageWidth > boundWidth) { + newImageWidth = boundWidth; // scale height to maintain aspect ratio - newImgHeight = (newImgWidth * imgHeight) / imgWidth; + newImageHeight = (newImageWidth * imageHeight) / imageWidth; } // then check if the new height is also bigger than expected - if (newImgHeight > boundHeight) { - newImgHeight = boundHeight; + if (newImageHeight > boundHeight) { + newImageHeight = boundHeight; // scale width to maintain aspect ratio - newImgWidth = (newImgHeight * imgWidth) / imgHeight; + newImageWidth = (newImageHeight * imageWidth) / imageHeight; } - return new Dimension(newImgWidth, newImgHeight); + + float[] imageDimension = {newImageWidth, newImageHeight}; + return imageDimension; } }