Import PNG images in the native PDF device
This commit is contained in:
1 parent
297431a547
commit
555a029505
13 files changed
+467
-35
No files matched your search
@@ -1,5 +1,6 @@
|
||||
//! Native image XObjects and the resource objects reachable from their content.
|
||||
mod jpeg;
|
||||
mod png;
|
||||
use crate::resources::{LoadedResource, ResourceVersion};
|
||||
use lopdf::{Dictionary, Document, Object, ObjectId};
|
||||
use std::{
|
||||
@@ -52,6 +53,8 @@ pub struct PdfImageUse {
|
||||
pub struct PdfImageObject {
|
||||
pub image: Arc<PdfImageUse>,
|
||||
pub attributes: Vec<u8>,
|
||||
#[serde(default)]
|
||||
pub compression: u8,
|
||||
}
|
||||
|
||||
impl PdfImageUse {
|
||||
@@ -61,21 +64,24 @@ impl PdfImageUse {
|
||||
requested: [Option<i32>; 3],
|
||||
) -> Result<Self, String> {
|
||||
let [w, h] = image.natural;
|
||||
let jpeg = image.jpeg_frame().map(|(size, _)| size.map(i64::from));
|
||||
let [aspect_width, aspect_height] = jpeg.unwrap_or([w, h]);
|
||||
let raster = image.raster_frame().map(|(size, _)| size);
|
||||
let [aspect_width, aspect_height] = raster.unwrap_or([w, h]);
|
||||
let [width, height, depth] = requested.map(|value| value.map(i64::from));
|
||||
let ratio = |value: i64, numerator: i64, denominator: i64| {
|
||||
let product = value * numerator;
|
||||
product.signum() * ((product.abs() + denominator / 2) / denominator)
|
||||
let ratio = |value: i64, numerator: i64, denominator: i64| -> Result<i64, String> {
|
||||
let product = i128::from(value) * i128::from(numerator);
|
||||
let denominator = i128::from(denominator);
|
||||
let rounded = product.signum() * ((product.abs() + denominator / 2) / denominator);
|
||||
i64::try_from(rounded)
|
||||
.map_err(|_| "native PDF image dimension capacity exceeded".into())
|
||||
};
|
||||
let depth = depth.unwrap_or(0);
|
||||
let (width, height) = match (width, height) {
|
||||
(None, None) if jpeg.is_some() && requested[2].is_some() => {
|
||||
(ratio(h, aspect_width, aspect_height), h - depth)
|
||||
(None, None) if raster.is_some() && requested[2].is_some() => {
|
||||
(ratio(h, aspect_width, aspect_height)?, h - depth)
|
||||
}
|
||||
(None, None) => (w, h - depth),
|
||||
(Some(width), None) => (width, ratio(width, aspect_height, aspect_width) - depth),
|
||||
(None, Some(height)) => (ratio(height + depth, aspect_width, aspect_height), height),
|
||||
(Some(width), None) => (width, ratio(width, aspect_height, aspect_width)? - depth),
|
||||
(None, Some(height)) => (ratio(height + depth, aspect_width, aspect_height)?, height),
|
||||
(Some(width), Some(height)) => (width, height),
|
||||
};
|
||||
if [width, height, depth].iter().any(|v| v.abs() > 1073741823) {
|
||||
@@ -104,6 +110,7 @@ enum Part {
|
||||
precision: u8,
|
||||
bytes: Vec<u8>,
|
||||
},
|
||||
Png(png::PngImage),
|
||||
}
|
||||
|
||||
pub(crate) fn real(value: f64) -> String {
|
||||
@@ -186,15 +193,38 @@ impl PdfImage {
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn jpeg_frame(&self) -> Option<([u16; 2], u8)> {
|
||||
pub(crate) fn is_png(resource: &LoadedResource) -> bool {
|
||||
resource.data.starts_with(png::SIGNATURE)
|
||||
}
|
||||
|
||||
pub(crate) fn read_png(
|
||||
resource: LoadedResource,
|
||||
fallback_resolution: i32,
|
||||
) -> Result<(Self, Option<&'static str>), String> {
|
||||
png::read(resource, fallback_resolution)
|
||||
}
|
||||
|
||||
pub(crate) fn raster_frame(&self) -> Option<([i64; 2], u8)> {
|
||||
self.objects.first()?.iter().find_map(|part| match part {
|
||||
Part::Jpeg {
|
||||
size, precision, ..
|
||||
} => Some((*size, *precision)),
|
||||
} => Some((size.map(i64::from), *precision)),
|
||||
Part::Png(png) => Some((png.size.map(i64::from), png.source_depth)),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn png(&self) -> Option<&png::PngImage> {
|
||||
match self.objects.first()?.as_slice() {
|
||||
[Part::Png(png)] => Some(png),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn png_transparency(&self, pdf_version: (i32, i32)) -> bool {
|
||||
self.png().is_some_and(|png| png.has_alpha(pdf_version))
|
||||
}
|
||||
|
||||
pub(crate) fn read(
|
||||
resource: LoadedResource,
|
||||
page: usize,
|
||||
@@ -324,7 +354,16 @@ impl PdfImage {
|
||||
self.extent.map(f64::from_bits)
|
||||
}
|
||||
|
||||
pub(crate) fn append_to(&self, pdf: &mut Vec<Vec<u8>>, attributes: &[u8]) -> usize {
|
||||
pub(crate) fn append_to(
|
||||
&self,
|
||||
pdf: &mut Vec<Vec<u8>>,
|
||||
attributes: &[u8],
|
||||
compression: u8,
|
||||
pdf_version: (i32, i32),
|
||||
) -> usize {
|
||||
if let Some(png) = self.png() {
|
||||
return png.append(pdf, attributes, compression, pdf_version);
|
||||
}
|
||||
let base = pdf.len() + 1;
|
||||
pdf.extend(self.objects.iter().enumerate().map(|(index, parts)| {
|
||||
let mut body = Vec::new();
|
||||
@@ -339,6 +378,7 @@ impl PdfImage {
|
||||
match part {
|
||||
Part::Bytes(bytes) | Part::Jpeg { bytes, .. } => body.extend_from_slice(bytes),
|
||||
Part::Ref(id) => write!(body, "{} 0 R", base + id).unwrap(),
|
||||
Part::Png(_) => unreachable!("PNG owns its image objects"),
|
||||
}
|
||||
}
|
||||
body
|
||||
|
||||
Reference in new issue
Block a user