@napi-rs/image 1.0.0
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/.gitattributes +13 -0
- package/Cargo.toml +35 -0
- package/LICENSE +21 -0
- package/README.md +91 -0
- package/__test__/lossless.spec.mjs +18 -0
- package/build.rs +5 -0
- package/index.d.ts +66 -0
- package/index.js +209 -0
- package/optimize-test.js +7 -0
- package/package.json +97 -0
- package/src/lib.rs +185 -0
- package/un-optimized.jpg +0 -0
- package/un-optimized.png +0 -0
package/.gitattributes
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Auto detect text files and perform LF normalization
|
|
2
|
+
* text=auto
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
*.ts text eol=lf merge=union
|
|
6
|
+
*.tsx text eol=lf merge=union
|
|
7
|
+
*.rs text eol=lf merge=union
|
|
8
|
+
*.js text eol=lf merge=union
|
|
9
|
+
*.json text eol=lf merge=union
|
|
10
|
+
*.debug text eol=lf merge=union
|
|
11
|
+
|
|
12
|
+
index.js linguist-detectable=false
|
|
13
|
+
index.d.ts inguist-detectable=false
|
package/Cargo.toml
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-present LongYinan(github@lyn.one)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# `@napi-rs/image`
|
|
2
|
+
|
|
3
|
+
Image processing library.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
[](https://packagephobia.com/result?p=@napi-rs/image)
|
|
7
|
+
[](https://npmcharts.com/compare/@napi-rs/image?minimal=true)
|
|
8
|
+
|
|
9
|
+
## Support matrix
|
|
10
|
+
|
|
11
|
+
| | node10 | node12 | node14 | node16 | node17 |
|
|
12
|
+
| --------------------- | ------ | ------ | ------ | ------ | ------ |
|
|
13
|
+
| Windows x64 | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
14
|
+
| Windows x32 | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
15
|
+
| macOS x64 | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
16
|
+
| macOS arm64 (m chips) | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
17
|
+
| Linux x64 gnu | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
18
|
+
| Linux x64 musl | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
19
|
+
| Linux arm gnu | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
20
|
+
| Linux arm64 gnu | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
21
|
+
| Linux arm64 musl | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
22
|
+
| Android arm64 | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
23
|
+
| Android armv7 | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
24
|
+
| FreeBSD x64 | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
25
|
+
|
|
26
|
+
## Lossless compression
|
|
27
|
+
|
|
28
|
+
### `PNG`
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
export interface PNGLosslessOptions {
|
|
32
|
+
/**
|
|
33
|
+
* Attempt to fix errors when decoding the input file rather than returning an Err.
|
|
34
|
+
* Default: `false`
|
|
35
|
+
*/
|
|
36
|
+
fixErrors?: boolean | undefined | null
|
|
37
|
+
/**
|
|
38
|
+
* Write to output even if there was no improvement in compression.
|
|
39
|
+
* Default: `false`
|
|
40
|
+
*/
|
|
41
|
+
force?: boolean | undefined | null
|
|
42
|
+
/** Which filters to try on the file (0-5) */
|
|
43
|
+
filter?: Array<number> | undefined | null
|
|
44
|
+
/**
|
|
45
|
+
* Whether to attempt bit depth reduction
|
|
46
|
+
* Default: `true`
|
|
47
|
+
*/
|
|
48
|
+
bitDepthReduction?: boolean | undefined | null
|
|
49
|
+
/**
|
|
50
|
+
* Whether to attempt color type reduction
|
|
51
|
+
* Default: `true`
|
|
52
|
+
*/
|
|
53
|
+
colorTypeReduction?: boolean | undefined | null
|
|
54
|
+
/**
|
|
55
|
+
* Whether to attempt palette reduction
|
|
56
|
+
* Default: `true`
|
|
57
|
+
*/
|
|
58
|
+
paletteReduction?: boolean | undefined | null
|
|
59
|
+
/**
|
|
60
|
+
* Whether to attempt grayscale reduction
|
|
61
|
+
* Default: `true`
|
|
62
|
+
*/
|
|
63
|
+
grayscaleReduction?: boolean | undefined | null
|
|
64
|
+
/**
|
|
65
|
+
* Whether to perform IDAT recoding
|
|
66
|
+
* If any type of reduction is performed, IDAT recoding will be performed regardless of this setting
|
|
67
|
+
* Default: `true`
|
|
68
|
+
*/
|
|
69
|
+
idatRecoding?: boolean | undefined | null
|
|
70
|
+
/** Whether to remove ***All non-critical headers*** on PNG */
|
|
71
|
+
strip?: boolean | undefined | null
|
|
72
|
+
/** Whether to use heuristics to pick the best filter and compression */
|
|
73
|
+
useHeuristics?: boolean | undefined | null
|
|
74
|
+
}
|
|
75
|
+
export function losslessCompressPng(input: Buffer, options?: PNGLosslessOptions | undefined | null): Buffer
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### `JPEG`
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
export interface JpegCompressOptions {
|
|
82
|
+
/** Output quality, default is 100 (lossless) */
|
|
83
|
+
quality?: number | undefined | null
|
|
84
|
+
/**
|
|
85
|
+
* If true, it will use MozJPEG’s scan optimization. Makes progressive image files smaller.
|
|
86
|
+
* Default is `true`
|
|
87
|
+
*/
|
|
88
|
+
optimizeScans?: boolean | undefined | null
|
|
89
|
+
}
|
|
90
|
+
export function compressJpeg(input: Buffer, options?: JpegCompressOptions | undefined | null): Buffer
|
|
91
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
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
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/* auto-generated by NAPI-RS */
|
|
5
|
+
|
|
6
|
+
export class ExternalObject<T> {
|
|
7
|
+
readonly '': {
|
|
8
|
+
readonly '': unique symbol
|
|
9
|
+
[K: symbol]: T
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export interface PNGLosslessOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Attempt to fix errors when decoding the input file rather than returning an Err.
|
|
15
|
+
* Default: `false`
|
|
16
|
+
*/
|
|
17
|
+
fixErrors?: boolean | undefined | null
|
|
18
|
+
/**
|
|
19
|
+
* Write to output even if there was no improvement in compression.
|
|
20
|
+
* Default: `false`
|
|
21
|
+
*/
|
|
22
|
+
force?: boolean | undefined | null
|
|
23
|
+
/** Which filters to try on the file (0-5) */
|
|
24
|
+
filter?: Array<number> | undefined | null
|
|
25
|
+
/**
|
|
26
|
+
* Whether to attempt bit depth reduction
|
|
27
|
+
* Default: `true`
|
|
28
|
+
*/
|
|
29
|
+
bitDepthReduction?: boolean | undefined | null
|
|
30
|
+
/**
|
|
31
|
+
* Whether to attempt color type reduction
|
|
32
|
+
* Default: `true`
|
|
33
|
+
*/
|
|
34
|
+
colorTypeReduction?: boolean | undefined | null
|
|
35
|
+
/**
|
|
36
|
+
* Whether to attempt palette reduction
|
|
37
|
+
* Default: `true`
|
|
38
|
+
*/
|
|
39
|
+
paletteReduction?: boolean | undefined | null
|
|
40
|
+
/**
|
|
41
|
+
* Whether to attempt grayscale reduction
|
|
42
|
+
* Default: `true`
|
|
43
|
+
*/
|
|
44
|
+
grayscaleReduction?: boolean | undefined | null
|
|
45
|
+
/**
|
|
46
|
+
* Whether to perform IDAT recoding
|
|
47
|
+
* If any type of reduction is performed, IDAT recoding will be performed regardless of this setting
|
|
48
|
+
* Default: `true`
|
|
49
|
+
*/
|
|
50
|
+
idatRecoding?: boolean | undefined | null
|
|
51
|
+
/** Whether to remove ***All non-critical headers*** on PNG */
|
|
52
|
+
strip?: boolean | undefined | null
|
|
53
|
+
/** Whether to use heuristics to pick the best filter and compression */
|
|
54
|
+
useHeuristics?: boolean | undefined | null
|
|
55
|
+
}
|
|
56
|
+
export function losslessCompressPng(input: Buffer, options?: PNGLosslessOptions | undefined | null): Buffer
|
|
57
|
+
export interface JpegCompressOptions {
|
|
58
|
+
/** Output quality, default is 100 (lossless) */
|
|
59
|
+
quality?: number | undefined | null
|
|
60
|
+
/**
|
|
61
|
+
* If true, it will use MozJPEG’s scan optimization. Makes progressive image files smaller.
|
|
62
|
+
* Default is `true`
|
|
63
|
+
*/
|
|
64
|
+
optimizeScans?: boolean | undefined | null
|
|
65
|
+
}
|
|
66
|
+
export function compressJpeg(input: Buffer, options?: JpegCompressOptions | undefined | null): Buffer
|
package/index.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
const { existsSync, readFileSync } = require('fs')
|
|
2
|
+
const { join } = require('path')
|
|
3
|
+
|
|
4
|
+
const { platform, arch } = process
|
|
5
|
+
|
|
6
|
+
let nativeBinding = null
|
|
7
|
+
let localFileExisted = false
|
|
8
|
+
let loadError = null
|
|
9
|
+
|
|
10
|
+
function isMusl() {
|
|
11
|
+
// For Node 10
|
|
12
|
+
if (!process.report || typeof process.report.getReport !== 'function') {
|
|
13
|
+
try {
|
|
14
|
+
return readFileSync('/usr/bin/ldd', 'utf8').includes('musl')
|
|
15
|
+
} catch (e) {
|
|
16
|
+
return false
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
const { glibcVersionRuntime } = process.report.getReport().header
|
|
20
|
+
return !Boolean(glibcVersionRuntime)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
switch (platform) {
|
|
25
|
+
case 'android':
|
|
26
|
+
if (arch !== 'arm64') {
|
|
27
|
+
throw new Error(`Unsupported architecture on Android ${arch}`)
|
|
28
|
+
}
|
|
29
|
+
localFileExisted = existsSync(join(__dirname, 'image.android-arm64.node'))
|
|
30
|
+
try {
|
|
31
|
+
if (localFileExisted) {
|
|
32
|
+
nativeBinding = require('./image.android-arm64.node')
|
|
33
|
+
} else {
|
|
34
|
+
nativeBinding = require('@napi-rs/image-android-arm64')
|
|
35
|
+
}
|
|
36
|
+
} catch (e) {
|
|
37
|
+
loadError = e
|
|
38
|
+
}
|
|
39
|
+
break
|
|
40
|
+
case 'win32':
|
|
41
|
+
switch (arch) {
|
|
42
|
+
case 'x64':
|
|
43
|
+
localFileExisted = existsSync(join(__dirname, 'image.win32-x64-msvc.node'))
|
|
44
|
+
try {
|
|
45
|
+
if (localFileExisted) {
|
|
46
|
+
nativeBinding = require('./image.win32-x64-msvc.node')
|
|
47
|
+
} else {
|
|
48
|
+
nativeBinding = require('@napi-rs/image-win32-x64-msvc')
|
|
49
|
+
}
|
|
50
|
+
} catch (e) {
|
|
51
|
+
loadError = e
|
|
52
|
+
}
|
|
53
|
+
break
|
|
54
|
+
case 'ia32':
|
|
55
|
+
localFileExisted = existsSync(join(__dirname, 'image.win32-ia32-msvc.node'))
|
|
56
|
+
try {
|
|
57
|
+
if (localFileExisted) {
|
|
58
|
+
nativeBinding = require('./image.win32-ia32-msvc.node')
|
|
59
|
+
} else {
|
|
60
|
+
nativeBinding = require('@napi-rs/image-win32-ia32-msvc')
|
|
61
|
+
}
|
|
62
|
+
} catch (e) {
|
|
63
|
+
loadError = e
|
|
64
|
+
}
|
|
65
|
+
break
|
|
66
|
+
case 'arm64':
|
|
67
|
+
localFileExisted = existsSync(join(__dirname, 'image.win32-arm64-msvc.node'))
|
|
68
|
+
try {
|
|
69
|
+
if (localFileExisted) {
|
|
70
|
+
nativeBinding = require('./image.win32-arm64-msvc.node')
|
|
71
|
+
} else {
|
|
72
|
+
nativeBinding = require('@napi-rs/image-win32-arm64-msvc')
|
|
73
|
+
}
|
|
74
|
+
} catch (e) {
|
|
75
|
+
loadError = e
|
|
76
|
+
}
|
|
77
|
+
break
|
|
78
|
+
default:
|
|
79
|
+
throw new Error(`Unsupported architecture on Windows: ${arch}`)
|
|
80
|
+
}
|
|
81
|
+
break
|
|
82
|
+
case 'darwin':
|
|
83
|
+
switch (arch) {
|
|
84
|
+
case 'x64':
|
|
85
|
+
localFileExisted = existsSync(join(__dirname, 'image.darwin-x64.node'))
|
|
86
|
+
try {
|
|
87
|
+
if (localFileExisted) {
|
|
88
|
+
nativeBinding = require('./image.darwin-x64.node')
|
|
89
|
+
} else {
|
|
90
|
+
nativeBinding = require('@napi-rs/image-darwin-x64')
|
|
91
|
+
}
|
|
92
|
+
} catch (e) {
|
|
93
|
+
loadError = e
|
|
94
|
+
}
|
|
95
|
+
break
|
|
96
|
+
case 'arm64':
|
|
97
|
+
localFileExisted = existsSync(join(__dirname, 'image.darwin-arm64.node'))
|
|
98
|
+
try {
|
|
99
|
+
if (localFileExisted) {
|
|
100
|
+
nativeBinding = require('./image.darwin-arm64.node')
|
|
101
|
+
} else {
|
|
102
|
+
nativeBinding = require('@napi-rs/image-darwin-arm64')
|
|
103
|
+
}
|
|
104
|
+
} catch (e) {
|
|
105
|
+
loadError = e
|
|
106
|
+
}
|
|
107
|
+
break
|
|
108
|
+
default:
|
|
109
|
+
throw new Error(`Unsupported architecture on macOS: ${arch}`)
|
|
110
|
+
}
|
|
111
|
+
break
|
|
112
|
+
case 'freebsd':
|
|
113
|
+
if (arch !== 'x64') {
|
|
114
|
+
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
|
|
115
|
+
}
|
|
116
|
+
localFileExisted = existsSync(join(__dirname, 'image.freebsd-x64.node'))
|
|
117
|
+
try {
|
|
118
|
+
if (localFileExisted) {
|
|
119
|
+
nativeBinding = require('./image.freebsd-x64.node')
|
|
120
|
+
} else {
|
|
121
|
+
nativeBinding = require('@napi-rs/image-freebsd-x64')
|
|
122
|
+
}
|
|
123
|
+
} catch (e) {
|
|
124
|
+
loadError = e
|
|
125
|
+
}
|
|
126
|
+
break
|
|
127
|
+
case 'linux':
|
|
128
|
+
switch (arch) {
|
|
129
|
+
case 'x64':
|
|
130
|
+
if (isMusl()) {
|
|
131
|
+
localFileExisted = existsSync(join(__dirname, 'image.linux-x64-musl.node'))
|
|
132
|
+
try {
|
|
133
|
+
if (localFileExisted) {
|
|
134
|
+
nativeBinding = require('./image.linux-x64-musl.node')
|
|
135
|
+
} else {
|
|
136
|
+
nativeBinding = require('@napi-rs/image-linux-x64-musl')
|
|
137
|
+
}
|
|
138
|
+
} catch (e) {
|
|
139
|
+
loadError = e
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
localFileExisted = existsSync(join(__dirname, 'image.linux-x64-gnu.node'))
|
|
143
|
+
try {
|
|
144
|
+
if (localFileExisted) {
|
|
145
|
+
nativeBinding = require('./image.linux-x64-gnu.node')
|
|
146
|
+
} else {
|
|
147
|
+
nativeBinding = require('@napi-rs/image-linux-x64-gnu')
|
|
148
|
+
}
|
|
149
|
+
} catch (e) {
|
|
150
|
+
loadError = e
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
break
|
|
154
|
+
case 'arm64':
|
|
155
|
+
if (isMusl()) {
|
|
156
|
+
localFileExisted = existsSync(join(__dirname, 'image.linux-arm64-musl.node'))
|
|
157
|
+
try {
|
|
158
|
+
if (localFileExisted) {
|
|
159
|
+
nativeBinding = require('./image.linux-arm64-musl.node')
|
|
160
|
+
} else {
|
|
161
|
+
nativeBinding = require('@napi-rs/image-linux-arm64-musl')
|
|
162
|
+
}
|
|
163
|
+
} catch (e) {
|
|
164
|
+
loadError = e
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
localFileExisted = existsSync(join(__dirname, 'image.linux-arm64-gnu.node'))
|
|
168
|
+
try {
|
|
169
|
+
if (localFileExisted) {
|
|
170
|
+
nativeBinding = require('./image.linux-arm64-gnu.node')
|
|
171
|
+
} else {
|
|
172
|
+
nativeBinding = require('@napi-rs/image-linux-arm64-gnu')
|
|
173
|
+
}
|
|
174
|
+
} catch (e) {
|
|
175
|
+
loadError = e
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
break
|
|
179
|
+
case 'arm':
|
|
180
|
+
localFileExisted = existsSync(join(__dirname, 'image.linux-arm-gnueabihf.node'))
|
|
181
|
+
try {
|
|
182
|
+
if (localFileExisted) {
|
|
183
|
+
nativeBinding = require('./image.linux-arm-gnueabihf.node')
|
|
184
|
+
} else {
|
|
185
|
+
nativeBinding = require('@napi-rs/image-linux-arm-gnueabihf')
|
|
186
|
+
}
|
|
187
|
+
} catch (e) {
|
|
188
|
+
loadError = e
|
|
189
|
+
}
|
|
190
|
+
break
|
|
191
|
+
default:
|
|
192
|
+
throw new Error(`Unsupported architecture on Linux: ${arch}`)
|
|
193
|
+
}
|
|
194
|
+
break
|
|
195
|
+
default:
|
|
196
|
+
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (!nativeBinding) {
|
|
200
|
+
if (loadError) {
|
|
201
|
+
throw loadError
|
|
202
|
+
}
|
|
203
|
+
throw new Error(`Failed to load native binding`)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const { losslessCompressPng, compressJpeg } = nativeBinding
|
|
207
|
+
|
|
208
|
+
module.exports.losslessCompressPng = losslessCompressPng
|
|
209
|
+
module.exports.compressJpeg = compressJpeg
|
package/optimize-test.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
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/package.json
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@napi-rs/image",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"types": "index.d.ts",
|
|
6
|
+
"description": "Image processing library",
|
|
7
|
+
"author": {
|
|
8
|
+
"email": "github@lyn.one",
|
|
9
|
+
"name": "LongYinan",
|
|
10
|
+
"url": "https://lyn.one"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"image",
|
|
14
|
+
"lossless",
|
|
15
|
+
"compression",
|
|
16
|
+
"jpeg",
|
|
17
|
+
"jpg",
|
|
18
|
+
"png"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"registry": "https://registry.npmjs.org/",
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"napi": {
|
|
25
|
+
"name": "image",
|
|
26
|
+
"triples": {
|
|
27
|
+
"additional": [
|
|
28
|
+
"aarch64-apple-darwin",
|
|
29
|
+
"aarch64-linux-android",
|
|
30
|
+
"aarch64-unknown-linux-gnu",
|
|
31
|
+
"aarch64-unknown-linux-musl",
|
|
32
|
+
"armv7-unknown-linux-gnueabihf",
|
|
33
|
+
"x86_64-unknown-linux-musl",
|
|
34
|
+
"x86_64-unknown-freebsd",
|
|
35
|
+
"i686-pc-windows-msvc",
|
|
36
|
+
"armv7-linux-androideabi"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@napi-rs/cli": "^2.2.1",
|
|
43
|
+
"@types/node": "^17.0.8",
|
|
44
|
+
"ava": "^4.0.1",
|
|
45
|
+
"npm-run-all": "^4.1.5",
|
|
46
|
+
"prettier": "^2.5.1"
|
|
47
|
+
},
|
|
48
|
+
"ava": {
|
|
49
|
+
"extensions": [
|
|
50
|
+
"mjs"
|
|
51
|
+
],
|
|
52
|
+
"timeout": "3m",
|
|
53
|
+
"environmentVariables": {
|
|
54
|
+
"NODE_ENV": "ava"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">= 10"
|
|
59
|
+
},
|
|
60
|
+
"funding": {
|
|
61
|
+
"type": "github",
|
|
62
|
+
"url": "https://github.com/sponsors/Brooooooklyn"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"artifacts": "napi artifacts",
|
|
66
|
+
"build": "napi build --platform --release",
|
|
67
|
+
"build:debug": "napi build --platform",
|
|
68
|
+
"format": "run-p format:prettier format:rs",
|
|
69
|
+
"format:prettier": "prettier --config ./package.json -w .",
|
|
70
|
+
"format:rs": "cargo fmt --all",
|
|
71
|
+
"prepublishOnly": "napi prepublish -t npm",
|
|
72
|
+
"test": "ava",
|
|
73
|
+
"version": "napi version"
|
|
74
|
+
},
|
|
75
|
+
"prettier": {
|
|
76
|
+
"printWidth": 120,
|
|
77
|
+
"semi": false,
|
|
78
|
+
"trailingComma": "all",
|
|
79
|
+
"singleQuote": true,
|
|
80
|
+
"arrowParens": "always"
|
|
81
|
+
},
|
|
82
|
+
"repository": "git@github.com:Brooooooklyn/imgquant.git",
|
|
83
|
+
"optionalDependencies": {
|
|
84
|
+
"@napi-rs/image-win32-x64-msvc": "1.0.0",
|
|
85
|
+
"@napi-rs/image-darwin-x64": "1.0.0",
|
|
86
|
+
"@napi-rs/image-linux-x64-gnu": "1.0.0",
|
|
87
|
+
"@napi-rs/image-darwin-arm64": "1.0.0",
|
|
88
|
+
"@napi-rs/image-android-arm64": "1.0.0",
|
|
89
|
+
"@napi-rs/image-linux-arm64-gnu": "1.0.0",
|
|
90
|
+
"@napi-rs/image-linux-arm64-musl": "1.0.0",
|
|
91
|
+
"@napi-rs/image-linux-arm-gnueabihf": "1.0.0",
|
|
92
|
+
"@napi-rs/image-linux-x64-musl": "1.0.0",
|
|
93
|
+
"@napi-rs/image-freebsd-x64": "1.0.0",
|
|
94
|
+
"@napi-rs/image-win32-ia32-msvc": "1.0.0",
|
|
95
|
+
"@napi-rs/image-android-arm-eabi": "1.0.0"
|
|
96
|
+
}
|
|
97
|
+
}
|
package/src/lib.rs
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
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
ADDED
|
Binary file
|
package/un-optimized.png
ADDED
|
Binary file
|