Skip to main content

ImageContent

Struct ImageContent 

Source
pub struct ImageContent {
Show 14 fields pub bbox: Rect, pub format: ImageFormat, pub data: Vec<u8>, pub width: u32, pub height: u32, pub bits_per_component: u8, pub color_space: ColorSpace, pub reading_order: Option<usize>, pub alt_text: Option<String>, pub horizontal_dpi: Option<f32>, pub vertical_dpi: Option<f32>, pub soft_mask: Option<Vec<u8>>, pub matrix: Option<[f32; 6]>, pub is_artifact: bool,
}
Expand description

Image content that can be extracted from or written to a PDF.

This represents an embedded image with its positioning information.

Fields§

§bbox: Rect

Bounding box where the image is placed

§format: ImageFormat

Image format

§data: Vec<u8>

Image bytes. When constructed via from_bytes() or new() this holds the encoded file bytes (PNG/JPEG). When extracted from an existing PDF the bytes are the raw decoded pixel data as stored in the XObject stream.

§width: u32

Image width in pixels

§height: u32

Image height in pixels

§bits_per_component: u8

Bits per component (typically 8)

§color_space: ColorSpace

Color space

§reading_order: Option<usize>

Reading order index

§alt_text: Option<String>

Alternative text for accessibility

§horizontal_dpi: Option<f32>

Horizontal DPI (dots per inch) calculated from pixel width and bbox

§vertical_dpi: Option<f32>

Vertical DPI (dots per inch) calculated from pixel height and bbox

§soft_mask: Option<Vec<u8>>

Optional alpha / transparency channel for PNG-style soft masks. Already compressed (FlateDecode) by the image decoder. Carried through to the PDF as an /SMask XObject so transparent PNGs render with their alpha preserved. None for opaque images and for formats that don’t carry transparency (JPEG, CMYK).

§matrix: Option<[f32; 6]>

Optional 2D affine transform in PDF row order [a b c d e f]. When set, the image is wrapped in q ... cm ... Q on emission so graphics-state stays scoped. Populated by FluentPageBuilder::{rotated, scaled, translated, with_transform} closures. #393 Bundle A-2 follow-up.

§is_artifact: bool

When true the image is purely decorative and must be marked as an /Artifact in the content stream so assistive technology ignores it. PDF/UA-1 §7.1 (F-3). Mutually exclusive with alt_text.

Implementations§

Source§

impl ImageContent

Source

pub fn new( bbox: Rect, format: ImageFormat, data: Vec<u8>, width: u32, height: u32, ) -> Self

Create a new image content element.

Source

pub fn as_artifact(self) -> Self

Mark this image as a decorative artifact (PDF/UA-1 §7.1). The image will be wrapped in /Artifact BDC/EMC so assistive technology skips it. Clears any alt_text that was previously set.

Source

pub fn with_soft_mask(self, mask: Vec<u8>) -> Self

Attach a pre-compressed soft-mask (alpha) channel. The bytes are emitted verbatim as a separate XObject stream linked via /SMask so PDF viewers composite the alpha at paint time.

Source

pub fn with_reading_order(self, order: usize) -> Self

Set the reading order.

Source

pub fn with_alt_text(self, text: impl Into<String>) -> Self

Set alternative text for accessibility.

Source

pub fn aspect_ratio(&self) -> f32

Get the aspect ratio (width / height).

Source

pub fn is_grayscale(&self) -> bool

Check if this is a grayscale image.

Source

pub fn calculate_dpi(&mut self)

Calculate and set the DPI values based on pixel dimensions and bounding box.

DPI = pixels / inches, where inches = points / 72 (1 inch = 72 points)

Source

pub fn resolution(&self) -> Option<(f32, f32)>

Get the resolution as (horizontal_dpi, vertical_dpi).

Returns None if DPI hasn’t been calculated or bbox is invalid.

Source

pub fn get_horizontal_dpi(&self) -> Option<f32>

Get the horizontal DPI.

Source

pub fn get_vertical_dpi(&self) -> Option<f32>

Get the vertical DPI.

Source

pub fn is_high_resolution(&self) -> bool

Check if this image is high resolution (>= 300 DPI in both dimensions).

300 DPI is considered the standard for print-quality images.

Source

pub fn is_low_resolution(&self) -> bool

Check if this image is low resolution (< 150 DPI in either dimension).

150 DPI is often considered the minimum for reasonable quality.

Source

pub fn is_medium_resolution(&self) -> bool

Check if this image is medium resolution (>= 150 DPI but < 300 DPI).

Source

pub fn from_bytes(bbox: Rect, data: Vec<u8>) -> Result<Self, Error>

Create an ImageContent from raw image bytes, auto-detecting JPEG/PNG by magic number.

Pixel dimensions, color space, and soft mask are parsed from the image header automatically — no need to supply width or height. bbox controls the on-page display rectangle in PDF points (72 pt = 1 inch).

Returns an error if the bytes do not start with a recognised image magic number.

Trait Implementations§

Source§

impl Clone for ImageContent

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ImageContent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ImageContent

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more