@@ -26,75 +26,30 @@ pub fn render(
26
26
return Ok ( ( ) ) ;
27
27
}
28
28
29
+ let load_with_format = |content, format| {
30
+ image:: load_from_memory_with_format ( content, format) . map_err ( |_| InvalidImage )
31
+ } ;
32
+
29
33
// Will return the name of the image (in the Resources dictionary) and the dimensions of the
30
34
// actual image (i.e. the actual image size, not the size in the PDF, which will always be 1x1
31
35
// because that's how ImageXObjects are scaled by default.
32
36
let ( image_name, image_size) = match kind {
33
37
ImageKind :: JPEG ( content) => {
34
- let dynamic_image =
35
- image:: load_from_memory_with_format ( content, ImageFormat :: Jpeg )
36
- . map_err ( |_| InvalidImage ) ?;
37
38
// 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)
47
41
}
48
42
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)
64
45
}
65
46
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)
81
49
}
82
50
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)
98
53
}
99
54
// SVGs just get rendered recursively.
100
55
ImageKind :: SVG ( tree) => create_svg_image ( tree, chunk, ctx, rc) ?,
@@ -129,7 +84,12 @@ pub fn render(
129
84
Ok ( ( ) )
130
85
}
131
86
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 ) {
133
93
let color = image. color ( ) ;
134
94
let bits = color. bits_per_pixel ( ) ;
135
95
let channels = color. channel_count ( ) as u16 ;
@@ -179,7 +139,15 @@ fn handle_transparent_image(image: &DynamicImage) -> (Vec<u8>, Filter, Option<Ve
179
139
let compressed_mask =
180
140
encoded_mask. map ( |m| compress_to_vec_zlib ( & m, compression_level) ) ;
181
141
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
+ )
183
151
}
184
152
185
153
fn create_raster_image (
0 commit comments