diff --git a/README.md b/README.md index 97b156a..fcdec7e 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ pub struct CSParameters { pub png: png::Parameters, pub gif: gif::Parameters, pub webp: webp::Parameters, + pub tiff: tiff::Parameters, pub keep_metadata: bool, pub optimize: bool, pub width: u32, @@ -97,6 +98,16 @@ pub struct Parameters { ``` - `quality`: in a range from 0 to 100, the quality of the resulting image. If the optimization flag is `true`, this option will be ignored. Default: `60`. +#### tiff +Supported TIFF compression is only lossless. The supported algorithms are: Lzw, Deflate, Packbits, Uncompressed. +```Rust +pub struct Parameters { + pub algorithm: TiffCompression, + pub deflate_level: DeflateLevel, +} +``` +- `deflate_level`: can be one of `Fast`, `Balanced`, `Best`. + _________________ ## Usage in C @@ -156,13 +167,15 @@ The C options struct is slightly different from the Rust one: ```Rust #[repr(C)] pub struct CCSParameters { - pub keep_metadata: bool, + pub keep_metadata: bool, pub jpeg_quality: u32, pub jpeg_chroma_subsampling: u32, pub png_quality: u32, pub png_force_zopfli: bool, pub gif_quality: u32, pub webp_quality: u32, + pub tiff_compression: u32, + pub tiff_deflate_level: u32, pub optimize: bool, pub width: u32, pub height: u32, @@ -170,6 +183,8 @@ pub struct CCSParameters { ``` The option description is the same as the Rust counterpart. Valid values for `jpeg_chroma_subsampling` are [444, 422, 420, 411]. Any other value will be ignored and will be used the default option. +Valid values for `tiff_compression` are [0 (Uncompressed), 1 (Lzw), 2 (Deflate), 3 (Packbits)]. Any other value will be ignored and `0` will be used. +Valid values for `tiff_deflate_level` are [3 (Fast), 6 (Balanced), 9 (Best)]. Any other value will be ignored and `Best` will be used. ## Download Binaries not available. Please refer to the compilation section below. diff --git a/src/interface.rs b/src/interface.rs index f82b0fc..99310dc 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -1,8 +1,10 @@ use std::ffi::{CStr, CString}; use std::os::raw::c_char; +use tiff::encoder::compression::DeflateLevel::{Balanced, Best, Fast}; use crate::jpeg::ChromaSubsampling; use crate::{compress, compress_to_size, error, initialize_parameters, CSParameters}; +use crate::tiff::TiffCompression::{Deflate, Lzw, Packbits, Uncompressed}; #[repr(C)] pub struct CCSParameters { @@ -13,6 +15,8 @@ pub struct CCSParameters { pub png_force_zopfli: bool, pub gif_quality: u32, pub webp_quality: u32, + pub tiff_compression: u32, + pub tiff_deflate_level: u32, pub optimize: bool, pub width: u32, pub height: u32, @@ -108,5 +112,18 @@ fn c_set_parameters(params: CCSParameters) -> CSParameters { _ => ChromaSubsampling::Auto, }; + parameters.tiff.algorithm = match params.tiff_compression { + 1 => Lzw, + 2 => Deflate, + 3 => Packbits, + _ => Uncompressed + }; + + parameters.tiff.deflate_level = match params.tiff_deflate_level { + 3 => Fast, + 6 => Balanced, + _ => Best + }; + parameters } diff --git a/src/lib.rs b/src/lib.rs index e405b75..8a4ffe8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -199,7 +199,14 @@ pub fn compress_to_size_in_memory( smallest_result = result; } } - smallest_result + return if return_smallest { + Ok(smallest_result) + } else { + Err(CaesiumError { + message: "Cannot compress to desired quality".into(), + code: 10202, + }) + }; } _ => loop { if tries >= max_tries { diff --git a/src/tiff.rs b/src/tiff.rs index 583489a..8b790ec 100644 --- a/src/tiff.rs +++ b/src/tiff.rs @@ -12,10 +12,10 @@ use crate::CSParameters; #[derive(Copy, Clone, PartialEq)] pub enum TiffCompression { - Lzw, - Deflate, - Packbits, - Uncompressed, + Uncompressed = 0, + Lzw = 1, + Deflate = 2, + Packbits = 3, } pub fn compress(