blob: f19e21c8ff2aaa79219b10ec3d68340988f8bc05 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_edit.h"
8
dsinclair39c62fd2016-09-29 12:49:17 -07009#include "core/fpdfapi/cpdf_modulemgr.h"
dsinclair41872fa2016-10-04 11:29:35 -070010#include "core/fpdfapi/page/cpdf_image.h"
11#include "core/fpdfapi/page/cpdf_imageobject.h"
12#include "core/fpdfapi/page/cpdf_pageobject.h"
Jane Liube63ab92017-08-09 14:09:34 -040013#include "core/fpdfapi/parser/cpdf_array.h"
14#include "core/fpdfapi/parser/cpdf_name.h"
dsinclair114e46a2016-09-29 17:18:21 -070015#include "fpdfsdk/fsdk_define.h"
tsepez36eb4bd2016-10-03 15:24:27 -070016#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Lei Zhangcbd89572017-03-15 17:35:47 -070018namespace {
thestigc54bb432016-07-29 19:34:20 -070019
Lei Zhangcbd89572017-03-15 17:35:47 -070020bool LoadJpegHelper(FPDF_PAGE* pages,
21 int nCount,
22 FPDF_PAGEOBJECT image_object,
23 FPDF_FILEACCESS* fileAccess,
24 bool inlineJpeg) {
Andrew Weintraub21f88ff2017-05-10 13:19:52 -040025 if (!image_object || !fileAccess)
tsepez4cf55152016-11-02 14:37:54 -070026 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
tsepez833619b2016-12-07 09:21:17 -080028 CFX_RetainPtr<IFX_SeekableReadStream> pFile =
29 MakeSeekableReadStream(fileAccess);
Nicolas Pena46abb662017-05-17 17:23:22 -040030 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Andrew Weintraub21f88ff2017-05-10 13:19:52 -040031
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 Weber9d8ec5a2015-08-04 13:00:21 -070038 }
rbpotterf085db32016-12-14 11:44:31 -080039
40 if (inlineJpeg)
41 pImgObj->GetImage()->SetJpegImageInline(pFile);
42 else
43 pImgObj->GetImage()->SetJpegImage(pFile);
wileyryae858aa42017-05-31 14:49:05 -050044 pImgObj->SetDirty(true);
tsepez4cf55152016-11-02 14:37:54 -070045 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046}
47
Lei Zhangcbd89572017-03-15 17:35:47 -070048} // namespace
49
Dan Sinclair00d2ad12017-08-10 14:13:02 -040050FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV
Lei Zhangcbd89572017-03-15 17:35:47 -070051FPDFPageObj_NewImageObj(FPDF_DOCUMENT document) {
52 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
53 if (!pDoc)
54 return nullptr;
55
Tom Sepez7d4f6a82017-03-31 17:10:34 -070056 auto pImageObj = pdfium::MakeUnique<CPDF_ImageObject>();
57 pImageObj->SetImage(pdfium::MakeRetain<CPDF_Image>(pDoc));
58 return pImageObj.release();
Lei Zhangcbd89572017-03-15 17:35:47 -070059}
60
Dan Sinclair00d2ad12017-08-10 14:13:02 -040061FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
rbpotterf085db32016-12-14 11:44:31 -080062FPDFImageObj_LoadJpegFile(FPDF_PAGE* pages,
63 int nCount,
64 FPDF_PAGEOBJECT image_object,
65 FPDF_FILEACCESS* fileAccess) {
Lei Zhangcbd89572017-03-15 17:35:47 -070066 return LoadJpegHelper(pages, nCount, image_object, fileAccess, false);
rbpotterf085db32016-12-14 11:44:31 -080067}
68
Dan Sinclair00d2ad12017-08-10 14:13:02 -040069FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
rbpotterf085db32016-12-14 11:44:31 -080070FPDFImageObj_LoadJpegFileInline(FPDF_PAGE* pages,
71 int nCount,
72 FPDF_PAGEOBJECT image_object,
73 FPDF_FILEACCESS* fileAccess) {
Lei Zhangcbd89572017-03-15 17:35:47 -070074 return LoadJpegHelper(pages, nCount, image_object, fileAccess, true);
rbpotterf085db32016-12-14 11:44:31 -080075}
76
Dan Sinclair00d2ad12017-08-10 14:13:02 -040077FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
78FPDFImageObj_SetMatrix(FPDF_PAGEOBJECT image_object,
79 double a,
80 double b,
81 double c,
82 double d,
83 double e,
84 double f) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 if (!image_object)
tsepez4cf55152016-11-02 14:37:54 -070086 return false;
thestigc54bb432016-07-29 19:34:20 -070087
Nicolas Pena46abb662017-05-17 17:23:22 -040088 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Dan Sinclair05df0752017-03-14 14:43:42 -040089 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 Weber9d8ec5a2015-08-04 13:00:21 -070092 pImgObj->CalcBoundingBox();
wileyryae858aa42017-05-31 14:49:05 -050093 pImgObj->SetDirty(true);
tsepez4cf55152016-11-02 14:37:54 -070094 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095}
96
Dan Sinclair00d2ad12017-08-10 14:13:02 -040097FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
98FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
99 int nCount,
100 FPDF_PAGEOBJECT image_object,
101 FPDF_BITMAP bitmap) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (!image_object || !bitmap || !pages)
tsepez4cf55152016-11-02 14:37:54 -0700103 return false;
thestigc54bb432016-07-29 19:34:20 -0700104
Nicolas Pena46abb662017-05-17 17:23:22 -0400105 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 for (int index = 0; index < nCount; index++) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700107 CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
thestigc54bb432016-07-29 19:34:20 -0700108 if (pPage)
thestigf41d9dc2016-08-05 22:34:58 -0700109 pImgObj->GetImage()->ResetCache(pPage, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 }
Tom Sepezf0799fe2017-03-28 09:31:32 -0700111 CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap));
112 pImgObj->GetImage()->SetImage(holder);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 pImgObj->CalcBoundingBox();
wileyryae858aa42017-05-31 14:49:05 -0500114 pImgObj->SetDirty(true);
tsepez4cf55152016-11-02 14:37:54 -0700115 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
Jane Liu28fb7ba2017-08-02 21:45:57 -0400117
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400118FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV
Jane Liu28fb7ba2017-08-02 21:45:57 -0400119FPDFImageObj_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 Liu548334e2017-08-03 16:33:40 -0400144
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400145FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu548334e2017-08-03 16:33:40 -0400146FPDFImageObj_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 Sinclair00d2ad12017-08-10 14:13:02 -0400164FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu548334e2017-08-03 16:33:40 -0400165FPDFImageObj_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 Liube63ab92017-08-09 14:09:34 -0400186
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400187FPDF_EXPORT int FPDF_CALLCONV
Jane Liube63ab92017-08-09 14:09:34 -0400188FPDFImageObj_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 Sinclair00d2ad12017-08-10 14:13:02 -0400210FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liube63ab92017-08-09 14:09:34 -0400211FPDFImageObj_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}