@napi-rs/image 1.0.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +601 -22
- package/index.d.ts +346 -13
- package/index.js +69 -25
- package/package.json +23 -39
- package/.gitattributes +0 -13
- package/Cargo.toml +0 -35
- package/__test__/lossless.spec.mjs +0 -18
- package/build.rs +0 -5
- package/optimize-test.js +0 -7
- package/src/lib.rs +0 -185
- package/un-optimized.jpg +0 -0
- package/un-optimized.png +0 -0
package/Cargo.toml
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
[package]
|
|
2
|
-
edition = "2021"
|
|
3
|
-
name = "napi-rs_pngquant"
|
|
4
|
-
version = "0.0.0"
|
|
5
|
-
|
|
6
|
-
[lib]
|
|
7
|
-
crate-type = ["cdylib"]
|
|
8
|
-
|
|
9
|
-
[dependencies]
|
|
10
|
-
libc = "0.2"
|
|
11
|
-
napi = {version = "2", default-features = false, features = ["napi3"]}
|
|
12
|
-
napi-derive = {version = "2", default-features = false, features = ["type-def"]}
|
|
13
|
-
|
|
14
|
-
[target.'cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")))'.dependencies.oxipng]
|
|
15
|
-
default-features = false
|
|
16
|
-
features = ["parallel", "libdeflater"]
|
|
17
|
-
version = "5"
|
|
18
|
-
|
|
19
|
-
[target.'cfg(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"))'.dependencies.oxipng]
|
|
20
|
-
default-features = false
|
|
21
|
-
features = ["parallel"]
|
|
22
|
-
version = "5"
|
|
23
|
-
|
|
24
|
-
[target.'cfg(not(all(target_os = "linux", target_arch = "arm")))'.dependencies.mozjpeg-sys]
|
|
25
|
-
version = "1"
|
|
26
|
-
|
|
27
|
-
[target.'cfg(all(target_os = "linux", target_arch = "arm"))'.dependencies.mozjpeg-sys]
|
|
28
|
-
default-features = false
|
|
29
|
-
version = "1"
|
|
30
|
-
|
|
31
|
-
[build-dependencies]
|
|
32
|
-
napi-build = "1"
|
|
33
|
-
|
|
34
|
-
[profile.release]
|
|
35
|
-
lto = true
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { promises as fs } from 'fs'
|
|
2
|
-
|
|
3
|
-
import test from 'ava'
|
|
4
|
-
|
|
5
|
-
import { losslessCompressPng, compressJpeg } from '../index.js'
|
|
6
|
-
|
|
7
|
-
const PNG = await fs.readFile('un-optimized.png')
|
|
8
|
-
const JPEG = await fs.readFile('un-optimized.jpg')
|
|
9
|
-
|
|
10
|
-
test('should be able to lossless optimize png image', async (t) => {
|
|
11
|
-
const dest = losslessCompressPng(PNG)
|
|
12
|
-
t.true(dest.length < PNG.length)
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
test('should be able to lossless optimize jpeg image', async (t) => {
|
|
16
|
-
const dest = compressJpeg(JPEG, { quality: 100 })
|
|
17
|
-
t.true(dest.length < PNG.length)
|
|
18
|
-
})
|
package/build.rs
DELETED
package/optimize-test.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
const { readFileSync, writeFileSync } = require('fs')
|
|
2
|
-
|
|
3
|
-
const { losslessCompressPng, compressJpeg } = require('./index')
|
|
4
|
-
|
|
5
|
-
writeFileSync('optimized-lossless.png', losslessCompressPng(readFileSync('./un-optimized.png')))
|
|
6
|
-
|
|
7
|
-
writeFileSync('optimized-lossless.jpg', compressJpeg(readFileSync('./un-optimized.jpg')))
|
package/src/lib.rs
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
#![deny(clippy::all)]
|
|
2
|
-
|
|
3
|
-
use std::iter::FromIterator;
|
|
4
|
-
|
|
5
|
-
use napi::{bindgen_prelude::*, JsBuffer};
|
|
6
|
-
use napi_derive::napi;
|
|
7
|
-
|
|
8
|
-
#[napi(object, js_name = "PNGLosslessOptions")]
|
|
9
|
-
#[derive(Default)]
|
|
10
|
-
pub struct PNGLosslessOptions {
|
|
11
|
-
/// Attempt to fix errors when decoding the input file rather than returning an Err.
|
|
12
|
-
/// Default: `false`
|
|
13
|
-
pub fix_errors: Option<bool>,
|
|
14
|
-
/// Write to output even if there was no improvement in compression.
|
|
15
|
-
/// Default: `false`
|
|
16
|
-
pub force: Option<bool>,
|
|
17
|
-
/// Which filters to try on the file (0-5)
|
|
18
|
-
pub filter: Option<Vec<u32>>,
|
|
19
|
-
/// Whether to attempt bit depth reduction
|
|
20
|
-
/// Default: `true`
|
|
21
|
-
pub bit_depth_reduction: Option<bool>,
|
|
22
|
-
/// Whether to attempt color type reduction
|
|
23
|
-
/// Default: `true`
|
|
24
|
-
pub color_type_reduction: Option<bool>,
|
|
25
|
-
/// Whether to attempt palette reduction
|
|
26
|
-
/// Default: `true`
|
|
27
|
-
pub palette_reduction: Option<bool>,
|
|
28
|
-
/// Whether to attempt grayscale reduction
|
|
29
|
-
/// Default: `true`
|
|
30
|
-
pub grayscale_reduction: Option<bool>,
|
|
31
|
-
/// Whether to perform IDAT recoding
|
|
32
|
-
/// If any type of reduction is performed, IDAT recoding will be performed regardless of this setting
|
|
33
|
-
/// Default: `true`
|
|
34
|
-
pub idat_recoding: Option<bool>,
|
|
35
|
-
/// Whether to remove ***All non-critical headers*** on PNG
|
|
36
|
-
pub strip: Option<bool>,
|
|
37
|
-
/// Whether to use heuristics to pick the best filter and compression
|
|
38
|
-
pub use_heuristics: Option<bool>,
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
#[inline(always)]
|
|
42
|
-
fn to_oxipng_options(options: Option<PNGLosslessOptions>) -> oxipng::Options {
|
|
43
|
-
let opt = options.unwrap_or_default();
|
|
44
|
-
oxipng::Options {
|
|
45
|
-
fix_errors: opt.fix_errors.unwrap_or(false),
|
|
46
|
-
force: opt.force.unwrap_or(false),
|
|
47
|
-
filter: opt
|
|
48
|
-
.filter
|
|
49
|
-
.map(|v| v.into_iter().map(|i| i as u8).collect())
|
|
50
|
-
.unwrap_or_else(|| oxipng::IndexSet::from_iter(0..5)),
|
|
51
|
-
bit_depth_reduction: opt.bit_depth_reduction.unwrap_or(true),
|
|
52
|
-
color_type_reduction: opt.color_type_reduction.unwrap_or(true),
|
|
53
|
-
palette_reduction: opt.palette_reduction.unwrap_or(true),
|
|
54
|
-
grayscale_reduction: opt.grayscale_reduction.unwrap_or(true),
|
|
55
|
-
idat_recoding: opt.idat_recoding.unwrap_or(true),
|
|
56
|
-
strip: opt
|
|
57
|
-
.strip
|
|
58
|
-
.map(|s| {
|
|
59
|
-
if s {
|
|
60
|
-
oxipng::Headers::All
|
|
61
|
-
} else {
|
|
62
|
-
oxipng::Headers::None
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
.unwrap_or(oxipng::Headers::All),
|
|
66
|
-
use_heuristics: opt.use_heuristics.unwrap_or(true),
|
|
67
|
-
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")))]
|
|
68
|
-
deflate: oxipng::Deflaters::Libdeflater,
|
|
69
|
-
..Default::default()
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
#[napi]
|
|
74
|
-
pub fn lossless_compress_png(input: Buffer, options: Option<PNGLosslessOptions>) -> Result<Buffer> {
|
|
75
|
-
let output = oxipng::optimize_from_memory(input.as_ref(), &to_oxipng_options(options))
|
|
76
|
-
.map_err(|err| Error::new(Status::InvalidArg, format!("Optimize failed {}", err)))?;
|
|
77
|
-
Ok(output.into())
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
#[napi(object)]
|
|
81
|
-
#[derive(Default)]
|
|
82
|
-
pub struct JpegCompressOptions {
|
|
83
|
-
/// Output quality, default is 100 (lossless)
|
|
84
|
-
pub quality: Option<u32>,
|
|
85
|
-
/// If true, it will use MozJPEG’s scan optimization. Makes progressive image files smaller.
|
|
86
|
-
/// Default is `true`
|
|
87
|
-
pub optimize_scans: Option<bool>,
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
#[napi]
|
|
91
|
-
pub unsafe fn compress_jpeg(
|
|
92
|
-
env: Env,
|
|
93
|
-
input: Buffer,
|
|
94
|
-
options: Option<JpegCompressOptions>,
|
|
95
|
-
) -> Result<JsBuffer> {
|
|
96
|
-
std::panic::catch_unwind(|| {
|
|
97
|
-
let opts = options.unwrap_or_default();
|
|
98
|
-
let mut de_c_info: mozjpeg_sys::jpeg_decompress_struct = std::mem::zeroed();
|
|
99
|
-
let mut err_handler = create_error_handler();
|
|
100
|
-
de_c_info.common.err = &mut err_handler;
|
|
101
|
-
mozjpeg_sys::jpeg_create_decompress(&mut de_c_info);
|
|
102
|
-
let input_buf = input.as_ref();
|
|
103
|
-
#[cfg(any(target_os = "windows", target_arch = "arm"))]
|
|
104
|
-
mozjpeg_sys::jpeg_mem_src(&mut de_c_info, input_buf.as_ptr(), input_buf.len() as u32);
|
|
105
|
-
#[cfg(not(any(target_os = "windows", target_arch = "arm")))]
|
|
106
|
-
mozjpeg_sys::jpeg_mem_src(&mut de_c_info, input_buf.as_ptr(), input_buf.len() as u64);
|
|
107
|
-
let mut compress_c_info: mozjpeg_sys::jpeg_compress_struct = std::mem::zeroed();
|
|
108
|
-
compress_c_info.optimize_coding = 1;
|
|
109
|
-
compress_c_info.common.err = &mut err_handler;
|
|
110
|
-
mozjpeg_sys::jpeg_create_compress(&mut compress_c_info);
|
|
111
|
-
mozjpeg_sys::jpeg_read_header(&mut de_c_info, 1);
|
|
112
|
-
let src_coef_arrays = mozjpeg_sys::jpeg_read_coefficients(&mut de_c_info);
|
|
113
|
-
mozjpeg_sys::jpeg_copy_critical_parameters(&de_c_info, &mut compress_c_info);
|
|
114
|
-
if let Some(quality) = opts.quality {
|
|
115
|
-
mozjpeg_sys::jpeg_set_quality(&mut compress_c_info, quality as i32, 0);
|
|
116
|
-
}
|
|
117
|
-
if opts.optimize_scans.unwrap_or(true) {
|
|
118
|
-
mozjpeg_sys::jpeg_c_set_bool_param(
|
|
119
|
-
&mut compress_c_info,
|
|
120
|
-
mozjpeg_sys::J_BOOLEAN_PARAM::JBOOLEAN_OPTIMIZE_SCANS,
|
|
121
|
-
1,
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
mozjpeg_sys::jpeg_c_set_int_param(
|
|
125
|
-
&mut compress_c_info,
|
|
126
|
-
mozjpeg_sys::J_INT_PARAM::JINT_DC_SCAN_OPT_MODE,
|
|
127
|
-
0,
|
|
128
|
-
);
|
|
129
|
-
let mut buf = std::ptr::null_mut();
|
|
130
|
-
let mut outsize = 0;
|
|
131
|
-
mozjpeg_sys::jpeg_mem_dest(&mut compress_c_info, &mut buf, &mut outsize);
|
|
132
|
-
mozjpeg_sys::jpeg_write_coefficients(&mut compress_c_info, src_coef_arrays);
|
|
133
|
-
mozjpeg_sys::jpeg_finish_compress(&mut compress_c_info);
|
|
134
|
-
mozjpeg_sys::jpeg_finish_decompress(&mut de_c_info);
|
|
135
|
-
env
|
|
136
|
-
.create_buffer_with_borrowed_data(
|
|
137
|
-
buf,
|
|
138
|
-
outsize as usize,
|
|
139
|
-
(de_c_info, compress_c_info, buf),
|
|
140
|
-
|(mut input, mut output, buf), _| {
|
|
141
|
-
mozjpeg_sys::jpeg_destroy_decompress(&mut input);
|
|
142
|
-
mozjpeg_sys::jpeg_destroy_compress(&mut output);
|
|
143
|
-
libc::free(buf as *mut std::ffi::c_void);
|
|
144
|
-
},
|
|
145
|
-
)
|
|
146
|
-
.map(|v| v.into_raw())
|
|
147
|
-
})
|
|
148
|
-
.map_err(|err| {
|
|
149
|
-
Error::new(
|
|
150
|
-
Status::GenericFailure,
|
|
151
|
-
format!("Compress JPEG failed {:?}", err),
|
|
152
|
-
)
|
|
153
|
-
})
|
|
154
|
-
.and_then(|v| v)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
unsafe fn create_error_handler() -> mozjpeg_sys::jpeg_error_mgr {
|
|
158
|
-
let mut err: mozjpeg_sys::jpeg_error_mgr = std::mem::zeroed();
|
|
159
|
-
mozjpeg_sys::jpeg_std_error(&mut err);
|
|
160
|
-
err.error_exit = Some(unwind_error_exit);
|
|
161
|
-
err.emit_message = Some(silence_message);
|
|
162
|
-
err
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
extern "C" fn unwind_error_exit(cinfo: &mut mozjpeg_sys::jpeg_common_struct) {
|
|
166
|
-
let message = unsafe {
|
|
167
|
-
let err = cinfo.err.as_ref().unwrap();
|
|
168
|
-
match err.format_message {
|
|
169
|
-
Some(fmt) => {
|
|
170
|
-
let buffer = std::mem::zeroed();
|
|
171
|
-
fmt(cinfo, &buffer);
|
|
172
|
-
let len = buffer.iter().take_while(|&&c| c != 0).count();
|
|
173
|
-
String::from_utf8_lossy(&buffer[..len]).into()
|
|
174
|
-
}
|
|
175
|
-
None => format!("libjpeg error: {}", err.msg_code),
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
std::panic::resume_unwind(Box::new(message))
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
extern "C" fn silence_message(
|
|
182
|
-
_cinfo: &mut mozjpeg_sys::jpeg_common_struct,
|
|
183
|
-
_level: std::os::raw::c_int,
|
|
184
|
-
) {
|
|
185
|
-
}
|
package/un-optimized.jpg
DELETED
|
Binary file
|
package/un-optimized.png
DELETED
|
Binary file
|