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: ImageFormatDetected or specified image format.
page_size: PageSizePage size in millimeters.
Implementations§
Source§impl ImageSource
impl ImageSource
Sourcepub fn new(
bytes: Vec<u8>,
format: ImageFormat,
width_mm: f64,
height_mm: f64,
) -> Self
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.
Sourcepub fn jpeg(bytes: Vec<u8>, width_px: u32, height_px: u32, dpi: f64) -> Self
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.
Sourcepub fn jpeg_mm(bytes: Vec<u8>, width_mm: f64, height_mm: f64) -> Self
pub fn jpeg_mm(bytes: Vec<u8>, width_mm: f64, height_mm: f64) -> Self
Create from JPEG bytes with page size in mm.
Sourcepub fn png_mm(bytes: Vec<u8>, width_mm: f64, height_mm: f64) -> Self
pub fn png_mm(bytes: Vec<u8>, width_mm: f64, height_mm: f64) -> Self
Create from PNG bytes with page size in mm.
Sourcepub fn auto_detect(bytes: Vec<u8>, dpi: f64) -> Option<Self>
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.
Sourcepub fn auto_detect_ppm(bytes: Vec<u8>, ppm: f64) -> Option<Self>
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.
Sourcepub fn auto_detect_default(bytes: Vec<u8>) -> Option<Self>
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?
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}