From 60bde10736fd78a2333cf1513aea779df9346b35 Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Wed, 26 Jul 2017 13:50:12 -0400 Subject: Fix FPDF_RenderPageBitmapWithMatrix This CL fixes FPDF_RenderPageBitmapWithMatrix and improves tests. Bug: pdfium:837 Change-Id: I98f90b667cc9a50fb0e915b8a758603488b44d40 Reviewed-on: https://pdfium-review.googlesource.com/9010 Reviewed-by: Lei Zhang Reviewed-by: dsinclair Commit-Queue: dsinclair --- fpdfsdk/fpdfview.cpp | 18 ++++++++------- fpdfsdk/fpdfview_embeddertest.cpp | 42 +++++++++++++++++++++++++++-------- testing/resources/rectangles.in | 38 ++++++++++++++++++++++++++++++++ testing/resources/rectangles.pdf | 46 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 17 deletions(-) create mode 100644 testing/resources/rectangles.in create mode 100644 testing/resources/rectangles.pdf diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index 956e6db671..c51ee90fe0 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -967,12 +967,6 @@ DLLEXPORT void STDCALL FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap, CFX_RetainPtr pBitmap(CFXBitmapFromFPDFBitmap(bitmap)); pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false); - CFX_Matrix transform_matrix = pPage->GetPageMatrix(); - if (matrix) { - transform_matrix.Concat(CFX_Matrix(matrix->a, matrix->b, matrix->c, - matrix->d, matrix->e, matrix->f)); - } - CFX_FloatRect clipping_rect; if (clipping) { clipping_rect.left = clipping->left; @@ -980,8 +974,16 @@ DLLEXPORT void STDCALL FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap, clipping_rect.right = clipping->right; clipping_rect.top = clipping->top; } - RenderPageImpl(pContext, pPage, transform_matrix, clipping_rect.ToFxRect(), - flags, true, nullptr); + FX_RECT clip_rect = clipping_rect.ToFxRect(); + + CFX_Matrix transform_matrix = pPage->GetDisplayMatrix( + clip_rect.left, clip_rect.top, clip_rect.Width(), clip_rect.Height(), 0); + if (matrix) { + transform_matrix.Concat(CFX_Matrix(matrix->a, matrix->b, matrix->c, + matrix->d, matrix->e, matrix->f)); + } + RenderPageImpl(pContext, pPage, transform_matrix, clip_rect, flags, true, + nullptr); pPage->SetRenderContext(nullptr); } diff --git a/fpdfsdk/fpdfview_embeddertest.cpp b/fpdfsdk/fpdfview_embeddertest.cpp index f36edbc4d6..a986a1cd96 100644 --- a/fpdfsdk/fpdfview_embeddertest.cpp +++ b/fpdfsdk/fpdfview_embeddertest.cpp @@ -330,19 +330,21 @@ TEST_F(FPDFViewEmbeddertest, Hang_360) { } TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) { - const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; - const char kTopLeftQuarterBlackMd5sum[] = "24e4d1ec06fa0258af758cfc8b2ad50a"; + const char kOriginalMD5[] = "210157942bcce97b057a1107a1fd62f8"; + const char kTopLeftQuarterMD5[] = "c54d58dda13e3cd04eb63e1d0db0feda"; + const char kTrimmedMD5[] = "88225d7951a21d0eef191cfed06c36ce"; + const char kRotatedMD5[] = "7d38bc58aa50ad271bc432e77256d3de"; - EXPECT_TRUE(OpenDocument("black.pdf")); + EXPECT_TRUE(OpenDocument("rectangles.pdf")); FPDF_PAGE page = LoadPage(0); EXPECT_NE(nullptr, page); const int width = static_cast(FPDF_GetPageWidth(page)); const int height = static_cast(FPDF_GetPageHeight(page)); - EXPECT_EQ(612, width); - EXPECT_EQ(792, height); + EXPECT_EQ(200, width); + EXPECT_EQ(200, height); FPDF_BITMAP bitmap = RenderPage(page); - CompareBitmap(bitmap, width, height, kAllBlackMd5sum); + CompareBitmap(bitmap, width, height, kOriginalMD5); FPDFBitmap_Destroy(bitmap); // Try rendering with an identity matrix. The output should be the same as @@ -364,17 +366,39 @@ TEST_F(FPDFViewEmbeddertest, FPDF_RenderPageBitmapWithMatrix) { bitmap = FPDFBitmap_Create(width, height, 0); FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); - CompareBitmap(bitmap, width, height, kAllBlackMd5sum); + CompareBitmap(bitmap, width, height, kOriginalMD5); FPDFBitmap_Destroy(bitmap); - // Now render again with the image scaled. + // Now render again with the image scaled smaller. matrix.a = 0.5; matrix.d = 0.5; bitmap = FPDFBitmap_Create(width, height, 0); FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); - CompareBitmap(bitmap, width, height, kTopLeftQuarterBlackMd5sum); + CompareBitmap(bitmap, width, height, kTopLeftQuarterMD5); + FPDFBitmap_Destroy(bitmap); + + // Now render again with the image scaled larger horizontally (will be + // trimmed). + matrix.a = 2; + matrix.d = 1; + bitmap = FPDFBitmap_Create(width, height, 0); + FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); + FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); + CompareBitmap(bitmap, width, height, kTrimmedMD5); + FPDFBitmap_Destroy(bitmap); + + // Now try a 90 degree rotation + matrix.a = 0; + matrix.b = 1; + matrix.c = -1; + matrix.d = 0; + matrix.e = width; + bitmap = FPDFBitmap_Create(width, height, 0); + FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); + FPDF_RenderPageBitmapWithMatrix(bitmap, page, &matrix, &rect, 0); + CompareBitmap(bitmap, width, height, kRotatedMD5); FPDFBitmap_Destroy(bitmap); UnloadPage(page); diff --git a/testing/resources/rectangles.in b/testing/resources/rectangles.in new file mode 100644 index 0000000000..ed71cf830a --- /dev/null +++ b/testing/resources/rectangles.in @@ -0,0 +1,38 @@ +{{header}} +{{object 1 0}} << + /Type /Catalog + /Pages 2 0 R +>> +endobj +{{object 2 0}} << + /Type /Pages + /MediaBox [ 0 0 200 200 ] + /Count 1 + /Kids [ 3 0 R ] +>> +endobj +{{object 3 0}} << + /Type /Page + /Parent 2 0 R + /Contents 4 0 R +>> +endobj +{{object 4 0}} << +>> +stream +q +0 0 0 rg +10 80 50 30 re B* +0 0 1 rg +70 135 50 30 re B* +0 1 0 rg +130 80 50 30 re B* +1 0 0 rg +70 25 50 30 re B* +Q +endstream +endobj +{{xref}} +{{trailer}} +{{startxref}} +%%EOF diff --git a/testing/resources/rectangles.pdf b/testing/resources/rectangles.pdf new file mode 100644 index 0000000000..718bee56d9 --- /dev/null +++ b/testing/resources/rectangles.pdf @@ -0,0 +1,46 @@ +%PDF-1.7 +% ò¤ô +1 0 obj << + /Type /Catalog + /Pages 2 0 R +>> +endobj +2 0 obj << + /Type /Pages + /MediaBox [ 0 0 200 200 ] + /Count 1 + /Kids [ 3 0 R ] +>> +endobj +3 0 obj << + /Type /Page + /Parent 2 0 R + /Contents 4 0 R +>> +endobj +4 0 obj << +>> +stream +q +0 0 0 rg +10 80 50 30 re B* +0 0 1 rg +70 135 50 30 re B* +0 1 0 rg +130 80 50 30 re B* +1 0 0 rg +70 25 50 30 re B* +Q +endstream +endobj +xref +0 5 +0000000000 65535 f +0000000015 00000 n +0000000068 00000 n +0000000161 00000 n +0000000230 00000 n +trailer<< /Root 1 0 R /Size 5 >> +startxref +382 +%%EOF -- cgit v1.2.3