Skip to content

Commit

Permalink
[feat] Works on specific areas
Browse files Browse the repository at this point in the history
  • Loading branch information
uttarayan21 committed Jan 12, 2024
1 parent 44064b9 commit 4bbbb86
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 29 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ bindgen = ["libraw-sys/bindgen"]
exif = ["dep:libc"]
openmp = ["libraw-sys/openmp"]
openmp_static = ["libraw-sys/openmp_static"]
default = ["exif"]
debug = []

default = ["exif", "debug"]

[build-dependencies]
anyhow = "1.0.58"
Expand Down
9 changes: 4 additions & 5 deletions examples/dcraw_half.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use libraw_r::traits::LRString;
use std::path::Path;
use std::sync::Arc;
use std::{io::Seek, path::Path};

Check warning on line 2 in examples/dcraw_half.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

unused import: `io::Seek`

Check warning on line 2 in examples/dcraw_half.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

unused import: `io::Seek`

pub fn main() -> anyhow::Result<()> {
for arg in std::env::args().skip(1) {
Expand All @@ -9,8 +8,8 @@ pub fn main() -> anyhow::Result<()> {
// .build();
let p = libraw_r::EmptyProcessor::new()?;
let file = std::fs::File::open(&arg)?;
let mut buffered = std::io::BufReader::new(file);
let mut p = p.open(&mut buffered)?;
let buffered = std::io::BufReader::new(file);
let mut p = p.open(buffered)?;
println!(
"Processing {arg} ({}, {})",
p.idata().make.as_ascii(),
Expand All @@ -19,7 +18,7 @@ pub fn main() -> anyhow::Result<()> {
p.unpack()?;
p.dcraw_process()?;
p.dcraw_ppm_tiff_writer(Path::new(&arg).with_extension("ppm"))?;
drop(buffered);
// drop(buffered);
println!("Writing to {arg}.ppm");
}
Ok(())
Expand Down
55 changes: 48 additions & 7 deletions io/io.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include "io.h"
#include "riio.h"
#include <libraw.h>
#include <libraw/libraw_const.h>
#define ID lr->get_internal_data_pointer()->internal_data

class LibrawIO : public LibRaw_abstract_datastream {
public:
LibrawIO(LibrawOpaqueDatastream *lod) { this->inner = lod; }
virtual ~LibrawIO() {}
virtual ~LibrawIO() {
printf("LibrawIO::~LibrawIO()\n");
lod_drop(this->inner);
}

virtual int valid() { return lod_valid(inner); }
virtual int read(void *ptr, size_t size, size_t nmemb) {
Expand All @@ -15,7 +20,7 @@ class LibrawIO : public LibRaw_abstract_datastream {
virtual INT64 tell() { return lod_tell(inner); }
virtual INT64 size() { return lod_size(inner); }
virtual int scanf_one(const char *fmt, void *val) {
return lod_scanf_one(inner, fmt, val);
return lod_scanf_one(inner, fmt, val);
}
virtual int get_char() { return lod_get_char(inner); }
virtual char *gets(char *str, int maxlen) {
Expand All @@ -26,11 +31,47 @@ class LibrawIO : public LibRaw_abstract_datastream {
LibrawOpaqueDatastream *inner;
};

int libraw_open_io(libraw_data_t *libraw,
LibrawOpaqueDatastream *io) {
LibrawIO* libraw_io = new LibrawIO(io);
/* int libraw_open_io(libraw_data_t *libraw, LibrawOpaqueDatastream *io) { */
/* LibRaw *lr = (LibRaw *)libraw->parent_class; */
/* LibrawIO *stream; */
/* stream = new LibrawIO(io); */
/* if (!stream || !stream->valid()) { */
/* delete stream; */
/* return LIBRAW_IO_ERROR; */
/* } */
/* ID.input_internal = 0; // preserve from deletion on error */
/* int ret = lr->open_datastream(stream); */
/* if (ret == LIBRAW_SUCCESS) */
/* { */
/* ID.input_internal = 1; // flag to delete datastream on recycle */
/* } */
/* else */
/* { */
/* delete stream; */
/* ID.input_internal = 0; */
/* } */
/* return ret; */
/* } */
int libraw_open_io(libraw_data_t *libraw, LibrawOpaqueDatastream *io) {
LibRaw *lr = (LibRaw *)libraw->parent_class;
/* printf("libraw_open_io\n"); */
LibRaw_abstract_datastream *stream;
stream = new LibrawIO(io);
if (!stream->valid()) {
delete stream;
return LIBRAW_IO_ERROR;
}
/* ID.input_internal = 0; // preserve from deletion on error */
int ret = lr->open_datastream(stream);
/* if (ret == LIBRAW_SUCCESS) { */
/* ID.input_internal = 1; // flag to delete datastream on recycle */
/* } else { */
/* delete stream; */
/* ID.input_internal = 0; */
/* } */
return ret;
}

return lr->open_datastream(libraw_io);
int libraw_valid_check(libraw_data_t *libraw) {
LibRaw *lr = (LibRaw *)libraw->parent_class;
return ID.input->valid();
}
1 change: 1 addition & 0 deletions io/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
int libraw_open_io(libraw_data_t *libraw,
LibrawOpaqueDatastream *io);

int libraw_valid_check(libraw_data_t *libraw);
#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions io/riio.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ char *lod_gets(struct LibrawOpaqueDatastream *this_, char *buffer, int size);

int lod_scanf_one(struct LibrawOpaqueDatastream *this_, const char *fmt, void *val);

void lod_drop(struct LibrawOpaqueDatastream *this_);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion libraw-sys/vendor
2 changes: 1 addition & 1 deletion src/dcraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Processor<'_> {
}

pub fn dcraw_ppm_tiff_writer(
self,
&mut self,
path: impl AsRef<std::path::Path>,
) -> Result<(), LibrawError> {
LibrawError::check(unsafe {
Expand Down
65 changes: 58 additions & 7 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
pub mod bindings;

#[cfg(feature = "debug")]
pub trait MaybeDebug: core::fmt::Debug {}

#[cfg(feature = "debug")]
impl<T: core::fmt::Debug> MaybeDebug for T {}

#[cfg(not(feature = "debug"))]
pub trait MaybeDebug {}

#[cfg(not(feature = "debug"))]
impl<T> MaybeDebug for T {}

/// Input layer abstraction
///
/// class LibRaw_abstract_datastream - abstract RAW read interface
Expand Down Expand Up @@ -108,7 +121,7 @@ pub mod bindings;
// #endif
// };
// ```
pub trait LibrawDatastream: Read + Seek + Eof {
pub trait LibrawDatastream: Read + Seek + Eof + MaybeDebug {
/// # Safety
///
/// This function will be called from ffi (c++)
Expand All @@ -134,7 +147,7 @@ pub trait LibrawDatastream: Read + Seek + Eof {
}
/// # Safety
///
///
///
unsafe fn seek(&mut self, offset: i64, whence: u32) -> i32 {
match whence {
sys::SEEK_SET => {
Expand Down Expand Up @@ -188,9 +201,9 @@ pub trait LibrawDatastream: Read + Seek + Eof {
}
}

impl<T: Read + Seek + Eof> LibrawDatastream for T {}
impl<T: Read + Seek + Eof + MaybeDebug> LibrawDatastream for T {}

pub trait LibrawBufferedDatastream: LibrawDatastream + BufRead {
pub trait LibrawBufferedDatastream: LibrawDatastream + BufRead + MaybeDebug {
/// # Safety
///
/// This function is unsafe because it dereferences a raw pointer and is called from ffi.
Expand Down Expand Up @@ -223,7 +236,7 @@ pub trait LibrawBufferedDatastream: LibrawDatastream + BufRead {
}
}

impl<T: LibrawDatastream + BufRead> LibrawBufferedDatastream for T {}
impl<T: LibrawDatastream + BufRead + MaybeDebug> LibrawBufferedDatastream for T {}

use core::ops::{Deref, DerefMut};
// pub trait Libraw
Expand Down Expand Up @@ -341,12 +354,13 @@ fn read_until<R: BufRead + ?Sized>(

// impl<T: LibrawBufferedDatastream> AbstractDatastream<T> {
#[repr(C)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct LibrawOpaqueDatastream<'a> {
inner: Box<dyn LibrawBufferedDatastream + 'a>,
}

impl<'a> LibrawOpaqueDatastream<'a> {
pub fn new(inner: impl LibrawBufferedDatastream + 'a) -> Self {
pub fn new(inner: impl LibrawBufferedDatastream + MaybeDebug + 'a) -> Self {
Self {
inner: Box::new(inner),
}
Expand Down Expand Up @@ -384,6 +398,7 @@ pub unsafe extern "C" fn lod_seek(
) -> i32 {
assert!(!this.is_null());
let this = unsafe { &mut *this };
// this.inner.tell();
LibrawDatastream::seek(&mut this.inner, offset, whence)
}

Expand Down Expand Up @@ -434,5 +449,41 @@ pub unsafe extern "C" fn lod_scanf_one(
fmt: *const libc::c_char,
val: *mut libc::c_void,
) -> libc::c_int {
todo!();
assert!(!this.is_null());
use core::ffi::*;
let this = unsafe { &mut *this };
let fmt = unsafe { CStr::from_ptr(fmt) };
// let val = unsafe { &mut *(val as *mut u32) };
// let mut buf = [0u8; 4];
// todo!()
match fmt.to_bytes() {
b"%d" => {
let val = unsafe { &mut *(val as *mut i32) };
let mut buf = [0u8; 4];
if this.inner.read_exact(&mut buf).is_err() {
return -1;
}
*val = i32::from_ne_bytes(buf);
1
}
b"%f" => {
let val = unsafe { &mut *(val as *mut f32) };
let mut buf = [0u8; 4];
if this.inner.read_exact(&mut buf).is_err() {
return -1;
}
*val = f32::from_ne_bytes(buf);
1
}
b"%s" => unimplemented!("skipped for now"),
_ => return -1,
}
}

#[no_mangle]
pub unsafe extern "C" fn lod_drop(this: *mut LibrawOpaqueDatastream) {
assert!(!this.is_null());
let this = unsafe { this.read() };

Check warning on line 486 in src/io.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

unused variable: `this`

Check failure on line 486 in src/io.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

unused variable: `this`

Check warning on line 486 in src/io.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

unused variable: `this`

Check failure on line 486 in src/io.rs

View workflow job for this annotation

GitHub Actions / macOS (x86_64-apple-darwin)

unused variable: `this`

Check warning on line 486 in src/io.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

unused variable: `this`

Check warning on line 486 in src/io.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

unused variable: `this`

Check failure on line 486 in src/io.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

unused variable: `this`
panic!("dropping");
drop(this)

Check warning on line 488 in src/io.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

unreachable expression

Check failure on line 488 in src/io.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

unreachable expression

Check warning on line 488 in src/io.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

unreachable expression

Check failure on line 488 in src/io.rs

View workflow job for this annotation

GitHub Actions / macOS (x86_64-apple-darwin)

unreachable expression

Check warning on line 488 in src/io.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

unreachable expression

Check warning on line 488 in src/io.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

unreachable expression

Check failure on line 488 in src/io.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

unreachable expression
}
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pub mod dcraw;
pub mod image;
pub mod io;
pub mod processor;
pub use io::{LibrawBufferedDatastream, LibrawDatastream, MaybeDebug};
use processor::Processor;

// pub mod dcraw;
// pub mod defaults;
// #[cfg(feature = "exif")]
Expand All @@ -13,16 +15,14 @@ use processor::Processor;
// pub mod progress;
pub mod traits;

use alloc::sync::Arc;
pub use error::LibrawError;

extern crate alloc;
extern crate libraw_sys as sys;
use core::ptr::NonNull;
use core::sync::atomic::AtomicBool;
use semver::Version;
use std::ffi::CString;
use std::ops::Drop;
use std::mem::ManuallyDrop;
use std::path::Path;

/// Returns the version of libraw the bindings were generated against
Expand Down Expand Up @@ -54,14 +54,18 @@ impl EmptyProcessor {
})
}

pub fn open<'reader, T: std::io::BufRead + std::io::Seek + 'reader>(
pub fn open<'reader, T: std::io::BufRead + std::io::Seek + MaybeDebug + '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) };
let ret = unsafe {
io::bindings::libraw_open_io(self.inner.as_mut(), core::ptr::addr_of_mut!(io))
};
LibrawError::check(ret)?;
core::mem::forget(io);
Ok(unsafe { Processor::new(self.inner) })
let mut p = unsafe { Processor::new(self.inner) };

Check warning on line 66 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

variable does not need to be mutable

Check failure on line 66 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

variable does not need to be mutable

Check warning on line 66 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Linux (x86_64-unknown-linux-gnu)

variable does not need to be mutable

Check failure on line 66 in src/lib.rs

View workflow job for this annotation

GitHub Actions / macOS (x86_64-apple-darwin)

variable does not need to be mutable

Check warning on line 66 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

variable does not need to be mutable

Check warning on line 66 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

variable does not need to be mutable

Check failure on line 66 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC (x86_64-pc-windows-msvc)

variable does not need to be mutable
let _ = ManuallyDrop::new(io);
// p.unpack()?;
Ok(p)
}
}
5 changes: 5 additions & 0 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ impl<'reader> Processor<'reader> {
unsafe { &self.inner.as_ref().idata }
}

pub fn valid(&mut self) -> Result<(), LibrawError> {
let ret = unsafe { crate::io::bindings::libraw_valid_check(self.inner.as_mut()) };
LibrawError::check(ret)
}

pub fn unpack(&mut self) -> Result<(), LibrawError> {
let ret = unsafe { sys::libraw_unpack(self.inner.as_mut()) };
LibrawError::check(ret)
Expand Down

0 comments on commit 4bbbb86

Please sign in to comment.