Compare commits

...

93 Commits

Author SHA1 Message Date
Jason Miller
4e04053e4c LibSquoosh: add smoke test 2021-09-07 14:26:36 -04:00
Jake Archibald
255dfa434a Update pointer-tracker (#1147) 2021-09-07 14:43:19 +01:00
François Beaufort
0bef05bcd4 Remove origin trial header 2021-09-06 15:00:48 +01:00
Surma
00cfdafdf3 Merge pull request #1138 from jcao219/fix-windows-path
Use pathify to calculate absolute paths for cross-platform compatibility
2021-08-31 13:34:28 +01:00
Jimmy Cao
92f52319da Use pathify to calculate absolute paths for cross-platform compatibility 2021-08-31 00:18:21 -07:00
Jake Archibald
023304803f JPEG-XL: Better 'effort' and adding noise synth (#1039) 2021-08-27 14:18:54 +01:00
Vishal
6b08cd2355 Aliasing toggle button (#1132) 2021-08-26 16:44:20 +01:00
Jake Archibald
db1a5138e6 Fix pinch zoom on iOS (#1133) 2021-08-26 15:59:42 +01:00
Jake Archibald
a547491146 Maintain DC_SCAN_OPT_MODE if subsampling is > 2. Fixes #1129 (#1130) 2021-08-26 14:29:13 +01:00
Jake Archibald
6e427f9208 The observer is null if user prefers reduced motion
Turns out TypeScript is there for a reason and shouldn't be bypassed. Fixes #1135
2021-08-25 14:10:57 +01:00
Jake Archibald
0d35fbd349 Fix horizontal scroll on option elements on mobile (#1134) 2021-08-25 09:25:09 +01:00
Mike DelGaudio
4e901c714c [RFC] Add Three Homepage Sections (#1112) 2021-08-25 09:08:22 +01:00
Adam Burgess
b74788e036 Stable mousewheel scrolling, one wheel up + one wheel down = no change (#1131) 2021-08-24 14:52:25 +01:00
Vishal
14c3d190e9 Max cap on scale #905 (#1128) 2021-08-23 13:27:17 +01:00
Surma
eeaa19589e Merge pull request #1120 from veluca93/jxl_05
Bump JPEG XL to v0.5.
2021-08-14 09:23:04 +01:00
Luca Versari
35b8c56f1a Bump JPEG XL to v0.5. 2021-08-13 23:27:44 +02:00
Surma
816d1f92fd Merge pull request #1110 from styfle/libsquoosh-ts-remaining 2021-08-10 12:38:11 +01:00
Steven
4091f2efec Remove unused export 2021-08-06 13:20:14 -04:00
Steven
821d14c6ab Merge branch 'dev' into libsquoosh-ts-remaining 2021-08-06 13:02:46 -04:00
Steven
a72ca46531 Move as to input param instead of return type 2021-08-06 12:49:17 -04:00
ergunsh
fafcf97f0c Update code for the review comments
* Make decode module return value `ImageData`
* Fix global definition of ImageData
* Use concrete Encoder types for encode functions
* Use ArrayBufferView in FileLike instead of using a similar type
* Throw error when the `encode` functions
return null
* Use generic types for WorkerPool
* Fix `encode` function typing
in `index.ts`
* Remove ts-ignore for web-streams-polyfill
and handle nulls for TransformStream
* Fix rollup entry point (now we need to have
`index.ts` instead of `index.js`)
2021-08-06 16:05:14 +03:00
Surma
d526877147 Merge pull request #1099 from mikedelgaudio/add-meta-info 2021-07-27 11:48:04 +01:00
Jake Archibald
0ed7ef842f Include site origin in build 2021-07-27 11:30:58 +01:00
Steven
de4eb9c8f7 Add encode() and decode() types 2021-07-26 23:22:28 -04:00
Steven
16a53caa48 Convert remaining JS to TS in libSquoosh 2021-07-26 22:37:00 -04:00
Mike DelGaudio
3d4c62fede Merge branch 'dev' into add-meta-info 2021-07-26 12:23:06 -04:00
Mike DelGaudio
ce7be359c0 remove keywords
Co-authored-by: Anton <dev@atjn.dk>
2021-07-26 12:23:00 -04:00
Surma
3f2dd66726 Merge pull request #1030 from veluca93/jxl_github 2021-07-23 13:49:46 +01:00
Surma
9198f748b8 Merge branch 'dev' into jxl_github 2021-07-23 13:37:37 +01:00
Surma
a726adf0e8 Merge pull request #1107 from styfle/patch-1
Fix typo uncluding => including
2021-07-23 13:15:21 +01:00
Steven
04580b0bcb Fix typo uncluding => including 2021-07-22 13:48:19 -04:00
Luca Versari
a040c47047 Update JPEG XL to latest version.
Also update repositories.
2021-07-21 18:55:52 +02:00
Surma
2427763a14 Squoosh CLI v0.7.2 2021-07-21 17:19:19 +01:00
Surma
c582c54922 libsquoosh v0.4.0 2021-07-21 17:15:59 +01:00
Surma
9ae27c1887 Merge pull request #1106 from GoogleChromeLabs/issue-1102 2021-07-21 17:03:21 +01:00
Surma
bf95eb39c2 Fix handling of PNGs that are not 8 bit 2021-07-21 16:16:44 +01:00
Surma
011c0346c1 Merge pull request #1100 from MaxGraey/opt-visdif-codec 2021-07-20 19:17:23 +01:00
Surma
9b36c3f9af Merge remote-tracking branch 'origin/dev' into opt-visdif-codec 2021-07-20 19:14:54 +01:00
Surma
490fe2aace Add build instructions 2021-07-20 19:14:23 +01:00
Mike DelGaudio
48efb4ddeb remove twitter image 2021-07-20 10:55:47 -04:00
Surma
0977bd94d3 Merge pull request #1074 from GoogleChromeLabs/avif-node-mt 2021-07-20 12:01:47 +01:00
Surma
d5f12a8c61 Merge remote-tracking branch 'origin/dev' into avif-node-mt 2021-07-20 11:51:10 +01:00
Surma
fb867dcdaa Review by rreverser 2021-07-20 11:50:48 +01:00
Surma
f038c1bd7d Update src/static-build/pages/index/index.tsx 2021-07-20 11:26:19 +01:00
Mike DelGaudio
b37cc0784f typo fix for secure 2021-07-19 20:31:58 -04:00
Mike DelGaudio
ff40000473 adding secure 2021-07-19 20:28:23 -04:00
Mike DelGaudio
7232524f4d add twitter 2021-07-19 20:20:03 -04:00
Surma
bf683cdf59 Merge pull request #1093 from mikedelgaudio/update-readme
[DOCS] Update README, Remove Passive Voice
2021-07-19 17:27:54 +01:00
Mike DelGaudio
f848a9384e Merge branch 'dev' into update-readme 2021-07-19 11:48:13 -04:00
Mike DelGaudio
d4d0db6c49 update header statement 2021-07-19 11:47:52 -04:00
Surma
0719abff27 Merge pull request #1081 from atjn/unused-code
Remove unused variable
2021-07-19 12:42:01 +01:00
Surma
2c561687af Merge branch 'dev' into unused-code 2021-07-19 12:39:51 +01:00
Surma
cd20082e5d Build wasm 2021-07-19 12:36:43 +01:00
MaxGraey
8262c79bb6 fix 2021-07-13 22:38:30 +03:00
MaxGraey
ef176d7565 optimize more 2021-07-13 22:38:10 +03:00
MaxGraey
3b0b7dbdb1 fix 2021-07-13 19:00:26 +03:00
MaxGraey
cc9a887386 optimize srgb -> linear preparation by using lookup table 2021-07-13 18:33:15 +03:00
Mike DelGaudio
71ca1ab0ca Update src/static-build/pages/index/index.tsx
Co-authored-by: Joan León <joan.leon@gmail.com>
2021-07-12 14:40:37 -04:00
Mike DelGaudio
f5d9535fd2 adding meta tags for better SEO 2021-07-12 11:13:51 -04:00
Mike DelGaudio
cea6a61366 Modify meta data 2021-07-09 15:53:18 -04:00
Mike DelGaudio
192cfc62ee Update README.md 2021-07-07 09:57:44 -04:00
Mike DelGaudio
fe21322b2b [DOCS] Update list of develop steps 2021-07-06 14:34:04 -04:00
Mike DelGaudio
d953822d19 [DOCS] Remove passive voice, simplify README 2021-07-05 22:37:04 -04:00
Mike DelGaudio
021b082884 [DOCS] Remove passive voice, simplify README 2021-07-05 22:24:20 -04:00
Surma
ad5002c79c Merge pull request #1078 from ergunsh/auto-optimizer-to-ts
Typescriptify auto optimizer in libSquoosh
2021-07-01 15:53:06 +01:00
Surma
883bb92e48 Merge branch 'dev' into auto-optimizer-to-ts 2021-07-01 15:45:38 +01:00
Surma
79efd0d32e Merge pull request #1080 from pekeq/doc-resize-with-aspect
libsquoosh/README: Add code example to resize with aspect ratio preserved.
2021-07-01 15:45:32 +01:00
Surma
99741d665d Merge branch 'dev' into auto-optimizer-to-ts 2021-07-01 15:22:23 +01:00
Surma
b38765b4e6 Merge branch 'dev' into doc-resize-with-aspect 2021-07-01 15:22:13 +01:00
Bob Fanger
a11ac15008 fix: Don't overwrite value while typing (#1082)
Fixes #316
2021-07-01 11:11:00 +01:00
atjn
4f6d21199c Remove unused variable 2021-07-01 00:08:58 +02:00
Hideo Matsumoto
fb7e00067f Update libsquoosh/README.md code example
Add code example to resize with aspect ratio preserved.
2021-06-30 22:43:20 +09:00
ergunsh
a2121ec47b Typescriptify auto optimizer in libSquoosh 2021-06-27 22:27:43 +02:00
Surma
d4056026fb Add AVIF thread support 2021-06-25 00:11:25 +01:00
Surma
d12b040bd3 Add threaded AVIF encoder for node 2021-06-24 17:07:57 +01:00
Surma
149ebf5a67 Merge pull request #1073 from atjn/patch-1
Fix codecs link
2021-06-24 16:35:31 +01:00
Anton
d8297aad10 Fix codecs link 2021-06-24 17:03:17 +02:00
Surma
0e84a5b5f7 Merge pull request #1071 from simon04/patch-1 2021-06-24 10:56:26 +01:00
Simon Legner
187a5bed01 cli/README: linkfix src/codecs.ts 2021-06-24 09:34:09 +02:00
Jake Archibald
07a288398e Spotted another leak (#1069) 2021-06-23 10:39:27 +01:00
Surma
dbb31a1add Merge remote-tracking branch 'origin/dev' into dev 2021-06-21 12:58:41 +01:00
Surma
6e58e8edb0 Merge pull request #1061 from atjn/engines
Specify supported node versions
2021-06-21 12:58:28 +01:00
Surma
955079b18f Merge branch 'dev' into engines 2021-06-21 12:53:32 +01:00
Vajahath Ahmed
f6f70c590e Add repo info in package.json so that people can backtrack from npm site (#1066) 2021-06-21 11:59:21 +01:00
Surma
55c4aa51ac Merge pull request #1063 from SimonGolms/patch-1
fix(cli): append suffix option at the end
2021-06-19 14:25:55 +01:00
Simon G
aea316c604 fix(cli): append suffix option at the end
- close #1062
2021-06-19 11:40:52 +02:00
atjn
d604e94ad2 Specify supported node versions 2021-06-18 21:18:37 +02:00
Surma
61de471e52 Merge pull request #1058 from atjn/fix-detectors
Fix WebP sniffing some more
2021-06-18 15:58:03 +01:00
atjn
955b2ac1ba Fix WebP sniffing some more 2021-06-18 15:51:20 +02:00
Surma
3d225966c5 CLI v0.7.1 2021-06-16 17:13:59 +01:00
Surma
851da25302 Update CLI dependency 2021-06-16 17:13:37 +01:00
Surma
32e3528666 libsquoosh v0.3.1 2021-06-16 17:11:15 +01:00
Surma
1e52837931 Oooops. Old files 2021-06-16 17:02:54 +01:00
79 changed files with 1219 additions and 323 deletions

View File

@@ -1,36 +1,42 @@
# [Squoosh]!
[Squoosh] is an image compression web app that allows you to dive into the advanced options provided
by various image compressors.
[Squoosh] is an image compression web app that reduces image sizes through numerous formats.
# API & CLI
Squoosh now has [an API](https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh) and [a CLI](https://github.com/GoogleChromeLabs/squoosh/tree/dev/cli) that allows you to compress many images at once.
Squoosh has [an API](https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh) and [a CLI](https://github.com/GoogleChromeLabs/squoosh/tree/dev/cli) to compress many images at once.
# Privacy
Google Analytics is used to record the following:
Squoosh does not send your image to a server. All image compression processes locally.
- [Basic visit data](https://support.google.com/analytics/answer/6004245?ref_topic=2919631).
- Before and after image size once an image is downloaded. These values are rounded to the nearest
kilobyte.
- If install is available, when Squoosh is installed, and what method was used to install Squoosh.
However, Squoosh utilizes Google Analytics to collect the following:
Image compression is handled locally; no additional data is sent to the server.
- [Basic visitor data](https://support.google.com/analytics/answer/6004245?ref_topic=2919631).
- The before and after image size value.
- If Squoosh PWA, the type of Squoosh installation.
- If Squoosh PWA, the installation time and date.
# Building locally
# Developing
Clone the repo, and:
To develop for Squoosh:
```sh
npm install
npm run build
```
1. Clone the repository
1. To install node packages, run:
```sh
npm install
```
1. Then build the app by running:
```sh
npm run build
```
1. After building, start the development server by running:
```sh
npm run dev
```
You can run the development server with:
# Contributing
```sh
npm run dev
```
Squoosh is an open-source project that appreciates all community involvement. To contribute to the project, follow the [contribute guide](/CONTRIBUTING.md).
[squoosh]: https://squoosh.app

View File

@@ -42,7 +42,7 @@ Options:
-h, --help display help for command
```
The default values for each `config` option can be found in the [`codecs.js`][codecs.js] file under `defaultEncoderOptions`. Every unspecified value will use the default value specified here. _Better documentation is needed here._
The default values for each `config` option can be found in the [`codecs.ts`][codecs.ts] file under `defaultEncoderOptions`. Every unspecified value will use the default value specified here. _Better documentation is needed here._
## Auto optimizer
@@ -55,5 +55,5 @@ $ npx @squoosh/cli --wp2 auto test.png
```
[squoosh]: https://squoosh.app
[codecs.js]: https://github.com/GoogleChromeLabs/squoosh/blob/dev/libsquoosh/src/codecs.js
[codecs.ts]: https://github.com/GoogleChromeLabs/squoosh/blob/dev/libsquoosh/src/codecs.ts
[butteraugli]: https://github.com/google/butteraugli

36
cli/package-lock.json generated
View File

@@ -1,15 +1,15 @@
{
"name": "@squoosh/cli",
"version": "0.7.0",
"version": "0.7.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@squoosh/cli",
"version": "0.7.0",
"version": "0.7.2",
"license": "Apache-2.0",
"dependencies": {
"@squoosh/lib": "^0.2.0",
"@squoosh/lib": "^0.4.0",
"commander": "^7.2.0",
"json5": "^2.2.0",
"kleur": "^4.1.4",
@@ -18,14 +18,21 @@
"bin": {
"cli": "src/index.js",
"squoosh-cli": "src/index.js"
},
"engines": {
"node": " ^12.20.2 || ^14.13.1 || ^16.0.0 "
}
},
"node_modules/@squoosh/lib": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/@squoosh/lib/-/lib-0.2.0.tgz",
"integrity": "sha512-zKId9h/LzEnCdoOGnIgjzvqbk5g2aHgfEqgKQ+S+r5K3TasgD/DAsT6r7v6gXft1ao0f/00CTcwIp1KviWTQbw==",
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/@squoosh/lib/-/lib-0.4.0.tgz",
"integrity": "sha512-O1LyugWLZjMI4JZeZMA5vzfhfPjfMZXH5/HmVkRagP8B70wH3uoR7tjxfGNdSavey357MwL8YJDxbGwBBdHp7Q==",
"dependencies": {
"wasm-feature-detect": "^1.2.11",
"web-streams-polyfill": "^3.0.3"
},
"engines": {
"node": " ^12.5.0 || ^14.0.0 || ^16.0.0 "
}
},
"node_modules/ansi-regex": {
@@ -323,6 +330,11 @@
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
},
"node_modules/wasm-feature-detect": {
"version": "1.2.11",
"resolved": "https://registry.npmjs.org/wasm-feature-detect/-/wasm-feature-detect-1.2.11.tgz",
"integrity": "sha512-HUqwaodrQGaZgz1lZaNioIkog9tkeEJjrM3eq4aUL04whXOVDRc/o2EGb/8kV0QX411iAYWEqq7fMBmJ6dKS6w=="
},
"node_modules/wcwidth": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz",
@@ -342,10 +354,11 @@
},
"dependencies": {
"@squoosh/lib": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/@squoosh/lib/-/lib-0.2.0.tgz",
"integrity": "sha512-zKId9h/LzEnCdoOGnIgjzvqbk5g2aHgfEqgKQ+S+r5K3TasgD/DAsT6r7v6gXft1ao0f/00CTcwIp1KviWTQbw==",
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/@squoosh/lib/-/lib-0.4.0.tgz",
"integrity": "sha512-O1LyugWLZjMI4JZeZMA5vzfhfPjfMZXH5/HmVkRagP8B70wH3uoR7tjxfGNdSavey357MwL8YJDxbGwBBdHp7Q==",
"requires": {
"wasm-feature-detect": "^1.2.11",
"web-streams-polyfill": "^3.0.3"
}
},
@@ -578,6 +591,11 @@
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
},
"wasm-feature-detect": {
"version": "1.2.11",
"resolved": "https://registry.npmjs.org/wasm-feature-detect/-/wasm-feature-detect-1.2.11.tgz",
"integrity": "sha512-HUqwaodrQGaZgz1lZaNioIkog9tkeEJjrM3eq4aUL04whXOVDRc/o2EGb/8kV0QX411iAYWEqq7fMBmJ6dKS6w=="
},
"wcwidth": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz",

View File

@@ -1,9 +1,14 @@
{
"name": "@squoosh/cli",
"version": "0.7.0",
"version": "0.7.2",
"description": "A CLI for Squoosh",
"public": true,
"type": "module",
"homepage": "https://github.com/GoogleChromeLabs/squoosh",
"repository": {
"type": "git",
"url": "https://github.com/GoogleChromeLabs/squoosh.git"
},
"bin": {
"squoosh-cli": "src/index.js",
"@squoosh/cli": "src/index.js"
@@ -14,8 +19,11 @@
"keywords": [],
"author": "Google Chrome Developers <chromium-dev@google.com>",
"license": "Apache-2.0",
"engines": {
"node": " ^12.20.2 || ^14.13.1 || ^16.0.0 "
},
"dependencies": {
"@squoosh/lib": "^0.2.0",
"@squoosh/lib": "^0.4.0",
"commander": "^7.2.0",
"json5": "^2.2.0",
"kleur": "^4.1.4",

View File

@@ -177,8 +177,8 @@ async function processFiles(files) {
jobsFinished++;
const outputPath = path.join(
program.opts().outputDir,
program.opts().suffix +
path.basename(originalFile, path.extname(originalFile)),
path.basename(originalFile, path.extname(originalFile)) +
program.opts().suffix
);
for (const output of Object.values(image.encodedWith)) {
const outputFile = `${outputPath}.${(await output).extension}`;

View File

@@ -16,10 +16,10 @@ export
OUT_ENC_JS = enc/avif_enc.js
OUT_NODE_ENC_JS = enc/avif_node_enc.js
OUT_ENC_MT_JS = enc/avif_enc_mt.js
OUT_NODE_ENC_MT_JS = enc/avif_node_enc_mt.js
OUT_DEC_JS = dec/avif_dec.js
OUT_NODE_DEC_JS = dec/avif_node_dec.js
OUT_ENC_CPP = enc/avif_enc.cpp
OUT_ENC_CPP = enc/avif_enc.cpp
OUT_DEC_CPP = dec/avif_dec.cpp
ENVIRONMENT = worker
@@ -28,9 +28,9 @@ HELPER_MAKEFLAGS := -f helper.Makefile
.PHONY: all clean
all: $(OUT_ENC_JS) $(OUT_DEC_JS) $(OUT_ENC_MT_JS) $(OUT_NODE_ENC_JS) $(OUT_NODE_DEC_JS)
all: $(OUT_ENC_JS) $(OUT_DEC_JS) $(OUT_ENC_MT_JS) $(OUT_NODE_ENC_JS) $(OUT_NODE_ENC_MT_JS) $(OUT_NODE_DEC_JS)
$(OUT_NODE_ENC_JS): ENVIRONMENT=node
$(OUT_NODE_ENC_JS) $(OUT_NODE_ENC_MT_JS): ENVIRONMENT=node
$(OUT_NODE_ENC_JS) $(OUT_ENC_JS): $(OUT_ENC_CPP) $(CODEC_DIR)/CMakeLists.txt $(LIBAOM_DIR)/CMakeLists.txt
$(MAKE) \
$(HELPER_MAKEFLAGS) \
@@ -44,7 +44,7 @@ $(OUT_NODE_ENC_JS) $(OUT_ENC_JS): $(OUT_ENC_CPP) $(CODEC_DIR)/CMakeLists.txt $(L
ENVIRONMENT=$(ENVIRONMENT) \
LIBAVIF_FLAGS="-DAVIF_CODEC_AOM_DECODE=0"
$(OUT_ENC_MT_JS): $(OUT_ENC_CPP) $(CODEC_DIR)/CMakeLists.txt $(LIBAOM_DIR)/CMakeLists.txt
$(OUT_ENC_MT_JS) $(OUT_NODE_ENC_MT_JS): $(OUT_ENC_CPP) $(CODEC_DIR)/CMakeLists.txt $(LIBAOM_DIR)/CMakeLists.txt
$(MAKE) \
$(HELPER_MAKEFLAGS) \
OUT_JS=$@ \
@@ -89,4 +89,7 @@ $(LIBAOM_DIR)/CMakeLists.txt: $(LIBAOM_PACKAGE)
clean:
$(MAKE) $(HELPER_MAKEFLAGS) OUT_JS=$(OUT_ENC_JS) clean
$(MAKE) $(HELPER_MAKEFLAGS) OUT_JS=$(OUT_ENC_MT_JS) clean
$(MAKE) $(HELPER_MAKEFLAGS) OUT_JS=$(OUT_ENC_NODE_JS) clean
$(MAKE) $(HELPER_MAKEFLAGS) OUT_JS=$(OUT_ENC_NODE_MT_JS) clean
$(MAKE) $(HELPER_MAKEFLAGS) OUT_JS=$(OUT_DEC_JS) clean
$(MAKE) $(HELPER_MAKEFLAGS) OUT_JS=$(OUT_DEV_NODE_JS) clean

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

16
codecs/avif/enc/avif_node_enc_mt.js generated Normal file

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -0,0 +1 @@
"use strict";var Module={};if(typeof process==="object"&&typeof process.versions==="object"&&typeof process.versions.node==="string"){var nodeWorkerThreads=require("worker_threads");var parentPort=nodeWorkerThreads.parentPort;parentPort.on("message",function(data){onmessage({data:data})});var nodeFS=require("fs");Object.assign(global,{self:global,require:require,Module:Module,location:{href:__filename},Worker:nodeWorkerThreads.Worker,importScripts:function(f){(0,eval)(nodeFS.readFileSync(f,"utf8"))},postMessage:function(msg){parentPort.postMessage(msg)},performance:global.performance||{now:function(){return Date.now()}}})}var initializedJS=false;function threadPrintErr(){var text=Array.prototype.slice.call(arguments).join(" ");console.error(text)}function threadAlert(){var text=Array.prototype.slice.call(arguments).join(" ");postMessage({cmd:"alert",text:text,threadId:Module["_pthread_self"]()})}var err=threadPrintErr;self.alert=threadAlert;Module["instantiateWasm"]=function(info,receiveInstance){var instance=new WebAssembly.Instance(Module["wasmModule"],info);receiveInstance(instance);Module["wasmModule"]=null;return instance.exports};function moduleLoaded(){}self.onmessage=function(e){try{if(e.data.cmd==="load"){Module["wasmModule"]=e.data.wasmModule;Module["wasmMemory"]=e.data.wasmMemory;Module["buffer"]=Module["wasmMemory"].buffer;Module["ENVIRONMENT_IS_PTHREAD"]=true;(e.data.urlOrBlob?import(e.data.urlOrBlob):import("./avif_node_enc_mt.js")).then(function(exports){return exports.default(Module)}).then(function(instance){Module=instance;moduleLoaded()})}else if(e.data.cmd==="objectTransfer"){Module["PThread"].receiveObjectTransfer(e.data)}else if(e.data.cmd==="run"){Module["__performance_now_clock_drift"]=performance.now()-e.data.time;Module["__emscripten_thread_init"](e.data.threadInfoStruct,0,0);var max=e.data.stackBase;var top=e.data.stackBase+e.data.stackSize;Module["establishStackSpace"](top,max);Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].threadInit();if(!initializedJS){Module["___embind_register_native_and_builtin_types"]();initializedJS=true}try{var result=Module["invokeEntryPoint"](e.data.start_routine,e.data.arg);if(Module["keepRuntimeAlive"]()){Module["PThread"].setExitStatus(result)}else{Module["PThread"].threadExit(result)}}catch(ex){if(ex==="Canceled!"){Module["PThread"].threadCancel()}else if(ex!="unwind"){if(ex instanceof Module["ExitStatus"]){if(Module["keepRuntimeAlive"]()){}else{Module["PThread"].threadExit(ex.status)}}else{Module["PThread"].threadExit(-2);throw ex}}}}else if(e.data.cmd==="cancel"){if(Module["_pthread_self"]()){Module["PThread"].threadCancel()}}else if(e.data.target==="setimmediate"){}else if(e.data.cmd==="processThreadQueue"){if(Module["_pthread_self"]()){Module["_emscripten_current_thread_process_queued_calls"]()}}else{err("worker.js received unknown command "+e.data.cmd);err(e.data)}}catch(ex){err("worker.js onmessage() captured an uncaught exception: "+ex);if(ex&&ex.stack)err(ex.stack);throw ex}};

View File

@@ -1,5 +1,5 @@
CODEC_URL = https://gitlab.com/wg1/jpeg-xl.git
CODEC_VERSION = ab7c5e9b6795134377aa4846ceaae2c5bc504f76
CODEC_URL = https://github.com/libjxl/libjxl.git
CODEC_VERSION = v0.5
CODEC_DIR = node_modules/jxl
CODEC_BUILD_ROOT := $(CODEC_DIR)/build
CODEC_MT_BUILD_DIR := $(CODEC_BUILD_ROOT)/mt

Binary file not shown.

Binary file not shown.

View File

@@ -10,15 +10,13 @@ using namespace emscripten;
thread_local const val Uint8Array = val::global("Uint8Array");
struct JXLOptions {
// 1 = slowest
// 7 = fastest
int speed;
int effort;
float quality;
bool progressive;
int epf;
int nearLossless;
bool lossyPalette;
size_t decodingSpeedTier;
float photonNoiseIso;
};
val encode(std::string image, int width, int height, JXLOptions options) {
@@ -33,15 +31,20 @@ val encode(std::string image, int width, int height, JXLOptions options) {
pool_ptr = &pool;
#endif
size_t st = 10 - options.effort;
cparams.speed_tier = jxl::SpeedTier(st);
cparams.epf = options.epf;
cparams.speed_tier = static_cast<jxl::SpeedTier>(options.speed);
cparams.near_lossless = options.nearLossless;
cparams.decoding_speed_tier = options.decodingSpeedTier;
cparams.photon_noise_iso = options.photonNoiseIso;
if (options.lossyPalette) {
cparams.lossy_palette = true;
cparams.palette_colors = 0;
cparams.options.predictor = jxl::Predictor::Zero;
// Near-lossless assumes -R 0
cparams.responsive = 0;
cparams.modular_mode = true;
}
float quality = options.quality;
@@ -77,12 +80,6 @@ val encode(std::string image, int width, int height, JXLOptions options) {
}
}
if (cparams.near_lossless) {
// Near-lossless assumes -R 0
cparams.responsive = 0;
cparams.modular_mode = true;
}
io.metadata.m.SetAlphaBits(8);
if (!io.metadata.size.Set(width, height)) {
return val::null();
@@ -110,12 +107,12 @@ val encode(std::string image, int width, int height, JXLOptions options) {
EMSCRIPTEN_BINDINGS(my_module) {
value_object<JXLOptions>("JXLOptions")
.field("speed", &JXLOptions::speed)
.field("effort", &JXLOptions::effort)
.field("quality", &JXLOptions::quality)
.field("progressive", &JXLOptions::progressive)
.field("nearLossless", &JXLOptions::nearLossless)
.field("lossyPalette", &JXLOptions::lossyPalette)
.field("decodingSpeedTier", &JXLOptions::decodingSpeedTier)
.field("photonNoiseIso", &JXLOptions::photonNoiseIso)
.field("epf", &JXLOptions::epf);
function("encode", &encode);

View File

@@ -1,11 +1,11 @@
export interface EncodeOptions {
speed: number;
effort: number;
quality: number;
progressive: boolean;
epf: number;
nearLossless: number;
lossyPalette: boolean;
decodingSpeedTier: number;
photonNoiseIso: number;
}
export interface JXLModule extends EmscriptenWasm.Module {

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -158,6 +158,11 @@ val encode(std::string image_in, int image_width, int image_height, MozJpegOptio
if (!opts.auto_subsample && opts.color_space == JCS_YCbCr) {
cinfo.comp_info[0].h_samp_factor = opts.chroma_subsample;
cinfo.comp_info[0].v_samp_factor = opts.chroma_subsample;
if (opts.chroma_subsample > 2) {
// Otherwise encoding fails.
jpeg_c_set_int_param(&cinfo, JINT_DC_SCAN_OPT_MODE, 1);
}
}
if (!opts.baseline && opts.progressive) {

Binary file not shown.

Binary file not shown.

View File

@@ -53,7 +53,11 @@ where
#[wasm_bindgen]
pub fn decode(mut data: &[u8]) -> ImageData {
let mut decoder = png::Decoder::new(&mut data);
decoder.set_transformations(png::Transformations::EXPAND);
decoder.set_transformations(
png::Transformations::EXPAND | // Turn paletted images into RGB
png::Transformations::PACKING | // Turn images <8bit to 8bit
png::Transformations::STRIP_16, // Turn 16bit into 8 bit
);
let (info, mut reader) = decoder.read_info().unwrap_throw();
let num_pixels = (info.width * info.height) as usize;
let mut buf = vec![0; num_pixels * 4];

14
codecs/visdif/BUILD.md Normal file
View File

@@ -0,0 +1,14 @@
This codec currently needs monkey-patching of Emscripten
```
$ docker run --rm -it -v $(PWD):/src squoosh-cpp "/bin/bash"
# cat << EOF | patch /emsdk/upstream/emscripten/system/lib/dlmalloc.c
659c659
< #define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *)))
---
> #define MALLOC_ALIGNMENT ((size_t)(16U))
EOF
# emcc --clear-cache
# /emsdk/upstream/emscripten/embuilder build libdlmalloc --force
# emmake make
```

View File

@@ -1,22 +0,0 @@
import {dirname} from "path";
globalThis.__dirname = dirname(import.meta.url);
import { createRequire } from 'module';
globalThis.require = createRequire(import.meta.url);
import visdif from './visdif.js';
const {VisDiff} = await visdif({
locateFile() {
return new URL("./visdif.wasm", import.meta.url).pathname;
}
});
const comparator = new VisDiff(
new Uint8ClampedArray([0, 0, 0, 255]),
1,
1
);
const distance = comparator.distance(new Uint8ClampedArray([1,1,1,255]));
console.log({distance});

View File

@@ -5,12 +5,19 @@
using namespace emscripten;
using namespace butteraugli;
#define GAMMA 2.2
static float SrgbToLinear[256];
inline void gammaLookupTable() {
SrgbToLinear[0] = 0;
for (int i = 1; i < 256; ++i) {
SrgbToLinear[i] = static_cast<float>(255.0 * pow(i / 255.0, GAMMA));
}
}
// Turns an interleaved RGBA buffer into 4 planes for each color channel
void planarize(std::vector<ImageF>& img,
const uint8_t* rgba,
int width,
int height,
float gamma = 2.2) {
void planarize(std::vector<ImageF>& img, const uint8_t* rgba, int width, int height) {
assert(img.size() == 0);
img.push_back(ImageF(width, height));
img.push_back(ImageF(width, height));
@@ -22,10 +29,10 @@ void planarize(std::vector<ImageF>& img,
float* const row_b = img[2].Row(y);
float* const row_a = img[3].Row(y);
for (int x = 0; x < width; x++) {
row_r[x] = 255.0 * pow(rgba[(y * width + x) * 4 + 0] / 255.0, gamma);
row_g[x] = 255.0 * pow(rgba[(y * width + x) * 4 + 1] / 255.0, gamma);
row_b[x] = 255.0 * pow(rgba[(y * width + x) * 4 + 2] / 255.0, gamma);
row_a[x] = 255.0 * pow(rgba[(y * width + x) * 4 + 3] / 255.0, gamma);
row_r[x] = SrgbToLinear[rgba[(y * width + x) * 4 + 0]];
row_g[x] = SrgbToLinear[rgba[(y * width + x) * 4 + 1]];
row_b[x] = SrgbToLinear[rgba[(y * width + x) * 4 + 2]];
row_a[x] = SrgbToLinear[rgba[(y * width + x) * 4 + 3]];
}
}
}
@@ -37,6 +44,7 @@ class VisDiff {
public:
VisDiff(std::string ref_img, int width, int height) {
gammaLookupTable();
planarize(this->ref_img, (uint8_t*)ref_img.c_str(), width, height);
this->width = width;
this->height = height;

View File

@@ -1,21 +0,0 @@
import {dirname} from "path";
globalThis.__dirname = dirname(import.meta.url);
import { createRequire } from 'module';
globalThis.require = createRequire(import.meta.url);
import visdif from './visdif.js';
const {VisDiff} = await visdif({
locateFile() {
return new URL("./visdif.wasm", import.meta.url).pathname;
}
});
const comparator = new VisDiff(
new Uint8ClampedArray([0, 0, 0, 255]),
1,
1
);
const distance = comparator.distance(new Uint8ClampedArray([1,1,1,255]));
console.log({distance});

Binary file not shown.

View File

@@ -30,7 +30,7 @@ const imagePath = 'path/to/image.png';
const image = imagePool.ingestImage(imagePath);
```
The `ingestImage` function can take anything the node [`readFile`][readfile] function can take, uncluding a buffer and `FileHandle`.
The `ingestImage` function can take anything the node [`readFile`][readfile] function can take, including a buffer and `FileHandle`.
The returned `image` object is a representation of the original image, that you can now preprocess, encode, and extract information about.
@@ -42,11 +42,19 @@ When an image has been ingested, you can start preprocessing it and encoding it
await image.decoded; //Wait until the image is decoded before running preprocessors.
const preprocessOptions = {
//When both width and height are specified, the image resized to specified size.
resize: {
enabled: true,
width: 100,
height: 50,
}
/*
//When either width or height is specified, the image resized to specified size keeping aspect ratio.
resize: {
enabled: true,
width: 100,
}
*/
}
await image.preprocess(preprocessOptions);
@@ -60,7 +68,7 @@ await image.encode(encodeOptions);
```
The default values for each option can be found in the [`codecs.js`][codecs.js] file under `defaultEncoderOptions`. Every unspecified value will use the default value specified there. _Better documentation is needed here._
The default values for each option can be found in the [`codecs.ts`][codecs.ts] file under `defaultEncoderOptions`. Every unspecified value will use the default value specified there. _Better documentation is needed here._
You can run your own code inbetween the different steps, if, for example, you want to change how much the image should be resized based on its original height. (See [Extracting image information](#extracting-image-information) to learn how to get the image dimensions).
@@ -158,6 +166,6 @@ const encodeOptions: {
```
[squoosh]: https://squoosh.app
[codecs.js]: https://github.com/GoogleChromeLabs/squoosh/blob/dev/libsquoosh/src/codecs.js
[codecs.ts]: https://github.com/GoogleChromeLabs/squoosh/blob/dev/libsquoosh/src/codecs.ts
[butteraugli]: https://github.com/google/butteraugli
[readfile]: https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options

View File

@@ -0,0 +1,49 @@
/**
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { promises as fs } from 'fs';
import { basename } from 'path';
const defaultOpts = {
prefix: 'chunk-url',
};
export default function chunkPlugin(opts) {
opts = { ...defaultOpts, ...opts };
const prefix = opts.prefix + ':';
return {
name: 'chunk-plugin',
async resolveId(id, importer) {
if (!id.startsWith(prefix)) return;
const realId = id.slice(prefix.length);
const resolveResult = await this.resolve(realId, importer);
if (!resolveResult) {
throw Error(`Cannot find ${realId}`);
}
return prefix + resolveResult.id;
},
async load(id) {
if (!id.startsWith(prefix)) return;
const realId = id.slice(prefix.length);
const source = await fs.readFile(realId);
this.addWatchFile(realId);
return `export default import.meta.ROLLUP_FILE_URL_${this.emitFile({
type: 'chunk',
source,
id: realId,
})}`;
},
};
}

View File

@@ -1,18 +0,0 @@
import { ImagePool } from './build/index.js';
console.log("Starting");
const imagePool = new ImagePool();
// const imagePath = '/Users/surma/Downloads/happy_dog.png';
const imagePath = './squoosh.png';
console.log("INgesting");
const image = imagePool.ingestImage(imagePath);
console.log("Decoding");
await image.decoded;
const encodeOptions = {
mozjpeg: 'auto',
};
console.log("Encoding");
await image.encode(encodeOptions);
console.log("Closing");
await imagePool.close();
console.log("Done");

View File

@@ -1,6 +1,6 @@
{
"name": "@squoosh/lib",
"version": "0.2.3",
"version": "0.4.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -1060,6 +1060,21 @@
"to-fast-properties": "^2.0.0"
}
},
"@cspotcode/source-map-consumer": {
"version": "0.8.0",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz",
"integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==",
"dev": true
},
"@cspotcode/source-map-support": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.6.1.tgz",
"integrity": "sha512-DX3Z+T5dt1ockmPdobJS/FAsQPW4V4SrWEhD2iYQT2Cb2tQsiMnYxrcUH9By/Z3B+v0S5LMBkQtV/XOBbpLEOg==",
"dev": true,
"requires": {
"@cspotcode/source-map-consumer": "0.8.0"
}
},
"@rollup/plugin-babel": {
"version": "5.3.0",
"resolved": "https://registry.npmjs.org/@rollup/plugin-babel/-/plugin-babel-5.3.0.tgz",
@@ -1118,6 +1133,30 @@
"picomatch": "^2.2.2"
}
},
"@tsconfig/node10": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz",
"integrity": "sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==",
"dev": true
},
"@tsconfig/node12": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.9.tgz",
"integrity": "sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==",
"dev": true
},
"@tsconfig/node14": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.1.tgz",
"integrity": "sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==",
"dev": true
},
"@tsconfig/node16": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz",
"integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==",
"dev": true
},
"@types/estree": {
"version": "0.0.39",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz",
@@ -1139,6 +1178,18 @@
"@types/node": "*"
}
},
"acorn": {
"version": "8.5.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.5.0.tgz",
"integrity": "sha512-yXbYeFy+jUuYd3/CDcg2NkIYE991XYX/bje7LmjJigUciaeO1JR4XxXgCIV1/Zc/dRuFEyw1L0pbA+qynJkW5Q==",
"dev": true
},
"acorn-walk": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz",
"integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==",
"dev": true
},
"ansi-styles": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
@@ -1148,6 +1199,12 @@
"color-convert": "^1.9.0"
}
},
"arg": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
"dev": true
},
"babel-plugin-dynamic-import-node": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.3.tgz",
@@ -1321,6 +1378,12 @@
}
}
},
"create-require": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true
},
"debug": {
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz",
@@ -1345,6 +1408,18 @@
"object-keys": "^1.0.12"
}
},
"dequal": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.2.tgz",
"integrity": "sha512-q9K8BlJVxK7hQYqa6XISGmBZbtQQWVXSrRrWreHC94rMt1QL/Impruc+7p2CYSYuVIUr+YCt6hjrs1kkdJRTug==",
"dev": true
},
"diff": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz",
"integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==",
"dev": true
},
"electron-to-chromium": {
"version": "1.3.725",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.725.tgz",
@@ -1547,6 +1622,12 @@
"minimist": "^1.2.5"
}
},
"kleur": {
"version": "4.1.4",
"resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.4.tgz",
"integrity": "sha512-8QADVssbrFjivHWQU7KkMgptGTl6WAcSdlbBPY4uNF+mWr6DGcKrvY2w4FQJoXch7+fKMjj0dRrL75vk3k23OA==",
"dev": true
},
"lodash.debounce": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz",
@@ -1562,6 +1643,12 @@
"sourcemap-codec": "^1.4.4"
}
},
"make-error": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
"dev": true
},
"merge-stream": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
@@ -1583,6 +1670,12 @@
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
"dev": true
},
"mri": {
"version": "1.1.6",
"resolved": "https://registry.npmjs.org/mri/-/mri-1.1.6.tgz",
"integrity": "sha512-oi1b3MfbyGa7FJMP9GmLTttni5JoICpYBRlq+x5V16fZbLsnL9N3wFqqIm/nIG43FjUFkFh9Epzp/kzUGUnJxQ==",
"dev": true
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@@ -1747,6 +1840,15 @@
"terser": "^5.0.0"
}
},
"sade": {
"version": "1.7.4",
"resolved": "https://registry.npmjs.org/sade/-/sade-1.7.4.tgz",
"integrity": "sha512-y5yauMD93rX840MwUJr7C1ysLFBgMspsdTo4UVrDg3fXDvtwOyIqykhVAAm6fk/3au77773itJStObgK+LKaiA==",
"dev": true,
"requires": {
"mri": "^1.1.0"
}
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
@@ -1832,6 +1934,40 @@
"integrity": "sha1-3F5pjL0HkmW8c+A3doGk5Og/YW4=",
"dev": true
},
"totalist": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/totalist/-/totalist-2.0.0.tgz",
"integrity": "sha512-+Y17F0YzxfACxTyjfhnJQEe7afPA0GSpYlFkl2VFMxYP7jshQf9gXV7cH47EfToBumFThfKBvfAcoUn6fdNeRQ==",
"dev": true
},
"ts-node": {
"version": "10.2.1",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.2.1.tgz",
"integrity": "sha512-hCnyOyuGmD5wHleOQX6NIjJtYVIO8bPP8F2acWkB4W06wdlkgyvJtubO/I9NkI88hCFECbsEgoLc0VNkYmcSfw==",
"dev": true,
"requires": {
"@cspotcode/source-map-support": "0.6.1",
"@tsconfig/node10": "^1.0.7",
"@tsconfig/node12": "^1.0.7",
"@tsconfig/node14": "^1.0.0",
"@tsconfig/node16": "^1.0.2",
"acorn": "^8.4.1",
"acorn-walk": "^8.1.1",
"arg": "^4.1.0",
"create-require": "^1.1.0",
"diff": "^4.0.1",
"make-error": "^1.1.1",
"yn": "3.1.1"
},
"dependencies": {
"diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
"dev": true
}
}
},
"typescript": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.2.tgz",
@@ -1866,6 +2002,24 @@
"integrity": "sha512-PqSoPh/pWetQ2phoj5RLiaqIk4kCNwoV3CI+LfGmWLKI3rE3kl1h59XpX2BjgDrmbxD9ARtQobPGU1SguCYuQg==",
"dev": true
},
"uvu": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/uvu/-/uvu-0.5.1.tgz",
"integrity": "sha512-JGxttnOGDFs77FaZ0yMUHIzczzQ5R1IlDeNW6Wymw6gAscwMdAffVOP6TlxLIfReZyK8tahoGwWZaTCJzNFDkg==",
"dev": true,
"requires": {
"dequal": "^2.0.0",
"diff": "^5.0.0",
"kleur": "^4.0.3",
"sade": "^1.7.3",
"totalist": "^2.0.0"
}
},
"wasm-feature-detect": {
"version": "1.2.11",
"resolved": "https://registry.npmjs.org/wasm-feature-detect/-/wasm-feature-detect-1.2.11.tgz",
"integrity": "sha512-HUqwaodrQGaZgz1lZaNioIkog9tkeEJjrM3eq4aUL04whXOVDRc/o2EGb/8kV0QX411iAYWEqq7fMBmJ6dKS6w=="
},
"web-streams-polyfill": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.0.3.tgz",
@@ -1885,6 +2039,12 @@
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true
},
"yn": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
"dev": true
}
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@squoosh/lib",
"version": "0.2.3",
"version": "0.4.0",
"description": "A Node library for Squoosh",
"public": true,
"main": "./build/index.js",
@@ -8,12 +8,22 @@
"/build/*"
],
"scripts": {
"build": "rollup -c"
"build": "rollup -c",
"test": "uvu -r ts-node/register test"
},
"keywords": [],
"author": "Google Chrome Developers <chromium-dev@google.com>",
"homepage": "https://github.com/GoogleChromeLabs/squoosh",
"repository": {
"type": "git",
"url": "https://github.com/GoogleChromeLabs/squoosh.git"
},
"license": "Apache-2.0",
"engines": {
"node": " ^12.5.0 || ^14.0.0 || ^16.0.0 "
},
"dependencies": {
"wasm-feature-detect": "^1.2.11",
"web-streams-polyfill": "^3.0.3"
},
"devDependencies": {
@@ -25,7 +35,9 @@
"@types/node": "^15.6.1",
"rollup": "^2.46.0",
"rollup-plugin-terser": "^7.0.2",
"ts-node": "^10.2.1",
"typescript": "^4.1.3",
"uvu": "^0.5.1",
"which": "^2.0.2"
}
}

View File

@@ -2,6 +2,7 @@ import resolve from '@rollup/plugin-node-resolve';
import cjs from '@rollup/plugin-commonjs';
import simpleTS from './lib/simple-ts';
import asset from './lib/asset-plugin.js';
import chunk from './lib/chunk-plugin.js';
import json from './lib/json-plugin.js';
import autojson from './lib/autojson-plugin.js';
import { getBabelOutputPlugin } from '@rollup/plugin-babel';
@@ -9,7 +10,7 @@ import { builtinModules } from 'module';
/** @type {import('rollup').RollupOptions} */
export default {
input: 'src/index.js',
input: 'src/index.ts',
output: {
dir: 'build',
format: 'cjs',
@@ -18,6 +19,7 @@ export default {
plugins: [
resolve(),
cjs(),
chunk(),
asset(),
autojson(),
json(),

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

View File

@@ -2,6 +2,39 @@ import { instantiateEmscriptenWasm } from './emscripten-utils.js';
import visdif from '../../codecs/visdif/visdif.js';
import visdifWasm from 'asset-url:../../codecs/visdif/visdif.wasm';
import type ImageData from './image_data';
interface VisDiff {
distance: (data: Uint8ClampedArray) => number;
delete: () => void;
}
interface VisdiffConstructor {
new (data: Uint8ClampedArray, width: number, height: number): VisDiff;
}
interface VisDiffModule extends EmscriptenWasm.Module {
VisDiff: VisdiffConstructor;
}
type VisDiffModuleFactory = EmscriptenWasm.ModuleFactory<VisDiffModule>;
interface BinarySearchParams {
min?: number;
max?: number;
epsilon?: number;
maxRounds?: number;
}
interface AutoOptimizeParams extends BinarySearchParams {
butteraugliDistanceGoal?: number;
}
interface AutoOptimizeResult {
bitmap: ImageData;
binary: Uint8Array;
quality: number;
}
// `measure` is a (async) function that takes exactly one numeric parameter and
// returns a value. The function is assumed to be monotonic (an increase in `parameter`
@@ -9,9 +42,9 @@ import visdifWasm from 'asset-url:../../codecs/visdif/visdif.wasm';
// to find `parameter` such that `measure` returns `measureGoal`, within an error
// of `epsilon`. It will use at most `maxRounds` attempts.
export async function binarySearch(
measureGoal,
measure,
{ min = 0, max = 100, epsilon = 0.1, maxRounds = 6 } = {},
measureGoal: number,
measure: (val: number) => Promise<number>,
{ min = 0, max = 100, epsilon = 0.1, maxRounds = 6 }: BinarySearchParams = {},
) {
let parameter = (max - min) / 2 + min;
let delta = (max - min) / 4;
@@ -33,12 +66,21 @@ export async function binarySearch(
}
export async function autoOptimize(
bitmapIn,
encode,
decode,
{ butteraugliDistanceGoal = 1.4, ...otherOpts } = {},
) {
const { VisDiff } = await instantiateEmscriptenWasm(visdif, visdifWasm);
bitmapIn: ImageData,
encode: (
bitmap: ImageData,
quality: number,
) => Promise<Uint8Array> | Uint8Array,
decode: (binary: Uint8Array) => Promise<ImageData> | ImageData,
{
butteraugliDistanceGoal = 1.4,
...binarySearchParams
}: AutoOptimizeParams = {},
): Promise<AutoOptimizeResult> {
const { VisDiff } = await instantiateEmscriptenWasm(
visdif as VisDiffModuleFactory,
visdifWasm,
);
const comparator = new VisDiff(
bitmapIn.data,
@@ -46,8 +88,11 @@ export async function autoOptimize(
bitmapIn.height,
);
let bitmapOut;
let binaryOut;
// We're able to do non null assertion because
// we know that binarySearch will set these values
let bitmapOut!: ImageData;
let binaryOut!: Uint8Array;
// Increasing quality means _decrease_ in Butteraugli distance.
// `binarySearch` assumes that increasing `parameter` will
// increase the metric value. So multipliy Butteraugli values by -1.
@@ -58,7 +103,7 @@ export async function autoOptimize(
bitmapOut = await decode(binaryOut);
return -1 * comparator.distance(bitmapOut.data);
},
otherOpts,
binarySearchParams,
);
comparator.delete();

View File

@@ -1,5 +1,19 @@
import { promises as fsp } from 'fs';
import { instantiateEmscriptenWasm, pathify } from './emscripten-utils.js';
import { threads } from 'wasm-feature-detect';
import { cpus } from 'os';
// We use `navigator.hardwareConcurrency` for Emscriptens pthread pool size.
// This is the only workaround I can get working without crying.
(globalThis as any).navigator = {
hardwareConcurrency: cpus().length,
};
interface DecodeModule extends EmscriptenWasm.Module {
decode: (data: Uint8Array) => ImageData;
}
type DecodeModuleFactory = EmscriptenWasm.ModuleFactory<DecodeModule>;
interface RotateModuleInstance {
exports: {
@@ -25,7 +39,7 @@ interface ResizeInstantiateOptions {
declare global {
// Needed for being able to use ImageData as type in codec types
type ImageData = typeof import('./image_data.js');
type ImageData = import('./image_data.js').default;
// Needed for being able to assign to `globalThis.ImageData`
var ImageData: ImageData['constructor'];
}
@@ -33,30 +47,38 @@ declare global {
import type { QuantizerModule } from '../../codecs/imagequant/imagequant.js';
// MozJPEG
import type { MozJPEGModule as MozJPEGEncodeModule } from '../../codecs/mozjpeg/enc/mozjpeg_enc';
import mozEnc from '../../codecs/mozjpeg/enc/mozjpeg_node_enc.js';
import mozEncWasm from 'asset-url:../../codecs/mozjpeg/enc/mozjpeg_node_enc.wasm';
import mozDec from '../../codecs/mozjpeg/dec/mozjpeg_node_dec.js';
import mozDecWasm from 'asset-url:../../codecs/mozjpeg/dec/mozjpeg_node_dec.wasm';
// WebP
import type { WebPModule as WebPEncodeModule } from '../../codecs/webp/enc/webp_enc';
import webpEnc from '../../codecs/webp/enc/webp_node_enc.js';
import webpEncWasm from 'asset-url:../../codecs/webp/enc/webp_node_enc.wasm';
import webpDec from '../../codecs/webp/dec/webp_node_dec.js';
import webpDecWasm from 'asset-url:../../codecs/webp/dec/webp_node_dec.wasm';
// AVIF
import type { AVIFModule as AVIFEncodeModule } from '../../codecs/avif/enc/avif_enc';
import avifEnc from '../../codecs/avif/enc/avif_node_enc.js';
import avifEncWasm from 'asset-url:../../codecs/avif/enc/avif_node_enc.wasm';
import avifEncMt from '../../codecs/avif/enc/avif_node_enc_mt.js';
import avifEncMtWorker from 'chunk-url:../../codecs/avif/enc/avif_node_enc_mt.worker.js';
import avifEncMtWasm from 'asset-url:../../codecs/avif/enc/avif_node_enc_mt.wasm';
import avifDec from '../../codecs/avif/dec/avif_node_dec.js';
import avifDecWasm from 'asset-url:../../codecs/avif/dec/avif_node_dec.wasm';
// JXL
import type { JXLModule as JXLEncodeModule } from '../../codecs/jxl/enc/jxl_enc';
import jxlEnc from '../../codecs/jxl/enc/jxl_node_enc.js';
import jxlEncWasm from 'asset-url:../../codecs/jxl/enc/jxl_node_enc.wasm';
import jxlDec from '../../codecs/jxl/dec/jxl_node_dec.js';
import jxlDecWasm from 'asset-url:../../codecs/jxl/dec/jxl_node_dec.wasm';
// WP2
import type { WP2Module as WP2EncodeModule } from '../../codecs/wp2/enc/wp2_enc';
import wp2Enc from '../../codecs/wp2/enc/wp2_node_enc.js';
import wp2EncWasm from 'asset-url:../../codecs/wp2/enc/wp2_node_enc.wasm';
import wp2Dec from '../../codecs/wp2/dec/wp2_node_dec.js';
@@ -246,15 +268,20 @@ export const preprocessors = {
numRotations: 0,
},
},
};
} as const;
export const codecs = {
mozjpeg: {
name: 'MozJPEG',
extension: 'jpg',
detectors: [/^\xFF\xD8\xFF/],
dec: () => instantiateEmscriptenWasm(mozDec, mozDecWasm),
enc: () => instantiateEmscriptenWasm(mozEnc, mozEncWasm),
dec: () =>
instantiateEmscriptenWasm(mozDec as DecodeModuleFactory, mozDecWasm),
enc: () =>
instantiateEmscriptenWasm(
mozEnc as EmscriptenWasm.ModuleFactory<MozJPEGEncodeModule>,
mozEncWasm,
),
defaultEncoderOptions: {
quality: 75,
baseline: false,
@@ -282,9 +309,14 @@ export const codecs = {
webp: {
name: 'WebP',
extension: 'webp',
detectors: [/^RIFF....WEBPVP8[LX ]/],
dec: () => instantiateEmscriptenWasm(webpDec, webpDecWasm),
enc: () => instantiateEmscriptenWasm(webpEnc, webpEncWasm),
detectors: [/^RIFF....WEBPVP8[LX ]/s],
dec: () =>
instantiateEmscriptenWasm(webpDec as DecodeModuleFactory, webpDecWasm),
enc: () =>
instantiateEmscriptenWasm(
webpEnc as EmscriptenWasm.ModuleFactory<WebPEncodeModule>,
webpEncWasm,
),
defaultEncoderOptions: {
quality: 75,
target_size: 0,
@@ -324,8 +356,21 @@ export const codecs = {
name: 'AVIF',
extension: 'avif',
detectors: [/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/],
dec: () => instantiateEmscriptenWasm(avifDec, avifDecWasm),
enc: () => instantiateEmscriptenWasm(avifEnc, avifEncWasm),
dec: () =>
instantiateEmscriptenWasm(avifDec as DecodeModuleFactory, avifDecWasm),
enc: async () => {
if (await threads()) {
return instantiateEmscriptenWasm(
avifEncMt as EmscriptenWasm.ModuleFactory<AVIFEncodeModule>,
avifEncMtWasm,
avifEncMtWorker,
);
}
return instantiateEmscriptenWasm(
avifEnc as EmscriptenWasm.ModuleFactory<AVIFEncodeModule>,
avifEncWasm,
);
},
defaultEncoderOptions: {
cqLevel: 33,
cqAlphaLevel: -1,
@@ -348,8 +393,13 @@ export const codecs = {
name: 'JPEG-XL',
extension: 'jxl',
detectors: [/^\xff\x0a/],
dec: () => instantiateEmscriptenWasm(jxlDec, jxlDecWasm),
enc: () => instantiateEmscriptenWasm(jxlEnc, jxlEncWasm),
dec: () =>
instantiateEmscriptenWasm(jxlDec as DecodeModuleFactory, jxlDecWasm),
enc: () =>
instantiateEmscriptenWasm(
jxlEnc as EmscriptenWasm.ModuleFactory<JXLEncodeModule>,
jxlEncWasm,
),
defaultEncoderOptions: {
speed: 4,
quality: 75,
@@ -369,8 +419,13 @@ export const codecs = {
name: 'WebP2',
extension: 'wp2',
detectors: [/^\xF4\xFF\x6F/],
dec: () => instantiateEmscriptenWasm(wp2Dec, wp2DecWasm),
enc: () => instantiateEmscriptenWasm(wp2Enc, wp2EncWasm),
dec: () =>
instantiateEmscriptenWasm(wp2Dec as DecodeModuleFactory, wp2DecWasm),
enc: () =>
instantiateEmscriptenWasm(
wp2Enc as EmscriptenWasm.ModuleFactory<WP2EncodeModule>,
wp2EncWasm,
),
defaultEncoderOptions: {
quality: 75,
alpha_quality: 75,
@@ -401,7 +456,7 @@ export const codecs = {
await oxipngPromise;
return {
encode: (
buffer: Uint8Array,
buffer: Uint8ClampedArray | ArrayBuffer,
width: number,
height: number,
opts: { level: number },
@@ -424,4 +479,4 @@ export const codecs = {
max: 1,
},
},
};
} as const;

View File

@@ -1,4 +1,4 @@
import { fileURLToPath } from 'url';
import { fileURLToPath, URL } from 'url';
export function pathify(path: string): string {
if (path.startsWith('file://')) {
@@ -10,10 +10,17 @@ export function pathify(path: string): string {
export function instantiateEmscriptenWasm<T extends EmscriptenWasm.Module>(
factory: EmscriptenWasm.ModuleFactory<T>,
path: string,
workerJS: string = '',
): Promise<T> {
return factory({
locateFile() {
return pathify(path);
locateFile(requestPath) {
// The glue code generated by emscripten uses the original
// file names of the worker file and the wasm binary.
// These will have changed in the bundling process and
// we need to inject them here.
if (requestPath.endsWith('.wasm')) return pathify(path);
if (requestPath.endsWith('.worker.js')) return pathify(workerJS);
return requestPath;
},
});
}

View File

@@ -5,10 +5,18 @@ import { promises as fsp } from 'fs';
import { codecs as encoders, preprocessors } from './codecs.js';
import WorkerPool from './worker_pool.js';
import { autoOptimize } from './auto-optimizer.js';
import type ImageData from './image_data';
export { ImagePool, encoders, preprocessors };
type EncoderKey = keyof typeof encoders;
type PreprocessorKey = keyof typeof preprocessors;
type FileLike = Buffer | ArrayBuffer | string | ArrayBufferView;
async function decodeFile({ file }) {
async function decodeFile({
file,
}: {
file: FileLike;
}): Promise<{ bitmap: ImageData; size: number }> {
let buffer;
if (ArrayBuffer.isView(file)) {
buffer = Buffer.from(file.buffer);
@@ -16,8 +24,9 @@ async function decodeFile({ file }) {
} else if (file instanceof ArrayBuffer) {
buffer = Buffer.from(file);
file = 'Binary blob';
} else if (file instanceof Buffer) {
buffer = file;
} else if ((file as unknown) instanceof Buffer) {
// TODO: Check why we need type assertions here.
buffer = (file as unknown) as Buffer;
file = 'Binary blob';
} else if (typeof file === 'string') {
buffer = await fsp.readFile(file);
@@ -28,23 +37,33 @@ async function decodeFile({ file }) {
const firstChunkString = Array.from(firstChunk)
.map((v) => String.fromCodePoint(v))
.join('');
const key = Object.entries(encoders).find(([name, { detectors }]) =>
const key = Object.entries(encoders).find(([_name, { detectors }]) =>
detectors.some((detector) => detector.exec(firstChunkString)),
)?.[0];
)?.[0] as EncoderKey | undefined;
if (!key) {
throw Error(`${file} has an unsupported format`);
}
const rgba = (await encoders[key].dec()).decode(new Uint8Array(buffer));
const encoder = encoders[key];
const mod = await encoder.dec();
const rgba = mod.decode(new Uint8Array(buffer));
return {
bitmap: rgba,
size: buffer.length,
};
}
async function preprocessImage({ preprocessorName, options, image }) {
async function preprocessImage({
preprocessorName,
options,
image,
}: {
preprocessorName: PreprocessorKey;
options: any;
image: { bitmap: ImageData };
}) {
const preprocessor = await preprocessors[preprocessorName].instantiate();
image.bitmap = await preprocessor(
image.bitmap.data,
Uint8Array.from(image.bitmap.data),
image.bitmap.width,
image.bitmap.height,
options,
@@ -58,26 +77,39 @@ async function encodeImage({
encConfig,
optimizerButteraugliTarget,
maxOptimizerRounds,
}: {
bitmap: ImageData;
encName: EncoderKey;
encConfig: any;
optimizerButteraugliTarget: number;
maxOptimizerRounds: number;
}) {
let binary;
let binary: Uint8Array;
let optionsUsed = encConfig;
const encoder = await encoders[encName].enc();
if (encConfig === 'auto') {
const optionToOptimize = encoders[encName].autoOptimize.option;
const decoder = await encoders[encName].dec();
const encode = (bitmapIn, quality) =>
const encode = (bitmapIn: ImageData, quality: number) =>
encoder.encode(
bitmapIn.data,
bitmapIn.width,
bitmapIn.height,
Object.assign({}, encoders[encName].defaultEncoderOptions, {
Object.assign({}, encoders[encName].defaultEncoderOptions as any, {
[optionToOptimize]: quality,
}),
);
const decode = (binary) => decoder.decode(binary);
const decode = (binary: Uint8Array) => decoder.decode(binary);
const nonNullEncode = (bitmap: ImageData, quality: number): Uint8Array => {
const result = encode(bitmap, quality);
if (!result) {
throw new Error('There was an error while encoding');
}
return result;
};
const { binary: optimizedBinary, quality } = await autoOptimize(
bitmapIn,
encode,
nonNullEncode,
decode,
{
min: encoders[encName].autoOptimize.min,
@@ -92,12 +124,18 @@ async function encodeImage({
[optionToOptimize]: Math.round(quality * 10000) / 10000,
};
} else {
binary = encoder.encode(
const result = encoder.encode(
bitmapIn.data.buffer,
bitmapIn.width,
bitmapIn.height,
encConfig,
);
if (!result) {
throw new Error('There was an error while encoding');
}
binary = result;
}
return {
optionsUsed,
@@ -107,10 +145,15 @@ async function encodeImage({
};
}
// both decoding and encoding go through the worker pool
function handleJob(params) {
const { operation } = params;
switch (operation) {
type EncodeParams = { operation: 'encode' } & Parameters<typeof encodeImage>[0];
type DecodeParams = { operation: 'decode' } & Parameters<typeof decodeFile>[0];
type PreprocessParams = { operation: 'preprocess' } & Parameters<
typeof preprocessImage
>[0];
type JobMessage = EncodeParams | DecodeParams | PreprocessParams;
function handleJob(params: JobMessage) {
switch (params.operation) {
case 'encode':
return encodeImage(params);
case 'decode':
@@ -118,7 +161,7 @@ function handleJob(params) {
case 'preprocess':
return preprocessImage(params);
default:
throw Error(`Invalid job "${operation}"`);
throw Error(`Invalid job "${(params as any).operation}"`);
}
}
@@ -126,7 +169,12 @@ function handleJob(params) {
* Represents an ingested image.
*/
class Image {
constructor(workerPool, file) {
public file: FileLike;
public workerPool: WorkerPool<JobMessage, any>;
public decoded: Promise<{ bitmap: ImageData }>;
public encodedWith: { [key: string]: any };
constructor(workerPool: WorkerPool<JobMessage, any>, file: FileLike) {
this.file = file;
this.workerPool = workerPool;
this.decoded = workerPool.dispatchJob({ operation: 'decode', file });
@@ -143,14 +191,15 @@ class Image {
if (!Object.keys(preprocessors).includes(name)) {
throw Error(`Invalid preprocessor "${name}"`);
}
const preprocessorName = name as PreprocessorKey;
const preprocessorOptions = Object.assign(
{},
preprocessors[name].defaultOptions,
preprocessors[preprocessorName].defaultOptions,
options,
);
this.decoded = this.workerPool.dispatchJob({
operation: 'preprocess',
preprocessorName: name,
preprocessorName,
image: await this.decoded,
options: preprocessorOptions,
});
@@ -161,14 +210,22 @@ class Image {
/**
* Define one or several encoders to use on the image.
* @param {object} encodeOptions - An object with encoders to use, and their settings.
* @returns {Promise<undefined>} - A promise that resolves when the image has been encoded with all the specified encoders.
* @returns {Promise<void>} - A promise that resolves when the image has been encoded with all the specified encoders.
*/
async encode(encodeOptions = {}) {
async encode(
encodeOptions: {
optimizerButteraugliTarget?: number;
maxOptimizerRounds?: number;
} & {
[key in EncoderKey]?: any; // any is okay for now
} = {},
): Promise<void> {
const { bitmap } = await this.decoded;
for (const [encName, options] of Object.entries(encodeOptions)) {
if (!Object.keys(encoders).includes(encName)) {
for (const [name, options] of Object.entries(encodeOptions)) {
if (!Object.keys(encoders).includes(name)) {
continue;
}
const encName = name as EncoderKey;
const encRef = encoders[encName];
const encConfig =
typeof options === 'string'
@@ -193,28 +250,30 @@ class Image {
* A pool where images can be ingested and squooshed.
*/
class ImagePool {
public workerPool: WorkerPool<JobMessage, any>;
/**
* Create a new pool.
* @param {number} [threads] - Number of concurrent image processes to run in the pool. Defaults to the number of CPU cores in the system.
*/
constructor(threads) {
constructor(threads: number) {
this.workerPool = new WorkerPool(threads || cpus().length, __filename);
}
/**
* Ingest an image into the image pool.
* @param {string | Buffer | URL | object} image - The image or path to the image that should be ingested and decoded.
* @param {FileLike} image - The image or path to the image that should be ingested and decoded.
* @returns {Image} - A custom class reference to the decoded image.
*/
ingestImage(image) {
ingestImage(image: FileLike): Image {
return new Image(this.workerPool, image);
}
/**
* Closes the underlying image processing pipeline. The already processed images will still be there, but no new processing can start.
* @returns {Promise<undefined>} - A promise that resolves when the underlying pipeline has closed.
* @returns {Promise<void>} - A promise that resolves when the underlying pipeline has closed.
*/
async close() {
async close(): Promise<void> {
await this.workerPool.join();
}
}

View File

@@ -23,6 +23,11 @@ declare module 'asset-url:../../codecs/resize/pkg/squoosh_resize_bg.wasm' {
export default value;
}
declare module 'chunk-url:../../codecs/avif/enc/avif_node_enc_mt.worker.js' {
const value: string;
export default value;
}
// These don't exist in NodeJS types so we're not able to use them but they are referenced in some emscripten and codec types
// Thus, we need to explicitly assign them to be `never`
// We're also not able to use the APIs that use these types

View File

@@ -7,26 +7,19 @@ function uuid() {
).join('');
}
function jobPromise(worker, msg) {
return new Promise((resolve, reject) => {
const id = uuid();
worker.postMessage({ msg, id });
worker.on('message', function f({ error, result, id: rid }) {
if (rid !== id) {
return;
}
if (error) {
reject(error);
return;
}
worker.off('message', f);
resolve(result);
});
});
interface Job<I> {
msg: I;
resolve: Function;
reject: Function;
}
export default class WorkerPool {
constructor(numWorkers, workerFile) {
export default class WorkerPool<I, O> {
public numWorkers: number;
public jobQueue: TransformStream<Job<I>, Job<I>>;
public workerQueue: TransformStream<Worker, Worker>;
public done: Promise<void>;
constructor(numWorkers: number, workerFile: string) {
this.numWorkers = numWorkers;
this.jobQueue = new TransformStream();
this.workerQueue = new TransformStream();
@@ -48,9 +41,14 @@ export default class WorkerPool {
await this._terminateAll();
return;
}
if (!value) {
throw new Error('Reader did not return any value');
}
const { msg, resolve, reject } = value;
const worker = await this._nextWorker();
jobPromise(worker, msg)
this.jobPromise(worker, msg)
.then((result) => resolve(result))
.catch((reason) => reject(reason))
.finally(() => {
@@ -64,8 +62,12 @@ export default class WorkerPool {
async _nextWorker() {
const reader = this.workerQueue.readable.getReader();
const { value, done } = await reader.read();
const { value } = await reader.read();
reader.releaseLock();
if (!value) {
throw new Error('No worker left');
}
return value;
}
@@ -82,7 +84,7 @@ export default class WorkerPool {
await this.done;
}
dispatchJob(msg) {
dispatchJob(msg: I): Promise<O> {
return new Promise((resolve, reject) => {
const writer = this.jobQueue.writable.getWriter();
writer.write({ msg, resolve, reject });
@@ -90,14 +92,32 @@ export default class WorkerPool {
});
}
static useThisThreadAsWorker(cb) {
parentPort.on('message', async (data) => {
private jobPromise(worker: Worker, msg: I) {
return new Promise((resolve, reject) => {
const id = uuid();
worker.postMessage({ msg, id });
worker.on('message', function f({ error, result, id: rid }) {
if (rid !== id) {
return;
}
if (error) {
reject(error);
return;
}
worker.off('message', f);
resolve(result);
});
});
}
static useThisThreadAsWorker<I, O>(cb: (msg: I) => O) {
parentPort!.on('message', async (data) => {
const { msg, id } = data;
try {
const result = await cb(msg);
parentPort.postMessage({ result, id });
parentPort!.postMessage({ result, id });
} catch (e) {
parentPort.postMessage({ error: e.message, id });
parentPort!.postMessage({ error: e.message, id });
}
});
}

View File

@@ -0,0 +1,43 @@
import * as path from 'path';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { ImagePool } from '..';
let imagePool: ImagePool;
test.after.each(async () => {
if (imagePool) {
try {
await imagePool.close();
} catch (e) {}
}
imagePool = undefined;
});
test('smoke test', async () => {
imagePool = new ImagePool(1);
const imagePath = path.resolve(__dirname, '../../icon-large-maskable.png');
const image = imagePool.ingestImage(imagePath);
const { bitmap } = await image.decoded;
assert.equal(bitmap.width, 1024);
await image.preprocess({
resize: {
enabled: true,
width: 100,
},
});
await image.encode({
mozjpeg: {},
});
const { size } = await image.encodedWith.mozjpeg;
// resulting image is 1554b
assert.ok(size > 500);
assert.ok(size < 5000);
});
test.run();

View File

@@ -5,5 +5,12 @@
"types": ["node"],
"allowJs": true
},
"include": ["src/**/*", "../codecs/**/*"]
"include": ["src/**/*", "../codecs/**/*"],
"ts-node": {
"transpileOnly": true,
"compilerOptions": {
"module": "commonjs"
},
"include": ["tests/**/*"]
}
}

28
package-lock.json generated
View File

@@ -32,7 +32,7 @@
"lodash.camelcase": "^4.3.0",
"mime-types": "^2.1.28",
"npm-run-all": "^4.1.5",
"pointer-tracker": "^2.4.0",
"pointer-tracker": "^2.5.3",
"postcss": "^7.0.35",
"postcss-modules": "^3.2.2",
"postcss-nested": "^4.2.3",
@@ -48,6 +48,20 @@
"which": "^2.0.2"
}
},
"../pointer-tracker": {
"version": "2.5.0",
"extraneous": true,
"license": "Apache-2.0",
"devDependencies": {
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
"rollup": "^2.23.1",
"rollup-plugin-terser": "^7.0.0",
"rollup-plugin-typescript2": "^0.27.2",
"typescript": "^3.9.7"
}
},
"node_modules/@babel/code-frame": {
"version": "7.10.4",
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.10.4.tgz",
@@ -3871,9 +3885,9 @@
}
},
"node_modules/pointer-tracker": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/pointer-tracker/-/pointer-tracker-2.4.0.tgz",
"integrity": "sha512-pWI2tpaM/XNtc9mUTv42Rmjf6mkHvE8LT5DDEq0G7baPNhxNM9E3CepubPplSoSLk9E5bwQrAMyDcPVmJyTW4g==",
"version": "2.5.3",
"resolved": "https://registry.npmjs.org/pointer-tracker/-/pointer-tracker-2.5.3.tgz",
"integrity": "sha512-LiJUeIbzk4dXq678YeyrZ++mdY17q4n/2sBHfU9wIuvmSzdiPgMvmvWN2g8mY4J7YwYOIrqrZUWP/MfFHVwYtg==",
"dev": true
},
"node_modules/postcss": {
@@ -11946,9 +11960,9 @@
}
},
"pointer-tracker": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/pointer-tracker/-/pointer-tracker-2.4.0.tgz",
"integrity": "sha512-pWI2tpaM/XNtc9mUTv42Rmjf6mkHvE8LT5DDEq0G7baPNhxNM9E3CepubPplSoSLk9E5bwQrAMyDcPVmJyTW4g==",
"version": "2.5.3",
"resolved": "https://registry.npmjs.org/pointer-tracker/-/pointer-tracker-2.5.3.tgz",
"integrity": "sha512-LiJUeIbzk4dXq678YeyrZ++mdY17q4n/2sBHfU9wIuvmSzdiPgMvmvWN2g8mY4J7YwYOIrqrZUWP/MfFHVwYtg==",
"dev": true
},
"postcss": {

View File

@@ -6,9 +6,9 @@
"scripts": {
"build": "rollup -c && node lib/move-output.js",
"debug": "node --inspect-brk node_modules/.bin/rollup -c",
"dev": "run-p watch serve",
"dev": "DEV_PORT=\"${DEV_PORT:=5000}\" run-p watch serve",
"watch": "rollup -cw",
"serve": "serve --config ../../../serve.json .tmp/build/static"
"serve": "serve --listen=$DEV_PORT --config ../../../serve.json .tmp/build/static"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
@@ -32,7 +32,7 @@
"lodash.camelcase": "^4.3.0",
"mime-types": "^2.1.28",
"npm-run-all": "^4.1.5",
"pointer-tracker": "^2.4.0",
"pointer-tracker": "^2.5.3",
"postcss": "^7.0.35",
"postcss-modules": "^3.2.2",
"postcss-nested": "^4.2.3",

View File

@@ -20,7 +20,7 @@ const REFLECTED_ATTRIBUTES = [
'disabled',
];
function getPrescision(value: string): number {
function getPrecision(value: string): number {
const afterDecimal = value.split('.')[1];
return afterDecimal ? afterDecimal.length : 0;
}
@@ -112,18 +112,24 @@ class RangeInputElement extends HTMLElement {
this.dispatchEvent(retargetted);
};
private _getDisplayValue(value: number): string {
if (value >= 10000) return (value / 1000).toFixed(1) + 'k';
const labelPrecision =
Number(this.labelPrecision) || getPrecision(this.step) || 0;
return labelPrecision
? value.toFixed(labelPrecision)
: Math.round(value).toString();
}
private _update = () => {
// Not connected?
if (!this._valueDisplay) return;
const value = Number(this.value) || 0;
const min = Number(this.min) || 0;
const max = Number(this.max) || 100;
const labelPrecision =
Number(this.labelPrecision) || getPrescision(this.step) || 0;
const percent = (100 * (value - min)) / (max - min);
const displayValue = labelPrecision
? value.toFixed(labelPrecision)
: Math.round(value).toString();
const displayValue = this._getDisplayValue(value);
this._valueDisplay!.textContent = displayValue;
this.style.setProperty('--value-percent', percent + '%');

View File

@@ -6,10 +6,13 @@ import './custom-els/RangeInput';
import { linkRef } from 'shared/prerendered-app/util';
interface Props extends preact.JSX.HTMLAttributes {}
interface State {}
interface State {
textFocused: boolean;
}
export default class Range extends Component<Props, State> {
rangeWc?: RangeInputElement;
inputEl?: HTMLInputElement;
private onTextInput = (event: Event) => {
const input = event.target as HTMLInputElement;
@@ -23,10 +26,19 @@ export default class Range extends Component<Props, State> {
);
};
render(props: Props) {
private onTextFocus = () => {
this.setState({ textFocused: true });
};
private onTextBlur = () => {
this.setState({ textFocused: false });
};
render(props: Props, state: State) {
const { children, ...otherProps } = props;
const { value, min, max, step } = props;
const textValue = state.textFocused ? this.inputEl!.value : value;
return (
<label class={style.range}>
@@ -41,13 +53,16 @@ export default class Range extends Component<Props, State> {
/>
</div>
<input
ref={linkRef(this, 'inputEl')}
type="number"
class={style.textInput}
value={value}
value={textValue}
min={min}
max={max}
step={step}
onInput={this.onTextInput}
onFocus={this.onTextFocus}
onBlur={this.onTextBlur}
/>
</label>
);

View File

@@ -35,7 +35,7 @@
text-decoration-style: dotted;
text-decoration-color: var(--main-theme-color);
text-underline-position: under;
width: 48px;
width: 54px;
position: relative;
left: 5px;

View File

@@ -1,6 +1,7 @@
.options-scroller {
--horizontal-padding: 15px;
border-radius: var(--scroller-radius);
overflow: hidden;
/* At smaller widths, the multi-panel handles the scrolling */
@media (min-width: 600px) {

View File

@@ -1,5 +1,6 @@
import PointerTracker, { Pointer } from 'pointer-tracker';
import 'add-css:./styles.css';
import { isSafari } from 'client/lazy-app/util';
interface Point {
clientX: number;
@@ -81,6 +82,7 @@ function createPoint(): SVGPoint {
}
const MIN_SCALE = 0.01;
const MAX_SCALE = 100000;
export default class PinchZoom extends HTMLElement {
// The element that we'll transform.
@@ -104,14 +106,23 @@ export default class PinchZoom extends HTMLElement {
const pointerTracker: PointerTracker = new PointerTracker(this, {
start: (pointer, event) => {
// We only want to track 2 pointers at most
if (pointerTracker.currentPointers.length === 2 || !this._positioningEl)
if (
pointerTracker.currentPointers.length === 2 ||
!this._positioningEl
) {
return false;
}
event.preventDefault();
return true;
},
move: (previousPointers) => {
this._onPointerMove(previousPointers, pointerTracker.currentPointers);
},
// Unfortunately Safari on iOS has a bug where pointer event capturing
// doesn't work in some cases, and we hit those cases due to our event
// retargeting in pinch-zoom.
// https://bugs.webkit.org/show_bug.cgi?id=220196
avoidPointerEvents: isSafari,
});
this.addEventListener('wheel', (event) => this._onWheel(event));
@@ -244,6 +255,9 @@ export default class PinchZoom extends HTMLElement {
// Avoid scaling to zero
if (scale < MIN_SCALE) return;
// Avoid scaling to very large values
if (scale > MAX_SCALE) return;
// Return if there's no change
if (scale === this.scale && x === this.x && y === this.y) return;
@@ -296,9 +310,13 @@ export default class PinchZoom extends HTMLElement {
deltaY *= 15;
}
const zoomingOut = deltaY > 0;
// ctrlKey is true when pinch-zooming on a trackpad.
const divisor = ctrlKey ? 100 : 300;
const scaleDiff = 1 - deltaY / divisor;
// when zooming out, invert the delta and the ratio to keep zoom stable
const ratio = 1 - (zoomingOut ? -deltaY : deltaY) / divisor;
const scaleDiff = zoomingOut ? 1 / ratio : ratio;
this._applyChange({
scaleDiff,

View File

@@ -5,8 +5,10 @@ import './custom-els/PinchZoom';
import './custom-els/TwoUp';
import * as style from './style.css';
import 'add-css:./style.css';
import { shallowEqual } from '../../util';
import { shallowEqual, isSafari } from '../../util';
import {
ToggleAliasingIcon,
ToggleAliasingActiveIcon,
ToggleBackgroundIcon,
AddIcon,
RemoveIcon,
@@ -19,7 +21,6 @@ import { cleanSet } from '../../util/clean-modify';
import type { SourceImage } from '../../Compress';
import { linkRef } from 'shared/prerendered-app/util';
import { drawDataToCanvas } from 'client/lazy-app/util/canvas';
interface Props {
source?: SourceImage;
preprocessorState?: PreprocessorState;
@@ -35,6 +36,7 @@ interface State {
scale: number;
editingScale: boolean;
altBackground: boolean;
aliasing: boolean;
}
const scaleToOpts: ScaleToOpts = {
@@ -49,6 +51,7 @@ export default class Output extends Component<Props, State> {
scale: 1,
editingScale: false,
altBackground: false,
aliasing: false,
};
canvasLeft?: HTMLCanvasElement;
canvasRight?: HTMLCanvasElement;
@@ -145,6 +148,12 @@ export default class Output extends Component<Props, State> {
return props.rightCompressed || (props.source && props.source.preprocessed);
}
private toggleAliasing = () => {
this.setState((state) => ({
aliasing: !state.aliasing,
}));
};
private toggleBackground = () => {
this.setState({
altBackground: !this.state.altBackground,
@@ -255,7 +264,7 @@ export default class Output extends Component<Props, State> {
render(
{ mobileView, leftImgContain, rightImgContain, source }: Props,
{ scale, editingScale, altBackground }: State,
{ scale, editingScale, altBackground, aliasing }: State,
) {
const leftDraw = this.leftDrawable();
const rightDraw = this.rightDrawable();
@@ -275,7 +284,11 @@ export default class Output extends Component<Props, State> {
onTouchStartCapture={this.onRetargetableEvent}
onTouchEndCapture={this.onRetargetableEvent}
onTouchMoveCapture={this.onRetargetableEvent}
onPointerDownCapture={this.onRetargetableEvent}
onPointerDownCapture={
// We avoid pointer events in our PinchZoom due to a Safari bug.
// That means we also need to avoid them here too, else we end up preventing the fallback mouse events.
isSafari ? undefined : this.onRetargetableEvent
}
onMouseDownCapture={this.onRetargetableEvent}
onWheelCapture={this.onRetargetableEvent}
>
@@ -285,7 +298,9 @@ export default class Output extends Component<Props, State> {
ref={linkRef(this, 'pinchZoomLeft')}
>
<canvas
class={style.pinchTarget}
class={`${style.pinchTarget} ${
aliasing ? style.pixelated : ''
}`}
ref={linkRef(this, 'canvasLeft')}
width={leftDraw && leftDraw.width}
height={leftDraw && leftDraw.height}
@@ -301,7 +316,9 @@ export default class Output extends Component<Props, State> {
ref={linkRef(this, 'pinchZoomRight')}
>
<canvas
class={style.pinchTarget}
class={`${style.pinchTarget} ${
aliasing ? style.pixelated : ''
}`}
ref={linkRef(this, 'canvasRight')}
width={rightDraw && rightDraw.width}
height={rightDraw && rightDraw.height}
@@ -345,10 +362,31 @@ export default class Output extends Component<Props, State> {
</button>
</div>
<div class={style.buttonGroup}>
<button class={style.firstButton} onClick={this.onRotateClick}>
<button
class={style.firstButton}
onClick={this.onRotateClick}
title="Rotate"
>
<RotateIcon />
</button>
<button class={style.lastButton} onClick={this.toggleBackground}>
{!isSafari && (
<button
class={style.button}
onClick={this.toggleAliasing}
title="Toggle smoothing"
>
{aliasing ? (
<ToggleAliasingActiveIcon />
) : (
<ToggleAliasingIcon />
)}
</button>
)}
<button
class={style.lastButton}
onClick={this.toggleBackground}
title="Toggle background"
>
{altBackground ? (
<ToggleBackgroundActiveIcon />
) : (

View File

@@ -86,8 +86,7 @@
font-size: 1.2rem;
cursor: pointer;
&:focus {
/* box-shadow: 0 0 0 2px var(--hot-pink); */
&:focus-visible {
box-shadow: 0 0 0 2px #fff;
outline: none;
z-index: 1;
@@ -161,3 +160,8 @@ input.zoom {
pointer-events: auto;
}
}
.pixelated {
image-rendering: crisp-edges;
image-rendering: pixelated;
}

View File

@@ -11,6 +11,25 @@ const Icon = (props: preact.JSX.HTMLAttributes) => (
/>
);
export const ToggleAliasingIcon = (props: preact.JSX.HTMLAttributes) => (
<Icon {...props}>
<circle
cx="12"
cy="12"
r="8"
fill="none"
stroke="currentColor"
stroke-width="2"
/>
</Icon>
);
export const ToggleAliasingActiveIcon = (props: preact.JSX.HTMLAttributes) => (
<Icon {...props}>
<path d="M12 3h5v2h2v2h2v5h-2V9h-2V7h-2V5h-3V3M21 12v5h-2v2h-2v2h-5v-2h3v-2h2v-2h2v-3h2M12 21H7v-2H5v-2H3v-5h2v3h2v2h2v2h3v2M3 12V7h2V5h2V3h5v2H9v2H7v2H5v3H3" />
</Icon>
);
export const ToggleBackgroundIcon = (props: preact.JSX.HTMLAttributes) => (
<Icon {...props}>
<path d="M3 13h2v-2H3v2zm0 4h2v-2H3v2zm2 4v-2H3c0 1.1.9 2 2 2zM3 9h2V7H3v2zm12 12h2v-2h-2v2zm4-18H9a2 2 0 0 0-2 2v10c0 1.1.9 2 2 2h10a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2zm0 12H9V5h10v10zm-8 6h2v-2h-2v2zm-4 0h2v-2H7v2z" />

View File

@@ -14,6 +14,11 @@
import * as WebCodecs from '../util/web-codecs';
import { drawableToImageData } from './canvas';
/** If render engine is Safari */
export const isSafari =
/Safari\//.test(navigator.userAgent) &&
!/Chrom(e|ium)\//.test(navigator.userAgent);
/**
* Compare two objects, returning a boolean indicating if
* they have the same properties and strictly equal values.
@@ -95,7 +100,7 @@ const magicNumberMapInput = [
[/^I I/, 'image/tiff'],
[/^II*/, 'image/tiff'],
[/^MM\x00*/, 'image/tiff'],
[/^RIFF....WEBPVP8[LX ]/, 'image/webp'],
[/^RIFF....WEBPVP8[LX ]/s, 'image/webp'],
[/^\xF4\xFF\x6F/, 'image/webp2'],
[/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/, 'image/avif'],
[/^\xff\x0a/, 'image/jxl'],

View File

@@ -29,10 +29,9 @@ interface State {
slightLoss: boolean;
autoEdgePreservingFilter: boolean;
decodingSpeedTier: number;
photonNoiseIso: number;
}
const maxSpeed = 7;
export class Options extends Component<Props, State> {
static getDerivedStateFromProps(
props: Props,
@@ -47,7 +46,7 @@ export class Options extends Component<Props, State> {
// Create default form state from options
return {
options,
effort: maxSpeed - options.speed,
effort: options.effort,
quality: options.quality,
progressive: options.progressive,
edgePreservingFilter: options.epf === -1 ? 2 : options.epf,
@@ -55,6 +54,7 @@ export class Options extends Component<Props, State> {
slightLoss: options.lossyPalette,
autoEdgePreservingFilter: options.epf === -1,
decodingSpeedTier: options.decodingSpeedTier,
photonNoiseIso: options.photonNoiseIso,
};
}
@@ -87,15 +87,15 @@ export class Options extends Component<Props, State> {
};
const newOptions: EncodeOptions = {
speed: maxSpeed - optionState.effort,
effort: optionState.effort,
quality: optionState.lossless ? 100 : optionState.quality,
progressive: optionState.progressive,
epf: optionState.autoEdgePreservingFilter
? -1
: optionState.edgePreservingFilter,
nearLossless: 0,
lossyPalette: optionState.lossless ? optionState.slightLoss : false,
decodingSpeedTier: optionState.decodingSpeedTier,
photonNoiseIso: optionState.photonNoiseIso,
};
// Updating options, so we don't recalculate in getDerivedStateFromProps.
@@ -121,6 +121,7 @@ export class Options extends Component<Props, State> {
slightLoss,
autoEdgePreservingFilter,
decodingSpeedTier,
photonNoiseIso,
}: State,
) {
// I'm rendering both lossy and lossless forms, as it becomes much easier when
@@ -164,7 +165,6 @@ export class Options extends Component<Props, State> {
<label class={style.optionToggle}>
Auto edge filter
<Checkbox
name="autoEdgeFilter"
checked={autoEdgePreservingFilter}
onChange={this._inputChange(
'autoEdgePreservingFilter',
@@ -199,6 +199,17 @@ export class Options extends Component<Props, State> {
Optimise for decoding speed (worse compression):
</Range>
</div>
<div class={style.optionOneCell}>
<Range
min="0"
max="50000"
step="100"
value={photonNoiseIso}
onInput={this._inputChange('photonNoiseIso', 'number')}
>
Noise equivalent to ISO:
</Range>
</div>
</div>
)}
</Expander>
@@ -212,8 +223,8 @@ export class Options extends Component<Props, State> {
</label>
<div class={style.optionOneCell}>
<Range
min="0"
max={maxSpeed - 1}
min="3"
max="9"
value={effort}
onInput={this._inputChange('effort', 'number')}
>

View File

@@ -18,11 +18,11 @@ export const label = 'JPEG XL (beta)';
export const mimeType = 'image/jxl';
export const extension = 'jxl';
export const defaultOptions: EncodeOptions = {
speed: 4,
effort: 7,
quality: 75,
progressive: false,
epf: -1,
nearLossless: 0,
lossyPalette: false,
decodingSpeedTier: 0,
photonNoiseIso: 0,
};

View File

@@ -0,0 +1,51 @@
import { h, Component, RenderableProps } from 'preact';
interface Props {}
interface State {}
export default class SlideOnScroll extends Component<Props, State> {
private observer?: IntersectionObserver;
componentDidMount() {
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
const base = this.base as HTMLElement;
let wasOutOfView = false;
this.observer = new IntersectionObserver(
(entries, observer) => {
for (const entry of entries) {
if (!entry.isIntersecting) {
wasOutOfView = true;
base.style.opacity = '0';
return;
}
// Only transition in if the element was at some point out of view.
if (wasOutOfView) {
base.style.opacity = '';
base.animate(
{ offset: 0, opacity: '0', transform: 'translateY(40px)' },
{ duration: 300, easing: 'ease' },
);
}
observer.unobserve(entry.target);
}
},
{ threshold: 0.2 },
);
this.observer.observe(base);
}
componentWillUnmount() {
// Have to manually disconnect due to memory leaks in browsers.
// One day we'll be able to remove this, and the private property.
// https://twitter.com/jaffathecake/status/1405437361643790337
if (this.observer) this.observer.disconnect();
}
render({ children }: RenderableProps<Props>) {
return <div>{children}</div>;
}
}

View File

@@ -329,10 +329,11 @@ export function startBlobAnim(canvas: HTMLCanvasElement) {
hasFocus = false;
};
new ResizeObserver(() => {
const resizeObserver = new ResizeObserver(() => {
// Redraw for new canvas size
if (!animating) drawFrame(0);
}).observe(canvas);
});
resizeObserver.observe(canvas);
addEventListener('focus', focusListener);
addEventListener('blur', blurListener);
@@ -341,6 +342,7 @@ export function startBlobAnim(canvas: HTMLCanvasElement) {
function destruct() {
removeEventListener('focus', focusListener);
removeEventListener('blur', blurListener);
resizeObserver.disconnect();
document.removeEventListener('visibilitychange', visibilityListener);
}

View File

@@ -0,0 +1 @@
<svg width="498" height="333" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M401.17 125.52C387.072 53.923 324.253.173 248.787.173c-59.916 0-111.954 34.035-137.869 83.841C48.513 90.655 0 143.574 0 207.7c0 68.692 55.77 124.516 124.394 124.516h269.519c57.221 0 103.662-46.486 103.662-103.763 0-54.787-42.502-99.198-96.405-102.933z" fill="#91D3FF" fill-opacity=".3"/><path d="M187.247 121.321l-3.987-3.987-3.481 4.434c-11.519 14.67-18.366 33.15-18.366 53.242 0 48.003 38.882 86.885 86.885 86.885 20.091 0 38.572-6.848 53.242-18.366l4.434-3.482-3.987-3.986-114.74-114.74zM309.348 228.7l3.987 3.986 3.481-4.434c11.519-14.669 18.366-33.15 18.366-53.242 0-48.002-38.882-86.884-86.884-86.884-20.092 0-38.573 6.847-53.242 18.365l-4.435 3.482 3.987 3.986L309.348 228.7zm-158.406-53.69c0-53.739 43.617-97.355 97.356-97.355 53.738 0 97.355 43.616 97.355 97.355 0 53.739-43.617 97.356-97.355 97.356-53.739 0-97.356-43.617-97.356-97.356z" fill="#FF3385" stroke="#FF3385" stroke-width="10"/></svg>

After

Width:  |  Height:  |  Size: 991 B

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 5.2 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

@@ -10,12 +10,16 @@ import deviceScreen from 'url:./imgs/demos/demo-device-screen.png';
import largePhotoIcon from 'url:./imgs/demos/icon-demo-large-photo.jpg';
import artworkIcon from 'url:./imgs/demos/icon-demo-artwork.jpg';
import deviceScreenIcon from 'url:./imgs/demos/icon-demo-device-screen.jpg';
import smallSectionAsset from 'url:./imgs/info-content/small.svg';
import simpleSectionAsset from 'url:./imgs/info-content/simple.svg';
import secureSectionAsset from 'url:./imgs/info-content/secure.svg';
import logoIcon from 'url:./imgs/demos/icon-demo-logo.png';
import logoWithText from 'url:./imgs/logo-with-text.svg';
import * as style from './style.css';
import type SnackBarElement from 'shared/custom-els/snack-bar';
import 'shared/custom-els/snack-bar';
import { startBlobs } from './blob-anim/meta';
import SlideOnScroll from './SlideOnScroll';
const demos = [
{
@@ -336,37 +340,125 @@ export default class Intro extends Component<Props, State> {
</ul>
</div>
</div>
<div class={style.footer}>
<div class={style.bottomWave}>
<svg viewBox="0 0 1920 79" class={style.topWave}>
<path
d="M0 59l64-11c64-11 192-34 320-43s256-5 384 4 256 23 384 34 256 21 384 14 256-30 320-41l64-11v94H0z"
class={style.footerWave}
class={style.infoWave}
/>
</svg>
<div class={style.contentPadding}>
<footer class={style.footerItems}>
<a
class={style.footerLink}
href="https://github.com/GoogleChromeLabs/squoosh/blob/dev/README.md#privacy"
>
Privacy
</a>
<a
class={style.footerLink}
href="https://github.com/GoogleChromeLabs/squoosh/tree/dev/cli"
>
Squoosh CLI
</a>
<a
class={style.footerLinkWithLogo}
href="https://github.com/GoogleChromeLabs/squoosh"
>
<img src={githubLogo} alt="" width="10" height="10" />
Source on Github
</a>
</footer>
</div>
</div>
<section class={style.info}>
<div class={style.infoContainer}>
<SlideOnScroll>
<div class={style.infoContent}>
<div class={style.infoTextWrapper}>
<h2 class={style.infoTitle}>Small</h2>
<p class={style.infoCaption}>
Smaller images mean faster load times. Squoosh can reduce
file size and maintain high quality.
</p>
</div>
<div class={style.infoImgWrapper}>
<img
class={style.infoImg}
src={smallSectionAsset}
alt="silhouette of a large 1.4 megabyte image shrunk into a smaller 80 kilobyte image"
width="536"
height="522"
/>
</div>
</div>
</SlideOnScroll>
</div>
</section>
<section class={style.info}>
<div class={style.infoContainer}>
<SlideOnScroll>
<div class={style.infoContent}>
<div class={style.infoTextWrapper}>
<h2 class={style.infoTitle}>Simple</h2>
<p class={style.infoCaption}>
Open your image, inspect the differences, then save
instantly. Feeling adventurous? Adjust the settings for even
smaller files.
</p>
</div>
<div class={style.infoImgWrapper}>
<img
class={style.infoImg}
src={simpleSectionAsset}
alt="grid of multiple shrunk images displaying various options"
width="538"
height="384"
/>
</div>
</div>
</SlideOnScroll>
</div>
</section>
<section class={style.info}>
<div class={style.infoContainer}>
<SlideOnScroll>
<div class={style.infoContent}>
<div class={style.infoTextWrapper}>
<h2 class={style.infoTitle}>Secure</h2>
<p class={style.infoCaption}>
Worried about privacy? Images never leave your device since
Squoosh does all the work locally.
</p>
</div>
<div class={style.infoImgWrapper}>
<img
class={style.infoImg}
src={secureSectionAsset}
alt="silhouette of a cloud with a 'no' symbol on top"
width="498"
height="333"
/>
</div>
</div>
</SlideOnScroll>
</div>
</section>
<footer class={style.footer}>
<div class={style.footerContainer}>
<svg viewBox="0 0 1920 79" class={style.topWave}>
<path
d="M0 59l64-11c64-11 192-34 320-43s256-5 384 4 256 23 384 34 256 21 384 14 256-30 320-41l64-11v94H0z"
class={style.footerWave}
/>
</svg>
<div class={style.footerPadding}>
<footer class={style.footerItems}>
<a
class={style.footerLink}
href="https://github.com/GoogleChromeLabs/squoosh/blob/dev/README.md#privacy"
>
Privacy
</a>
<a
class={style.footerLink}
href="https://github.com/GoogleChromeLabs/squoosh/tree/dev/cli"
>
Squoosh CLI
</a>
<a
class={style.footerLinkWithLogo}
href="https://github.com/GoogleChromeLabs/squoosh"
>
<img src={githubLogo} alt="" width="10" height="10" />
Source on Github
</a>
</footer>
</div>
</div>
</footer>
{beforeInstallEvent && (
<button class={style.installBtn} onClick={this.onInstallClick}>
Install

View File

@@ -118,7 +118,114 @@
fill: var(--light-blue);
}
.bottom-wave {
position: relative;
}
.info {
background: var(--white);
position: relative;
padding: 5em 2em;
@media (min-width: 1200px) {
padding: 5em 0;
}
}
.info-container {
max-width: 1000px;
margin: 0 auto;
}
.info-content {
display: grid;
align-items: center;
grid-template-columns: 1fr;
gap: 1em;
grid-template-areas:
'text'
'img';
@media (min-width: 712px) {
grid-template-columns: 1fr 1fr;
grid-template-areas: 'text img';
.info:nth-child(even) & {
grid-template-areas: 'img text';
}
}
}
.info-title {
color: var(--pink);
font-size: 3em;
margin: 0;
}
.info-caption {
font-size: 1.5em;
line-height: 1.75;
margin: 1em 0 0.5em 0;
}
.info-link {
font-size: 1.25em;
text-underline-offset: 0.25em;
color: var(--off-black);
transition: color 400ms ease-in-out;
margin-top: 1em;
&:hover {
color: var(--dim-blue);
}
}
.info-text-wrapper {
display: flex;
flex-flow: column wrap;
max-width: 27em;
justify-self: center;
grid-area: text;
@media (min-width: 712px) {
justify-self: start;
.info:nth-child(even) & {
text-align: right;
justify-self: end;
}
}
}
.info-img-wrapper {
grid-area: img;
}
.info-img {
display: block;
width: 100%;
height: auto;
max-width: 400px;
margin: 0 auto;
@media (min-width: 768px) {
margin: 0 0 0 auto;
.info:nth-child(even) & {
margin: 0;
}
}
}
.info-wave {
fill: var(--white);
}
.footer {
background: var(--white);
padding-top: 5em;
}
.footer-container {
position: relative;
background: var(--light-gray);
}
@@ -128,7 +235,11 @@
}
.content-padding {
padding: 2rem;
padding: 2em 0;
}
.footer-padding {
padding: 2em;
}
.footer-items {

View File

@@ -25,18 +25,6 @@ import { lookup as lookupMime } from 'mime-types';
const manifestSize = ({ width, height }: { width: number; height: number }) =>
`${width}x${height}`;
// Set by Netlify
const branch = process.env.BRANCH;
const branchOriginTrialIds = new Map([
[
'live',
'Aj5GY7W9AHM8di+yvMCajIhLRHoYN7slruwOYXE/Iub5hgmW/r2RQt07vrUuT4eUTkWxcyNCAVkiI+5ugdVW3gAAAABUeyJvcmlnaW4iOiJodHRwczovL3NxdW9vc2guYXBwOjQ0MyIsImZlYXR1cmUiOiJXZWJBc3NlbWJseVNpbWQiLCJleHBpcnkiOjE2MjM4MDE1OTl9',
],
]);
const originTrialId = branchOriginTrialIds.get(branch || '');
interface Output {
[outputPath: string]: string;
}
@@ -110,15 +98,6 @@ const toOutput: Output = {
/*
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
# Origin trial for WebAssembly SIMD.
${
originTrialId
? ` Origin-Trial: ${originTrialId}`
: `# Cannot find origin trial ID. process.env.BRANCH is: ${JSON.stringify(
branch,
)}`
}
`,
};

View File

@@ -16,7 +16,8 @@ import baseCss from 'css:./base.css';
import initialCss from 'initial-css:';
import { allSrc } from 'client-bundle:client/initial-app';
import favicon from 'url:static-build/assets/favicon.ico';
import { escapeStyleScriptContent } from 'static-build/utils';
import ogImage from 'url:static-build/assets/icon-large-maskable.png';
import { escapeStyleScriptContent, siteOrigin } from 'static-build/utils';
import Intro from 'shared/prerendered-app/Intro';
interface Props {}
@@ -27,7 +28,27 @@ const Index: FunctionalComponent<Props> = () => (
<title>Squoosh</title>
<meta
name="description"
content="Compress and compare images with different codecs, right in your browser"
content="Squoosh is the ultimate image optimizer that allows you to compress and compare images with different codecs in your browser."
/>
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@SquooshApp" />
<meta property="og:title" content="Squoosh" />
<meta property="og:type" content="website" />
<meta property="og:image" content={`${siteOrigin}${ogImage}`} />
<meta
property="og:image:secure_url"
content={`${siteOrigin}${ogImage}`}
/>
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="500" />
<meta property="og:image:height" content="500" />
<meta
property="og:image:alt"
content="A cartoon of a hand squeezing an image file on a dark background."
/>
<meta
name="og:description"
content="Squoosh is the ultimate image optimizer that allows you to compress and compare images with different codecs in your browser."
/>
<meta
name="viewport"
@@ -38,6 +59,7 @@ const Index: FunctionalComponent<Props> = () => (
<link rel="shortcut icon" href={favicon} />
<meta name="theme-color" content="#ff3385" />
<link rel="manifest" href="/manifest.json" />
<link rel="canonical" href={siteOrigin} />
<style
dangerouslySetInnerHTML={{ __html: escapeStyleScriptContent(baseCss) }}
/>

View File

@@ -56,3 +56,17 @@ export function escapeStyleScriptContent(str: string): string {
.replace(/<style/g, '<\\style')
.replace(/<\/style/g, '<\\/style');
}
/**
* Origin of the site, depending on the environment.
*/
export const siteOrigin = (() => {
if (process.env.DEV_PORT) return `http://localhost:${process.env.DEV_PORT}`;
// https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
if (process.env.CONTEXT === 'production') return 'https://squoosh.app';
if (process.env.DEPLOY_PRIME_URL) return process.env.DEPLOY_PRIME_URL;
console.warn(
'Unable to determine site origin, defaulting to https://squoosh.app',
);
return 'https://squoosh.app';
})();

View File

@@ -1,7 +1,7 @@
{
"extends": "./generic-tsconfig.json",
"compilerOptions": {
"lib": ["esnext", "dom"],
"lib": ["esnext", "dom", "dom.iterable"],
"types": ["node"]
},
"include": ["src/shared/**/*", "src/static-build/**/*"]