Skip to content

Commit

Permalink
close #382 Leniently handle high dpi causing maximum image dimensions…
Browse files Browse the repository at this point in the history
… to be exceeded
  • Loading branch information
ediweissmann committed Jul 27, 2020
1 parent 008f080 commit e0e0824
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,18 @@ public void noPages() throws IOException {
execute(parameters);
testContext.assertTaskFailed("No pages converted");
}

@Test
public void dpiExceedsJavaMaxBufferedImageSize() throws IOException {
PdfToJpegParameters parameters = new PdfToJpegParameters(ImageColorType.COLOR_RGB);
parameters.setResolutionInDpi(6000);
parameters.addPageRange(PageRange.one(1));
parameters.setLenient(true);
parameters.addSource(shortInput());
testContext.directoryOutputTo(parameters);

execute(parameters);
testContext.assertTaskCompleted();
testContext.assertTaskWarning("Maximum image dimensions exceeded on page 1, decreased resolution to 116 dpi");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.sejda.model.input.PdfSourceOpener;
import org.sejda.model.parameter.image.AbstractPdfToMultipleImageParameters;
import org.sejda.model.task.TaskExecutionContext;
import org.sejda.sambox.pdmodel.PDPage;
import org.sejda.sambox.pdmodel.common.PDRectangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -98,8 +100,34 @@ public void execute(T parameters) throws TaskException {

try {
LOG.trace("Converting page {}", currentPage);
int dpi = parameters.getResolutionInDpi();
float scale = dpi / 72f;

// check if the page at the specified dpi resolution does not exceed
// max java buffered image dimensions
PDPage page = documentHandler.getPage(currentPage);

PDRectangle cropbBox = page.getCropBox();
float widthPt = cropbBox.getWidth();
float heightPt = cropbBox.getHeight();

int widthPx = (int) Math.max(Math.floor(widthPt * scale), 1);
int heightPx = (int) Math.max(Math.floor(heightPt * scale), 1);

// the maximum size (w*h) of a buffered image is limited to Integer.MAX_VALUE
if ((long) widthPx * (long) heightPx > Integer.MAX_VALUE) {
float decreaseRatio = widthPt * heightPt * scale / Integer.MAX_VALUE;
dpi = (int) Math.round(Math.floor(dpi * decreaseRatio));
Exception e = new RuntimeException(
String.format("Maximum image dimensions exceeded on page %d", currentPage));
executionContext().assertTaskIsLenient(e);
notifyEvent(executionContext().notifiableTaskMetadata()).taskWarning(
String.format("Maximum image dimensions exceeded on page %d, decreased resolution to %d dpi",
currentPage, dpi), e);
}

BufferedImage pageImage = documentHandler.renderImage(currentPage,
parameters.getResolutionInDpi(), parameters.getOutputImageColorType());
dpi, parameters.getOutputImageColorType());

getWriter().openDestination(tmpFile, parameters);
getWriter().write(pageImage, parameters);
Expand Down

0 comments on commit e0e0824

Please sign in to comment.