Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to read HEIC file #4132

Closed
5 tasks done
dirkmc opened this issue Jun 18, 2024 · 15 comments
Closed
5 tasks done

Unable to read HEIC file #4132

dirkmc opened this issue Jun 18, 2024 · 15 comments

Comments

@dirkmc
Copy link

dirkmc commented Jun 18, 2024

Possible bug

  • Running npm install sharp completes without error.
  • Running node -e "require('sharp')" completes without error.
  • I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.
  System:
    OS: macOS 13.3.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 1.83 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.2 - ~/.nvm/versions/node/v18.20.2/bin/node
    Yarn: 1.22.21 - /usr/local/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v18.20.2/bin/npm
    pnpm: 9.0.6 - ~/.nvm/versions/node/v18.20.2/bin/pnpm
  npmPackages:
    sharp: ^0.33.4 => 0.33.4
  • Adding sharp.cache(false) does not fix this problem.
  • Using rotate() or keepExif() does not fix this problem.

What are the steps to reproduce?

I installed libheif and libvips with

brew install libheif
brew install libvips

Then I installed sharp, apparently succesfully. However I am still unable to convert a heic file to a jpg:

Error processing HEIC file: Error: /tmp/photobase/24fe87a508407a3ca5f19e81b7835f01.heic: bad seek to 2808991
heif: Unsupported feature: Unsupported codec (4.3000)
    at Sharp.toFile (/private/tmp/b/node_modules/sharp/lib/output.js:89:19)
    at Object.<anonymous> (/private/tmp/b/test.js:6:4)
    at Module._compile (node:internal/modules/cjs/loader:1364:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
    at Module.load (node:internal/modules/cjs/loader:1203:32)
    at Module._load (node:internal/modules/cjs/loader:1019:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
    at node:internal/main/run_main_module:28:49

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem

const sharp = require('sharp');
sharp.cache(false);
console.log(JSON.stringify(sharp.format.heif, null, 2));
sharp('input.heic')
  .toFormat('jpeg')
  .toFile('output.jpg', (err, info) => {
    if (err) {
      console.error('Error processing HEIC file:', err);
    } else {
      console.log('Successfully converted HEIC to JPG:', info);
    }
  });

Output of sharp.format.heif:

{
  "id": "heif",
  "input": {
    "file": true,
    "buffer": true,
    "stream": true,
    "fileSuffix": [
      ".heic",
      ".heif",
      ".avif"
    ]
  },
  "output": {
    "file": true,
    "buffer": true,
    "stream": true,
    "alias": [
      "avif",
      "heic"
    ]
  }
}

libheif and libvips versions:

brew info libheif
==> libheif: stable 1.17.6 (bottled)

brew info libvips
==> vips: stable 8.15.2 (bottled)

Please provide sample image(s) that help explain this problem

24fe87a508407a3ca5f19e81b7835f01.heic.zip

@lovell
Copy link
Owner

lovell commented Jun 18, 2024

Then I installed sharp, apparently succesfully

Please see https://sharp.pixelplumbing.com/install#building-from-source for the additional install-time requirements to build sharp from source against a globally-installed libvips.

@iamocean

This comment was marked as off-topic.

@iamocean
Copy link

iamocean commented Jun 19, 2024

npm install sharp --build-from-source --verbose --foreground-scripts

@lovell When I try use to npm install this way, I get the following error message.

npm verbose stack TypeError: Cannot set properties of null (setting 'peer')
npm verbose stack     at visit (/Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/calc-dep-flags.js:101:54)
npm verbose stack     at visitNode (/Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/treeverse/lib/depth-descent.js:58:25)
npm verbose stack     at next (/Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/treeverse/lib/depth-descent.js:44:19)
npm verbose stack     at depth (/Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/treeverse/lib/depth-descent.js:83:10)
npm verbose stack     at depth (/Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/treeverse/lib/depth.js:27:12)
npm verbose stack     at unsetFlag (/Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/calc-dep-flags.js:96:5)
npm verbose stack     at /Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/calc-dep-flags.js:63:7
npm verbose stack     at CIMap.forEach (<anonymous>)
npm verbose stack     at calcDepFlagsStep (/Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/calc-dep-flags.js:41:17)
npm verbose stack     at visit (/Users/username/.nvm/versions/node/v20.14.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/calc-dep-flags.js:12:20)
npm verbose cwd /Users/username/Workspace/github/heic-demo
npm verbose Darwin 23.5.0
npm verbose node v20.14.0
npm verbose npm  v10.7.0
npm error Cannot set properties of null (setting 'peer')
npm verbose exit 1
npm verbose code 1

@dirkmc
Copy link
Author

dirkmc commented Jun 19, 2024

Thank you both for your comments.

I tried following the instructions from @iamocean but it resulted in the same error when I run the test script:

Error processing HEIC file: Error: /tmp/photobase/24fe87a508407a3ca5f19e81b7835f01.heic: bad seek to 2808991
heif: Unsupported feature: Unsupported codec (4.3000)

@lovell thanks for the suggestion.
I ran these commands but it seems there's a compilation issue:

npm install --save node-addon-api node-gyp
SHARP_FORCE_GLOBAL_LIBVIPS=1 npm install sharp

npm ERR! In file included from ../common.cc:13:
npm ERR! /usr/local/include/vips/vips8:35:10: fatal error: 'glib-object.h' file not found
npm ERR! #include <glib-object.h>
npm ERR!          ^~~~~~~~~~~~~~~
npm ERR! 1 error generated.
npm ERR! make: *** [Release/obj.target/sharp-darwin-arm64/common.o] Error 1

@dirkmc
Copy link
Author

dirkmc commented Jun 19, 2024

@lovell I had assumed that this output means that it was able to find the heif encoder. Is that incorrect?

console.log(JSON.stringify(sharp.format.heif, null, 2));

{
  "id": "heif",
  "input": {
    "file": true,
    "buffer": true,
    "stream": true,
    "fileSuffix": [
      ".heic",
      ".heif",
      ".avif"
    ]
  },
  "output": {
    "file": true,
    "buffer": true,
    "stream": true,
    "alias": [
      "avif",
      "heic"
    ]
  }
}

@lovell
Copy link
Owner

lovell commented Jun 19, 2024

@iamocean That looks like an npm problem unrelated to sharp.

@dirkmc HEIF is the container, HEVC is the codec, HEIC is the combination of these. I've added commit ae06f46 to make the output of sharp.format.heif more accurate when using prebuilt binaries.

Please can you provide the complete output of running npm install sharp --foreground-scripts without setting SHARP_FORCE_GLOBAL_LIBVIPS.

@dirkmc
Copy link
Author

dirkmc commented Jun 19, 2024

@lovell thanks for clarifying 👍

Here is the output with --foreground-scripts:

$ npm install sharp --foreground-scripts
(###############⠂⠂⠂) ⠋ reify:@img/sharp-darwin-x64: timing reifyNode:node_modules/@img/sharp-libvips-darwin-x64 Complet
> [email protected] install
> node install/check

sharp: Detected globally-installed libvips v8.15.2rwin-arm64: timing reifyNode:node_modules/sharp Completed in 82ms
sharp: Attempting to build from source via node-gyp
sharp: Found node-addon-api 
sharp: Found node-gyp version 10.1.0arp-libvips-darwin-arm64: timing reifyNode:node_modules/sharp Completed in 82ms
sharp: See https://sharp.pixelplumbing.com/install#building-from-source
gyp info it worked if it ends with okrp-libvips-darwin-arm64: timing reifyNode:node_modules/sharp Completed in 82ms
gyp info using [email protected]
gyp info using [email protected] | darwin | arm64
gyp info chdir src
gyp info find Python using Python version 3.12.4 found at "/usr/local/opt/[email protected]/bin/python3.12"pleted in 82ms
(##################) ⠏ reify:@img/sharp-libvips-darwin-arm64: timing reifyNode:node_modules/sharp Completed in 82ms
gyp info spawn /usr/local/opt/[email protected]/bin/python3.12
gyp info spawn args [
gyp info spawn args '/private/tmp/b/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
gyp info spawn args '-I',
gyp info spawn args '/private/tmp/b/node_modules/sharp/src/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/private/tmp/b/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/Users/dirk/Library/Caches/node-gyp/18.20.2/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/Users/dirk/Library/Caches/node-gyp/18.20.2',
gyp info spawn args '-Dnode_gyp_dir=/private/tmp/b/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=/Users/dirk/Library/Caches/node-gyp/18.20.2/<(target_arch)/node.lib',
gyp info spawn args '-Dmodule_root_dir=/private/tmp/b/node_modules/sharp/src',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'build',
gyp info spawn args '-Goutput_dir=.'
gyp info spawn args ]
<string>:114: SyntaxWarning: invalid escape sequence '\/'m64: timing reifyNode:node_modules/sharp Completed in 82ms
gyp info spawn make) ⠏ reify:@img/sharp-libvips-darwin-arm64: timing reifyNode:node_modules/sharp Completed in 82ms
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  CC(target) Release/obj.target/nothing/../../node-addon-api/nothing.oeifyNode:node_modules/sharp Completed in 82ms
  LIBTOOL-STATIC Release/nothing.asharp-libvips-darwin-arm64: timing reifyNode:node_modules/sharp Completed in 82ms
warning: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: archive library: Release/nothing.a the table of contents is empty (no object file members in the library define global symbols)
  TOUCH Release/obj.target/libvips-cpp.stamp
  CXX(target) Release/obj.target/sharp-darwin-arm64/common.o
  CXX(target) Release/obj.target/sharp-darwin-arm64/metadata.otiming reifyNode:node_modules/sharp Completed in 82ms
  CXX(target) Release/obj.target/sharp-darwin-arm64/stats.o4: timing reifyNode:node_modules/sharp Completed in 82ms
  CXX(target) Release/obj.target/sharp-darwin-arm64/operations.oming reifyNode:node_modules/sharp Completed in 82ms
  CXX(target) Release/obj.target/sharp-darwin-arm64/pipeline.otiming reifyNode:node_modules/sharp Completed in 82ms
  CXX(target) Release/obj.target/sharp-darwin-arm64/utilities.oiming reifyNode:node_modules/sharp Completed in 82ms
  CXX(target) Release/obj.target/sharp-darwin-arm64/sharp.o4: timing reifyNode:node_modules/sharp Completed in 82ms
  SOLINK_MODULE(target) Release/sharp-darwin-arm64.nodearm64: timing reifyNode:node_modules/sharp Completed in 82ms
ld: warning: ignoring file /usr/local/Cellar/vips/8.15.2_1/lib/libvips.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /usr/local/Cellar/glib/2.80.3/lib/libglib-2.0.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /usr/local/opt/gettext/lib/libintl.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /usr/local/Cellar/glib/2.80.3/lib/libgio-2.0.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /usr/local/Cellar/vips/8.15.2_1/lib/libvips-cpp.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /usr/local/Cellar/glib/2.80.3/lib/libgobject-2.0.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
  TOUCH Release/obj.target/copy-dll.stampibvips-darwin-arm64: timing reifyNode:node_modules/sharp Completed in 82ms
gyp info ok 

@lovell
Copy link
Owner

lovell commented Jun 19, 2024

ld: warning: ignoring file /usr/local/Cellar/vips/8.15.2_1/lib/libvips.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64

It looks like you're trying to use an Intel-based Homebrew environment with an ARM64-based Node.js executable, which won't work. Perhaps you're running Homebrew under Rosetta emulation? If you want to do this, you'll also need to run Node.js under Rosetta emulation.

@dirkmc
Copy link
Author

dirkmc commented Jun 19, 2024

Well spotted @lovell, thanks 👍

I uninstalled intel-based Homebrew and installed ARM64-based Homebrew.
Now there is no error when running the install with --foreground-scripts:

$ brew install libheif
$ brew install libvips
$ npm install --save node-addon-api node-gyp
$ npm install sharp --foreground-scripts
(###############⠂⠂⠂) ⠋ reify:@img/sharp-darwin-x64: timing reifyNode:node_modules/@img/sharp-libvips-darwin-x64 Complete
> [email protected] install
> node install/check


added 8 packages, and audited 112 packages in 3s

However when I run my test file I still get the error message:

$ node test.js 
Error processing HEIC file: Error: /tmp/photobase/24fe87a508407a3ca5f19e81b7835f01.heic: bad seek to 2808991
heif: Unsupported feature: Unsupported codec (4.3000)

@lovell
Copy link
Owner

lovell commented Jun 19, 2024

Please can you provide the complete output of running npm uninstall sharp && npm install sharp --foreground-scripts

@dirkmc
Copy link
Author

dirkmc commented Jun 19, 2024

Sure, here it is:

$ npm uninstall sharp && npm install sharp --foreground-scripts

removed 8 packages, and audited 104 packages in 520ms

14 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
(###############⠂⠂⠂) ⠋ reify:@img/sharp-darwin-x64: timing reifyNode:node_modules/@img/sharp-libvips-darwin-x64 Complete
> [email protected] install
> node install/check


added 8 packages, and audited 112 packages in 3s

17 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

@lovell
Copy link
Owner

lovell commented Jun 19, 2024

Thanks, this suggests sharp didn't find a globally-installed libvips. My best guess would be some kind of Homebrew-related configuration or architecture mismatch problem. Make sure the paths in brew environment --plain are pointing to the right place, especially anything related to pkg-config e.g. PKG_CONFIG_LIBDIR.

@dirkmc
Copy link
Author

dirkmc commented Jun 19, 2024

Yes, you're right it was an environment problem - the ARM64-based Homebrew paths weren't set up correctly. Once I fixed the paths I was able to run the test script and successfully open a .heic file.
I know this is well outside the scope of the sharp module so thanks very much for taking the time to help out.

@dirkmc dirkmc closed this as completed Jun 19, 2024
@iamocean
Copy link

Sure, here it is:就是这儿:

$ npm uninstall sharp && npm install sharp --foreground-scripts

removed 8 packages, and audited 104 packages in 520ms

14 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
(###############⠂⠂⠂) ⠋ reify:@img/sharp-darwin-x64: timing reifyNode:node_modules/@img/sharp-libvips-darwin-x64 Complete
> [email protected] install
> node install/check


added 8 packages, and audited 112 packages in 3s

17 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

@lovell I have a question, why do I need to install libheif and libvips separately? Can't you handle .heic format images without them?

After running it, I get the error message

 ⨯ Error: source: bad seek to 685403
heif: Unsupported feature: Unsupported codec (4.3000)

@lovell
Copy link
Owner

lovell commented Jun 19, 2024

@iamocean Whilst HEVC remains patent-encumbered, the prebuilt binaries provided by sharp will not support HEIC (HEIF+HEVC) images.

https://github.com/search?q=repo%3Alovell%2Fsharp+hevc+patent&type=issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants