Skip to content

Commit

Permalink
Allows for bigger boxes on popup pictures
Browse files Browse the repository at this point in the history
Also refactors the scaling of the subimage into a new method

Builds towards #25
  • Loading branch information
kjlubick committed Mar 14, 2015
1 parent aa9a233 commit 2336aa6
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions src/main/java/edu/ncsu/dlf/model/Pdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -138,7 +139,13 @@ private BufferedImage makePopupSubImage(BufferedImage img, PDRectangle r) {
}

private enum PostExtractMarkup {
NONE, HIGHLIGHTS, POPUP
NONE(1), HIGHLIGHTS(1), POPUP(2);

public final float subImageContextMultiplier;

PostExtractMarkup(float subImageContextMultiplier) {
this.subImageContextMultiplier = subImageContextMultiplier;
}
}

/* adapted the specs of a pdf tool http://www.pdf-technologies.com/api/html/P_PDFTech_PDFMarkupAnnotation_QuadPoints.htm
Expand All @@ -154,35 +161,25 @@ private BufferedImage makeSubImage(BufferedImage img, float[] quadPoints, PostEx
if (quadPoints.length < 8) {
return null;
}

//we find the upper left corner
int minX = getMinXFromQuadPoints(quadPoints);
int minY = getMinYFromQuadPoints(quadPoints);

int x = Math.round((minX-BORDER_WIDTH) * SCALE_UP_FACTOR);
int y = Math.round((minY-BORDER_WIDTH) * SCALE_UP_FACTOR);

int width = Math.round((getMaxXFromQuadPoints(quadPoints) - minX + 2* BORDER_WIDTH) * SCALE_UP_FACTOR);
width = Math.min(width, img.getWidth() - x); //clamp width
int height = Math.round((getMaxYFromQuadPoints(quadPoints) - minY + 2* BORDER_WIDTH) * SCALE_UP_FACTOR);
height = Math.min(height, img.getHeight() - y); //clamp height

BufferedImage subImage = img.getSubimage(x, (img.getHeight() - y - height), width, height);
Rectangle subImageRect = scaleAndTransformSubImageQuad(quadPoints, img, markup);

BufferedImage subImage = img.getSubimage(subImageRect.x, subImageRect.y, subImageRect.width, subImageRect.height);

BufferedImage newImage = new BufferedImage(subImage.getWidth(), subImage.getHeight(), img.getType());
Graphics2D g2 = newImage.createGraphics();
// the y is counted from the bottom, so we have to flip our coordinate

g2.drawImage(subImage, 0, 0, null);

if (markup == PostExtractMarkup.HIGHLIGHTS) {
for (int n = 0; n < quadPoints.length; n += 8) {
float[] oneQuad = Arrays.copyOfRange(quadPoints, n, n + 8);

paintHighlight(g2, scaleAndTransformQuad(oneQuad, x, y, height));
paintHighlight(g2, scaleAndTransformAnnotationQuad(oneQuad, subImageRect, img.getHeight()));
}
} else if (markup == PostExtractMarkup.POPUP) {
//we know quadPoints will be only one quad because that's how makePopupSubImage defines it.
paintCommentBox(g2, scaleAndTransformQuad(quadPoints, x, y, height));
paintCommentBox(g2, scaleAndTransformAnnotationQuad(quadPoints, subImageRect, img.getHeight()));
}

g2.dispose();
Expand All @@ -202,7 +199,30 @@ private BufferedImage makeSubImage(BufferedImage img, float[] quadPoints, PostEx
return newImage;
}

private Rectangle scaleAndTransformQuad(float[] oneQuad, int xOffset, int yOffset, int imageHeight) {
private Rectangle scaleAndTransformSubImageQuad(float[] quadPoints, RenderedImage img, PostExtractMarkup markup) {
// we find the upper left corner
int minX = getMinXFromQuadPoints(quadPoints);
int minY = getMinYFromQuadPoints(quadPoints);

// allows us to make
float scaledBorder = BORDER_WIDTH * markup.subImageContextMultiplier;

int x = Math.round((minX - scaledBorder) * SCALE_UP_FACTOR);
x = Math.max(x, 0); // keep subimage on screen
int y = Math.round((minY - scaledBorder) * SCALE_UP_FACTOR);
y = Math.max(y, 0); // keep subimage on screen

int width = Math.round((getMaxXFromQuadPoints(quadPoints) - minX + 2 * scaledBorder) * SCALE_UP_FACTOR);
width = Math.min(width, img.getWidth() - x); // clamp width
int height = Math.round((getMaxYFromQuadPoints(quadPoints) - minY + 2 * scaledBorder) * SCALE_UP_FACTOR);
height = Math.min(height, img.getHeight() - y); // clamp height

// the y is counted from the bottom, so we have to flip our coordinate
y = (img.getHeight() - y - height);
return new Rectangle(x, y, width, height);
}

private Rectangle scaleAndTransformAnnotationQuad(float[] oneQuad, Rectangle boundingRect, int imageHeight) {
int x = getMinXFromQuadPoints(oneQuad);
int y = getMinYFromQuadPoints(oneQuad);

Expand All @@ -212,10 +232,9 @@ private Rectangle scaleAndTransformQuad(float[] oneQuad, int xOffset, int yOffse
x *= SCALE_UP_FACTOR;
y *= SCALE_UP_FACTOR;

x -= xOffset;
y -= yOffset;
// again, invert the y axis
y = imageHeight - y - height;
x -= boundingRect.x;
// invert y again
y = imageHeight - y - boundingRect.y - height;

return new Rectangle(x, y, width, height);
}
Expand Down Expand Up @@ -347,9 +366,9 @@ public void close() throws IOException {
}

@SuppressWarnings("unused")
private static void main(String[] args) throws Exception{
public static void main(String[] args) throws Exception{
FileInputStream fos = new FileInputStream("test.pdf");
Pdf pdf = new Pdf(fos, new FileInputStream("src/main/resources/comment_box.PNG"));
Pdf pdf = new Pdf(fos, new FileInputStream("src/main/webapp/images/comment_box.PNG"));

System.out.println(pdf.getPDFComments());
}
Expand Down

0 comments on commit 2336aa6

Please sign in to comment.