Skip to content

Commit

Permalink
Merge branch 'SZUE-frulenzo/image-cell-fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
dhorions committed Apr 2, 2016
2 parents a1616f7 + b5b6fb5 commit f032471
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 37 deletions.
115 changes: 93 additions & 22 deletions src/main/java/be/quodlibet/boxable/image/Image.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package be.quodlibet.boxable.image;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;

Expand All @@ -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};

/**
* <p>
* Constructor for default images
* </p>
*
* @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();
}

/**
* <p> Drawing simple {@link Image} in {@link PDPageContentStream}. </p>
* @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
* <p>
* Drawing simple {@link Image} in {@link PDPageContentStream}.
* </p>
*
* @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);
}


/**
* <p> Method which scale {@link Image} with designated width
* @param width Maximal width where {@link Image} needs to be scaled
* <p>
* Method which scale {@link Image} with designated width
* </p>
* @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);
}


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

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

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

Expand All @@ -77,5 +149,4 @@ public float getHeight() {
public float getWidth() {
return width;
}

}
28 changes: 13 additions & 15 deletions src/main/java/be/quodlibet/boxable/utils/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit f032471

Please sign in to comment.