Skip to content

Commit

Permalink
[fail] In progress commit will compile as lib
Browse files Browse the repository at this point in the history
  • Loading branch information
uttarayan21 committed Jan 11, 2024
1 parent f1f3c00 commit 13f0769
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 97 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ repository = "https://github.com/aftershootco/libraw-sys"
homepage = "https://github.com/aftershootco/libraw-sys"
publish = ["ktra"]

[lib]
name = "libraw_r"
crate-type = ["cdylib", "rlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand All @@ -34,6 +38,7 @@ default = ["exif"]

[build-dependencies]
anyhow = "1.0.58"
bindgen = "0.69.1"
cbindgen = "0.26.0"
cc = "1.0.73"

Expand Down
20 changes: 17 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub fn libread(out_dir: impl AsRef<Path>) -> anyhow::Result<()> {

pub fn riio(out_dir: impl AsRef<Path>) -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=src/io.rs");
let includes = std::env::var("DEP_RAW_R_INCLUDE")?;
let includes = std::env::split_paths(&includes).collect::<Vec<_>>();

cbindgen::Builder::new()
.with_crate(env!("CARGO_MANIFEST_DIR"))
.with_language(cbindgen::Language::C)
Expand All @@ -48,10 +51,21 @@ pub fn riio(out_dir: impl AsRef<Path>) -> anyhow::Result<()> {
.with_include_guard("RUST_IO_H")
.generate()
.expect("Unable to generate bindings")
.write_to_file("io/io.h");
.write_to_file("io/riio.h");

// dbg!(&includes);
// bindgen::Builder::default()
// // .header("io/io.cpp")
// .clang_args(
// includes
// .iter()
// .map(|p| format!("-I{}", p.to_str().unwrap())),
// )
// .generate()
// .unwrap()
// .write_to_file("src/wrapper.rs")
// .unwrap();

let includes = std::env::var("DEP_RAW_R_INCLUDE")?;
let includes = std::env::split_paths(&includes).collect::<Vec<_>>();
let mut riio = cc::Build::new();
riio.includes(includes)
.cpp(true)
Expand Down
8 changes: 4 additions & 4 deletions examples/dcraw_half.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ pub fn main() -> anyhow::Result<()> {
// let mut p = Processor::builder()
// .with_params([Params::HalfSize(true)])
// .build();
let mut p = libraw_r::defaults::half_size();
p.open(&arg)?;
let mut p = libraw_r::EmptyProcessor::new()?;
let mut p = p.open(&arg)?;
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"))?;
// println!("Writing to {arg}.ppm");
}
Ok(())
}
20 changes: 19 additions & 1 deletion io/io.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "io.h"
#include "riio.h"
#include <libraw.h>

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

virtual int valid() { return lod_valid(inner); }
Expand All @@ -12,7 +14,23 @@ class LibrawIO : public LibRaw_abstract_datastream {
virtual int seek(INT64 o, int whence) { return lod_seek(inner, o, whence); }
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); */
return 0;
}
virtual int get_char() { return lod_get_char(inner); }
virtual char *gets(char *str, int maxlen) {
return lod_gets(inner, str, maxlen);
}
virtual int eof() { return lod_eof(inner); }

protected:
LibrawOpaqueDatastream *inner;
};

extern "C" int libraw_open_io(libraw_data_t *libraw,
LibrawOpaqueDatastream *io) {
LibrawIO* libraw_io = new LibrawIO(io);
LibRaw *lr = (LibRaw *)libraw->parent_class;

return lr->open_datastream(libraw_io);
}
30 changes: 3 additions & 27 deletions io/io.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
#include<stdint.h>

#ifndef RUST_IO_H
#define RUST_IO_H



typedef struct LibrawOpaqueDatastream LibrawOpaqueDatastream;

int32_t lod_valid(struct LibrawOpaqueDatastream *this_);

int32_t lod_read(struct LibrawOpaqueDatastream *this_,
const void *buffer,
uintptr_t sz,
uintptr_t nmemb);

int32_t lod_seek(struct LibrawOpaqueDatastream *this_, int64_t offset, uint32_t whence);

int64_t lod_tell(struct LibrawOpaqueDatastream *this_);

int64_t lod_size(struct LibrawOpaqueDatastream *this_);

int lod_get_char(struct LibrawOpaqueDatastream *this_);

const char *lod_gets(struct LibrawOpaqueDatastream *this_, char *buffer, int size);

#endif /* RUST_IO_H */
/* #include "riio.h" */
/* #include <libraw.h> */
/* extern "C" int libraw_open_io(LibRaw *libraw, LibrawOpaqueDatastream *io); */
29 changes: 29 additions & 0 deletions io/riio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<stdint.h>

#ifndef RUST_IO_H
#define RUST_IO_H



typedef struct LibrawOpaqueDatastream LibrawOpaqueDatastream;

int32_t lod_valid(struct LibrawOpaqueDatastream *this_);

int32_t lod_read(struct LibrawOpaqueDatastream *this_,
const void *buffer,
uintptr_t sz,
uintptr_t nmemb);

int32_t lod_seek(struct LibrawOpaqueDatastream *this_, int64_t offset, uint32_t whence);

int64_t lod_tell(struct LibrawOpaqueDatastream *this_);

int32_t lod_eof(struct LibrawOpaqueDatastream *this_);

int64_t lod_size(struct LibrawOpaqueDatastream *this_);

int lod_get_char(struct LibrawOpaqueDatastream *this_);

char *lod_gets(struct LibrawOpaqueDatastream *this_, char *buffer, int size);

#endif /* RUST_IO_H */
19 changes: 10 additions & 9 deletions src/dcraw.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::image::*;
use crate::*;
impl Processor {
pub fn dcraw_process_make_mem_thumb(&mut self) -> Result<ProcessedImage, LibrawError> {
Expand Down Expand Up @@ -29,13 +30,13 @@ 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(())
// }
}
116 changes: 116 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use core::ptr::NonNull;

#[derive(Debug)]
pub struct ProcessedImage {
pub(crate) inner: NonNull<sys::libraw_processed_image_t>,
}

impl Drop for ProcessedImage {
fn drop(&mut self) {
unsafe { sys::libraw_dcraw_clear_mem(self.inner.as_ptr()) }
}
}

// impl Deref for ProcessedImage {
// type Target = *mut sys::libraw_processed_image_t;
// fn deref(&self) -> &Self::Target {
// &self.inner
// }
// }

// impl DerefMut for ProcessedImage {
// fn deref_mut(&mut self) -> &mut Self::Target {
// &mut self.inner
// }
// }

impl ProcessedImage {
pub fn raw(&self) -> &sys::libraw_processed_image_t {
unsafe { self.inner.as_ref() }
}
pub fn as_slice_u8(&self) -> &[u8] {
self.as_slice::<u8>()
}
pub fn as_slice_u16(&self) -> &[u16] {
self.as_slice::<u16>()
}

pub fn as_slice<T>(&self) -> &[T] {
unsafe {
std::slice::from_raw_parts(
self.inner.as_ref().data.as_ptr() as *const T,
self.inner.as_ref().data_size as usize / std::mem::size_of::<T>(),
)
}
}
pub fn width(&self) -> u32 {
self.raw().width.into()
}
pub fn height(&self) -> u32 {
self.raw().height.into()
}
pub fn type_(&self) -> ImageFormat {
ImageFormat::from(self.raw().type_)
}
pub fn bits(&self) -> u16 {
self.raw().bits
}
pub fn colors(&self) -> u16 {
self.raw().colors
}
pub fn size(&self) -> usize {
self.raw().data_size as usize
}
}

/// The thumbnail types that might be embedded inside a raw file
#[non_exhaustive]
#[cfg_attr(all(windows, target_env = "msvc"), repr(i32))]
#[cfg_attr(all(windows, target_env = "gnu"), repr(u32))]
#[cfg_attr(unix, repr(u32))]
pub enum ThumbnailFormat {
Unknown = sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_UNKNOWN,
Jpeg = sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_JPEG,
Bitmap = sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_BITMAP,
Bitmap16 = sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_BITMAP16,
Layer = sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_LAYER,
Rollei = sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_ROLLEI,
H265 = sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_H265,
}

impl From<sys::LibRaw_thumbnail_formats> for ThumbnailFormat {
fn from(tformat: sys::LibRaw_thumbnail_formats) -> Self {
use ThumbnailFormat::*;
match tformat {
sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_UNKNOWN => Unknown,
sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_JPEG => Jpeg,
sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_BITMAP => Bitmap,
sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_BITMAP16 => Bitmap16,
sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_LAYER => Layer,
sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_ROLLEI => Rollei,
sys::LibRaw_thumbnail_formats_LIBRAW_THUMBNAIL_H265 => H265,
_ => Unknown,
}
}
}

/// The format the raw file might be encoded in
#[non_exhaustive]
#[cfg_attr(all(windows, target_env = "msvc"), repr(i32))]
#[cfg_attr(all(windows, target_env = "gnu"), repr(u32))]
#[cfg_attr(unix, repr(u32))]
pub enum ImageFormat {
Jpeg = sys::LibRaw_image_formats_LIBRAW_IMAGE_JPEG,
Bitmap = sys::LibRaw_image_formats_LIBRAW_IMAGE_BITMAP,
}

impl From<sys::LibRaw_image_formats> for ImageFormat {
fn from(format: sys::LibRaw_image_formats) -> Self {
use ImageFormat::*;
match format {
sys::LibRaw_image_formats_LIBRAW_IMAGE_JPEG => Jpeg,
sys::LibRaw_image_formats_LIBRAW_IMAGE_BITMAP => Bitmap,
_ => unimplemented!("Please use the correct bindings for this version of libraw"),
}
}
}

0 comments on commit 13f0769

Please sign in to comment.