@wasm-codecs/mozjpeg
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@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

Dependents (7)

Package Sidebar

Install

npm i @wasm-codecs/mozjpeg

Weekly Downloads

820

Version

1.0.1

License

MIT

Unpacked Size

376 kB

Total Files

13

Last publish

Collaborators

  • cyrilwanner