use pdf2::{Dictionary, Document, Object, Stream};
use crate::canvas::{BlendMode, Canvas, ClipMask, Color};
use crate::geom::{Matrix, Point};
use crate::{Error, Result};
#[derive(Debug, Clone)]
pub struct Image {
width: u32,
height: u32,
pixels: Vec<Color>,
interpolate: bool,
masked: Option<Vec<bool>>,
}
impl Image {
pub fn from_stream(
document: &Document,
stream: &Stream,
id: Option<pdf2::ObjectId>,
) -> Result<Self> {
let dict = stream.dict();
let width = integer(dict, b"Width")?;
let height = integer(dict, b"Height")?;
if width == 0 || height == 0 {
return Err(Error::malformed("image with zero width or height"));
}
let bits = dict
.get(b"BitsPerComponent")
.and_then(Object::as_integer)
.unwrap_or(8);
if bits != 8 && bits != 1 {
return Err(Error::malformed(
"only 1 and 8 bits per component images are rendered",
));
}
let interpolate = matches!(dict.get(b"Interpolate"), Some(Object::Boolean(true)));
let space = resolve(document, dict.get(b"ColorSpace"))?;
let colours = ColourSpace::from_object(document, space)?;
let per_pixel = colours.components();
let count = (width as usize)
.checked_mul(height as usize)
.and_then(|pixels| pixels.checked_mul(per_pixel))
.ok_or_else(|| Error::malformed("image dimensions overflow"))?;
let limit = document.decoded_limit();
if count > limit {
return Err(Error::Limit {
what: "an image of the /Width and /Height it declares",
limit,
});
}
let key = match dict.get(b"Mask") {
None => None,
Some(Object::Array(items)) => Some(ColourKey::parse(items, per_pixel, bits)?),
Some(Object::Reference(_)) => {
return Err(Error::Unsupported {
what: "a /Mask image stencil",
});
}
Some(_) => {
return Err(Error::malformed(
"a /Mask that is neither an array nor a stream",
));
}
};
if key.is_some() && is_dct(dict) {
return Err(Error::Unsupported {
what: "colour-key /Mask on a /DCTDecode image",
});
}
if is_dct(dict) {
return Self::from_dct(document, stream, id, interpolate);
}
let decoded = document.decode_stream(stream)?;
let data = if bits == 1 {
widen_one_bit(&decoded, width, height, per_pixel)?
} else {
decoded
};
if data.len() < count {
return Err(Error::Malformed {
what: "image data is shorter than width × height × components",
offset: None,
});
}
let masked = key.map(|key| {
data.chunks_exact(per_pixel)
.map(|sample| key.masks(sample))
.collect::<Vec<bool>>()
});
let decode = decode_ranges(dict.get(b"Decode"), per_pixel);
let mut pixels = Vec::with_capacity((width as usize) * (height as usize));
for index in (0..count).step_by(per_pixel) {
let sample = &data[index..index + per_pixel];
let mut components = [0.0_f64; 4];
for (slot, byte) in sample.iter().enumerate().take(per_pixel) {
let (from, to) = decode[slot];
components[slot] = (from + f64::from(*byte) / 255.0 * (to - from)).clamp(0.0, 1.0);
}
pixels.push(colour_at(&colours, &components));
}
Ok(Self {
width,
height,
pixels,
interpolate,
masked,
})
}
#[cfg(feature = "dct")]
fn from_dct(
document: &Document,
_stream: &Stream,
id: Option<pdf2::ObjectId>,
interpolate: bool,
) -> Result<Self> {
let Some(id) = id else {
return Err(Error::malformed(
"a /DCTDecode image without an object number",
));
};
let data = document.stream_bytes(id)?;
let cmyk = dict_component_count(_stream) == Some(4);
let mut decoder =
zune_jpeg::JpegDecoder::new(zune_jpeg::zune_core::bytestream::ZCursor::new(&data));
if cmyk {
decoder.set_options(
zune_jpeg::zune_core::options::DecoderOptions::default()
.jpeg_set_out_colorspace(zune_jpeg::zune_core::colorspace::ColorSpace::CMYK),
);
}
let pixels = decoder
.decode()
.map_err(|_| Error::malformed("/DCTDecode payload"))?;
let Some(info) = decoder.info() else {
return Err(Error::malformed(
"/DCTDecode payload without a frame header",
));
};
let components = match decoder.output_colorspace() {
Some(zune_jpeg::zune_core::colorspace::ColorSpace::Luma) => 1,
Some(zune_jpeg::zune_core::colorspace::ColorSpace::CMYK) => 4,
_ => 3,
};
let count = usize::from(info.width)
.checked_mul(usize::from(info.height))
.and_then(|samples| samples.checked_mul(components))
.ok_or_else(|| Error::malformed("image dimensions overflow"))?;
let limit = document.decoded_limit();
if count > limit {
return Err(Error::Limit {
what: "a /DCTDecode image of the size its header declares",
limit,
});
}
if pixels.len() < count {
return Err(Error::malformed(
"/DCTDecode payload is shorter than its header",
));
}
let stated_decode = _stream.dict().get(b"Decode").is_some();
let invert = cmyk && !stated_decode && has_adobe_marker(&data);
let decode = decode_ranges(_stream.dict().get(b"Decode"), components);
let sample = |bytes: &[u8]| -> Vec<f64> {
bytes
.iter()
.enumerate()
.map(|(index, byte)| {
let value = f64::from(*byte) / 255.0;
let (from, to) = decode[index.min(decode.len() - 1)];
(from + value * (to - from)).clamp(0.0, 1.0)
})
.collect()
};
let pixels = pixels[..count]
.chunks_exact(components)
.map(|bytes| {
let bytes: Vec<u8> = if invert {
bytes.iter().map(|byte| 255 - byte).collect()
} else {
bytes.to_vec()
};
let values = sample(&bytes);
match components {
1 => Color::rgb(values[0], values[0], values[0]),
4 => Color::from_cmyk(values[0], values[1], values[2], values[3]),
_ => Color::rgb(values[0], values[1], values[2]),
}
})
.collect();
Ok(Self {
width: u32::from(info.width),
height: u32::from(info.height),
pixels,
interpolate,
masked: None,
})
}
#[cfg(not(feature = "dct"))]
fn from_dct(
_document: &Document,
_stream: &Stream,
_id: Option<pdf2::ObjectId>,
_interpolate: bool,
) -> Result<Self> {
Err(Error::Unsupported {
what: "/DCTDecode images (build with the `dct` feature)",
})
}
fn is_masked(&self, u: f64, v: f64) -> bool {
let Some(masked) = &self.masked else {
return false;
};
if self.width == 0 || self.height == 0 {
return false;
}
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let x = (u.clamp(0.0, 1.0) * f64::from(self.width)) as u32;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let y = (v.clamp(0.0, 1.0) * f64::from(self.height)) as u32;
let x = x.min(self.width - 1);
let y = y.min(self.height - 1);
masked
.get((y * self.width + x) as usize)
.copied()
.unwrap_or(false)
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn sample(&self, u: f64, v: f64) -> Color {
if self.width == 0 || self.height == 0 {
return Color::WHITE;
}
let x = (u.clamp(0.0, 1.0) * f64::from(self.width) - 0.5).max(0.0);
let y = (v.clamp(0.0, 1.0) * f64::from(self.height) - 0.5).max(0.0);
if !self.interpolate {
return self.pixel(x.round() as u32, y.round() as u32);
}
let x0 = x.floor() as u32;
let y0 = y.floor() as u32;
let x1 = (x0 + 1).min(self.width - 1);
let y1 = (y0 + 1).min(self.height - 1);
let fx = x - f64::from(x0);
let fy = y - f64::from(y0);
let top = mix(self.pixel(x0, y0), self.pixel(x1, y0), fx);
let bottom = mix(self.pixel(x0, y1), self.pixel(x1, y1), fx);
mix(top, bottom, fy)
}
fn pixel(&self, x: u32, y: u32) -> Color {
let x = x.min(self.width - 1);
let y = y.min(self.height - 1);
self.pixels[(y as usize) * (self.width as usize) + (x as usize)]
}
pub fn paint(
&self,
canvas: &mut Canvas,
mask: &ClipMask,
ctm: Matrix,
alpha: f64,
mode: BlendMode,
) {
let Some(inverse) = ctm.invert() else {
return;
};
let corners = [
ctm.apply(Point::new(0.0, 0.0)),
ctm.apply(Point::new(1.0, 0.0)),
ctm.apply(Point::new(0.0, 1.0)),
ctm.apply(Point::new(1.0, 1.0)),
];
let min_x = corners
.iter()
.fold(f64::INFINITY, |min, point| min.min(point.x))
.floor()
.max(0.0);
let max_x = corners
.iter()
.fold(f64::NEG_INFINITY, |max, point| max.max(point.x))
.ceil()
.min(f64::from(canvas.width()));
let min_y = corners
.iter()
.fold(f64::INFINITY, |min, point| min.min(point.y))
.floor()
.max(0.0);
let max_y = corners
.iter()
.fold(f64::NEG_INFINITY, |max, point| max.max(point.y))
.ceil()
.min(f64::from(canvas.height()));
for y in min_y as u32..max_y as u32 {
for x in min_x as u32..max_x as u32 {
let coverage = mask.alpha(x, y);
if coverage <= 0.0 {
continue;
}
let device = Point::new(f64::from(x) + 0.5, f64::from(y) + 0.5);
let user = inverse.apply(device);
if !(0.0..=1.0).contains(&user.x) || !(0.0..=1.0).contains(&user.y) {
continue;
}
let v = 1.0 - user.y;
if self.is_masked(user.x, v) {
continue;
}
let colour = self.sample(user.x, v);
canvas.blend_with(x, y, colour, alpha * coverage, mode);
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct ColourKey {
min: [u8; 4],
max: [u8; 4],
components: usize,
}
impl ColourKey {
fn parse(items: &[Object], components: usize, bits: i64) -> Result<Self> {
if items.len() != components * 2 {
return Err(Error::malformed(
"a /Mask whose length is not twice the image's component count",
));
}
let scale = if bits == 1 { 255.0 } else { 1.0 };
let limit = if bits == 1 { 1.0 } else { 255.0 };
let mut min = [0_u8; 4];
let mut max = [0_u8; 4];
for slot in 0..components {
let low = items[slot * 2].as_real().unwrap_or(-1.0);
let high = items[slot * 2 + 1].as_real().unwrap_or(-1.0);
if !(0.0..=limit).contains(&low) || !(0.0..=limit).contains(&high) {
return Err(Error::malformed("a /Mask value outside the sample range"));
}
min[slot] = (low * scale).round() as u8;
max[slot] = (high * scale).round() as u8;
}
Ok(Self {
min,
max,
components,
})
}
fn masks(&self, sample: &[u8]) -> bool {
sample
.iter()
.take(self.components)
.enumerate()
.all(|(slot, byte)| (self.min[slot]..=self.max[slot]).contains(byte))
}
}
fn decode_ranges(decode: Option<&Object>, components: usize) -> Vec<(f64, f64)> {
let mut ranges = vec![(0.0, 1.0); components];
let Some(Object::Array(items)) = decode else {
return ranges;
};
if items.len() < components * 2 {
return ranges;
}
for (slot, pair) in items.as_chunks::<2>().0.iter().enumerate().take(components) {
let (Some(from), Some(to)) = (pair[0].as_real(), pair[1].as_real()) else {
continue;
};
ranges[slot] = (from, to);
}
ranges
}
fn colour_at(colours: &ColourSpace, components: &[f64; 4]) -> Color {
match colours {
ColourSpace::Gray => Color::rgb(components[0], components[0], components[0]),
ColourSpace::Rgb => Color::rgb(components[0], components[1], components[2]),
ColourSpace::Cmyk => {
Color::from_cmyk(components[0], components[1], components[2], components[3])
}
ColourSpace::Indexed { base, lookup } => {
let index = (components[0] * 255.0).round().max(0.0) as usize;
let count = base.components();
let entry = index * count;
let slice = lookup.get(entry..entry + count).unwrap_or(&[]);
if slice.len() < count {
return Color::BLACK;
}
let mut table = [0.0_f64; 4];
for (slot, byte) in slice.iter().enumerate().take(4) {
table[slot] = f64::from(*byte) / 255.0;
}
colour_at(base, &table)
}
}
}
fn mix(left: Color, right: Color, t: f64) -> Color {
let t = t.clamp(0.0, 1.0);
Color::rgb(
left.r * (1.0 - t) + right.r * t,
left.g * (1.0 - t) + right.g * t,
left.b * (1.0 - t) + right.b * t,
)
}
#[derive(Debug, Clone, PartialEq)]
enum ColourSpace {
Gray,
Rgb,
Cmyk,
Indexed {
base: Box<ColourSpace>,
lookup: Vec<u8>,
},
}
impl ColourSpace {
fn from_object(document: &Document, object: Object) -> Result<Self> {
match object {
Object::Name(name) => match name.as_slice() {
b"DeviceGray" | b"G" | b"CalGray" => Ok(Self::Gray),
b"DeviceRGB" | b"RGB" | b"CalRGB" => Ok(Self::Rgb),
b"DeviceCMYK" | b"CMYK" => Ok(Self::Cmyk),
_ => Err(Error::malformed("unsupported image /ColorSpace")),
},
Object::Array(items) => {
let family = match items.first() {
Some(Object::Name(name)) => name.clone(),
_ => return Err(Error::malformed("image /ColorSpace array family")),
};
match family.as_slice() {
b"Indexed" | b"I" => {
let base = match items.get(1) {
Some(base) => {
Self::from_object(document, resolve(document, Some(base))?)?
}
None => return Err(Error::malformed("Indexed image without a base")),
};
let lookup = match items.get(3) {
Some(Object::String(string)) => string.as_bytes().to_vec(),
Some(Object::Reference(id)) => document.stream_data(*id)?,
_ => {
return Err(Error::malformed(
"Indexed image without a lookup table",
));
}
};
Ok(Self::Indexed {
base: Box::new(base),
lookup,
})
}
b"ICCBased" => {
let stream = match items.get(1) {
Some(Object::Reference(id)) => document.stream(*id)?,
_ => return Err(Error::malformed("ICCBased without a stream")),
};
let components = stream
.dict()
.get(b"N")
.and_then(Object::as_integer)
.unwrap_or(3);
match components {
1 => Ok(Self::Gray),
3 => Ok(Self::Rgb),
4 => Ok(Self::Cmyk),
_ => Err(Error::malformed("unsupported ICCBased components")),
}
}
_ => Err(Error::malformed("unsupported image /ColorSpace array")),
}
}
_ => Err(Error::malformed("unsupported image /ColorSpace value")),
}
}
fn components(&self) -> usize {
match self {
Self::Gray => 1,
Self::Rgb => 3,
Self::Cmyk => 4,
Self::Indexed { .. } => 1,
}
}
}
fn integer(dict: &Dictionary, key: &[u8]) -> Result<u32> {
dict.get(key)
.and_then(Object::as_integer)
.and_then(|value| u32::try_from(value).ok())
.ok_or_else(|| Error::malformed("image without integer /Width and /Height"))
}
fn dict_component_count(stream: &Stream) -> Option<usize> {
match stream.dict().get(b"ColorSpace") {
Some(Object::Name(name)) if name == b"DeviceCMYK" => Some(4),
Some(Object::Name(name)) if name == b"DeviceGray" => Some(1),
Some(Object::Name(name)) if name == b"DeviceRGB" => Some(3),
_ => None,
}
}
fn widen_one_bit(data: &[u8], width: u32, height: u32, components: usize) -> Result<Vec<u8>> {
if components != 1 {
return Err(Error::malformed(
"one-bit samples need a single-component colour space",
));
}
let bits_per_row = usize::try_from(width).unwrap_or(0);
let stride = bits_per_row.div_ceil(8);
let rows = usize::try_from(height).unwrap_or(0);
if data.len() < stride.saturating_mul(rows) {
return Err(Error::malformed(
"one-bit image data is shorter than its rows",
));
}
let mut out = Vec::with_capacity(bits_per_row.saturating_mul(rows));
for row in 0..rows {
for column in 0..bits_per_row {
let byte = data[row * stride + column / 8];
let shift = 7 - column % 8;
let bit = (byte >> shift) & 1;
out.push(if bit == 1 { 255 } else { 0 });
}
}
Ok(out)
}
fn has_adobe_marker(data: &[u8]) -> bool {
let mut index = 2; while index + 4 <= data.len() {
if data[index] != 0xFF {
return false;
}
let marker = data[index + 1];
if matches!(marker, 0xD8 | 0x01 | 0xD0..=0xD7) {
index += 2;
continue;
}
let length = usize::from(u16::from_be_bytes([data[index + 2], data[index + 3]]));
if length < 2 {
return false;
}
if marker == 0xEE {
let payload = &data[index + 4..(index + 2 + length).min(data.len())];
return payload.starts_with(b"Adobe");
}
if marker == 0xDA {
return false;
}
index += 2 + length;
}
false
}
fn is_dct(dict: &Dictionary) -> bool {
match dict.get(b"Filter") {
Some(Object::Name(name)) => name == b"DCTDecode",
Some(Object::Array(items)) => items
.iter()
.rev()
.find_map(Object::as_name)
.is_some_and(|name| name == b"DCTDecode"),
_ => false,
}
}
pub(crate) fn resolve(document: &Document, object: Option<&Object>) -> Result<Object> {
match object {
Some(Object::Reference(id)) => document.get_object(*id),
Some(other) => Ok(other.clone()),
None => Ok(Object::Null),
}
}
#[cfg(test)]
mod tests {
use super::Image;
use crate::canvas::{BlendMode, Canvas, ClipMask, Color};
use crate::geom::Matrix;
fn image(width: u32, height: u32, interpolate: bool) -> Image {
let mut pixels = Vec::new();
for y in 0..height {
for x in 0..width {
pixels.push(if (x + y) % 2 == 0 {
Color::BLACK
} else {
Color::WHITE
});
}
}
Image {
width,
height,
pixels,
interpolate,
masked: None,
}
}
#[test]
fn nearest_sampling_returns_whole_pixels() {
let image = image(2, 2, false);
assert_eq!(image.sample(0.25, 0.25), Color::BLACK);
assert_eq!(image.sample(0.75, 0.25), Color::WHITE);
assert_eq!(image.sample(0.25, 0.75), Color::WHITE);
}
#[test]
fn bilinear_sampling_blends_neighbours() {
let image = image(2, 2, true);
let middle = image.sample(0.5, 0.5);
assert!((middle.r - 0.5).abs() < 1e-9, "{middle:?}");
}
#[test]
fn painting_covers_the_unit_square_transform() {
let image = image(2, 2, false);
let mut canvas = Canvas::new(4, 4, Color::WHITE);
let mask = ClipMask::all(4, 4);
let ctm = Matrix::new(4.0, 0.0, 0.0, -4.0, 0.0, 4.0);
image.paint(&mut canvas, &mask, ctm, 1.0, BlendMode::Normal);
assert_eq!(canvas.pixel(0, 0), Some([0, 0, 0, 255]));
assert_eq!(canvas.pixel(3, 0), Some([255, 255, 255, 255]));
assert_eq!(canvas.pixel(0, 3), Some([255, 255, 255, 255]));
assert_eq!(canvas.pixel(3, 3), Some([0, 0, 0, 255]));
}
}