Skip to content

Commit

Permalink
Expand the pdf/examples/read to extract processable tiff files (#212)
Browse files Browse the repository at this point in the history
* rustfmt autoformatting

* add tiff metadata to make a valid tiff file
  • Loading branch information
jakob-ledermann committed Mar 5, 2024
1 parent 947466c commit 65c123d
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions pdf/examples/read.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extern crate pdf;

use std::collections::HashMap;
use std::env::args;
use std::time::SystemTime;
use std::fs;
use std::collections::HashMap;
use std::time::SystemTime;

use pdf::enc::StreamFilter;
use pdf::error::PdfError;
use pdf::file::{FileOptions, Log};
use pdf::object::*;
use pdf::primitive::Primitive;
use pdf::error::PdfError;
use pdf::enc::StreamFilter;

struct VerboseLog;
impl Log for VerboseLog {
Expand All @@ -21,7 +21,7 @@ impl Log for VerboseLog {
}
}

#[cfg(feature="cache")]
#[cfg(feature = "cache")]
fn main() -> Result<(), PdfError> {
let path = args().nth(1).expect("no file given");
println!("read: {}", path);
Expand All @@ -38,7 +38,7 @@ fn main() -> Result<(), PdfError> {
(Some(title), None) => title,
(None, Some(author)) => format!("[no title] – {}", author),
(Some(title), Some(author)) => format!("{} – {}", title, author),
_ => "PDF".into()
_ => "PDF".into(),
};
println!("{}", descr);
}
Expand All @@ -56,27 +56,35 @@ fn main() -> Result<(), PdfError> {
};
fonts.insert(name, font.clone());
}
images.extend(resources.xobjects.iter().map(|(_name, &r)| resolver.get(r).unwrap())
.filter(|o| matches!(**o, XObject::Image(_)))
images.extend(
resources
.xobjects
.iter()
.map(|(_name, &r)| resolver.get(r).unwrap())
.filter(|o| matches!(**o, XObject::Image(_))),
);
}

for (i, o) in images.iter().enumerate() {
let img = match **o {
XObject::Image(ref im) => im,
_ => continue
_ => continue,
};
let (data, filter) = img.raw_image_data(&resolver)?;
let (mut data, filter) = img.raw_image_data(&resolver)?;
let ext = match filter {
Some(StreamFilter::DCTDecode(_)) => "jpeg",
Some(StreamFilter::JBIG2Decode(_)) => "jbig2",
Some(StreamFilter::JPXDecode) => "jp2k",
Some(StreamFilter::FlateDecode(_)) => "png",
Some(StreamFilter::CCITTFaxDecode(_)) => {
data = fax::tiff::wrap(&data, img.width, img.height).into();
"tiff"
}
_ => continue,
};

let fname = format!("extracted_image_{}.{}", i, ext);

fs::write(fname.as_str(), data).unwrap();
println!("Wrote file {}", fname);
}
Expand Down Expand Up @@ -105,8 +113,10 @@ fn main() -> Result<(), PdfError> {
}

if let Ok(elapsed) = now.elapsed() {
println!("Time: {}s", elapsed.as_secs() as f64
+ elapsed.subsec_nanos() as f64 * 1e-9);
println!(
"Time: {}s",
elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 * 1e-9
);
}
Ok(())
}

0 comments on commit 65c123d

Please sign in to comment.