Code Monkey home page Code Monkey logo

libheif-js's Introduction

libheif-js

An Emscripten build of libheif distributed as an npm module for Node.JS and the browser.

github actions test jsdelivr npm-downloads npm-version

This module will respect the major and minor versions of the included libheif, with the patch version representing changes in this module itself. For the exact version of libheif, please see the install script.

Install

npm install libheif-js

Usage

Starting with version 1.17, there are multiple variants of libheif that you can use:

  • The default is still the classic pure-javascript implementation (for backwards compatibility, of course). You can still bundle this into your project with your bundler of choice.
    const libheif = require('libheif-js');
  • There is a wasm version available for use in NodeJS. This version will dymanically load the .wasm binary at runtime. While you may try to run this through a bundler, you are on your own for making it work.
    const libheif = require('libheif-js/wasm');
  • There is also a wasm version that is pre-bundled for you, which includes the .wasm binary inside the .js bundle. You will have a much easier time using this in your browser bundle project.
    const libheif = require('libheif-js/wasm-bundle');

If you'd like to include this module directly into an html page using a <script> tag, you have the following options:

Note: in the examples below, make sure to set the latest version when you use it. Always make sure to set a version, to make sure your website does not break unexpectedly when an update is released.

  • Use the pure-javascript implementation, exposing a libheif global:
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/libheif/libheif.js"></script>
  • Use the wasm bundle, exposing a libheif global:
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/libheif-wasm/libheif-bundle.js"></script>
  • Use the ES Module version, which now works in all major browsers and you should try it:
    <script type="module">
      import libheif from 'https://cdn.jsdelivr.net/npm/[email protected]/libheif-wasm/libheif-bundle.mjs';
    </script>

In all cases, you can use this sample code to decode an image:

const file = fs.readFileSync('./temp/0002.heic');

const decoder = new libheif.HeifDecoder();
const data = decoder.decode(file);
// data in an array holding all images inside the heic file

const image = data[0];
const width = image.get_width();
const height = image.get_height();

In NodeJS, you might use this decoded data with other libraries, such as pngjs:

const { PNG } = require('pngjs');

const imageData = await new Promise((resolve, reject) => {
  image.display({ data: new Uint8ClampedArray(width*height*4), width, height }, (displayData) => {
    if (!displayData) {
      return reject(new Error('HEIF processing error'));
    }

    resolve(displayData);
  });
});

const png = new PNG({ width: imageData.width, height: imageData.height });
png.data = imageData.data;

const pngBuffer = PNG.sync.write(png);

In the browser, you might use this decoded data with canvas to display or convert the image:

const canvas = document.createElement('canvas');

canvas.width = width;
canvas.height = height;

const context = canvas.getContext('2d');
const imageData = context.createImageData(width, height);

await new Promise((resolve, reject) => {
  image.display(imageData, (displayData) => {
    if (!displayData) {
      return reject(new Error('HEIF processing error'));
    }

    resolve();
  });
});

context.putImageData(imageData, 0, 0);

Related

This module contains the low-level libheif implementation. For more user-friendly functionality, check out these projects:

  • heic-cli - convert heic/heif images to jpeg or png from the command line
  • heic-convert - convert heic/heif images to jpeg and png
  • heic-decode - decode heic images to raw image data

libheif-js's People

Contributors

catdad avatar kaatt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

libheif-js's Issues

AVIF Support

According to https://github.com/strukturag/libheif, libheif now supports the AVIF file format. Since this repo (and it’s associated packages heic-cli, heic-convert, heic-decode) depend on libheif, did they automatically receive AVIF support too? Anyway, thank you for making these packages πŸ˜„

Not working on Android

Is there any way it can work on Android.
Throws exception: Could not parse HEIF file,Invalid input: No 'ftyp' box".

On Windows it works perfectly.

Thanks for your excellent work!

Error: libheif.HeifDecoder is not a constructor

I'm trying to run libheif.js directly into an HTML page, but I'm getting the following error: libheif.HeifDecoder is not a constructor. Any ideas on what the problem is?

index.html

<script type="module" src="image_parser.js"></script>
<script type="module"> 
   import libheif from 'https://cdn.jsdelivr.net/npm/[email protected]/libheif-wasm/libheif-bundle.mjs'; 
</script>

image_parser.js

import libheif from 'https://cdn.jsdelivr.net/npm/[email protected]/libheif-wasm/libheif-bundle.mjs';
const decoder = new libheif.HeifDecoder();

Additional Functionality

It looks like libheif-js does a great job decoding a HEIF image, but there are many additional functions listed in the libheif library. Are there plans to extend libheif-js to include more of those functions? Do these things transfer well into a web application? Or is this more of an experimental repository as suggested by the name "catdata-experiments"?

Please upgrade to 1.8.0

Hi!

Thank you for the wonderful work to push libheif to NPM. Can you compile a new version, 1.8.0 that should be tagged today? It contains some fixes that prevent converting some of ios made heics.

Thanks!

(Btw, if you can publish the container spec you use to do the emscripten build it would be really nice!)

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Hey, first of all, thanks for building and open sourcing this lib.

I'm trying to use it in my react application bundling it with Webpack and I'm getting this error. Do you know what can it be?

Compiled with problems:X

WARNING in ./node_modules/libheif-js/libheif-wasm/libheif-bundle.js 1:235-242

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted


WARNING in ./node_modules/libheif-js/libheif-wasm/libheif-bundle.js 1:364-371

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Module['ENVIRONMENT'] value is not valid

One of my team members is having trouble using this locally, but on my machine and a AWS Deployment of our Node application, it works fine.

The error being generated is the following:

\node_modules\libheif-js\libheif\libheif.js:1
var Module=typeof Module!=="undefined"?Module:{};((function(){var Module={print:(function(text){text=Array.prototype.slice.call(arguments).join(" ");console.log(text)}),printErr:(function(text){text=Array.prototype.slice.call(arguments).join(" ");console.error(text)}),canvas:{},noInitialRun:true};var moduleOverrides={};var key;for(key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=false;var ENVIRONMENT_IS_WORKER=false;var ENVIRONMENT_IS_NODE=false;var ENVIRONMENT_IS_SHELL=false;if(Module["ENVIRONMENT"]){if(Module["ENVIRONMENT"]==="WEB"){ENVIRONMENT_IS_WEB=true}else if(Module["ENVIRONMENT"]==="WORKER"){ENVIRONMENT_IS_WORKER=true}else if(Module["ENVIRONMENT"]==="NODE"){ENVIRONMENT_IS_NODE=true}else if(Module["ENVIRONMENT"]==="SHELL"){ENVIRONMENT_IS_SHELL=true}else{throw new Error("The provided Module['ENVIRONMENT'] value is not valid. It must be one of: WEB|W

Error: write EOF
at WriteWrap.afterWrite [as oncomplete] (net.js:788:14)

Any guidance on this would be appreciated, thanks for your time.

Run in browser vs server

I got libheif-js working great in a node.js server, but is it also possible to run it directly in the browser with an HTML <script src="..."> tag?

I'm fairly new to JavaScript and I thought this would be straightforward but I'm not able to figure that out.

libheif library can shutdowns apps

Hello!

This library is quite dangerous to use inside a project ;
It looks like the transpiled code can make a whole process shut down.
libheif1-1-12

How can the minified code contain uncaughtException,
...but not the source? I see traces of exitCode which is not as annoying

process.exitCode = 1;

Does the modification process has an issue?
Edit: ok I see the code fully comes from emscripten, and you may not have the capability to fix this easily (is there an option, or can it be postscripted?)

Not working with Vite

Hello,

I am using heic-decode, which underneath seems to be using libheif-js. I am currently using Vite.js for my project. However, upon importing the library, this error pops up in the console, preventing me from using the library:

The message on Firefox:

Uncaught TypeError: "stack" is read-only
    ensureErrnoError libheif.js:4153
    ensureErrnoError libheif.js:4151
    staticInit libheif.js:4157
    js libheif.js:7160
    js libheif.js:124105
    __require2 heic-decode.js:10
    js index.js:1
    __require2 heic-decode.js:10
    <anonymous> heic-decode:1

The message on Chrome:

Uncaught TypeError: Cannot assign to read only property 'stack' of object 'Error: No such file or directory'
    at libheif.js:4153:32
    at Array.forEach (<anonymous>)
    at Object.ensureErrnoError (libheif.js:4151:28)
    at Object.staticInit (libheif.js:4157:10)
    at Object.<anonymous> (libheif.js:7160:6)
    at node_modules/libheif-js/libheif/libheif.js (libheif.js:124105:3)
    at __require2 (heic-decode.js?v=6d7735c8:10:50)
    at node_modules/heic-decode/index.js (index.js:1:17)
    at __require2 (heic-decode.js?v=6d7735c8:10:50)
    at dep:heic-decode:1:16

Turning off minification, I was able to narrow down the error to this line in the built libheif-js file:

FS.genericErrors[code].stack = "<generic error, no stack>";

However, I am not sure how to fix this. I have been stuck on this issue for a while, so I would appreciate it if someone could take a look!

A reproducible repo with a very simple Vite project is available at https://github.com/Armster15/heic-decode-vite

Thank you for your time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.