summaryrefslogtreecommitdiff
path: root/src/tiff.rs
blob: 04eaf021bc2de3fe3a49a6a204fe7b72f58c7e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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<unsafe extern "C" fn(module: *const c_char, fmt: *const c_char, ap: *mut c_void)>;

/// # 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)
    }
}