Allowing to set oxipng optimization level
This commit is contained in:
parent
9ee4b5b2b4
commit
860ce1b597
|
@ -74,11 +74,13 @@ pub struct Parameters {
|
|||
```Rust
|
||||
pub struct Parameters {
|
||||
pub quality: u32,
|
||||
pub force_zopfli: bool
|
||||
pub force_zopfli: bool,
|
||||
pub deflater_level: u32
|
||||
}
|
||||
```
|
||||
- `quality`: in a range from 0 to 100, the quality of the resulting image. Default `80`.
|
||||
- `force_zopfli`: if `optimization` is `true` and this option is also `true`, will use zopfli algorithm for compression, resulting in a smaller image, but it may take minutes to finish the process. Default `false`.
|
||||
- `optimization_level`: if `optimization` is `true` will set the level of oxipng optimization, from 1 to 6. Default `3`.
|
||||
|
||||
#### gif
|
||||
GIF support is experimental, has many know issues and does not support optimization. Expect bugs (especially on Windows).
|
||||
|
@ -171,6 +173,7 @@ pub struct CCSParameters {
|
|||
pub jpeg_quality: u32,
|
||||
pub jpeg_chroma_subsampling: u32,
|
||||
pub png_quality: u32,
|
||||
pub png_optimization_level: u32,
|
||||
pub png_force_zopfli: bool,
|
||||
pub gif_quality: u32,
|
||||
pub webp_quality: u32,
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use std::ffi::CString;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
|
||||
use crate::error::CaesiumError;
|
||||
use crate::CSParameters;
|
||||
use crate::error::CaesiumError;
|
||||
|
||||
pub fn compress(
|
||||
input_path: String,
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::c_char;
|
||||
|
||||
use tiff::encoder::compression::DeflateLevel::{Balanced, Best, Fast};
|
||||
|
||||
use crate::{compress, compress_to_size, CSParameters, error, initialize_parameters};
|
||||
use crate::jpeg::ChromaSubsampling;
|
||||
use crate::{compress, compress_to_size, error, initialize_parameters, CSParameters};
|
||||
use crate::tiff::TiffCompression::{Deflate, Lzw, Packbits, Uncompressed};
|
||||
|
||||
#[repr(C)]
|
||||
|
@ -12,6 +13,7 @@ pub struct CCSParameters {
|
|||
pub jpeg_quality: u32,
|
||||
pub jpeg_chroma_subsampling: u32,
|
||||
pub png_quality: u32,
|
||||
pub png_optimization_level: bool,
|
||||
pub png_force_zopfli: bool,
|
||||
pub gif_quality: u32,
|
||||
pub webp_quality: u32,
|
||||
|
@ -98,6 +100,7 @@ fn c_set_parameters(params: CCSParameters) -> CSParameters {
|
|||
parameters.png.quality = params.png_quality;
|
||||
parameters.optimize = params.optimize;
|
||||
parameters.keep_metadata = params.keep_metadata;
|
||||
parameters.png.optimization_level = params.png_optimization_level as u8;
|
||||
parameters.png.force_zopfli = params.png_force_zopfli;
|
||||
parameters.gif.quality = params.gif_quality;
|
||||
parameters.webp.quality = params.webp_quality;
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
use std::{fs, ptr};
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::mem;
|
||||
use std::panic::catch_unwind;
|
||||
use std::{fs, ptr};
|
||||
|
||||
use image::ImageOutputFormat::Jpeg;
|
||||
use img_parts::jpeg::Jpeg as PartsJpeg;
|
||||
use img_parts::{ImageEXIF, ImageICC};
|
||||
use img_parts::jpeg::Jpeg as PartsJpeg;
|
||||
use libc::free;
|
||||
use mozjpeg_sys::*;
|
||||
|
||||
use crate::CSParameters;
|
||||
use crate::error::CaesiumError;
|
||||
use crate::resize::resize;
|
||||
use crate::CSParameters;
|
||||
|
||||
static mut JPEG_ERROR: c_int = 0;
|
||||
|
||||
|
|
11
src/lib.rs
11
src/lib.rs
|
@ -1,8 +1,8 @@
|
|||
extern crate alloc;
|
||||
|
||||
use std::{cmp, fs};
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::{cmp, fs};
|
||||
|
||||
use ::tiff::encoder::compression::DeflateLevel;
|
||||
use ::tiff::encoder::compression::DeflateLevel::Best;
|
||||
|
@ -39,6 +39,7 @@ pub struct JpegParameters {
|
|||
pub struct PngParameters {
|
||||
pub quality: u32,
|
||||
pub force_zopfli: bool,
|
||||
pub optimization_level: u8
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
|
@ -79,6 +80,7 @@ pub fn initialize_parameters() -> CSParameters {
|
|||
let png = PngParameters {
|
||||
quality: 80,
|
||||
force_zopfli: false,
|
||||
optimization_level: 3,
|
||||
};
|
||||
let gif = GifParameters { quality: 80 };
|
||||
let webp = WebPParameters { quality: 80 };
|
||||
|
@ -327,6 +329,13 @@ fn validate_parameters(parameters: &CSParameters) -> error::Result<()> {
|
|||
});
|
||||
}
|
||||
|
||||
if parameters.png.optimization_level > 6 {
|
||||
return Err(CaesiumError {
|
||||
message: "Invalid PNG optimization level".into(),
|
||||
code: 10006,
|
||||
});
|
||||
}
|
||||
|
||||
if parameters.gif.quality > 100 {
|
||||
return Err(CaesiumError {
|
||||
message: "Invalid GIF quality value".into(),
|
||||
|
|
|
@ -4,11 +4,11 @@ use std::io::Write;
|
|||
use std::num::NonZeroU8;
|
||||
|
||||
use image::ImageOutputFormat;
|
||||
use oxipng::Deflaters::{Libdeflater, Zopfli};
|
||||
use oxipng::Deflaters::Zopfli;
|
||||
|
||||
use crate::CSParameters;
|
||||
use crate::error::CaesiumError;
|
||||
use crate::resize::resize;
|
||||
use crate::CSParameters;
|
||||
|
||||
pub fn compress(
|
||||
input_path: String,
|
||||
|
@ -122,8 +122,7 @@ fn lossless(in_file: Vec<u8>, parameters: &CSParameters) -> Result<Vec<u8>, Caes
|
|||
iterations: NonZeroU8::new(15).unwrap(),
|
||||
};
|
||||
} else {
|
||||
oxipng_options = oxipng::Options::from_preset(3);
|
||||
oxipng_options.deflate = Libdeflater { compression: 6 };
|
||||
oxipng_options = oxipng::Options::from_preset(parameters.png.optimization_level);
|
||||
}
|
||||
|
||||
let optimized_png =
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use std::io::Cursor;
|
||||
|
||||
use image::DynamicImage;
|
||||
use image::imageops::FilterType;
|
||||
use image::io::Reader as ImageReader;
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::error::CaesiumError;
|
||||
|
||||
|
|
|
@ -6,9 +6,9 @@ use tiff::encoder::colortype::{RGB8, RGBA8};
|
|||
use tiff::encoder::compression::{Deflate, Lzw, Packbits, Uncompressed};
|
||||
use tiff::encoder::TiffEncoder;
|
||||
|
||||
use crate::CSParameters;
|
||||
use crate::error::CaesiumError;
|
||||
use crate::resize::resize_image;
|
||||
use crate::CSParameters;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub enum TiffCompression {
|
||||
|
|
|
@ -2,9 +2,9 @@ use std::fs::File;
|
|||
use std::io::{Read, Write};
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::CSParameters;
|
||||
use crate::error::CaesiumError;
|
||||
use crate::resize::resize_image;
|
||||
use crate::CSParameters;
|
||||
|
||||
pub fn compress(
|
||||
input_path: String,
|
||||
|
|
Loading…
Reference in New Issue