Allocate native PNG transparency and palette objects

This commit is contained in:
gloamxun committed 2026-11-27 21:30:31 +08:00
1 parent 51451813dc
commit 342bedf9b4
9 files changed
+215 -29

No files matched your search

+32 -8
View File
@@ -1,5 +1,5 @@
//! Decode PNG samples for native PDF image XObjects.
use super::{Part, PdfImage, RasterProcSet};
use super::{Part, PdfImage, PdfImageTargets, RasterProcSet};
use crate::{pdf::objects, resources::LoadedResource};
use ::png::{ColorType, Decoder, PixelDimensions, Transformations, Unit};
use serde::{Deserialize, Serialize};
@@ -20,6 +20,8 @@ pub(super) struct SampledImage {
pub(super) source_depth: u8,
#[serde(default)]
source_indexed: bool,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub(super) source_alpha: bool,
depth: u8,
color: ColorSpace,
samples: Vec<u8>,
@@ -37,6 +39,7 @@ pub(super) fn decode(bytes: &[u8]) -> Result<(SampledImage, Option<PixelDimensio
let [width, height] = [reader.info().width, reader.info().height];
let source_depth = reader.info().bit_depth as u8;
let source_indexed = reader.info().color_type == ColorType::Indexed;
let source_alpha = matches!(reader.info().color_type, ColorType::GrayscaleAlpha | ColorType::Rgba);
// Preserve packed grayscale and palettes unless tRNS needs an alpha channel.
if reader.info().trns.is_some() {
reader = read_info(Transformations::EXPAND)?;
@@ -94,6 +97,7 @@ pub(super) fn decode(bytes: &[u8]) -> Result<(SampledImage, Option<PixelDimensio
size: [width, height],
source_depth,
source_indexed,
source_alpha,
depth,
color,
samples: buf,
@@ -155,6 +159,7 @@ impl SampledImage {
size,
source_depth: 8,
source_indexed: false,
source_alpha: false,
depth: 8,
color: ColorSpace::Rgb,
samples,
@@ -177,6 +182,10 @@ impl SampledImage {
pdf_version >= (1, 4) && !self.alpha.is_empty()
}
pub(super) fn uses_palette(&self) -> bool {
matches!(self.color, ColorSpace::Indexed(_))
}
pub(super) fn append(
&self,
pdf: &mut Vec<Vec<u8>>,
@@ -184,14 +193,19 @@ impl SampledImage {
compression: u8,
pdf_version: (i32, i32),
color_space: Option<i64>,
target: Option<usize>,
targets: PdfImageTargets,
) -> usize {
let target = targets.image;
let id = target.unwrap_or(pdf.len() + 1);
let [width, height] = self.size;
let mut color = match (color_space, &self.color) {
(Some(id), _) => format!("{id} 0 R"),
(None, ColorSpace::Gray) => "/DeviceGray".to_owned(),
(None, ColorSpace::Rgb) => "/DeviceRGB".to_owned(),
(None, ColorSpace::Indexed(table)) if targets.png_auxiliary.is_some() => {
let palette = targets.png_auxiliary.unwrap();
format!("[/Indexed /DeviceRGB {} {palette} 0 R]", table.len() / 3 - 1)
}
(None, ColorSpace::Indexed(table)) => {
let mut color = format!("[/Indexed /DeviceRGB {} <", table.len() / 3 - 1);
for byte in table {
@@ -201,9 +215,10 @@ impl SampledImage {
color
}
};
let has_alpha = self.has_alpha(pdf_version);
if has_alpha {
let mask = pdf.len() + 1 + usize::from(target.is_none());
let mask = self.has_alpha(pdf_version).then(|| {
targets.png_auxiliary.unwrap_or(pdf.len() + 1 + usize::from(target.is_none()))
});
if let Some(mask) = mask {
write!(color, " /SMask {mask} 0 R").expect("writing to a string");
}
let strip_16 = pdf_version < (1, 5) && self.depth == 16;
@@ -228,15 +243,24 @@ impl SampledImage {
} else {
pdf.push(stream);
}
if has_alpha {
pdf.push(objects::stream(
if let Some(mask) = mask {
let stream = objects::stream(
&format!(
"/Type /XObject /Subtype /Image /Width {width} /Height {height} \
/ColorSpace /DeviceGray /BitsPerComponent 8"
),
&self.alpha,
compression,
));
);
if targets.png_auxiliary.is_some() {
pdf[mask - 1] = stream;
} else {
pdf.push(stream);
}
} else if let ColorSpace::Indexed(table) = &self.color
&& let Some(palette) = targets.png_auxiliary
{
pdf[palette - 1] = objects::stream("", table, compression);
}
id
}