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" |
| 12 | #include "core/fpdfapi/page/cpdf_pageobject.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 13 | #include "fpdfsdk/fsdk_define.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 14 | #include "third_party/base/ptr_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 15 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 16 | namespace { |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 17 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 18 | bool LoadJpegHelper(FPDF_PAGE* pages, |
| 19 | int nCount, |
| 20 | FPDF_PAGEOBJECT image_object, |
| 21 | FPDF_FILEACCESS* fileAccess, |
| 22 | bool inlineJpeg) { |
Andrew Weintraub | 21f88ff | 2017-05-10 13:19:52 -0400 | [diff] [blame] | 23 | if (!image_object || !fileAccess) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 24 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 25 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 26 | CFX_RetainPtr<IFX_SeekableReadStream> pFile = |
| 27 | MakeSeekableReadStream(fileAccess); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame^] | 28 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Andrew Weintraub | 21f88ff | 2017-05-10 13:19:52 -0400 | [diff] [blame] | 29 | |
| 30 | if (pages) { |
| 31 | for (int index = 0; index < nCount; index++) { |
| 32 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
| 33 | if (pPage) |
| 34 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
| 35 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | } |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 37 | |
| 38 | if (inlineJpeg) |
| 39 | pImgObj->GetImage()->SetJpegImageInline(pFile); |
| 40 | else |
| 41 | pImgObj->GetImage()->SetJpegImage(pFile); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 42 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 43 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 46 | } // namespace |
| 47 | |
| 48 | DLLEXPORT FPDF_PAGEOBJECT STDCALL |
| 49 | FPDFPageObj_NewImageObj(FPDF_DOCUMENT document) { |
| 50 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 51 | if (!pDoc) |
| 52 | return nullptr; |
| 53 | |
Tom Sepez | 7d4f6a8 | 2017-03-31 17:10:34 -0700 | [diff] [blame] | 54 | auto pImageObj = pdfium::MakeUnique<CPDF_ImageObject>(); |
| 55 | pImageObj->SetImage(pdfium::MakeRetain<CPDF_Image>(pDoc)); |
| 56 | return pImageObj.release(); |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 59 | DLLEXPORT FPDF_BOOL STDCALL |
| 60 | FPDFImageObj_LoadJpegFile(FPDF_PAGE* pages, |
| 61 | int nCount, |
| 62 | FPDF_PAGEOBJECT image_object, |
| 63 | FPDF_FILEACCESS* fileAccess) { |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 64 | return LoadJpegHelper(pages, nCount, image_object, fileAccess, false); |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | DLLEXPORT FPDF_BOOL STDCALL |
| 68 | FPDFImageObj_LoadJpegFileInline(FPDF_PAGE* pages, |
| 69 | int nCount, |
| 70 | FPDF_PAGEOBJECT image_object, |
| 71 | FPDF_FILEACCESS* fileAccess) { |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 72 | return LoadJpegHelper(pages, nCount, image_object, fileAccess, true); |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetMatrix(FPDF_PAGEOBJECT image_object, |
| 76 | double a, |
| 77 | double b, |
| 78 | double c, |
| 79 | double d, |
| 80 | double e, |
| 81 | double f) { |
| 82 | if (!image_object) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 83 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 84 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame^] | 85 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 86 | pImgObj->set_matrix(CFX_Matrix(static_cast<float>(a), static_cast<float>(b), |
| 87 | static_cast<float>(c), static_cast<float>(d), |
| 88 | static_cast<float>(e), static_cast<float>(f))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | pImgObj->CalcBoundingBox(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 90 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages, |
| 94 | int nCount, |
| 95 | FPDF_PAGEOBJECT image_object, |
| 96 | FPDF_BITMAP bitmap) { |
| 97 | if (!image_object || !bitmap || !pages) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 98 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 99 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame^] | 100 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | for (int index = 0; index < nCount; index++) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 102 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 103 | if (pPage) |
thestig | f41d9dc | 2016-08-05 22:34:58 -0700 | [diff] [blame] | 104 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 106 | CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap)); |
| 107 | pImgObj->GetImage()->SetImage(holder); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | pImgObj->CalcBoundingBox(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 109 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 110 | } |