From 28fb7ba083dba5e09493fd37a11994de51527dfc Mon Sep 17 00:00:00 2001 From: Jane Liu Date: Wed, 2 Aug 2017 21:45:57 -0400 Subject: APIs and tests for extracting bitmaps from image objects Added FPDFImageObj_GetBitmap() that returns the bitmap of an image object, and a FPDFBitmap_GetFormat() that returns the format of a bitmap. * Fixed a small bitmap conversion bug in cfx_dibsource.cpp. * Enabled EmbedderTest::CompareBitmap() to support different formats of bitmaps. * Added an embedder test and a test PDF file with images of many different formats. Bug=pdfium:677 Change-Id: I6a72f9d969cf5f3577db9400ca33197c213622ed Reviewed-on: https://pdfium-review.googlesource.com/9690 Reviewed-by: Lei Zhang Commit-Queue: Jane Liu --- testing/embedder_test.cpp | 28 +++++++++++++++++++++++++--- testing/resources/embedded_images.pdf | Bin 0 -> 27089 bytes 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 testing/resources/embedded_images.pdf (limited to 'testing') diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp index 0846d8ccb7..82ffb3b825 100644 --- a/testing/embedder_test.cpp +++ b/testing/embedder_test.cpp @@ -42,6 +42,22 @@ FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {} +int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) { + const int format = FPDFBitmap_GetFormat(bitmap); + switch (format) { + case FPDFBitmap_Gray: + return 1; + case FPDFBitmap_BGR: + return 3; + case FPDFBitmap_BGRx: + case FPDFBitmap_BGRA: + return 4; + default: + ASSERT(false); + return 0; + } +} + } // namespace EmbedderTest::EmbedderTest() @@ -381,8 +397,10 @@ std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap, int expected_width, int expected_height) { uint8_t digest[16]; - CRYPT_MD5Generate(static_cast(FPDFBitmap_GetBuffer(bitmap)), - expected_width * 4 * expected_height, digest); + CRYPT_MD5Generate( + static_cast(FPDFBitmap_GetBuffer(bitmap)), + expected_width * GetBitmapBytesPerPixel(bitmap) * expected_height, + digest); return CryptToBase16(digest); } @@ -393,7 +411,11 @@ void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap, const char* expected_md5sum) { ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap)); ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap)); - const int expected_stride = expected_width * 4; + + // The expected stride is calculated using the same formula as in + // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride. + const int expected_stride = + (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4; ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap)); if (!expected_md5sum) diff --git a/testing/resources/embedded_images.pdf b/testing/resources/embedded_images.pdf new file mode 100644 index 0000000000..81845822a4 Binary files /dev/null and b/testing/resources/embedded_images.pdf differ -- cgit v1.2.3