Skip to content

Commit

Permalink
Progressing #52
Browse files Browse the repository at this point in the history
  • Loading branch information
Alastair Carey committed Oct 9, 2022
1 parent 9ccde8a commit 1dc627d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/page_object_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,11 @@ impl<'a> PdfPageImageObject<'a> {
// We attempt to work around two separate problems in Pdfium's
// FPDFImageObj_GetRenderedBitmap() function.

// First, the call to FPDFImageObj_GetRenderedBitmap() can fail,
// returning a null FPDF_BITMAP handle, if the image object's transformation matrix
// includes negative values for either the matrix.a or matrix.d values.
// We flip those values in the transformation matrix if they are negative,
// and we make sure we return them to their original values before we return to
// the caller.
// First, the call to FPDFImageObj_GetRenderedBitmap() can fail, returning
// a null FPDF_BITMAP handle, if the image object's transformation matrix includes
// negative values for either the matrix.a or matrix.d values. We flip those values
// in the transformation matrix if they are negative, and we make sure we return them
// to their original values before we return to the caller.

// Second, Pdfium seems to often return a rendered bitmap that is much smaller
// than the image object's metadata suggests. We look at the dimensions of the bitmap
Expand All @@ -249,7 +248,7 @@ impl<'a> PdfPageImageObject<'a> {

let original_matrix = matrix; // We'll reset the matrix to this before we return.

// Flip the matrix.a and/or matrix.d values if they are negative.
// Ensure the matrix.a and matrix.d values are not negative.

if matrix.a < 0f32 {
matrix.a = -matrix.a;
Expand Down Expand Up @@ -284,7 +283,7 @@ impl<'a> PdfPageImageObject<'a> {

let result = result.unwrap();

return if width as u32 == result.width() && height as u32 == result.height() {
if width as u32 == result.width() && height as u32 == result.height() {
// The bitmap generated by Pdfium is already at the caller's requested dimensions.
// Restore the original transformation matrix values before we return to the caller.

Expand All @@ -293,8 +292,8 @@ impl<'a> PdfPageImageObject<'a> {
Ok(result)
} else {
// The bitmap generated by Pdfium is not at the caller's requested dimensions.
// We apply a scale transform to the page object to coerce Pdfium into generating
// a bitmap at the caller's requested dimensions.
// We apply a scale transform to the page object to encourage Pdfium to generate
// a bitmap matching the caller's requested dimensions.

self.transform_impl(
width as f64 / result.width() as f64,
Expand Down Expand Up @@ -325,7 +324,7 @@ impl<'a> PdfPageImageObject<'a> {
self.set_matrix(original_matrix)?;

result
};
}
}

pub(crate) fn get_image_from_bitmap_handle(
Expand Down
Binary file modified test/attachments-test.pdf
Binary file not shown.

0 comments on commit 1dc627d

Please sign in to comment.