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" |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 14 | #include "core/fpdfapi/parser/cpdf_name.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 15 | #include "fpdfsdk/fsdk_define.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 16 | #include "third_party/base/ptr_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 17 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 18 | namespace { |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 19 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 20 | bool LoadJpegHelper(FPDF_PAGE* pages, |
| 21 | int nCount, |
| 22 | FPDF_PAGEOBJECT image_object, |
| 23 | FPDF_FILEACCESS* fileAccess, |
| 24 | bool inlineJpeg) { |
Andrew Weintraub | 21f88ff | 2017-05-10 13:19:52 -0400 | [diff] [blame] | 25 | if (!image_object || !fileAccess) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 26 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 27 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 28 | CFX_RetainPtr<IFX_SeekableReadStream> pFile = |
| 29 | MakeSeekableReadStream(fileAccess); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 30 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Andrew Weintraub | 21f88ff | 2017-05-10 13:19:52 -0400 | [diff] [blame] | 31 | |
| 32 | if (pages) { |
| 33 | for (int index = 0; index < nCount; index++) { |
| 34 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
| 35 | if (pPage) |
| 36 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
| 37 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 38 | } |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 39 | |
| 40 | if (inlineJpeg) |
| 41 | pImgObj->GetImage()->SetJpegImageInline(pFile); |
| 42 | else |
| 43 | pImgObj->GetImage()->SetJpegImage(pFile); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 44 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 45 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 48 | } // namespace |
| 49 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 50 | DLLEXPORT FPDF_PAGEOBJECT STDCALL |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 51 | FPDFPageObj_NewImageObj(FPDF_DOCUMENT document) { |
| 52 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 53 | if (!pDoc) |
| 54 | return nullptr; |
| 55 | |
Tom Sepez | 7d4f6a8 | 2017-03-31 17:10:34 -0700 | [diff] [blame] | 56 | auto pImageObj = pdfium::MakeUnique<CPDF_ImageObject>(); |
| 57 | pImageObj->SetImage(pdfium::MakeRetain<CPDF_Image>(pDoc)); |
| 58 | return pImageObj.release(); |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 61 | DLLEXPORT FPDF_BOOL STDCALL |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 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 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 69 | DLLEXPORT FPDF_BOOL STDCALL |
rbpotter | f085db3 | 2016-12-14 11:44:31 -0800 | [diff] [blame] | 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 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [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) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 87 | CPDF_ImageObject* pImgObj = static_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(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 92 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 93 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 96 | DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages, |
| 97 | int nCount, |
| 98 | FPDF_PAGEOBJECT image_object, |
| 99 | FPDF_BITMAP bitmap) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 100 | if (!image_object || !bitmap || !pages) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 101 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 102 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 103 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 104 | for (int index = 0; index < nCount; index++) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 105 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 106 | if (pPage) |
thestig | f41d9dc | 2016-08-05 22:34:58 -0700 | [diff] [blame] | 107 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 109 | CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap)); |
| 110 | pImgObj->GetImage()->SetImage(holder); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | pImgObj->CalcBoundingBox(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 112 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 113 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 114 | } |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 115 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 116 | DLLEXPORT FPDF_BITMAP STDCALL |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 117 | FPDFImageObj_GetBitmap(FPDF_PAGEOBJECT image_object) { |
| 118 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 119 | if (!pObj || !pObj->IsImage()) |
| 120 | return nullptr; |
| 121 | |
| 122 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 123 | if (!pImg) |
| 124 | return nullptr; |
| 125 | |
| 126 | CFX_RetainPtr<CFX_DIBSource> pSource = pImg->LoadDIBSource(); |
| 127 | if (!pSource) |
| 128 | return nullptr; |
| 129 | |
| 130 | CFX_RetainPtr<CFX_DIBitmap> pBitmap; |
| 131 | // If the source image has a representation of 1 bit per pixel, then convert |
| 132 | // it to a grayscale bitmap having 1 byte per pixel, since bitmaps have no |
| 133 | // concept of bits. Otherwise, convert the source image to a bitmap directly, |
| 134 | // retaining its color representation. |
| 135 | if (pSource->GetBPP() == 1) |
| 136 | pBitmap = pSource->CloneConvert(FXDIB_8bppRgb); |
| 137 | else |
| 138 | pBitmap = pSource->Clone(nullptr); |
| 139 | |
| 140 | return pBitmap.Leak(); |
| 141 | } |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 142 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 143 | DLLEXPORT unsigned long STDCALL |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 144 | FPDFImageObj_GetImageDataDecoded(FPDF_PAGEOBJECT image_object, |
| 145 | void* buffer, |
| 146 | unsigned long buflen) { |
| 147 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 148 | if (!pObj || !pObj->IsImage()) |
| 149 | return 0; |
| 150 | |
| 151 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 152 | if (!pImg) |
| 153 | return 0; |
| 154 | |
| 155 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 156 | if (!pImgStream) |
| 157 | return 0; |
| 158 | |
| 159 | return DecodeStreamMaybeCopyAndReturnLength(pImgStream, buffer, buflen); |
| 160 | } |
| 161 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 162 | DLLEXPORT unsigned long STDCALL |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 163 | FPDFImageObj_GetImageDataRaw(FPDF_PAGEOBJECT image_object, |
| 164 | void* buffer, |
| 165 | unsigned long buflen) { |
| 166 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 167 | if (!pObj || !pObj->IsImage()) |
| 168 | return 0; |
| 169 | |
| 170 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 171 | if (!pImg) |
| 172 | return 0; |
| 173 | |
| 174 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 175 | if (!pImgStream) |
| 176 | return 0; |
| 177 | |
| 178 | uint32_t len = pImgStream->GetRawSize(); |
| 179 | if (buffer && buflen >= len) |
| 180 | memcpy(buffer, pImgStream->GetRawData(), len); |
| 181 | |
| 182 | return len; |
| 183 | } |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 184 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 185 | DLLEXPORT int STDCALL |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 186 | FPDFImageObj_GetImageFilterCount(FPDF_PAGEOBJECT image_object) { |
| 187 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 188 | if (!pObj || !pObj->IsImage()) |
| 189 | return 0; |
| 190 | |
| 191 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 192 | if (!pImg) |
| 193 | return 0; |
| 194 | |
| 195 | CPDF_Dictionary* pDict = pImg->GetDict(); |
| 196 | CPDF_Object* pFilter = pDict ? pDict->GetDirectObjectFor("Filter") : nullptr; |
| 197 | if (!pFilter) |
| 198 | return 0; |
| 199 | |
| 200 | if (pFilter->IsArray()) |
| 201 | return pFilter->AsArray()->GetCount(); |
| 202 | if (pFilter->IsName()) |
| 203 | return 1; |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Henrique Nakashima | 7e805d1 | 2017-08-10 15:13:19 +0000 | [diff] [blame] | 208 | DLLEXPORT unsigned long STDCALL |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 209 | FPDFImageObj_GetImageFilter(FPDF_PAGEOBJECT image_object, |
| 210 | int index, |
| 211 | void* buffer, |
| 212 | unsigned long buflen) { |
| 213 | if (index < 0 || index >= FPDFImageObj_GetImageFilterCount(image_object)) |
| 214 | return 0; |
| 215 | |
| 216 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 217 | CPDF_Object* pFilter = |
| 218 | pObj->AsImage()->GetImage()->GetDict()->GetDirectObjectFor("Filter"); |
| 219 | CFX_WideString wsFilters; |
| 220 | if (pFilter->IsName()) |
| 221 | wsFilters = pFilter->AsName()->GetUnicodeText(); |
| 222 | else |
| 223 | wsFilters = pFilter->AsArray()->GetUnicodeTextAt(index); |
| 224 | |
| 225 | return Utf16EncodeMaybeCopyAndReturnLength(wsFilters, buffer, buflen); |
| 226 | } |