summaryrefslogtreecommitdiff
path: root/fpdfsdk
diff options
context:
space:
mode:
authorJane Liu <janeliulwq@google.com>2017-08-02 21:45:57 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-08-03 20:20:02 +0000
commit28fb7ba083dba5e09493fd37a11994de51527dfc (patch)
treede009c28d419bed2a9a123c257f923893db0b053 /fpdfsdk
parent844d5dac8a6c97b2cd3bd92bf07d0cc62158408a (diff)
downloadpdfium-28fb7ba083dba5e09493fd37a11994de51527dfc.tar.xz
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 <thestig@chromium.org> Commit-Queue: Jane Liu <janeliulwq@google.com>
Diffstat (limited to 'fpdfsdk')
-rw-r--r--fpdfsdk/fpdfedit_embeddertest.cpp54
-rw-r--r--fpdfsdk/fpdfeditimg.cpp27
-rw-r--r--fpdfsdk/fpdfview.cpp20
-rw-r--r--fpdfsdk/fpdfview_c_api_test.c2
4 files changed, 103 insertions, 0 deletions
diff --git a/fpdfsdk/fpdfedit_embeddertest.cpp b/fpdfsdk/fpdfedit_embeddertest.cpp
index cc3e50b362..dcaeb945d7 100644
--- a/fpdfsdk/fpdfedit_embeddertest.cpp
+++ b/fpdfsdk/fpdfedit_embeddertest.cpp
@@ -925,3 +925,57 @@ TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
}
TestAndCloseSaved(612, 792, md5);
}
+
+TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
+ ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
+ FPDF_PAGE page = LoadPage(0);
+ ASSERT_TRUE(page);
+ ASSERT_EQ(39, FPDFPage_CountObject(page));
+
+ FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
+ EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
+
+ obj = FPDFPage_GetObject(page, 33);
+ ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
+ EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
+ CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
+ FPDFBitmap_Destroy(bitmap);
+
+ obj = FPDFPage_GetObject(page, 34);
+ ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ bitmap = FPDFImageObj_GetBitmap(obj);
+ EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
+ CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
+ FPDFBitmap_Destroy(bitmap);
+
+ obj = FPDFPage_GetObject(page, 35);
+ ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ bitmap = FPDFImageObj_GetBitmap(obj);
+ EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
+ CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
+ FPDFBitmap_Destroy(bitmap);
+
+ obj = FPDFPage_GetObject(page, 36);
+ ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ bitmap = FPDFImageObj_GetBitmap(obj);
+ EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
+ CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
+ FPDFBitmap_Destroy(bitmap);
+
+ obj = FPDFPage_GetObject(page, 37);
+ ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ bitmap = FPDFImageObj_GetBitmap(obj);
+ EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
+ CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
+ FPDFBitmap_Destroy(bitmap);
+
+ obj = FPDFPage_GetObject(page, 38);
+ ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ bitmap = FPDFImageObj_GetBitmap(obj);
+ EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
+ CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
+ FPDFBitmap_Destroy(bitmap);
+ UnloadPage(page);
+}
diff --git a/fpdfsdk/fpdfeditimg.cpp b/fpdfsdk/fpdfeditimg.cpp
index fdc98e06e9..bfd12b2441 100644
--- a/fpdfsdk/fpdfeditimg.cpp
+++ b/fpdfsdk/fpdfeditimg.cpp
@@ -110,3 +110,30 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
pImgObj->SetDirty(true);
return true;
}
+
+DLLEXPORT FPDF_BITMAP STDCALL
+FPDFImageObj_GetBitmap(FPDF_PAGEOBJECT image_object) {
+ CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object);
+ if (!pObj || !pObj->IsImage())
+ return nullptr;
+
+ CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage();
+ if (!pImg)
+ return nullptr;
+
+ CFX_RetainPtr<CFX_DIBSource> pSource = pImg->LoadDIBSource();
+ if (!pSource)
+ return nullptr;
+
+ CFX_RetainPtr<CFX_DIBitmap> pBitmap;
+ // If the source image has a representation of 1 bit per pixel, then convert
+ // it to a grayscale bitmap having 1 byte per pixel, since bitmaps have no
+ // concept of bits. Otherwise, convert the source image to a bitmap directly,
+ // retaining its color representation.
+ if (pSource->GetBPP() == 1)
+ pBitmap = pSource->CloneConvert(FXDIB_8bppRgb);
+ else
+ pBitmap = pSource->Clone(nullptr);
+
+ return pBitmap.Leak();
+}
diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp
index 355fcb3f8f..5aa80139ae 100644
--- a/fpdfsdk/fpdfview.cpp
+++ b/fpdfsdk/fpdfview.cpp
@@ -1126,6 +1126,26 @@ DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
return pBitmap.Leak();
}
+DLLEXPORT int STDCALL FPDFBitmap_GetFormat(FPDF_BITMAP bitmap) {
+ if (!bitmap)
+ return FPDFBitmap_Unknown;
+
+ FXDIB_Format format = CFXBitmapFromFPDFBitmap(bitmap)->GetFormat();
+ switch (format) {
+ case FXDIB_8bppRgb:
+ case FXDIB_8bppMask:
+ return FPDFBitmap_Gray;
+ case FXDIB_Rgb:
+ return FPDFBitmap_BGR;
+ case FXDIB_Rgb32:
+ return FPDFBitmap_BGRx;
+ case FXDIB_Argb:
+ return FPDFBitmap_BGRA;
+ default:
+ return FPDFBitmap_Unknown;
+ }
+}
+
DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
int left,
int top,
diff --git a/fpdfsdk/fpdfview_c_api_test.c b/fpdfsdk/fpdfview_c_api_test.c
index 7d42be8792..e47f4d172c 100644
--- a/fpdfsdk/fpdfview_c_api_test.c
+++ b/fpdfsdk/fpdfview_c_api_test.c
@@ -132,6 +132,7 @@ int CheckPDFiumCApi() {
CHK(FPDFImageObj_LoadJpegFileInline);
CHK(FPDFImageObj_SetMatrix);
CHK(FPDFImageObj_SetBitmap);
+ CHK(FPDFImageObj_GetBitmap);
CHK(FPDFPageObj_CreateNewPath);
CHK(FPDFPageObj_CreateNewRect);
CHK(FPDFPath_SetStrokeColor);
@@ -302,6 +303,7 @@ int CheckPDFiumCApi() {
CHK(FPDF_PageToDevice);
CHK(FPDFBitmap_Create);
CHK(FPDFBitmap_CreateEx);
+ CHK(FPDFBitmap_GetFormat);
CHK(FPDFBitmap_FillRect);
CHK(FPDFBitmap_GetBuffer);
CHK(FPDFBitmap_GetWidth);