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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
use std::os::raw::{c_char, c_uint, c_void, c_longlong, c_long};
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: c_uint, ...) -> c_long;
fn TIFFStripSize(tiff_ptr: *mut TIFF) -> c_longlong;
fn TIFFNumberOfStrips(tiff_ptr: *mut TIFF) -> c_longlong;
fn TIFFReadRawStrip(tiff_ptr: *mut TIFF, strip: c_uint, buf: *mut c_void, size: c_longlong) -> c_longlong;
}
/// 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)
}
}
pub fn read_strip(tiff_ptr: *mut TIFF, strip: u32) -> Vec<u8> {
let strip_size = get_strip_size(tiff_ptr);
assert!(strip_size>0);
let mut buf: Vec<u8> = vec![0u8; strip_size as usize];
unsafe {
TIFFReadRawStrip(tiff_ptr, strip, buf.as_mut_ptr() as *mut c_void, strip_size);
}
buf
}
pub fn read(tiff_ptr: *mut TIFF) -> Vec<u8> {
let strip_size = get_strip_size(tiff_ptr);
let strip_count = get_strip_count(tiff_ptr);
let mut total_buf: Vec<u8> = vec![0u8; (strip_size * strip_count) as usize];
for strip_index in 0..strip_count {
let sub_buffer: Vec<u8> = read_strip(tiff_ptr, strip_index as u32);
let base_index = strip_index * strip_size;
for data_index in 0..strip_size {
let total_index = base_index + data_index;
let data = sub_buffer[data_index as usize];
total_buf[total_index as usize] = data;
}
}
total_buf
}
|