Skip to content

Commit

Permalink
Progressing #47
Browse files Browse the repository at this point in the history
  • Loading branch information
Alastair Carey committed Oct 29, 2022
1 parent 6e87a81 commit 24f3579
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 56 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ doctest = false
[dependencies]
bitflags = "^1"
bytes = "^1"
image = ">= 0.24.0" # DynamicImage trait definitions changed between 0.23.14 and 0.24.0; we expect at least version 0.24.0
image = ">= 0.24.0" # DynamicImage trait definitions changed between 0.23.14 and 0.24.0; we use trait from version 0.24.0 and later.
iter_tools = "^0.1"
lazy_static = "^1"
log = "^0.4"
Expand All @@ -31,7 +31,7 @@ vecmath = "^1"
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_log = "^0.2"
console_error_panic_hook = "^0.1"
js-sys = "^0.3.60"
js-sys = "^0.3"
wasm-bindgen = { version = "^0.2", features = ["enable-interning"] }
wasm-bindgen-futures = { version = "^0.4" }
web-sys = { version = "^0.3", features = ["TextDecoder", "ImageData", "Window", "Response", "Blob"] }
Expand All @@ -43,9 +43,8 @@ libloading = "^0.7"
bindgen = { version = "^0" }

[dev-dependencies]
# Dependencies specific to examples.
# Note that dependencies for the WASM example in examples/wasm.rs are specified separately
# in examples/Cargo.toml.
# Dependencies specific to examples. Dependencies for the WASM example in examples/wasm.rs
# are specified separately in examples/Cargo.toml.
rand = "^0" # Used by examples/create.rs
rayon = "^1" # Used by examples/thread_safe.rs

Expand Down
2 changes: 1 addition & 1 deletion examples/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() -> Result<(), PdfiumError> {

let mut object = PdfPageImageObject::new_with_width(
&document,
image,
&image,
PdfPoints::new(target_object_width_on_page),
)?;

Expand Down
79 changes: 30 additions & 49 deletions src/page_object_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ impl<'a> PdfPageImageObject<'a> {
/// [PdfPageImageObject::new_with_height()], or [PdfPageImageObject::new_with_size()] functions
/// to scale the page object to a specific width and/or height at the time the object is created.
#[inline]
pub fn new(document: &PdfDocument<'a>, image: DynamicImage) -> Result<Self, PdfiumError> {
pub fn new(document: &PdfDocument<'a>, image: &DynamicImage) -> Result<Self, PdfiumError> {
Self::new_from_handle(*document.handle(), image, document.bindings())
}

// Takes a raw FPDF_DOCUMENT handle to avoid cascading lifetime problems
// associated with borrowing PdfDocument<'a>.
pub(crate) fn new_from_handle(
document: FPDF_DOCUMENT,
image: DynamicImage,
image: &DynamicImage,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Result<Self, PdfiumError> {
let handle = bindings.FPDFPageObj_NewImageObj(document);
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<'a> PdfPageImageObject<'a> {
/// added to a `PdfPage` using the `PdfPageObjects::add_image_object()` function.
pub fn new_with_width(
document: &PdfDocument<'a>,
image: DynamicImage,
image: &DynamicImage,
width: PdfPoints,
) -> Result<Self, PdfiumError> {
let aspect_ratio = image.height() as f32 / image.width() as f32;
Expand All @@ -124,7 +124,7 @@ impl<'a> PdfPageImageObject<'a> {
/// added to a `PdfPage` using the `PdfPageObjects::add_image_object()` function.
pub fn new_with_height(
document: &PdfDocument<'a>,
image: DynamicImage,
image: &DynamicImage,
height: PdfPoints,
) -> Result<Self, PdfiumError> {
let aspect_ratio = image.height() as f32 / image.width() as f32;
Expand All @@ -140,7 +140,7 @@ impl<'a> PdfPageImageObject<'a> {
#[inline]
pub fn new_with_size(
document: &PdfDocument<'a>,
image: DynamicImage,
image: &DynamicImage,
width: PdfPoints,
height: PdfPoints,
) -> Result<Self, PdfiumError> {
Expand Down Expand Up @@ -399,60 +399,41 @@ impl<'a> PdfPageImageObject<'a> {
}

/// Applies the byte data in the given `Image::DynamicImage` to this [PdfPageImageObject].
pub fn set_image(&mut self, image: DynamicImage) -> Result<(), PdfiumError> {
if let Some(image) = image.as_rgba8() {
// The given image is already in RGBA format.

let width: u16 = image
.width()
.try_into()
.map_err(|_| PdfiumError::ImageSizeOutOfBounds)?;
pub fn set_image(&mut self, image: &DynamicImage) -> Result<(), PdfiumError> {
let width: u16 = image
.width()
.try_into()
.map_err(|_| PdfiumError::ImageSizeOutOfBounds)?;

let height: u16 = image
.height()
.try_into()
.map_err(|_| PdfiumError::ImageSizeOutOfBounds)?;
let height: u16 = image
.height()
.try_into()
.map_err(|_| PdfiumError::ImageSizeOutOfBounds)?;

let bitmap = PdfBitmap::empty(width, height, PdfBitmapFormat::BGRA, self.bindings)?;
let bitmap = PdfBitmap::empty(width, height, PdfBitmapFormat::BGRA, self.bindings)?;

if !self
.bindings
.FPDFBitmap_SetBuffer(*bitmap.handle(), rgba_to_bgra(image.as_bytes()).as_slice())
{
return Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
));
}
let buffer = if let Some(image) = image.as_rgba8() {
// The given image is already in RGBA format.

self.set_bitmap(&bitmap)
rgba_to_bgra(image.as_bytes())
} else {
// The image must be converted to RGBA first.

let image = image.to_rgba8();

let width: u16 = image
.width()
.try_into()
.map_err(|_| PdfiumError::ImageSizeOutOfBounds)?;

let height: u16 = image
.height()
.try_into()
.map_err(|_| PdfiumError::ImageSizeOutOfBounds)?;

let bitmap = PdfBitmap::empty(width, height, PdfBitmapFormat::BGRA, self.bindings)?;

if !self
.bindings
.FPDFBitmap_SetBuffer(*bitmap.handle(), rgba_to_bgra(image.as_bytes()).as_slice())
{
return Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
));
}
rgba_to_bgra(image.as_bytes())
};

self.set_bitmap(&bitmap)
if !self
.bindings
.FPDFBitmap_SetBuffer(*bitmap.handle(), buffer.as_slice())
{
return Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
));
}

self.set_bitmap(&bitmap)
}

/// Applies the byte data in the given [PdfBitmap] to this [PdfPageImageObject].
Expand Down Expand Up @@ -749,7 +730,7 @@ pub mod tests {
let object = page.objects_mut().create_image_object(
PdfPoints::new(100.0),
PdfPoints::new(100.0),
image.clone(),
&image,
Some(PdfPoints::new(image.width() as f32)),
Some(PdfPoints::new(image.height() as f32)),
)?;
Expand Down
2 changes: 1 addition & 1 deletion src/page_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl<'a> PdfPageObjects<'a> {
&mut self,
x: PdfPoints,
y: PdfPoints,
image: DynamicImage,
image: &DynamicImage,
width: Option<PdfPoints>,
height: Option<PdfPoints>,
) -> Result<PdfPageObject<'a>, PdfiumError> {
Expand Down

0 comments on commit 24f3579

Please sign in to comment.