Skip to content

Commit

Permalink
Add basic error handling for images
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Nov 29, 2024
1 parent 30463c6 commit 2db891d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions crates/krilla/src/chunk_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct ChunkContainer {
pub(crate) shading_functions: Vec<Chunk>,
pub(crate) patterns: Vec<Chunk>,
pub(crate) pages: Vec<Deferred<Chunk>>,
pub(crate) images: Vec<Deferred<Chunk>>,
pub(crate) images: Vec<Deferred<KrillaResult<Chunk>>>,

pub(crate) metadata: Option<Metadata>,
}
Expand Down Expand Up @@ -149,7 +149,7 @@ impl ChunkContainer {
($remapper:expr, $pdf:expr; $($field:expr),+) => {
$(
for chunk in $field {
let chunk = chunk.wait();
let chunk = chunk.wait().res()?;
chunk.renumber_into($pdf, |old| *$remapper.get(&old).unwrap());
}
)+
Expand Down
3 changes: 3 additions & 0 deletions crates/krilla/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! invalid fonts are provided. This module provides the basic error types krilla uses.
use crate::font::Font;
use crate::image::Image;
use crate::validation::ValidationError;

/// A wrapper type for krilla errors.
Expand All @@ -22,4 +23,6 @@ pub enum KrillaError {
///
/// [`SerializeSettings`]: crate::SerializeSettings
ValidationError(Vec<ValidationError>),
/// An image couldn't be processed properly.
ImageError(Image),
}
16 changes: 13 additions & 3 deletions crates/krilla/src/object/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// TODO: CLean up and update docs

use crate::color::{ICCBasedColorSpace, ICCProfile, ICCProfileWrapper, DEVICE_CMYK, DEVICE_RGB};
use crate::error::{KrillaError, KrillaResult};
use crate::object::color::DEVICE_GRAY;
use crate::resource::RegisterableResource;
use crate::serialize::SerializerContext;
Expand Down Expand Up @@ -233,7 +234,11 @@ impl Image {
self.0.color_space()
}

pub(crate) fn serialize(self, sc: &mut SerializerContext, root_ref: Ref) -> Deferred<Chunk> {
pub(crate) fn serialize(
self,
sc: &mut SerializerContext,
root_ref: Ref,
) -> Deferred<KrillaResult<Chunk>> {
let soft_mask_id = sc.new_ref();
let icc_ref = self.icc().and_then(|ic| {
if sc
Expand All @@ -260,7 +265,12 @@ impl Image {
Deferred::new(move || {
let mut chunk = Chunk::new();

let repr = self.0.inner.wait().as_ref().unwrap();
let repr = self
.0
.inner
.wait()
.as_ref()
.ok_or(KrillaError::ImageError(self.clone()))?;

let alpha_mask = match repr {
Repr::Sampled(sampled) => sampled.mask_data.as_ref().map(|mask_data| {
Expand Down Expand Up @@ -323,7 +333,7 @@ impl Image {
}
image_x_object.finish();

chunk
Ok(chunk)
})
}
}
Expand Down

0 comments on commit 2db891d

Please sign in to comment.