blob: b4254e97c65166284b6f5f5de112d8765e279c23 [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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000050DLLEXPORT FPDF_PAGEOBJECT STDCALL
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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000061DLLEXPORT FPDF_BOOL STDCALL
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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000069DLLEXPORT FPDF_BOOL STDCALL
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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000077DLLEXPORT 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 Weber9d8ec5a2015-08-04 13:00:21 -070084 if (!image_object)
tsepez4cf55152016-11-02 14:37:54 -070085 return false;
thestigc54bb432016-07-29 19:34:20 -070086
Nicolas Pena46abb662017-05-17 17:23:22 -040087 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Dan Sinclair05df0752017-03-14 14:43:42 -040088 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 Weber9d8ec5a2015-08-04 13:00:21 -070091 pImgObj->CalcBoundingBox();
wileyryae858aa42017-05-31 14:49:05 -050092 pImgObj->SetDirty(true);
tsepez4cf55152016-11-02 14:37:54 -070093 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Henrique Nakashima7e805d12017-08-10 15:13:19 +000096DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
97 int nCount,
98 FPDF_PAGEOBJECT image_object,
99 FPDF_BITMAP bitmap) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 if (!image_object || !bitmap || !pages)
tsepez4cf55152016-11-02 14:37:54 -0700101 return false;
thestigc54bb432016-07-29 19:34:20 -0700102
Nicolas Pena46abb662017-05-17 17:23:22 -0400103 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 for (int index = 0; index < nCount; index++) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700105 CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
thestigc54bb432016-07-29 19:34:20 -0700106 if (pPage)
thestigf41d9dc2016-08-05 22:34:58 -0700107 pImgObj->GetImage()->ResetCache(pPage, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 }
Tom Sepezf0799fe2017-03-28 09:31:32 -0700109 CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap));
110 pImgObj->GetImage()->SetImage(holder);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 pImgObj->CalcBoundingBox();
wileyryae858aa42017-05-31 14:49:05 -0500112 pImgObj->SetDirty(true);
tsepez4cf55152016-11-02 14:37:54 -0700113 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114}
Jane Liu28fb7ba2017-08-02 21:45:57 -0400115
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000116DLLEXPORT FPDF_BITMAP STDCALL
Jane Liu28fb7ba2017-08-02 21:45:57 -0400117FPDFImageObj_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 Liu548334e2017-08-03 16:33:40 -0400142
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000143DLLEXPORT unsigned long STDCALL
Jane Liu548334e2017-08-03 16:33:40 -0400144FPDFImageObj_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 Nakashima7e805d12017-08-10 15:13:19 +0000162DLLEXPORT unsigned long STDCALL
Jane Liu548334e2017-08-03 16:33:40 -0400163FPDFImageObj_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 Liube63ab92017-08-09 14:09:34 -0400184
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000185DLLEXPORT int STDCALL
Jane Liube63ab92017-08-09 14:09:34 -0400186FPDFImageObj_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 Nakashima7e805d12017-08-10 15:13:19 +0000208DLLEXPORT unsigned long STDCALL
Jane Liube63ab92017-08-09 14:09:34 -0400209FPDFImageObj_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}