From e6963c87bb8ce115a24b92d744e532d53d209416 Mon Sep 17 00:00:00 2001 From: Hrvoje Stimac Date: Tue, 22 Mar 2016 12:50:46 +0100 Subject: [PATCH 1/6] Implemented new method for scaling image by height --- src/main/java/be/quodlibet/boxable/ImageCell.java | 2 +- src/main/java/be/quodlibet/boxable/image/Image.java | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/be/quodlibet/boxable/ImageCell.java b/src/main/java/be/quodlibet/boxable/ImageCell.java index 380c4ef4..f9d114ad 100644 --- a/src/main/java/be/quodlibet/boxable/ImageCell.java +++ b/src/main/java/be/quodlibet/boxable/ImageCell.java @@ -23,7 +23,7 @@ public class ImageCell extends Cell { } public void scaleToFit() { - img = img.scale(getInnerWidth()); + img = img.scaleByWidth(getInnerWidth()); } ImageCell(Row row, float width, Image image, boolean isCalculated, HorizontalAlignment align, diff --git a/src/main/java/be/quodlibet/boxable/image/Image.java b/src/main/java/be/quodlibet/boxable/image/Image.java index d5067ee9..0dbffcf1 100644 --- a/src/main/java/be/quodlibet/boxable/image/Image.java +++ b/src/main/java/be/quodlibet/boxable/image/Image.java @@ -50,11 +50,21 @@ public void draw(final PDDocument doc, final PDPageContentStream stream, float x * @param width Maximal width where {@link Image} needs to be scaled * @return Scaled {@link Image} */ - public Image scale(float width) { + public Image scaleByWidth(float width) { float factorWidth = width / this.width; return scale(width, this.height * factorWidth); } + /** + *

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); + } + /** *

Method which scale {@link Image} with designated width und height * @param width Maximal width where {@link Image} needs to be scaled From 4c54c152457a377b2b6f1da0dd1a9676905da3e4 Mon Sep 17 00:00:00 2001 From: Hrvoje Stimac Date: Fri, 4 Mar 2016 14:42:45 +0100 Subject: [PATCH 2/6] Added DPI argument in Image class --- .../be/quodlibet/boxable/image/Image.java | 73 ++++++++++++++----- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/src/main/java/be/quodlibet/boxable/image/Image.java b/src/main/java/be/quodlibet/boxable/image/Image.java index 0dbffcf1..90844ac5 100644 --- a/src/main/java/be/quodlibet/boxable/image/Image.java +++ b/src/main/java/be/quodlibet/boxable/image/Image.java @@ -2,7 +2,11 @@ import java.awt.Dimension; import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; +import java.io.InputStream; + +import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPageContentStream; @@ -14,61 +18,95 @@ public class Image { private final BufferedImage image; - + private float width; - + private float height; + private float[] dpi; + /** *

* Constructor for default images *

* * @param image + * @return + * @throws IOException */ public Image(final BufferedImage image) { this.image = image; width = image.getWidth(); 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; + width = image.getWidth(); + height = image.getHeight(); + this.dpi[0] = dpiX; + this.dpi[1] = dpiY; + } + /** - *

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 + *

+ * @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); } - + /** - *

Method which scale {@link Image} with designated height - * @param width 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); } - + /** - *

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 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) { @@ -87,5 +125,4 @@ public float getHeight() { public float getWidth() { return width; } - } From c5461b7bc360b95a6cafcfc37feff8d029ba5465 Mon Sep 17 00:00:00 2001 From: Hrvoje Stimac Date: Fri, 4 Mar 2016 16:45:07 +0100 Subject: [PATCH 3/6] added convertion methods from pixel to points --- .../be/quodlibet/boxable/image/Image.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/be/quodlibet/boxable/image/Image.java b/src/main/java/be/quodlibet/boxable/image/Image.java index 90844ac5..5471debe 100644 --- a/src/main/java/be/quodlibet/boxable/image/Image.java +++ b/src/main/java/be/quodlibet/boxable/image/Image.java @@ -23,7 +23,8 @@ public class Image { private float height; - private float[] dpi; + // standard DPI + private float[] dpi = {72,72}; /** *

@@ -36,8 +37,8 @@ public class Image { */ 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) { @@ -45,14 +46,11 @@ public Image(final BufferedImage image, float dpi) { } public Image(final BufferedImage image, float dpiX, float dpiY) { - this.image = image; - width = image.getWidth(); - height = image.getHeight(); + this(image); this.dpi[0] = dpiX; this.dpi[1] = dpiY; } - /** *

* Drawing simple {@link Image} in {@link PDPageContentStream}. @@ -85,11 +83,17 @@ public Image scaleByWidth(float width) { float factorWidth = width / this.width; return scale(width, this.height * factorWidth); } + + public Image scaleImageFromPixelToPoints() { + float dpiX = dpi[0]; + float dpiY = dpi[1]; + return scale(getImageWidthInPoints(dpiX),getImageHeightInPoints(dpiY)); + } /** *

* Method which scale {@link Image} with designated height - *

+ * * @param width * Maximal height where {@link Image} needs to be scaled * @return Scaled {@link Image} @@ -98,11 +102,19 @@ 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 From 3c8196fac9e6d2541a2ca52299983117a9ef202f Mon Sep 17 00:00:00 2001 From: Hrvoje Stimac Date: Mon, 7 Mar 2016 09:59:48 +0100 Subject: [PATCH 4/6] removed usage of Dimension for image scalation (using floats instead) --- .../be/quodlibet/boxable/image/Image.java | 24 +++++++--------- .../quodlibet/boxable/utils/ImageUtils.java | 28 +++++++++---------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/main/java/be/quodlibet/boxable/image/Image.java b/src/main/java/be/quodlibet/boxable/image/Image.java index 5471debe..51686904 100644 --- a/src/main/java/be/quodlibet/boxable/image/Image.java +++ b/src/main/java/be/quodlibet/boxable/image/Image.java @@ -1,12 +1,7 @@ package be.quodlibet.boxable.image; -import java.awt.Dimension; import java.awt.image.BufferedImage; -import java.io.File; import java.io.IOException; -import java.io.InputStream; - -import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPageContentStream; @@ -46,9 +41,12 @@ public Image(final BufferedImage image, float dpi) { } public Image(final BufferedImage image, float dpiX, float dpiY) { - this(image); + this.image = image; + this.width = image.getWidth(); + this.height = image.getHeight(); this.dpi[0] = dpiX; this.dpi[1] = dpiY; + scaleImageFromPixelToPoints(); } /** @@ -84,10 +82,10 @@ public Image scaleByWidth(float width) { return scale(width, this.height * factorWidth); } - public Image scaleImageFromPixelToPoints() { + private void scaleImageFromPixelToPoints() { float dpiX = dpi[0]; float dpiY = dpi[1]; - return scale(getImageWidthInPoints(dpiX),getImageHeightInPoints(dpiY)); + scale(getImageWidthInPoints(dpiX),getImageHeightInPoints(dpiY)); } /** @@ -121,12 +119,10 @@ public float getImageHeightInPoints(float dpiY) { * 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; } 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; } } From 10bdbc1c82a046550cebdce3ca4b8534adc6fae4 Mon Sep 17 00:00:00 2001 From: Hrvoje Stimac Date: Thu, 31 Mar 2016 12:55:21 +0200 Subject: [PATCH 5/6] making scale(float width) method as deprecated --- .../java/be/quodlibet/boxable/image/Image.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/be/quodlibet/boxable/image/Image.java b/src/main/java/be/quodlibet/boxable/image/Image.java index 51686904..14e2c3cb 100644 --- a/src/main/java/be/quodlibet/boxable/image/Image.java +++ b/src/main/java/be/quodlibet/boxable/image/Image.java @@ -8,6 +8,7 @@ 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 { @@ -69,6 +70,21 @@ public void draw(final PDDocument doc, final PDPageContentStream stream, float x stream.drawImage(imageXObject, x, y - height, width, height); } + + /** + *

+ * 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 From 72f59d87e75fac715fc9cb349f7956db06b88f7d Mon Sep 17 00:00:00 2001 From: Hrvoje Stimac Date: Thu, 31 Mar 2016 12:58:42 +0200 Subject: [PATCH 6/6] using deprecated method in scaleToFit() method to ensure API functionalty --- src/main/java/be/quodlibet/boxable/ImageCell.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/be/quodlibet/boxable/ImageCell.java b/src/main/java/be/quodlibet/boxable/ImageCell.java index f9d114ad..380c4ef4 100644 --- a/src/main/java/be/quodlibet/boxable/ImageCell.java +++ b/src/main/java/be/quodlibet/boxable/ImageCell.java @@ -23,7 +23,7 @@ public class ImageCell extends Cell { } public void scaleToFit() { - img = img.scaleByWidth(getInnerWidth()); + img = img.scale(getInnerWidth()); } ImageCell(Row row, float width, Image image, boolean isCalculated, HorizontalAlignment align,