blob: 0d0c54604b8cdaa685e6092a54bff8c18acffc7e [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"
dsinclair114e46a2016-09-29 17:18:21 -070013#include "fpdfsdk/fsdk_define.h"
tsepez36eb4bd2016-10-03 15:24:27 -070014#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Lei Zhangcbd89572017-03-15 17:35:47 -070016namespace {
thestigc54bb432016-07-29 19:34:20 -070017
Lei Zhangcbd89572017-03-15 17:35:47 -070018bool LoadJpegHelper(FPDF_PAGE* pages,
19 int nCount,
20 FPDF_PAGEOBJECT image_object,
21 FPDF_FILEACCESS* fileAccess,
22 bool inlineJpeg) {
Andrew Weintraub21f88ff2017-05-10 13:19:52 -040023 if (!image_object || !fileAccess)
tsepez4cf55152016-11-02 14:37:54 -070024 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
tsepez833619b2016-12-07 09:21:17 -080026 CFX_RetainPtr<IFX_SeekableReadStream> pFile =
27 MakeSeekableReadStream(fileAccess);
Nicolas Pena46abb662017-05-17 17:23:22 -040028 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Andrew Weintraub21f88ff2017-05-10 13:19:52 -040029
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 Weber9d8ec5a2015-08-04 13:00:21 -070036 }
rbpotterf085db32016-12-14 11:44:31 -080037
38 if (inlineJpeg)
39 pImgObj->GetImage()->SetJpegImageInline(pFile);
40 else
41 pImgObj->GetImage()->SetJpegImage(pFile);
wileyryae858aa42017-05-31 14:49:05 -050042 pImgObj->SetDirty(true);
tsepez4cf55152016-11-02 14:37:54 -070043 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
45
Lei Zhangcbd89572017-03-15 17:35:47 -070046} // namespace
47
48DLLEXPORT FPDF_PAGEOBJECT STDCALL
49FPDFPageObj_NewImageObj(FPDF_DOCUMENT document) {
50 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
51 if (!pDoc)
52 return nullptr;
53
Tom Sepez7d4f6a82017-03-31 17:10:34 -070054 auto pImageObj = pdfium::MakeUnique<CPDF_ImageObject>();
55 pImageObj->SetImage(pdfium::MakeRetain<CPDF_Image>(pDoc));
56 return pImageObj.release();
Lei Zhangcbd89572017-03-15 17:35:47 -070057}
58
rbpotterf085db32016-12-14 11:44:31 -080059DLLEXPORT FPDF_BOOL STDCALL
60FPDFImageObj_LoadJpegFile(FPDF_PAGE* pages,
61 int nCount,
62 FPDF_PAGEOBJECT image_object,
63 FPDF_FILEACCESS* fileAccess) {
Lei Zhangcbd89572017-03-15 17:35:47 -070064 return LoadJpegHelper(pages, nCount, image_object, fileAccess, false);
rbpotterf085db32016-12-14 11:44:31 -080065}
66
67DLLEXPORT FPDF_BOOL STDCALL
68FPDFImageObj_LoadJpegFileInline(FPDF_PAGE* pages,
69 int nCount,
70 FPDF_PAGEOBJECT image_object,
71 FPDF_FILEACCESS* fileAccess) {
Lei Zhangcbd89572017-03-15 17:35:47 -070072 return LoadJpegHelper(pages, nCount, image_object, fileAccess, true);
rbpotterf085db32016-12-14 11:44:31 -080073}
74
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075DLLEXPORT 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)
tsepez4cf55152016-11-02 14:37:54 -070083 return false;
thestigc54bb432016-07-29 19:34:20 -070084
Nicolas Pena46abb662017-05-17 17:23:22 -040085 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Dan Sinclair05df0752017-03-14 14:43:42 -040086 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 Weber9d8ec5a2015-08-04 13:00:21 -070089 pImgObj->CalcBoundingBox();
wileyryae858aa42017-05-31 14:49:05 -050090 pImgObj->SetDirty(true);
tsepez4cf55152016-11-02 14:37:54 -070091 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094DLLEXPORT 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)
tsepez4cf55152016-11-02 14:37:54 -070099 return false;
thestigc54bb432016-07-29 19:34:20 -0700100
Nicolas Pena46abb662017-05-17 17:23:22 -0400101 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 for (int index = 0; index < nCount; index++) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700103 CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
thestigc54bb432016-07-29 19:34:20 -0700104 if (pPage)
thestigf41d9dc2016-08-05 22:34:58 -0700105 pImgObj->GetImage()->ResetCache(pPage, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 }
Tom Sepezf0799fe2017-03-28 09:31:32 -0700107 CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap));
108 pImgObj->GetImage()->SetImage(holder);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 pImgObj->CalcBoundingBox();
wileyryae858aa42017-05-31 14:49:05 -0500110 pImgObj->SetDirty(true);
tsepez4cf55152016-11-02 14:37:54 -0700111 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112}
Jane Liu28fb7ba2017-08-02 21:45:57 -0400113
114DLLEXPORT FPDF_BITMAP STDCALL
115FPDFImageObj_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 Liu548334e2017-08-03 16:33:40 -0400140
141DLLEXPORT unsigned long STDCALL
142FPDFImageObj_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
160DLLEXPORT unsigned long STDCALL
161FPDFImageObj_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}