Updated README and compress to size for TIFF

This commit is contained in:
Matteo Paonessa 2024-02-15 14:25:15 +01:00
parent 77ba5b7bd0
commit e193e132a3
4 changed files with 45 additions and 6 deletions

View File

@ -47,6 +47,7 @@ pub struct CSParameters {
pub png: png::Parameters, pub png: png::Parameters,
pub gif: gif::Parameters, pub gif: gif::Parameters,
pub webp: webp::Parameters, pub webp: webp::Parameters,
pub tiff: tiff::Parameters,
pub keep_metadata: bool, pub keep_metadata: bool,
pub optimize: bool, pub optimize: bool,
pub width: u32, 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`. - `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 ## Usage in C
@ -163,6 +174,8 @@ pub struct CCSParameters {
pub png_force_zopfli: bool, pub png_force_zopfli: bool,
pub gif_quality: u32, pub gif_quality: u32,
pub webp_quality: u32, pub webp_quality: u32,
pub tiff_compression: u32,
pub tiff_deflate_level: u32,
pub optimize: bool, pub optimize: bool,
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,
@ -170,6 +183,8 @@ pub struct CCSParameters {
``` ```
The option description is the same as the Rust counterpart. 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 `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 ## Download
Binaries not available. Please refer to the compilation section below. Binaries not available. Please refer to the compilation section below.

View File

@ -1,8 +1,10 @@
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::os::raw::c_char; use std::os::raw::c_char;
use tiff::encoder::compression::DeflateLevel::{Balanced, Best, Fast};
use crate::jpeg::ChromaSubsampling; use crate::jpeg::ChromaSubsampling;
use crate::{compress, compress_to_size, error, initialize_parameters, CSParameters}; use crate::{compress, compress_to_size, error, initialize_parameters, CSParameters};
use crate::tiff::TiffCompression::{Deflate, Lzw, Packbits, Uncompressed};
#[repr(C)] #[repr(C)]
pub struct CCSParameters { pub struct CCSParameters {
@ -13,6 +15,8 @@ pub struct CCSParameters {
pub png_force_zopfli: bool, pub png_force_zopfli: bool,
pub gif_quality: u32, pub gif_quality: u32,
pub webp_quality: u32, pub webp_quality: u32,
pub tiff_compression: u32,
pub tiff_deflate_level: u32,
pub optimize: bool, pub optimize: bool,
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,
@ -108,5 +112,18 @@ fn c_set_parameters(params: CCSParameters) -> CSParameters {
_ => ChromaSubsampling::Auto, _ => 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 parameters
} }

View File

@ -199,7 +199,14 @@ pub fn compress_to_size_in_memory(
smallest_result = result; 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 { _ => loop {
if tries >= max_tries { if tries >= max_tries {

View File

@ -12,10 +12,10 @@ use crate::CSParameters;
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub enum TiffCompression { pub enum TiffCompression {
Lzw, Uncompressed = 0,
Deflate, Lzw = 1,
Packbits, Deflate = 2,
Uncompressed, Packbits = 3,
} }
pub fn compress( pub fn compress(