Skip to content

Commit 1db8439

Browse files
committed
Deduplicate image handling code
1 parent c948e12 commit 1db8439

File tree

1 file changed

+27
-59
lines changed

1 file changed

+27
-59
lines changed

src/render/image.rs

+27-59
Original file line numberDiff line numberDiff line change
@@ -26,75 +26,30 @@ pub fn render(
2626
return Ok(());
2727
}
2828

29+
let load_with_format = |content, format| {
30+
image::load_from_memory_with_format(content, format).map_err(|_| InvalidImage)
31+
};
32+
2933
// Will return the name of the image (in the Resources dictionary) and the dimensions of the
3034
// actual image (i.e. the actual image size, not the size in the PDF, which will always be 1x1
3135
// because that's how ImageXObjects are scaled by default.
3236
let (image_name, image_size) = match kind {
3337
ImageKind::JPEG(content) => {
34-
let dynamic_image =
35-
image::load_from_memory_with_format(content, ImageFormat::Jpeg)
36-
.map_err(|_| InvalidImage)?;
3738
// JPEGs don't support alphas, so no extra processing is required.
38-
create_raster_image(
39-
chunk,
40-
ctx,
41-
content,
42-
Filter::DctDecode,
43-
&dynamic_image,
44-
None,
45-
rc,
46-
)
39+
let image = load_with_format(content, ImageFormat::Jpeg)?;
40+
create_raster_image(chunk, ctx, content, Filter::DctDecode, &image, None, rc)
4741
}
4842
ImageKind::PNG(content) => {
49-
let dynamic_image =
50-
image::load_from_memory_with_format(content, ImageFormat::Png)
51-
.map_err(|_| InvalidImage)?;
52-
// Alpha channels need to be written separately as a soft mask, hence the extra processing
53-
// step.
54-
let (samples, filter, alpha_mask) = handle_transparent_image(&dynamic_image);
55-
create_raster_image(
56-
chunk,
57-
ctx,
58-
&samples,
59-
filter,
60-
&dynamic_image,
61-
alpha_mask.as_deref(),
62-
rc,
63-
)
43+
let image = load_with_format(content, ImageFormat::Png)?;
44+
create_transparent_image(chunk, ctx, &image, rc)
6445
}
6546
ImageKind::GIF(content) => {
66-
let dynamic_image =
67-
image::load_from_memory_with_format(content, ImageFormat::Gif)
68-
.map_err(|_| InvalidImage)?;
69-
// Alpha channels need to be written separately as a soft mask, hence the extra processing
70-
// step.
71-
let (samples, filter, alpha_mask) = handle_transparent_image(&dynamic_image);
72-
create_raster_image(
73-
chunk,
74-
ctx,
75-
&samples,
76-
filter,
77-
&dynamic_image,
78-
alpha_mask.as_deref(),
79-
rc,
80-
)
47+
let image = load_with_format(content, ImageFormat::Gif)?;
48+
create_transparent_image(chunk, ctx, &image, rc)
8149
}
8250
ImageKind::WEBP(content) => {
83-
let dynamic_image =
84-
image::load_from_memory_with_format(content, ImageFormat::WebP)
85-
.map_err(|_| InvalidImage)?;
86-
// Alpha channels need to be written separately as a soft mask, hence the extra processing
87-
// step.
88-
let (samples, filter, alpha_mask) = handle_transparent_image(&dynamic_image);
89-
create_raster_image(
90-
chunk,
91-
ctx,
92-
&samples,
93-
filter,
94-
&dynamic_image,
95-
alpha_mask.as_deref(),
96-
rc,
97-
)
51+
let image = load_with_format(content, ImageFormat::WebP)?;
52+
create_transparent_image(chunk, ctx, &image, rc)
9853
}
9954
// SVGs just get rendered recursively.
10055
ImageKind::SVG(tree) => create_svg_image(tree, chunk, ctx, rc)?,
@@ -129,7 +84,12 @@ pub fn render(
12984
Ok(())
13085
}
13186

132-
fn handle_transparent_image(image: &DynamicImage) -> (Vec<u8>, Filter, Option<Vec<u8>>) {
87+
fn create_transparent_image(
88+
chunk: &mut Chunk,
89+
ctx: &mut Context,
90+
image: &DynamicImage,
91+
rc: &mut ResourceContainer,
92+
) -> (Rc<String>, Size) {
13393
let color = image.color();
13494
let bits = color.bits_per_pixel();
13595
let channels = color.channel_count() as u16;
@@ -179,7 +139,15 @@ fn handle_transparent_image(image: &DynamicImage) -> (Vec<u8>, Filter, Option<Ve
179139
let compressed_mask =
180140
encoded_mask.map(|m| compress_to_vec_zlib(&m, compression_level));
181141

182-
(compressed_image, Filter::FlateDecode, compressed_mask)
142+
create_raster_image(
143+
chunk,
144+
ctx,
145+
&compressed_image,
146+
Filter::FlateDecode,
147+
image,
148+
compressed_mask.as_deref(),
149+
rc,
150+
)
183151
}
184152

185153
fn create_raster_image(

0 commit comments

Comments
 (0)