Merge branch 'master' into chunk4
This commit is contained in:
commit
f758de44c4
3 changed files with 39 additions and 19 deletions
|
|
@ -47,8 +47,8 @@ version = "^2.10.0"
|
||||||
optional = true
|
optional = true
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
|
|
||||||
[dependencies.cloudflare-zlib-sys]
|
[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
|
||||||
version = "^0.1.2"
|
cloudflare-zlib-sys = "^0.1.2"
|
||||||
|
|
||||||
[dependencies.image]
|
[dependencies.image]
|
||||||
default-features = false
|
default-features = false
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ pub fn inflate(data: &[u8]) -> PngResult<Vec<u8>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compress a data stream using the DEFLATE algorithm
|
/// Compress a data stream using the DEFLATE algorithm
|
||||||
|
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
|
||||||
pub fn deflate(data: &[u8], zc: u8, zs: u8, zw: u8, max_size: &AtomicMin) -> PngResult<Vec<u8>> {
|
pub fn deflate(data: &[u8], zc: u8, zs: u8, zw: u8, max_size: &AtomicMin) -> PngResult<Vec<u8>> {
|
||||||
if is_cfzlib_supported() {
|
if is_cfzlib_supported() {
|
||||||
return cfzlib_deflate(data, zc, zs, zw, max_size);
|
return cfzlib_deflate(data, zc, zs, zw, max_size);
|
||||||
|
|
@ -23,22 +24,29 @@ pub fn deflate(data: &[u8], zc: u8, zs: u8, zw: u8, max_size: &AtomicMin) -> Png
|
||||||
miniz_stream::compress_to_vec_oxipng(data, zc, zw.into(), zs.into(), max_size)
|
miniz_stream::compress_to_vec_oxipng(data, zc, zw.into(), zs.into(), max_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Compress a data stream using the DEFLATE algorithm
|
||||||
|
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
|
||||||
|
pub fn deflate(data: &[u8], zc: u8, zs: u8, zw: u8, max_size: &AtomicMin) -> PngResult<Vec<u8>> {
|
||||||
|
miniz_stream::compress_to_vec_oxipng(data, zc, zw.into(), zs.into(), max_size)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "x86_64")]
|
||||||
fn is_cfzlib_supported() -> bool {
|
fn is_cfzlib_supported() -> bool {
|
||||||
#[cfg(target_arch = "x86_64")]
|
if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
|
||||||
{
|
return true;
|
||||||
if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[cfg(target_arch = "aarch64")]
|
|
||||||
{
|
|
||||||
if is_arm_feature_detected!("neon") && is_arm_feature_detected!("crc") {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "aarch64")]
|
||||||
|
fn is_cfzlib_supported() -> bool {
|
||||||
|
if is_arm_feature_detected!("neon") && is_arm_feature_detected!("crc") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
|
||||||
pub fn cfzlib_deflate(
|
pub fn cfzlib_deflate(
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
level: u8,
|
level: u8,
|
||||||
|
|
|
||||||
24
src/lib.rs
24
src/lib.rs
|
|
@ -1,5 +1,6 @@
|
||||||
extern crate bit_vec;
|
extern crate bit_vec;
|
||||||
extern crate byteorder;
|
extern crate byteorder;
|
||||||
|
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
|
||||||
extern crate cloudflare_zlib_sys;
|
extern crate cloudflare_zlib_sys;
|
||||||
extern crate crc;
|
extern crate crc;
|
||||||
extern crate image;
|
extern crate image;
|
||||||
|
|
@ -687,8 +688,15 @@ fn optimize_png(png: &mut PngData, original_data: &[u8], opts: &Options) -> PngR
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let old_png = image::load_from_memory_with_format(original_data, ImageFormat::PNG);
|
let (old_png, new_png) = {
|
||||||
let new_png = image::load_from_memory_with_format(&output, ImageFormat::PNG);
|
let old_png = || image::load_from_memory_with_format(original_data, ImageFormat::PNG);
|
||||||
|
let new_png = || image::load_from_memory_with_format(&output, ImageFormat::PNG);
|
||||||
|
#[cfg(feature = "parallel")]
|
||||||
|
let res = rayon::join(old_png, new_png);
|
||||||
|
#[cfg(not(feature = "parallel"))]
|
||||||
|
let res = (old_png(), new_png());
|
||||||
|
res
|
||||||
|
};
|
||||||
|
|
||||||
if let Ok(new_png) = new_png {
|
if let Ok(new_png) = new_png {
|
||||||
if let Ok(old_png) = old_png {
|
if let Ok(old_png) = old_png {
|
||||||
|
|
@ -893,11 +901,15 @@ fn copy_permissions(input_path: &Path, out_file: &File, verbosity: Option<u8>) {
|
||||||
fn images_equal(old_png: &DynamicImage, new_png: &DynamicImage) -> bool {
|
fn images_equal(old_png: &DynamicImage, new_png: &DynamicImage) -> bool {
|
||||||
let a = old_png
|
let a = old_png
|
||||||
.pixels()
|
.pixels()
|
||||||
.map(|x| x.2.channels().to_owned())
|
.filter(|x| {
|
||||||
.filter(|p| !(p.len() == 4 && p[3] == 0));
|
let p = x.2.channels();
|
||||||
|
!(p.len() == 4 && p[3] == 0)
|
||||||
|
});
|
||||||
let b = new_png
|
let b = new_png
|
||||||
.pixels()
|
.pixels()
|
||||||
.map(|x| x.2.channels().to_owned())
|
.filter(|x| {
|
||||||
.filter(|p| !(p.len() == 4 && p[3] == 0));
|
let p = x.2.channels();
|
||||||
|
!(p.len() == 4 && p[3] == 0)
|
||||||
|
});
|
||||||
a.eq(b)
|
a.eq(b)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue