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

@saschazar/wasm-avif

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

@saschazar/wasm-avif

Encodes and decodes AVIF images using WebAssembly

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27
decreased by-46%
Maintainers
1
Weekly downloads
 
Created
Source

npm

📦 WebAssembly AVIF decoder/encoder

A dependency-free AVIF decoder/encoder written in WebAssembly

It encodes raw RGB(A) pixels in a Uint8Array into AVIF-encoded image data and vice versa.

Installation

yarn add @saschazar/wasm-avif

or

npm install --save @saschazar/wasm-avif

Usage

It supports usage in the browser, in a Web Worker and of course Node.js.

// Node.js
import wasm_avif from '@saschazar/wasm-avif';
import defaultOptions from '@saschazar/wasm-avif/options';

// Web Worker - see: https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
importScripts('wasm_avif.js');

// -------- Browser/Web Worker/Node.js code below --------

// Decoding example:
// Load encoded AVIF image data in Uint8Array
const array = new Uint8Array(['some', 'encoded', 'AVIF', 'image', 'data']);
let result;

// Initialize the WebAssembly Module
const avifModule = wasm_avif({
  onRuntimeInitialized() {
    const alpha = true; // return RGBA buffer, instead of RGB
    result = avifModule.decode(array, array.length, alpha); // decode image data and return a new Uint8Array
    avifModule.free(); // clean up memory after encoding is done
  },
});

// Encoding example:
// Load raw RGB(A) pixels in Uint8Array
const array = new Uint8Array(['some', 'raw', 'RGB', 'image', 'data']);
const width = 800;
const height = 600;
let result;

// Initialize the WebAssembly Module
const avifModule = wasm_avif({
  onRuntimeInitialized() {
    const channels = 4; // 4 representing RGBA buffer in source array, 3 RGB
    const chroma = 3; // chroma subsampling: 1 for 4:4:4, 2 for 4:2:2, 3 for 4:2:0
    result = avifModule.encode(
      array,
      width,
      height,
      channels,
      defaultOptions,
      chroma
    ); // encode image data and return a new Uint8Array
    avifModule.free(); // clean up memory after encoding is done
  },
});

Example

A working example is available on RunKit.

Status

The main backbone of the project is the libavif library.

Both decoding and encoding is done via the AOM AV1 codec. Encoding is still very slow, since helpers such as multithreading and/or runtime CPU detection have to be disabled in order to successfully compile to WebAssembly.

Help needed

Concerning the encoding functionality, a few trials have been made towards using the more performant rav1e encoder, but it constantly fails during linking.

As soon as encoding is done via rav1e, decoding could be switched back to dav1d again.

Credits

This module uses the AOMediaCodec/libavif's source code and most of the code is written according to the examples provided in that repository.

License

Licensed under the MIT license.

Copyright ©️ 2020—2021 Sascha Zarhuber

Keywords

FAQs

Package last updated on 20 Oct 2021

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