• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "image_object.h"
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 #include "fpdf_edit.h"
23 #include "logging.h"
24 
25 #define LOG_TAG "image_object"
26 
27 namespace pdfClient {
28 
GetBitmapFormat(int bitmap_format)29 BitmapFormat GetBitmapFormat(int bitmap_format) {
30     switch (bitmap_format) {
31         case FPDFBitmap_BGR: {
32             return BitmapFormat::BGR;
33         }
34         case FPDFBitmap_BGRA: {
35             return BitmapFormat::BGRA;
36         }
37         case FPDFBitmap_BGRx: {
38             return BitmapFormat::BGRx;
39         }
40         default: {
41             return BitmapFormat::Unknown;
42         }
43     }
44 }
45 
ImageObject()46 ImageObject::ImageObject() : PageObject(Type::Image) {}
47 
CreateFPDFInstance(FPDF_DOCUMENT document,FPDF_PAGE page)48 ScopedFPDFPageObject ImageObject::CreateFPDFInstance(FPDF_DOCUMENT document, FPDF_PAGE page) {
49     // Create a scoped PDFium image object.
50     ScopedFPDFPageObject scoped_image_object(FPDFPageObj_NewImageObj(document));
51     if (!scoped_image_object) {
52         return nullptr;
53     }
54     // Update attributes of PDFium image object.
55     if (!UpdateFPDFInstance(scoped_image_object.get(), page)) {
56         return nullptr;
57     }
58     return scoped_image_object;
59 }
60 
UpdateFPDFInstance(FPDF_PAGEOBJECT image_object,FPDF_PAGE page)61 bool ImageObject::UpdateFPDFInstance(FPDF_PAGEOBJECT image_object, FPDF_PAGE page) {
62     if (!image_object) {
63         return false;
64     }
65 
66     // Check for Type Correctness.
67     if (FPDFPageObj_GetType(image_object) != FPDF_PAGEOBJ_IMAGE) {
68         return false;
69     }
70 
71     // Set the updated bitmap.
72     if (!FPDFImageObj_SetBitmap(nullptr, 0, image_object, bitmap_.get())) {
73         return false;
74     }
75 
76     // Set the updated matrix.
77     if (!SetDeviceToPageMatrix(image_object, page)) {
78         return false;
79     }
80 
81     // Set the updated dimensions.
82     width_ = FPDFBitmap_GetWidth(bitmap_.get());
83     height_ = FPDFBitmap_GetHeight(bitmap_.get());
84 
85     // Set the updated bitmap format.
86     bitmap_format_ = GetBitmapFormat(FPDFBitmap_GetFormat(bitmap_.get()));
87 
88     return true;
89 }
90 
PopulateFromFPDFInstance(FPDF_PAGEOBJECT image_object,FPDF_PAGE page)91 bool ImageObject::PopulateFromFPDFInstance(FPDF_PAGEOBJECT image_object, FPDF_PAGE page) {
92     // Get bitmap.
93     bitmap_ = ScopedFPDFBitmap(FPDFImageObj_GetBitmap(image_object));
94     if (bitmap_.get() == nullptr) {
95         return false;
96     }
97 
98     // Get matrix.
99     if (!GetPageToDeviceMatrix(image_object, page)) {
100         return false;
101     }
102 
103     // Get dimensions.
104     width_ = FPDFBitmap_GetWidth(bitmap_.get());
105     height_ = FPDFBitmap_GetHeight(bitmap_.get());
106 
107     // Get bitmap format.
108     bitmap_format_ = GetBitmapFormat(FPDFBitmap_GetFormat(bitmap_.get()));
109     if (bitmap_format_ == BitmapFormat::Unknown) {
110         LOGE("Bitmap format unknown");
111         return false;
112     }
113     return true;
114 }
115 
GetBitmapBuffer() const116 void* ImageObject::GetBitmapBuffer() const {
117     return FPDFBitmap_GetBuffer(bitmap_.get());
118 }
119 
120 ImageObject::~ImageObject() = default;
121 
122 }  // namespace pdfClient