Skip to main content

ImageSource

Struct ImageSource 

Source
pub struct ImageSource {
    pub bytes: Vec<u8>,
    pub format: ImageFormat,
    pub page_size: PageSize,
}
Expand description

Source for a single image page to be embedded in the OFD.

Each ImageSource becomes one page in the output document. The image fills the entire page boundary (no margins).

Fields§

§bytes: Vec<u8>

Raw image bytes.

§format: ImageFormat

Detected or specified image format.

§page_size: PageSize

Page size in millimeters.

Implementations§

Source§

impl ImageSource

Source

pub fn new( bytes: Vec<u8>, format: ImageFormat, width_mm: f64, height_mm: f64, ) -> Self

Create from image bytes with explicit format and mm dimensions.

Source

pub fn jpeg(bytes: Vec<u8>, width_px: u32, height_px: u32, dpi: f64) -> Self

Create from JPEG bytes with known pixel dimensions and DPI.

Source

pub fn jpeg_mm(bytes: Vec<u8>, width_mm: f64, height_mm: f64) -> Self

Create from JPEG bytes with page size in mm.

Source

pub fn png_mm(bytes: Vec<u8>, width_mm: f64, height_mm: f64) -> Self

Create from PNG bytes with page size in mm.

Source

pub fn auto_detect(bytes: Vec<u8>, dpi: f64) -> Option<Self>

Auto-detect format and dimensions, calculate page size from DPI.

Returns None if format or dimensions cannot be detected.

Source

pub fn auto_detect_ppm(bytes: Vec<u8>, ppm: f64) -> Option<Self>

Auto-detect format and dimensions using pixels-per-mm ratio.

Uses ofdrw’s conversion: mm = pixels / ppm. See PPM_DEFAULT for the standard 5 px/mm ratio. Returns None if format or dimensions cannot be detected.

Source

pub fn auto_detect_default(bytes: Vec<u8>) -> Option<Self>

Auto-detect format and dimensions using ofdrw’s default ratio (5 px/mm).

Equivalent to auto_detect_ppm(bytes, PPM_DEFAULT). Returns None if format or dimensions cannot be detected.

Examples found in repository?
examples/image_to_ofd.rs (line 32)
8fn main() {
9    let args: Vec<String> = std::env::args().collect();
10    if args.len() < 2 {
11        eprintln!("Usage: {} <input_image> [output.ofd]", args[0]);
12        std::process::exit(1);
13    }
14
15    let input_path = &args[1];
16    let output_path = if args.len() > 2 {
17        args[2].clone()
18    } else {
19        let stem = Path::new(input_path)
20            .file_stem()
21            .and_then(|s| s.to_str())
22            .unwrap_or("output");
23        let parent = Path::new(input_path)
24            .parent()
25            .unwrap_or(Path::new("."));
26        parent.join(format!("{}.ofd", stem)).to_string_lossy().into_owned()
27    };
28
29    let image_bytes = std::fs::read(input_path).expect("failed to read input file");
30
31    // Auto-detect format + dimensions, use ofdrw default ratio (5 px/mm ≈ 127 DPI)
32    let source = ImageSource::auto_detect_default(image_bytes)
33        .expect("unsupported image format or cannot read dimensions");
34
35    println!(
36        "Image: {}x{} mm (page size)",
37        format!("{:.1}", source.page_size.width_mm),
38        format!("{:.1}", source.page_size.height_mm),
39    );
40
41    let ofd_bytes = OfdWriter::from_images(vec![source])
42        .build()
43        .expect("failed to build OFD");
44
45    std::fs::write(&output_path, &ofd_bytes).expect("failed to write OFD file");
46    println!("Created: {} ({} bytes)", output_path, ofd_bytes.len());
47}
Source

pub fn auto_detect_mm( bytes: Vec<u8>, width_mm: f64, height_mm: f64, ) -> Option<Self>

Auto-detect format, use explicit mm dimensions for page size.

Returns None if format cannot be detected from file header.

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.