libcaesium/tests/tiff.rs

178 lines
6.4 KiB
Rust
Raw Permalink Normal View History

2024-02-15 14:00:54 +01:00
use crate::cleanup::remove_compressed_test_file;
2025-02-21 13:35:34 +01:00
use caesium::parameters::{CSParameters, TiffCompression, TiffDeflateLevel};
use std::{fs::File, sync::Once};
2024-02-15 14:00:54 +01:00
mod cleanup;
static INIT: Once = Once::new();
pub fn initialize(file: &str) {
INIT.call_once(|| {
remove_compressed_test_file(file);
});
}
#[test]
fn rgb8_uncompressed() {
let output = "tests/samples/output/uncompressed_rgb8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Uncompressed;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgb8.tif"), String::from(output), &params).unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
2024-02-15 14:00:54 +01:00
remove_compressed_test_file(output)
}
#[test]
fn rgba8_uncompressed() {
let output = "tests/samples/output/uncompressed_rgba8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Uncompressed;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgba8.tif"), String::from(output), &params).unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
2024-02-15 14:00:54 +01:00
remove_compressed_test_file(output)
}
#[test]
fn rgb8_deflate() {
let output = "tests/samples/output/deflate_rgb8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Deflate;
params.tiff.deflate_level = TiffDeflateLevel::Balanced;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgb8.tif"), String::from(output), &params).unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
2024-02-15 14:00:54 +01:00
remove_compressed_test_file(output)
}
#[test]
fn rgba8_deflate() {
let output = "tests/samples/output/deflate_rgba8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Deflate;
params.tiff.deflate_level = TiffDeflateLevel::Balanced;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgba8.tif"), String::from(output), &params).unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
2024-02-15 14:00:54 +01:00
remove_compressed_test_file(output)
}
#[test]
fn rgb8_lzw() {
let output = "tests/samples/output/lzw_rgb8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Lzw;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgb8.tif"), String::from(output), &params).unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
2024-02-15 14:00:54 +01:00
remove_compressed_test_file(output)
}
#[test]
fn rgba8_lzw() {
let output = "tests/samples/output/lzw_rgba8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Lzw;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgba8.tif"), String::from(output), &params).unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
2024-02-15 14:00:54 +01:00
remove_compressed_test_file(output)
}
#[test]
fn rgb8_packbits() {
let output = "tests/samples/output/packbits_rgb8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Packbits;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgb8.tif"), String::from(output), &params).unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
2024-02-15 14:00:54 +01:00
remove_compressed_test_file(output)
}
#[test]
fn rgba8_packbits() {
let output = "tests/samples/output/packbits_rgba8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Packbits;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgba8.tif"), String::from(output), &params).unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
2024-02-15 14:00:54 +01:00
remove_compressed_test_file(output)
}
#[test]
fn rgb8_downscale() {
let output = "tests/samples/output/downscale_rgb8.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Lzw;
2024-02-15 14:00:54 +01:00
params.width = 50;
params.height = 20;
2025-02-21 13:35:34 +01:00
caesium::compress(String::from("tests/samples/rgb8.tif"), String::from(output), &params).unwrap();
assert!(std::path::Path::new(output).exists());
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
assert_eq!(image::image_dimensions(output).unwrap(), (50, 20));
remove_compressed_test_file(output)
}
#[test]
fn rgb8_downscale_to_size() {
2025-02-21 14:11:55 +01:00
let max_output_size = 100_000;
2025-02-21 13:35:34 +01:00
let output = "tests/samples/output/downscale_rgb8_to_size.tif";
initialize(output);
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Lzw;
params.width = 50;
params.height = 20;
caesium::compress_to_size(
2024-02-15 14:00:54 +01:00
String::from("tests/samples/rgb8.tif"),
String::from(output),
2025-02-21 13:35:34 +01:00
&mut params,
max_output_size,
2025-02-21 14:11:55 +01:00
true,
2024-02-15 14:00:54 +01:00
)
2025-02-21 13:35:34 +01:00
.unwrap();
2024-02-15 14:00:54 +01:00
assert!(std::path::Path::new(output).exists());
2025-02-21 13:35:34 +01:00
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff");
assert!(File::open(output).unwrap().metadata().unwrap().len() < max_output_size as u64);
2024-02-15 14:00:54 +01:00
assert_eq!(image::image_dimensions(output).unwrap(), (50, 20));
remove_compressed_test_file(output)
}
#[test]
fn unsupported() {
let output = "tests/samples/output/unsupported.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Lzw;
2024-02-15 14:00:54 +01:00
assert!(caesium::compress(
String::from("tests/samples/unsupported.tif"),
String::from(output),
&params,
2025-02-21 13:35:34 +01:00
)
.is_err());
}
#[test]
fn prevent_panic() {
let output = "tests/samples/output/panic.tif";
initialize(output);
2024-10-10 20:02:38 +02:00
let mut params = CSParameters::new();
params.tiff.algorithm = TiffCompression::Lzw;
assert!(caesium::compress(
String::from("tests/samples/unsupported.tif"),
String::from(output),
&params,
2025-02-21 13:35:34 +01:00
)
.is_err());
}