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