Write per-page pdfTeX ProcSet from shipped resources

This commit is contained in:
gloamxun committed 2026-11-26 17:03:52 +08:00
1 parent de481fd762
commit 1fe0ac9dd5
10 files changed
+162 -15

No files matched your search

+17 -1
View File
@@ -1,5 +1,5 @@
//! Decode PNG samples for native PDF image XObjects.
use super::{Part, PdfImage};
use super::{Part, PdfImage, RasterProcSet};
use crate::{pdf::objects, resources::LoadedResource};
use ::png::{ColorType, Decoder, PixelDimensions, Transformations, Unit};
use serde::{Deserialize, Serialize};
@@ -18,6 +18,8 @@ enum ColorSpace {
pub(super) struct SampledImage {
pub(super) size: [u32; 2],
pub(super) source_depth: u8,
#[serde(default)]
source_indexed: bool,
depth: u8,
color: ColorSpace,
samples: Vec<u8>,
@@ -34,6 +36,7 @@ pub(super) fn decode(bytes: &[u8]) -> Result<(SampledImage, Option<PixelDimensio
let mut reader = read_info(Transformations::IDENTITY)?;
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;
// Preserve packed grayscale and palettes unless tRNS needs an alpha channel.
if reader.info().trns.is_some() {
reader = read_info(Transformations::EXPAND)?;
@@ -90,6 +93,7 @@ pub(super) fn decode(bytes: &[u8]) -> Result<(SampledImage, Option<PixelDimensio
SampledImage {
size: [width, height],
source_depth,
source_indexed,
depth,
color,
samples: buf,
@@ -147,6 +151,7 @@ impl SampledImage {
Self {
size,
source_depth: 8,
source_indexed: false,
depth: 8,
color: ColorSpace::Rgb,
samples,
@@ -154,6 +159,17 @@ impl SampledImage {
}
}
pub(super) fn procset(&self) -> RasterProcSet {
// Expanding a PNG transparency table may change the decoded colors.
if self.source_indexed || matches!(self.color, ColorSpace::Indexed(_)) {
RasterProcSet::Indexed
} else if matches!(self.color, ColorSpace::Gray) {
RasterProcSet::Gray
} else {
RasterProcSet::Color
}
}
pub(super) fn has_alpha(&self, pdf_version: (i32, i32)) -> bool {
pdf_version >= (1, 4) && !self.alpha.is_empty()
}