blob: 69151d1999452474e8a303192ff150a4c206bb35 [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) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 if (!image_object || !fileAccess || !pages)
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);
thestigc54bb432016-07-29 19:34:20 -070028 CPDF_ImageObject* pImgObj = reinterpret_cast<CPDF_ImageObject*>(image_object);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 for (int index = 0; index < nCount; index++) {
Tom Sepezdb0be962015-10-16 14:00:21 -070030 CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
thestigc54bb432016-07-29 19:34:20 -070031 if (pPage)
thestigf41d9dc2016-08-05 22:34:58 -070032 pImgObj->GetImage()->ResetCache(pPage, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 }
rbpotterf085db32016-12-14 11:44:31 -080034
35 if (inlineJpeg)
36 pImgObj->GetImage()->SetJpegImageInline(pFile);
37 else
38 pImgObj->GetImage()->SetJpegImage(pFile);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039
tsepez4cf55152016-11-02 14:37:54 -070040 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041}
42
Lei Zhangcbd89572017-03-15 17:35:47 -070043} // namespace
44
45DLLEXPORT FPDF_PAGEOBJECT STDCALL
46FPDFPageObj_NewImageObj(FPDF_DOCUMENT document) {
47 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
48 if (!pDoc)
49 return nullptr;
50
51 CPDF_ImageObject* pImageObj = new CPDF_ImageObject;
52 pImageObj->SetOwnedImage(pdfium::MakeUnique<CPDF_Image>(pDoc));
53 return pImageObj;
54}
55
56DLLEXPORT FPDF_PAGEOBJECT STDCALL
57FPDFPageObj_NewImgeObj(FPDF_DOCUMENT document) {
58 return FPDFPageObj_NewImageObj(document);
59}
60
rbpotterf085db32016-12-14 11:44:31 -080061DLLEXPORT FPDF_BOOL STDCALL
62FPDFImageObj_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
69DLLEXPORT FPDF_BOOL STDCALL
70FPDFImageObj_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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077DLLEXPORT 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) {
84 if (!image_object)
tsepez4cf55152016-11-02 14:37:54 -070085 return false;
thestigc54bb432016-07-29 19:34:20 -070086
87 CPDF_ImageObject* pImgObj = reinterpret_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();
tsepez4cf55152016-11-02 14:37:54 -070092 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
96 int nCount,
97 FPDF_PAGEOBJECT image_object,
98 FPDF_BITMAP bitmap) {
99 if (!image_object || !bitmap || !pages)
tsepez4cf55152016-11-02 14:37:54 -0700100 return false;
thestigc54bb432016-07-29 19:34:20 -0700101
102 CPDF_ImageObject* pImgObj = reinterpret_cast<CPDF_ImageObject*>(image_object);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 for (int index = 0; index < nCount; index++) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700104 CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
thestigc54bb432016-07-29 19:34:20 -0700105 if (pPage)
thestigf41d9dc2016-08-05 22:34:58 -0700106 pImgObj->GetImage()->ResetCache(pPage, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 }
thestig4ccdb142016-11-21 15:09:23 -0800108 pImgObj->GetImage()->SetImage(reinterpret_cast<CFX_DIBitmap*>(bitmap));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 pImgObj->CalcBoundingBox();
tsepez4cf55152016-11-02 14:37:54 -0700110 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111}