diff --git a/codecs/mozjpeg/Makefile b/codecs/mozjpeg/Makefile index ea0e34b6..0ba09a10 100644 --- a/codecs/mozjpeg/Makefile +++ b/codecs/mozjpeg/Makefile @@ -11,14 +11,21 @@ $(filter enc/%,$(OUT_JS)): enc/mozjpeg_enc.cpp %.js: $(CXX) \ - -I ${MOZJPEG}/include \ - -L ${MOZJPEG}/lib \ - ${CXXFLAGS} \ - ${LDFLAGS} \ - --bind \ + -O3 \ + -flto \ + -s FILESYSTEM=0 \ + -s PTHREAD_POOL_SIZE=navigator.hardwareConcurrency \ + -s ALLOW_MEMORY_GROWTH=1 \ + -s TEXTDECODER=2 \ + -s NODEJS_CATCH_EXIT=0 -s NODEJS_CATCH_REJECTION=0 \ -s ENVIRONMENT=$(ENVIRONMENT) \ -s EXPORT_ES6=1 \ + -lembind \ + ${CXXFLAGS} \ + ${LDFLAGS} \ -o $@ \ + -I ${MOZJPEG}/include \ + -L ${MOZJPEG}/lib \ -ljpeg \ ${MOZJPEG}/lib/rdswitch.o \ $+ diff --git a/codecs/mozjpeg/enc/mozjpeg_enc.d.ts b/codecs/mozjpeg/enc/mozjpeg_enc.d.ts new file mode 100644 index 00000000..3c730ae3 --- /dev/null +++ b/codecs/mozjpeg/enc/mozjpeg_enc.d.ts @@ -0,0 +1,37 @@ +export const enum MozJpegColorSpace { + GRAYSCALE = 1, + RGB, + YCbCr, +} + +export interface EncodeOptions { + quality: number; + baseline: boolean; + arithmetic: boolean; + progressive: boolean; + optimize_coding: boolean; + smoothing: number; + color_space: MozJpegColorSpace; + quant_table: number; + trellis_multipass: boolean; + trellis_opt_zero: boolean; + trellis_opt_table: boolean; + trellis_loops: number; + auto_subsample: boolean; + chroma_subsample: number; + separate_chroma_quality: boolean; + chroma_quality: number; +} + +export interface MozJPEGModule extends EmscriptenWasm.Module { + encode( + data: BufferSource, + width: number, + height: number, + options: EncodeOptions, + ): Uint8Array; +} + +declare var moduleFactory: EmscriptenWasm.ModuleFactory; + +export default moduleFactory; diff --git a/codecs/mozjpeg/flake.nix b/codecs/mozjpeg/flake.nix index d680a11e..9fb00e60 100644 --- a/codecs/mozjpeg/flake.nix +++ b/codecs/mozjpeg/flake.nix @@ -20,12 +20,12 @@ pkgs = nixpkgs.legacyPackages.${system}; in with pkgs; - { + rec { packages = rec { default = mozjpeg-squoosh; mozjpeg-squoosh = stdenv.mkDerivation { name = "mozjpeg-squoosh"; - src = ./.; + src = lib.cleanSource ./.; nativeBuildInputs = [ emscripten mozjpeg @@ -38,7 +38,7 @@ ''; installPhase = '' mkdir -p $out - cp enc/*.{wasm,js} $out + cp -r enc $out ''; }; mozjpeg = stdenv.mkDerivation { @@ -77,14 +77,15 @@ dontFixup = true; }; installScript = writeShellScriptBin "install.sh" '' - ${pkgs.coreutils}/bin/mkdir build - ${pkgs.coreutils}/bin/cp ${mozjpeg-squoosh}/* build/ + ${pkgs.coreutils}/bin/rm -rf build + ${pkgs.coreutils}/bin/mkdir -p build + ${pkgs.rsync}/bin/rsync --chmod=u+w -r ${mozjpeg-squoosh}/* build/ ''; }; apps = { install = { type = "app"; - # program = " + program = "${packages.installScript}/bin/install.sh"; }; }; } diff --git a/codecs/mozjpeg/package-lock.json b/codecs/mozjpeg/package-lock.json deleted file mode 100644 index a6f8fa4f..00000000 --- a/codecs/mozjpeg/package-lock.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "mozjpeg_enc", - "lockfileVersion": 1 -} diff --git a/codecs/mozjpeg/package.json b/codecs/mozjpeg/package.json deleted file mode 100644 index 37fdf439..00000000 --- a/codecs/mozjpeg/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "scripts": { - "build": "../build-cpp.sh" - } -} diff --git a/src/features/encoders/mozJPEG/shared/meta.ts b/src/features/encoders/mozJPEG/shared/meta.ts index 3bfbae6c..fae306a9 100644 --- a/src/features/encoders/mozJPEG/shared/meta.ts +++ b/src/features/encoders/mozJPEG/shared/meta.ts @@ -13,7 +13,7 @@ import { EncodeOptions, MozJpegColorSpace, -} from 'codecs/mozjpeg/enc/mozjpeg_enc'; +} from 'codecs/mozjpeg/build/enc/mozjpeg_enc'; export { EncodeOptions, MozJpegColorSpace }; export const label = 'MozJPEG'; diff --git a/src/features/encoders/mozJPEG/worker/mozjpegEncode.ts b/src/features/encoders/mozJPEG/worker/mozjpegEncode.ts index 949999a9..017909b6 100644 --- a/src/features/encoders/mozJPEG/worker/mozjpegEncode.ts +++ b/src/features/encoders/mozJPEG/worker/mozjpegEncode.ts @@ -10,7 +10,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import mozjpeg_enc, { MozJPEGModule } from 'codecs/mozjpeg/enc/mozjpeg_enc'; +import mozjpeg_enc, { + MozJPEGModule, +} from 'codecs/mozjpeg/build/enc/mozjpeg_enc'; import { EncodeOptions } from '../shared/meta'; import { initEmscriptenModule } from 'features/worker-utils';