use std::os::raw::{c_char, c_void}; use std::ffi::CString; /// # TIFF Internal Structure /// An opaque type #[repr(C)] pub struct TIFF { _unused: [u8; 0], } /// # TIFF Warning Handler Definition type TIFFWarningHandler = Option; /// # TIFF Ignore Warnings Handle unsafe extern "C" fn tiff_ignore_warning_handle(_module: *const c_char, _fmt: *const c_char, _ap: *mut std::ffi::c_void) { // Do nothing } #[link(name = "tiff")] unsafe extern "C" { fn TIFFSetWarningHandler(handler: TIFFWarningHandler) -> TIFFWarningHandler; fn TIFFOpen(filename: *const c_char, mode: *const c_char) -> *mut TIFF; fn TIFFClose(tiff_ptr: *mut TIFF); fn TIFFGetField(tiff_ptr: *mut TIFF, tag: u32, ...) -> i32; fn TIFFStripSize(tiff_ptr: *mut TIFF) -> i64; fn TIFFNumberOfStrips(tiff_ptr: *mut TIFF) -> i64; } /// From tiff.h const TIFFTAG_IMAGEWIDTH: u32 = 256; /// From tiff.h const TIFFTAG_IMAGEHEIGHT: u32 = 257; /// # Ignore TIFF Warnings pub fn ignore_warnings() { unsafe { let _ = TIFFSetWarningHandler(Some(tiff_ignore_warning_handle)); } } /// # Open TIFF File pub fn open(filename: &str) -> Option<*mut TIFF> { let c_filename = CString::new(filename).expect("Cast error"); let c_mode = CString::new("r").expect("Cast error"); unsafe { let result = TIFFOpen(c_filename.as_ptr(), c_mode.as_ptr()); if result.is_null() { return None; } else { return Some(result); } } } /// # Close TIFF File pub fn close(tiff_ptr: *mut TIFF) { unsafe { TIFFClose(tiff_ptr); } } pub fn get_width(tiff_ptr: *mut TIFF) -> u32 { let mut width: u32 = 0; unsafe { let status = TIFFGetField(tiff_ptr, TIFFTAG_IMAGEWIDTH, &mut width as *mut u32); } return width; } pub fn get_height(tiff_ptr: *mut TIFF) -> u32 { let mut height: u32 = 0; unsafe { let status = TIFFGetField(tiff_ptr, TIFFTAG_IMAGEHEIGHT, &mut height as *mut u32); } return height; } pub fn get_strip_size(tiff_ptr: *mut TIFF) -> i64 { unsafe { TIFFStripSize(tiff_ptr) } } pub fn get_strip_count(tiff_ptr: *mut TIFF) -> i64 { unsafe { TIFFNumberOfStrips(tiff_ptr) } }