Added faster method for getting the size of an image object.
Example when the old method works slowly:
https://bugs.chromium.org/p/pdfium/issues/detail?id=1924#c6
But this is not the solution for bug 1924. It's just adding
new method for usability.
Change-Id: I22e4d26e05c30d49411a872f3b31ac6360b785ef
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/106290
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp
index 47a753a..670892a 100644
--- a/fpdfsdk/fpdf_edit_embeddertest.cpp
+++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -4433,6 +4433,38 @@
UnloadPage(page);
}
+TEST_F(FPDFEditEmbedderTest, GetImagePixelSize) {
+ ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
+ FPDF_PAGE page = LoadPage(0);
+ ASSERT_TRUE(page);
+
+ // Check that getting the size of a null object would fail.
+ unsigned int width = 0;
+ unsigned int height = 0;
+ EXPECT_FALSE(FPDFImageObj_GetImagePixelSize(nullptr, &width, &height));
+
+ // Check that receiving the size with a null width and height pointers would
+ // fail.
+ FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
+ ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ EXPECT_FALSE(FPDFImageObj_GetImagePixelSize(obj, nullptr, nullptr));
+ EXPECT_FALSE(FPDFImageObj_GetImagePixelSize(obj, nullptr, &height));
+ EXPECT_FALSE(FPDFImageObj_GetImagePixelSize(obj, &width, nullptr));
+
+ // Verify the pixel size of image.
+ ASSERT_TRUE(FPDFImageObj_GetImagePixelSize(obj, &width, &height));
+ EXPECT_EQ(92u, width);
+ EXPECT_EQ(68u, height);
+
+ obj = FPDFPage_GetObject(page, 37);
+ ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
+ ASSERT_TRUE(FPDFImageObj_GetImagePixelSize(obj, &width, &height));
+ EXPECT_EQ(126u, width);
+ EXPECT_EQ(106u, height);
+
+ UnloadPage(page);
+}
+
TEST_F(FPDFEditEmbedderTest, GetRenderedBitmapForHelloWorldText) {
ASSERT_TRUE(OpenDocument("hello_world.pdf"));
FPDF_PAGE page = LoadPage(0);
diff --git a/fpdfsdk/fpdf_editimg.cpp b/fpdfsdk/fpdf_editimg.cpp
index 05d1ff7..3fae5b4 100644
--- a/fpdfsdk/fpdf_editimg.cpp
+++ b/fpdfsdk/fpdf_editimg.cpp
@@ -407,3 +407,22 @@
}
return true;
}
+
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
+FPDFImageObj_GetImagePixelSize(FPDF_PAGEOBJECT image_object,
+ unsigned int* width,
+ unsigned int* height) {
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
+ if (!pImgObj || !width || !height) {
+ return false;
+ }
+
+ RetainPtr<CPDF_Image> pImg = pImgObj->GetImage();
+ if (!pImg) {
+ return false;
+ }
+
+ *width = pImg->GetPixelWidth();
+ *height = pImg->GetPixelHeight();
+ return true;
+}
diff --git a/fpdfsdk/fpdf_view_c_api_test.c b/fpdfsdk/fpdf_view_c_api_test.c
index e9231d7..b66fe62 100644
--- a/fpdfsdk/fpdf_view_c_api_test.c
+++ b/fpdfsdk/fpdf_view_c_api_test.c
@@ -179,6 +179,7 @@
CHK(FPDFImageObj_GetImageFilter);
CHK(FPDFImageObj_GetImageFilterCount);
CHK(FPDFImageObj_GetImageMetadata);
+ CHK(FPDFImageObj_GetImagePixelSize);
CHK(FPDFImageObj_GetRenderedBitmap);
CHK(FPDFImageObj_LoadJpegFile);
CHK(FPDFImageObj_LoadJpegFileInline);
diff --git a/public/fpdf_edit.h b/public/fpdf_edit.h
index 2e4e7c0..8df2e3b 100644
--- a/public/fpdf_edit.h
+++ b/public/fpdf_edit.h
@@ -731,6 +731,19 @@
FPDF_PAGE page,
FPDF_IMAGEOBJ_METADATA* metadata);
+// Experimental API.
+// Get the image size in pixels. Faster method to get only image size.
+//
+// image_object - handle to an image object.
+// width - receives the image width in pixels; must not be NULL.
+// height - receives the image height in pixels; must not be NULL.
+//
+// Returns true if successful.
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
+FPDFImageObj_GetImagePixelSize(FPDF_PAGEOBJECT image_object,
+ unsigned int* width,
+ unsigned int* height);
+
// Create a new path object at an initial position.
//
// x - initial horizontal position.