summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs68
-rw-r--r--src/tiff.rs89
2 files changed, 92 insertions, 65 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 457eadc..ebec3fc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,7 @@
mod tiff;
+pub const TEST_IMAGE_PATH: &str = "../test.tif";
+
#[cfg(test)]
mod tests {
use super::*;
@@ -18,7 +20,7 @@ mod tests {
#[test]
fn tiff_open_test() {
tiff::ignore_warnings();
- if let Some(result) = tiff::open("../test.tif") {
+ if let Some(result) = tiff::open(TEST_IMAGE_PATH) {
tiff::close(result);
} else {
assert!(false);
@@ -26,22 +28,10 @@ mod tests {
}
#[test]
- fn tiff_height_test() {
- tiff::ignore_warnings();
- if let Some(t_handle) = tiff::open("../test.tif") {
- let height = tiff::get_height(t_handle);
- assert!(height > 0);
- tiff::close(t_handle);
- } else {
- assert!(false);
- }
- }
-
- #[test]
fn tiff_width_test() {
tiff::ignore_warnings();
- if let Some(t_handle) = tiff::open("../test.tif") {
- let width = tiff::get_width(t_handle);
+ if let Some(t_handle) = tiff::open(TEST_IMAGE_PATH) {
+ let width = tiff::get_image_width(t_handle);
assert!(width > 0);
tiff::close(t_handle);
} else {
@@ -50,31 +40,11 @@ mod tests {
}
#[test]
- fn tiff_strip_count() {
- tiff::ignore_warnings();
- if let Some(t_handle) = tiff::open("../test.tif") {
- let strip_count = tiff::get_strip_count(t_handle);
- assert!(strip_count > 0);
- tiff::close(t_handle);
- } else {
- assert!(false);
- }
- }
-
- #[test]
- fn tiff_data_depth() {
+ fn tiff_height_test() {
tiff::ignore_warnings();
- if let Some(t_handle) = tiff::open("../test.tif") {
- let strip_size = tiff::get_strip_size(t_handle);
- let strip_count = tiff::get_strip_count(t_handle);
- let strip_depth = strip_size * strip_count;
- assert!(strip_depth > 0);
- 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);
- let strip_depth = strip_depth as f64;
- let image_depth = image_depth as f64;
+ if let Some(t_handle) = tiff::open(TEST_IMAGE_PATH) {
+ let height = tiff::get_image_height(t_handle);
+ assert!(height > 0);
tiff::close(t_handle);
} else {
assert!(false);
@@ -82,11 +52,10 @@ mod tests {
}
#[test]
- fn tiff_strip_size() {
+ fn tiff_channel_test() {
tiff::ignore_warnings();
- if let Some(t_handle) = tiff::open("../test.tif") {
- let strip_size = tiff::get_strip_size(t_handle);
- assert!(strip_size > 0);
+ if let Some(t_handle) = tiff::open(TEST_IMAGE_PATH) {
+ let channels = tiff::get_image_channels(t_handle);
tiff::close(t_handle);
} else {
assert!(false);
@@ -96,22 +65,11 @@ mod tests {
#[test]
fn tiff_read() {
tiff::ignore_warnings();
- if let Some(t_handle) = tiff::open("../test.tif") {
+ if let Some(t_handle) = tiff::open(TEST_IMAGE_PATH) {
let data = tiff::read(t_handle);
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 8e77233..53b8de9 100644
--- a/src/tiff.rs
+++ b/src/tiff.rs
@@ -60,40 +60,49 @@ pub fn close(tiff_ptr: *mut TIFF) {
}
}
-pub fn get_width(tiff_ptr: *mut TIFF) -> u32 {
+pub fn get_image_width(tiff_ptr: *mut TIFF) -> usize {
let mut width: u32 = 0;
unsafe {
let status = TIFFGetField(tiff_ptr, TIFFTAG_IMAGEWIDTH, &mut width as *mut u32);
}
- return width;
+ return width as usize;
}
-pub fn get_height(tiff_ptr: *mut TIFF) -> u32 {
+pub fn get_image_height(tiff_ptr: *mut TIFF) -> usize {
let mut height: u32 = 0;
unsafe {
let status = TIFFGetField(tiff_ptr, TIFFTAG_IMAGEHEIGHT, &mut height as *mut u32);
}
- return height;
+ return height as usize;
}
-pub fn get_strip_size(tiff_ptr: *mut TIFF) -> i64 {
+
+pub fn get_image_channels(tiff_ptr: *mut TIFF) -> usize {
+ let strip_size = get_strip_size(tiff_ptr);
+ let strip_count = get_strip_count(tiff_ptr);
+ let width = get_image_width(tiff_ptr);
+ let height = get_image_height(tiff_ptr);
+ return (strip_size * strip_count) / (width * height);
+}
+
+fn get_strip_size(tiff_ptr: *mut TIFF) -> usize {
unsafe {
- TIFFStripSize(tiff_ptr)
+ TIFFStripSize(tiff_ptr) as usize
}
}
-pub fn get_strip_count(tiff_ptr: *mut TIFF) -> i64 {
+fn get_strip_count(tiff_ptr: *mut TIFF) -> usize {
unsafe {
- TIFFNumberOfStrips(tiff_ptr)
+ TIFFNumberOfStrips(tiff_ptr) as usize
}
}
-pub fn read_strip(tiff_ptr: *mut TIFF, strip: u32) -> Vec<u8> {
+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);
+ TIFFReadRawStrip(tiff_ptr, strip, buf.as_mut_ptr() as *mut c_void, strip_size as c_longlong);
}
buf
}
@@ -113,3 +122,63 @@ pub fn read(tiff_ptr: *mut TIFF) -> Vec<u8> {
}
total_buf
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn tiff_strip_size() {
+ ignore_warnings();
+ if let Some(t_handle) = open(crate::TEST_IMAGE_PATH) {
+ let strip_size = get_strip_size(t_handle);
+ assert!(strip_size > 0);
+ close(t_handle);
+ } else {
+ assert!(false);
+ }
+ }
+
+ #[test]
+ fn tiff_strip_count() {
+ ignore_warnings();
+ if let Some(t_handle) = open(crate::TEST_IMAGE_PATH) {
+ let strip_count = get_strip_count(t_handle);
+ assert!(strip_count > 0);
+ close(t_handle);
+ } else {
+ assert!(false);
+ }
+ }
+
+ #[test]
+ fn tiff_strip_read() {
+ ignore_warnings();
+ if let Some(t_handle) = open(crate::TEST_IMAGE_PATH) {
+ let strip = read_strip(t_handle, 0);
+ close(t_handle);
+ } else {
+ assert!(false);
+ }
+ }
+
+ #[test]
+ fn tiff_data_depth() {
+ ignore_warnings();
+ if let Some(t_handle) = open(crate::TEST_IMAGE_PATH) {
+ let strip_size = get_strip_size(t_handle);
+ let strip_count = get_strip_count(t_handle);
+ let strip_depth = strip_size * strip_count;
+ assert!(strip_depth > 0);
+ let image_width = get_image_width(t_handle);
+ let image_height = get_image_height(t_handle);
+ let image_depth = image_width * image_height;
+ assert!(image_depth > 0);
+ let strip_depth = strip_depth as f64;
+ let image_depth = image_depth as f64;
+ close(t_handle);
+ } else {
+ assert!(false);
+ }
+ }
+}