Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

icodec

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

icodec

Image encoders & decoders built with WebAssembly

  • 0.6.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

icodec

NPM Version NPM Downloads NPM Type Definitions No Dependency

Image encoders & decoders built with WebAssembly, support high-depth.

ModuleEncoderDecoderBit Depth
jpeg MozJPEG 8
png OxiPNG + imagequant image-png 8, 16
qoi qoi 8
webp libwebp 8
heic libheif + x265 libheif + libde265 8, 10, 12
avif libavif + aom 8, 10, 12, 16*
jxl libjxl from 8 to 16
wp2 libwebp2 8

[!WARNING] Since libheif does not support specify thread count for x265 encoder, The encode of the heic module only work in webworker.

wp2 is experimental, file encoded in old version may be invalid for newer decoder.

* 16-bit AVIF uses experimental simple transform that store image in 12-bit + extra 4-bit hidden image item.

icodec is aimed at the web platform and has some limitations:

  • Decode output & Encode input only support RGBA format.
  • No animated image support, you should use video instead.

Usage

Requirement: The target environment must support WebAssembly SIMD.

pnpm add icodec

Use in browser:

// All codec modules (see the table above) are named export.
import { avif, jxl } from "icodec";

const response = await fetch("https://raw.githubusercontent.com/Kaciras/icodec/master/test/snapshot/image.avif");
const data = new Uint8Array(await response.arrayBuffer());

// This should be called once before you invoke `decode()`
await avif.loadDecoder();

// Decode AVIF to ImageData.
const image = avif.decode(data);

// This should be called once before you invoke `encode()`
await jxl.loadEncoder();

// Encode the image to JPEG XL.
const jxlData = jxl.encode(image/*, { options }*/);

To use icodec in Node, just change the import specifier to icodec/node, and loadEncoder/loadDecoder will use readFileSync instead of fetch.

import { avif, jxl } from "icodec/node";

If your bundler requires special handing of WebAssembly, you can pass the URL of WASM files to load* function. WASM files are exported in the format icodec/<codec>-<enc|dec>.wasm.

icodec is tree-shakable, with a bundler the unused code and wasm files can be eliminated.

import { avif, jxl } from "icodec";

// Example for Vite
import AVIFEncWASM from "icodec/avif-enc.wasm?url";
import JxlDecWASM from "icodec/jxl-dec.wasm?url";

await avif.loadDecoder(AVIFEncWASM);
await jxl.loadEncoder(JxlDecWASM);

Type of each codec module:

/**
 * Provides a uniform type for codec modules that support encoding.
 *
 * @example
 * import { wp2, ICodecModule } from "icodec";
 *
 * const encoder: ICodecModule<wp2.Options> = wp2;
 */
interface ICodecModule<T = any> {
  /**
   * The default options of `encode` function.
   */
  defaultOptions: Required<T>;

  /**
   * The MIME type string of the format.
   */
  mimeType: string;

  /**
   * File extension (without the dot) of this format.
   */
  extension: string;

  /**
   * List of supported bit depth, from lower to higher.
   */
  bitDepth: number[];

  /**
   * Load the decoder WASM file, must be called once before decode.
   * Multiple calls are ignored, and return the first result.
   *
   * @param source If pass a string, it's the URL of WASM file to fetch,
   *               else it will be treated as the WASM bytes.
   * @return the underlying WASM module, which is not part of
   *               the public API and can be changed at any time.
   */
  loadDecoder(source?: WasmSource): Promise<any>;

  /**
   * Convert the image to raw RGBA data.
   */
  decode(input: Uint8Array): ImageData;

  /**
   * Load the encoder WASM file, must be called once before encode.
   * Multiple calls are ignored, and return the first result.
   *
   * @param source If pass a string, it's the URL of WASM file to fetch,
   *               else it will be treated as the WASM bytes.
   * @return the underlying WASM module, which is not part of
   *               the public API and can be changed at any time.
   */
  loadEncoder(source?: WasmSource): Promise<any>;

  /**
   * Encode an image with RGBA pixels data.
   */
  encode(image: ImageDataLike, options?: T): Uint8Array;
}

The png module exports extra members:

/**
 * Reduces the colors used in the image at a slight loss, using a combination
 * of vector quantization algorithms.
 *
 * Can be used before other compression algorithm to boost compression ratio.
 */
declare function reduceColors(image: ImageDataLike, options?: QuantizeOptions): Uint8Array;

High Bit-Depth

icodec supports high bit-depth images, for image with bit-depth > 8, the data should be 2-bytes per channel in Little-Endian (both encode input and decode result).

If you want to encode an image with bit-depth does not supported by the codec, you must scale it before.

In browser, decode result of the 8-bit image is an instance of ImageData, otherwise is not.

Performance

Decode & Encode test/snapshot/image.* files, 417px x 114px, 8-bit, time.SD is Standard Deviation of the time.

This benchmark ignores extra code size introduced by icodec, which in practice needs to be taken into account.

Decode on Edge browser.

No.Namecodectimetime.SD
0icodecavif2.30 ms19.02 us
12davif1.46 ms6.34 us
2WebGLavif2.78 ms12.80 us
3icodecheic2.55 ms9.82 us
4icodecjpeg719.84 us3.00 us
52djpeg584.23 us2.52 us
6WebGLjpeg1,674.88 us5.84 us
7icodecjxl3.51 ms30.08 us
8icodecpng336.74 us1.21 us
92dpng561.65 us2.14 us
10WebGLpng1,654.59 us18.81 us
11icodecqoi432.43 us1.44 us
12icodecwebp779.77 us2.38 us
132dwebp799.01 us1.48 us
14WebGLwebp1,952.10 us2.55 us
15icodecwp22.55 ms12.66 us

Decode on Node, vs Sharp.

No.Namecodectimetime.SD
0icodecavif2.01 ms3.24 us
1Sharpavif2.54 ms10.39 us
2icodecheic2.28 ms4.41 us
3icodecjpeg470.25 us2.96 us
4Sharpjpeg836.00 us1.24 us
5icodecjxl3.22 ms290.77 us
6icodecpng109.22 us883.82 ns
7Sharppng637.05 us1,947.88 ns
8icodecqoi191.46 us1.18 us
9icodecwebp548.78 us600.09 ns
10Sharpwebp1,700.14 us7,637.86 ns
11icodecwp22.28 ms2.86 us

Encode on Node, vs Sharp. Note that icodec and Sharp do not use the same code, so the output images are not exactly equal.

No.Namecodectimetime.SD
0icodecavif47.47 ms78.77 us
1Sharpavif52.77 ms78.89 us
2icodecjpeg7,664.33 us3.62 us
3Sharpjpeg802.02 us2.95 us
4icodecjxl32.05 ms37.34 us
5icodecpng70.11 ms132.71 us
6Sharppng10.88 ms69.48 us
7icodecqoi371.47 us666.00 ns
8icodecwebp4.42 ms3.17 us
9Sharpwebp4.04 ms13.09 us
10icodecwp290.14 ms295.49 us

Contribute

To build WASM modules, you will need to install:

build the project:

pnpm exec tsc
node scripts/build.js [--debug] [--rebuild] [--parallel=<int>] [--cmakeBuilder=<Ninja|...>]

Run tests:

node --test test/test-*.js

Start web demo:

node scripts/start-demo.js

TODOs:

  • Could it be possible to remove HEIC & VVIC encoder dependency on pthread, or limit the number of threads?
  • Cannot specify vvenc & vvdec paths for libheif build.

Keywords

FAQs

Package last updated on 10 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc