summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcc <cc@localhost>2025-08-20 17:39:28 -0700
committercc <cc@localhost>2025-08-20 17:39:46 -0700
commit972a413ebf54edbdd777379f1db7ebb6731f5ecc (patch)
treec013d8bf0322d02c7e8786252353eddc331c6757 /src
parentb248c22d0d69643f33f779c65882f1158ed8f959 (diff)
Read TIFF Data
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs18
-rw-r--r--src/tiff.rs19
2 files changed, 26 insertions, 11 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 9c2033d..4493a14 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -31,7 +31,6 @@ mod tests {
if let Some(t_handle) = tiff::open("../test.tif") {
let height = tiff::get_height(t_handle);
assert!(height > 0);
- println!("{:?}", height);
tiff::close(t_handle);
} else {
assert!(false);
@@ -44,7 +43,6 @@ mod tests {
if let Some(t_handle) = tiff::open("../test.tif") {
let width = tiff::get_width(t_handle);
assert!(width > 0);
- println!("{:?}", width);
tiff::close(t_handle);
} else {
assert!(false);
@@ -57,7 +55,6 @@ mod tests {
if let Some(t_handle) = tiff::open("../test.tif") {
let strip_count = tiff::get_strip_count(t_handle);
assert!(strip_count > 0);
- println!("{:?}", strip_count);
tiff::close(t_handle);
} else {
assert!(false);
@@ -72,15 +69,12 @@ mod tests {
let strip_count = tiff::get_strip_count(t_handle);
let strip_depth = strip_size * strip_count;
assert!(strip_depth > 0);
- println!("{:?}", strip_depth);
let image_width = tiff::get_width(t_handle);
let image_height = tiff::get_height(t_handle);
let image_depth = image_width * image_height;
assert!(image_depth > 0);
- println!("{:?}", image_depth);
let strip_depth = strip_depth as f64;
let image_depth = image_depth as f64;
- println!("{:?}", strip_depth / image_depth);
tiff::close(t_handle);
} else {
assert!(false);
@@ -93,7 +87,17 @@ mod tests {
if let Some(t_handle) = tiff::open("../test.tif") {
let strip_size = tiff::get_strip_size(t_handle);
assert!(strip_size > 0);
- println!("{:?}", strip_size);
+ tiff::close(t_handle);
+ } else {
+ assert!(false);
+ }
+ }
+
+ #[test]
+ fn tiff_strip_read() {
+ tiff::ignore_warnings();
+ if let Some(t_handle) = tiff::open("../test.tif") {
+ let strip = tiff::read_strip(t_handle, 0);
tiff::close(t_handle);
} else {
assert!(false);
diff --git a/src/tiff.rs b/src/tiff.rs
index 04eaf02..08f4d54 100644
--- a/src/tiff.rs
+++ b/src/tiff.rs
@@ -1,4 +1,4 @@
-use std::os::raw::{c_char, c_void};
+use std::os::raw::{c_char, c_uint, c_void, c_longlong, c_long};
use std::ffi::CString;
/// # TIFF Internal Structure
@@ -21,9 +21,10 @@ 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;
+ 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
@@ -86,3 +87,13 @@ pub fn get_strip_count(tiff_ptr: *mut TIFF) -> i64 {
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
+}