1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::bindgen::{
FPDFBitmap_BGR, FPDFBitmap_BGRA, FPDFBitmap_BGRx, FPDFBitmap_Gray, FPDFBitmap_Unknown,
FPDF_BITMAP,
};
use crate::bindings::PdfiumLibraryBindings;
use crate::error::{PdfiumError, PdfiumInternalError};
use image::{DynamicImage, ImageBuffer};
use std::f32::consts::{FRAC_PI_2, PI};
use std::os::raw::c_int;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::{Clamped, JsValue};
#[cfg(target_arch = "wasm32")]
use web_sys::ImageData;
#[cfg(target_arch = "wasm32")]
use js_sys::Uint8Array;
pub type Pixels = u16;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum PdfBitmapFormat {
Gray = FPDFBitmap_Gray as isize,
BGR = FPDFBitmap_BGR as isize,
BRGx = FPDFBitmap_BGRx as isize,
BGRA = FPDFBitmap_BGRA as isize,
}
impl PdfBitmapFormat {
#[inline]
#[allow(non_upper_case_globals)]
pub(crate) fn from_pdfium(format: u32) -> Result<Self, PdfiumError> {
match format {
FPDFBitmap_Unknown => Err(PdfiumError::UnknownBitmapFormat),
FPDFBitmap_Gray => Ok(PdfBitmapFormat::Gray),
FPDFBitmap_BGR => Ok(PdfBitmapFormat::BGR),
FPDFBitmap_BGRx => Ok(PdfBitmapFormat::BRGx),
FPDFBitmap_BGRA => Ok(PdfBitmapFormat::BGRA),
_ => Err(PdfiumError::UnknownBitmapFormat),
}
}
#[inline]
pub(crate) fn as_pdfium(&self) -> u32 {
match self {
PdfBitmapFormat::Gray => FPDFBitmap_Gray,
PdfBitmapFormat::BGR => FPDFBitmap_BGR,
PdfBitmapFormat::BRGx => FPDFBitmap_BGRx,
PdfBitmapFormat::BGRA => FPDFBitmap_BGRA,
}
}
}
impl Default for PdfBitmapFormat {
#[inline]
fn default() -> Self {
PdfBitmapFormat::BGRA
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum PdfBitmapRotation {
None,
Degrees90,
Degrees180,
Degrees270,
}
impl PdfBitmapRotation {
#[inline]
pub(crate) fn from_pdfium(rotate: i32) -> Result<Self, PdfiumError> {
match rotate {
0 => Ok(PdfBitmapRotation::None),
1 => Ok(PdfBitmapRotation::Degrees90),
2 => Ok(PdfBitmapRotation::Degrees180),
3 => Ok(PdfBitmapRotation::Degrees270),
_ => Err(PdfiumError::UnknownBitmapRotation),
}
}
#[inline]
pub(crate) fn as_pdfium(&self) -> i32 {
match self {
PdfBitmapRotation::None => 0,
PdfBitmapRotation::Degrees90 => 1,
PdfBitmapRotation::Degrees180 => 2,
PdfBitmapRotation::Degrees270 => 3,
}
}
#[inline]
pub const fn as_degrees(&self) -> f32 {
match self {
PdfBitmapRotation::None => 0.0,
PdfBitmapRotation::Degrees90 => 90.0,
PdfBitmapRotation::Degrees180 => 180.0,
PdfBitmapRotation::Degrees270 => 270.0,
}
}
pub const DEGREES_90_AS_RADIANS: f32 = FRAC_PI_2;
pub const DEGREES_180_AS_RADIANS: f32 = PI;
pub const DEGREES_270_AS_RADIANS: f32 = FRAC_PI_2 + PI;
#[inline]
pub const fn as_radians(&self) -> f32 {
match self {
PdfBitmapRotation::None => 0.0,
PdfBitmapRotation::Degrees90 => Self::DEGREES_90_AS_RADIANS,
PdfBitmapRotation::Degrees180 => Self::DEGREES_180_AS_RADIANS,
PdfBitmapRotation::Degrees270 => Self::DEGREES_270_AS_RADIANS,
}
}
#[inline]
pub const fn as_radians_cos(&self) -> f32 {
match self {
PdfBitmapRotation::None => 1.0,
PdfBitmapRotation::Degrees90 => 0.0,
PdfBitmapRotation::Degrees180 => -1.0,
PdfBitmapRotation::Degrees270 => 0.0,
}
}
#[inline]
pub const fn as_radians_sin(&self) -> f32 {
match self {
PdfBitmapRotation::None => 0.0,
PdfBitmapRotation::Degrees90 => 1.0,
PdfBitmapRotation::Degrees180 => 0.0,
PdfBitmapRotation::Degrees270 => -1.0,
}
}
}
pub struct PdfBitmap<'a> {
handle: FPDF_BITMAP,
bindings: &'a dyn PdfiumLibraryBindings,
}
impl<'a> PdfBitmap<'a> {
pub(crate) fn from_pdfium(
handle: FPDF_BITMAP,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Self {
PdfBitmap { handle, bindings }
}
pub fn empty(
width: Pixels,
height: Pixels,
format: PdfBitmapFormat,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Result<PdfBitmap, PdfiumError> {
let handle = bindings.FPDFBitmap_CreateEx(
width as c_int,
height as c_int,
format.as_pdfium() as c_int,
std::ptr::null_mut(),
0, );
if handle.is_null() {
if let Some(error) = bindings.get_pdfium_last_error() {
Err(PdfiumError::PdfiumLibraryInternalError(error))
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Ok(Self::from_pdfium(handle, bindings))
}
}
#[inline]
pub(crate) fn handle(&self) -> &FPDF_BITMAP {
&self.handle
}
#[inline]
pub fn bindings(&self) -> &dyn PdfiumLibraryBindings {
self.bindings
}
#[inline]
pub fn width(&self) -> Pixels {
self.bindings.FPDFBitmap_GetWidth(self.handle) as Pixels
}
#[inline]
pub fn height(&self) -> Pixels {
self.bindings.FPDFBitmap_GetHeight(self.handle) as Pixels
}
#[inline]
pub fn format(&self) -> Result<PdfBitmapFormat, PdfiumError> {
PdfBitmapFormat::from_pdfium(self.bindings.FPDFBitmap_GetFormat(self.handle) as u32)
}
pub fn as_bytes(&mut self) -> &'a [u8] {
let buffer_length = self.bindings.FPDFBitmap_GetStride(self.handle)
* self.bindings.FPDFBitmap_GetHeight(self.handle);
let buffer_start = self.bindings.FPDFBitmap_GetBuffer(self.handle);
unsafe { std::slice::from_raw_parts(buffer_start as *const u8, buffer_length as usize) }
}
pub fn as_image(&mut self) -> DynamicImage {
ImageBuffer::from_raw(
self.width() as u32,
self.height() as u32,
self.as_bytes().to_owned(),
)
.map(DynamicImage::ImageRgba8)
.unwrap()
}
#[deprecated(
since = "0.7.12",
note = "This function is no longer necessary since all page rendering operations are now processed eagerly rather than lazily. Calls to this function can be removed."
)]
#[doc(hidden)]
#[inline]
pub fn render(&mut self) {}
#[cfg(target_arch = "wasm32")]
pub fn as_array(&mut self) -> Uint8Array {
self.bindings.FPDFBitmap_GetArray(self.handle)
}
#[cfg(target_arch = "wasm32")]
pub fn as_image_data(&mut self) -> Result<ImageData, JsValue> {
ImageData::new_with_u8_clamped_array_and_sh(
Clamped(self.as_bytes()),
self.width() as u32,
self.height() as u32,
)
}
}
impl<'a> Drop for PdfBitmap<'a> {
#[inline]
fn drop(&mut self) {
self.bindings.FPDFBitmap_Destroy(self.handle);
}
}