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 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 54 | CFX_RetainPtr<IFX_SeekableReadStream> pFile = |
| 55 | MakeSeekableReadStream(fileAccess); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 56 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Andrew Weintraub | 21f88ff | 2017-05-10 13:19:52 -0400 | [diff] [blame] | 57 | |
| 58 | if (pages) { |
| 59 | for (int index = 0; index < nCount; index++) { |
| 60 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
| 61 | if (pPage) |
| 62 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
| 63 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | } |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 65 | |
| 66 | if (inlineJpeg) |
| 67 | pImgObj->GetImage()->SetJpegImageInline(pFile); |
| 68 | else |
| 69 | pImgObj->GetImage()->SetJpegImage(pFile); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 70 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 71 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 74 | } // namespace |
| 75 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 76 | FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 77 | FPDFPageObj_NewImageObj(FPDF_DOCUMENT document) { |
| 78 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 79 | if (!pDoc) |
| 80 | return nullptr; |
| 81 | |
Tom Sepez | 7d4f6a8 | 2017-03-31 17:10:34 -0700 | [diff] [blame] | 82 | auto pImageObj = pdfium::MakeUnique<CPDF_ImageObject>(); |
| 83 | pImageObj->SetImage(pdfium::MakeRetain<CPDF_Image>(pDoc)); |
| 84 | return pImageObj.release(); |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 87 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 88 | FPDFImageObj_LoadJpegFile(FPDF_PAGE* pages, |
| 89 | int nCount, |
| 90 | FPDF_PAGEOBJECT image_object, |
| 91 | FPDF_FILEACCESS* fileAccess) { |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 92 | return LoadJpegHelper(pages, nCount, image_object, fileAccess, false); |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 95 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 96 | FPDFImageObj_LoadJpegFileInline(FPDF_PAGE* pages, |
| 97 | int nCount, |
| 98 | FPDF_PAGEOBJECT image_object, |
| 99 | FPDF_FILEACCESS* fileAccess) { |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 100 | return LoadJpegHelper(pages, nCount, image_object, fileAccess, true); |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 103 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 104 | FPDFImageObj_SetMatrix(FPDF_PAGEOBJECT image_object, |
| 105 | double a, |
| 106 | double b, |
| 107 | double c, |
| 108 | double d, |
| 109 | double e, |
| 110 | double f) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | if (!image_object) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 112 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 113 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 114 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 115 | pImgObj->set_matrix(CFX_Matrix(static_cast<float>(a), static_cast<float>(b), |
| 116 | static_cast<float>(c), static_cast<float>(d), |
| 117 | static_cast<float>(e), static_cast<float>(f))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | pImgObj->CalcBoundingBox(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 119 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 120 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 123 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 124 | FPDFImageObj_SetBitmap(FPDF_PAGE* pages, |
| 125 | int nCount, |
| 126 | FPDF_PAGEOBJECT image_object, |
| 127 | FPDF_BITMAP bitmap) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 128 | if (!image_object || !bitmap || !pages) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 129 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 130 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 131 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | for (int index = 0; index < nCount; index++) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 133 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 134 | if (pPage) |
thestig | f41d9dc | 2016-08-05 22:34:58 -0700 | [diff] [blame] | 135 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 137 | CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap)); |
| 138 | pImgObj->GetImage()->SetImage(holder); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 139 | pImgObj->CalcBoundingBox(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 140 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 141 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 142 | } |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 143 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 144 | FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 145 | FPDFImageObj_GetBitmap(FPDF_PAGEOBJECT image_object) { |
| 146 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 147 | if (!pObj || !pObj->IsImage()) |
| 148 | return nullptr; |
| 149 | |
| 150 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 151 | if (!pImg) |
| 152 | return nullptr; |
| 153 | |
| 154 | CFX_RetainPtr<CFX_DIBSource> pSource = pImg->LoadDIBSource(); |
| 155 | if (!pSource) |
| 156 | return nullptr; |
| 157 | |
| 158 | CFX_RetainPtr<CFX_DIBitmap> pBitmap; |
| 159 | // If the source image has a representation of 1 bit per pixel, then convert |
| 160 | // it to a grayscale bitmap having 1 byte per pixel, since bitmaps have no |
| 161 | // concept of bits. Otherwise, convert the source image to a bitmap directly, |
| 162 | // retaining its color representation. |
| 163 | if (pSource->GetBPP() == 1) |
| 164 | pBitmap = pSource->CloneConvert(FXDIB_8bppRgb); |
| 165 | else |
| 166 | pBitmap = pSource->Clone(nullptr); |
| 167 | |
| 168 | return pBitmap.Leak(); |
| 169 | } |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 170 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 171 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 172 | FPDFImageObj_GetImageDataDecoded(FPDF_PAGEOBJECT image_object, |
| 173 | void* buffer, |
| 174 | unsigned long buflen) { |
| 175 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 176 | if (!pObj || !pObj->IsImage()) |
| 177 | return 0; |
| 178 | |
| 179 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 180 | if (!pImg) |
| 181 | return 0; |
| 182 | |
| 183 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 184 | if (!pImgStream) |
| 185 | return 0; |
| 186 | |
| 187 | return DecodeStreamMaybeCopyAndReturnLength(pImgStream, buffer, buflen); |
| 188 | } |
| 189 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 190 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 191 | FPDFImageObj_GetImageDataRaw(FPDF_PAGEOBJECT image_object, |
| 192 | void* buffer, |
| 193 | unsigned long buflen) { |
| 194 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 195 | if (!pObj || !pObj->IsImage()) |
| 196 | return 0; |
| 197 | |
| 198 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 199 | if (!pImg) |
| 200 | return 0; |
| 201 | |
| 202 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 203 | if (!pImgStream) |
| 204 | return 0; |
| 205 | |
| 206 | uint32_t len = pImgStream->GetRawSize(); |
| 207 | if (buffer && buflen >= len) |
| 208 | memcpy(buffer, pImgStream->GetRawData(), len); |
| 209 | |
| 210 | return len; |
| 211 | } |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 212 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 213 | FPDF_EXPORT int FPDF_CALLCONV |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 214 | FPDFImageObj_GetImageFilterCount(FPDF_PAGEOBJECT image_object) { |
| 215 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 216 | if (!pObj || !pObj->IsImage()) |
| 217 | return 0; |
| 218 | |
| 219 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 220 | if (!pImg) |
| 221 | return 0; |
| 222 | |
| 223 | CPDF_Dictionary* pDict = pImg->GetDict(); |
| 224 | CPDF_Object* pFilter = pDict ? pDict->GetDirectObjectFor("Filter") : nullptr; |
| 225 | if (!pFilter) |
| 226 | return 0; |
| 227 | |
| 228 | if (pFilter->IsArray()) |
| 229 | return pFilter->AsArray()->GetCount(); |
| 230 | if (pFilter->IsName()) |
| 231 | return 1; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 236 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 237 | FPDFImageObj_GetImageFilter(FPDF_PAGEOBJECT image_object, |
| 238 | int index, |
| 239 | void* buffer, |
| 240 | unsigned long buflen) { |
| 241 | if (index < 0 || index >= FPDFImageObj_GetImageFilterCount(image_object)) |
| 242 | return 0; |
| 243 | |
| 244 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 245 | CPDF_Object* pFilter = |
| 246 | pObj->AsImage()->GetImage()->GetDict()->GetDirectObjectFor("Filter"); |
| 247 | CFX_WideString wsFilters; |
| 248 | if (pFilter->IsName()) |
| 249 | wsFilters = pFilter->AsName()->GetUnicodeText(); |
| 250 | else |
| 251 | wsFilters = pFilter->AsArray()->GetUnicodeTextAt(index); |
| 252 | |
| 253 | return Utf16EncodeMaybeCopyAndReturnLength(wsFilters, buffer, buflen); |
| 254 | } |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame^] | 255 | |
| 256 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 257 | FPDFImageObj_GetImageMetadata(FPDF_PAGEOBJECT image_object, |
| 258 | FPDF_PAGE page, |
| 259 | FPDF_IMAGEOBJ_METADATA* metadata) { |
| 260 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 261 | if (!pObj || !pObj->IsImage() || !metadata) |
| 262 | return false; |
| 263 | |
| 264 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 265 | if (!pImg) |
| 266 | return false; |
| 267 | |
| 268 | const int nPixelWidth = pImg->GetPixelWidth(); |
| 269 | const int nPixelHeight = pImg->GetPixelHeight(); |
| 270 | metadata->width = nPixelWidth; |
| 271 | metadata->height = nPixelHeight; |
| 272 | |
| 273 | const float nWidth = pObj->m_Right - pObj->m_Left; |
| 274 | const float nHeight = pObj->m_Top - pObj->m_Bottom; |
| 275 | constexpr int nPointsPerInch = 72; |
| 276 | if (nWidth != 0 && nHeight != 0) { |
| 277 | metadata->horizontal_dpi = nPixelWidth / nWidth * nPointsPerInch; |
| 278 | metadata->vertical_dpi = nPixelHeight / nHeight * nPointsPerInch; |
| 279 | } |
| 280 | |
| 281 | metadata->bits_per_pixel = 0; |
| 282 | metadata->colorspace = FPDF_COLORSPACE_UNKNOWN; |
| 283 | |
| 284 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 285 | if (!pPage || !pPage->m_pDocument.Get() || !pImg->GetStream()) |
| 286 | return true; |
| 287 | |
| 288 | auto pSource = pdfium::MakeRetain<CPDF_DIBSource>(); |
| 289 | if (!pSource->StartLoadDIBSource(pPage->m_pDocument.Get(), pImg->GetStream(), |
| 290 | false, nullptr, |
| 291 | pPage->m_pPageResources.Get())) { |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | metadata->bits_per_pixel = pSource->GetBPP(); |
| 296 | if (pSource->GetColorSpace()) |
| 297 | metadata->colorspace = pSource->GetColorSpace()->GetFamily(); |
| 298 | |
| 299 | return true; |
| 300 | } |