JSPM

@wasm-codecs/mozjpeg

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 449
  • Score
    100M100P100Q103043F
  • License MIT

MozJPEG image encoder in WebAssembly

Package Exports

  • @wasm-codecs/mozjpeg

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@wasm-codecs/mozjpeg) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@wasm-codecs/mozjpeg npm version license downloads

MozJPEG WebAssembly Codec

Table of contents

  1. Installation
  2. Usage
  3. API
  4. Examples
  5. License

Installation

npm install @wasm-codecs/mozjpeg

Requirements:

  • Node.js 10 or later

Usage

import encode from '@wasm-codecs/mozjpeg';

(async () => {
  const encodedImage = await encode(image, inputInfo, encodeOptions);
})();

API

encode(image, inputInfo, encodeOptions?): Buffer

Returns a buffer containing the compressed image data.

image: Buffer

A raw RGB image input buffer.

inputInfo: InputInfo

Those are required informations about the raw input image. Channels specifies the amount of channels in the image: Buffer param and defaults to 3 (RGB).

type InputInfo = {
  width: number;
  height: number;
  channels?: number;
}
encodeOptions?: EncodeOptions

All encoding options are optional and fall back to the default values.

type EncodeOptions = {
  quality?: number;
  baseline?: boolean;
  arithmetic?: boolean;
  progressive?: boolean;
  optimizeCoding?: boolean;
  smoothing?: number;
  colorSpace?: ColorSpace;
  quantTable?: number;
  trellisMultipass?: boolean;
  trellisOptZero?: boolean;
  trellisOptTable?: boolean;
  trellisLoops?: number;
  autoSubsample?: boolean;
  chromaSubsample?: number;
  separateChromaQuality?: boolean;
  chromaQuality?: number;
}

Examples

Using sharp in Node.js

import fs from 'fs';
import sharp from 'sharp';
import encode from '@wasm-codecs/mozjpeg';

(async () => {
  // read input image and convert it to a raw buffer using sharp
  const {
    data,
    info: { width, height, channels },
  } = await sharp('in.jpg')
    .raw()
    .toBuffer({ resolveWithObject: true });

  // encode the image using @wasm-codecs/mozjpeg
  const output = await encode(data, { width, height, channels });

  // save the image to the file system
  fs.writeFileSync('out.jpg', output);
})();

License

Licensed under the MIT license.

© Copyright Cyril Wanner