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); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 42 | pImgObj->SetDirty(true); |
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(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 90 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 91 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 94 | DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages, |
| 95 | int nCount, |
| 96 | FPDF_PAGEOBJECT image_object, |
| 97 | FPDF_BITMAP bitmap) { |
| 98 | if (!image_object || !bitmap || !pages) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 99 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 100 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 101 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | for (int index = 0; index < nCount; index++) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 103 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 104 | if (pPage) |
thestig | f41d9dc | 2016-08-05 22:34:58 -0700 | [diff] [blame] | 105 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 106 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 107 | CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap)); |
| 108 | pImgObj->GetImage()->SetImage(holder); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 109 | pImgObj->CalcBoundingBox(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 110 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 111 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 112 | } |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 113 | |
| 114 | DLLEXPORT FPDF_BITMAP STDCALL |
| 115 | FPDFImageObj_GetBitmap(FPDF_PAGEOBJECT image_object) { |
| 116 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 117 | if (!pObj || !pObj->IsImage()) |
| 118 | return nullptr; |
| 119 | |
| 120 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 121 | if (!pImg) |
| 122 | return nullptr; |
| 123 | |
| 124 | CFX_RetainPtr<CFX_DIBSource> pSource = pImg->LoadDIBSource(); |
| 125 | if (!pSource) |
| 126 | return nullptr; |
| 127 | |
| 128 | CFX_RetainPtr<CFX_DIBitmap> pBitmap; |
| 129 | // If the source image has a representation of 1 bit per pixel, then convert |
| 130 | // it to a grayscale bitmap having 1 byte per pixel, since bitmaps have no |
| 131 | // concept of bits. Otherwise, convert the source image to a bitmap directly, |
| 132 | // retaining its color representation. |
| 133 | if (pSource->GetBPP() == 1) |
| 134 | pBitmap = pSource->CloneConvert(FXDIB_8bppRgb); |
| 135 | else |
| 136 | pBitmap = pSource->Clone(nullptr); |
| 137 | |
| 138 | return pBitmap.Leak(); |
| 139 | } |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 140 | |
| 141 | DLLEXPORT unsigned long STDCALL |
| 142 | FPDFImageObj_GetImageDataDecoded(FPDF_PAGEOBJECT image_object, |
| 143 | void* buffer, |
| 144 | unsigned long buflen) { |
| 145 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 146 | if (!pObj || !pObj->IsImage()) |
| 147 | return 0; |
| 148 | |
| 149 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 150 | if (!pImg) |
| 151 | return 0; |
| 152 | |
| 153 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 154 | if (!pImgStream) |
| 155 | return 0; |
| 156 | |
| 157 | return DecodeStreamMaybeCopyAndReturnLength(pImgStream, buffer, buflen); |
| 158 | } |
| 159 | |
| 160 | DLLEXPORT unsigned long STDCALL |
| 161 | FPDFImageObj_GetImageDataRaw(FPDF_PAGEOBJECT image_object, |
| 162 | void* buffer, |
| 163 | unsigned long buflen) { |
| 164 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 165 | if (!pObj || !pObj->IsImage()) |
| 166 | return 0; |
| 167 | |
| 168 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 169 | if (!pImg) |
| 170 | return 0; |
| 171 | |
| 172 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 173 | if (!pImgStream) |
| 174 | return 0; |
| 175 | |
| 176 | uint32_t len = pImgStream->GetRawSize(); |
| 177 | if (buffer && buflen >= len) |
| 178 | memcpy(buffer, pImgStream->GetRawData(), len); |
| 179 | |
| 180 | return len; |
| 181 | } |