Skip to content

Commit

Permalink
[feat] Add lifetimes to LibrawOpaqueDatastream
Browse files Browse the repository at this point in the history
  • Loading branch information
uttarayan21 committed Jan 12, 2024
1 parent f70a628 commit 44064b9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
10 changes: 7 additions & 3 deletions examples/dcraw_half.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
use libraw_r::traits::LRString;
use std::path::Path;
use std::sync::Arc;

pub fn main() -> anyhow::Result<()> {
for arg in std::env::args().skip(1) {
// let mut p = Processor::builder()
// .with_params([Params::HalfSize(true)])
// .build();
let p = libraw_r::EmptyProcessor::new()?;
let mut p = p.open(&arg)?;
let file = std::fs::File::open(&arg)?;
let mut buffered = std::io::BufReader::new(file);
let mut p = p.open(&mut buffered)?;
println!(
"Processing {arg} ({}, {})",
p.idata().make.as_ascii(),
p.idata().model.as_ascii(),
);
p.unpack()?;
p.dcraw_process()?;
// p.dcraw_ppm_tiff_writer(Path::new(&arg).with_extension("ppm"))?;
// println!("Writing to {arg}.ppm");
p.dcraw_ppm_tiff_writer(Path::new(&arg).with_extension("ppm"))?;
drop(buffered);
println!("Writing to {arg}.ppm");
}
Ok(())
}
33 changes: 23 additions & 10 deletions src/dcraw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::image::*;
use crate::*;
impl Processor {
impl Processor<'_> {
pub fn dcraw_process_make_mem_thumb(&mut self) -> Result<ProcessedImage, LibrawError> {
let mut errc = 0;
let data = unsafe { sys::libraw_dcraw_make_mem_thumb(self.inner.as_ptr(), &mut errc) };
Expand Down Expand Up @@ -30,13 +30,26 @@ impl Processor {
)
}

// pub fn dcraw_ppm_tiff_writer(
// self,
// path: impl AsRef<std::path::Path>,
// ) -> Result<(), LibrawError> {
// LibrawError::check(unsafe {
// sys::libraw_dcraw_ppm_tiff_writer(self.inner.as_ptr(), path_to_cstr(path)?.as_ptr())
// })?;
// Ok(())
// }
pub fn dcraw_ppm_tiff_writer(
self,
path: impl AsRef<std::path::Path>,
) -> Result<(), LibrawError> {
LibrawError::check(unsafe {
sys::libraw_dcraw_ppm_tiff_writer(self.inner.as_ptr(), path_to_cstr(path)?.as_ptr())
})?;
Ok(())
}
}

#[cfg(unix)]
fn path_to_cstr(path: impl AsRef<Path>) -> Result<CString, std::ffi::NulError> {
use std::os::unix::ffi::OsStrExt;
let path = path.as_ref().as_os_str().as_bytes();
CString::new(path)
}
#[cfg(windows)]
fn path_to_cstr(path: impl AsRef<Path>) -> Result<CString, std::ffi::NulError> {
let path = path.as_ref().display().to_string();
let path = path.as_bytes();
CString::new(path)
}
10 changes: 5 additions & 5 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub trait LibrawDatastream: Read + Seek + Eof {
}
/// # Safety
///
/// Sus
///
unsafe fn seek(&mut self, offset: i64, whence: u32) -> i32 {
match whence {
sys::SEEK_SET => {
Expand Down Expand Up @@ -341,12 +341,12 @@ fn read_until<R: BufRead + ?Sized>(

// impl<T: LibrawBufferedDatastream> AbstractDatastream<T> {
#[repr(C)]
pub struct LibrawOpaqueDatastream {
inner: Box<dyn LibrawBufferedDatastream>,
pub struct LibrawOpaqueDatastream<'a> {
inner: Box<dyn LibrawBufferedDatastream + 'a>,
}

impl LibrawOpaqueDatastream {
pub fn new(inner: impl LibrawBufferedDatastream + 'static) -> Self {
impl<'a> LibrawOpaqueDatastream<'a> {
pub fn new(inner: impl LibrawBufferedDatastream + 'a) -> Self {
Self {
inner: Box::new(inner),
}
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ impl EmptyProcessor {
})
}

pub fn open(mut self, path: impl AsRef<Path>) -> Result<Processor, LibrawError> {
let file = std::fs::File::open(path)?;
let buffered = std::io::BufReader::new(file);
let mut io = io::LibrawOpaqueDatastream::new(buffered);
pub fn open<'reader, T: std::io::BufRead + std::io::Seek + 'reader>(
mut self,
reader: T,
) -> Result<Processor<'reader>, LibrawError> {
let mut io = io::LibrawOpaqueDatastream::new(reader);
let ret = unsafe { io::bindings::libraw_open_io(self.inner.as_mut(), &mut io) };
LibrawError::check(ret)?;
core::mem::forget(io);
Ok(Processor { inner: self.inner })
Ok(unsafe { Processor::new(self.inner) })
}
}
13 changes: 11 additions & 2 deletions src/processor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use crate::*;
use core::ptr::NonNull;

pub struct Processor {
pub struct Processor<'reader> {
pub(crate) inner: NonNull<sys::libraw_data_t>,
pub(crate) _marker: std::marker::PhantomData<&'reader ()>,
}
impl Processor {

impl<'reader> Processor<'reader> {
pub unsafe fn new(inner: NonNull<sys::libraw_data_t>) -> Self {
Self {
inner,
_marker: std::marker::PhantomData,
}
}

pub fn idata(&'_ self) -> &'_ sys::libraw_iparams_t {
unsafe { &self.inner.as_ref().idata }
}
Expand Down

0 comments on commit 44064b9

Please sign in to comment.