blob: 9f4b2968ee07530cd6a9d3396df40f0dc4ea3452 [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);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
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();
tsepez4cf55152016-11-02 14:37:54 -070090 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091}
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
94 int nCount,
95 FPDF_PAGEOBJECT image_object,
96 FPDF_BITMAP bitmap) {
97 if (!image_object || !bitmap || !pages)
tsepez4cf55152016-11-02 14:37:54 -070098 return false;
thestigc54bb432016-07-29 19:34:20 -070099
Nicolas Pena46abb662017-05-17 17:23:22 -0400100 CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 for (int index = 0; index < nCount; index++) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700102 CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
thestigc54bb432016-07-29 19:34:20 -0700103 if (pPage)
thestigf41d9dc2016-08-05 22:34:58 -0700104 pImgObj->GetImage()->ResetCache(pPage, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 }
Tom Sepezf0799fe2017-03-28 09:31:32 -0700106 CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap));
107 pImgObj->GetImage()->SetImage(holder);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 pImgObj->CalcBoundingBox();
tsepez4cf55152016-11-02 14:37:54 -0700109 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110}