Compare commits
92 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
956f7e29db | ||
|
|
4264c0577e | ||
|
|
cc37b59309 | ||
|
|
9f2f92095d | ||
|
|
0c1075c089 | ||
|
|
9c64710c8b | ||
|
|
f6f16b91db | ||
|
|
1986b5cfe6 | ||
|
|
6445b72d41 | ||
|
|
df7b8ba738 | ||
|
|
202083999e | ||
|
|
315f519e1d | ||
|
|
d7d580ae6f | ||
|
|
7017af303d | ||
|
|
0dc325daa4 | ||
|
|
6dffb47973 | ||
|
|
b19dad69d6 | ||
|
|
94c5ac64e9 | ||
|
|
c4bcd088fb | ||
|
|
aeecbe9396 | ||
|
|
171aade9cd | ||
|
|
67213ae86c | ||
|
|
24d9e53c3f | ||
|
|
573ed5f4b5 | ||
|
|
ceff628add | ||
|
|
0bb8cb9203 | ||
|
|
98349bde28 | ||
|
|
f09be932eb | ||
|
|
4c57ac2bbe | ||
|
|
1dd93c1b6b | ||
|
|
c9f85fe27f | ||
|
|
419cbe50f6 | ||
|
|
5031c8323f | ||
|
|
762d5913ce | ||
|
|
290df1b1c7 | ||
|
|
79170afc51 | ||
|
|
bba00c2bfe | ||
|
|
f7e2b3688f | ||
|
|
8d49b7dde1 | ||
|
|
138e60adb3 | ||
|
|
d6376c31e0 | ||
|
|
a7003e93c8 | ||
|
|
4821a11223 | ||
|
|
bf1b326988 | ||
|
|
39ddb6a175 | ||
|
|
b2a0b8c0f0 | ||
|
|
4debc46d0e | ||
|
|
f4e259d10f | ||
|
|
774d78228e | ||
|
|
0e62bde5c3 | ||
|
|
2bbd9b23e6 | ||
|
|
02676140e8 | ||
|
|
182beaa4a1 | ||
|
|
7c08a09529 | ||
|
|
ef964b5472 | ||
|
|
ee54ce9913 | ||
|
|
e59e146887 | ||
|
|
103ec0d58f | ||
|
|
a0d89ed514 | ||
|
|
c10888e6fe | ||
|
|
93455f8eb5 | ||
|
|
65acd96c8d | ||
|
|
fabe720b9b | ||
|
|
53dd313e97 | ||
|
|
2678d7a660 | ||
|
|
46718102c6 | ||
|
|
0f473fe3b1 | ||
|
|
2872602c9e | ||
|
|
ab653cae33 | ||
|
|
e6a035e575 | ||
|
|
fbe48d75dd | ||
|
|
20ba0f49dd | ||
|
|
c213e9878d | ||
|
|
9704ca4c18 | ||
|
|
49dce6219e | ||
|
|
260ff6c94f | ||
|
|
3ec281d104 | ||
|
|
c4c43d525b | ||
|
|
6c5cde363a | ||
|
|
d46b4d950f | ||
|
|
b369c4bb8a | ||
|
|
c3898487c4 | ||
|
|
ca3c5b400f | ||
|
|
97772b176c | ||
|
|
08a2965f1c | ||
|
|
76dcddfa3d | ||
|
|
79f476ae4d | ||
|
|
d406cb619c | ||
|
|
4f3890f1e4 | ||
|
|
7a9d58cc51 | ||
|
|
eef87da0e1 | ||
|
|
00e65f6f14 |
@@ -2,6 +2,7 @@ freebsd_instance:
|
||||
image_family: freebsd-13-0-snap
|
||||
|
||||
task:
|
||||
name: FreeBSD 13.0
|
||||
env:
|
||||
IGNORE_OSVERSION: yes
|
||||
prerequisites_script:
|
||||
@@ -9,6 +10,6 @@ task:
|
||||
- pkg upgrade -y
|
||||
- pkg install -y pkgconf vips node npm
|
||||
install_script:
|
||||
- npm install --unsafe-perm
|
||||
- npm install --build-from-source --unsafe-perm
|
||||
test_script:
|
||||
- npm test
|
||||
|
||||
6
.github/CONTRIBUTING.md
vendored
@@ -27,7 +27,7 @@ Please select the `master` branch as the destination for your Pull Request so yo
|
||||
|
||||
Please squash your changes into a single commit using a command like `git rebase -i upstream/master`.
|
||||
|
||||
To test C++ changes, you can compile the module using `npm install` and then run the tests using `npm test`.
|
||||
To test C++ changes, you can compile the module using `npm install --build-from-source` and then run the tests using `npm test`.
|
||||
|
||||
## Submit a Pull Request with a new feature
|
||||
|
||||
@@ -42,10 +42,6 @@ You deserve to add your details to the [list of contributors](https://github.com
|
||||
|
||||
Any change that modifies the existing public API should be added to the relevant work-in-progress branch for inclusion in the next major release.
|
||||
|
||||
| Release | WIP branch |
|
||||
| ------: | :--------- |
|
||||
| v0.26.0 | zoom |
|
||||
|
||||
Please squash your changes into a single commit using a command like `git rebase -i upstream/<wip-branch>`.
|
||||
|
||||
### Add a new public method
|
||||
|
||||
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: Documentation
|
||||
url: https://sharp.pixelplumbing.com/
|
||||
about: Installation instructions, complete API documentation with examples, changelog
|
||||
6
.github/ISSUE_TEMPLATE/installation.md
vendored
@@ -7,11 +7,13 @@ labels: installation
|
||||
|
||||
Did you see the [documentation relating to installation](https://sharp.pixelplumbing.com/install)?
|
||||
|
||||
Have you ensured the platform and version of Node.js used for `npm install` is the same as the platform and version of Node.js used at runtime?
|
||||
Have you ensured the architecture and platform of Node.js used for `npm install` is the same as the architecture and platform of Node.js used at runtime?
|
||||
|
||||
Are you using the latest version? Is the version currently in use as reported by `npm ls sharp` the same as the latest version as reported by `npm view sharp dist-tags.latest`?
|
||||
|
||||
If you are installing as a `root` or `sudo` user, have you tried with the `npm install --unsafe-perm` flag?
|
||||
If you are using npm v6 or earlier and installing as a `root` or `sudo` user, have you tried with the `npm install --unsafe-perm` flag?
|
||||
|
||||
If you are using npm v7, does the user running `npm install` own the directory it is run in?
|
||||
|
||||
If you are using the `ignore-scripts` feature of `npm`, have you tried with the `npm install --ignore-scripts=false` flag?
|
||||
|
||||
|
||||
85
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
on:
|
||||
- push
|
||||
- pull_request
|
||||
jobs:
|
||||
CI:
|
||||
runs-on: ${{ matrix.os }}
|
||||
container: ${{ matrix.container }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- os: ubuntu-20.04
|
||||
container: centos:7
|
||||
nodejs_version: 10
|
||||
coverage: true
|
||||
prebuild: true
|
||||
- os: ubuntu-20.04
|
||||
container: centos:7
|
||||
nodejs_version: 12
|
||||
- os: ubuntu-20.04
|
||||
container: centos:7
|
||||
nodejs_version: 14
|
||||
- os: ubuntu-20.04
|
||||
container: centos:7
|
||||
nodejs_version: 15
|
||||
- os: ubuntu-20.04
|
||||
container: node:10-alpine3.11
|
||||
prebuild: true
|
||||
- os: ubuntu-20.04
|
||||
container: node:12-alpine3.11
|
||||
- os: ubuntu-20.04
|
||||
container: node:14-alpine3.11
|
||||
- os: ubuntu-20.04
|
||||
container: node:15-alpine3.11
|
||||
- os: macos-10.15
|
||||
nodejs_version: 10
|
||||
prebuild: true
|
||||
- os: macos-10.15
|
||||
nodejs_version: 12
|
||||
- os: macos-10.15
|
||||
nodejs_version: 14
|
||||
- os: macos-10.15
|
||||
nodejs_version: 15
|
||||
- os: windows-2019
|
||||
nodejs_version: 10
|
||||
prebuild: true
|
||||
- os: windows-2019
|
||||
nodejs_version: 12
|
||||
- os: windows-2019
|
||||
nodejs_version: 14
|
||||
- os: windows-2019
|
||||
nodejs_version: 15
|
||||
steps:
|
||||
- name: Dependencies (Linux glibc)
|
||||
if: contains(matrix.container, 'centos')
|
||||
run: |
|
||||
curl -sL https://rpm.nodesource.com/setup_${{ matrix.nodejs_version }}.x | bash -
|
||||
yum install -y gcc-c++ make git python3 nodejs
|
||||
- name: Dependencies (Linux musl)
|
||||
if: contains(matrix.container, 'alpine')
|
||||
run: apk add build-base git python3 --update-cache
|
||||
- name: Dependencies (macOS, Windows)
|
||||
if: contains(matrix.os, 'macos') || contains(matrix.os, 'windows')
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.nodejs_version }}
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: Fix working directory ownership
|
||||
if: matrix.container
|
||||
run: chown root.root .
|
||||
- name: Install
|
||||
run: npm install --build-from-source --unsafe-perm
|
||||
- name: Test
|
||||
run: npm test
|
||||
- name: Coverage
|
||||
if: matrix.coverage
|
||||
uses: coverallsapp/github-action@v1.1.2
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Prebuild
|
||||
if: matrix.prebuild && startsWith(github.ref, 'refs/tags/')
|
||||
env:
|
||||
prebuild_upload: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: npx prebuild --runtime napi --target 3
|
||||
127
.travis.yml
@@ -1,81 +1,19 @@
|
||||
jobs:
|
||||
include:
|
||||
- name: "Linux x64 (CentOS 7, glibc 2.17) - Node.js 10"
|
||||
os: linux
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --env TRAVIS_TAG --env prebuild_upload --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp centos:7
|
||||
- sudo docker exec sharp bash -c "curl -sL https://rpm.nodesource.com/setup_10.x | bash -"
|
||||
- sudo docker exec sharp yum install -y gcc-c++ make git nodejs
|
||||
install: sudo docker exec sharp bash -c "npm install --unsafe-perm"
|
||||
script: sudo docker exec sharp bash -c "npm test"
|
||||
|
||||
- name: "Linux x64 (CentOS 7, glibc 2.17) - Node.js 12"
|
||||
os: linux
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp centos:7
|
||||
- sudo docker exec sharp bash -c "curl -sL https://rpm.nodesource.com/setup_12.x | bash -"
|
||||
- sudo docker exec sharp yum install -y gcc-c++ make git nodejs
|
||||
install: sudo docker exec sharp bash -c "npm install --unsafe-perm"
|
||||
script: sudo docker exec sharp bash -c "npm test"
|
||||
|
||||
- name: "Linux x64 (CentOS 7, glibc 2.17) - Node.js 14"
|
||||
os: linux
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp centos:7
|
||||
- sudo docker exec sharp bash -c "curl -sL https://rpm.nodesource.com/setup_14.x | bash -"
|
||||
- sudo docker exec sharp yum install -y gcc-c++ make git nodejs
|
||||
install: sudo docker exec sharp bash -c "npm install --unsafe-perm"
|
||||
script: sudo docker exec sharp bash -c "npm test"
|
||||
|
||||
- name: "Linux x64 (Alpine 3.9, musl 1.1.20) - Node.js 10"
|
||||
os: linux
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --env TRAVIS_TAG --env prebuild_upload --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:10.17.0-alpine3.9 # https://github.com/nodejs/docker-node/issues/1158
|
||||
- sudo docker exec sharp apk add build-base git python2 --update-cache
|
||||
install: sudo docker exec sharp sh -c "npm install --unsafe-perm"
|
||||
script: sudo docker exec sharp sh -c "npm test"
|
||||
|
||||
- name: "Linux x64 (Alpine 3.11, musl 1.1.20) - Node.js 12"
|
||||
os: linux
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:12.0-alpine
|
||||
- sudo docker exec sharp apk add build-base git python2 --update-cache
|
||||
install: sudo docker exec sharp sh -c "npm install --unsafe-perm"
|
||||
script: sudo docker exec sharp sh -c "npm test"
|
||||
|
||||
- name: "Linux x64 (Alpine 3.11, musl 1.1.20) - Node.js 14"
|
||||
os: linux
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:14.0-alpine
|
||||
- sudo docker exec sharp apk add build-base git python2 --update-cache
|
||||
install: sudo docker exec sharp sh -c "npm install --unsafe-perm"
|
||||
script: sudo docker exec sharp sh -c "npm test"
|
||||
|
||||
- name: "Linux ARM64v8 (Debian 11, glibc 2.29) - Node.js 10"
|
||||
arch: arm64
|
||||
os: linux
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --env TRAVIS_TAG --env prebuild_upload --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python2 curl"
|
||||
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python3 curl"
|
||||
- sudo docker exec sharp sh -c "curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -"
|
||||
- sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_10.x buster main' >/etc/apt/sources.list.d/nodesource.list"
|
||||
- sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_10.x sid main' >/etc/apt/sources.list.d/nodesource.list"
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y nodejs=10.*"
|
||||
install: sudo docker exec sharp sh -c "npm install --unsafe-perm"
|
||||
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
|
||||
script: sudo docker exec sharp sh -c "npm test"
|
||||
after_success: "[[ -n $TRAVIS_TAG ]] && sudo docker exec --env prebuild_upload sharp sh -c \"npx prebuild --runtime napi --target 3\""
|
||||
|
||||
- name: "Linux ARM64v8 (Debian 11, glibc 2.29) - Node.js 12"
|
||||
arch: arm64
|
||||
@@ -83,12 +21,12 @@ jobs:
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python2 curl"
|
||||
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python3 curl"
|
||||
- sudo docker exec sharp sh -c "curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -"
|
||||
- sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_12.x buster main' >/etc/apt/sources.list.d/nodesource.list"
|
||||
- sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_12.x sid main' >/etc/apt/sources.list.d/nodesource.list"
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y nodejs"
|
||||
install: sudo docker exec sharp sh -c "npm install --unsafe-perm"
|
||||
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
|
||||
script: sudo docker exec sharp sh -c "npm test"
|
||||
|
||||
- name: "Linux ARM64v8 (Debian 11, glibc 2.29) - Node.js 14"
|
||||
@@ -97,43 +35,28 @@ jobs:
|
||||
dist: bionic
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo docker run -dit --name sharp --env CI --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python2 curl"
|
||||
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python3 curl"
|
||||
- sudo docker exec sharp sh -c "curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -"
|
||||
- sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_14.x buster main' >/etc/apt/sources.list.d/nodesource.list"
|
||||
- sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_14.x sid main' >/etc/apt/sources.list.d/nodesource.list"
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y nodejs"
|
||||
install: sudo docker exec sharp sh -c "npm install --unsafe-perm"
|
||||
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
|
||||
script: sudo docker exec sharp sh -c "npm test"
|
||||
|
||||
- name: "macOS (10.13) - Node.js 10"
|
||||
os: osx
|
||||
osx_image: xcode10.1
|
||||
language: node_js
|
||||
node_js: "10"
|
||||
|
||||
- name: "macOS (10.13) - Node.js 12"
|
||||
os: osx
|
||||
osx_image: xcode10.1
|
||||
language: node_js
|
||||
node_js: "12"
|
||||
before_install: unset prebuild_upload
|
||||
|
||||
- name: "macOS (10.13) - Node.js 14"
|
||||
os: osx
|
||||
osx_image: xcode10.1
|
||||
language: node_js
|
||||
node_js: "14"
|
||||
before_install: unset prebuild_upload
|
||||
|
||||
- name: "Unit test coverage report"
|
||||
- name: "Linux ARM64v8 (Debian 11, glibc 2.29) - Node.js 15"
|
||||
arch: arm64
|
||||
os: linux
|
||||
dist: bionic
|
||||
language: node_js
|
||||
node_js: "13"
|
||||
before_install: unset prebuild_upload
|
||||
after_success:
|
||||
- npm install coveralls
|
||||
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
|
||||
language: shell
|
||||
before_install:
|
||||
- sudo chown 0.0 ${PWD}
|
||||
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python3 curl"
|
||||
- sudo docker exec sharp sh -c "curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -"
|
||||
- sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_15.x sid main' >/etc/apt/sources.list.d/nodesource.list"
|
||||
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y nodejs"
|
||||
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
|
||||
script: sudo docker exec sharp sh -c "npm test"
|
||||
|
||||
cache:
|
||||
npm: false
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
The typical use case for this high speed Node.js module
|
||||
is to convert large images in common formats to
|
||||
smaller, web-friendly JPEG, PNG and WebP images of varying dimensions.
|
||||
smaller, web-friendly JPEG, PNG, WebP and AVIF images of varying dimensions.
|
||||
|
||||
Resizing an image is typically 4x-5x faster than using the
|
||||
quickest ImageMagick and GraphicsMagick settings
|
||||
@@ -16,7 +16,7 @@ Lanczos resampling ensures quality is not sacrificed for speed.
|
||||
As well as image resizing, operations such as
|
||||
rotation, extraction, compositing and gamma correction are available.
|
||||
|
||||
Most modern macOS, Windows and Linux systems running Node.js v10.16.0+
|
||||
Most modern macOS, Windows and Linux systems running Node.js v10+
|
||||
do not require any additional install or runtime dependencies.
|
||||
|
||||
## Examples
|
||||
@@ -102,7 +102,7 @@ covers reporting bugs, requesting features and submitting code changes.
|
||||
|
||||
### Licensing
|
||||
|
||||
Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Lovell Fuller and contributors.
|
||||
Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Lovell Fuller and contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
24
appveyor.yml
@@ -1,26 +1,18 @@
|
||||
os: Visual Studio 2017
|
||||
os: Visual Studio 2019
|
||||
version: "{build}"
|
||||
build: off
|
||||
platform: x86
|
||||
environment:
|
||||
matrix:
|
||||
- nodejs_version: "10"
|
||||
platform: x86
|
||||
- nodejs_version: "10"
|
||||
platform: x64
|
||||
prebuild: true
|
||||
- nodejs_version: "12"
|
||||
platform: x86
|
||||
prebuild_upload: ""
|
||||
- nodejs_version: "12"
|
||||
platform: x64
|
||||
prebuild_upload: ""
|
||||
- nodejs_version: "14.2.0"
|
||||
platform: x86
|
||||
prebuild_upload: ""
|
||||
- nodejs_version: "14"
|
||||
platform: x64
|
||||
prebuild_upload: ""
|
||||
- nodejs_version: "15"
|
||||
install:
|
||||
- ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version) $env:platform
|
||||
- npm install
|
||||
- ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version)
|
||||
- npm install --build-from-source
|
||||
test_script:
|
||||
- npm test
|
||||
on_success:
|
||||
- if [%prebuild%] == [true] if [%APPVEYOR_REPO_TAG%] == [true] npx prebuild --runtime napi --target 3
|
||||
|
||||
@@ -95,7 +95,7 @@
|
||||
'src/sharp.cc'
|
||||
],
|
||||
'include_dirs': [
|
||||
'<!@(node -p "require(\'node-addon-api\').include")',
|
||||
'<!(node -p "require(\'node-addon-api\').include_dir")',
|
||||
],
|
||||
'conditions': [
|
||||
['use_global_libvips == "true"', {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
The typical use case for this high speed Node.js module
|
||||
is to convert large images in common formats to
|
||||
smaller, web-friendly JPEG, PNG and WebP images of varying dimensions.
|
||||
smaller, web-friendly JPEG, PNG, AVIF and WebP images of varying dimensions.
|
||||
|
||||
Resizing an image is typically 4x-5x faster than using the
|
||||
quickest ImageMagick and GraphicsMagick settings
|
||||
@@ -16,14 +16,14 @@ Lanczos resampling ensures quality is not sacrificed for speed.
|
||||
As well as image resizing, operations such as
|
||||
rotation, extraction, compositing and gamma correction are available.
|
||||
|
||||
Most modern macOS, Windows and Linux systems running Node.js v10.16.0+
|
||||
Most modern macOS, Windows and Linux systems running Node.js v10+
|
||||
do not require any additional install or runtime dependencies.
|
||||
|
||||
### Formats
|
||||
|
||||
This module supports reading JPEG, PNG, WebP, TIFF, GIF and SVG images.
|
||||
This module supports reading JPEG, PNG, WebP, AVIF, TIFF, GIF and SVG images.
|
||||
|
||||
Output images can be in JPEG, PNG, WebP and TIFF formats as well as uncompressed raw pixel data.
|
||||
Output images can be in JPEG, PNG, WebP, AVIF and TIFF formats as well as uncompressed raw pixel data.
|
||||
|
||||
Streams, Buffer objects and the filesystem can be used for input and output.
|
||||
|
||||
@@ -66,7 +66,7 @@ covers reporting bugs, requesting features and submitting code changes.
|
||||
|
||||
### Licensing
|
||||
|
||||
Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Lovell Fuller and contributors.
|
||||
Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Lovell Fuller and contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -70,7 +70,7 @@ Channel ordering follows vips convention:
|
||||
- sRGB: 0: Red, 1: Green, 2: Blue, 3: Alpha.
|
||||
- CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha.
|
||||
|
||||
Buffers may be any of the image formats supported by sharp: JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data.
|
||||
Buffers may be any of the image formats supported by sharp.
|
||||
For raw pixel input, the `options` object should contain a `raw` attribute, which follows the format of the attribute of the same name in the `sharp()` constructor.
|
||||
|
||||
### Parameters
|
||||
|
||||
@@ -48,6 +48,14 @@ By default output image will be web-friendly sRGB, with additional channels inte
|
||||
|
||||
- `colourspace` **[string][1]?** output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...][6]
|
||||
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
// Output 16 bits per pixel RGB
|
||||
await sharp(input)
|
||||
.toColourspace('rgb16')
|
||||
.toFile('16-bpp.png')
|
||||
```
|
||||
|
||||
- Throws **[Error][4]** Invalid parameters
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
Constructor factory to create an instance of `sharp`, to which further methods are chained.
|
||||
|
||||
JPEG, PNG, WebP or TIFF format image data can be streamed out from this object.
|
||||
JPEG, PNG, WebP, AVIF or TIFF format image data can be streamed out from this object.
|
||||
When using Stream based output, derived attributes are available from the `info` event.
|
||||
|
||||
Non-critical problems encountered during processing are emitted as `warning` events.
|
||||
@@ -13,32 +13,36 @@ Implements the [stream.Duplex][1] class.
|
||||
|
||||
### Parameters
|
||||
|
||||
- `input` **([Buffer][2] \| [string][3])?** if present, can be
|
||||
a Buffer containing JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data, or
|
||||
a String containing the filesystem path to an JPEG, PNG, WebP, GIF, SVG or TIFF image file.
|
||||
JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
|
||||
- `options` **[Object][4]?** if present, is an Object with optional attributes.
|
||||
- `options.failOnError` **[boolean][5]** by default halt processing and raise an error when loading invalid images.
|
||||
- `input` **([Buffer][2] \| [Uint8Array][3] \| [Uint8ClampedArray][4] \| [string][5])?** if present, can be
|
||||
a Buffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data, or
|
||||
a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file.
|
||||
JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
|
||||
- `options` **[Object][6]?** if present, is an Object with optional attributes.
|
||||
- `options.failOnError` **[boolean][7]** by default halt processing and raise an error when loading invalid images.
|
||||
Set this flag to `false` if you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid. (optional, default `true`)
|
||||
- `options.limitInputPixels` **([number][6] \| [boolean][5])** Do not process input images where the number of pixels
|
||||
- `options.limitInputPixels` **([number][8] \| [boolean][7])** Do not process input images where the number of pixels
|
||||
(width x height) exceeds this limit. Assumes image dimensions contained in the input metadata can be trusted.
|
||||
An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF). (optional, default `268402689`)
|
||||
- `options.sequentialRead` **[boolean][5]** Set this to `true` to use sequential rather than random access where possible.
|
||||
- `options.sequentialRead` **[boolean][7]** Set this to `true` to use sequential rather than random access where possible.
|
||||
This can reduce memory usage and might improve performance on some systems. (optional, default `false`)
|
||||
- `options.density` **[number][6]** number representing the DPI for vector images. (optional, default `72`)
|
||||
- `options.pages` **[number][6]** number of pages to extract for multi-page input (GIF, TIFF, PDF), use -1 for all pages. (optional, default `1`)
|
||||
- `options.page` **[number][6]** page number to start extracting from for multi-page input (GIF, TIFF, PDF), zero based. (optional, default `0`)
|
||||
- `options.level` **[number][6]** level to extract from a multi-level input (OpenSlide), zero based. (optional, default `0`)
|
||||
- `options.animated` **[boolean][5]** Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). (optional, default `false`)
|
||||
- `options.raw` **[Object][4]?** describes raw pixel input image data. See `raw()` for pixel ordering.
|
||||
- `options.raw.width` **[number][6]?**
|
||||
- `options.raw.height` **[number][6]?**
|
||||
- `options.raw.channels` **[number][6]?** 1-4
|
||||
- `options.create` **[Object][4]?** describes a new image to be created.
|
||||
- `options.create.width` **[number][6]?**
|
||||
- `options.create.height` **[number][6]?**
|
||||
- `options.create.channels` **[number][6]?** 3-4
|
||||
- `options.create.background` **([string][3] \| [Object][4])?** parsed by the [color][7] module to extract values for red, green, blue and alpha.
|
||||
- `options.density` **[number][8]** number representing the DPI for vector images in the range 1 to 100000. (optional, default `72`)
|
||||
- `options.pages` **[number][8]** number of pages to extract for multi-page input (GIF, WebP, AVIF, TIFF, PDF), use -1 for all pages. (optional, default `1`)
|
||||
- `options.page` **[number][8]** page number to start extracting from for multi-page input (GIF, WebP, AVIF, TIFF, PDF), zero based. (optional, default `0`)
|
||||
- `options.level` **[number][8]** level to extract from a multi-level input (OpenSlide), zero based. (optional, default `0`)
|
||||
- `options.animated` **[boolean][7]** Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). (optional, default `false`)
|
||||
- `options.raw` **[Object][6]?** describes raw pixel input image data. See `raw()` for pixel ordering.
|
||||
- `options.raw.width` **[number][8]?** integral number of pixels wide.
|
||||
- `options.raw.height` **[number][8]?** integral number of pixels high.
|
||||
- `options.raw.channels` **[number][8]?** integral number of channels, between 1 and 4.
|
||||
- `options.create` **[Object][6]?** describes a new image to be created.
|
||||
- `options.create.width` **[number][8]?** integral number of pixels wide.
|
||||
- `options.create.height` **[number][8]?** integral number of pixels high.
|
||||
- `options.create.channels` **[number][8]?** integral number of channels, either 3 (RGB) or 4 (RGBA).
|
||||
- `options.create.background` **([string][5] \| [Object][6])?** parsed by the [color][9] module to extract values for red, green, blue and alpha.
|
||||
- `options.create.noise` **[Object][6]?** describes a noise to be created.
|
||||
- `options.create.noise.type` **[string][5]?** type of generated noise, currently only `gaussian` is supported.
|
||||
- `options.create.noise.mean` **[number][8]?** mean of pixels in generated noise.
|
||||
- `options.create.noise.sigma` **[number][8]?** standard deviation of pixels in generated noise.
|
||||
|
||||
### Examples
|
||||
|
||||
@@ -84,9 +88,40 @@ sharp({
|
||||
await sharp('in.gif', { animated: true }).toFile('out.webp');
|
||||
```
|
||||
|
||||
- Throws **[Error][8]** Invalid parameters
|
||||
```javascript
|
||||
// Read a raw array of pixels and save it to a png
|
||||
const input = Uint8Array.from([255, 255, 255, 0, 0, 0]); // or Uint8ClampedArray
|
||||
const image = sharp(input, {
|
||||
// because the input does not contain its dimensions or how many channels it has
|
||||
// we need to specify it in the constructor options
|
||||
raw: {
|
||||
width: 2,
|
||||
height: 1,
|
||||
channels: 3
|
||||
}
|
||||
});
|
||||
await image.toFile('my-two-pixels.png');
|
||||
```
|
||||
|
||||
Returns **[Sharp][9]**
|
||||
```javascript
|
||||
// Generate RGB Gaussian noise
|
||||
await sharp({
|
||||
create: {
|
||||
width: 300,
|
||||
height: 200,
|
||||
channels: 3,
|
||||
noise: {
|
||||
type: 'gaussian',
|
||||
mean: 128,
|
||||
sigma: 30
|
||||
}
|
||||
}
|
||||
}).toFile('noise.png');
|
||||
```
|
||||
|
||||
- Throws **[Error][10]** Invalid parameters
|
||||
|
||||
Returns **[Sharp][11]**
|
||||
|
||||
## clone
|
||||
|
||||
@@ -154,22 +189,26 @@ Promise.all(promises)
|
||||
});
|
||||
```
|
||||
|
||||
Returns **[Sharp][9]**
|
||||
Returns **[Sharp][11]**
|
||||
|
||||
[1]: http://nodejs.org/api/stream.html#stream_class_stream_duplex
|
||||
|
||||
[2]: https://nodejs.org/api/buffer.html
|
||||
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray
|
||||
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[7]: https://www.npmjs.org/package/color
|
||||
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
[9]: #sharp
|
||||
[9]: https://www.npmjs.org/package/color
|
||||
|
||||
[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
[11]: #sharp
|
||||
|
||||
@@ -65,6 +65,61 @@ The use of `flop` implies the removal of the EXIF `Orientation` tag, if any.
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
## affine
|
||||
|
||||
Perform an affine transform on an image. This operation will always occur after resizing, extraction and rotation, if any.
|
||||
|
||||
You must provide an array of length 4 or a 2x2 affine transformation matrix.
|
||||
By default, new pixels are filled with a black background. You can provide a background color with the `background` option.
|
||||
A particular interpolator may also be specified. Set the `interpolator` option to an attribute of the `sharp.interpolator` Object e.g. `sharp.interpolator.nohalo`.
|
||||
|
||||
In the case of a 2x2 matrix, the transform is:
|
||||
|
||||
- X = `matrix[0, 0]` \* (x + `idx`) + `matrix[0, 1]` \* (y + `idy`) + `odx`
|
||||
- Y = `matrix[1, 0]` \* (x + `idx`) + `matrix[1, 1]` \* (y + `idy`) + `ody`
|
||||
|
||||
where:
|
||||
|
||||
- x and y are the coordinates in input image.
|
||||
- X and Y are the coordinates in output image.
|
||||
- (0,0) is the upper left corner.
|
||||
|
||||
### Parameters
|
||||
|
||||
- `matrix` **([Array][7]<[Array][7]<[number][1]>> | [Array][7]<[number][1]>)** affine transformation matrix
|
||||
- `options` **[Object][2]?** if present, is an Object with optional attributes.
|
||||
- `options.background` **([String][3] \| [Object][2])** parsed by the [color][4] module to extract values for red, green, blue and alpha. (optional, default `"#000000"`)
|
||||
- `options.idx` **[Number][1]** input horizontal offset (optional, default `0`)
|
||||
- `options.idy` **[Number][1]** input vertical offset (optional, default `0`)
|
||||
- `options.odx` **[Number][1]** output horizontal offset (optional, default `0`)
|
||||
- `options.ody` **[Number][1]** output vertical offset (optional, default `0`)
|
||||
- `options.interpolator` **[String][3]** interpolator (optional, default `sharp.interpolators.bicubic`)
|
||||
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const pipeline = sharp()
|
||||
.affine([[1, 0.3], [0.1, 0.7]], {
|
||||
background: 'white',
|
||||
interpolate: sharp.interpolators.nohalo
|
||||
})
|
||||
.toBuffer((err, outputBuffer, info) => {
|
||||
// outputBuffer contains the transformed image
|
||||
// info.width and info.height contain the new dimensions
|
||||
});
|
||||
|
||||
inputStream
|
||||
.pipe(pipeline);
|
||||
```
|
||||
|
||||
- Throws **[Error][5]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
**Meta**
|
||||
|
||||
- **since**: 0.27.0
|
||||
|
||||
## sharpen
|
||||
|
||||
Sharpen the image.
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
Write output image data to a file.
|
||||
|
||||
If an explicit output format is not selected, it will be inferred from the extension,
|
||||
with JPEG, PNG, WebP, TIFF, DZI, and libvips' V format supported.
|
||||
with JPEG, PNG, WebP, AVIF, TIFF, DZI, and libvips' V format supported.
|
||||
Note that raw pixel data is only supported for buffer output.
|
||||
|
||||
By default all metadata will be removed, which includes EXIF-based orientation.
|
||||
@@ -42,7 +42,7 @@ Returns **[Promise][5]<[Object][6]>** when no callback is provided
|
||||
## toBuffer
|
||||
|
||||
Write output to a Buffer.
|
||||
JPEG, PNG, WebP, TIFF and RAW output are supported.
|
||||
JPEG, PNG, WebP, AVIF, TIFF and raw pixel data output are supported.
|
||||
|
||||
If no explicit format is set, the output format will match the input image, except GIF and SVG input which become PNG output.
|
||||
|
||||
@@ -86,6 +86,21 @@ sharp(input)
|
||||
.catch(err => { ... });
|
||||
```
|
||||
|
||||
```javascript
|
||||
const data = await sharp('my-image.jpg')
|
||||
// output the raw pixels
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
// create a more type safe way to work with the raw pixel data
|
||||
// this will not copy the data, instead it will change `data`s underlying ArrayBuffer
|
||||
// so `data` and `pixelArray` point to the same memory location
|
||||
const pixelArray = new Uint8ClampedArray(data.buffer);
|
||||
|
||||
// When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
|
||||
await sharp(pixelArray).toFile('my-changed-image.jpg');
|
||||
```
|
||||
|
||||
Returns **[Promise][5]<[Buffer][8]>** when no callback is provided
|
||||
|
||||
## withMetadata
|
||||
@@ -272,15 +287,15 @@ Use these TIFF options for output image.
|
||||
- `options` **[Object][6]?** output options
|
||||
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.force` **[boolean][7]** force TIFF output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options.compression` **[boolean][7]** compression options: lzw, deflate, jpeg, ccittfax4 (optional, default `'jpeg'`)
|
||||
- `options.predictor` **[boolean][7]** compression predictor options: none, horizontal, float (optional, default `'horizontal'`)
|
||||
- `options.compression` **[string][2]** compression options: lzw, deflate, jpeg, ccittfax4 (optional, default `'jpeg'`)
|
||||
- `options.predictor` **[string][2]** compression predictor options: none, horizontal, float (optional, default `'horizontal'`)
|
||||
- `options.pyramid` **[boolean][7]** write an image pyramid (optional, default `false`)
|
||||
- `options.tile` **[boolean][7]** write a tiled tiff (optional, default `false`)
|
||||
- `options.tileWidth` **[boolean][7]** horizontal tile size (optional, default `256`)
|
||||
- `options.tileHeight` **[boolean][7]** vertical tile size (optional, default `256`)
|
||||
- `options.tileWidth` **[number][9]** horizontal tile size (optional, default `256`)
|
||||
- `options.tileHeight` **[number][9]** vertical tile size (optional, default `256`)
|
||||
- `options.xres` **[number][9]** horizontal resolution in pixels/mm (optional, default `1.0`)
|
||||
- `options.yres` **[number][9]** vertical resolution in pixels/mm (optional, default `1.0`)
|
||||
- `options.bitdepth` **[boolean][7]** reduce bitdepth to 1, 2 or 4 bit (optional, default `8`)
|
||||
- `options.bitdepth` **[number][9]** reduce bitdepth to 1, 2 or 4 bit (optional, default `8`)
|
||||
|
||||
### Examples
|
||||
|
||||
@@ -299,23 +314,45 @@ sharp('input.svg')
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
## heif
|
||||
## avif
|
||||
|
||||
Use these HEIF options for output image.
|
||||
Use these AVIF options for output image.
|
||||
|
||||
Support for HEIF (HEIC/AVIF) is experimental.
|
||||
Do not use this in production systems.
|
||||
|
||||
Requires a custom, globally-installed libvips compiled with support for libheif.
|
||||
|
||||
Most versions of libheif support only the patent-encumbered HEVC compression format.
|
||||
Whilst it is possible to create AVIF images smaller than 16x16 pixels,
|
||||
most web browsers do not display these properly.
|
||||
|
||||
### Parameters
|
||||
|
||||
- `options` **[Object][6]?** output options
|
||||
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.compression` **[boolean][7]** compression format: hevc, avc, jpeg, av1 (optional, default `'hevc'`)
|
||||
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `50`)
|
||||
- `options.lossless` **[boolean][7]** use lossless compression (optional, default `false`)
|
||||
- `options.speed` **[number][9]** CPU effort vs file size, 0 (slowest/smallest) to 8 (fastest/largest) (optional, default `5`)
|
||||
- `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling, requires libvips v8.11.0 (optional, default `'4:2:0'`)
|
||||
|
||||
|
||||
- Throws **[Error][4]** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
**Meta**
|
||||
|
||||
- **since**: 0.27.0
|
||||
|
||||
## heif
|
||||
|
||||
Use these HEIF options for output image.
|
||||
|
||||
Support for patent-encumbered HEIC images requires the use of a
|
||||
globally-installed libvips compiled with support for libheif, libde265 and x265.
|
||||
|
||||
### Parameters
|
||||
|
||||
- `options` **[Object][6]?** output options
|
||||
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `50`)
|
||||
- `options.compression` **[string][2]** compression format: av1, hevc (optional, default `'av1'`)
|
||||
- `options.lossless` **[boolean][7]** use lossless compression (optional, default `false`)
|
||||
- `options.speed` **[number][9]** CPU effort vs file size, 0 (slowest/smallest) to 8 (fastest/largest) (optional, default `5`)
|
||||
- `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling, requires libvips v8.11.0 (optional, default `'4:2:0'`)
|
||||
|
||||
|
||||
- Throws **[Error][4]** Invalid options
|
||||
@@ -372,6 +409,8 @@ Warning: multiple sharp instances concurrently producing tile output can expose
|
||||
- `options.skipBlanks` **[number][9]** threshold to skip tile generation, a value 0 - 255 for 8-bit images or 0 - 65535 for 16-bit images (optional, default `-1`)
|
||||
- `options.container` **[string][2]** tile container, with value `fs` (filesystem) or `zip` (compressed file). (optional, default `'fs'`)
|
||||
- `options.layout` **[string][2]** filesystem layout, possible values are `dz`, `iiif`, `zoomify` or `google`. (optional, default `'dz'`)
|
||||
- `options.centre` **[boolean][7]** centre image in tile. (optional, default `false`)
|
||||
- `options.center` **[boolean][7]** alternative spelling of centre. (optional, default `false`)
|
||||
|
||||
### Examples
|
||||
|
||||
|
||||
@@ -209,7 +209,8 @@ Returns **Sharp**
|
||||
Trim "boring" pixels from all edges that contain values similar to the top-left pixel.
|
||||
Images consisting entirely of a single colour will calculate "boring" using the alpha channel, if any.
|
||||
|
||||
The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties.
|
||||
The `info` response Object, obtained from callback of `.toFile()` or `.toBuffer()`,
|
||||
will contain `trimOffsetLeft` and `trimOffsetTop` properties.
|
||||
|
||||
### Parameters
|
||||
|
||||
|
||||
@@ -12,6 +12,36 @@ console.log(sharp.format);
|
||||
|
||||
Returns **[Object][1]**
|
||||
|
||||
## interpolators
|
||||
|
||||
An Object containing the available interpolators and their proper values
|
||||
|
||||
Type: [string][2]
|
||||
|
||||
### nearest
|
||||
|
||||
[Nearest neighbour interpolation][3]. Suitable for image enlargement only.
|
||||
|
||||
### bilinear
|
||||
|
||||
[Bilinear interpolation][4]. Faster than bicubic but with less smooth results.
|
||||
|
||||
### bicubic
|
||||
|
||||
[Bicubic interpolation][5] (the default).
|
||||
|
||||
### locallyBoundedBicubic
|
||||
|
||||
[LBB interpolation][6]. Prevents some "[acutance][7]" but typically reduces performance by a factor of 2.
|
||||
|
||||
### nohalo
|
||||
|
||||
[Nohalo interpolation][8]. Prevents acutance but typically reduces performance by a factor of 3.
|
||||
|
||||
### vertexSplitQuadraticBasisSpline
|
||||
|
||||
[VSQBS interpolation][9]. Prevents "staircasing" when enlarging.
|
||||
|
||||
## versions
|
||||
|
||||
An Object containing the version numbers of libvips and its dependencies.
|
||||
@@ -31,10 +61,10 @@ useful for determining how much working memory is required for a particular task
|
||||
|
||||
### Parameters
|
||||
|
||||
- `options` **([Object][1] \| [boolean][2])** Object with the following attributes, or boolean where true uses default cache settings and false removes all caching (optional, default `true`)
|
||||
- `options.memory` **[number][3]** is the maximum memory in MB to use for this cache (optional, default `50`)
|
||||
- `options.files` **[number][3]** is the maximum number of files to hold open (optional, default `20`)
|
||||
- `options.items` **[number][3]** is the maximum number of operations to cache (optional, default `100`)
|
||||
- `options` **([Object][1] \| [boolean][10])** Object with the following attributes, or boolean where true uses default cache settings and false removes all caching (optional, default `true`)
|
||||
- `options.memory` **[number][11]** is the maximum memory in MB to use for this cache (optional, default `50`)
|
||||
- `options.files` **[number][11]** is the maximum number of files to hold open (optional, default `20`)
|
||||
- `options.items` **[number][11]** is the maximum number of operations to cache (optional, default `100`)
|
||||
|
||||
### Examples
|
||||
|
||||
@@ -64,7 +94,7 @@ This method always returns the current concurrency.
|
||||
|
||||
### Parameters
|
||||
|
||||
- `concurrency` **[number][3]?**
|
||||
- `concurrency` **[number][11]?**
|
||||
|
||||
### Examples
|
||||
|
||||
@@ -74,7 +104,7 @@ sharp.concurrency(2); // 2
|
||||
sharp.concurrency(0); // 4
|
||||
```
|
||||
|
||||
Returns **[number][3]** concurrency
|
||||
Returns **[number][11]** concurrency
|
||||
|
||||
## queue
|
||||
|
||||
@@ -116,7 +146,7 @@ by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM N
|
||||
|
||||
### Parameters
|
||||
|
||||
- `simd` **[boolean][2]** (optional, default `true`)
|
||||
- `simd` **[boolean][10]** (optional, default `true`)
|
||||
|
||||
### Examples
|
||||
|
||||
@@ -130,10 +160,26 @@ const simd = sharp.simd(false);
|
||||
// prevent libvips from using liborc at runtime
|
||||
```
|
||||
|
||||
Returns **[boolean][2]**
|
||||
Returns **[boolean][10]**
|
||||
|
||||
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
[3]: http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
|
||||
|
||||
[4]: http://en.wikipedia.org/wiki/Bilinear_interpolation
|
||||
|
||||
[5]: http://en.wikipedia.org/wiki/Bicubic_interpolation
|
||||
|
||||
[6]: https://github.com/jcupitt/libvips/blob/master/libvips/resample/lbb.cpp#L100
|
||||
|
||||
[7]: http://en.wikipedia.org/wiki/Acutance
|
||||
|
||||
[8]: http://eprints.soton.ac.uk/268086/
|
||||
|
||||
[9]: https://github.com/jcupitt/libvips/blob/master/libvips/resample/vsqbs.cpp#L48
|
||||
|
||||
[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
25
docs/build.js
Normal file
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const documentation = require('documentation');
|
||||
|
||||
[
|
||||
'constructor',
|
||||
'input',
|
||||
'resize',
|
||||
'composite',
|
||||
'operation',
|
||||
'colour',
|
||||
'channel',
|
||||
'output',
|
||||
'utility'
|
||||
].forEach(async (m) => {
|
||||
const input = path.join('lib', `${m}.js`);
|
||||
const output = path.join('docs', `api-${m}.md`);
|
||||
|
||||
const ast = await documentation.build(input, { shallow: true });
|
||||
const markdown = await documentation.formats.md(ast, { markdownToc: false });
|
||||
|
||||
await fs.writeFile(output, markdown);
|
||||
});
|
||||
@@ -1,9 +1,104 @@
|
||||
# Changelog
|
||||
|
||||
## v0.27 - *avif*
|
||||
|
||||
Requires libvips v8.10.5
|
||||
|
||||
### v0.27.2 - 22nd February 2021
|
||||
|
||||
* macOS: Prevent use of globally-installed ARM64 libvips with Rosetta x64 emulation.
|
||||
[#2460](https://github.com/lovell/sharp/issues/2460)
|
||||
|
||||
* Linux (musl): Prevent use of prebuilt linuxmusl-x64 binaries with musl >= 1.2.0.
|
||||
[#2570](https://github.com/lovell/sharp/issues/2570)
|
||||
|
||||
* Improve 16-bit grey+alpha support by using libvips' `has_alpha` detection.
|
||||
[#2569](https://github.com/lovell/sharp/issues/2569)
|
||||
|
||||
* Allow the use of non lower case extensions with `toFormat`.
|
||||
[#2581](https://github.com/lovell/sharp/pull/2581)
|
||||
[@florian-busch](https://github.com/florian-busch)
|
||||
|
||||
* Allow use of `recomb` operation with single channel input.
|
||||
[#2584](https://github.com/lovell/sharp/issues/2584)
|
||||
|
||||
### v0.27.1 - 27th January 2021
|
||||
|
||||
* Ensure TIFF is cast when using float predictor.
|
||||
[#2502](https://github.com/lovell/sharp/pull/2502)
|
||||
[@randyridge](https://github.com/randyridge)
|
||||
|
||||
* Add support for Uint8Array and Uint8ClampedArray input.
|
||||
[#2511](https://github.com/lovell/sharp/pull/2511)
|
||||
[@leon](https://github.com/leon)
|
||||
|
||||
* Revert: ensure all platforms use fontconfig for font rendering.
|
||||
[#2515](https://github.com/lovell/sharp/issues/2515)
|
||||
|
||||
* Expose libvips gaussnoise operation to allow creation of Gaussian noise.
|
||||
[#2527](https://github.com/lovell/sharp/pull/2527)
|
||||
[@alza54](https://github.com/alza54)
|
||||
|
||||
### v0.27.0 - 22nd December 2020
|
||||
|
||||
* Add support for AVIF to prebuilt binaries.
|
||||
|
||||
* Remove experimental status from `heif` output, defaults are now AVIF-centric.
|
||||
|
||||
* Allow negative top/left offsets for composite operation.
|
||||
[#2391](https://github.com/lovell/sharp/pull/2391)
|
||||
[@CurosMJ](https://github.com/CurosMJ)
|
||||
|
||||
* Ensure all platforms use fontconfig for font rendering.
|
||||
[#2399](https://github.com/lovell/sharp/issues/2399)
|
||||
|
||||
## v0.26 - *zoom*
|
||||
|
||||
Requires libvips v8.10.0
|
||||
|
||||
### v0.26.3 - 16th November 2020
|
||||
|
||||
* Expose libvips' affine operation.
|
||||
[#2336](https://github.com/lovell/sharp/pull/2336)
|
||||
[@guillevc](https://github.com/guillevc)
|
||||
|
||||
* Fallback to tar.gz for prebuilt libvips when Brotli not available.
|
||||
[#2412](https://github.com/lovell/sharp/pull/2412)
|
||||
[@ascorbic](https://github.com/ascorbic)
|
||||
|
||||
### v0.26.2 - 14th October 2020
|
||||
|
||||
* Add support for EXR input. Requires libvips compiled with OpenEXR.
|
||||
[#698](https://github.com/lovell/sharp/issues/698)
|
||||
|
||||
* Ensure support for yarn v2.
|
||||
[#2379](https://github.com/lovell/sharp/pull/2379)
|
||||
[@jalovatt](https://github.com/jalovatt)
|
||||
|
||||
* Add centre/center option to tile-based output.
|
||||
[#2397](https://github.com/lovell/sharp/pull/2397)
|
||||
[@beig](https://github.com/beig)
|
||||
|
||||
### v0.26.1 - 20th September 2020
|
||||
|
||||
* Ensure correct pageHeight when verifying multi-page image dimensions.
|
||||
[#2343](https://github.com/lovell/sharp/pull/2343)
|
||||
[@derom](https://github.com/derom)
|
||||
|
||||
* Allow input density range up to 100000 DPI.
|
||||
[#2348](https://github.com/lovell/sharp/pull/2348)
|
||||
[@stefanprobst](https://github.com/stefanprobst)
|
||||
|
||||
* Ensure animation-related properties can be set for Stream-based input.
|
||||
[#2369](https://github.com/lovell/sharp/pull/2369)
|
||||
[@AcrylicShrimp](https://github.com/AcrylicShrimp)
|
||||
|
||||
* Ensure `stats` can be calculated for 1x1 input.
|
||||
[#2372](https://github.com/lovell/sharp/issues/2372)
|
||||
|
||||
* Ensure animated GIF output is optimised.
|
||||
[#2376](https://github.com/lovell/sharp/issues/2376)
|
||||
|
||||
### v0.26.0 - 25th August 2020
|
||||
|
||||
* Prebuilt libvips binaries are now statically-linked and Brotli-compressed, requiring Node.js 10.16.0+.
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"public": ".",
|
||||
"ignore": [
|
||||
".*",
|
||||
"build.js",
|
||||
"firebase.json",
|
||||
"*.md",
|
||||
"image/**",
|
||||
|
||||
@@ -197,3 +197,15 @@ GitHub: https://github.com/deftomat
|
||||
|
||||
Name: Robert O'Rourke
|
||||
GitHub: https://github.com/roborourke
|
||||
|
||||
Name: Denis Soldatov
|
||||
GitHub: https://github.com/derom
|
||||
|
||||
Name: Stefan Probst
|
||||
GitHub: https://github.com/stefanprobst
|
||||
|
||||
Name: Thomas Beiganz
|
||||
GitHub: https://github.com/beig
|
||||
|
||||
Name: Florian Busch
|
||||
GitHub: https://github.com/florian-busch
|
||||
|
||||
11
docs/image/sharp-logo-mono.svg
Normal file
@@ -0,0 +1,11 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="86 86 550 550">
|
||||
<!-- Creative Commons CC0 1.0 Universal Public Domain Dedication -->
|
||||
<defs>
|
||||
<mask id="c">
|
||||
<path fill="none" stroke="#fff" stroke-width="80" d="M258.411 285.777l200.176-26.8M244.113 466.413L451.44 438.66m.001 0V238.484m0-150.121v171.572l178.725-23.917m-359.843 19.584V477.22m2.387 156.95V462.591L93.984 486.515"/>
|
||||
<path fill="none" stroke="#000" stroke-width="112" d="M451.441 610.246V438.66l178.725-23.91M269.688 112.59v171.58L90.964 308.093"/>
|
||||
</mask>
|
||||
</defs>
|
||||
<path mask="url(#c)" fill="none" stroke="#000" stroke-width="80" d="M258.411 285.777l200.176-26.8M244.113 466.413L451.44 438.66m.001 0V238.484m0-150.121v171.572l178.725-23.917m-359.843 19.584V477.22m2.387 156.95V462.591L93.984 486.515"/>
|
||||
<path fill="none" stroke="#000" stroke-width="80" d="M451.441 610.246V438.66l178.725-23.91M269.688 112.59v171.58L90.964 308.093"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 929 B |
@@ -1,5 +1,5 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="86 86 550 550">
|
||||
<!-- Copyright 2019 Lovell Fuller. This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) License. -->
|
||||
<!-- Creative Commons CC0 1.0 Universal Public Domain Dedication -->
|
||||
<path fill="none" stroke="#9c0" stroke-width="80" d="M258.411 285.777l200.176-26.8M244.113 466.413L451.44 438.66M451.441 438.66V238.484M451.441 88.363v171.572l178.725-23.917M270.323 255.602V477.22M272.71 634.17V462.591L93.984 486.515"/>
|
||||
<path fill="none" stroke="#090" stroke-width="80" d="M451.441 610.246V438.66l178.725-23.91M269.688 112.59v171.58L90.964 308.093"/>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 592 B After Width: | Height: | Size: 508 B |
@@ -4,11 +4,11 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<meta name="description" content="Resize large images in common formats to smaller, web-friendly JPEG, PNG and WebP images of varying dimensions">
|
||||
<meta name="description" content="Resize large images in common formats to smaller, web-friendly JPEG, PNG, WebP and AVIF images of varying dimensions">
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; object-src 'none'; style-src 'unsafe-inline';
|
||||
img-src 'unsafe-inline' data: https://pixel.plumbing/px/ https://cdn.jsdelivr.net/gh/lovell/ https://www.google-analytics.com;
|
||||
connect-src 'self' https://cdn.jsdelivr.net/gh/lovell/ https://www.google-analytics.com;
|
||||
script-src 'sha256-iXDHv+t2aGdJcmEvwHfqmGH4SrOSx+P8rCF6+WLLwXA=' 'unsafe-eval'
|
||||
script-src 'unsafe-inline' 'unsafe-eval'
|
||||
https://cdn.jsdelivr.net/npm/docute@4/dist/docute.min.js
|
||||
https://www.google-analytics.com/analytics.js;">
|
||||
<link rel="icon" type="image/png" href="https://pixel.plumbing/px/32x32/sharp-logo.svg">
|
||||
@@ -19,7 +19,7 @@
|
||||
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="https://pixel.plumbing/px/72x72/sharp-logo.svg">
|
||||
<link rel="apple-touch-icon-precomposed" href="https://pixel.plumbing/px/57x57/sharp-logo.svg">
|
||||
<link rel="author" href="/humans.txt" type="text/plain">
|
||||
<link rel="preload" href="https://cdn.jsdelivr.net/gh/lovell/sharp@v0.26.0/docs/README.md" as="fetch" type="text/markdown" crossorigin>
|
||||
<link rel="preload" href="https://cdn.jsdelivr.net/gh/lovell/sharp@v0.27.2/docs/README.md" as="fetch" type="text/markdown" crossorigin>
|
||||
<link rel="preload" href="https://cdn.jsdelivr.net/gh/lovell/sharp@master/docs/image/sharp-logo.svg" as="image" type="image/svg+xml" crossorigin>
|
||||
<link rel="dns-prefetch" href="https://pixel.plumbing">
|
||||
<link rel="dns-prefetch" href="https://www.google-analytics.com">
|
||||
@@ -38,7 +38,7 @@
|
||||
"@type": "Person",
|
||||
"name": "Lovell Fuller"
|
||||
},
|
||||
"copyrightYear": [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020],
|
||||
"copyrightYear": [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021],
|
||||
"license": "https://www.apache.org/licenses/LICENSE-2.0"
|
||||
}
|
||||
</script>
|
||||
@@ -139,7 +139,7 @@
|
||||
docuteApiTitlePlugin,
|
||||
docuteApiSearchPlugin
|
||||
],
|
||||
sourcePath: 'https://cdn.jsdelivr.net/gh/lovell/sharp@v0.26.0/docs',
|
||||
sourcePath: 'https://cdn.jsdelivr.net/gh/lovell/sharp@v0.27.2/docs',
|
||||
nav: [
|
||||
{
|
||||
title: 'Funding',
|
||||
|
||||
103
docs/install.md
@@ -10,31 +10,34 @@ yarn add sharp
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* Node.js v10.16.0+
|
||||
* Node.js v10+
|
||||
|
||||
## Prebuilt binaries
|
||||
|
||||
Ready-compiled sharp and libvips binaries are provided for use with
|
||||
Node.js v10.16.0+ on the most common platforms:
|
||||
Node.js v10+ on the most common platforms:
|
||||
|
||||
* macOS x64 (>= 10.13)
|
||||
* Linux x64 (glibc >= 2.17, musl >= 1.1.24)
|
||||
* Linux x64 (glibc >= 2.17, musl >=1.1.24 <1.2.0)
|
||||
* Linux ARM64 (glibc >= 2.29)
|
||||
* Windows
|
||||
* Windows x64
|
||||
* Windows x86
|
||||
|
||||
A ~7MB tarball containing libvips and its most commonly used dependencies
|
||||
A ~9MB tarball containing libvips and its most commonly used dependencies
|
||||
is downloaded via HTTPS and stored within `node_modules/sharp/vendor` during `npm install`.
|
||||
|
||||
This provides support for the
|
||||
JPEG, PNG, WebP, TIFF, GIF (input) and SVG (input) image formats.
|
||||
JPEG, PNG, WebP, AVIF, TIFF, GIF (input) and SVG (input) image formats.
|
||||
|
||||
The following platforms have prebuilt libvips but not sharp:
|
||||
|
||||
* Linux ARMv6
|
||||
* Linux ARMv7 (glibc >= 2.28)
|
||||
* Windows ARM64
|
||||
|
||||
The following platforms require compilation of both libvips and sharp from source:
|
||||
|
||||
* macOS ARM64
|
||||
* Linux x86
|
||||
* Linux x64 (glibc <= 2.16, includes RHEL/CentOS 6)
|
||||
* Linux ARM64 (glibc <= 2.28, musl)
|
||||
@@ -47,12 +50,32 @@ The following platforms require compilation of both libvips and sharp from sourc
|
||||
The architecture and platform of Node.js used for `npm install`
|
||||
must be the same as the architecture and platform of Node.js used at runtime.
|
||||
|
||||
The `npm install --unsafe-perm` flag must be used when installing as `root` or a `sudo` user.
|
||||
When using npm v6 or earlier, the `npm install --unsafe-perm` flag must be used when installing as `root` or a `sudo` user.
|
||||
|
||||
When using npm v7, the user running `npm install` must own the directory it is run in.
|
||||
|
||||
The `npm install --ignore-scripts=false` flag must be used when `npm` has been configured to ignore installation scripts.
|
||||
|
||||
Check the output of running `npm install --verbose sharp` for useful error messages.
|
||||
|
||||
## Apple M1
|
||||
|
||||
If you are using ARM64 Node.js, which can be checked using:
|
||||
|
||||
```sh
|
||||
node -p "process.arch === 'arm64'"
|
||||
```
|
||||
|
||||
then libvips must currently be installed via Homebrew before installing sharp.
|
||||
|
||||
```sh
|
||||
brew install vips
|
||||
```
|
||||
|
||||
When this new ARM64 CPU is made freely available
|
||||
to open source projects via a CI service
|
||||
then prebuilt binaries can be provided.
|
||||
|
||||
## Custom libvips
|
||||
|
||||
To use a custom, globally-installed version of libvips instead of the provided binaries,
|
||||
@@ -69,7 +92,7 @@ The use of a globally-installed libvips is unsupported on Windows.
|
||||
This module will be compiled from source at `npm install` time when:
|
||||
|
||||
* a globally-installed libvips is detected (set the `SHARP_IGNORE_GLOBAL_LIBVIPS` environment variable to skip this),
|
||||
* prebuilt binaries do not exist for the current platform and Node.js version, or
|
||||
* prebuilt sharp binaries do not exist for the current platform, or
|
||||
* when the `npm install --build-from-source` flag is used.
|
||||
|
||||
Building from source requires:
|
||||
@@ -89,11 +112,14 @@ To install the prebuilt libvips binaries from a custom URL,
|
||||
set the `sharp_libvips_binary_host` npm config option
|
||||
or the `npm_config_sharp_libvips_binary_host` environment variable.
|
||||
|
||||
The version subpath and file name are appended to these.
|
||||
The version subpath and file name are appended to these. There should be tarballs available
|
||||
that are compressed with both gzip and Brotli, as the format downloaded will vary depending
|
||||
on whether the user's version of Node supports Brotli decompression (Node.js v10.16.0+)
|
||||
|
||||
For example, if `sharp_libvips_binary_host` is set to `https://hostname/path`
|
||||
and the libvips version is `1.2.3` then the resultant URL will be
|
||||
`https://hostname/path/v1.2.3/libvips-1.2.3-platform-arch.tar.br`.
|
||||
`https://hostname/path/v1.2.3/libvips-1.2.3-platform-arch.tar.br` or
|
||||
`https://hostname/path/v1.2.3/libvips-1.2.3-platform-arch.tar.gz`.
|
||||
|
||||
See the Chinese mirror below for a further example.
|
||||
|
||||
@@ -147,10 +173,18 @@ The binaries in the `node_modules` directory of the
|
||||
[deployment package](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html)
|
||||
must be for the Linux x64 platform.
|
||||
|
||||
On machines other than Linux x64, such as macOS and Windows, run the following:
|
||||
When building your deployment package on machines other than Linux x64 (glibc),
|
||||
run the following commands:
|
||||
|
||||
macOS:
|
||||
```sh
|
||||
rm -rf node_modules/sharp
|
||||
SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux sharp
|
||||
```
|
||||
|
||||
Windows:
|
||||
```sh
|
||||
rmdir /s /q node_modules/sharp
|
||||
npm install --arch=x64 --platform=linux sharp
|
||||
```
|
||||
|
||||
@@ -164,23 +198,50 @@ docker run -v "$PWD":/var/task lambci/lambda:build-nodejs12.x npm install sharp
|
||||
To get the best performance select the largest memory available.
|
||||
A 1536 MB function provides ~12x more CPU time than a 128 MB function.
|
||||
|
||||
## Electron
|
||||
## Webpack
|
||||
|
||||
Electron provides versions of the V8 JavaScript engine
|
||||
that are incompatible with Node.js.
|
||||
To ensure the correct binaries are used, run the following:
|
||||
Ensure sharp is added to the
|
||||
[externals](https://webpack.js.org/configuration/externals/)
|
||||
configuration.
|
||||
|
||||
```sh
|
||||
npm install
|
||||
npx electron-rebuild
|
||||
```js
|
||||
externals: {
|
||||
'sharp': 'commonjs sharp'
|
||||
}
|
||||
```
|
||||
|
||||
Further help can be found at
|
||||
[https://electronjs.org/docs/tutorial/using-native-node-modules](https://electronjs.org/docs/tutorial/using-native-node-modules)
|
||||
|
||||
## Worker threads
|
||||
|
||||
The main thread must call `require('sharp')`
|
||||
before worker threads are created
|
||||
to ensure shared libraries remain loaded in memory
|
||||
until after all threads are complete.
|
||||
|
||||
## Known conflicts
|
||||
|
||||
### Electron and Linux
|
||||
|
||||
The prebuilt binaries provided by Electron for Linux depend on many shared system libraries.
|
||||
|
||||
One of these, `libgobject-2.0.so`,
|
||||
is known to conflict with the statically-linked binaries provided by sharp
|
||||
and the following error can occur:
|
||||
```
|
||||
basic_string::_S_construct null not valid
|
||||
```
|
||||
|
||||
To workaround this, set the `LD_PRELOAD` environment variable before the `electron` binary is run.
|
||||
```sh
|
||||
LD_PRELOAD=node_modules/sharp/vendor/8.10.5/lib/libvips.so.42 electron script.js
|
||||
```
|
||||
|
||||
### Canvas and Windows
|
||||
|
||||
The prebuilt binaries provided by `canvas` for Windows depend on the unmaintained GTK 2, last updated in 2011.
|
||||
|
||||
These conflict with the modern, up-to-date binaries provided by sharp.
|
||||
|
||||
If both modules are used in the same Windows process, the following error will occur:
|
||||
```
|
||||
The specified procedure could not be found.
|
||||
```
|
||||
|
||||
@@ -4,11 +4,11 @@ A test to benchmark the performance of this module relative to alternatives.
|
||||
|
||||
## The contenders
|
||||
|
||||
* [jimp](https://www.npmjs.com/package/jimp) v0.16.0 - Image processing in pure JavaScript. Provides bicubic interpolation.
|
||||
* [mapnik](https://www.npmjs.org/package/mapnik) v4.5.2 - Whilst primarily a map renderer, Mapnik contains bitmap image utilities.
|
||||
* [jimp](https://www.npmjs.com/package/jimp) v0.16.1 - Image processing in pure JavaScript. Provides bicubic interpolation.
|
||||
* [mapnik](https://www.npmjs.org/package/mapnik) v4.5.5 - Whilst primarily a map renderer, Mapnik contains bitmap image utilities.
|
||||
* [imagemagick](https://www.npmjs.com/package/imagemagick) v0.1.3 - Supports filesystem only and "*has been unmaintained for a long time*".
|
||||
* [gm](https://www.npmjs.com/package/gm) v1.23.1 - Fully featured wrapper around GraphicsMagick's `gm` command line utility.
|
||||
* sharp v0.26.0 / libvips v8.10.0 - Caching within libvips disabled to ensure a fair comparison.
|
||||
* sharp v0.27.0 / libvips v8.10.5 - Caching within libvips disabled to ensure a fair comparison.
|
||||
|
||||
## The task
|
||||
|
||||
@@ -18,22 +18,22 @@ then compress to JPEG at a "quality" setting of 80.
|
||||
|
||||
## Test environment
|
||||
|
||||
* AWS EC2 eu-west-1 [c5d.large](https://aws.amazon.com/ec2/instance-types/c5/) (2x Xeon Platinum 8124M CPU @ 3.00GHz)
|
||||
* Ubuntu 20.04 (ami-0f1d11c92a9467c07)
|
||||
* Node.js v14.8.0
|
||||
* AWS EC2 eu-west-1 [c5d.large](https://aws.amazon.com/ec2/instance-types/c5/) (2x Xeon Platinum 8275CL CPU @ 3.00GHz)
|
||||
* Ubuntu 20.10 (ami-046cdbcee95cdd75c)
|
||||
* Node.js v14.15.3
|
||||
|
||||
## Results
|
||||
|
||||
| Module | Input | Output | Ops/sec | Speed-up |
|
||||
| :----------------- | :----- | :----- | ------: | -------: |
|
||||
| jimp | buffer | buffer | 0.75 | 1.0 |
|
||||
| mapnik | buffer | buffer | 3.00 | 4.0 |
|
||||
| gm | buffer | buffer | 4.12 | 5.5 |
|
||||
| gm | file | file | 4.13 | 5.5 |
|
||||
| imagemagick | file | file | 4.30 | 5.7 |
|
||||
| sharp | stream | stream | 22.37 | 29.8 |
|
||||
| sharp | file | file | 23.40 | 31.2 |
|
||||
| sharp | buffer | buffer | 24.01 | 32.0 |
|
||||
| jimp | buffer | buffer | 0.77 | 1.0 |
|
||||
| mapnik | buffer | buffer | 3.39 | 4.4 |
|
||||
| gm | buffer | buffer | 4.30 | 5.6 |
|
||||
| gm | file | file | 4.33 | 5.6 |
|
||||
| imagemagick | file | file | 4.39 | 5.7 |
|
||||
| sharp | stream | stream | 23.81 | 30.9 |
|
||||
| sharp | file | file | 25.09 | 32.6 |
|
||||
| sharp | buffer | buffer | 25.60 | 33.2 |
|
||||
|
||||
Greater libvips performance can be expected with caching enabled (default)
|
||||
and using 4+ core machines, especially those with larger L1/L2 CPU caches.
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { extractDescription, extractKeywords } = require('./extract');
|
||||
|
||||
const searchIndex = [];
|
||||
|
||||
// Install
|
||||
const contents = fs.readFileSync(`${__dirname}/../install.md`, 'utf8');
|
||||
const contents = fs.readFileSync(path.join(__dirname, '..', 'install.md'), 'utf8');
|
||||
const matches = contents.matchAll(
|
||||
/## (?<title>[A-Za-z ]+)\n\n(?<body>[^#]+)/gs
|
||||
/## (?<title>[A-Za-z0-9 ]+)\n\n(?<body>[^#]+)/gs
|
||||
);
|
||||
for (const match of matches) {
|
||||
const { title, body } = match.groups;
|
||||
@@ -34,7 +35,7 @@ for (const match of matches) {
|
||||
'colour',
|
||||
'utility'
|
||||
].forEach((section) => {
|
||||
const contents = fs.readFileSync(`${__dirname}/../api-${section}.md`, 'utf8');
|
||||
const contents = fs.readFileSync(path.join(__dirname, '..', `api-${section}.md`), 'utf8');
|
||||
const matches = contents.matchAll(
|
||||
/\n## (?<title>[A-Za-z]+)\n\n(?<firstparagraph>.+?)\n\n/gs
|
||||
);
|
||||
@@ -54,6 +55,6 @@ for (const match of matches) {
|
||||
});
|
||||
|
||||
fs.writeFileSync(
|
||||
`${__dirname}/../search-index.json`,
|
||||
path.join(__dirname, '..', 'search-index.json'),
|
||||
JSON.stringify(searchIndex)
|
||||
);
|
||||
|
||||
@@ -1,65 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const stopWords = [
|
||||
'a',
|
||||
'about',
|
||||
'all',
|
||||
'already',
|
||||
'always',
|
||||
'an',
|
||||
'and',
|
||||
'any',
|
||||
'are',
|
||||
'as',
|
||||
'at',
|
||||
'be',
|
||||
'been',
|
||||
'by',
|
||||
'can',
|
||||
'do',
|
||||
'does',
|
||||
'each',
|
||||
'either',
|
||||
'etc',
|
||||
'for',
|
||||
'from',
|
||||
'get',
|
||||
'gets',
|
||||
'has',
|
||||
'have',
|
||||
'how',
|
||||
'if',
|
||||
'in',
|
||||
'is',
|
||||
'it',
|
||||
'its',
|
||||
'may',
|
||||
'more',
|
||||
'much',
|
||||
'no',
|
||||
'not',
|
||||
'of',
|
||||
'on',
|
||||
'or',
|
||||
'over',
|
||||
'set',
|
||||
'sets',
|
||||
'should',
|
||||
'that',
|
||||
'the',
|
||||
'their',
|
||||
'there',
|
||||
'therefore',
|
||||
'these',
|
||||
'this',
|
||||
'to',
|
||||
'use',
|
||||
'using',
|
||||
'when',
|
||||
'which',
|
||||
'will',
|
||||
'with'
|
||||
];
|
||||
const stopWords = require('./stop-words');
|
||||
|
||||
const extractDescription = (str) =>
|
||||
str
|
||||
@@ -71,10 +12,13 @@ const extractDescription = (str) =>
|
||||
.trim();
|
||||
|
||||
const extractKeywords = (str) =>
|
||||
str
|
||||
.split(/[ -/]/)
|
||||
.map((word) => word.toLowerCase().replace(/[^a-z]/g, ''))
|
||||
.filter((word) => word.length > 2 && !stopWords.includes(word))
|
||||
.join(' ');
|
||||
[
|
||||
...new Set(
|
||||
str
|
||||
.split(/[ -/]/)
|
||||
.map((word) => word.toLowerCase().replace(/[^a-z]/g, ''))
|
||||
.filter((word) => word.length > 2 && word.length < 15 && !stopWords.includes(word))
|
||||
)
|
||||
].join(' ');
|
||||
|
||||
module.exports = { extractDescription, extractKeywords };
|
||||
|
||||
73
docs/search-index/stop-words.js
Normal file
@@ -0,0 +1,73 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = [
|
||||
'about',
|
||||
'after',
|
||||
'all',
|
||||
'already',
|
||||
'alternative',
|
||||
'always',
|
||||
'and',
|
||||
'any',
|
||||
'are',
|
||||
'been',
|
||||
'before',
|
||||
'can',
|
||||
'containing',
|
||||
'default',
|
||||
'does',
|
||||
'each',
|
||||
'either',
|
||||
'ensure',
|
||||
'etc',
|
||||
'every',
|
||||
'for',
|
||||
'from',
|
||||
'get',
|
||||
'gets',
|
||||
'given',
|
||||
'has',
|
||||
'have',
|
||||
'how',
|
||||
'image',
|
||||
'its',
|
||||
'may',
|
||||
'more',
|
||||
'most',
|
||||
'much',
|
||||
'must',
|
||||
'non',
|
||||
'not',
|
||||
'occur',
|
||||
'occurs',
|
||||
'over',
|
||||
'perform',
|
||||
'performs',
|
||||
'provide',
|
||||
'provided',
|
||||
'set',
|
||||
'sets',
|
||||
'should',
|
||||
'spelling',
|
||||
'support',
|
||||
'supported',
|
||||
'take',
|
||||
'that',
|
||||
'the',
|
||||
'their',
|
||||
'there',
|
||||
'therefore',
|
||||
'these',
|
||||
'this',
|
||||
'use',
|
||||
'used',
|
||||
'using',
|
||||
'value',
|
||||
'values',
|
||||
'when',
|
||||
'which',
|
||||
'while',
|
||||
'will',
|
||||
'with',
|
||||
'without'
|
||||
];
|
||||
@@ -25,6 +25,7 @@ const minimumGlibcVersionByArch = {
|
||||
const { minimumLibvipsVersion, minimumLibvipsVersionLabelled } = libvips;
|
||||
const distHost = process.env.npm_config_sharp_libvips_binary_host || 'https://github.com/lovell/sharp-libvips/releases/download';
|
||||
const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SHARP_DIST_BASE_URL || `${distHost}/v${minimumLibvipsVersionLabelled}/`;
|
||||
const supportsBrotli = ('BrotliDecompress' in zlib);
|
||||
|
||||
const fail = function (err) {
|
||||
npmLog.error('sharp', err.message);
|
||||
@@ -43,7 +44,7 @@ const extractTarball = function (tarPath) {
|
||||
libvips.mkdirSync(versionedVendorPath);
|
||||
stream.pipeline(
|
||||
fs.createReadStream(tarPath),
|
||||
new zlib.BrotliDecompress(),
|
||||
supportsBrotli ? new zlib.BrotliDecompress() : new zlib.Gunzip(),
|
||||
tarFs.extract(versionedVendorPath),
|
||||
function (err) {
|
||||
if (err) {
|
||||
@@ -58,6 +59,7 @@ const extractTarball = function (tarPath) {
|
||||
|
||||
try {
|
||||
const useGlobalLibvips = libvips.useGlobalLibvips();
|
||||
|
||||
if (useGlobalLibvips) {
|
||||
const globalLibvipsVersion = libvips.globalLibvipsVersion();
|
||||
npmLog.info('sharp', `Detected globally-installed libvips v${globalLibvipsVersion}`);
|
||||
@@ -72,6 +74,9 @@ try {
|
||||
if (arch === 'ia32' && !platformAndArch.startsWith('win32')) {
|
||||
throw new Error(`Intel Architecture 32-bit systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
||||
}
|
||||
if (platformAndArch === 'darwin-arm64') {
|
||||
throw new Error("Please run 'brew install vips' to install libvips on Apple M1 (ARM64) systems");
|
||||
}
|
||||
if (platformAndArch === 'freebsd-x64' || platformAndArch === 'openbsd-x64' || platformAndArch === 'sunos-x64') {
|
||||
throw new Error(`BSD/SunOS systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
||||
}
|
||||
@@ -80,8 +85,21 @@ try {
|
||||
throw new Error(`Use with glibc ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
||||
}
|
||||
}
|
||||
if (detectLibc.family === detectLibc.MUSL && detectLibc.version) {
|
||||
if (!semver.satisfies(detectLibc.version, '>=1.1.24 <1.2.0')) {
|
||||
throw new Error(`Use with musl ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
||||
}
|
||||
}
|
||||
|
||||
const supportedNodeVersion = process.env.npm_package_engines_node || require('../package.json').engines.node;
|
||||
if (!semver.satisfies(process.versions.node, supportedNodeVersion)) {
|
||||
throw new Error(`Expected Node.js version ${supportedNodeVersion} but found ${process.versions.node}`);
|
||||
}
|
||||
|
||||
const extension = supportsBrotli ? 'br' : 'gz';
|
||||
|
||||
// Download to per-process temporary file
|
||||
const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.br';
|
||||
const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.' + extension;
|
||||
const tarPathCache = path.join(libvips.cachePath(), tarFilename);
|
||||
if (fs.existsSync(tarPathCache)) {
|
||||
npmLog.info('sharp', `Using cached ${tarPathCache}`);
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const { spawnSync } = require('child_process');
|
||||
|
||||
const { prebuild_upload: hasToken, APPVEYOR_REPO_TAG_NAME, TRAVIS_TAG } = process.env;
|
||||
|
||||
if (hasToken && (APPVEYOR_REPO_TAG_NAME || TRAVIS_TAG)) {
|
||||
spawnSync('node',
|
||||
['./node_modules/prebuild/bin.js', '--runtime', 'napi', '--target', '3'],
|
||||
{ shell: true, stdio: 'inherit' }
|
||||
);
|
||||
}
|
||||
@@ -85,7 +85,7 @@ function extractChannel (channel) {
|
||||
* - sRGB: 0: Red, 1: Green, 2: Blue, 3: Alpha.
|
||||
* - CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha.
|
||||
*
|
||||
* Buffers may be any of the image formats supported by sharp: JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data.
|
||||
* Buffers may be any of the image formats supported by sharp.
|
||||
* For raw pixel input, the `options` object should contain a `raw` attribute, which follows the format of the attribute of the same name in the `sharp()` constructor.
|
||||
*
|
||||
* @param {Array<string|Buffer>|string|Buffer} images - one or more images (file paths, Buffers).
|
||||
|
||||
@@ -57,6 +57,13 @@ function grayscale (grayscale) {
|
||||
/**
|
||||
* Set the output colourspace.
|
||||
* By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
|
||||
*
|
||||
* @example
|
||||
* // Output 16 bits per pixel RGB
|
||||
* await sharp(input)
|
||||
* .toColourspace('rgb16')
|
||||
* .toFile('16-bpp.png')
|
||||
*
|
||||
* @param {string} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/libvips/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568)
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
|
||||
@@ -105,8 +105,9 @@ function composite (images) {
|
||||
input: this._createInputDescriptor(image.input, inputOptions, { allowStream: false }),
|
||||
blend: 'over',
|
||||
tile: false,
|
||||
left: -1,
|
||||
top: -1,
|
||||
left: 0,
|
||||
top: 0,
|
||||
hasOffset: false,
|
||||
gravity: 0,
|
||||
premultiplied: false
|
||||
};
|
||||
@@ -125,21 +126,23 @@ function composite (images) {
|
||||
}
|
||||
}
|
||||
if (is.defined(image.left)) {
|
||||
if (is.integer(image.left) && image.left >= 0) {
|
||||
if (is.integer(image.left)) {
|
||||
composite.left = image.left;
|
||||
} else {
|
||||
throw is.invalidParameterError('left', 'positive integer', image.left);
|
||||
throw is.invalidParameterError('left', 'integer', image.left);
|
||||
}
|
||||
}
|
||||
if (is.defined(image.top)) {
|
||||
if (is.integer(image.top) && image.top >= 0) {
|
||||
if (is.integer(image.top)) {
|
||||
composite.top = image.top;
|
||||
} else {
|
||||
throw is.invalidParameterError('top', 'positive integer', image.top);
|
||||
throw is.invalidParameterError('top', 'integer', image.top);
|
||||
}
|
||||
}
|
||||
if (composite.left !== composite.top && Math.min(composite.left, composite.top) === -1) {
|
||||
if (is.defined(image.top) !== is.defined(image.left)) {
|
||||
throw new Error('Expected both left and top to be set');
|
||||
} else {
|
||||
composite.hasOffset = is.integer(image.top) && is.integer(image.left);
|
||||
}
|
||||
if (is.defined(image.gravity)) {
|
||||
if (is.integer(image.gravity) && is.inRange(image.gravity, 0, 8)) {
|
||||
|
||||
@@ -40,7 +40,7 @@ const debuglog = util.debuglog('sharp');
|
||||
/**
|
||||
* Constructor factory to create an instance of `sharp`, to which further methods are chained.
|
||||
*
|
||||
* JPEG, PNG, WebP or TIFF format image data can be streamed out from this object.
|
||||
* JPEG, PNG, WebP, AVIF or TIFF format image data can be streamed out from this object.
|
||||
* When using Stream based output, derived attributes are available from the `info` event.
|
||||
*
|
||||
* Non-critical problems encountered during processing are emitted as `warning` events.
|
||||
@@ -90,10 +90,39 @@ const debuglog = util.debuglog('sharp');
|
||||
* // Convert an animated GIF to an animated WebP
|
||||
* await sharp('in.gif', { animated: true }).toFile('out.webp');
|
||||
*
|
||||
* @param {(Buffer|string)} [input] - if present, can be
|
||||
* a Buffer containing JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data, or
|
||||
* a String containing the filesystem path to an JPEG, PNG, WebP, GIF, SVG or TIFF image file.
|
||||
* JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
|
||||
* @example
|
||||
* // Read a raw array of pixels and save it to a png
|
||||
* const input = Uint8Array.from([255, 255, 255, 0, 0, 0]); // or Uint8ClampedArray
|
||||
* const image = sharp(input, {
|
||||
* // because the input does not contain its dimensions or how many channels it has
|
||||
* // we need to specify it in the constructor options
|
||||
* raw: {
|
||||
* width: 2,
|
||||
* height: 1,
|
||||
* channels: 3
|
||||
* }
|
||||
* });
|
||||
* await image.toFile('my-two-pixels.png');
|
||||
*
|
||||
* @example
|
||||
* // Generate RGB Gaussian noise
|
||||
* await sharp({
|
||||
* create: {
|
||||
* width: 300,
|
||||
* height: 200,
|
||||
* channels: 3,
|
||||
* noise: {
|
||||
* type: 'gaussian',
|
||||
* mean: 128,
|
||||
* sigma: 30
|
||||
* }
|
||||
* }
|
||||
* }).toFile('noise.png');
|
||||
*
|
||||
* @param {(Buffer|Uint8Array|Uint8ClampedArray|string)} [input] - if present, can be
|
||||
* a Buffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data, or
|
||||
* a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file.
|
||||
* JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
|
||||
* @param {Object} [options] - if present, is an Object with optional attributes.
|
||||
* @param {boolean} [options.failOnError=true] - by default halt processing and raise an error when loading invalid images.
|
||||
* Set this flag to `false` if you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid.
|
||||
@@ -102,20 +131,24 @@ const debuglog = util.debuglog('sharp');
|
||||
* An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF).
|
||||
* @param {boolean} [options.sequentialRead=false] - Set this to `true` to use sequential rather than random access where possible.
|
||||
* This can reduce memory usage and might improve performance on some systems.
|
||||
* @param {number} [options.density=72] - number representing the DPI for vector images.
|
||||
* @param {number} [options.pages=1] - number of pages to extract for multi-page input (GIF, TIFF, PDF), use -1 for all pages.
|
||||
* @param {number} [options.page=0] - page number to start extracting from for multi-page input (GIF, TIFF, PDF), zero based.
|
||||
* @param {number} [options.density=72] - number representing the DPI for vector images in the range 1 to 100000.
|
||||
* @param {number} [options.pages=1] - number of pages to extract for multi-page input (GIF, WebP, AVIF, TIFF, PDF), use -1 for all pages.
|
||||
* @param {number} [options.page=0] - page number to start extracting from for multi-page input (GIF, WebP, AVIF, TIFF, PDF), zero based.
|
||||
* @param {number} [options.level=0] - level to extract from a multi-level input (OpenSlide), zero based.
|
||||
* @param {boolean} [options.animated=false] - Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`).
|
||||
* @param {Object} [options.raw] - describes raw pixel input image data. See `raw()` for pixel ordering.
|
||||
* @param {number} [options.raw.width]
|
||||
* @param {number} [options.raw.height]
|
||||
* @param {number} [options.raw.channels] - 1-4
|
||||
* @param {number} [options.raw.width] - integral number of pixels wide.
|
||||
* @param {number} [options.raw.height] - integral number of pixels high.
|
||||
* @param {number} [options.raw.channels] - integral number of channels, between 1 and 4.
|
||||
* @param {Object} [options.create] - describes a new image to be created.
|
||||
* @param {number} [options.create.width]
|
||||
* @param {number} [options.create.height]
|
||||
* @param {number} [options.create.channels] - 3-4
|
||||
* @param {number} [options.create.width] - integral number of pixels wide.
|
||||
* @param {number} [options.create.height] - integral number of pixels high.
|
||||
* @param {number} [options.create.channels] - integral number of channels, either 3 (RGB) or 4 (RGBA).
|
||||
* @param {string|Object} [options.create.background] - parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
|
||||
* @param {Object} [options.create.noise] - describes a noise to be created.
|
||||
* @param {string} [options.create.noise.type] - type of generated noise, currently only `gaussian` is supported.
|
||||
* @param {number} [options.create.noise.mean] - mean of pixels in generated noise.
|
||||
* @param {number} [options.create.noise.sigma] - standard deviation of pixels in generated noise.
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
@@ -155,6 +188,13 @@ const Sharp = function (input, options) {
|
||||
extendRight: 0,
|
||||
extendBackground: [0, 0, 0, 255],
|
||||
withoutEnlargement: false,
|
||||
affineMatrix: [],
|
||||
affineBackground: [0, 0, 0, 255],
|
||||
affineIdx: 0,
|
||||
affineIdy: 0,
|
||||
affineOdx: 0,
|
||||
affineOdy: 0,
|
||||
affineInterpolator: this.constructor.interpolators.bilinear,
|
||||
kernel: 'lanczos3',
|
||||
fastShrinkOnLoad: true,
|
||||
// operations
|
||||
@@ -226,9 +266,11 @@ const Sharp = function (input, options) {
|
||||
tiffTileWidth: 256,
|
||||
tiffXres: 1.0,
|
||||
tiffYres: 1.0,
|
||||
heifQuality: 80,
|
||||
heifQuality: 50,
|
||||
heifLossless: false,
|
||||
heifCompression: 'hevc',
|
||||
heifCompression: 'av1',
|
||||
heifSpeed: 5,
|
||||
heifChromaSubsampling: '4:2:0',
|
||||
tileSize: 256,
|
||||
tileOverlap: 0,
|
||||
tileContainer: 'fs',
|
||||
@@ -238,6 +280,7 @@ const Sharp = function (input, options) {
|
||||
tileAngle: 0,
|
||||
tileSkipBlanks: -1,
|
||||
tileBackground: [255, 255, 255, 255],
|
||||
tileCentre: false,
|
||||
linearA: 1,
|
||||
linearB: 0,
|
||||
// Function to notify of libvips warnings
|
||||
|
||||
61
lib/input.js
@@ -9,9 +9,9 @@ const sharp = require('../build/Release/sharp.node');
|
||||
* @private
|
||||
*/
|
||||
function _inputOptionsFromObject (obj) {
|
||||
const { raw, density, limitInputPixels, sequentialRead, failOnError } = obj;
|
||||
return [raw, density, limitInputPixels, sequentialRead, failOnError].some(is.defined)
|
||||
? { raw, density, limitInputPixels, sequentialRead, failOnError }
|
||||
const { raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages } = obj;
|
||||
return [raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages].some(is.defined)
|
||||
? { raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages }
|
||||
: undefined;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,9 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
|
||||
} else if (is.buffer(input)) {
|
||||
// Buffer
|
||||
inputDescriptor.buffer = input;
|
||||
} else if (is.uint8Array(input)) {
|
||||
// Uint8Array or Uint8ClampedArray
|
||||
inputDescriptor.buffer = Buffer.from(input.buffer);
|
||||
} else if (is.plainObject(input) && !is.defined(inputOptions)) {
|
||||
// Plain Object descriptor, e.g. create
|
||||
inputOptions = input;
|
||||
@@ -57,10 +60,10 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
|
||||
}
|
||||
// Density
|
||||
if (is.defined(inputOptions.density)) {
|
||||
if (is.inRange(inputOptions.density, 1, 2400)) {
|
||||
if (is.inRange(inputOptions.density, 1, 100000)) {
|
||||
inputDescriptor.density = inputOptions.density;
|
||||
} else {
|
||||
throw is.invalidParameterError('density', 'number between 1 and 2400', inputOptions.density);
|
||||
throw is.invalidParameterError('density', 'number between 1 and 100000', inputOptions.density);
|
||||
}
|
||||
}
|
||||
// limitInputPixels
|
||||
@@ -134,22 +137,50 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
|
||||
is.object(inputOptions.create) &&
|
||||
is.integer(inputOptions.create.width) && inputOptions.create.width > 0 &&
|
||||
is.integer(inputOptions.create.height) && inputOptions.create.height > 0 &&
|
||||
is.integer(inputOptions.create.channels) && is.inRange(inputOptions.create.channels, 3, 4) &&
|
||||
is.defined(inputOptions.create.background)
|
||||
is.integer(inputOptions.create.channels)
|
||||
) {
|
||||
inputDescriptor.createWidth = inputOptions.create.width;
|
||||
inputDescriptor.createHeight = inputOptions.create.height;
|
||||
inputDescriptor.createChannels = inputOptions.create.channels;
|
||||
const background = color(inputOptions.create.background);
|
||||
inputDescriptor.createBackground = [
|
||||
background.red(),
|
||||
background.green(),
|
||||
background.blue(),
|
||||
Math.round(background.alpha() * 255)
|
||||
];
|
||||
// Noise
|
||||
if (is.defined(inputOptions.create.noise)) {
|
||||
if (!is.object(inputOptions.create.noise)) {
|
||||
throw new Error('Expected noise to be an object');
|
||||
}
|
||||
if (!is.inArray(inputOptions.create.noise.type, ['gaussian'])) {
|
||||
throw new Error('Only gaussian noise is supported at the moment');
|
||||
}
|
||||
if (!is.inRange(inputOptions.create.channels, 1, 4)) {
|
||||
throw is.invalidParameterError('create.channels', 'number between 1 and 4', inputOptions.create.channels);
|
||||
}
|
||||
inputDescriptor.createNoiseType = inputOptions.create.noise.type;
|
||||
if (is.number(inputOptions.create.noise.mean) && is.inRange(inputOptions.create.noise.mean, 0, 10000)) {
|
||||
inputDescriptor.createNoiseMean = inputOptions.create.noise.mean;
|
||||
} else {
|
||||
throw is.invalidParameterError('create.noise.mean', 'number between 0 and 10000', inputOptions.create.noise.mean);
|
||||
}
|
||||
if (is.number(inputOptions.create.noise.sigma) && is.inRange(inputOptions.create.noise.sigma, 0, 10000)) {
|
||||
inputDescriptor.createNoiseSigma = inputOptions.create.noise.sigma;
|
||||
} else {
|
||||
throw is.invalidParameterError('create.noise.sigma', 'number between 0 and 10000', inputOptions.create.noise.sigma);
|
||||
}
|
||||
} else if (is.defined(inputOptions.create.background)) {
|
||||
if (!is.inRange(inputOptions.create.channels, 3, 4)) {
|
||||
throw is.invalidParameterError('create.channels', 'number between 3 and 4', inputOptions.create.channels);
|
||||
}
|
||||
const background = color(inputOptions.create.background);
|
||||
inputDescriptor.createBackground = [
|
||||
background.red(),
|
||||
background.green(),
|
||||
background.blue(),
|
||||
Math.round(background.alpha() * 255)
|
||||
];
|
||||
} else {
|
||||
throw new Error('Expected valid noise or background to create a new input image');
|
||||
}
|
||||
delete inputDescriptor.buffer;
|
||||
} else {
|
||||
throw new Error('Expected width, height, channels and background to create a new input image');
|
||||
throw new Error('Expected valid width, height and channels to create a new input image');
|
||||
}
|
||||
}
|
||||
} else if (is.defined(inputOptions)) {
|
||||
|
||||
10
lib/is.js
@@ -48,6 +48,15 @@ const buffer = function (val) {
|
||||
return val instanceof Buffer;
|
||||
};
|
||||
|
||||
/**
|
||||
* Is this value a Uint8Array or Uint8ClampedArray object?
|
||||
* @private
|
||||
*/
|
||||
const uint8Array = function (val) {
|
||||
// allow both since Uint8ClampedArray simply clamps the values between 0-255
|
||||
return val instanceof Uint8Array || val instanceof Uint8ClampedArray;
|
||||
};
|
||||
|
||||
/**
|
||||
* Is this value a non-empty string?
|
||||
* @private
|
||||
@@ -110,6 +119,7 @@ module.exports = {
|
||||
fn: fn,
|
||||
bool: bool,
|
||||
buffer: buffer,
|
||||
uint8Array: uint8Array,
|
||||
string: string,
|
||||
number: number,
|
||||
integer: integer,
|
||||
|
||||
@@ -37,10 +37,20 @@ const cachePath = function () {
|
||||
return libvipsCachePath;
|
||||
};
|
||||
|
||||
const isRosetta = function () {
|
||||
/* istanbul ignore next */
|
||||
if (process.platform === 'darwin' && process.arch === 'x64') {
|
||||
const translated = spawnSync('sysctl sysctl.proc_translated', spawnSyncOptions).stdout;
|
||||
return (translated || '').trim() === 'sysctl.proc_translated: 1';
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const globalLibvipsVersion = function () {
|
||||
if (process.platform !== 'win32') {
|
||||
const globalLibvipsVersion = spawnSync(`PKG_CONFIG_PATH="${pkgConfigPath()}" pkg-config --modversion vips-cpp`, spawnSyncOptions).stdout || '';
|
||||
return globalLibvipsVersion.trim();
|
||||
const globalLibvipsVersion = spawnSync(`PKG_CONFIG_PATH="${pkgConfigPath()}" pkg-config --modversion vips-cpp`, spawnSyncOptions).stdout;
|
||||
/* istanbul ignore next */
|
||||
return (globalLibvipsVersion || '').trim();
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
@@ -81,7 +91,10 @@ const useGlobalLibvips = function () {
|
||||
if (Boolean(env.SHARP_IGNORE_GLOBAL_LIBVIPS) === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (isRosetta()) {
|
||||
return false;
|
||||
}
|
||||
const globalVipsVersion = globalLibvipsVersion();
|
||||
return !!globalVipsVersion && /* istanbul ignore next */
|
||||
semver.gte(globalVipsVersion, minimumLibvipsVersion);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const { flatten: flattenArray } = require('array-flatten');
|
||||
const color = require('color');
|
||||
const is = require('./is');
|
||||
|
||||
@@ -82,6 +83,103 @@ function flop (flop) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an affine transform on an image. This operation will always occur after resizing, extraction and rotation, if any.
|
||||
*
|
||||
* You must provide an array of length 4 or a 2x2 affine transformation matrix.
|
||||
* By default, new pixels are filled with a black background. You can provide a background color with the `background` option.
|
||||
* A particular interpolator may also be specified. Set the `interpolator` option to an attribute of the `sharp.interpolator` Object e.g. `sharp.interpolator.nohalo`.
|
||||
*
|
||||
* In the case of a 2x2 matrix, the transform is:
|
||||
* - X = `matrix[0, 0]` \* (x + `idx`) + `matrix[0, 1]` \* (y + `idy`) + `odx`
|
||||
* - Y = `matrix[1, 0]` \* (x + `idx`) + `matrix[1, 1]` \* (y + `idy`) + `ody`
|
||||
*
|
||||
* where:
|
||||
* - x and y are the coordinates in input image.
|
||||
* - X and Y are the coordinates in output image.
|
||||
* - (0,0) is the upper left corner.
|
||||
*
|
||||
* @since 0.27.0
|
||||
*
|
||||
* @example
|
||||
* const pipeline = sharp()
|
||||
* .affine([[1, 0.3], [0.1, 0.7]], {
|
||||
* background: 'white',
|
||||
* interpolate: sharp.interpolators.nohalo
|
||||
* })
|
||||
* .toBuffer((err, outputBuffer, info) => {
|
||||
* // outputBuffer contains the transformed image
|
||||
* // info.width and info.height contain the new dimensions
|
||||
* });
|
||||
*
|
||||
* inputStream
|
||||
* .pipe(pipeline);
|
||||
*
|
||||
* @param {Array<Array<number>>|Array<number>} matrix - affine transformation matrix
|
||||
* @param {Object} [options] - if present, is an Object with optional attributes.
|
||||
* @param {String|Object} [options.background="#000000"] - parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
|
||||
* @param {Number} [options.idx=0] - input horizontal offset
|
||||
* @param {Number} [options.idy=0] - input vertical offset
|
||||
* @param {Number} [options.odx=0] - output horizontal offset
|
||||
* @param {Number} [options.ody=0] - output vertical offset
|
||||
* @param {String} [options.interpolator=sharp.interpolators.bicubic] - interpolator
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
function affine (matrix, options) {
|
||||
const flatMatrix = flattenArray(matrix);
|
||||
if (flatMatrix.length === 4 && flatMatrix.every(is.number)) {
|
||||
this.options.affineMatrix = flatMatrix;
|
||||
} else {
|
||||
throw is.invalidParameterError('matrix', '1x4 or 2x2 array', matrix);
|
||||
}
|
||||
|
||||
if (is.defined(options)) {
|
||||
if (is.object(options)) {
|
||||
this._setBackgroundColourOption('affineBackground', options.background);
|
||||
if (is.defined(options.idx)) {
|
||||
if (is.number(options.idx)) {
|
||||
this.options.affineIdx = options.idx;
|
||||
} else {
|
||||
throw is.invalidParameterError('options.idx', 'number', options.idx);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.idy)) {
|
||||
if (is.number(options.idy)) {
|
||||
this.options.affineIdy = options.idy;
|
||||
} else {
|
||||
throw is.invalidParameterError('options.idy', 'number', options.idy);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.odx)) {
|
||||
if (is.number(options.odx)) {
|
||||
this.options.affineOdx = options.odx;
|
||||
} else {
|
||||
throw is.invalidParameterError('options.odx', 'number', options.odx);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.ody)) {
|
||||
if (is.number(options.ody)) {
|
||||
this.options.affineOdy = options.ody;
|
||||
} else {
|
||||
throw is.invalidParameterError('options.ody', 'number', options.ody);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.interpolator)) {
|
||||
if (is.inArray(options.interpolator, Object.values(this.constructor.interpolators))) {
|
||||
this.options.affineInterpolator = options.interpolator;
|
||||
} else {
|
||||
throw is.invalidParameterError('options.interpolator', 'valid interpolator name', options.interpolator);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw is.invalidParameterError('options', 'object', options);
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sharpen the image.
|
||||
* When used without parameters, performs a fast, mild sharpen of the output image.
|
||||
@@ -482,6 +580,7 @@ module.exports = function (Sharp) {
|
||||
rotate,
|
||||
flip,
|
||||
flop,
|
||||
affine,
|
||||
sharpen,
|
||||
median,
|
||||
blur,
|
||||
|
||||
126
lib/output.js
@@ -6,6 +6,7 @@ const sharp = require('../build/Release/sharp.node');
|
||||
const formats = new Map([
|
||||
['heic', 'heif'],
|
||||
['heif', 'heif'],
|
||||
['avif', 'avif'],
|
||||
['jpeg', 'jpeg'],
|
||||
['jpg', 'jpeg'],
|
||||
['png', 'png'],
|
||||
@@ -15,11 +16,13 @@ const formats = new Map([
|
||||
['gif', 'gif']
|
||||
]);
|
||||
|
||||
const errMagickSave = new Error('GIF output requires libvips with support for ImageMagick');
|
||||
|
||||
/**
|
||||
* Write output image data to a file.
|
||||
*
|
||||
* If an explicit output format is not selected, it will be inferred from the extension,
|
||||
* with JPEG, PNG, WebP, TIFF, DZI, and libvips' V format supported.
|
||||
* with JPEG, PNG, WebP, AVIF, TIFF, DZI, and libvips' V format supported.
|
||||
* Note that raw pixel data is only supported for buffer output.
|
||||
*
|
||||
* By default all metadata will be removed, which includes EXIF-based orientation.
|
||||
@@ -46,32 +49,30 @@ const formats = new Map([
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
function toFile (fileOut, callback) {
|
||||
if (!fileOut || fileOut.length === 0) {
|
||||
const errOutputInvalid = new Error('Missing output file path');
|
||||
let err;
|
||||
if (!is.string(fileOut)) {
|
||||
err = new Error('Missing output file path');
|
||||
} else if (this.options.input.file === fileOut) {
|
||||
err = new Error('Cannot use same file for input and output');
|
||||
} else if (this.options.formatOut === 'input' && fileOut.toLowerCase().endsWith('.gif') && !this.constructor.format.magick.output.file) {
|
||||
err = errMagickSave;
|
||||
}
|
||||
if (err) {
|
||||
if (is.fn(callback)) {
|
||||
callback(errOutputInvalid);
|
||||
callback(err);
|
||||
} else {
|
||||
return Promise.reject(errOutputInvalid);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
} else {
|
||||
if (this.options.input.file === fileOut) {
|
||||
const errOutputIsInput = new Error('Cannot use same file for input and output');
|
||||
if (is.fn(callback)) {
|
||||
callback(errOutputIsInput);
|
||||
} else {
|
||||
return Promise.reject(errOutputIsInput);
|
||||
}
|
||||
} else {
|
||||
this.options.fileOut = fileOut;
|
||||
return this._pipeline(callback);
|
||||
}
|
||||
this.options.fileOut = fileOut;
|
||||
return this._pipeline(callback);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write output to a Buffer.
|
||||
* JPEG, PNG, WebP, TIFF and RAW output are supported.
|
||||
* JPEG, PNG, WebP, AVIF, TIFF and raw pixel data output are supported.
|
||||
*
|
||||
* If no explicit format is set, the output format will match the input image, except GIF and SVG input which become PNG output.
|
||||
*
|
||||
@@ -103,6 +104,20 @@ function toFile (fileOut, callback) {
|
||||
* .then(({ data, info }) => { ... })
|
||||
* .catch(err => { ... });
|
||||
*
|
||||
* @example
|
||||
* const data = await sharp('my-image.jpg')
|
||||
* // output the raw pixels
|
||||
* .raw()
|
||||
* .toBuffer();
|
||||
*
|
||||
* // create a more type safe way to work with the raw pixel data
|
||||
* // this will not copy the data, instead it will change `data`s underlying ArrayBuffer
|
||||
* // so `data` and `pixelArray` point to the same memory location
|
||||
* const pixelArray = new Uint8ClampedArray(data.buffer);
|
||||
*
|
||||
* // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
|
||||
* await sharp(pixelArray).toFile('my-changed-image.jpg');
|
||||
*
|
||||
* @param {Object} [options]
|
||||
* @param {boolean} [options.resolveWithObject] Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
|
||||
* @param {Function} [callback]
|
||||
@@ -173,7 +188,7 @@ function withMetadata (options) {
|
||||
* @throws {Error} unsupported format or options
|
||||
*/
|
||||
function toFormat (format, options) {
|
||||
const actualFormat = formats.get(is.object(format) && is.string(format.id) ? format.id : format);
|
||||
const actualFormat = formats.get((is.object(format) && is.string(format.id) ? format.id : format).toLowerCase());
|
||||
if (!actualFormat) {
|
||||
throw is.invalidParameterError('format', `one of: ${[...formats.keys()].join(', ')}`, format);
|
||||
}
|
||||
@@ -411,7 +426,7 @@ function webp (options) {
|
||||
/* istanbul ignore next */
|
||||
function gif (options) {
|
||||
if (!this.constructor.format.magick.output.buffer) {
|
||||
throw new Error('The gif operation requires libvips to have been installed with support for ImageMagick');
|
||||
throw errMagickSave;
|
||||
}
|
||||
trySetAnimationOptions(options, this.options);
|
||||
return this._updateFormatOut('gif', options);
|
||||
@@ -471,15 +486,15 @@ function trySetAnimationOptions (source, target) {
|
||||
* @param {Object} [options] - output options
|
||||
* @param {number} [options.quality=80] - quality, integer 1-100
|
||||
* @param {boolean} [options.force=true] - force TIFF output, otherwise attempt to use input format
|
||||
* @param {boolean} [options.compression='jpeg'] - compression options: lzw, deflate, jpeg, ccittfax4
|
||||
* @param {boolean} [options.predictor='horizontal'] - compression predictor options: none, horizontal, float
|
||||
* @param {string} [options.compression='jpeg'] - compression options: lzw, deflate, jpeg, ccittfax4
|
||||
* @param {string} [options.predictor='horizontal'] - compression predictor options: none, horizontal, float
|
||||
* @param {boolean} [options.pyramid=false] - write an image pyramid
|
||||
* @param {boolean} [options.tile=false] - write a tiled tiff
|
||||
* @param {boolean} [options.tileWidth=256] - horizontal tile size
|
||||
* @param {boolean} [options.tileHeight=256] - vertical tile size
|
||||
* @param {number} [options.tileWidth=256] - horizontal tile size
|
||||
* @param {number} [options.tileHeight=256] - vertical tile size
|
||||
* @param {number} [options.xres=1.0] - horizontal resolution in pixels/mm
|
||||
* @param {number} [options.yres=1.0] - vertical resolution in pixels/mm
|
||||
* @param {boolean} [options.bitdepth=8] - reduce bitdepth to 1, 2 or 4 bit
|
||||
* @param {number} [options.bitdepth=8] - reduce bitdepth to 1, 2 or 4 bit
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
*/
|
||||
@@ -556,29 +571,44 @@ function tiff (options) {
|
||||
return this._updateFormatOut('tiff', options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use these AVIF options for output image.
|
||||
*
|
||||
* Whilst it is possible to create AVIF images smaller than 16x16 pixels,
|
||||
* most web browsers do not display these properly.
|
||||
*
|
||||
* @since 0.27.0
|
||||
*
|
||||
* @param {Object} [options] - output options
|
||||
* @param {number} [options.quality=50] - quality, integer 1-100
|
||||
* @param {boolean} [options.lossless=false] - use lossless compression
|
||||
* @param {number} [options.speed=5] - CPU effort vs file size, 0 (slowest/smallest) to 8 (fastest/largest)
|
||||
* @param {string} [options.chromaSubsampling='4:2:0'] - set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling, requires libvips v8.11.0
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
*/
|
||||
function avif (options) {
|
||||
return this.heif({ ...options, compression: 'av1' });
|
||||
}
|
||||
|
||||
/**
|
||||
* Use these HEIF options for output image.
|
||||
*
|
||||
* Support for HEIF (HEIC/AVIF) is experimental.
|
||||
* Do not use this in production systems.
|
||||
*
|
||||
* Requires a custom, globally-installed libvips compiled with support for libheif.
|
||||
*
|
||||
* Most versions of libheif support only the patent-encumbered HEVC compression format.
|
||||
* Support for patent-encumbered HEIC images requires the use of a
|
||||
* globally-installed libvips compiled with support for libheif, libde265 and x265.
|
||||
*
|
||||
* @since 0.23.0
|
||||
*
|
||||
* @param {Object} [options] - output options
|
||||
* @param {number} [options.quality=80] - quality, integer 1-100
|
||||
* @param {boolean} [options.compression='hevc'] - compression format: hevc, avc, jpeg, av1
|
||||
* @param {number} [options.quality=50] - quality, integer 1-100
|
||||
* @param {string} [options.compression='av1'] - compression format: av1, hevc
|
||||
* @param {boolean} [options.lossless=false] - use lossless compression
|
||||
* @param {number} [options.speed=5] - CPU effort vs file size, 0 (slowest/smallest) to 8 (fastest/largest)
|
||||
* @param {string} [options.chromaSubsampling='4:2:0'] - set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling, requires libvips v8.11.0
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
*/
|
||||
function heif (options) {
|
||||
if (!this.constructor.format.heif.output.buffer) {
|
||||
throw new Error('The heif operation requires libvips to have been installed with support for libheif');
|
||||
}
|
||||
if (is.object(options)) {
|
||||
if (is.defined(options.quality)) {
|
||||
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
|
||||
@@ -595,10 +625,24 @@ function heif (options) {
|
||||
}
|
||||
}
|
||||
if (is.defined(options.compression)) {
|
||||
if (is.string(options.compression) && is.inArray(options.compression, ['hevc', 'avc', 'jpeg', 'av1'])) {
|
||||
if (is.string(options.compression) && is.inArray(options.compression, ['av1', 'hevc'])) {
|
||||
this.options.heifCompression = options.compression;
|
||||
} else {
|
||||
throw is.invalidParameterError('compression', 'one of: hevc, avc, jpeg, av1', options.compression);
|
||||
throw is.invalidParameterError('compression', 'one of: av1, hevc', options.compression);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.speed)) {
|
||||
if (is.integer(options.speed) && is.inRange(options.speed, 0, 8)) {
|
||||
this.options.heifSpeed = options.speed;
|
||||
} else {
|
||||
throw is.invalidParameterError('speed', 'integer between 0 and 8', options.speed);
|
||||
}
|
||||
}
|
||||
if (is.defined(options.chromaSubsampling)) {
|
||||
if (is.string(options.chromaSubsampling) && is.inArray(options.chromaSubsampling, ['4:2:0', '4:4:4'])) {
|
||||
this.options.heifChromaSubsampling = options.chromaSubsampling;
|
||||
} else {
|
||||
throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -658,6 +702,8 @@ function raw () {
|
||||
* @param {number} [options.skipBlanks=-1] threshold to skip tile generation, a value 0 - 255 for 8-bit images or 0 - 65535 for 16-bit images
|
||||
* @param {string} [options.container='fs'] tile container, with value `fs` (filesystem) or `zip` (compressed file).
|
||||
* @param {string} [options.layout='dz'] filesystem layout, possible values are `dz`, `iiif`, `zoomify` or `google`.
|
||||
* @param {boolean} [options.centre=false] centre image in tile.
|
||||
* @param {boolean} [options.center=false] alternative spelling of centre.
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
@@ -726,6 +772,11 @@ function tile (options) {
|
||||
} else if (is.defined(options.layout) && options.layout === 'google') {
|
||||
this.options.tileSkipBlanks = 5;
|
||||
}
|
||||
// Center image in tile
|
||||
const centre = is.bool(options.center) ? options.center : options.centre;
|
||||
if (is.defined(centre)) {
|
||||
this._setBooleanOption('tileCentre', centre);
|
||||
}
|
||||
}
|
||||
// Format
|
||||
if (is.inArray(this.options.formatOut, ['jpeg', 'png', 'webp'])) {
|
||||
@@ -884,6 +935,7 @@ module.exports = function (Sharp) {
|
||||
png,
|
||||
webp,
|
||||
tiff,
|
||||
avif,
|
||||
heif,
|
||||
gif,
|
||||
raw,
|
||||
|
||||
@@ -386,7 +386,8 @@ function extract (options) {
|
||||
* Trim "boring" pixels from all edges that contain values similar to the top-left pixel.
|
||||
* Images consisting entirely of a single colour will calculate "boring" using the alpha channel, if any.
|
||||
*
|
||||
* The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties.
|
||||
* The `info` response Object, obtained from callback of `.toFile()` or `.toBuffer()`,
|
||||
* will contain `trimOffsetLeft` and `trimOffsetTop` properties.
|
||||
*
|
||||
* @param {number} [threshold=10] the allowed difference from the top-left pixel, a number greater than zero.
|
||||
* @returns {Sharp}
|
||||
|
||||
@@ -13,6 +13,26 @@ const sharp = require('../build/Release/sharp.node');
|
||||
*/
|
||||
const format = sharp.format();
|
||||
|
||||
/**
|
||||
* An Object containing the available interpolators and their proper values
|
||||
* @readonly
|
||||
* @enum {string}
|
||||
*/
|
||||
const interpolators = {
|
||||
/** [Nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation). Suitable for image enlargement only. */
|
||||
nearest: 'nearest',
|
||||
/** [Bilinear interpolation](http://en.wikipedia.org/wiki/Bilinear_interpolation). Faster than bicubic but with less smooth results. */
|
||||
bilinear: 'bilinear',
|
||||
/** [Bicubic interpolation](http://en.wikipedia.org/wiki/Bicubic_interpolation) (the default). */
|
||||
bicubic: 'bicubic',
|
||||
/** [LBB interpolation](https://github.com/jcupitt/libvips/blob/master/libvips/resample/lbb.cpp#L100). Prevents some "[acutance](http://en.wikipedia.org/wiki/Acutance)" but typically reduces performance by a factor of 2. */
|
||||
locallyBoundedBicubic: 'lbb',
|
||||
/** [Nohalo interpolation](http://eprints.soton.ac.uk/268086/). Prevents acutance but typically reduces performance by a factor of 3. */
|
||||
nohalo: 'nohalo',
|
||||
/** [VSQBS interpolation](https://github.com/jcupitt/libvips/blob/master/libvips/resample/vsqbs.cpp#L48). Prevents "staircasing" when enlarging. */
|
||||
vertexSplitQuadraticBasisSpline: 'vsqbs'
|
||||
};
|
||||
|
||||
/**
|
||||
* An Object containing the version numbers of libvips and its dependencies.
|
||||
* @member
|
||||
@@ -137,15 +157,12 @@ simd(true);
|
||||
* @private
|
||||
*/
|
||||
module.exports = function (Sharp) {
|
||||
[
|
||||
cache,
|
||||
concurrency,
|
||||
counters,
|
||||
simd
|
||||
].forEach(function (f) {
|
||||
Sharp[f.name] = f;
|
||||
});
|
||||
Sharp.cache = cache;
|
||||
Sharp.concurrency = concurrency;
|
||||
Sharp.counters = counters;
|
||||
Sharp.simd = simd;
|
||||
Sharp.format = format;
|
||||
Sharp.interpolators = interpolators;
|
||||
Sharp.versions = versions;
|
||||
Sharp.queue = queue;
|
||||
};
|
||||
|
||||
42
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "sharp",
|
||||
"description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP and TIFF images",
|
||||
"version": "0.26.0",
|
||||
"description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images",
|
||||
"version": "0.27.2",
|
||||
"author": "Lovell Fuller <npm@lovell.info>",
|
||||
"homepage": "https://github.com/lovell/sharp",
|
||||
"contributors": [
|
||||
@@ -69,17 +69,22 @@
|
||||
"Edward Silverton <e.silverton@gmail.com>",
|
||||
"Roman Malieiev <aromaleev@gmail.com>",
|
||||
"Tomas Szabo <tomas.szabo@deftomat.com>",
|
||||
"Robert O'Rourke <robert@o-rourke.org>"
|
||||
"Robert O'Rourke <robert@o-rourke.org>",
|
||||
"Guillermo Alfonso Varela Chouciño <guillevch@gmail.com>",
|
||||
"Christian Flintrup <chr@gigahost.dk>",
|
||||
"Manan Jadhav <manan@motionden.com>",
|
||||
"Leon Radley <leon@radley.se>",
|
||||
"alza54 <alza54@thiocod.in>"
|
||||
],
|
||||
"scripts": {
|
||||
"install": "(node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)",
|
||||
"clean": "rm -rf node_modules/ build/ vendor/ .nyc_output/ coverage/ test/fixtures/output.*",
|
||||
"test": "semistandard && cpplint && npm run test-unit && npm run test-licensing && node install/prebuild-ci",
|
||||
"test": "semistandard && cpplint && npm run test-unit && npm run test-licensing",
|
||||
"test-unit": "nyc --reporter=lcov --branches=99 mocha --slow=5000 --timeout=60000 ./test/unit/*.js",
|
||||
"test-licensing": "license-checker --production --summary --onlyAllow=\"Apache-2.0;BSD;ISC;MIT\"",
|
||||
"test-coverage": "./test/coverage/report.sh",
|
||||
"test-leak": "./test/leak/leak.sh",
|
||||
"docs-build": "documentation lint lib && for m in constructor input resize composite operation colour channel output utility; do documentation build --shallow --format=md --markdown-toc=false lib/$m.js >docs/api-$m.md; done && node docs/search-index/build",
|
||||
"docs-build": "documentation lint lib && node docs/build && node docs/search-index/build",
|
||||
"docs-serve": "cd docs && npx serve",
|
||||
"docs-publish": "cd docs && npx firebase-tools deploy --project pixelplumbing --only hosting:pixelplumbing-sharp"
|
||||
},
|
||||
@@ -87,7 +92,6 @@
|
||||
"files": [
|
||||
"binding.gyp",
|
||||
"install/**",
|
||||
"!install/prebuild-ci.js",
|
||||
"lib/**",
|
||||
"src/**"
|
||||
],
|
||||
@@ -99,6 +103,7 @@
|
||||
"jpeg",
|
||||
"png",
|
||||
"webp",
|
||||
"avif",
|
||||
"tiff",
|
||||
"gif",
|
||||
"svg",
|
||||
@@ -112,39 +117,40 @@
|
||||
"vips"
|
||||
],
|
||||
"dependencies": {
|
||||
"color": "^3.1.2",
|
||||
"array-flatten": "^3.0.0",
|
||||
"color": "^3.1.3",
|
||||
"detect-libc": "^1.0.3",
|
||||
"node-addon-api": "^3.0.0",
|
||||
"node-addon-api": "^3.1.0",
|
||||
"npmlog": "^4.1.2",
|
||||
"prebuild-install": "^5.3.5",
|
||||
"semver": "^7.3.2",
|
||||
"prebuild-install": "^6.0.1",
|
||||
"semver": "^7.3.4",
|
||||
"simple-get": "^4.0.0",
|
||||
"tar-fs": "^2.1.0",
|
||||
"tar-fs": "^2.1.1",
|
||||
"tunnel-agent": "^0.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"async": "^3.2.0",
|
||||
"cc": "^2.0.1",
|
||||
"decompress-zip": "^0.3.2",
|
||||
"documentation": "^13.0.2",
|
||||
"cc": "^3.0.1",
|
||||
"decompress-zip": "^0.3.3",
|
||||
"documentation": "^13.1.1",
|
||||
"exif-reader": "^1.0.3",
|
||||
"icc": "^2.0.0",
|
||||
"license-checker": "^25.0.1",
|
||||
"mocha": "^8.1.1",
|
||||
"mocha": "^8.3.0",
|
||||
"mock-fs": "^4.13.0",
|
||||
"nyc": "^15.1.0",
|
||||
"prebuild": "^10.0.1",
|
||||
"rimraf": "^3.0.2",
|
||||
"semistandard": "^14.2.3"
|
||||
"semistandard": "^16.0.0"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"config": {
|
||||
"libvips": "8.10.0",
|
||||
"libvips": "8.10.5",
|
||||
"runtime": "napi",
|
||||
"target": 3
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.16.0"
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://opencollective.com/libvips"
|
||||
|
||||
138
src/common.cc
@@ -54,13 +54,13 @@ namespace sharp {
|
||||
bool AttrAsBool(Napi::Object obj, std::string attr) {
|
||||
return obj.Get(attr).As<Napi::Boolean>().Value();
|
||||
}
|
||||
std::vector<double> AttrAsRgba(Napi::Object obj, std::string attr) {
|
||||
Napi::Array background = obj.Get(attr).As<Napi::Array>();
|
||||
std::vector<double> rgba(background.Length());
|
||||
for (unsigned int i = 0; i < background.Length(); i++) {
|
||||
rgba[i] = AttrAsDouble(background, i);
|
||||
std::vector<double> AttrAsVectorOfDouble(Napi::Object obj, std::string attr) {
|
||||
Napi::Array napiArray = obj.Get(attr).As<Napi::Array>();
|
||||
std::vector<double> vectorOfDouble(napiArray.Length());
|
||||
for (unsigned int i = 0; i < napiArray.Length(); i++) {
|
||||
vectorOfDouble[i] = AttrAsDouble(napiArray, i);
|
||||
}
|
||||
return rgba;
|
||||
return vectorOfDouble;
|
||||
}
|
||||
std::vector<int32_t> AttrAsInt32Vector(Napi::Object obj, std::string attr) {
|
||||
Napi::Array array = obj.Get(attr).As<Napi::Array>();
|
||||
@@ -109,7 +109,13 @@ namespace sharp {
|
||||
descriptor->createChannels = AttrAsUint32(input, "createChannels");
|
||||
descriptor->createWidth = AttrAsUint32(input, "createWidth");
|
||||
descriptor->createHeight = AttrAsUint32(input, "createHeight");
|
||||
descriptor->createBackground = AttrAsRgba(input, "createBackground");
|
||||
if (HasAttr(input, "createNoiseType")) {
|
||||
descriptor->createNoiseType = AttrAsStr(input, "createNoiseType");
|
||||
descriptor->createNoiseMean = AttrAsDouble(input, "createNoiseMean");
|
||||
descriptor->createNoiseSigma = AttrAsDouble(input, "createNoiseSigma");
|
||||
} else {
|
||||
descriptor->createBackground = AttrAsVectorOfDouble(input, "createBackground");
|
||||
}
|
||||
}
|
||||
// Limit input images to a given number of pixels, where pixels = width * height
|
||||
descriptor->limitInputPixels = AttrAsUint32(input, "limitInputPixels");
|
||||
@@ -180,6 +186,7 @@ namespace sharp {
|
||||
case ImageType::OPENSLIDE: id = "openslide"; break;
|
||||
case ImageType::PPM: id = "ppm"; break;
|
||||
case ImageType::FITS: id = "fits"; break;
|
||||
case ImageType::EXR: id = "exr"; break;
|
||||
case ImageType::VIPS: id = "vips"; break;
|
||||
case ImageType::RAW: id = "raw"; break;
|
||||
case ImageType::UNKNOWN: id = "unknown"; break;
|
||||
@@ -188,30 +195,38 @@ namespace sharp {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate this table with something like:
|
||||
*
|
||||
* $ vips -l foreign | grep -i load | awk '{ print $2, $1; }'
|
||||
*
|
||||
* Plus a bit of editing.
|
||||
*/
|
||||
std::map<std::string, ImageType> loaderToType = {
|
||||
{ "jpegload", ImageType::JPEG },
|
||||
{ "jpegload_buffer", ImageType::JPEG },
|
||||
{ "pngload", ImageType::PNG },
|
||||
{ "pngload_buffer", ImageType::PNG },
|
||||
{ "webpload", ImageType::WEBP },
|
||||
{ "webpload_buffer", ImageType::WEBP },
|
||||
{ "tiffload", ImageType::TIFF },
|
||||
{ "tiffload_buffer", ImageType::TIFF },
|
||||
{ "gifload", ImageType::GIF },
|
||||
{ "gifload_buffer", ImageType::GIF },
|
||||
{ "svgload", ImageType::SVG },
|
||||
{ "svgload_buffer", ImageType::SVG },
|
||||
{ "heifload", ImageType::HEIF },
|
||||
{ "heifload_buffer", ImageType::HEIF },
|
||||
{ "pdfload", ImageType::PDF },
|
||||
{ "pdfload_buffer", ImageType::PDF },
|
||||
{ "magickload", ImageType::MAGICK },
|
||||
{ "magickload_buffer", ImageType::MAGICK },
|
||||
{ "openslideload", ImageType::OPENSLIDE },
|
||||
{ "ppmload", ImageType::PPM },
|
||||
{ "fitsload", ImageType::FITS },
|
||||
{ "vipsload", ImageType::VIPS },
|
||||
{ "rawload", ImageType::RAW }
|
||||
{ "VipsForeignLoadJpegFile", ImageType::JPEG },
|
||||
{ "VipsForeignLoadJpegBuffer", ImageType::JPEG },
|
||||
{ "VipsForeignLoadPngFile", ImageType::PNG },
|
||||
{ "VipsForeignLoadPngBuffer", ImageType::PNG },
|
||||
{ "VipsForeignLoadWebpFile", ImageType::WEBP },
|
||||
{ "VipsForeignLoadWebpBuffer", ImageType::WEBP },
|
||||
{ "VipsForeignLoadTiffFile", ImageType::TIFF },
|
||||
{ "VipsForeignLoadTiffBuffer", ImageType::TIFF },
|
||||
{ "VipsForeignLoadGifFile", ImageType::GIF },
|
||||
{ "VipsForeignLoadGifBuffer", ImageType::GIF },
|
||||
{ "VipsForeignLoadSvgFile", ImageType::SVG },
|
||||
{ "VipsForeignLoadSvgBuffer", ImageType::SVG },
|
||||
{ "VipsForeignLoadHeifFile", ImageType::HEIF },
|
||||
{ "VipsForeignLoadHeifBuffer", ImageType::HEIF },
|
||||
{ "VipsForeignLoadPdfFile", ImageType::PDF },
|
||||
{ "VipsForeignLoadPdfBuffer", ImageType::PDF },
|
||||
{ "VipsForeignLoadMagickFile", ImageType::MAGICK },
|
||||
{ "VipsForeignLoadMagickBuffer", ImageType::MAGICK },
|
||||
{ "VipsForeignLoadOpenslide", ImageType::OPENSLIDE },
|
||||
{ "VipsForeignLoadPpmFile", ImageType::PPM },
|
||||
{ "VipsForeignLoadFits", ImageType::FITS },
|
||||
{ "VipsForeignLoadOpenexr", ImageType::EXR },
|
||||
{ "VipsForeignLoadVips", ImageType::VIPS },
|
||||
{ "VipsForeignLoadRaw", ImageType::RAW }
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -221,7 +236,7 @@ namespace sharp {
|
||||
ImageType imageType = ImageType::UNKNOWN;
|
||||
char const *load = vips_foreign_find_load_buffer(buffer, length);
|
||||
if (load != nullptr) {
|
||||
auto it = loaderToType.find(vips_nickname_find(g_type_from_name(load)));
|
||||
auto it = loaderToType.find(load);
|
||||
if (it != loaderToType.end()) {
|
||||
imageType = it->second;
|
||||
}
|
||||
@@ -236,7 +251,7 @@ namespace sharp {
|
||||
ImageType imageType = ImageType::UNKNOWN;
|
||||
char const *load = vips_foreign_find_load(file);
|
||||
if (load != nullptr) {
|
||||
auto it = loaderToType.find(vips_nickname_find(g_type_from_name(load)));
|
||||
auto it = loaderToType.find(load);
|
||||
if (it != loaderToType.end()) {
|
||||
imageType = it->second;
|
||||
}
|
||||
@@ -316,15 +331,35 @@ namespace sharp {
|
||||
} else {
|
||||
if (descriptor->createChannels > 0) {
|
||||
// Create new image
|
||||
std::vector<double> background = {
|
||||
descriptor->createBackground[0],
|
||||
descriptor->createBackground[1],
|
||||
descriptor->createBackground[2]
|
||||
};
|
||||
if (descriptor->createChannels == 4) {
|
||||
background.push_back(descriptor->createBackground[3]);
|
||||
if (descriptor->createNoiseType == "gaussian") {
|
||||
int const channels = descriptor->createChannels;
|
||||
image = VImage::new_matrix(descriptor->createWidth, descriptor->createHeight);
|
||||
std::vector<VImage> bands = {};
|
||||
bands.reserve(channels);
|
||||
for (int _band = 0; _band < channels; _band++) {
|
||||
bands.push_back(image.gaussnoise(
|
||||
descriptor->createWidth,
|
||||
descriptor->createHeight,
|
||||
VImage::option()->set("mean", descriptor->createNoiseMean)->set("sigma", descriptor->createNoiseSigma)));
|
||||
}
|
||||
image = image.bandjoin(bands);
|
||||
image = image.cast(VipsBandFormat::VIPS_FORMAT_UCHAR);
|
||||
if (channels < 3) {
|
||||
image = image.colourspace(VIPS_INTERPRETATION_B_W);
|
||||
} else {
|
||||
image = image.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||
}
|
||||
} else {
|
||||
std::vector<double> background = {
|
||||
descriptor->createBackground[0],
|
||||
descriptor->createBackground[1],
|
||||
descriptor->createBackground[2]
|
||||
};
|
||||
if (descriptor->createChannels == 4) {
|
||||
background.push_back(descriptor->createBackground[3]);
|
||||
}
|
||||
image = VImage::new_matrix(descriptor->createWidth, descriptor->createHeight).new_from_image(background);
|
||||
}
|
||||
image = VImage::new_matrix(descriptor->createWidth, descriptor->createHeight).new_from_image(background);
|
||||
image.get_image()->Type = VIPS_INTERPRETATION_sRGB;
|
||||
imageType = ImageType::RAW;
|
||||
} else {
|
||||
@@ -386,12 +421,7 @@ namespace sharp {
|
||||
Uses colour space interpretation with number of channels to guess this.
|
||||
*/
|
||||
bool HasAlpha(VImage image) {
|
||||
int const bands = image.bands();
|
||||
VipsInterpretation const interpretation = image.interpretation();
|
||||
return (
|
||||
(bands == 2 && interpretation == VIPS_INTERPRETATION_B_W) ||
|
||||
(bands == 4 && interpretation != VIPS_INTERPRETATION_CMYK) ||
|
||||
(bands == 5 && interpretation == VIPS_INTERPRETATION_CMYK));
|
||||
return image.has_alpha();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -485,8 +515,8 @@ namespace sharp {
|
||||
Check the proposed format supports the current dimensions.
|
||||
*/
|
||||
void AssertImageTypeDimensions(VImage image, ImageType const imageType) {
|
||||
const int height = image.get_typeof("pageHeight") == G_TYPE_INT
|
||||
? image.get_int("pageHeight")
|
||||
const int height = image.get_typeof(VIPS_META_PAGE_HEIGHT) == G_TYPE_INT
|
||||
? image.get_int(VIPS_META_PAGE_HEIGHT)
|
||||
: image.height();
|
||||
if (imageType == ImageType::JPEG) {
|
||||
if (image.width() > 65535 || height > 65535) {
|
||||
@@ -656,26 +686,18 @@ namespace sharp {
|
||||
int top = 0;
|
||||
|
||||
// assign only if valid
|
||||
if (x >= 0 && x < (inWidth - outWidth)) {
|
||||
if (x < (inWidth - outWidth)) {
|
||||
left = x;
|
||||
} else if (x >= (inWidth - outWidth)) {
|
||||
left = inWidth - outWidth;
|
||||
}
|
||||
|
||||
if (y >= 0 && y < (inHeight - outHeight)) {
|
||||
if (y < (inHeight - outHeight)) {
|
||||
top = y;
|
||||
} else if (y >= (inHeight - outHeight)) {
|
||||
top = inHeight - outHeight;
|
||||
}
|
||||
|
||||
// the resulting left and top could have been outside the image after calculation from bottom/right edges
|
||||
if (left < 0) {
|
||||
left = 0;
|
||||
}
|
||||
if (top < 0) {
|
||||
top = 0;
|
||||
}
|
||||
|
||||
return std::make_tuple(left, top);
|
||||
}
|
||||
|
||||
|
||||
16
src/common.h
@@ -24,8 +24,10 @@
|
||||
|
||||
// Verify platform and compiler compatibility
|
||||
|
||||
#if (VIPS_MAJOR_VERSION < 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 10))
|
||||
#error "libvips version 8.10.0+ is required - please see https://sharp.pixelplumbing.com/install"
|
||||
#if (VIPS_MAJOR_VERSION < 8) || \
|
||||
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 10) || \
|
||||
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 10 && VIPS_MICRO_VERSION < 5)
|
||||
#error "libvips version 8.10.5+ is required - please see https://sharp.pixelplumbing.com/install"
|
||||
#endif
|
||||
|
||||
#if ((!defined(__clang__)) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6)))
|
||||
@@ -62,6 +64,9 @@ namespace sharp {
|
||||
int createWidth;
|
||||
int createHeight;
|
||||
std::vector<double> createBackground;
|
||||
std::string createNoiseType;
|
||||
double createNoiseMean;
|
||||
double createNoiseSigma;
|
||||
|
||||
InputDescriptor():
|
||||
buffer(nullptr),
|
||||
@@ -80,7 +85,9 @@ namespace sharp {
|
||||
createChannels(0),
|
||||
createWidth(0),
|
||||
createHeight(0),
|
||||
createBackground{ 0.0, 0.0, 0.0, 255.0 } {}
|
||||
createBackground{ 0.0, 0.0, 0.0, 255.0 },
|
||||
createNoiseMean(0.0),
|
||||
createNoiseSigma(0.0) {}
|
||||
};
|
||||
|
||||
// Convenience methods to access the attributes of a Napi::Object
|
||||
@@ -92,7 +99,7 @@ namespace sharp {
|
||||
double AttrAsDouble(Napi::Object obj, std::string attr);
|
||||
double AttrAsDouble(Napi::Object obj, unsigned int const attr);
|
||||
bool AttrAsBool(Napi::Object obj, std::string attr);
|
||||
std::vector<double> AttrAsRgba(Napi::Object obj, std::string attr);
|
||||
std::vector<double> AttrAsVectorOfDouble(Napi::Object obj, std::string attr);
|
||||
std::vector<int32_t> AttrAsInt32Vector(Napi::Object obj, std::string attr);
|
||||
|
||||
// Create an InputDescriptor instance from a Napi::Object describing an input image
|
||||
@@ -111,6 +118,7 @@ namespace sharp {
|
||||
OPENSLIDE,
|
||||
PPM,
|
||||
FITS,
|
||||
EXR,
|
||||
VIPS,
|
||||
RAW,
|
||||
UNKNOWN,
|
||||
|
||||
@@ -74,6 +74,9 @@ class MetadataWorker : public Napi::AsyncWorker {
|
||||
if (image.get_typeof("heif-primary") == G_TYPE_INT) {
|
||||
baton->pagePrimary = image.get_int("heif-primary");
|
||||
}
|
||||
if (image.get_typeof("heif-compression") == VIPS_TYPE_REF_STRING) {
|
||||
baton->compression = image.get_string("heif-compression");
|
||||
}
|
||||
if (image.get_typeof("openslide.level-count") == VIPS_TYPE_REF_STRING) {
|
||||
int const levels = std::stoi(image.get_string("openslide.level-count"));
|
||||
for (int l = 0; l < levels; l++) {
|
||||
@@ -186,6 +189,9 @@ class MetadataWorker : public Napi::AsyncWorker {
|
||||
if (baton->pagePrimary > -1) {
|
||||
info.Set("pagePrimary", baton->pagePrimary);
|
||||
}
|
||||
if (!baton->compression.empty()) {
|
||||
info.Set("compression", baton->compression);
|
||||
}
|
||||
if (!baton->levels.empty()) {
|
||||
int i = 0;
|
||||
Napi::Array levels = Napi::Array::New(env, static_cast<size_t>(baton->levels.size()));
|
||||
|
||||
@@ -39,6 +39,7 @@ struct MetadataBaton {
|
||||
int loop;
|
||||
std::vector<int> delay;
|
||||
int pagePrimary;
|
||||
std::string compression;
|
||||
std::vector<std::pair<int, int>> levels;
|
||||
bool hasProfile;
|
||||
bool hasAlpha;
|
||||
|
||||
@@ -149,8 +149,8 @@ namespace sharp {
|
||||
*/
|
||||
VImage Recomb(VImage image, std::unique_ptr<double[]> const &matrix) {
|
||||
double *m = matrix.get();
|
||||
image = image.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||
return image
|
||||
.colourspace(VIPS_INTERPRETATION_sRGB)
|
||||
.recomb(image.bands() == 3
|
||||
? VImage::new_from_memory(
|
||||
m, 9 * sizeof(double), 3, 3, 1, VIPS_FORMAT_DOUBLE
|
||||
|
||||
@@ -485,6 +485,18 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
baton->leftOffsetPost, baton->topOffsetPost, baton->widthPost, baton->heightPost);
|
||||
}
|
||||
|
||||
// Affine transform
|
||||
if (baton->affineMatrix.size() > 0) {
|
||||
std::vector<double> background;
|
||||
std::tie(image, background) = sharp::ApplyAlpha(image, baton->affineBackground);
|
||||
image = image.affine(baton->affineMatrix, VImage::option()->set("background", background)
|
||||
->set("idx", baton->affineIdx)
|
||||
->set("idy", baton->affineIdy)
|
||||
->set("odx", baton->affineOdx)
|
||||
->set("ody", baton->affineOdy)
|
||||
->set("interpolate", baton->affineInterpolator));
|
||||
}
|
||||
|
||||
// Extend edges
|
||||
if (baton->extendTop > 0 || baton->extendBottom > 0 || baton->extendLeft > 0 || baton->extendRight > 0) {
|
||||
std::vector<double> background;
|
||||
@@ -558,7 +570,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
int left;
|
||||
int top;
|
||||
compositeImage = compositeImage.replicate(across, down);
|
||||
if (composite->left >= 0 && composite->top >= 0) {
|
||||
if (composite->hasOffset) {
|
||||
std::tie(left, top) = sharp::CalculateCrop(
|
||||
compositeImage.width(), compositeImage.height(), image.width(), image.height(),
|
||||
composite->left, composite->top);
|
||||
@@ -580,7 +592,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
// Calculate position
|
||||
int left;
|
||||
int top;
|
||||
if (composite->left >= 0 && composite->top >= 0) {
|
||||
if (composite->hasOffset) {
|
||||
// Composite image at given offsets
|
||||
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
|
||||
compositeImage.width(), compositeImage.height(), composite->left, composite->top);
|
||||
@@ -718,7 +730,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
if (baton->formatOut == "jpeg" || (baton->formatOut == "input" && inputImageType == sharp::ImageType::JPEG)) {
|
||||
// Write JPEG to buffer
|
||||
sharp::AssertImageTypeDimensions(image, sharp::ImageType::JPEG);
|
||||
VipsArea *area = VIPS_AREA(image.jpegsave_buffer(VImage::option()
|
||||
VipsArea *area = reinterpret_cast<VipsArea*>(image.jpegsave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->jpegQuality)
|
||||
->set("interlace", baton->jpegProgressive)
|
||||
@@ -745,7 +757,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
inputImageType == sharp::ImageType::SVG))) {
|
||||
// Write PNG to buffer
|
||||
sharp::AssertImageTypeDimensions(image, sharp::ImageType::PNG);
|
||||
VipsArea *area = VIPS_AREA(image.pngsave_buffer(VImage::option()
|
||||
VipsArea *area = reinterpret_cast<VipsArea*>(image.pngsave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("interlace", baton->pngProgressive)
|
||||
->set("compression", baton->pngCompressionLevel)
|
||||
@@ -763,7 +775,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
(baton->formatOut == "input" && inputImageType == sharp::ImageType::WEBP)) {
|
||||
// Write WEBP to buffer
|
||||
sharp::AssertImageTypeDimensions(image, sharp::ImageType::WEBP);
|
||||
VipsArea *area = VIPS_AREA(image.webpsave_buffer(VImage::option()
|
||||
VipsArea *area = reinterpret_cast<VipsArea*>(image.webpsave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->webpQuality)
|
||||
->set("lossless", baton->webpLossless)
|
||||
@@ -780,8 +792,10 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
(baton->formatOut == "input" && inputImageType == sharp::ImageType::GIF && supportsGifOutput)) {
|
||||
// Write GIF to buffer
|
||||
sharp::AssertImageTypeDimensions(image, sharp::ImageType::GIF);
|
||||
VipsArea *area = VIPS_AREA(image.magicksave_buffer(VImage::option()
|
||||
VipsArea *area = reinterpret_cast<VipsArea*>(image.magicksave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("optimize_gif_frames", TRUE)
|
||||
->set("optimize_gif_transparency", TRUE)
|
||||
->set("format", "gif")));
|
||||
baton->bufferOut = static_cast<char*>(area->data);
|
||||
baton->bufferOutLength = area->length;
|
||||
@@ -799,7 +813,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
if (baton->tiffPredictor == VIPS_FOREIGN_TIFF_PREDICTOR_FLOAT) {
|
||||
image = image.cast(VIPS_FORMAT_FLOAT);
|
||||
}
|
||||
VipsArea *area = VIPS_AREA(image.tiffsave_buffer(VImage::option()
|
||||
VipsArea *area = reinterpret_cast<VipsArea*>(image.tiffsave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->tiffQuality)
|
||||
->set("bitdepth", baton->tiffBitdepth)
|
||||
@@ -819,10 +833,15 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
} else if (baton->formatOut == "heif" ||
|
||||
(baton->formatOut == "input" && inputImageType == sharp::ImageType::HEIF)) {
|
||||
// Write HEIF to buffer
|
||||
VipsArea *area = VIPS_AREA(image.heifsave_buffer(VImage::option()
|
||||
VipsArea *area = reinterpret_cast<VipsArea*>(image.heifsave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("compression", baton->heifCompression)
|
||||
->set("Q", baton->heifQuality)
|
||||
->set("speed", baton->heifSpeed)
|
||||
#if defined(VIPS_TYPE_FOREIGN_SUBSAMPLE)
|
||||
->set("subsample_mode", baton->heifChromaSubsampling == "4:4:4"
|
||||
? VIPS_FOREIGN_SUBSAMPLE_OFF : VIPS_FOREIGN_SUBSAMPLE_ON)
|
||||
#endif
|
||||
->set("lossless", baton->heifLossless)));
|
||||
baton->bufferOut = static_cast<char*>(area->data);
|
||||
baton->bufferOutLength = area->length;
|
||||
@@ -871,7 +890,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
bool const isV = sharp::IsV(baton->fileOut);
|
||||
bool const mightMatchInput = baton->formatOut == "input";
|
||||
bool const willMatchInput = mightMatchInput &&
|
||||
!(isJpeg || isPng || isWebp || isGif || isTiff || isDz || isDzZip || isV);
|
||||
!(isJpeg || isPng || isWebp || isGif || isTiff || isHeif || isDz || isDzZip || isV);
|
||||
|
||||
if (baton->formatOut == "jpeg" || (mightMatchInput && isJpeg) ||
|
||||
(willMatchInput && inputImageType == sharp::ImageType::JPEG)) {
|
||||
@@ -925,6 +944,8 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
sharp::AssertImageTypeDimensions(image, sharp::ImageType::GIF);
|
||||
image.magicksave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("optimize_gif_frames", TRUE)
|
||||
->set("optimize_gif_transparency", TRUE)
|
||||
->set("format", "gif"));
|
||||
baton->formatOut = "gif";
|
||||
} else if (baton->formatOut == "tiff" || (mightMatchInput && isTiff) ||
|
||||
@@ -934,6 +955,10 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
sharp::AssertImageTypeDimensions(image, sharp::ImageType::JPEG);
|
||||
baton->channels = std::min(baton->channels, 3);
|
||||
}
|
||||
// Cast pixel values to float, if required
|
||||
if (baton->tiffPredictor == VIPS_FOREIGN_TIFF_PREDICTOR_FLOAT) {
|
||||
image = image.cast(VIPS_FORMAT_FLOAT);
|
||||
}
|
||||
image.tiffsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->tiffQuality)
|
||||
@@ -950,13 +975,15 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
} else if (baton->formatOut == "heif" || (mightMatchInput && isHeif) ||
|
||||
(willMatchInput && inputImageType == sharp::ImageType::HEIF)) {
|
||||
// Write HEIF to file
|
||||
if (sharp::IsAvif(baton->fileOut)) {
|
||||
baton->heifCompression = VIPS_FOREIGN_HEIF_COMPRESSION_AV1;
|
||||
}
|
||||
image.heifsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->heifQuality)
|
||||
->set("compression", baton->heifCompression)
|
||||
->set("speed", baton->heifSpeed)
|
||||
#if defined(VIPS_TYPE_FOREIGN_SUBSAMPLE)
|
||||
->set("subsample_mode", baton->heifChromaSubsampling == "4:4:4"
|
||||
? VIPS_FOREIGN_SUBSAMPLE_OFF : VIPS_FOREIGN_SUBSAMPLE_ON)
|
||||
#endif
|
||||
->set("lossless", baton->heifLossless));
|
||||
baton->formatOut = "heif";
|
||||
} else if (baton->formatOut == "dz" || isDz || isDzZip) {
|
||||
@@ -1010,6 +1037,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
||||
->set("suffix", const_cast<char*>(suffix.data()))
|
||||
->set("angle", CalculateAngleRotation(baton->tileAngle))
|
||||
->set("background", baton->tileBackground)
|
||||
->set("centre", baton->tileCentre)
|
||||
->set("skip_blanks", baton->tileSkipBlanks);
|
||||
// libvips chooses a default depth based on layout. Instead of replicating that logic here by
|
||||
// not passing anything - libvips will handle choice
|
||||
@@ -1237,6 +1265,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
||||
composite->gravity = sharp::AttrAsUint32(compositeObject, "gravity");
|
||||
composite->left = sharp::AttrAsInt32(compositeObject, "left");
|
||||
composite->top = sharp::AttrAsInt32(compositeObject, "top");
|
||||
composite->hasOffset = sharp::AttrAsBool(compositeObject, "hasOffset");
|
||||
composite->tile = sharp::AttrAsBool(compositeObject, "tile");
|
||||
composite->premultiplied = sharp::AttrAsBool(compositeObject, "premultiplied");
|
||||
baton->composite.push_back(composite);
|
||||
@@ -1244,7 +1273,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
||||
// Resize options
|
||||
baton->withoutEnlargement = sharp::AttrAsBool(options, "withoutEnlargement");
|
||||
baton->position = sharp::AttrAsInt32(options, "position");
|
||||
baton->resizeBackground = sharp::AttrAsRgba(options, "resizeBackground");
|
||||
baton->resizeBackground = sharp::AttrAsVectorOfDouble(options, "resizeBackground");
|
||||
baton->kernel = sharp::AttrAsStr(options, "kernel");
|
||||
baton->fastShrinkOnLoad = sharp::AttrAsBool(options, "fastShrinkOnLoad");
|
||||
// Join Channel Options
|
||||
@@ -1257,7 +1286,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
||||
}
|
||||
// Operators
|
||||
baton->flatten = sharp::AttrAsBool(options, "flatten");
|
||||
baton->flattenBackground = sharp::AttrAsRgba(options, "flattenBackground");
|
||||
baton->flattenBackground = sharp::AttrAsVectorOfDouble(options, "flattenBackground");
|
||||
baton->negate = sharp::AttrAsBool(options, "negate");
|
||||
baton->blurSigma = sharp::AttrAsDouble(options, "blurSigma");
|
||||
baton->brightness = sharp::AttrAsDouble(options, "brightness");
|
||||
@@ -1279,7 +1308,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
||||
baton->useExifOrientation = sharp::AttrAsBool(options, "useExifOrientation");
|
||||
baton->angle = sharp::AttrAsInt32(options, "angle");
|
||||
baton->rotationAngle = sharp::AttrAsDouble(options, "rotationAngle");
|
||||
baton->rotationBackground = sharp::AttrAsRgba(options, "rotationBackground");
|
||||
baton->rotationBackground = sharp::AttrAsVectorOfDouble(options, "rotationBackground");
|
||||
baton->rotateBeforePreExtract = sharp::AttrAsBool(options, "rotateBeforePreExtract");
|
||||
baton->flip = sharp::AttrAsBool(options, "flip");
|
||||
baton->flop = sharp::AttrAsBool(options, "flop");
|
||||
@@ -1287,8 +1316,15 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
||||
baton->extendBottom = sharp::AttrAsInt32(options, "extendBottom");
|
||||
baton->extendLeft = sharp::AttrAsInt32(options, "extendLeft");
|
||||
baton->extendRight = sharp::AttrAsInt32(options, "extendRight");
|
||||
baton->extendBackground = sharp::AttrAsRgba(options, "extendBackground");
|
||||
baton->extendBackground = sharp::AttrAsVectorOfDouble(options, "extendBackground");
|
||||
baton->extractChannel = sharp::AttrAsInt32(options, "extractChannel");
|
||||
baton->affineMatrix = sharp::AttrAsVectorOfDouble(options, "affineMatrix");
|
||||
baton->affineBackground = sharp::AttrAsVectorOfDouble(options, "affineBackground");
|
||||
baton->affineIdx = sharp::AttrAsDouble(options, "affineIdx");
|
||||
baton->affineIdy = sharp::AttrAsDouble(options, "affineIdy");
|
||||
baton->affineOdx = sharp::AttrAsDouble(options, "affineOdx");
|
||||
baton->affineOdy = sharp::AttrAsDouble(options, "affineOdy");
|
||||
baton->affineInterpolator = vips::VInterpolate::new_from_name(sharp::AttrAsStr(options, "affineInterpolator").data());
|
||||
|
||||
baton->removeAlpha = sharp::AttrAsBool(options, "removeAlpha");
|
||||
baton->ensureAlpha = sharp::AttrAsBool(options, "ensureAlpha");
|
||||
@@ -1371,6 +1407,8 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
||||
baton->heifCompression = static_cast<VipsForeignHeifCompression>(
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_HEIF_COMPRESSION,
|
||||
sharp::AttrAsStr(options, "heifCompression").data()));
|
||||
baton->heifSpeed = sharp::AttrAsUint32(options, "heifSpeed");
|
||||
baton->heifChromaSubsampling = sharp::AttrAsStr(options, "heifChromaSubsampling");
|
||||
|
||||
// Animated output
|
||||
if (sharp::HasAttr(options, "pageHeight")) {
|
||||
@@ -1387,7 +1425,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
||||
baton->tileSize = sharp::AttrAsUint32(options, "tileSize");
|
||||
baton->tileOverlap = sharp::AttrAsUint32(options, "tileOverlap");
|
||||
baton->tileAngle = sharp::AttrAsInt32(options, "tileAngle");
|
||||
baton->tileBackground = sharp::AttrAsRgba(options, "tileBackground");
|
||||
baton->tileBackground = sharp::AttrAsVectorOfDouble(options, "tileBackground");
|
||||
baton->tileSkipBlanks = sharp::AttrAsInt32(options, "tileSkipBlanks");
|
||||
baton->tileContainer = static_cast<VipsForeignDzContainer>(
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_DZ_CONTAINER,
|
||||
@@ -1399,6 +1437,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
||||
baton->tileDepth = static_cast<VipsForeignDzDepth>(
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_DZ_DEPTH,
|
||||
sharp::AttrAsStr(options, "tileDepth").data()));
|
||||
baton->tileCentre = sharp::AttrAsBool(options, "tileCentre");
|
||||
|
||||
// Force random access for certain operations
|
||||
if (baton->input->access == VIPS_ACCESS_SEQUENTIAL) {
|
||||
|
||||
@@ -40,6 +40,7 @@ struct Composite {
|
||||
int gravity;
|
||||
int left;
|
||||
int top;
|
||||
bool hasOffset;
|
||||
bool tile;
|
||||
bool premultiplied;
|
||||
|
||||
@@ -47,8 +48,9 @@ struct Composite {
|
||||
input(nullptr),
|
||||
mode(VIPS_BLEND_MODE_OVER),
|
||||
gravity(0),
|
||||
left(-1),
|
||||
top(-1),
|
||||
left(0),
|
||||
top(0),
|
||||
hasOffset(false),
|
||||
tile(false),
|
||||
premultiplied(false) {}
|
||||
};
|
||||
@@ -79,6 +81,7 @@ struct PipelineBaton {
|
||||
int cropOffsetLeft;
|
||||
int cropOffsetTop;
|
||||
bool premultiplied;
|
||||
bool tileCentre;
|
||||
std::string kernel;
|
||||
bool fastShrinkOnLoad;
|
||||
double tintA;
|
||||
@@ -118,6 +121,13 @@ struct PipelineBaton {
|
||||
int extendRight;
|
||||
std::vector<double> extendBackground;
|
||||
bool withoutEnlargement;
|
||||
std::vector<double> affineMatrix;
|
||||
std::vector<double> affineBackground;
|
||||
double affineIdx;
|
||||
double affineIdy;
|
||||
double affineOdx;
|
||||
double affineOdy;
|
||||
vips::VInterpolate affineInterpolator;
|
||||
int jpegQuality;
|
||||
bool jpegProgressive;
|
||||
std::string jpegChromaSubsampling;
|
||||
@@ -151,6 +161,8 @@ struct PipelineBaton {
|
||||
double tiffYres;
|
||||
int heifQuality;
|
||||
VipsForeignHeifCompression heifCompression;
|
||||
int heifSpeed;
|
||||
std::string heifChromaSubsampling;
|
||||
bool heifLossless;
|
||||
std::string err;
|
||||
bool withMetadata;
|
||||
@@ -230,6 +242,13 @@ struct PipelineBaton {
|
||||
extendRight(0),
|
||||
extendBackground{ 0.0, 0.0, 0.0, 255.0 },
|
||||
withoutEnlargement(false),
|
||||
affineMatrix{ 1.0, 0.0, 0.0, 1.0 },
|
||||
affineBackground{ 0.0, 0.0, 0.0, 255.0 },
|
||||
affineIdx(0),
|
||||
affineIdy(0),
|
||||
affineOdx(0),
|
||||
affineOdy(0),
|
||||
affineInterpolator(vips::VInterpolate::new_from_name("bicubic")),
|
||||
jpegQuality(80),
|
||||
jpegProgressive(false),
|
||||
jpegChromaSubsampling("4:2:0"),
|
||||
@@ -261,8 +280,10 @@ struct PipelineBaton {
|
||||
tiffTileWidth(256),
|
||||
tiffXres(1.0),
|
||||
tiffYres(1.0),
|
||||
heifQuality(80),
|
||||
heifCompression(VIPS_FOREIGN_HEIF_COMPRESSION_HEVC),
|
||||
heifQuality(50),
|
||||
heifCompression(VIPS_FOREIGN_HEIF_COMPRESSION_AV1),
|
||||
heifSpeed(5),
|
||||
heifChromaSubsampling("4:2:0"),
|
||||
heifLossless(false),
|
||||
withMetadata(false),
|
||||
withMetadataOrientation(-1),
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "stats.h"
|
||||
|
||||
static void* sharp_vips_init(void*) {
|
||||
g_setenv("VIPS_MIN_STACK_SIZE", "2m", FALSE);
|
||||
vips_init("sharp");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
14
src/stats.cc
@@ -80,12 +80,14 @@ class StatsWorker : public Napi::AsyncWorker {
|
||||
// Estimate entropy via histogram of greyscale value frequency
|
||||
baton->entropy = std::abs(greyscale.hist_find().hist_entropy());
|
||||
// Estimate sharpness via standard deviation of greyscale laplacian
|
||||
VImage laplacian = VImage::new_matrixv(3, 3,
|
||||
0.0, 1.0, 0.0,
|
||||
1.0, -4.0, 1.0,
|
||||
0.0, 1.0, 0.0);
|
||||
laplacian.set("scale", 9.0);
|
||||
baton->sharpness = greyscale.conv(laplacian).deviate();
|
||||
if (image.width() > 1 || image.height() > 1) {
|
||||
VImage laplacian = VImage::new_matrixv(3, 3,
|
||||
0.0, 1.0, 0.0,
|
||||
1.0, -4.0, 1.0,
|
||||
0.0, 1.0, 0.0);
|
||||
laplacian.set("scale", 9.0);
|
||||
baton->sharpness = greyscale.conv(laplacian).deviate();
|
||||
}
|
||||
// Most dominant sRGB colour via 4096-bin 3D histogram
|
||||
vips::VImage hist = sharp::RemoveAlpha(image)
|
||||
.colourspace(VIPS_INTERPRETATION_sRGB)
|
||||
|
||||
@@ -8,16 +8,16 @@
|
||||
"test": "node perf && node random && node parallel"
|
||||
},
|
||||
"devDependencies": {
|
||||
"async": "^3.1.0",
|
||||
"benchmark": "^2.1.4",
|
||||
"gm": "^1.23.1",
|
||||
"imagemagick": "^0.1.3",
|
||||
"jimp": "^0.16.0",
|
||||
"mapnik": "^4.5.2",
|
||||
"semver": "^7.1.1"
|
||||
"async": "3.2.0",
|
||||
"benchmark": "2.1.4",
|
||||
"gm": "1.23.1",
|
||||
"imagemagick": "0.1.3",
|
||||
"jimp": "0.16.1",
|
||||
"mapnik": "4.5.5",
|
||||
"semver": "7.3.4"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=10.16.0"
|
||||
"node": "14"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,10 @@ const jimp = require('jimp');
|
||||
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
const outputJpg = fixtures.path('output.jpg');
|
||||
const outputPng = fixtures.path('output.png');
|
||||
const outputWebP = fixtures.path('output.webp');
|
||||
|
||||
const width = 720;
|
||||
const height = 588;
|
||||
|
||||
@@ -56,7 +60,7 @@ async.series({
|
||||
image
|
||||
.resize(width, height, jimp.RESIZE_BICUBIC)
|
||||
.quality(80)
|
||||
.write(fixtures.outputJpg, function (err) {
|
||||
.write(outputJpg, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -77,7 +81,7 @@ async.series({
|
||||
.resize(width, height, {
|
||||
scaling_method: mapnik.imageScaling.lanczos
|
||||
})
|
||||
.save(fixtures.outputJpg, 'jpeg:quality=80', function (err) {
|
||||
.save(outputJpg, 'jpeg:quality=80', function (err) {
|
||||
if (err) throw err;
|
||||
deferred.resolve();
|
||||
});
|
||||
@@ -105,7 +109,7 @@ async.series({
|
||||
fn: function (deferred) {
|
||||
imagemagick.resize({
|
||||
srcPath: fixtures.inputJpg,
|
||||
dstPath: fixtures.outputJpg,
|
||||
dstPath: outputJpg,
|
||||
quality: 0.8,
|
||||
width: width,
|
||||
height: height,
|
||||
@@ -128,7 +132,7 @@ async.series({
|
||||
.filter('Lanczos')
|
||||
.resize(width, height)
|
||||
.quality(80)
|
||||
.write(fixtures.outputJpg, function (err) {
|
||||
.write(outputJpg, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -159,7 +163,7 @@ async.series({
|
||||
.filter('Lanczos')
|
||||
.resize(width, height)
|
||||
.quality(80)
|
||||
.write(fixtures.outputJpg, function (err) {
|
||||
.write(outputJpg, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -190,7 +194,7 @@ async.series({
|
||||
fn: function (deferred) {
|
||||
sharp(inputJpgBuffer)
|
||||
.resize(width, height)
|
||||
.toFile(fixtures.outputJpg, function (err) {
|
||||
.toFile(outputJpg, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -217,7 +221,7 @@ async.series({
|
||||
fn: function (deferred) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(width, height)
|
||||
.toFile(fixtures.outputJpg, function (err) {
|
||||
.toFile(outputJpg, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -229,7 +233,7 @@ async.series({
|
||||
defer: true,
|
||||
fn: function (deferred) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
const writable = fs.createWriteStream(outputJpg);
|
||||
writable.on('finish', function () {
|
||||
deferred.resolve();
|
||||
});
|
||||
@@ -600,7 +604,7 @@ async.series({
|
||||
.resize(width, height)
|
||||
.deflateLevel(6)
|
||||
.filterType(0)
|
||||
.write(fixtures.outputPng, function (err) {
|
||||
.write(outputPng, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -625,7 +629,7 @@ async.series({
|
||||
if (err) throw err;
|
||||
img.demultiply(function (err, img) {
|
||||
if (err) throw err;
|
||||
img.save(fixtures.outputPng, 'png', function (err) {
|
||||
img.save(outputPng, 'png', function (err) {
|
||||
if (err) throw err;
|
||||
deferred.resolve();
|
||||
});
|
||||
@@ -663,7 +667,7 @@ async.series({
|
||||
fn: function (deferred) {
|
||||
imagemagick.resize({
|
||||
srcPath: fixtures.inputPngAlphaPremultiplicationLarge,
|
||||
dstPath: fixtures.outputPng,
|
||||
dstPath: outputPng,
|
||||
width: width,
|
||||
height: height,
|
||||
filter: 'Lanczos',
|
||||
@@ -689,7 +693,7 @@ async.series({
|
||||
.resize(width, height)
|
||||
.define('PNG:compression-level=6')
|
||||
.define('PNG:compression-filter=0')
|
||||
.write(fixtures.outputPng, function (err) {
|
||||
.write(outputPng, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -723,7 +727,7 @@ async.series({
|
||||
sharp(inputPngBuffer)
|
||||
.resize(width, height)
|
||||
.png({ compressionLevel: 6 })
|
||||
.toFile(fixtures.outputPng, function (err) {
|
||||
.toFile(outputPng, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -754,7 +758,7 @@ async.series({
|
||||
sharp(fixtures.inputPngAlphaPremultiplicationLarge)
|
||||
.resize(width, height)
|
||||
.png({ compressionLevel: 6 })
|
||||
.toFile(fixtures.outputPng, function (err) {
|
||||
.toFile(outputPng, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -841,7 +845,7 @@ async.series({
|
||||
fn: function (deferred) {
|
||||
sharp(inputWebPBuffer)
|
||||
.resize(width, height)
|
||||
.toFile(fixtures.outputWebP, function (err) {
|
||||
.toFile(outputWebP, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
@@ -868,7 +872,7 @@ async.series({
|
||||
fn: function (deferred) {
|
||||
sharp(fixtures.inputWebP)
|
||||
.resize(width, height)
|
||||
.toFile(fixtures.outputWebP, function (err) {
|
||||
.toFile(outputWebP, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
|
||||
@@ -23,7 +23,7 @@ new Benchmark.Suite('random').add('imagemagick', {
|
||||
fn: function (deferred) {
|
||||
imagemagick.resize({
|
||||
srcPath: fixtures.inputJpg,
|
||||
dstPath: fixtures.outputJpg,
|
||||
dstPath: fixtures.path('output.jpg'),
|
||||
quality: 0.8,
|
||||
width: randomDimension(),
|
||||
height: randomDimension(),
|
||||
|
||||
BIN
test/fixtures/big-height.webp
vendored
Normal file
|
After Width: | Height: | Size: 7.1 KiB |
3
test/fixtures/circle.svg
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 8 8">
|
||||
<circle r="3.75" cx="4" cy="4" fill="deeppink" />
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 122 B |
BIN
test/fixtures/cosmos_frame12924_yuv420_10bpc_bt2020_pq_q50.avif
vendored
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
test/fixtures/expected/affine-background-all-offsets-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 36 KiB |
BIN
test/fixtures/expected/affine-background-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 26 KiB |
BIN
test/fixtures/expected/affine-background-output-offsets-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 78 KiB |
BIN
test/fixtures/expected/affine-bicubic-2x-upscale-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 57 KiB |
BIN
test/fixtures/expected/affine-bilinear-2x-upscale-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 58 KiB |
BIN
test/fixtures/expected/affine-extract-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 8.0 KiB |
BIN
test/fixtures/expected/affine-extract-rotate-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 38 KiB |
BIN
test/fixtures/expected/affine-lbb-2x-upscale-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 57 KiB |
BIN
test/fixtures/expected/affine-nearest-2x-upscale-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 64 KiB |
BIN
test/fixtures/expected/affine-nohalo-2x-upscale-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 58 KiB |
BIN
test/fixtures/expected/affine-resize-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 46 KiB |
BIN
test/fixtures/expected/affine-rotate-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 31 KiB |
BIN
test/fixtures/expected/affine-vsqbs-2x-upscale-expected.jpg
vendored
Normal file
|
After Width: | Height: | Size: 48 KiB |
BIN
test/fixtures/expected/circle.png
vendored
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
test/fixtures/expected/hilutite.jpg
vendored
|
Before Width: | Height: | Size: 1.0 MiB After Width: | Height: | Size: 424 KiB |
BIN
test/fixtures/expected/icc-cmyk.jpg
vendored
|
Before Width: | Height: | Size: 2.8 MiB After Width: | Height: | Size: 943 KiB |
BIN
test/fixtures/expected/overlay-negative-offset-with-gravity.jpg
vendored
Normal file
|
After Width: | Height: | Size: 24 KiB |
BIN
test/fixtures/expected/tile_centered.jpg
vendored
Normal file
|
After Width: | Height: | Size: 7.0 KiB |
BIN
test/fixtures/expected/tint-cmyk.jpg
vendored
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
10
test/fixtures/index.js
vendored
@@ -95,6 +95,7 @@ module.exports = {
|
||||
inputWebPWithTransparency: getPath('5_webp_a.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp
|
||||
inputWebPAnimated: getPath('rotating-squares.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp
|
||||
inputWebPAnimatedLoop3: getPath('animated-loop-3.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp
|
||||
inputWebPAnimatedBigHeight: getPath('big-height.webp'),
|
||||
inputTiff: getPath('G31D.TIF'), // http://www.fileformat.info/format/tiff/sample/e6c9a6e5253348f4aef6d17b534360ab/index.htm
|
||||
inputTiffMultipage: getPath('G31D_MULTI.TIF'), // gm convert G31D.TIF -resize 50% G31D_2.TIF ; tiffcp G31D.TIF G31D_2.TIF G31D_MULTI.TIF
|
||||
inputTiffCielab: getPath('cielab-dagams.tiff'), // https://github.com/lovell/sharp/issues/646
|
||||
@@ -106,7 +107,9 @@ module.exports = {
|
||||
inputGifAnimated: getPath('rotating-squares.gif'), // CC0 https://loading.io/spinner/blocks/-rotating-squares-preloader-gif
|
||||
inputGifAnimatedLoop3: getPath('animated-loop-3.gif'), // CC-BY-SA-4.0 Petrus3743 https://commons.wikimedia.org/wiki/File:01-Goldener_Schnitt_Formel-Animation.gif
|
||||
inputSvg: getPath('check.svg'), // http://dev.w3.org/SVG/tools/svgweb/samples/svg-files/check.svg
|
||||
inputSvgSmallViewBox: getPath('circle.svg'),
|
||||
inputSvgWithEmbeddedImages: getPath('struct-image-04-t.svg'), // https://dev.w3.org/SVG/profiles/1.2T/test/svg/struct-image-04-t.svg
|
||||
inputAvif: getPath('cosmos_frame12924_yuv420_10bpc_bt2020_pq_q50.avif'), // CC by-nc-nd https://github.com/AOMediaCodec/av1-avif/tree/master/testFiles/Netflix
|
||||
|
||||
inputJPGBig: getPath('flowers.jpeg'),
|
||||
|
||||
@@ -117,13 +120,6 @@ module.exports = {
|
||||
|
||||
inputV: getPath('vfile.v'),
|
||||
|
||||
outputJpg: getPath('output.jpg'),
|
||||
outputPng: getPath('output.png'),
|
||||
outputWebP: getPath('output.webp'),
|
||||
outputV: getPath('output.v'),
|
||||
outputTiff: getPath('output.tiff'),
|
||||
outputZoinks: getPath('output.zoinks'), // an 'unknown' file extension
|
||||
|
||||
testPattern: getPath('test-pattern.png'),
|
||||
|
||||
// Path for tests requiring human inspection
|
||||
|
||||
@@ -189,6 +189,24 @@
|
||||
...
|
||||
fun:gsf_output_write
|
||||
}
|
||||
{
|
||||
param_gsf_new_do_write
|
||||
Memcheck:Param
|
||||
write(buf)
|
||||
...
|
||||
fun:new_do_write
|
||||
...
|
||||
fun:gsf_output_close
|
||||
}
|
||||
{
|
||||
param_gsf_output_write
|
||||
Memcheck:Param
|
||||
write(buf)
|
||||
...
|
||||
fun:new_do_write
|
||||
...
|
||||
fun:gsf_output_write
|
||||
}
|
||||
|
||||
# fontconfig
|
||||
{
|
||||
@@ -218,6 +236,84 @@
|
||||
fun:XML_ParseBuffer
|
||||
obj:*/libfontconfig.so.*
|
||||
}
|
||||
{
|
||||
leak_fontconfig_FcInitLoadConfigAndFonts
|
||||
Memcheck:Leak
|
||||
match-leak-kinds: definite
|
||||
fun:malloc
|
||||
...
|
||||
fun:XML_ParseBuffer
|
||||
...
|
||||
fun:FcInitLoadConfigAndFonts
|
||||
}
|
||||
|
||||
# heif
|
||||
{
|
||||
cond_heif_encode_image
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:heif_context_encode_image
|
||||
}
|
||||
{
|
||||
value8_heif_encode_image
|
||||
Memcheck:Value8
|
||||
...
|
||||
fun:heif_context_encode_image
|
||||
}
|
||||
{
|
||||
cond_heif_aom_codec_encode
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:aom_codec_encode
|
||||
}
|
||||
{
|
||||
value8_heif_aom_codec_encode
|
||||
Memcheck:Value8
|
||||
...
|
||||
fun:aom_codec_encode
|
||||
}
|
||||
{
|
||||
value1_heif_aom_codec_encode
|
||||
Memcheck:Value1
|
||||
...
|
||||
fun:aom_codec_encode
|
||||
}
|
||||
{
|
||||
cond_heif_av1_encode_frame
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:av1_encode_frame
|
||||
}
|
||||
{
|
||||
value8_heif_av1_encode_frame
|
||||
Memcheck:Value8
|
||||
...
|
||||
fun:av1_encode_frame
|
||||
}
|
||||
{
|
||||
cond_heif_context_write
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:heif_context_write
|
||||
}
|
||||
{
|
||||
value8_heif_context_write
|
||||
Memcheck:Value8
|
||||
...
|
||||
fun:heif_context_write
|
||||
}
|
||||
{
|
||||
cond_heif_context_read
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:heif_context_read_from_reader
|
||||
}
|
||||
{
|
||||
value8_heif_context_read
|
||||
Memcheck:Value8
|
||||
...
|
||||
fun:heif_context_read_from_reader
|
||||
}
|
||||
|
||||
# libvips
|
||||
{
|
||||
@@ -306,6 +402,30 @@
|
||||
...
|
||||
fun:start_thread
|
||||
}
|
||||
{
|
||||
cond_libvips_source_read
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:vips_source_read
|
||||
}
|
||||
{
|
||||
value8_libvips_source_read
|
||||
Memcheck:Value8
|
||||
...
|
||||
fun:vips_source_read
|
||||
}
|
||||
{
|
||||
cond_libvips_target_finish
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:vips_target_finish
|
||||
}
|
||||
{
|
||||
value8_libvips_target_finish
|
||||
Memcheck:Value8
|
||||
...
|
||||
fun:vips_target_finish
|
||||
}
|
||||
{
|
||||
leak_libvips_init
|
||||
Memcheck:Leak
|
||||
@@ -321,8 +441,34 @@
|
||||
fun:malloc
|
||||
...
|
||||
fun:rsvg_rust_handle_new_from_stream_sync
|
||||
}
|
||||
{
|
||||
leak_rsvg_rsvg_rust_handle_new_from_gfile_sync
|
||||
Memcheck:Leak
|
||||
match-leak-kinds: definite
|
||||
fun:malloc
|
||||
...
|
||||
fun:vips_object_build
|
||||
fun:rsvg_rust_handle_new_from_gfile_sync
|
||||
}
|
||||
{
|
||||
leak_rsvg_rust_handle_new_from_stream_sync
|
||||
Memcheck:Leak
|
||||
match-leak-kinds: possible
|
||||
fun:malloc
|
||||
...
|
||||
fun:xmlParseElement
|
||||
...
|
||||
fun:rsvg_rust_handle_new_from_stream_sync
|
||||
}
|
||||
{
|
||||
leak_rsvg_rust_handle_new_from_gfile_sync
|
||||
Memcheck:Leak
|
||||
match-leak-kinds: possible
|
||||
fun:malloc
|
||||
...
|
||||
fun:xmlParseElement
|
||||
...
|
||||
fun:rsvg_rust_handle_new_from_gfile_sync
|
||||
}
|
||||
|
||||
# libuv warnings
|
||||
@@ -691,3 +837,27 @@
|
||||
...
|
||||
fun:_ZN12v8_inspector10toString16ERKNS_10StringViewE
|
||||
}
|
||||
{
|
||||
cond_v8_Builtins_InterpreterEntryTrampoline
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:Builtins_InterpreterEntryTrampoline
|
||||
}
|
||||
{
|
||||
cond_v8_ZN2v88internal18ArrayBufferSweeper9SweepFullEv
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:_ZN2v88internal18ArrayBufferSweeper9SweepFullEv
|
||||
}
|
||||
{
|
||||
cond_v8_ZN4node11Environment27RunAndClearNativeImmediatesEb
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:_ZN4node11Environment27RunAndClearNativeImmediatesEb
|
||||
}
|
||||
{
|
||||
cond_v8_ZN2v88internal18ArrayBufferSweeper10ReleaseAllEv
|
||||
Memcheck:Cond
|
||||
...
|
||||
fun:_ZN2v88internal18ArrayBufferSweeper10ReleaseAllEv
|
||||
}
|
||||
|
||||
175
test/unit/affine.js
Normal file
@@ -0,0 +1,175 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('Affine transform', () => {
|
||||
describe('Invalid input', () => {
|
||||
it('Missing matrix', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine();
|
||||
});
|
||||
});
|
||||
it('Invalid 1d matrix', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine(['123', 123, 123, 123]);
|
||||
});
|
||||
});
|
||||
it('Invalid 2d matrix', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine([[123, 123], [null, 123]]);
|
||||
});
|
||||
});
|
||||
it('Invalid options parameter type', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine([[1, 0], [0, 1]], 'invalid options type');
|
||||
});
|
||||
});
|
||||
it('Invalid background color', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine([4, 4, 4, 4], { background: 'not a color' });
|
||||
});
|
||||
});
|
||||
it('Invalid idx offset type', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine([[4, 4], [4, 4]], { idx: 'invalid idx type' });
|
||||
});
|
||||
});
|
||||
it('Invalid idy offset type', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine([4, 4, 4, 4], { idy: 'invalid idy type' });
|
||||
});
|
||||
});
|
||||
it('Invalid odx offset type', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine([[4, 4], [4, 4]], { odx: 'invalid odx type' });
|
||||
});
|
||||
});
|
||||
it('Invalid ody offset type', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine([[4, 4], [4, 4]], { ody: 'invalid ody type' });
|
||||
});
|
||||
});
|
||||
it('Invalid interpolator', () => {
|
||||
assert.throws(() => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.affine([[4, 4], [4, 4]], { interpolator: 'cubic' });
|
||||
});
|
||||
});
|
||||
});
|
||||
it('Applies identity matrix', done => {
|
||||
const input = fixtures.inputJpg;
|
||||
sharp(input)
|
||||
.affine([[1, 0], [0, 1]])
|
||||
.toBuffer((err, data) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(input, data, done);
|
||||
});
|
||||
});
|
||||
it('Applies resize affine matrix', done => {
|
||||
const input = fixtures.inputJpg;
|
||||
const inputWidth = 2725;
|
||||
const inputHeight = 2225;
|
||||
sharp(input)
|
||||
.affine([[0.2, 0], [0, 1.5]])
|
||||
.toBuffer((err, data, info) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(input, data, done);
|
||||
assert.strictEqual(info.width, Math.ceil(inputWidth * 0.2));
|
||||
assert.strictEqual(info.height, Math.ceil(inputHeight * 1.5));
|
||||
});
|
||||
});
|
||||
it('Resizes and applies affine transform', done => {
|
||||
const input = fixtures.inputJpg;
|
||||
sharp(input)
|
||||
.resize(500, 500)
|
||||
.affine([[0.5, 1], [1, 0.5]])
|
||||
.toBuffer((err, data) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(data, fixtures.expected('affine-resize-expected.jpg'), done);
|
||||
});
|
||||
});
|
||||
it('Extracts and applies affine transform', done => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.extract({ left: 300, top: 300, width: 600, height: 600 })
|
||||
.affine([0.3, 0, -0.5, 0.3])
|
||||
.toBuffer((err, data) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(data, fixtures.expected('affine-extract-expected.jpg'), done);
|
||||
});
|
||||
});
|
||||
it('Rotates and applies affine transform', done => {
|
||||
sharp(fixtures.inputJpg320x240)
|
||||
.rotate(90)
|
||||
.affine([[-1.2, 0], [0, -1.2]])
|
||||
.toBuffer((err, data) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(data, fixtures.expected('affine-rotate-expected.jpg'), done);
|
||||
});
|
||||
});
|
||||
it('Extracts, rotates and applies affine transform', done => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.extract({ left: 1000, top: 1000, width: 200, height: 200 })
|
||||
.rotate(45, { background: 'blue' })
|
||||
.affine([[2, 1], [2, -0.5]], { background: 'red' })
|
||||
.toBuffer((err, data) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('affine-extract-rotate-expected.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
it('Applies affine transform with background color', done => {
|
||||
sharp(fixtures.inputJpg320x240)
|
||||
.rotate(180)
|
||||
.affine([[-1.5, 1.2], [-1, 1]], { background: 'red' })
|
||||
.toBuffer((err, data) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('affine-background-expected.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
it('Applies affine transform with background color and output offsets', done => {
|
||||
sharp(fixtures.inputJpg320x240)
|
||||
.rotate(180)
|
||||
.affine([[-2, 1.5], [-1, 2]], { background: 'blue', odx: 40, ody: -100 })
|
||||
.toBuffer((err, data) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('affine-background-output-offsets-expected.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
it('Applies affine transform with background color and all offsets', done => {
|
||||
sharp(fixtures.inputJpg320x240)
|
||||
.rotate(180)
|
||||
.affine([[-1.2, 1.8], [-1, 2]], { background: 'yellow', idx: 10, idy: -40, odx: 10, ody: -50 })
|
||||
.toBuffer((err, data) => {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('affine-background-all-offsets-expected.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
describe('Interpolations', () => {
|
||||
const input = fixtures.inputJpg320x240;
|
||||
const inputWidth = 320;
|
||||
const inputHeight = 240;
|
||||
for (const interp in sharp.interpolators) {
|
||||
it(`Performs 2x upscale with ${interp} interpolation`, done => {
|
||||
sharp(input)
|
||||
.affine([[2, 0], [0, 2]], { interpolator: sharp.interpolators[interp] })
|
||||
.toBuffer((err, data, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(info.width, Math.ceil(inputWidth * 2));
|
||||
assert.strictEqual(info.height, Math.ceil(inputHeight * 2));
|
||||
fixtures.assertSimilar(fixtures.expected(`affine-${sharp.interpolators[interp]}-2x-upscale-expected.jpg`), data, done);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
84
test/unit/avif.js
Normal file
@@ -0,0 +1,84 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const sharp = require('../../');
|
||||
const { inputAvif, inputJpg } = require('../fixtures');
|
||||
|
||||
describe('AVIF', () => {
|
||||
it('called without options does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().avif();
|
||||
});
|
||||
});
|
||||
|
||||
it('can passthrough AVIF', async () => {
|
||||
const data = await sharp(inputAvif)
|
||||
.resize(32)
|
||||
.toBuffer();
|
||||
const metadata = await sharp(data)
|
||||
.metadata();
|
||||
const { compression, size, ...metadataWithoutSize } = metadata;
|
||||
assert.deepStrictEqual(metadataWithoutSize, {
|
||||
channels: 3,
|
||||
depth: 'uchar',
|
||||
format: 'heif',
|
||||
hasAlpha: false,
|
||||
hasProfile: false,
|
||||
height: 12,
|
||||
isProgressive: false,
|
||||
pageHeight: 12,
|
||||
pagePrimary: 0,
|
||||
pages: 1,
|
||||
space: 'srgb',
|
||||
width: 32
|
||||
});
|
||||
});
|
||||
|
||||
it('can convert AVIF to JPEG', async () => {
|
||||
const data = await sharp(inputAvif)
|
||||
.resize(32)
|
||||
.jpeg()
|
||||
.toBuffer();
|
||||
const metadata = await sharp(data)
|
||||
.metadata();
|
||||
const { compression, size, ...metadataWithoutSize } = metadata;
|
||||
assert.deepStrictEqual(metadataWithoutSize, {
|
||||
channels: 3,
|
||||
chromaSubsampling: '4:2:0',
|
||||
density: 72,
|
||||
depth: 'uchar',
|
||||
format: 'jpeg',
|
||||
hasAlpha: false,
|
||||
hasProfile: false,
|
||||
height: 13,
|
||||
isProgressive: false,
|
||||
space: 'srgb',
|
||||
width: 32
|
||||
});
|
||||
});
|
||||
|
||||
it('can convert JPEG to AVIF', async () => {
|
||||
const data = await sharp(inputJpg)
|
||||
.resize(32)
|
||||
.avif()
|
||||
.toBuffer();
|
||||
const metadata = await sharp(data)
|
||||
.metadata();
|
||||
const { compression, size, ...metadataWithoutSize } = metadata;
|
||||
assert.deepStrictEqual(metadataWithoutSize, {
|
||||
channels: 3,
|
||||
depth: 'uchar',
|
||||
format: 'heif',
|
||||
hasAlpha: false,
|
||||
hasProfile: false,
|
||||
height: 26,
|
||||
isProgressive: false,
|
||||
pageHeight: 26,
|
||||
pagePrimary: 0,
|
||||
pages: 1,
|
||||
space: 'srgb',
|
||||
width: 32
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -172,6 +172,24 @@ describe('composite', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('negative offset and gravity', done => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(400)
|
||||
.composite([{
|
||||
input: fixtures.inputPngWithTransparency16bit,
|
||||
left: -10,
|
||||
top: -10,
|
||||
gravity: 4
|
||||
}])
|
||||
.toBuffer((err, data, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(3, info.channels);
|
||||
fixtures.assertSimilar(
|
||||
fixtures.expected('overlay-negative-offset-with-gravity.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('offset, gravity and tile', done => {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(80)
|
||||
@@ -333,13 +351,25 @@ describe('composite', () => {
|
||||
it('invalid left', () => {
|
||||
assert.throws(() => {
|
||||
sharp().composite([{ input: 'test', left: 0.5 }]);
|
||||
}, /Expected positive integer for left but received 0.5 of type number/);
|
||||
}, /Expected integer for left but received 0.5 of type number/);
|
||||
assert.throws(() => {
|
||||
sharp().composite([{ input: 'test', left: 'invalid' }]);
|
||||
}, /Expected integer for left but received invalid of type string/);
|
||||
assert.throws(() => {
|
||||
sharp().composite([{ input: 'test', left: 'invalid', top: 10 }]);
|
||||
}, /Expected integer for left but received invalid of type string/);
|
||||
});
|
||||
|
||||
it('invalid top', () => {
|
||||
assert.throws(() => {
|
||||
sharp().composite([{ input: 'test', top: -1 }]);
|
||||
}, /Expected positive integer for top but received -1 of type number/);
|
||||
sharp().composite([{ input: 'test', top: 0.5 }]);
|
||||
}, /Expected integer for top but received 0.5 of type number/);
|
||||
assert.throws(() => {
|
||||
sharp().composite([{ input: 'test', top: 'invalid' }]);
|
||||
}, /Expected integer for top but received invalid of type string/);
|
||||
assert.throws(() => {
|
||||
sharp().composite([{ input: 'test', top: 'invalid', left: 10 }]);
|
||||
}, /Expected integer for top but received invalid of type string/);
|
||||
});
|
||||
|
||||
it('left but no top', () => {
|
||||
|
||||
@@ -53,7 +53,7 @@ describe('failOnError', function () {
|
||||
|
||||
it('returns errors to callback for truncated JPEG', function (done) {
|
||||
sharp(fixtures.inputJpgTruncated).toBuffer(function (err, data, info) {
|
||||
assert.ok(err.message.includes('VipsJpeg: Premature end of JPEG file'), err);
|
||||
assert.ok(err.message.includes('VipsJpeg: Premature end of'), err);
|
||||
assert.strictEqual(data, undefined);
|
||||
assert.strictEqual(info, undefined);
|
||||
done();
|
||||
@@ -76,7 +76,7 @@ describe('failOnError', function () {
|
||||
throw new Error('Expected rejection');
|
||||
})
|
||||
.catch(err => {
|
||||
done(err.message.includes('VipsJpeg: Premature end of JPEG file') ? undefined : err);
|
||||
done(err.message.includes('VipsJpeg: Premature end of') ? undefined : err);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -63,12 +63,17 @@ describe('GIF input', () => {
|
||||
);
|
||||
|
||||
if (!sharp.format.magick.output.buffer) {
|
||||
it('GIF output should fail due to missing ImageMagick', () => {
|
||||
it('GIF buffer output should fail due to missing ImageMagick', () => {
|
||||
assert.throws(
|
||||
() => {
|
||||
sharp().gif();
|
||||
},
|
||||
/The gif operation requires libvips to have been installed with support for ImageMagick/
|
||||
() => sharp().gif(),
|
||||
/GIF output requires libvips with support for ImageMagick/
|
||||
);
|
||||
});
|
||||
|
||||
it('GIF file output should fail due to missing ImageMagick', () => {
|
||||
assert.rejects(
|
||||
async () => await sharp().toFile('test.gif'),
|
||||
/GIF output requires libvips with support for ImageMagick/
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -98,4 +103,36 @@ describe('GIF input', () => {
|
||||
sharp().gif({ delay: [65536] });
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with streams when only animated is set', function (done) {
|
||||
if (sharp.format.magick.output.buffer) {
|
||||
fs.createReadStream(fixtures.inputGifAnimated)
|
||||
.pipe(sharp({ animated: true }))
|
||||
.gif()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual('gif', info.format);
|
||||
fixtures.assertSimilar(fixtures.inputGifAnimated, data, done);
|
||||
});
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should work with streams when only pages is set', function (done) {
|
||||
if (sharp.format.magick.output.buffer) {
|
||||
fs.createReadStream(fixtures.inputGifAnimated)
|
||||
.pipe(sharp({ pages: -1 }))
|
||||
.gif()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual('gif', info.format);
|
||||
fixtures.assertSimilar(fixtures.inputGifAnimated, data, done);
|
||||
});
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,76 +4,75 @@ const assert = require('assert');
|
||||
|
||||
const sharp = require('../../');
|
||||
|
||||
const formatHeifOutputBuffer = sharp.format.heif.output.buffer;
|
||||
|
||||
describe('HEIF (experimental)', () => {
|
||||
describe('Stubbed without support for HEIF', () => {
|
||||
before(() => {
|
||||
sharp.format.heif.output.buffer = false;
|
||||
});
|
||||
after(() => {
|
||||
sharp.format.heif.output.buffer = formatHeifOutputBuffer;
|
||||
});
|
||||
|
||||
it('should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif();
|
||||
});
|
||||
describe('HEIF', () => {
|
||||
it('called without options does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Stubbed with support for HEIF', () => {
|
||||
before(() => {
|
||||
sharp.format.heif.output.buffer = true;
|
||||
it('valid quality does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif({ quality: 80 });
|
||||
});
|
||||
after(() => {
|
||||
sharp.format.heif.output.buffer = formatHeifOutputBuffer;
|
||||
});
|
||||
it('invalid quality should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ quality: 101 });
|
||||
});
|
||||
|
||||
it('called without options does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif();
|
||||
});
|
||||
});
|
||||
it('non-numeric quality should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ quality: 'fail' });
|
||||
});
|
||||
it('valid quality does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif({ quality: 50 });
|
||||
});
|
||||
});
|
||||
it('valid lossless does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif({ lossless: true });
|
||||
});
|
||||
it('invalid quality should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ quality: 101 });
|
||||
});
|
||||
});
|
||||
it('non-boolean lossless should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ lossless: 'fail' });
|
||||
});
|
||||
it('non-numeric quality should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ quality: 'fail' });
|
||||
});
|
||||
});
|
||||
it('valid compression does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif({ compression: 'hevc' });
|
||||
});
|
||||
it('valid lossless does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif({ lossless: true });
|
||||
});
|
||||
});
|
||||
it('unknown compression should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ compression: 'fail' });
|
||||
});
|
||||
it('non-boolean lossless should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ lossless: 'fail' });
|
||||
});
|
||||
});
|
||||
it('invalid compression should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ compression: 1 });
|
||||
});
|
||||
it('valid compression does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif({ compression: 'avc' });
|
||||
});
|
||||
});
|
||||
it('valid speed does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif({ speed: 6 });
|
||||
});
|
||||
it('unknown compression should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ compression: 'fail' });
|
||||
});
|
||||
});
|
||||
it('out of range speed should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ speed: 9 });
|
||||
});
|
||||
it('invalid compression should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ compression: 1 });
|
||||
});
|
||||
});
|
||||
it('invalid speed should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ compression: 'fail' });
|
||||
});
|
||||
});
|
||||
it('invalid chromaSubsampling should throw an error', () => {
|
||||
assert.throws(() => {
|
||||
sharp().heif({ chromaSubsampling: 'fail' });
|
||||
});
|
||||
});
|
||||
it('valid chromaSubsampling does not throw an error', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
sharp().heif({ chromaSubsampling: '4:4:4' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
115
test/unit/io.js
@@ -7,6 +7,8 @@ const rimraf = require('rimraf');
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
const outputJpg = fixtures.path('output.jpg');
|
||||
|
||||
describe('Input/output', function () {
|
||||
beforeEach(function () {
|
||||
sharp.cache(false);
|
||||
@@ -16,16 +18,16 @@ describe('Input/output', function () {
|
||||
});
|
||||
|
||||
it('Read from File and write to Stream', function (done) {
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
const writable = fs.createWriteStream(outputJpg);
|
||||
writable.on('close', function () {
|
||||
sharp(fixtures.outputJpg).toBuffer(function (err, data, info) {
|
||||
sharp(outputJpg).toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual(data.length, info.size);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
rimraf(fixtures.outputJpg, done);
|
||||
rimraf(outputJpg, done);
|
||||
});
|
||||
});
|
||||
sharp(fixtures.inputJpg).resize(320, 240).pipe(writable);
|
||||
@@ -33,16 +35,16 @@ describe('Input/output', function () {
|
||||
|
||||
it('Read from Buffer and write to Stream', function (done) {
|
||||
const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg);
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
const writable = fs.createWriteStream(outputJpg);
|
||||
writable.on('close', function () {
|
||||
sharp(fixtures.outputJpg).toBuffer(function (err, data, info) {
|
||||
sharp(outputJpg).toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual(data.length, info.size);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
rimraf(fixtures.outputJpg, done);
|
||||
rimraf(outputJpg, done);
|
||||
});
|
||||
});
|
||||
sharp(inputJpgBuffer).resize(320, 240).pipe(writable);
|
||||
@@ -50,13 +52,13 @@ describe('Input/output', function () {
|
||||
|
||||
it('Read from Stream and write to File', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const pipeline = sharp().resize(320, 240).toFile(fixtures.outputJpg, function (err, info) {
|
||||
const pipeline = sharp().resize(320, 240).toFile(outputJpg, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
rimraf(fixtures.outputJpg, done);
|
||||
rimraf(outputJpg, done);
|
||||
});
|
||||
readable.pipe(pipeline);
|
||||
});
|
||||
@@ -131,25 +133,57 @@ describe('Input/output', function () {
|
||||
|
||||
it('Read from Stream and write to Stream', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
const writable = fs.createWriteStream(outputJpg);
|
||||
writable.on('close', function () {
|
||||
sharp(fixtures.outputJpg).toBuffer(function (err, data, info) {
|
||||
sharp(outputJpg).toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual(data.length, info.size);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
rimraf(fixtures.outputJpg, done);
|
||||
rimraf(outputJpg, done);
|
||||
});
|
||||
});
|
||||
const pipeline = sharp().resize(320, 240);
|
||||
readable.pipe(pipeline).pipe(writable);
|
||||
});
|
||||
|
||||
it('Read from Uint8Array and write to Buffer', async () => {
|
||||
const uint8array = Uint8Array.from([255, 255, 255, 0, 0, 0]);
|
||||
const { data, info } = await sharp(uint8array, {
|
||||
raw: {
|
||||
width: 2,
|
||||
height: 1,
|
||||
channels: 3
|
||||
}
|
||||
}).toBuffer({ resolveWithObject: true });
|
||||
|
||||
assert.deepStrictEqual(uint8array, new Uint8Array(data));
|
||||
assert.strictEqual(info.width, 2);
|
||||
assert.strictEqual(info.height, 1);
|
||||
});
|
||||
|
||||
it('Read from Uint8ClampedArray and output to Buffer', async () => {
|
||||
// since a Uint8ClampedArray is the same as Uint8Array but clamps the values
|
||||
// between 0-255 it seemed good to add this also
|
||||
const uint8array = Uint8ClampedArray.from([255, 255, 255, 0, 0, 0]);
|
||||
const { data, info } = await sharp(uint8array, {
|
||||
raw: {
|
||||
width: 2,
|
||||
height: 1,
|
||||
channels: 3
|
||||
}
|
||||
}).toBuffer({ resolveWithObject: true });
|
||||
|
||||
assert.deepStrictEqual(uint8array, new Uint8ClampedArray(data));
|
||||
assert.strictEqual(info.width, 2);
|
||||
assert.strictEqual(info.height, 1);
|
||||
});
|
||||
|
||||
it('Stream should emit info event', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
const writable = fs.createWriteStream(outputJpg);
|
||||
const pipeline = sharp().resize(320, 240);
|
||||
let infoEventEmitted = false;
|
||||
pipeline.on('info', function (info) {
|
||||
@@ -161,7 +195,7 @@ describe('Input/output', function () {
|
||||
});
|
||||
writable.on('close', function () {
|
||||
assert.strictEqual(true, infoEventEmitted);
|
||||
rimraf(fixtures.outputJpg, done);
|
||||
rimraf(outputJpg, done);
|
||||
});
|
||||
readable.pipe(pipeline).pipe(writable);
|
||||
});
|
||||
@@ -173,10 +207,10 @@ describe('Input/output', function () {
|
||||
anErrorWasEmitted = !!err;
|
||||
}).on('end', function () {
|
||||
assert(anErrorWasEmitted);
|
||||
rimraf(fixtures.outputJpg, done);
|
||||
rimraf(outputJpg, done);
|
||||
});
|
||||
const readableButNotAnImage = fs.createReadStream(__filename);
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
const writable = fs.createWriteStream(outputJpg);
|
||||
readableButNotAnImage.pipe(pipeline).pipe(writable);
|
||||
});
|
||||
|
||||
@@ -187,24 +221,24 @@ describe('Input/output', function () {
|
||||
anErrorWasEmitted = !!err;
|
||||
}).on('end', function () {
|
||||
assert(anErrorWasEmitted);
|
||||
rimraf(fixtures.outputJpg, done);
|
||||
rimraf(outputJpg, done);
|
||||
});
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
const writable = fs.createWriteStream(outputJpg);
|
||||
readableButNotAnImage.pipe(writable);
|
||||
});
|
||||
|
||||
it('Readable side of Stream can start flowing after Writable side has finished', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
const writable = fs.createWriteStream(outputJpg);
|
||||
writable.on('close', function () {
|
||||
sharp(fixtures.outputJpg).toBuffer(function (err, data, info) {
|
||||
sharp(outputJpg).toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual(data.length, info.size);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
rimraf(fixtures.outputJpg, done);
|
||||
rimraf(outputJpg, done);
|
||||
});
|
||||
});
|
||||
const pipeline = sharp().resize(320, 240);
|
||||
@@ -302,7 +336,6 @@ describe('Input/output', function () {
|
||||
});
|
||||
|
||||
it('Fail when input is empty Buffer', function (done) {
|
||||
if (sharp.format.magick.input.buffer) return this.skip(); // can be removed with libvips 8.10.0+
|
||||
sharp(Buffer.alloc(0)).toBuffer().then(function () {
|
||||
assert(false);
|
||||
done();
|
||||
@@ -412,68 +445,70 @@ describe('Input/output', function () {
|
||||
});
|
||||
|
||||
describe('Output filename with unknown extension', function () {
|
||||
const outputZoinks = fixtures.path('output.zoinks');
|
||||
|
||||
it('Match JPEG input', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 80)
|
||||
.toFile(fixtures.outputZoinks, function (err, info) {
|
||||
.toFile(outputZoinks, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
rimraf(fixtures.outputZoinks, done);
|
||||
rimraf(outputZoinks, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Match PNG input', function (done) {
|
||||
sharp(fixtures.inputPng)
|
||||
.resize(320, 80)
|
||||
.toFile(fixtures.outputZoinks, function (err, info) {
|
||||
.toFile(outputZoinks, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
rimraf(fixtures.outputZoinks, done);
|
||||
rimraf(outputZoinks, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Match WebP input', function (done) {
|
||||
sharp(fixtures.inputWebP)
|
||||
.resize(320, 80)
|
||||
.toFile(fixtures.outputZoinks, function (err, info) {
|
||||
.toFile(outputZoinks, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
assert.strictEqual('webp', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
rimraf(fixtures.outputZoinks, done);
|
||||
rimraf(outputZoinks, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Match TIFF input', function (done) {
|
||||
sharp(fixtures.inputTiff)
|
||||
.resize(320, 80)
|
||||
.toFile(fixtures.outputZoinks, function (err, info) {
|
||||
.toFile(outputZoinks, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
rimraf(fixtures.outputZoinks, done);
|
||||
rimraf(outputZoinks, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Autoconvert GIF input to PNG output', function (done) {
|
||||
sharp(fixtures.inputGif)
|
||||
.resize(320, 80)
|
||||
.toFile(fixtures.outputZoinks, function (err, info) {
|
||||
.toFile(outputZoinks, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
assert.strictEqual(sharp.format.magick.input.buffer ? 'gif' : 'png', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
rimraf(fixtures.outputZoinks, done);
|
||||
rimraf(outputZoinks, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -481,13 +516,13 @@ describe('Input/output', function () {
|
||||
sharp(fixtures.inputPng)
|
||||
.resize(320, 80)
|
||||
.jpeg()
|
||||
.toFile(fixtures.outputZoinks, function (err, info) {
|
||||
.toFile(outputZoinks, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
rimraf(fixtures.outputZoinks, done);
|
||||
rimraf(outputZoinks, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -517,19 +552,20 @@ describe('Input/output', function () {
|
||||
});
|
||||
|
||||
it('toFormat=JPEG takes precedence over WebP extension', function (done) {
|
||||
const outputWebP = fixtures.path('output.webp');
|
||||
sharp(fixtures.inputPng)
|
||||
.jpeg()
|
||||
.toFile(fixtures.outputWebP, function (err, info) {
|
||||
.toFile(outputWebP, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
done();
|
||||
rimraf(outputWebP, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('toFormat=WebP takes precedence over JPEG extension', function (done) {
|
||||
sharp(fixtures.inputPng)
|
||||
.webp()
|
||||
.toFile(fixtures.outputJpg, function (err, info) {
|
||||
.toFile(outputJpg, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('webp', info.format);
|
||||
done();
|
||||
@@ -550,15 +586,16 @@ describe('Input/output', function () {
|
||||
});
|
||||
|
||||
it('Save Vips V file', function (done) {
|
||||
const outputV = fixtures.path('output.v');
|
||||
sharp(fixtures.inputJpg)
|
||||
.extract({ left: 910, top: 1105, width: 70, height: 60 })
|
||||
.toFile(fixtures.outputV, function (err, info) {
|
||||
.toFile(outputV, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
assert.strictEqual('v', info.format);
|
||||
assert.strictEqual(70, info.width);
|
||||
assert.strictEqual(60, info.height);
|
||||
rimraf(fixtures.outputV, done);
|
||||
rimraf(outputV, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -647,7 +684,7 @@ describe('Input/output', function () {
|
||||
it('Invalid density: string', function () {
|
||||
assert.throws(function () {
|
||||
sharp({ density: 'zoinks' });
|
||||
}, /Expected number between 1 and 2400 for density but received zoinks of type string/);
|
||||
}, /Expected number between 1 and 100000 for density but received zoinks of type string/);
|
||||
});
|
||||
it('Setting animated property updates pages property', function () {
|
||||
assert.strictEqual(sharp({ animated: false }).options.input.pages, 1);
|
||||
|
||||
@@ -504,6 +504,7 @@ describe('Image metadata', function () {
|
||||
it('Apply CMYK output ICC profile', function (done) {
|
||||
const output = fixtures.path('output.icc-cmyk.jpg');
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(64)
|
||||
.withMetadata({ icc: 'cmyk' })
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
@@ -528,11 +529,11 @@ describe('Image metadata', function () {
|
||||
it('Apply custom output ICC profile', function (done) {
|
||||
const output = fixtures.path('output.hilutite.jpg');
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(64)
|
||||
.withMetadata({ icc: fixtures.path('hilutite.icm') })
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
fixtures.assertMaxColourDistance(output, fixtures.path('expected/hilutite.jpg'), 0);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.inputJpg, 16.5);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.path('expected/hilutite.jpg'), 9);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -667,7 +668,7 @@ describe('Image metadata', function () {
|
||||
sharp(fixtures.inputJpgWithCorruptHeader)
|
||||
.metadata(function (err) {
|
||||
assert.strictEqual(true, !!err);
|
||||
assert.strictEqual(true, /Input file has corrupt header: VipsJpeg: Premature end of JPEG file/.test(err.message));
|
||||
assert.ok(err.message.includes('Input file has corrupt header: VipsJpeg: Premature end of'), err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -676,7 +677,7 @@ describe('Image metadata', function () {
|
||||
sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader))
|
||||
.metadata(function (err) {
|
||||
assert.strictEqual(true, !!err);
|
||||
assert.strictEqual(true, /Input buffer has corrupt header: VipsJpeg: Premature end of JPEG file/.test(err.message));
|
||||
assert.ok(err.message.includes('Input buffer has corrupt header: VipsJpeg: Premature end of'), err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
258
test/unit/noise.js
Normal file
@@ -0,0 +1,258 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('Gaussian noise', function () {
|
||||
it('generate single-channel gaussian noise', function (done) {
|
||||
const output = fixtures.path('output.noise-1-channel.png');
|
||||
const noise = sharp({
|
||||
create: {
|
||||
width: 1024,
|
||||
height: 768,
|
||||
channels: 1, // b-w
|
||||
noise: {
|
||||
type: 'gaussian',
|
||||
mean: 128,
|
||||
sigma: 30
|
||||
}
|
||||
}
|
||||
});
|
||||
noise.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(1024, info.width);
|
||||
assert.strictEqual(768, info.height);
|
||||
assert.strictEqual(1, info.channels);
|
||||
sharp(output).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('b-w', metadata.space);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('generate 3-channels gaussian noise', function (done) {
|
||||
const output = fixtures.path('output.noise-3-channels.png');
|
||||
const noise = sharp({
|
||||
create: {
|
||||
width: 1024,
|
||||
height: 768,
|
||||
channels: 3, // sRGB
|
||||
noise: {
|
||||
type: 'gaussian',
|
||||
mean: 128,
|
||||
sigma: 30
|
||||
}
|
||||
}
|
||||
});
|
||||
noise.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(1024, info.width);
|
||||
assert.strictEqual(768, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
sharp(output).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('overlay 3-channels gaussian noise over image', function (done) {
|
||||
const output = fixtures.path('output.noise-image.jpg');
|
||||
const noise = sharp({
|
||||
create: {
|
||||
width: 320,
|
||||
height: 240,
|
||||
channels: 3,
|
||||
noise: {
|
||||
type: 'gaussian',
|
||||
mean: 0,
|
||||
sigma: 5
|
||||
}
|
||||
}
|
||||
});
|
||||
noise.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(3, info.channels);
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.composite([
|
||||
{
|
||||
input: data,
|
||||
blend: 'exclusion',
|
||||
raw: {
|
||||
width: info.width,
|
||||
height: info.height,
|
||||
channels: info.channels
|
||||
}
|
||||
}
|
||||
])
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
// perceptual hashing detects that images are the same (difference is <=1%)
|
||||
fixtures.assertSimilar(output, fixtures.inputJpg, function (err) {
|
||||
if (err) throw err;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('overlay strong single-channel (sRGB) gaussian noise with 25% transparency over transparent png image', function (done) {
|
||||
const output = fixtures.path('output.noise-image-transparent.png');
|
||||
const width = 320;
|
||||
const height = 240;
|
||||
const rawData = {
|
||||
width,
|
||||
height,
|
||||
channels: 1
|
||||
};
|
||||
const noise = sharp({
|
||||
create: {
|
||||
width,
|
||||
height,
|
||||
channels: 1,
|
||||
noise: {
|
||||
type: 'gaussian',
|
||||
mean: 200,
|
||||
sigma: 30
|
||||
}
|
||||
}
|
||||
});
|
||||
noise
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(1, info.channels);
|
||||
sharp(data, { raw: rawData })
|
||||
.joinChannel(data, { raw: rawData }) // r channel
|
||||
.joinChannel(data, { raw: rawData }) // b channel
|
||||
.joinChannel(Buffer.alloc(width * height, 64), { raw: rawData }) // alpha channel
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(4, info.channels);
|
||||
sharp(fixtures.inputPngRGBWithAlpha)
|
||||
.resize(width, height)
|
||||
.composite([
|
||||
{
|
||||
input: data,
|
||||
blend: 'exclusion',
|
||||
raw: {
|
||||
width: info.width,
|
||||
height: info.height,
|
||||
channels: info.channels
|
||||
}
|
||||
}
|
||||
])
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(width, info.width);
|
||||
assert.strictEqual(height, info.height);
|
||||
assert.strictEqual(4, info.channels);
|
||||
fixtures.assertSimilar(output, fixtures.inputPngRGBWithAlpha, { threshold: 10 }, function (err) {
|
||||
if (err) throw err;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('no create object properties specified', function () {
|
||||
assert.throws(function () {
|
||||
sharp({
|
||||
create: {}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('invalid noise object', function () {
|
||||
assert.throws(function () {
|
||||
sharp({
|
||||
create: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
channels: 3,
|
||||
noise: 'gaussian'
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('unknown type of noise', function () {
|
||||
assert.throws(function () {
|
||||
sharp({
|
||||
create: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
channels: 3,
|
||||
noise: {
|
||||
type: 'unknown'
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('gaussian noise, invalid amount of channels', function () {
|
||||
assert.throws(function () {
|
||||
sharp({
|
||||
create: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
channels: 5,
|
||||
noise: {
|
||||
type: 'gaussian',
|
||||
mean: 5,
|
||||
sigma: 10
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('gaussian noise, invalid mean', function () {
|
||||
assert.throws(function () {
|
||||
sharp({
|
||||
create: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
channels: 1,
|
||||
noise: {
|
||||
type: 'gaussian',
|
||||
mean: -1.5,
|
||||
sigma: 10
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('gaussian noise, invalid sigma', function () {
|
||||
assert.throws(function () {
|
||||
sharp({
|
||||
create: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
channels: 1,
|
||||
noise: {
|
||||
type: 'gaussian',
|
||||
mean: 0,
|
||||
sigma: -1.5
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,15 +5,17 @@ const assert = require('assert');
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
const sepia = [
|
||||
[0.3588, 0.7044, 0.1368],
|
||||
[0.299, 0.587, 0.114],
|
||||
[0.2392, 0.4696, 0.0912]
|
||||
];
|
||||
|
||||
describe('Recomb', function () {
|
||||
it('applies a sepia filter using recomb', function (done) {
|
||||
const output = fixtures.path('output.recomb-sepia.jpg');
|
||||
sharp(fixtures.inputJpgWithLandscapeExif1)
|
||||
.recomb([
|
||||
[0.3588, 0.7044, 0.1368],
|
||||
[0.299, 0.587, 0.114],
|
||||
[0.2392, 0.4696, 0.0912]
|
||||
])
|
||||
.recomb(sepia)
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
@@ -31,11 +33,7 @@ describe('Recomb', function () {
|
||||
it('applies a sepia filter using recomb to an PNG with Alpha', function (done) {
|
||||
const output = fixtures.path('output.recomb-sepia.png');
|
||||
sharp(fixtures.inputPngAlphaPremultiplicationSmall)
|
||||
.recomb([
|
||||
[0.3588, 0.7044, 0.1368],
|
||||
[0.299, 0.587, 0.114],
|
||||
[0.2392, 0.4696, 0.0912]
|
||||
])
|
||||
.recomb(sepia)
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
@@ -50,6 +48,20 @@ describe('Recomb', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('recomb with a single channel input', async () => {
|
||||
const { info } = await sharp(Buffer.alloc(64), {
|
||||
raw: {
|
||||
width: 8,
|
||||
height: 8,
|
||||
channels: 1
|
||||
}
|
||||
})
|
||||
.recomb(sepia)
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
|
||||
assert.strictEqual(3, info.channels);
|
||||
});
|
||||
|
||||
it('applies a different sepia filter using recomb', function (done) {
|
||||
const output = fixtures.path('output.recomb-sepia2.jpg');
|
||||
sharp(fixtures.inputJpgWithLandscapeExif1)
|
||||
|
||||
@@ -7,7 +7,7 @@ const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
// Test Helpers
|
||||
var threshold = 0.001;
|
||||
const threshold = 0.001;
|
||||
function isInAcceptableRange (actual, expected) {
|
||||
return actual >= ((1 - threshold) * expected) && actual <= ((1 + threshold) * expected);
|
||||
}
|
||||
@@ -279,10 +279,10 @@ describe('Image Stats', function () {
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0].min);
|
||||
assert.strictEqual(true, isInRange(stats.channels[0].max, 254, 255));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[0].sum, 82506996));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[0].squaresSum, 11213984832));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[0].mean, 104.36947963892487));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[0].stdev, 57.379896254993135));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[0].sum, 83291370));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[0].squaresSum, 11379783198));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[0].mean, 105.36169496842616));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[0].stdev, 57.39412151419967));
|
||||
assert.strictEqual(true, isInteger(stats.channels[0].minX));
|
||||
assert.strictEqual(true, isInRange(stats.channels[0].minX, 0, 1024));
|
||||
assert.strictEqual(true, isInteger(stats.channels[0].minY));
|
||||
@@ -295,10 +295,10 @@ describe('Image Stats', function () {
|
||||
// green channel
|
||||
assert.strictEqual(0, stats.channels[1].min);
|
||||
assert.strictEqual(true, isInRange(stats.channels[1].max, 254, 255));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[1].sum, 120089056));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[1].squaresSum, 20533721114));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[1].mean, 151.90993361398964));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[1].stdev, 53.83370206587037));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[1].sum, 120877425));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[1].squaresSum, 20774687595));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[1].mean, 152.9072025279307));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[1].stdev, 53.84143349689916));
|
||||
assert.strictEqual(true, isInteger(stats.channels[1].minX));
|
||||
assert.strictEqual(true, isInRange(stats.channels[1].minX, 0, 1024));
|
||||
assert.strictEqual(true, isInteger(stats.channels[1].minY));
|
||||
@@ -311,10 +311,10 @@ describe('Image Stats', function () {
|
||||
// blue channel
|
||||
assert.strictEqual(0, stats.channels[2].min);
|
||||
assert.strictEqual(true, isInRange(stats.channels[2].max, 254, 255));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[2].sum, 138153653));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[2].squaresSum, 28172033081));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[2].mean, 174.76123932359133));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[2].stdev, 71.38276338513747));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[2].sum, 138938859));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[2].squaresSum, 28449125593));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[2].mean, 175.75450711423252));
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.channels[2].stdev, 71.39929031070358));
|
||||
assert.strictEqual(true, isInteger(stats.channels[2].minX));
|
||||
assert.strictEqual(true, isInRange(stats.channels[2].minX, 0, 1024));
|
||||
assert.strictEqual(true, isInteger(stats.channels[2].minY));
|
||||
@@ -462,6 +462,19 @@ describe('Image Stats', function () {
|
||||
})
|
||||
);
|
||||
|
||||
it('Entropy and sharpness of 1x1 input are zero', async () => {
|
||||
const { entropy, sharpness } = await sharp({
|
||||
create: {
|
||||
width: 1,
|
||||
height: 1,
|
||||
channels: 3,
|
||||
background: 'red'
|
||||
}
|
||||
}).stats();
|
||||
assert.strictEqual(entropy, 0);
|
||||
assert.strictEqual(sharpness, 0);
|
||||
});
|
||||
|
||||
it('Stream in, Callback out', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const pipeline = sharp().stats(function (err, stats) {
|
||||
|
||||
@@ -48,6 +48,31 @@ describe('SVG input', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Convert SVG to PNG at DPI larger than 2400', function (done) {
|
||||
const size = 1024;
|
||||
sharp(fixtures.inputSvgSmallViewBox).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
const density = (size / Math.max(metadata.width, metadata.height)) * metadata.density;
|
||||
sharp(fixtures.inputSvgSmallViewBox, { density })
|
||||
.resize(size)
|
||||
.toFormat('png')
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(size, info.width);
|
||||
assert.strictEqual(size, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('circle.png'), data, function (err) {
|
||||
if (err) throw err;
|
||||
sharp(data).metadata(function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(9216, info.density);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Convert SVG to PNG at 14.4DPI', function (done) {
|
||||
sharp(fixtures.inputSvg, { density: 14.4 })
|
||||
.toFormat('png')
|
||||
|
||||
@@ -8,6 +8,8 @@ const rimraf = require('rimraf');
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
const outputTiff = fixtures.path('output.tiff');
|
||||
|
||||
describe('TIFF', function () {
|
||||
it('Load TIFF from Buffer', function (done) {
|
||||
const inputTiffBuffer = fs.readFileSync(fixtures.inputTiff);
|
||||
@@ -120,11 +122,11 @@ describe('TIFF', function () {
|
||||
compression: 'none',
|
||||
predictor: 'none'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert.strictEqual(startSize, info.size);
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -137,11 +139,11 @@ describe('TIFF', function () {
|
||||
compression: 'none',
|
||||
predictor: 'none'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < (startSize / 2));
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -157,12 +159,12 @@ describe('TIFF', function () {
|
||||
xres: 1000,
|
||||
yres: 1000
|
||||
})
|
||||
.toFile(fixtures.outputTiff)
|
||||
.then(() => sharp(fixtures.outputTiff)
|
||||
.toFile(outputTiff)
|
||||
.then(() => sharp(outputTiff)
|
||||
.metadata()
|
||||
.then(({ density }) => {
|
||||
assert.strictEqual(25400, density);
|
||||
return promisify(rimraf)(fixtures.outputTiff);
|
||||
return promisify(rimraf)(outputTiff);
|
||||
})
|
||||
)
|
||||
);
|
||||
@@ -201,12 +203,12 @@ describe('TIFF', function () {
|
||||
compression: 'lzw',
|
||||
predictor: 'horizontal'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert(info.size < startSize);
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -222,7 +224,7 @@ describe('TIFF', function () {
|
||||
.tiff({
|
||||
compression: 'lzw'
|
||||
})
|
||||
.toFile(fixtures.outputTiff)
|
||||
.toFile(outputTiff)
|
||||
.then(info => {
|
||||
assert.strictEqual(4, info.channels);
|
||||
})
|
||||
@@ -254,11 +256,11 @@ describe('TIFF', function () {
|
||||
bitdepth: 1,
|
||||
compression: 'ccittfax4'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -269,26 +271,26 @@ describe('TIFF', function () {
|
||||
compression: 'deflate',
|
||||
predictor: 'horizontal'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF deflate compression with float predictor shrinks test file', function (done) {
|
||||
it('TIFF deflate compression of integral input with float predictor increases file size', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
|
||||
sharp(fixtures.inputTiffUncompressed)
|
||||
.tiff({
|
||||
compression: 'deflate',
|
||||
predictor: 'float'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
assert(info.size > startSize);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -299,11 +301,11 @@ describe('TIFF', function () {
|
||||
compression: 'deflate',
|
||||
predictor: 'none'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -313,11 +315,11 @@ describe('TIFF', function () {
|
||||
.tiff({
|
||||
compression: 'jpeg'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -385,11 +387,11 @@ describe('TIFF', function () {
|
||||
tileHeight: 256,
|
||||
tileWidth: 256
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
.toFile(outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size > startSize);
|
||||
rimraf(fixtures.outputTiff, done);
|
||||
rimraf(outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -289,6 +289,14 @@ describe('Tile', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid center parameter value fail', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tile({
|
||||
centre: 'true'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Deep Zoom layout', function (done) {
|
||||
const directory = fixtures.path('output.dzi_files');
|
||||
rimraf(directory, function () {
|
||||
@@ -765,6 +773,46 @@ describe('Tile', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout with center image in tile', function (done) {
|
||||
const directory = fixtures.path('output.google_center.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
center: true,
|
||||
layout: 'google'
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
fixtures.assertSimilar(fixtures.expected('tile_centered.jpg'), fs.readFileSync(path.join(directory, '0', '0', '0.jpg')), done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout with center image in tile centre', function (done) {
|
||||
const directory = fixtures.path('output.google_center.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
centre: true,
|
||||
layout: 'google'
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
fixtures.assertSimilar(fixtures.expected('tile_centered.jpg'), fs.readFileSync(path.join(directory, '0', '0', '0.jpg')), done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('IIIF layout', function (done) {
|
||||
const directory = fixtures.path('output.iiif.info');
|
||||
rimraf(directory, function () {
|
||||
|
||||
@@ -6,14 +6,19 @@ const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('toBuffer', () => {
|
||||
it('reusing same sharp object does not reset previously passed parameters to toBuffer', (done) => {
|
||||
it('reusing same sharp object does not reset previously passed parameters to toBuffer', async () => {
|
||||
const image = sharp(fixtures.inputJpg);
|
||||
image.toBuffer({ resolveWithObject: true }).then((obj) => {
|
||||
image.toBuffer().then((buff) => {
|
||||
assert.strictEqual(Buffer.isBuffer(buff), true);
|
||||
assert.strictEqual(typeof obj, 'object');
|
||||
done();
|
||||
});
|
||||
});
|
||||
const obj = await image.toBuffer({ resolveWithObject: true });
|
||||
assert.strictEqual(typeof obj, 'object');
|
||||
assert.strictEqual(typeof obj.info, 'object');
|
||||
assert.strictEqual(Buffer.isBuffer(obj.data), true);
|
||||
const data = await image.toBuffer();
|
||||
assert.strictEqual(Buffer.isBuffer(data), true);
|
||||
});
|
||||
|
||||
it('correctly process animated webp with height > 16383', async () => {
|
||||
const data = await sharp(fixtures.inputWebPAnimatedBigHeight, { animated: true })
|
||||
.toBuffer();
|
||||
assert.strictEqual(Buffer.isBuffer(data), true);
|
||||
});
|
||||
});
|
||||
|
||||
26
test/unit/toFormat.js
Normal file
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('toFormat', () => {
|
||||
it('accepts upper case characters as format parameter (string)', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.toFormat('PNG')
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('accepts upper case characters as format parameter (object)', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.toFormat({ id: 'PNG' })
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const assert = require('assert');
|
||||
|
||||
const sharp = require('../../');
|
||||
@@ -184,4 +185,28 @@ describe('WebP', function () {
|
||||
|
||||
assert.deepStrictEqual(updated.delay, expectedDelay);
|
||||
});
|
||||
|
||||
it('should work with streams when only animated is set', function (done) {
|
||||
fs.createReadStream(fixtures.inputWebPAnimated)
|
||||
.pipe(sharp({ animated: true }))
|
||||
.webp({ lossless: true })
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual('webp', info.format);
|
||||
fixtures.assertSimilar(fixtures.inputWebPAnimated, data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with streams when only pages is set', function (done) {
|
||||
fs.createReadStream(fixtures.inputWebPAnimated)
|
||||
.pipe(sharp({ pages: -1 }))
|
||||
.webp({ lossless: true })
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual('webp', info.format);
|
||||
fixtures.assertSimilar(fixtures.inputWebPAnimated, data, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||