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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 50 | FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV |
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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 61 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 69 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 77 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 78 | FPDFImageObj_SetMatrix(FPDF_PAGEOBJECT image_object, |
| 79 | double a, |
| 80 | double b, |
| 81 | double c, |
| 82 | double d, |
| 83 | double e, |
| 84 | double f) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | if (!image_object) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 86 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 87 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 88 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 89 | pImgObj->set_matrix(CFX_Matrix(static_cast<float>(a), static_cast<float>(b), |
| 90 | static_cast<float>(c), static_cast<float>(d), |
| 91 | static_cast<float>(e), static_cast<float>(f))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | pImgObj->CalcBoundingBox(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 93 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 94 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 97 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 98 | FPDFImageObj_SetBitmap(FPDF_PAGE* pages, |
| 99 | int nCount, |
| 100 | FPDF_PAGEOBJECT image_object, |
| 101 | FPDF_BITMAP bitmap) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | if (!image_object || !bitmap || !pages) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 103 | return false; |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 104 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 105 | CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 106 | for (int index = 0; index < nCount; index++) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 107 | CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
thestig | c54bb43 | 2016-07-29 19:34:20 -0700 | [diff] [blame] | 108 | if (pPage) |
thestig | f41d9dc | 2016-08-05 22:34:58 -0700 | [diff] [blame] | 109 | pImgObj->GetImage()->ResetCache(pPage, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 110 | } |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 111 | CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap)); |
| 112 | pImgObj->GetImage()->SetImage(holder); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | pImgObj->CalcBoundingBox(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 114 | pImgObj->SetDirty(true); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 115 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 116 | } |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 117 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 118 | FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 119 | FPDFImageObj_GetBitmap(FPDF_PAGEOBJECT image_object) { |
| 120 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 121 | if (!pObj || !pObj->IsImage()) |
| 122 | return nullptr; |
| 123 | |
| 124 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 125 | if (!pImg) |
| 126 | return nullptr; |
| 127 | |
| 128 | CFX_RetainPtr<CFX_DIBSource> pSource = pImg->LoadDIBSource(); |
| 129 | if (!pSource) |
| 130 | return nullptr; |
| 131 | |
| 132 | CFX_RetainPtr<CFX_DIBitmap> pBitmap; |
| 133 | // If the source image has a representation of 1 bit per pixel, then convert |
| 134 | // it to a grayscale bitmap having 1 byte per pixel, since bitmaps have no |
| 135 | // concept of bits. Otherwise, convert the source image to a bitmap directly, |
| 136 | // retaining its color representation. |
| 137 | if (pSource->GetBPP() == 1) |
| 138 | pBitmap = pSource->CloneConvert(FXDIB_8bppRgb); |
| 139 | else |
| 140 | pBitmap = pSource->Clone(nullptr); |
| 141 | |
| 142 | return pBitmap.Leak(); |
| 143 | } |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 144 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 145 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 146 | FPDFImageObj_GetImageDataDecoded(FPDF_PAGEOBJECT image_object, |
| 147 | void* buffer, |
| 148 | unsigned long buflen) { |
| 149 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 150 | if (!pObj || !pObj->IsImage()) |
| 151 | return 0; |
| 152 | |
| 153 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 154 | if (!pImg) |
| 155 | return 0; |
| 156 | |
| 157 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 158 | if (!pImgStream) |
| 159 | return 0; |
| 160 | |
| 161 | return DecodeStreamMaybeCopyAndReturnLength(pImgStream, buffer, buflen); |
| 162 | } |
| 163 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 164 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 165 | FPDFImageObj_GetImageDataRaw(FPDF_PAGEOBJECT image_object, |
| 166 | void* buffer, |
| 167 | unsigned long buflen) { |
| 168 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 169 | if (!pObj || !pObj->IsImage()) |
| 170 | return 0; |
| 171 | |
| 172 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 173 | if (!pImg) |
| 174 | return 0; |
| 175 | |
| 176 | CPDF_Stream* pImgStream = pImg->GetStream(); |
| 177 | if (!pImgStream) |
| 178 | return 0; |
| 179 | |
| 180 | uint32_t len = pImgStream->GetRawSize(); |
| 181 | if (buffer && buflen >= len) |
| 182 | memcpy(buffer, pImgStream->GetRawData(), len); |
| 183 | |
| 184 | return len; |
| 185 | } |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 186 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 187 | FPDF_EXPORT int FPDF_CALLCONV |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 188 | FPDFImageObj_GetImageFilterCount(FPDF_PAGEOBJECT image_object) { |
| 189 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 190 | if (!pObj || !pObj->IsImage()) |
| 191 | return 0; |
| 192 | |
| 193 | CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| 194 | if (!pImg) |
| 195 | return 0; |
| 196 | |
| 197 | CPDF_Dictionary* pDict = pImg->GetDict(); |
| 198 | CPDF_Object* pFilter = pDict ? pDict->GetDirectObjectFor("Filter") : nullptr; |
| 199 | if (!pFilter) |
| 200 | return 0; |
| 201 | |
| 202 | if (pFilter->IsArray()) |
| 203 | return pFilter->AsArray()->GetCount(); |
| 204 | if (pFilter->IsName()) |
| 205 | return 1; |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 210 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 211 | FPDFImageObj_GetImageFilter(FPDF_PAGEOBJECT image_object, |
| 212 | int index, |
| 213 | void* buffer, |
| 214 | unsigned long buflen) { |
| 215 | if (index < 0 || index >= FPDFImageObj_GetImageFilterCount(image_object)) |
| 216 | return 0; |
| 217 | |
| 218 | CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| 219 | CPDF_Object* pFilter = |
| 220 | pObj->AsImage()->GetImage()->GetDict()->GetDirectObjectFor("Filter"); |
| 221 | CFX_WideString wsFilters; |
| 222 | if (pFilter->IsName()) |
| 223 | wsFilters = pFilter->AsName()->GetUnicodeText(); |
| 224 | else |
| 225 | wsFilters = pFilter->AsArray()->GetUnicodeTextAt(index); |
| 226 | |
| 227 | return Utf16EncodeMaybeCopyAndReturnLength(wsFilters, buffer, buflen); |
| 228 | } |