Open jahed opened 1 month ago
Brilliant suggestion @jahed.
I don't have time to work on this myself. I'd welcome someone to look into providing one and I am happy to help where I can! 🙇
Tried building the current jpeg codecs from a default GitHub Codespace, switched to Node 20 to match CI.
nvm use 20
cd packages/jpeg/codec
npm run build
WASM files are created, but terser via empscripten hits a null pointer.
TypeError: Cannot read property 'print' of undefined
Could be due to https://github.com/emscripten-core/emscripten/issues/14525 which is fixed. https://github.com/emscripten-core/emscripten/issues/13297#issuecomment-1167602986
I noticed there are few old/deprecated versions, like emsdk using Node 14. emsdk@v3 is out. So some version upgrades might help.
I'll take another look when I'm ready to integrate this feature. If anyone else wants to, feel free. I'm not familiar with C++/Emscripten or mozjpeg's source so I'd appreciate it.
Thanks I believe that error is actually caused by --pre-js
option. You can comment out the follow line and it should compile:
https://github.com/jamsinclair/jSquash/blob/9e7eb888a707cb0f8bb951f24bf2ea4a839f9225/packages/jpeg/codec/Makefile#L24
We can probably use a later emscripten version to bypass that 🤔
Edit: Confirmed Emscripten 3.1.57 seems to work on my local codespace.
Thanks for taking a look @jahed.
Looks like we both added similar fixes at the same time. I've also gone ahead and updated the scripts in the main branch to build it with Emscripten 3.1.57.
After playing around further, one observation is that jpegtran
is a command line tool. Some extra care will be needed to make this work. One alternative would be to do the transformations ourselves by using the transupp.c
api.
Looks like emsdk@v3 can't clean compile mozjpeg. Downgrading back to emsdk@v2 works (with the terser error).
config.log shows a permission error which is caused by caches created during the docker build
RUN emcc
steps.
Fixed by setting permissions to the cache files immediately after as suggested here: https://github.com/emscripten-core/emsdk/issues/535#issuecomment-651849416
RUN chmod -R 777 ${EMSDK}/upstream/emscripten/cache
Next, it was failing at ./conftest
with no obvious errors. There's some [emcc](emcc: error: no input files)
errors logged but I'm guessing they're fine as one of them passes.
The old version of terser in emsdk@2 is failing at this block (unsupported import.meta
syntax):
If emsdk@v3 is too much effort to upgrade to right now, that block can be removed. Node 18 is the lowest maintained NodeJS version and it supports import.meta.url
.
Created a PR for that if it's a viable option:
I also commented in the merge request, but the import.meta.url
hotfix is still needed to support cloudflare workers.
I think I have solved the compilation issues with emsdk@3 if you pull the latest main branch.
Is your feature request related to a problem? Please describe. I'd like to be able to optimise existing JPGs without re-encoding them and losing quality.
Describe the solution you'd like jpegtran is a popular program to optimise JPGs. It's a part of libjpeg-turbo, so it's also available under mozjpeg. mozjpeg is already used in
@jsquash/jpeg
. So adding anoptimise
function export would make sense, similar to@jsquash/oxipng
.https://github.com/mozilla/mozjpeg/blob/master/jpegtran.c
jpegtran has a bunch of options, I typically use
-optimize -copy none -progressive
.https://github.com/mozilla/mozjpeg/blob/master/jpegtran.1
Describe alternatives you've considered jSquash is the only actively maintained library I've found that provides oxipng WASM builds with web browser support. It'd be perfect if it also provided a way to optimise JPGs. As far as I can tell, there's no actively maintained package for jpegtran WASM builds or any other package that can optimise JPGs like it can.
Additional context n/a