blob: 56875e233e13aedfa88aae5e0fa238a412dd96db [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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016DLLEXPORT FPDF_PAGEOBJECT STDCALL
17FPDFPageObj_NewImgeObj(FPDF_DOCUMENT document) {
Tom Sepez471a1032015-10-15 16:17:18 -070018 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
19 if (!pDoc)
20 return nullptr;
thestigc54bb432016-07-29 19:34:20 -070021
Tom Sepezae51c812015-08-05 12:34:06 -070022 CPDF_ImageObject* pImageObj = new CPDF_ImageObject;
tsepez36eb4bd2016-10-03 15:24:27 -070023 pImageObj->SetOwnedImage(pdfium::MakeUnique<CPDF_Image>(pDoc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 return pImageObj;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025}
26
rbpotterf085db32016-12-14 11:44:31 -080027FPDF_BOOL FPDFImageObj_LoadJpegHelper(FPDF_PAGE* pages,
28 int nCount,
29 FPDF_PAGEOBJECT image_object,
30 FPDF_FILEACCESS* fileAccess,
31 bool inlineJpeg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 if (!image_object || !fileAccess || !pages)
tsepez4cf55152016-11-02 14:37:54 -070033 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
tsepez833619b2016-12-07 09:21:17 -080035 CFX_RetainPtr<IFX_SeekableReadStream> pFile =
36 MakeSeekableReadStream(fileAccess);
thestigc54bb432016-07-29 19:34:20 -070037 CPDF_ImageObject* pImgObj = reinterpret_cast<CPDF_ImageObject*>(image_object);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038 for (int index = 0; index < nCount; index++) {
Tom Sepezdb0be962015-10-16 14:00:21 -070039 CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
thestigc54bb432016-07-29 19:34:20 -070040 if (pPage)
thestigf41d9dc2016-08-05 22:34:58 -070041 pImgObj->GetImage()->ResetCache(pPage, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 }
rbpotterf085db32016-12-14 11:44:31 -080043
44 if (inlineJpeg)
45 pImgObj->GetImage()->SetJpegImageInline(pFile);
46 else
47 pImgObj->GetImage()->SetJpegImage(pFile);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
tsepez4cf55152016-11-02 14:37:54 -070049 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050}
51
rbpotterf085db32016-12-14 11:44:31 -080052DLLEXPORT FPDF_BOOL STDCALL
53FPDFImageObj_LoadJpegFile(FPDF_PAGE* pages,
54 int nCount,
55 FPDF_PAGEOBJECT image_object,
56 FPDF_FILEACCESS* fileAccess) {
57 return FPDFImageObj_LoadJpegHelper(pages, nCount, image_object, fileAccess,
58 false);
59}
60
61DLLEXPORT FPDF_BOOL STDCALL
62FPDFImageObj_LoadJpegFileInline(FPDF_PAGE* pages,
63 int nCount,
64 FPDF_PAGEOBJECT image_object,
65 FPDF_FILEACCESS* fileAccess) {
66 return FPDFImageObj_LoadJpegHelper(pages, nCount, image_object, fileAccess,
67 true);
68}
69
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetMatrix(FPDF_PAGEOBJECT image_object,
71 double a,
72 double b,
73 double c,
74 double d,
75 double e,
76 double f) {
77 if (!image_object)
tsepez4cf55152016-11-02 14:37:54 -070078 return false;
thestigc54bb432016-07-29 19:34:20 -070079
80 CPDF_ImageObject* pImgObj = reinterpret_cast<CPDF_ImageObject*>(image_object);
thestig8a5e15d2016-11-21 14:54:29 -080081 pImgObj->set_matrix(
82 CFX_Matrix(static_cast<FX_FLOAT>(a), static_cast<FX_FLOAT>(b),
83 static_cast<FX_FLOAT>(c), static_cast<FX_FLOAT>(d),
84 static_cast<FX_FLOAT>(e), static_cast<FX_FLOAT>(f)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 pImgObj->CalcBoundingBox();
tsepez4cf55152016-11-02 14:37:54 -070086 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087}
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
90 int nCount,
91 FPDF_PAGEOBJECT image_object,
92 FPDF_BITMAP bitmap) {
93 if (!image_object || !bitmap || !pages)
tsepez4cf55152016-11-02 14:37:54 -070094 return false;
thestigc54bb432016-07-29 19:34:20 -070095
96 CPDF_ImageObject* pImgObj = reinterpret_cast<CPDF_ImageObject*>(image_object);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 for (int index = 0; index < nCount; index++) {
Tom Sepezdb0be962015-10-16 14:00:21 -070098 CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
thestigc54bb432016-07-29 19:34:20 -070099 if (pPage)
thestigf41d9dc2016-08-05 22:34:58 -0700100 pImgObj->GetImage()->ResetCache(pPage, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 }
thestig4ccdb142016-11-21 15:09:23 -0800102 pImgObj->GetImage()->SetImage(reinterpret_cast<CFX_DIBitmap*>(bitmap));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 pImgObj->CalcBoundingBox();
tsepez4cf55152016-11-02 14:37:54 -0700104 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105}