John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_edit.h" |
| 8 | |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame] | 9 | #include "core/fpdfapi/cpdf_modulemgr.h" |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 10 | #include "core/fpdfapi/page/cpdf_image.h" |
| 11 | #include "core/fpdfapi/page/cpdf_imageobject.h" |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 12 | #include "core/fpdfapi/page/cpdf_page.h" |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/page/cpdf_pageobject.h" |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 15 | #include "core/fpdfapi/parser/cpdf_name.h" |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 16 | #include "core/fpdfapi/render/cpdf_dibsource.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 17 | #include "fpdfsdk/fsdk_define.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 18 | #include "third_party/base/ptr_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 19 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 20 | namespace { |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 21 | |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 22 | // These checks ensure the consistency of colorspace values across core/ and |
| 23 | // public/. |
| 24 | static_assert(PDFCS_DEVICEGRAY == FPDF_COLORSPACE_DEVICEGRAY, |
| 25 | "PDFCS_DEVICEGRAY value mismatch"); |
| 26 | static_assert(PDFCS_DEVICERGB == FPDF_COLORSPACE_DEVICERGB, |
| 27 | "PDFCS_DEVICERGB value mismatch"); |
| 28 | static_assert(PDFCS_DEVICECMYK == FPDF_COLORSPACE_DEVICECMYK, |
| 29 | "PDFCS_DEVICECMYK value mismatch"); |
| 30 | static_assert(PDFCS_CALGRAY == FPDF_COLORSPACE_CALGRAY, |
| 31 | "PDFCS_CALGRAY value mismatch"); |
| 32 | static_assert(PDFCS_CALRGB == FPDF_COLORSPACE_CALRGB, |
| 33 | "PDFCS_CALRGB value mismatch"); |
| 34 | static_assert(PDFCS_LAB == FPDF_COLORSPACE_LAB, "PDFCS_LAB value mismatch"); |
| 35 | static_assert(PDFCS_ICCBASED == FPDF_COLORSPACE_ICCBASED, |
| 36 | "PDFCS_ICCBASED value mismatch"); |
| 37 | static_assert(PDFCS_SEPARATION == FPDF_COLORSPACE_SEPARATION, |
| 38 | "PDFCS_SEPARATION value mismatch"); |
| 39 | static_assert(PDFCS_DEVICEN == FPDF_COLORSPACE_DEVICEN, |
| 40 | "PDFCS_DEVICEN value mismatch"); |
| 41 | static_assert(PDFCS_INDEXED == FPDF_COLORSPACE_INDEXED, |
| 42 | "PDFCS_INDEXED value mismatch"); |
| 43 | static_assert(PDFCS_PATTERN == FPDF_COLORSPACE_PATTERN, |
| 44 | "PDFCS_PATTERN value mismatch"); |
| 45 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 46 | bool LoadJpegHelper(FPDF_PAGE* pages, |
| 47 | int nCount, |
| 48 | FPDF_PAGEOBJECT image_object, |
| 49 | FPDF_FILEACCESS* fileAccess, |
| 50 | bool inlineJpeg) { |
Andrew Weintraub | 21f88ff | 2017-05-10 13:19:52 -0400 | [diff] [blame] | 51 | if (!image_object || !fileAccess) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 52 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 53 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 54 | RetainPtr<IFX_SeekableReadStream> pFile = MakeSeekableReadStream(fileAccess); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 55 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Andrew Weintraub | 21f88ff | 2017-05-10 13:19:52 -0400 | [diff] [blame] | 56 | |
| 57 | if (pages) { |
| 58 | for (int index = 0; index < nCount; index++) { |
| 59 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
| 60 | if (pPage) |
| 61 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
| 62 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | } |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 64 | |
| 65 | if (inlineJpeg) |
| 66 | pImgObj->GetImage()->SetJpegImageInline(pFile); |
| 67 | else |
| 68 | pImgObj->GetImage()->SetJpegImage(pFile); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 69 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 70 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 73 | } // namespace |
| 74 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 75 | FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 76 | FPDFPageObj_NewImageObj(FPDF_DOCUMENT document) { |
| 77 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 78 | if (!pDoc) |
| 79 | return nullptr; |
| 80 | |
Tom Sepez | 7d4f6a8 | 2017-03-31 17:10:34 -0700 | [diff] [blame] | 81 | auto pImageObj = pdfium::MakeUnique<CPDF_ImageObject>(); |
| 82 | pImageObj->SetImage(pdfium::MakeRetain<CPDF_Image>(pDoc)); |
| 83 | return pImageObj.release(); |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 86 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 87 | FPDFImageObj_LoadJpegFile(FPDF_PAGE* pages, |
| 88 | int nCount, |
| 89 | FPDF_PAGEOBJECT image_object, |
| 90 | FPDF_FILEACCESS* fileAccess) { |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 91 | return LoadJpegHelper(pages, nCount, image_object, fileAccess, false); |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 94 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 95 | FPDFImageObj_LoadJpegFileInline(FPDF_PAGE* pages, |
| 96 | int nCount, |
| 97 | FPDF_PAGEOBJECT image_object, |
| 98 | FPDF_FILEACCESS* fileAccess) { |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 99 | return LoadJpegHelper(pages, nCount, image_object, fileAccess, true); |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 102 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 103 | FPDFImageObj_SetMatrix(FPDF_PAGEOBJECT image_object, |
| 104 | double a, |
| 105 | double b, |
| 106 | double c, |
| 107 | double d, |
| 108 | double e, |
| 109 | double f) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 110 | if (!image_object) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 111 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 112 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 113 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 114 | pImgObj->set_matrix(CFX_Matrix(static_cast<float>(a), static_cast<float>(b), |
| 115 | static_cast<float>(c), static_cast<float>(d), |
| 116 | static_cast<float>(e), static_cast<float>(f))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 117 | pImgObj->CalcBoundingBox(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 118 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 119 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 122 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 123 | FPDFImageObj_SetBitmap(FPDF_PAGE* pages, |
| 124 | int nCount, |
| 125 | FPDF_PAGEOBJECT image_object, |
| 126 | FPDF_BITMAP bitmap) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 127 | if (!image_object || !bitmap || !pages) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 128 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 129 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 130 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | for (int index = 0; index < nCount; index++) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 132 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 133 | if (pPage) |
thestig | f41d9dc | 2016-08-05 22:34:58 -0700 | [diff] [blame] | 134 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 135 | } |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 136 | RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap)); |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 137 | pImgObj->GetImage()->SetImage(holder); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 138 | pImgObj->CalcBoundingBox(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 139 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 140 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 141 | } |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 142 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 143 | FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 144 | FPDFImageObj_GetBitmap(FPDF_PAGEOBJECT image_object) { |
| 145 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 146 | if (!pObj || !pObj->IsImage()) |
| 147 | return nullptr; |
| 148 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 149 | RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 150 | if (!pImg) |
| 151 | return nullptr; |
| 152 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 153 | RetainPtr<CFX_DIBSource> pSource = pImg->LoadDIBSource(); |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 154 | if (!pSource) |
| 155 | return nullptr; |
| 156 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 157 | RetainPtr<CFX_DIBitmap> pBitmap; |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 158 | // If the source image has a representation of 1 bit per pixel, then convert |
| 159 | // it to a grayscale bitmap having 1 byte per pixel, since bitmaps have no |
| 160 | // concept of bits. Otherwise, convert the source image to a bitmap directly, |
| 161 | // retaining its color representation. |
| 162 | if (pSource->GetBPP() == 1) |
| 163 | pBitmap = pSource->CloneConvert(FXDIB_8bppRgb); |
| 164 | else |
| 165 | pBitmap = pSource->Clone(nullptr); |
| 166 | |
| 167 | return pBitmap.Leak(); |
| 168 | } |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 169 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 170 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 171 | FPDFImageObj_GetImageDataDecoded(FPDF_PAGEOBJECT image_object, |
| 172 | void* buffer, |
| 173 | unsigned long buflen) { |
| 174 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 175 | if (!pObj || !pObj->IsImage()) |
| 176 | return 0; |
| 177 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 178 | RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 179 | if (!pImg) |
| 180 | return 0; |
| 181 | |
| 182 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 183 | if (!pImgStream) |
| 184 | return 0; |
| 185 | |
| 186 | return DecodeStreamMaybeCopyAndReturnLength(pImgStream, buffer, buflen); |
| 187 | } |
| 188 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 189 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 190 | FPDFImageObj_GetImageDataRaw(FPDF_PAGEOBJECT image_object, |
| 191 | void* buffer, |
| 192 | unsigned long buflen) { |
| 193 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 194 | if (!pObj || !pObj->IsImage()) |
| 195 | return 0; |
| 196 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 197 | RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 198 | if (!pImg) |
| 199 | return 0; |
| 200 | |
| 201 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 202 | if (!pImgStream) |
| 203 | return 0; |
| 204 | |
| 205 | uint32_t len = pImgStream->GetRawSize(); |
| 206 | if (buffer && buflen >= len) |
| 207 | memcpy(buffer, pImgStream->GetRawData(), len); |
| 208 | |
| 209 | return len; |
| 210 | } |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 211 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 212 | FPDF_EXPORT int FPDF_CALLCONV |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 213 | FPDFImageObj_GetImageFilterCount(FPDF_PAGEOBJECT image_object) { |
| 214 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 215 | if (!pObj || !pObj->IsImage()) |
| 216 | return 0; |
| 217 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 218 | RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 219 | if (!pImg) |
| 220 | return 0; |
| 221 | |
| 222 | CPDF_Dictionary* pDict = pImg->GetDict(); |
| 223 | CPDF_Object* pFilter = pDict ? pDict->GetDirectObjectFor("Filter") : nullptr; |
| 224 | if (!pFilter) |
| 225 | return 0; |
| 226 | |
| 227 | if (pFilter->IsArray()) |
| 228 | return pFilter->AsArray()->GetCount(); |
| 229 | if (pFilter->IsName()) |
| 230 | return 1; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 235 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 236 | FPDFImageObj_GetImageFilter(FPDF_PAGEOBJECT image_object, |
| 237 | int index, |
| 238 | void* buffer, |
| 239 | unsigned long buflen) { |
| 240 | if (index < 0 || index >= FPDFImageObj_GetImageFilterCount(image_object)) |
| 241 | return 0; |
| 242 | |
| 243 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 244 | CPDF_Object* pFilter = |
| 245 | pObj->AsImage()->GetImage()->GetDict()->GetDirectObjectFor("Filter"); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 246 | ByteString bsFilter; |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 247 | if (pFilter->IsName()) |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 248 | bsFilter = pFilter->AsName()->GetString(); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 249 | else |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 250 | bsFilter = pFilter->AsArray()->GetStringAt(index); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 251 | |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 252 | unsigned long len = bsFilter.GetLength() + 1; |
| 253 | if (buffer && len <= buflen) |
| 254 | memcpy(buffer, bsFilter.c_str(), len); |
| 255 | return len; |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 256 | } |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 257 | |
| 258 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 259 | FPDFImageObj_GetImageMetadata(FPDF_PAGEOBJECT image_object, |
| 260 | FPDF_PAGE page, |
| 261 | FPDF_IMAGEOBJ_METADATA* metadata) { |
| 262 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 263 | if (!pObj || !pObj->IsImage() || !metadata) |
| 264 | return false; |
| 265 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 266 | RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 267 | if (!pImg) |
| 268 | return false; |
| 269 | |
| 270 | const int nPixelWidth = pImg->GetPixelWidth(); |
| 271 | const int nPixelHeight = pImg->GetPixelHeight(); |
| 272 | metadata->width = nPixelWidth; |
| 273 | metadata->height = nPixelHeight; |
| 274 | |
| 275 | const float nWidth = pObj->m_Right - pObj->m_Left; |
| 276 | const float nHeight = pObj->m_Top - pObj->m_Bottom; |
| 277 | constexpr int nPointsPerInch = 72; |
| 278 | if (nWidth != 0 && nHeight != 0) { |
| 279 | metadata->horizontal_dpi = nPixelWidth / nWidth * nPointsPerInch; |
| 280 | metadata->vertical_dpi = nPixelHeight / nHeight * nPointsPerInch; |
| 281 | } |
| 282 | |
| 283 | metadata->bits_per_pixel = 0; |
| 284 | metadata->colorspace = FPDF_COLORSPACE_UNKNOWN; |
| 285 | |
| 286 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 287 | if (!pPage || !pPage->m_pDocument.Get() || !pImg->GetStream()) |
| 288 | return true; |
| 289 | |
| 290 | auto pSource = pdfium::MakeRetain<CPDF_DIBSource>(); |
| 291 | if (!pSource->StartLoadDIBSource(pPage->m_pDocument.Get(), pImg->GetStream(), |
| 292 | false, nullptr, |
| 293 | pPage->m_pPageResources.Get())) { |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | metadata->bits_per_pixel = pSource->GetBPP(); |
| 298 | if (pSource->GetColorSpace()) |
| 299 | metadata->colorspace = pSource->GetColorSpace()->GetFamily(); |
| 300 | |
| 301 | return true; |
| 302 | } |